Skip to content

Commit eb0a714

Browse files
author
hartsantler
committed
lua backend: fixed calling functions with keyword args.
1 parent 0a79f88 commit eb0a714

2 files changed

Lines changed: 47 additions & 26 deletions

File tree

pythonjs/python_to_pythonjs.py

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2049,36 +2049,39 @@ def visit_FunctionDef(self, node):
20492049
writer.write("""JS("var %s = args[ %s ]")""" % (arg.id, i))
20502050

20512051
elif self._with_lua:
2052+
writer.write( 'var(%s)' %','.join([arg.id for arg in node.args.args]))
2053+
offset = len(node.args.args) - len(node.args.defaults)
20522054
for i,arg in enumerate(node.args.args):
2053-
writer.write( '%s = args[ %s ]' %(arg.id, i) )
2055+
dindex = i - offset
2056+
if dindex >= 0 and node.args.defaults:
2057+
default_value = self.visit( node.args.defaults[dindex] )
2058+
writer.write("%s = kwargs.%s or %s" % (arg.id, arg.id, default_value))
2059+
else:
2060+
writer.write( "%s = args[ %s ]" %(arg.id, i) )
20542061

20552062
elif len(node.args.defaults) or len(node.args.args) or node.args.vararg or node.args.kwarg:
2063+
# First check the arguments are well formed
2064+
# ie. that this function is not a callback of javascript code
2065+
writer.write("""if (JS('args instanceof Array') and JS("{}.toString.call(kwargs) === '[object Object]'") and arguments.length == 2):""")
2066+
# XXX: there is bug in the underlying translator preventing me to write the condition
2067+
# in a more readble way... something to do with brakects...
2068+
writer.push()
2069+
writer.write('pass') # do nothing if it's not called from javascript
2070+
writer.pull()
2071+
writer.write('else:')
2072+
writer.push()
2073+
# If it's the case, move use ``arguments`` to ``args``
2074+
writer.write('args = Array.prototype.slice.call(arguments)')
2075+
# This means you can't pass keyword argument from javascript but we already knew that
2076+
writer.write('kwargs = JSObject()')
2077+
writer.pull()
2078+
# done with pythonjs function used as callback of Python code
20562079

2057-
if self._with_lua:
2058-
pass
2059-
else:
2060-
# First check the arguments are well formed
2061-
# ie. that this function is not a callback of javascript code
2062-
writer.write("""if (JS('args instanceof Array') and JS("{}.toString.call(kwargs) === '[object Object]'") and arguments.length == 2):""")
2063-
# XXX: there is bug in the underlying translator preventing me to write the condition
2064-
# in a more readble way... something to do with brakects...
2065-
writer.push()
2066-
writer.write('pass') # do nothing if it's not called from javascript
2067-
writer.pull()
2068-
writer.write('else:')
2069-
writer.push()
2070-
# If it's the case, move use ``arguments`` to ``args``
2071-
writer.write('args = Array.prototype.slice.call(arguments)')
2072-
# This means you can't pass keyword argument from javascript but we already knew that
2073-
writer.write('kwargs = JSObject()')
2074-
writer.pull()
2075-
# done with pythonjs function used as callback of Python code
2076-
2077-
# new pythonjs' python function arguments handling
2078-
# create the structure representing the functions arguments
2079-
# first create the defaultkwargs JSObject
2080-
if not self._with_coffee:
2081-
writer.write('var(__sig__, __args__)')
2080+
# new pythonjs' python function arguments handling
2081+
# create the structure representing the functions arguments
2082+
# first create the defaultkwargs JSObject
2083+
if not self._with_coffee:
2084+
writer.write('var(__sig__, __args__)')
20822085

20832086
L = len(node.args.defaults)
20842087
kwargsdefault = map(lambda x: keyword(self.visit(x[0]), x[1]), zip(node.args.args[-L:], node.args.defaults))

pythonjs/pythonjs_to_lua.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,24 @@ class LuaGenerator( pythonjs.JSGenerator ):
3131
_classes = dict()
3232
_class_props = dict()
3333

34+
def visit_Name(self, node):
35+
if node.id == 'None':
36+
return 'nil'
37+
elif node.id == 'True':
38+
return 'true'
39+
elif node.id == 'False':
40+
return 'false'
41+
elif node.id == 'null':
42+
return 'nil'
43+
return node.id
44+
45+
def visit_And(self, node):
46+
return ' and '
47+
48+
def visit_Or(self, node):
49+
return ' or '
50+
51+
3452
def visit_Subscript(self, node):
3553
if isinstance(node.slice, ast.Ellipsis):
3654
return self._visit_subscript_ellipsis( node )

0 commit comments

Comments
 (0)