@@ -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 ))
0 commit comments