Skip to content

Commit dd172c0

Browse files
author
hartsantler
committed
optimized unpack assignment by using ternary to check if the container is an Array.
1 parent 7bc5b73 commit dd172c0

8 files changed

Lines changed: 196 additions & 103 deletions

File tree

pythonjs/python_to_pythonjs.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def __init__(self, source=None, module=None, module_path=None, dart=False, coffe
142142
self._line = None
143143
self._line_number = 0
144144

145-
self._direct_operators = set() ## optimize "+" operator
145+
self._direct_operators = set() ## optimize "+" and "*" operator
146146
self._with_ll = False ## lowlevel
147147
self._with_lua = lua
148148
self._with_coffee = coffee
@@ -197,6 +197,8 @@ def __init__(self, source=None, module=None, module_path=None, dart=False, coffe
197197
self._comprehensions = []
198198
self._generator_functions = set()
199199

200+
self._in_loop_with_else = False
201+
200202
self._custom_operators = {}
201203
self._injector = [] ## advanced meta-programming hacks
202204
self._in_class = None
@@ -1212,6 +1214,9 @@ def visit_BinOp(self, node):
12121214
power = POWER_OF_TWO.index( node.right.n )
12131215
return '%s >> %s'%(left, power)
12141216

1217+
elif not self._with_dart and op == '*' and '*' in self._direct_operators:
1218+
return '(%s * %s)'%(left, right)
1219+
12151220
elif not self._with_dart and not self._with_js and op == '*':
12161221
if left in self._typedef_vars and self._typedef_vars[left] in typedpython.native_number_types:
12171222
return '(%s * %s)'%(left, right)
@@ -1442,7 +1447,10 @@ def visit_Subscript(self, node):
14421447
## this is required because we need to support slices on String ##
14431448
return '__getslice__(%s, %s)'%(name, self.visit(node.slice))
14441449
else:
1445-
return '%s.__getslice__(%s)'%(name, self.visit(node.slice))
1450+
if not node.slice.lower and not node.slice.upper and not node.slice.step:
1451+
return '%s.copy()' %name
1452+
else:
1453+
return '%s.__getslice__(%s)'%(name, self.visit(node.slice))
14461454

14471455

14481456
elif isinstance(node.slice, ast.Index) and isinstance(node.slice.value, ast.Num):
@@ -1769,7 +1777,8 @@ def _visit_assign_helper(self, node, target):
17691777
elif self._with_js or self._with_dart:
17701778
writer.write("%s = %s[%s]" % (self.visit(target), r, i))
17711779
else:
1772-
writer.write("%s = __get__(__get__(%s, '__getitem__'), '__call__')([%s], __NULL_OBJECT__)" % (self.visit(target), r, i))
1780+
fallback = "__get__(__get__(%s, '__getitem__'), '__call__')([%s], __NULL_OBJECT__)" %(r, i)
1781+
writer.write("%s = __ternary_operator__(instanceof(%s,Array), %s[%s], %s)" % (self.visit(target), r, r,i, fallback ))
17731782

17741783
def visit_Print(self, node):
17751784
writer.write('print %s' % ', '.join(map(self.visit, node.values)))
@@ -2915,7 +2924,8 @@ def visit_Continue(self, node):
29152924
return ''
29162925

29172926
def visit_Break(self, node):
2918-
writer.write('__break__ = True')
2927+
if self._in_loop_with_else:
2928+
writer.write('__break__ = True')
29192929
writer.write('break')
29202930

29212931
def visit_For(self, node):
@@ -3158,6 +3168,7 @@ def visit_While(self, node):
31583168
self._call_ids += 1
31593169

31603170
if node.orelse:
3171+
self._in_loop_with_else = True
31613172
writer.write('var(__break__)')
31623173
writer.write('__break__ = False')
31633174

@@ -3169,6 +3180,7 @@ def visit_While(self, node):
31693180
writer.pull()
31703181

31713182
if node.orelse:
3183+
self._in_loop_with_else = False
31723184
writer.write('if __break__ == False:')
31733185
writer.push()
31743186
map(self.visit, node.orelse)

0 commit comments

Comments
 (0)