Skip to content

Commit eeb20b8

Browse files
author
hartsantler
committed
dart backend: added function calls with keyword defaults
1 parent a4611f2 commit eeb20b8

4 files changed

Lines changed: 54 additions & 11 deletions

File tree

pythonjs/python_to_pythonjs.py

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,7 +1574,8 @@ def visit_Call(self, node):
15741574

15751575
if name in self._generator_functions:
15761576
return ' new %s(%s)' %(name, ','.join(args))
1577-
if self._with_dart and name in self._builtin_functions_dart:
1577+
1578+
elif self._with_dart and name in self._builtin_functions_dart:
15781579
if args:
15791580
return self._builtin_functions_dart[name] % ','.join(args)
15801581
else:
@@ -1615,11 +1616,13 @@ def visit_Call(self, node):
16151616

16161617
else:
16171618
a = ','.join(args)
1618-
return '%s(%s)' %( self.visit(node.func), a )
1619+
if node.keywords:
1620+
args.extend( [self.visit(x.value) for x in node.keywords] )
1621+
return '%s(%s)' %( self.visit(node.func), ','.join(args) )
1622+
1623+
else:
1624+
return '%s(%s)' %( self.visit(node.func), ','.join(args) )
16191625

1620-
#elif isinstance(node.func, Name) and node.func.id == 'JS': ## avoids nested JS
1621-
# assert len(args) == 1
1622-
# return node.args[0].s ## string literal
16231626

16241627
elif isinstance(node.func, Name) and node.func.id in self._js_classes:
16251628
a = ','.join(args)
@@ -1628,9 +1631,29 @@ def visit_Call(self, node):
16281631
elif name in self._global_functions and self._with_inline:
16291632
return self.inline_function( node )
16301633

1631-
else:
1632-
a = ','.join(args)
1633-
return '%s(%s)' %( self.visit(node.func), a )
1634+
elif self._with_dart: ## DART
1635+
args
1636+
if node.keywords:
1637+
kwargs = ','.join( ['%s:%s'%(x.arg, self.visit(x.value)) for x in node.keywords] )
1638+
if args:
1639+
return '%s(%s, JS("%s"))' %( self.visit(node.func), ','.join(args), kwargs )
1640+
else:
1641+
return '%s( JS("%s"))' %( self.visit(node.func), kwargs )
1642+
1643+
else:
1644+
a = ','.join(args)
1645+
return '%s(%s)' %( self.visit(node.func), a )
1646+
1647+
else: ## javascript mode
1648+
if node.keywords:
1649+
args.extend( [self.visit(x.value) for x in node.keywords] )
1650+
return '%s(%s)' %( self.visit(node.func), ','.join(args) )
1651+
1652+
else:
1653+
return '%s(%s)' %( self.visit(node.func), ','.join(args) )
1654+
1655+
#a = ','.join(args)
1656+
#return '%s(%s)' %( self.visit(node.func), a )
16341657

16351658
elif isinstance(node.func, Name) and node.func.id in self._generator_functions:
16361659
name = self.visit(node.func)

pythonjs/pythonjs_to_dart.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def collect_names(node):
3030
class DartGenerator( pythonjs.JSGenerator ):
3131
_classes = dict()
3232
_class_props = dict()
33+
_raw_dict = False
3334

3435
def _visit_subscript_ellipsis(self, node):
3536
name = self.visit(node.value)
@@ -45,7 +46,10 @@ def visit_Dict(self, node):
4546
v = self.visit( node.values[i] )
4647
a.append( '%s:%s'%(k,v) )
4748
b = ','.join( a )
48-
return 'new dict( {%s} )' %b
49+
if self._raw_dict:
50+
return '{%s}' %b
51+
else:
52+
return 'new dict( {%s} )' %b
4953

5054
def visit_ClassDef(self, node):
5155
node._parents = set()
@@ -312,12 +316,13 @@ def _visit_function(self, node):
312316
dindex = i - offset
313317
if dindex >= 0 and node.args.defaults:
314318
default_value = self.visit( node.args.defaults[dindex] )
315-
oargs.append( '%s=%s' %(a, default_value) )
319+
oargs.append( '%s:%s' %(a, default_value) )
316320
else:
317321
args.append( a )
318322

319323
if oargs:
320-
args.append( '[%s]' % ','.join(oargs) )
324+
#args.append( '[%s]' % ','.join(oargs) )
325+
args.append( '{%s}' % ','.join(oargs) )
321326

322327
buffer = self.indent()
323328
if hasattr(node,'_prefix'): buffer += node._prefix + ' '

regtests/calling/args.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""simple function call"""
2+
def f(a, b, c):
3+
return a+b+c
4+
5+
def main():
6+
TestError( f(1,2,3) == 6)
7+

regtests/calling/keyword.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""keywords"""
2+
def f(a, b=None, c=None):
3+
return (a+b) * c
4+
5+
def main():
6+
TestError( f(1, b=2, c=3) == 9) ## inorder works in javascript mode
7+
TestError( f(1, c=3, b=2) == 9) ## out of order fails in javascript mode
8+

0 commit comments

Comments
 (0)