545 try: |
545 try: |
546 call_this, method_name = call_name.rsplit(".", 1) |
546 call_this, method_name = call_name.rsplit(".", 1) |
547 except ValueError: |
547 except ValueError: |
548 # Must be a function call ... |
548 # Must be a function call ... |
549 return ("pyjs_kwargs_function_call("+call_name+", " |
549 return ("pyjs_kwargs_function_call("+call_name+", " |
550 + star_arg_name |
550 + star_arg_name + ", ["+fn_args+"]" + ")") |
551 + ", ["+fn_args+"]" |
|
552 + ")") |
|
553 else: |
551 else: |
554 return ("pyjs_kwargs_method_call("+call_this+", '"+method_name+"', " |
552 return ("pyjs_kwargs_method_call("+call_this+", '"+method_name+"', " |
555 + star_arg_name |
553 + star_arg_name + ", ["+fn_args+"]" + ")") |
556 + ", ["+fn_args+"]" |
|
557 + ")") |
|
558 else: |
554 else: |
559 return call_name + "(" + ", ".join(call_args) + ")" |
555 return call_name + "(" + ", ".join(call_args) + ")" |
560 |
556 |
561 def _print(self, node, current_klass): |
557 def _print(self, node, current_klass): |
562 if self.optimize: |
558 if self.optimize: |
635 |
631 |
636 def modpfx(self): |
632 def modpfx(self): |
637 return strip_py(self.module_prefix) |
633 return strip_py(self.module_prefix) |
638 |
634 |
639 def _name(self, v, current_klass, top_level=False, |
635 def _name(self, v, current_klass, top_level=False, |
640 return_none_for_module=False): |
636 return_none_for_module=False): |
641 |
637 |
642 if v.name == 'ilikesillynamesfornicedebugcode': |
638 if v.name == 'ilikesillynamesfornicedebugcode': |
643 print top_level, current_klass, repr(v) |
639 print top_level, current_klass, repr(v) |
644 print self.top_level_vars |
640 print self.top_level_vars |
645 print self.top_level_functions |
641 print self.top_level_functions |
687 if hasattr(cls_name, "name"): |
683 if hasattr(cls_name, "name"): |
688 cls_name_ = cls_name.name_ |
684 cls_name_ = cls_name.name_ |
689 cls_name = cls_name.name |
685 cls_name = cls_name.name |
690 else: |
686 else: |
691 cls_name_ = current_klass + "_" # XXX ??? |
687 cls_name_ = current_klass + "_" # XXX ??? |
692 name = UU+cls_name_ + ".prototype.__class__." \ |
688 name = UU+cls_name_ + ".prototype.__class__." + v.name |
693 + v.name |
|
694 if v.name == 'listener': |
689 if v.name == 'listener': |
695 name = 'listener+' + name |
690 name = 'listener+' + name |
696 return name |
691 return name |
697 |
692 |
698 return v.name |
693 return v.name |
779 elif isinstance(node.bases[0], ast.Getattr): |
774 elif isinstance(node.bases[0], ast.Getattr): |
780 # the bases are not in scope of the class so do not |
775 # the bases are not in scope of the class so do not |
781 # pass our class to self._name |
776 # pass our class to self._name |
782 base_class_ = self._name(node.bases[0].expr, None) + \ |
777 base_class_ = self._name(node.bases[0].expr, None) + \ |
783 ".__" + node.bases[0].attrname |
778 ".__" + node.bases[0].attrname |
784 base_class = self._name(node.bases[0].expr, None) + \ |
779 base_class = \ |
785 "." + node.bases[0].attrname |
780 self._name(node.bases[0].expr, None) + \ |
|
781 "." + node.bases[0].attrname |
786 else: |
782 else: |
787 raise TranslationError("unsupported type (in _class)", node.bases[0]) |
783 raise TranslationError("unsupported type (in _class)", node.bases[0]) |
788 |
784 |
789 current_klass.set_base(base_class) |
785 current_klass.set_base(base_class) |
790 else: |
786 else: |
947 print >>self.output, " "+altexpr+".unbound_method = true;" |
943 print >>self.output, " "+altexpr+".unbound_method = true;" |
948 print >>self.output, " "+fexpr+".instance_method = true;" |
944 print >>self.output, " "+fexpr+".instance_method = true;" |
949 print >>self.output, " "+altexpr+".__name__ = '%s';" % node.name |
945 print >>self.output, " "+altexpr+".__name__ = '%s';" % node.name |
950 |
946 |
951 print >>self.output, UU + class_name_ + ".prototype.%s.__name__ = '%s';" % \ |
947 print >>self.output, UU + class_name_ + ".prototype.%s.__name__ = '%s';" % \ |
952 (node.name, node.name) |
948 (node.name, node.name) |
953 |
949 |
954 if node.kwargs or len(node.defaults): |
950 if node.kwargs or len(node.defaults): |
955 print >>self.output, " "+altexpr + ".parse_kwargs = " + fexpr + ".parse_kwargs;" |
951 print >>self.output, " "+altexpr + ".parse_kwargs = " + fexpr + ".parse_kwargs;" |
956 |
952 |
957 self.method_self = None |
953 self.method_self = None |
1012 lt = self.get_line_trace(node) |
1008 lt = self.get_line_trace(node) |
1013 |
1009 |
1014 haltException = self.module_prefix + "HaltException" |
1010 haltException = self.module_prefix + "HaltException" |
1015 isHaltFunction = self.module_prefix + "IsHaltException" |
1011 isHaltFunction = self.module_prefix + "IsHaltException" |
1016 |
1012 |
1017 print >>self.output, ' } catch (__err) {' |
1013 out = ( |
1018 print >>self.output, ' if (' + isHaltFunction + '(__err.name)) {' |
1014 ' } catch (__err) {', |
1019 print >>self.output, ' throw __err;' |
1015 ' if (' + isHaltFunction + '(__err.name)) {', |
1020 print >>self.output, ' } else {' |
1016 ' throw __err;', |
1021 print >>self.output, " st = sys.printstack() + "\ |
1017 ' } else {', |
1022 + '"%s"' % lt + "+ '\\n' ;" |
1018 ' st = sys.printstack() + ' + '"%s"' % lt + "+ '\\n' ;" |
1023 print >>self.output, ' alert("' + "Error in " \ |
1019 ' alert("' + 'Error in ' + lt + '"' |
1024 + lt + '"' \ |
1020 + '+"\\n"+__err.name+": "+__err.message' |
1025 + '+"\\n"+__err.name+": "+__err.message'\ |
1021 + '+"\\n\\nStack trace:\\n"' + '+st' + ');', |
1026 + '+"\\n\\nStack trace:\\n"' \ |
1022 ' debugger;', |
1027 + '+st' \ |
1023 ' throw new ' + self.module_prefix + 'HaltException();', |
1028 + ');' |
1024 ' }', |
1029 print >>self.output, ' debugger;' |
1025 ' }' |
1030 |
1026 ) |
1031 print >>self.output, ' throw new ' + self.module_prefix + "HaltException();" |
1027 for s in out: |
1032 print >>self.output, ' }' |
1028 print >>self.output, s |
1033 print >>self.output, ' }' |
|
1034 |
1029 |
1035 def get_line_trace(self, node): |
1030 def get_line_trace(self, node): |
1036 lineNum = "Unknown" |
1031 lineNum = "Unknown" |
1037 srcLine = "" |
1032 srcLine = "" |
1038 if hasattr(node, "lineno"): |
1033 if hasattr(node, "lineno"): |
1042 srcLine = srcLine.replace('\\', '\\\\') |
1037 srcLine = srcLine.replace('\\', '\\\\') |
1043 srcLine = srcLine.replace('"', '\\"') |
1038 srcLine = srcLine.replace('"', '\\"') |
1044 srcLine = srcLine.replace("'", "\\'") |
1039 srcLine = srcLine.replace("'", "\\'") |
1045 |
1040 |
1046 return self.raw_module_name + ".py, line " \ |
1041 return self.raw_module_name + ".py, line " \ |
1047 + str(lineNum) + ":"\ |
1042 + str(lineNum) + ":"\ |
1048 + "\\n" \ |
1043 + "\\n" \ |
1049 + " " + srcLine |
1044 + " " + srcLine |
1050 |
1045 |
1051 def _augassign(self, node, current_klass): |
1046 def _augassign(self, node, current_klass): |
1052 v = node.node |
1047 v = node.node |
1053 if isinstance(v, ast.Getattr): |
1048 if isinstance(v, ast.Getattr): |
1054 # XXX HACK! don't allow += on return result of getattr. |
1049 # XXX HACK! don't allow += on return result of getattr. |
1094 + v.name |
1089 + v.name |
1095 else: |
1090 else: |
1096 self.top_level_vars.add(v.name) |
1091 self.top_level_vars.add(v.name) |
1097 vname = self.modpfx() + v.name |
1092 vname = self.modpfx() + v.name |
1098 if not self.modpfx() and v.name not in\ |
1093 if not self.modpfx() and v.name not in\ |
1099 self.method_imported_globals: |
1094 self.method_imported_globals: |
1100 lhs = "var " + vname |
1095 lhs = "var " + vname |
1101 else: |
1096 else: |
1102 lhs = UU + vname |
1097 lhs = UU + vname |
1103 self.add_local_arg(v.name) |
1098 self.add_local_arg(v.name) |
1104 else: |
1099 else: |
1157 raise TranslationError("must have one sub " + |
1152 raise TranslationError("must have one sub " + |
1158 "(in _assign)", child) |
1153 "(in _assign)", child) |
1159 idx = self.expr(child.subs[0], current_klass) |
1154 idx = self.expr(child.subs[0], current_klass) |
1160 value = self.expr(node.expr, current_klass) |
1155 value = self.expr(node.expr, current_klass) |
1161 print >>self.output, " " + obj + ".__setitem__(" \ |
1156 print >>self.output, " " + obj + ".__setitem__(" \ |
1162 + idx + ", " + rhs + ");" |
1157 + idx + ", " + rhs + ");" |
1163 continue |
1158 continue |
1164 print >>self.output, " " + lhs + " = " + rhs + ";" |
1159 print >>self.output, " " + lhs + " = " + rhs + ";" |
1165 return |
1160 return |
1166 else: |
1161 else: |
1167 raise TranslationError("unsupported type (in _assign)", v) |
1162 raise TranslationError("unsupported type (in _assign)", v) |