396 default_pos = len(arg_names) - len(node.defaults) |
396 default_pos = len(arg_names) - len(node.defaults) |
397 if arg_names and arg_names[0] == self.method_self: |
397 if arg_names and arg_names[0] == self.method_self: |
398 default_pos -= 1 |
398 default_pos -= 1 |
399 self.printo(function_name+'.parse_kwargs = function (', ", ".join(["__kwargs"]+arg_names), ") {") |
399 self.printo(function_name+'.parse_kwargs = function (', ", ".join(["__kwargs"]+arg_names), ") {") |
400 for default_node in node.defaults: |
400 for default_node in node.defaults: |
401 default_value = self.expr(default_node, current_klass) |
401 # default_value = self.expr(default_node, current_klass) |
402 # if isinstance(default_node, ast.Const): |
402 # if isinstance(default_node, ast.Const): |
403 # default_value = self._const(default_node) |
403 # default_value = self._const(default_node) |
404 # elif isinstance(default_node, ast.Name): |
404 # elif isinstance(default_node, ast.Name): |
405 # default_value = self._name(default_node) |
405 # default_value = self._name(default_node) |
406 # elif isinstance(default_node, ast.UnarySub): |
406 # elif isinstance(default_node, ast.UnarySub): |
407 # default_value = self._unarysub(default_node, current_klass) |
407 # default_value = self._unarysub(default_node, current_klass) |
408 # else: |
408 # else: |
409 # raise TranslationError("unsupported type (in _method)", default_node) |
409 # raise TranslationError("unsupported type (in _method)", default_node) |
410 |
410 |
411 default_name = arg_names[default_pos] |
411 default_name = arg_names[default_pos] |
412 self.printo(" if (typeof %s == 'undefined')" % (default_name)) |
412 self.printo(" if (typeof %s == 'undefined')" % (default_name)) |
413 self.printo(" %s=__kwargs.%s;" % (default_name, default_name)) |
413 self.printo(" %s=__kwargs.%s;" % (default_name, default_name)) |
414 default_pos += 1 |
414 default_pos += 1 |
1009 raise TranslationError("unsupported type (in _stmt)", node) |
1009 raise TranslationError("unsupported type (in _stmt)", node) |
1010 |
1010 |
1011 if debugStmt: |
1011 if debugStmt: |
1012 |
1012 |
1013 lt = self.get_line_trace(node) |
1013 lt = self.get_line_trace(node) |
1014 |
|
1015 haltException = self.module_prefix + "HaltException" |
|
1016 isHaltFunction = self.module_prefix + "IsHaltException" |
1014 isHaltFunction = self.module_prefix + "IsHaltException" |
1017 |
1015 |
1018 out = ( |
1016 out = ( |
1019 ' } catch (__err) {', |
1017 ' } catch (__err) {', |
1020 ' if (' + isHaltFunction + '(__err.name)) {', |
1018 ' if (' + isHaltFunction + '(__err.name)) {', |
1075 local_var_names = self.local_arg_stack[-1] |
1073 local_var_names = self.local_arg_stack[-1] |
1076 |
1074 |
1077 def _lhsFromAttr(v, current_klass): |
1075 def _lhsFromAttr(v, current_klass): |
1078 attr_name = v.attrname |
1076 attr_name = v.attrname |
1079 if isinstance(v.expr, ast.Name): |
1077 if isinstance(v.expr, ast.Name): |
1080 obj = v.expr.name |
|
1081 lhs = self._name(v.expr, current_klass) + "." + attr_name |
1078 lhs = self._name(v.expr, current_klass) + "." + attr_name |
1082 elif isinstance(v.expr, ast.Getattr): |
1079 elif isinstance(v.expr, ast.Getattr): |
1083 lhs = self._getattr(v, current_klass) |
1080 lhs = self._getattr(v, current_klass) |
1084 elif isinstance(v.expr, ast.Subscript): |
1081 elif isinstance(v.expr, ast.Subscript): |
1085 lhs = self._subscript(v.expr, current_klass) + "." + attr_name |
1082 lhs = self._subscript(v.expr, current_klass) + "." + attr_name |
1316 raise TranslationError("unsupported type (in _for)", node.list) |
1313 raise TranslationError("unsupported type (in _for)", node.list) |
1317 |
1314 |
1318 lhs = "var " + assign_name |
1315 lhs = "var " + assign_name |
1319 iterator_name = "__" + assign_name |
1316 iterator_name = "__" + assign_name |
1320 |
1317 |
|
1318 loc_dict = { |
|
1319 "iterator_name": iterator_name, |
|
1320 "list_expr": list_expr, |
|
1321 "lhs": lhs, |
|
1322 "op": op, |
|
1323 "assign_tuple": assign_tuple, |
|
1324 } |
|
1325 |
1321 self.printo(""" |
1326 self.printo(""" |
1322 var %(iterator_name)s = %(list_expr)s.__iter__(); |
1327 var %(iterator_name)s = %(list_expr)s.__iter__(); |
1323 try { |
1328 try { |
1324 while (true) { |
1329 while (true) { |
1325 %(lhs)s %(op)s %(iterator_name)s.next(); |
1330 %(lhs)s %(op)s %(iterator_name)s.next(); |
1326 %(assign_tuple)s |
1331 %(assign_tuple)s |
1327 """ % locals()) |
1332 """ % loc_dict) |
1328 for node in node.body.nodes: |
1333 for node in node.body.nodes: |
1329 self._stmt(node, current_klass) |
1334 self._stmt(node, current_klass) |
1330 self.printo(""" |
1335 self.printo(""" |
1331 } |
1336 } |
1332 } catch (e) { |
1337 } catch (e) { |
1333 if (e.__name__ != pyjslib.StopIteration.__name__) { |
1338 if (e.__name__ != pyjslib.StopIteration.__name__) { |
1334 throw e; |
1339 throw e; |
1335 } |
1340 } |
1336 } |
1341 } |
1337 """ % locals()) |
1342 """) |
1338 |
1343 |
1339 def _while(self, node, current_klass): |
1344 def _while(self, node, current_klass): |
1340 test = self.expr(node.test, current_klass) |
1345 test = self.expr(node.test, current_klass) |
1341 self.printo(" while (pyjslib.bool(" + test + ")) {") |
1346 self.printo(" while (pyjslib.bool(" + test + ")) {") |
1342 if isinstance(node.body, ast.Stmt): |
1347 if isinstance(node.body, ast.Stmt): |
1528 f = file(file_name, "r") |
1533 f = file(file_name, "r") |
1529 src = f.read() |
1534 src = f.read() |
1530 f.close() |
1535 f.close() |
1531 output = cStringIO.StringIO() |
1536 output = cStringIO.StringIO() |
1532 mod = compiler.parseFile(file_name) |
1537 mod = compiler.parseFile(file_name) |
1533 t = Translator(module_name, module_name, module_name, src, debug, mod, output) |
1538 Translator(module_name, module_name, module_name, src, debug, mod, output) |
1534 return output.getvalue() |
1539 return output.getvalue() |
1535 |
1540 |
1536 |
1541 |
1537 class PlatformParser: |
1542 class PlatformParser: |
1538 def __init__(self, platform_dir="", verbose=True): |
1543 def __init__(self, platform_dir="", verbose=True): |