Permalink
Browse files

added support for [] subscript operator overloading ( __getitem__ ).

  • Loading branch information...
hartsantler committed Sep 23, 2013
1 parent d1ff684 commit e3e61088498a581b7359719d5cbd01073a81e865
Showing with 18 additions and 4 deletions.
  1. +18 −4 pythonscript/python_to_pythonjs.py
@@ -205,11 +205,25 @@ def visit_Attribute(self, node):
else:
return 'get_attribute(%s, "%s")' % (name, node.attr)
+ def visit_Index(self, node):
+ return self.visit(node.value)
+
def visit_Subscript(self, node):
- return 'get_attribute(%s, "__getitem__")([%s], JSObject())' % (
- self.visit(node.value),
- self.visit(node.slice),
- )
+ name = self.visit(node.value)
+ if name in self._instances: ## support x[y] operator overloading
+ klass = self._instances[ name ]
+ if '__getitem__' in self._classes[ klass ]:
+ return '__%s___getitem__( [%s, %s] )' % (klass, name, self.visit(node.slice))
+ else:
+ return 'get_attribute(%s, "__getitem__")([%s], JSObject())' % (
+ self.visit(node.value),
+ self.visit(node.slice),
+ )
+ else:
+ return 'get_attribute(%s, "__getitem__")([%s], JSObject())' % (
+ self.visit(node.value),
+ self.visit(node.slice),
+ )
def visit_Slice(self, node):
return "get_attribute(Slice, '__call__')([%s, %s, %s], JSObject())" % (self.visit(node.lower), self.visit(node.upper), self.visit(node.step))

0 comments on commit e3e6108

Please sign in to comment.