180 child_name = name[-1] |
180 child_name = name[-1] |
181 return "var %s = %s;\n" % (child_name, raw_module_name) |
181 return "var %s = %s;\n" % (child_name, raw_module_name) |
182 |
182 |
183 |
183 |
184 def gen_mod_import(parentName, importName, dynamic=1): |
184 def gen_mod_import(parentName, importName, dynamic=1): |
185 #pyjs_ajax_eval("%(n)s.cache.js", null, true); |
185 # pyjs_ajax_eval("%(n)s.cache.js", null, true); |
186 return """ |
186 return """ |
187 pyjslib.import_module(sys.loadpath, '%(p)s', '%(n)s', %(d)d, false); |
187 pyjslib.import_module(sys.loadpath, '%(p)s', '%(n)s', %(d)d, false); |
188 """ % ({'p': parentName, 'd': dynamic, 'n': importName}) + \ |
188 """ % ({'p': parentName, 'd': dynamic, 'n': importName}) + \ |
189 mod_var_name_decl(importName) |
189 mod_var_name_decl(importName) |
190 |
190 |
320 self._stmt(child, None) |
320 self._stmt(child, None) |
321 else: |
321 else: |
322 raise TranslationError("unsupported type (in __init__)", child) |
322 raise TranslationError("unsupported type (in __init__)", child) |
323 |
323 |
324 # Initialize all classes for this module |
324 # Initialize all classes for this module |
325 #print >> self.output, "__"+self.modpfx()+\ |
325 # print >> self.output, "__"+self.modpfx()+\ |
326 # "classes_initialize = function() {\n" |
326 # "classes_initialize = function() {\n" |
327 #for className in self.top_level_classes: |
327 # for className in self.top_level_classes: |
328 # print >> self.output, "\t"+UU+self.modpfx()+"__"+className+"_initialize();" |
328 # print >> self.output, "\t"+UU+self.modpfx()+"__"+className+"_initialize();" |
329 #print >> self.output, "};\n" |
329 # print >> self.output, "};\n" |
330 |
330 |
331 print >> self.output, "return this;\n" |
331 print >> self.output, "return this;\n" |
332 print >> self.output, "}; /* end %s */ \n" % module_name |
332 print >> self.output, "}; /* end %s */ \n" % module_name |
333 |
333 |
334 def module_imports(self): |
334 def module_imports(self): |
405 default_name = arg_names[default_pos] |
405 default_name = arg_names[default_pos] |
406 print >>self.output, " if (typeof %s == 'undefined')" % (default_name) |
406 print >>self.output, " if (typeof %s == 'undefined')" % (default_name) |
407 print >>self.output, " %s=__kwargs.%s;" % (default_name, default_name) |
407 print >>self.output, " %s=__kwargs.%s;" % (default_name, default_name) |
408 default_pos += 1 |
408 default_pos += 1 |
409 |
409 |
410 #self._default_args_handler(node, arg_names, current_klass) |
410 # self._default_args_handler(node, arg_names, current_klass) |
411 if node.kwargs: |
411 if node.kwargs: |
412 arg_names += ["pyjslib.Dict(__kwargs)"] |
412 arg_names += ["pyjslib.Dict(__kwargs)"] |
413 print >>self.output, " var __r = "+"".join(["[", ", ".join(arg_names), "]"])+";" |
413 print >>self.output, " var __r = "+"".join(["[", ", ".join(arg_names), "]"])+";" |
414 if node.varargs: |
414 if node.varargs: |
415 self._varargs_handler(node, "__args", arg_names, current_klass) |
415 self._varargs_handler(node, "__args", arg_names, current_klass) |
592 k = [" (%(err)s.__name__ == %(expr)s.__name__) " % dict(err=errName, expr=self.expr(expr, current_klass))] |
592 k = [" (%(err)s.__name__ == %(expr)s.__name__) " % dict(err=errName, expr=self.expr(expr, current_klass))] |
593 print >> self.output, " if(%s) {" % '||\n\t\t'.join(k) |
593 print >> self.output, " if(%s) {" % '||\n\t\t'.join(k) |
594 for stmt in node.handlers[0][2]: |
594 for stmt in node.handlers[0][2]: |
595 self._stmt(stmt, current_klass) |
595 self._stmt(stmt, current_klass) |
596 if expr: |
596 if expr: |
597 #print >> self.output, "} else { throw(%s); } " % errName |
597 # print >> self.output, "} else { throw(%s); } " % errName |
598 print >> self.output, "}" |
598 print >> self.output, "}" |
599 if node.else_ is not None: |
599 if node.else_ is not None: |
600 print >>self.output, " } finally {" |
600 print >>self.output, " } finally {" |
601 for stmt in node.else_: |
601 for stmt in node.else_: |
602 self._stmt(stmt, current_klass) |
602 self._stmt(stmt, current_klass) |
610 obj = self._name(v.expr, current_klass, return_none_for_module=True) |
610 obj = self._name(v.expr, current_klass, return_none_for_module=True) |
611 if obj is None and v.expr.name in self.module_imports(): |
611 if obj is None and v.expr.name in self.module_imports(): |
612 # XXX TODO: distinguish between module import classes |
612 # XXX TODO: distinguish between module import classes |
613 # and variables. right now, this is a hack to get |
613 # and variables. right now, this is a hack to get |
614 # the sys module working. |
614 # the sys module working. |
615 #if v.expr.name == 'sys': |
615 # if v.expr.name == 'sys': |
616 return v.expr.name+'.'+attr_name |
616 return v.expr.name+'.'+attr_name |
617 #return v.expr.name+'.__'+attr_name+'.prototype.__class__' |
617 # return v.expr.name+'.__'+attr_name+'.prototype.__class__' |
618 if not use_getattr or attr_name == '__class__' or \ |
618 if not use_getattr or attr_name == '__class__' or \ |
619 attr_name == '__name__': |
619 attr_name == '__name__': |
620 return obj + "." + attr_name |
620 return obj + "." + attr_name |
621 return "pyjslib.getattr(%s, '%s')" % (obj, attr_name) |
621 return "pyjslib.getattr(%s, '%s')" % (obj, attr_name) |
622 elif isinstance(v.expr, ast.Getattr): |
622 elif isinstance(v.expr, ast.Getattr): |
695 obj = v.name |
695 obj = v.name |
696 |
696 |
697 if obj in self.method_imported_globals: |
697 if obj in self.method_imported_globals: |
698 call_name = UU+self.modpfx() + obj + "." + attr_name |
698 call_name = UU+self.modpfx() + obj + "." + attr_name |
699 elif obj in self.imported_classes: |
699 elif obj in self.imported_classes: |
700 #attr_str = "" |
700 # attr_str = "" |
701 #if attr_name != "__init__": |
701 # if attr_name != "__init__": |
702 attr_str = ".prototype.__class__." + attr_name |
702 attr_str = ".prototype.__class__." + attr_name |
703 call_name = UU+self.imported_classes[obj] + '.__' + obj + attr_str |
703 call_name = UU+self.imported_classes[obj] + '.__' + obj + attr_str |
704 elif obj in self.module_imports(): |
704 elif obj in self.module_imports(): |
705 call_name = obj + "." + attr_name |
705 call_name = obj + "." + attr_name |
706 elif obj[0] == obj[0].upper(): # XXX HACK ALERT |
706 elif obj[0] == obj[0].upper(): # XXX HACK ALERT |
785 else: |
785 else: |
786 raise TranslationError("more than one base (in _class)", node) |
786 raise TranslationError("more than one base (in _class)", node) |
787 |
787 |
788 print >>self.output, UU+class_name_ + " = function () {" |
788 print >>self.output, UU+class_name_ + " = function () {" |
789 # call superconstructor |
789 # call superconstructor |
790 #if base_class: |
790 # if base_class: |
791 # print >>self.output, " __" + base_class + ".call(this);" |
791 # print >>self.output, " __" + base_class + ".call(this);" |
792 print >>self.output, "}" |
792 print >>self.output, "}" |
793 |
793 |
794 if not init_method: |
794 if not init_method: |
795 init_method = ast.Function([], "__init__", ["self"], [], 0, None, []) |
795 init_method = ast.Function([], "__init__", ["self"], [], 0, None, []) |
796 #self._method(init_method, current_klass, class_name) |
796 # self._method(init_method, current_klass, class_name) |
797 |
797 |
798 # Generate a function which constructs the object |
798 # Generate a function which constructs the object |
799 clsfunc = ast.Function( |
799 clsfunc = ast.Function( |
800 [], node.name, |
800 [], node.name, |
801 init_method.argnames[1:], |
801 init_method.argnames[1:], |
880 else: |
880 else: |
881 if len(arg_names) == 0: |
881 if len(arg_names) == 0: |
882 raise TranslationError("methods must take an argument 'self' (in _method)", node) |
882 raise TranslationError("methods must take an argument 'self' (in _method)", node) |
883 self.method_self = arg_names[0] |
883 self.method_self = arg_names[0] |
884 |
884 |
885 #if not classmethod and arg_names[0] != "self": |
885 # if not classmethod and arg_names[0] != "self": |
886 # raise TranslationError("first arg not 'self' (in _method)", node) |
886 # raise TranslationError("first arg not 'self' (in _method)", node) |
887 |
887 |
888 normal_arg_names = arg_names[1:] |
888 normal_arg_names = arg_names[1:] |
889 if node.kwargs: |
889 if node.kwargs: |
890 kwargname = normal_arg_names.pop() |
890 kwargname = normal_arg_names.pop() |
1698 imported_js.update(set(t.imported_js)) |
1698 imported_js.update(set(t.imported_js)) |
1699 imported_modules_str = "" |
1699 imported_modules_str = "" |
1700 for module in t.imported_modules: |
1700 for module in t.imported_modules: |
1701 if module not in self.library_modules: |
1701 if module not in self.library_modules: |
1702 self.library_modules.append(module) |
1702 self.library_modules.append(module) |
1703 #imported_js.update(set(t.imported_js)) |
1703 # imported_js.update(set(t.imported_js)) |
1704 #imported_modules_str += self._translate( |
1704 # imported_modules_str += self._translate( |
1705 # module, False, debug=debug, imported_js=imported_js) |
1705 # module, False, debug=debug, imported_js=imported_js) |
1706 |
1706 |
1707 return imported_modules_str + module_str |
1707 return imported_modules_str + module_str |
1708 |
1708 |
1709 def translate(self, module_name, is_app=True, debug=False, |
1709 def translate(self, module_name, is_app=True, debug=False, |