Skip to content

Commit 47dc524

Browse files
author
hartsantler
committed
fixed bug in keyword defaults in javascript mode.
new ternary logic speeds up "+" addition/concatenate operator (by checking if the first variable is a number) added PyPy to regression tests/benchmarks release version 0.9.2
1 parent b4a588e commit 47dc524

9 files changed

Lines changed: 237 additions & 88 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
##PythonJS 0.9.1
1+
##PythonJS 0.9.2
22

33
![logo](http://3.bp.blogspot.com/-BfPFXT-DF3A/UqKugvWVs7I/AAAAAAAAAj0/0Kon76_VDys/s400/pythonjs-0.8.6.png)
44

pythonjs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"author": "Brett Hartshorn <goatman.py@gmail.com>",
33
"name": "python-js",
44
"description": "python multi-translator: javascript, dart, coffee, lua, vis.js",
5-
"version": "0.9.1",
5+
"version": "0.9.2",
66
"license": "BSD-3-Clause",
77
"repository": {
88
"type": "git",

pythonjs/python_to_pythonjs.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ def __init__(self, source=None, module=None, module_path=None, dart=False, coffe
206206
self._with_runtime_exceptions = True
207207

208208
self._iter_ids = 0
209+
self._addop_ids = 0
209210

210211
self._cache_for_body_calls = False
211212
self._cache_while_body_calls = False
@@ -1123,7 +1124,19 @@ def visit_BinOp(self, node):
11231124
return 'Math.pow(%s,%s)' %(left, right)
11241125

11251126
elif op == '+' and not self._with_dart:
1126-
return '__add_op(%s, %s)'%(left, right)
1127+
#return '__add_op(%s, %s)'%(left, right) ## this is very slow compared to using the ternary op with typeof check
1128+
## the ternary operator in javascript is fast, the add op needs to be fast for adding numbers, so here typeof is
1129+
## used to check if the first variable is a number, and if so add the numbers, otherwise fallback to using the
1130+
## __add_op function, the __add_op function checks if the first variable is an Array, and if so then concatenate;
1131+
## else __add_op will call the "__add__" method of the left operand, passing right as the first argument.
1132+
l = '__left%s' %self._addop_ids
1133+
self._addop_ids += 1
1134+
r = '__right%s' %self._addop_ids
1135+
writer.write('var(%s,%s)' %(l,r))
1136+
self._addop_ids += 1
1137+
writer.write('%s = %s' %(l,left))
1138+
writer.write('%s = %s' %(r,right))
1139+
return '__ternary_operator__( typeof(%s)=="number", %s + %s, __add_op(%s, %s))'%(l, l, r, l, r)
11271140

11281141
elif isinstance(node.left, Name):
11291142
typedef = self.get_typedef( node.left )
@@ -2265,7 +2278,7 @@ def visit_FunctionDef(self, node):
22652278
if node.args.defaults:
22662279
kwargs_name = node.args.kwarg or '_kwargs_'
22672280
lines = [ 'if (!( %s instanceof Object )) {' %kwargs_name ]
2268-
a = '%s,'.join( ['%s: arguments[%s]' %(arg.id, i) for i,arg in enumerate(node.args.args)] )
2281+
a = ','.join( ['%s: arguments[%s]' %(arg.id, i) for i,arg in enumerate(node.args.args)] )
22692282
lines.append( 'var %s = {%s}' %(kwargs_name, a))
22702283
lines.append( '}')
22712284
for a in lines:
@@ -2557,9 +2570,11 @@ def visit_For(self, node):
25572570
iter_end = self.visit(iter.args[0])
25582571

25592572
iter_name = target.id
2560-
writer.write('var(%s)' %iter_name)
2573+
writer.write('var(%s, %s__end__)' %(iter_name, iter_name))
25612574
writer.write('%s = %s' %(iter_name, iter_start))
2562-
writer.write('while %s < %s:' %(iter_name, iter_end))
2575+
writer.write('%s__end__ = %s' %(iter_name, iter_end))
2576+
writer.write('while %s < %s__end__:' %(iter_name, iter_name))
2577+
25632578
writer.push()
25642579
map(self.visit, node.body)
25652580
writer.write('%s += 1' %iter_name )
@@ -2653,9 +2668,12 @@ def visit_For(self, node):
26532668
elif is_range:
26542669
iter_name = target.id
26552670
if not self._with_coffee:
2656-
writer.write('var(%s)' %iter_name)
2671+
writer.write('var(%s, %s__end__)' %(iter_name, iter_name))
26572672
writer.write('%s = %s' %(iter_name, iter_start))
2658-
writer.write('while %s < %s:' %(iter_name, iter_end))
2673+
writer.write('%s__end__ = %s' %(iter_name, iter_end)) ## assign to a temp variable.
2674+
#writer.write('while %s < %s:' %(iter_name, iter_end)) ## this fails with the ternary __add_op
2675+
writer.write('while %s < %s__end__:' %(iter_name, iter_name))
2676+
26592677
writer.push()
26602678
map(self.visit, node.body)
26612679
if self._with_lua:
@@ -2794,12 +2812,14 @@ def __init__(self, node, compiler=None):
27942812
self._builtin_functions = compiler._builtin_functions
27952813
self._js_classes = compiler._js_classes
27962814
self._global_functions = compiler._global_functions
2815+
self._addop_ids = compiler._addop_ids
27972816
self._with_inline = False
27982817
self._cache_for_body_calls = False
27992818
self._source = compiler._source
28002819
self._instances = dict()
28012820
self._head_yield = False
28022821
self.visit( node )
2822+
compiler._addop_ids = self._addop_ids
28032823

28042824
def visit_Yield(self, node):
28052825
if self._in_head:

0 commit comments

Comments
 (0)