laurent@371: #!/usr/bin/env python laurent@371: # Copyright 2006 James Tauber and contributors laurent@371: # laurent@371: # Licensed under the Apache License, Version 2.0 (the "License"); laurent@371: # you may not use this file except in compliance with the License. laurent@371: # You may obtain a copy of the License at laurent@371: # laurent@371: # http://www.apache.org/licenses/LICENSE-2.0 laurent@371: # laurent@371: # Unless required by applicable law or agreed to in writing, software laurent@371: # distributed under the License is distributed on an "AS IS" BASIS, laurent@371: # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. laurent@371: # See the License for the specific language governing permissions and laurent@371: # limitations under the License. laurent@371: laurent@371: andrej@1826: from __future__ import print_function laurent@371: import sys laurent@371: from types import StringType laurent@371: import compiler laurent@371: from compiler import ast laurent@371: import os laurent@371: import copy andrej@1783: import cStringIO laurent@371: laurent@371: # the standard location for builtins (e.g. pyjslib) can be laurent@371: # over-ridden by changing this. it defaults to sys.prefix laurent@371: # so that on a system-wide install of pyjamas the builtins laurent@371: # can be found in e.g. {sys.prefix}/share/pyjamas laurent@371: # laurent@371: # over-rides can be done by either explicitly modifying laurent@371: # pyjs.prefix or by setting an environment variable, PYJSPREFIX. laurent@371: laurent@371: prefix = sys.prefix laurent@371: andrej@1763: if 'PYJSPREFIX' in os.environ: laurent@371: prefix = os.environ['PYJSPREFIX'] laurent@371: laurent@371: # pyjs.path is the list of paths, just like sys.path, from which laurent@371: # library modules will be searched for, for compile purposes. laurent@371: # obviously we don't want to use sys.path because that would result laurent@371: # in compiling standard python modules into javascript! laurent@371: laurent@371: path = [os.path.abspath('')] laurent@371: andrej@1763: if 'PYJSPATH' in os.environ: laurent@371: for p in os.environ['PYJSPATH'].split(os.pathsep): laurent@371: p = os.path.abspath(p) laurent@371: if os.path.isdir(p): laurent@371: path.append(p) laurent@371: laurent@371: # this is the python function used to wrap native javascript laurent@371: NATIVE_JS_FUNC_NAME = "JS" laurent@371: laurent@371: UU = "" laurent@371: andrej@1742: PYJSLIB_BUILTIN_FUNCTIONS = ("cmp", andrej@1742: "map", andrej@1742: "filter", andrej@1742: "dir", andrej@1742: "getattr", andrej@1742: "setattr", andrej@1742: "hasattr", andrej@1742: "int", andrej@1742: "float", andrej@1742: "str", andrej@1742: "repr", andrej@1742: "range", andrej@1742: "len", andrej@1742: "hash", andrej@1742: "abs", andrej@1742: "ord", andrej@1742: "chr", andrej@1742: "enumerate", andrej@1742: "min", andrej@1742: "max", andrej@1742: "bool", andrej@1742: "type", andrej@1742: "isinstance") andrej@1742: andrej@1742: PYJSLIB_BUILTIN_CLASSES = ("BaseException", andrej@1742: "Exception", andrej@1742: "StandardError", andrej@1742: "StopIteration", andrej@1742: "AttributeError", andrej@1742: "TypeError", andrej@1742: "KeyError", andrej@1742: "LookupError", andrej@1742: "list", andrej@1742: "dict", andrej@1742: "object", andrej@1773: "tuple") laurent@371: andrej@1736: laurent@371: def pyjs_builtin_remap(name): laurent@371: # XXX HACK! laurent@371: if name == 'list': laurent@371: name = 'List' laurent@371: if name == 'object': laurent@371: name = '__Object' laurent@371: if name == 'dict': laurent@371: name = 'Dict' laurent@371: if name == 'tuple': laurent@371: name = 'Tuple' laurent@371: return name laurent@371: andrej@1749: laurent@371: # XXX: this is a hack: these should be dealt with another way laurent@371: # however, console is currently the only global name which is causing laurent@371: # problems. andrej@1742: PYJS_GLOBAL_VARS = ("console") laurent@371: laurent@371: # This is taken from the django project. laurent@371: # Escape every ASCII character with a value less than 32. laurent@371: JS_ESCAPES = ( laurent@371: ('\\', r'\x5C'), laurent@371: ('\'', r'\x27'), laurent@371: ('"', r'\x22'), laurent@371: ('>', r'\x3E'), laurent@371: ('<', r'\x3C'), laurent@371: ('&', r'\x26'), laurent@371: (';', r'\x3B') laurent@371: ) + tuple([('%c' % z, '\\x%02X' % z) for z in range(32)]) laurent@371: andrej@1736: laurent@371: def escapejs(value): laurent@371: """Hex encodes characters for use in JavaScript strings.""" laurent@371: for bad, good in JS_ESCAPES: laurent@371: value = value.replace(bad, good) laurent@371: return value laurent@371: andrej@1736: laurent@371: def uuprefix(name, leave_alone=0): laurent@371: name = name.split(".") laurent@371: name = name[:leave_alone] + map(lambda x: "__%s" % x, name[leave_alone:]) laurent@371: return '.'.join(name) laurent@371: andrej@1736: andrej@1851: class Klass(object): laurent@371: laurent@371: klasses = {} laurent@371: laurent@371: def __init__(self, name, name_): laurent@371: self.name = name laurent@371: self.name_ = name_ laurent@371: self.klasses[name] = self laurent@371: self.functions = set() laurent@371: laurent@371: def set_base(self, base_name): laurent@371: self.base = self.klasses.get(base_name) laurent@371: laurent@371: def add_function(self, function_name): laurent@371: self.functions.add(function_name) laurent@371: laurent@371: laurent@371: class TranslationError(Exception): laurent@371: def __init__(self, message, node): andrej@1836: Exception.__init__(self) laurent@371: self.message = "line %s:\n%s\n%s" % (node.lineno, message, node) laurent@371: laurent@371: def __str__(self): laurent@371: return self.message laurent@371: andrej@1736: laurent@371: def strip_py(name): laurent@371: return name laurent@371: andrej@1736: laurent@371: def mod_var_name_decl(raw_module_name): laurent@371: """ function to get the last component of the module e.g. laurent@371: pyjamas.ui.DOM into the "namespace". i.e. doing laurent@371: "import pyjamas.ui.DOM" actually ends up with _two_ laurent@371: variables - one pyjamas.ui.DOM, the other just "DOM". laurent@371: but "DOM" is actually local, hence the "var" prefix. laurent@371: laurent@371: for PyV8, this might end up causing problems - we'll have laurent@371: to see: gen_mod_import and mod_var_name_decl might have laurent@371: to end up in a library-specific module, somewhere. laurent@371: """ laurent@371: name = raw_module_name.split(".") laurent@371: if len(name) == 1: laurent@371: return '' laurent@371: child_name = name[-1] laurent@371: return "var %s = %s;\n" % (child_name, raw_module_name) laurent@371: andrej@1736: laurent@371: def gen_mod_import(parentName, importName, dynamic=1): andrej@1782: # pyjs_ajax_eval("%(n)s.cache.js", null, true); laurent@371: return """ laurent@371: pyjslib.import_module(sys.loadpath, '%(p)s', '%(n)s', %(d)d, false); laurent@371: """ % ({'p': parentName, 'd': dynamic, 'n': importName}) + \ andrej@1776: mod_var_name_decl(importName) laurent@371: andrej@1736: andrej@1851: class Translator(object): laurent@371: laurent@371: def __init__(self, mn, module_name, raw_module_name, src, debug, mod, output, laurent@371: dynamic=0, optimize=False, laurent@371: findFile=None): laurent@371: laurent@371: if module_name: laurent@371: self.module_prefix = module_name + "." laurent@371: else: laurent@371: self.module_prefix = "" laurent@371: self.raw_module_name = raw_module_name laurent@371: src = src.replace("\r\n", "\n") laurent@371: src = src.replace("\n\r", "\n") laurent@371: src = src.replace("\r", "\n") laurent@371: self.src = src.split("\n") laurent@371: self.debug = debug laurent@371: self.imported_modules = [] laurent@371: self.imported_modules_as = [] laurent@371: self.imported_js = set() laurent@371: self.top_level_functions = set() laurent@371: self.top_level_classes = set() laurent@371: self.top_level_vars = set() laurent@371: self.local_arg_stack = [[]] laurent@371: self.output = output laurent@371: self.imported_classes = {} laurent@371: self.method_imported_globals = set() laurent@371: self.method_self = None laurent@371: self.nextTupleAssignID = 1 laurent@371: self.dynamic = dynamic laurent@371: self.optimize = optimize laurent@371: self.findFile = findFile laurent@371: laurent@371: if module_name.find(".") >= 0: laurent@371: vdec = '' laurent@371: else: laurent@371: vdec = 'var ' andrej@1826: self.printo(UU+"%s%s = function (__mod_name__) {" % (vdec, module_name)) andrej@1826: andrej@1826: self.printo(" if("+module_name+".__was_initialized__) return;") andrej@1826: self.printo(" "+UU+module_name+".__was_initialized__ = true;") andrej@1826: self.printo(UU+"if (__mod_name__ == null) __mod_name__ = '%s';" % (mn)) andrej@1826: self.printo(UU+"%s.__name__ = __mod_name__;" % (raw_module_name)) laurent@371: laurent@371: decl = mod_var_name_decl(raw_module_name) laurent@371: if decl: andrej@1826: self.printo(decl) laurent@371: laurent@371: if self.debug: laurent@371: haltException = self.module_prefix + "HaltException" andrej@1826: self.printo(haltException + ' = function () {') andrej@1826: self.printo(' this.message = "Program Halted";') andrej@1826: self.printo(' this.name = "' + haltException + '";') andrej@1826: self.printo('}') andrej@1826: self.printo('') andrej@1826: self.printo(haltException + ".prototype.__str__ = function()") andrej@1826: self.printo('{') andrej@1826: self.printo('return this.message ;') andrej@1826: self.printo('}') andrej@1826: andrej@1826: self.printo(haltException + ".prototype.toString = function()") andrej@1826: self.printo('{') andrej@1826: self.printo('return this.name + ": \\"" + this.message + "\\"";') andrej@1826: self.printo('}') laurent@371: laurent@371: isHaltFunction = self.module_prefix + "IsHaltException" andrej@1826: self.printo(""") laurent@371: %s = function (s) { laurent@371: var suffix="HaltException"; laurent@371: if (s.length < suffix.length) { laurent@371: //alert(s + " " + suffix); laurent@371: return false; laurent@371: } else { laurent@371: var ss = s.substring(s.length, (s.length - suffix.length)); laurent@371: //alert(s + " " + suffix + " " + ss); laurent@371: return ss == suffix; laurent@371: } laurent@371: } andrej@1826: """ % isHaltFunction) laurent@371: for child in mod.node: laurent@371: if isinstance(child, ast.Function): laurent@371: self.top_level_functions.add(child.name) laurent@371: elif isinstance(child, ast.Class): laurent@371: self.top_level_classes.add(child.name) laurent@371: laurent@371: for child in mod.node: laurent@371: if isinstance(child, ast.Function): laurent@371: self._function(child, False) laurent@371: elif isinstance(child, ast.Class): laurent@371: self._class(child) laurent@371: elif isinstance(child, ast.Import): laurent@371: importName = child.names[0][0] andrej@1737: if importName == '__pyjamas__': # special module to help make pyjamas modules loadable in the python interpreter laurent@371: pass laurent@371: elif importName.endswith('.js'): andrej@1757: self.imported_js.add(importName) laurent@371: else: andrej@1757: self.add_imported_module(strip_py(importName)) laurent@371: elif isinstance(child, ast.From): andrej@1737: if child.modname == '__pyjamas__': # special module to help make pyjamas modules loadable in the python interpreter laurent@371: pass laurent@371: else: laurent@371: self.add_imported_module(child.modname) laurent@371: self._from(child) laurent@371: elif isinstance(child, ast.Discard): laurent@371: self._discard(child, None) laurent@371: elif isinstance(child, ast.Assign): laurent@371: self._assign(child, None, True) laurent@371: elif isinstance(child, ast.AugAssign): laurent@371: self._augassign(child, None) laurent@371: elif isinstance(child, ast.If): laurent@371: self._if(child, None) laurent@371: elif isinstance(child, ast.For): laurent@371: self._for(child, None) laurent@371: elif isinstance(child, ast.While): laurent@371: self._while(child, None) laurent@371: elif isinstance(child, ast.Subscript): laurent@371: self._subscript_stmt(child, None) laurent@371: elif isinstance(child, ast.Global): laurent@371: self._global(child, None) laurent@371: elif isinstance(child, ast.Printnl): andrej@1757: self._print(child, None) laurent@371: elif isinstance(child, ast.Print): andrej@1757: self._print(child, None) laurent@371: elif isinstance(child, ast.TryExcept): laurent@371: self._tryExcept(child, None) laurent@371: elif isinstance(child, ast.Raise): laurent@371: self._raise(child, None) laurent@371: elif isinstance(child, ast.Stmt): laurent@371: self._stmt(child, None) laurent@371: else: laurent@371: raise TranslationError("unsupported type (in __init__)", child) laurent@371: laurent@371: # Initialize all classes for this module andrej@1826: # self.printo("__"+self.modpfx()+\ andrej@1826: # "classes_initialize = function() {\n") andrej@1782: # for className in self.top_level_classes: andrej@1826: # self.printo("\t"+UU+self.modpfx()+"__"+className+"_initialize();") andrej@1826: # self.printo("};\n") andrej@1826: andrej@1826: self.printo("return this;\n") andrej@1826: self.printo("}; /* end %s */ \n" % module_name) andrej@1826: andrej@1826: def printo(self, *args): andrej@1826: print(*args, file=self.output) laurent@371: laurent@371: def module_imports(self): laurent@371: return self.imported_modules + self.imported_modules_as laurent@371: laurent@371: def add_local_arg(self, varname): laurent@371: local_vars = self.local_arg_stack[-1] laurent@371: if varname not in local_vars: laurent@371: local_vars.append(varname) laurent@371: laurent@371: def add_imported_module(self, importName): laurent@371: laurent@371: if importName in self.imported_modules: laurent@371: return laurent@371: self.imported_modules.append(importName) laurent@371: name = importName.split(".") laurent@371: if len(name) != 1: laurent@371: # add the name of the module to the namespace, laurent@371: # but don't add the short name to imported_modules laurent@371: # because then the short name would be attempted to be laurent@371: # added to the dependencies, and it's half way up the laurent@371: # module import directory structure! laurent@371: child_name = name[-1] andrej@1730: self.imported_modules_as.append(child_name) andrej@1826: self.printo(gen_mod_import(self.raw_module_name, andrej@1826: strip_py(importName), andrej@1826: self.dynamic)) laurent@371: laurent@371: def _default_args_handler(self, node, arg_names, current_klass, laurent@371: output=None): laurent@371: if len(node.defaults): laurent@371: output = output or self.output laurent@371: default_pos = len(arg_names) - len(node.defaults) laurent@371: if arg_names and arg_names[0] == self.method_self: laurent@371: default_pos -= 1 laurent@371: for default_node in node.defaults: laurent@371: if isinstance(default_node, ast.Const): laurent@371: default_value = self._const(default_node) laurent@371: elif isinstance(default_node, ast.Name): laurent@371: default_value = self._name(default_node, current_klass) laurent@371: elif isinstance(default_node, ast.UnarySub): laurent@371: default_value = self._unarysub(default_node, current_klass) laurent@371: else: laurent@371: raise TranslationError("unsupported type (in _method)", default_node) laurent@371: laurent@371: default_name = arg_names[default_pos] laurent@371: default_pos += 1 andrej@1826: self.printo(" if (typeof %s == 'undefined') %s=%s;" % (default_name, default_name, default_value)) laurent@371: laurent@371: def _varargs_handler(self, node, varargname, arg_names, current_klass): andrej@1826: self.printo(" var", varargname, '= new pyjslib.Tuple();') andrej@1826: self.printo(" for(var __va_arg="+str(len(arg_names))+"; __va_arg < arguments.length; __va_arg++) {") andrej@1826: self.printo(" var __arg = arguments[__va_arg];") andrej@1826: self.printo(" "+varargname+".append(__arg);") andrej@1826: self.printo(" }") laurent@371: laurent@371: def _kwargs_parser(self, node, function_name, arg_names, current_klass): laurent@371: if len(node.defaults) or node.kwargs: laurent@371: default_pos = len(arg_names) - len(node.defaults) laurent@371: if arg_names and arg_names[0] == self.method_self: laurent@371: default_pos -= 1 andrej@1826: self.printo(function_name+'.parse_kwargs = function (', ", ".join(["__kwargs"]+arg_names), ") {") andrej@1847: for _default_node in node.defaults: andrej@1846: # default_value = self.expr(default_node, current_klass) andrej@1846: # if isinstance(default_node, ast.Const): andrej@1846: # default_value = self._const(default_node) andrej@1846: # elif isinstance(default_node, ast.Name): andrej@1846: # default_value = self._name(default_node) andrej@1846: # elif isinstance(default_node, ast.UnarySub): andrej@1846: # default_value = self._unarysub(default_node, current_klass) andrej@1846: # else: andrej@1846: # raise TranslationError("unsupported type (in _method)", default_node) laurent@371: laurent@371: default_name = arg_names[default_pos] andrej@1826: self.printo(" if (typeof %s == 'undefined')" % (default_name)) andrej@1826: self.printo(" %s=__kwargs.%s;" % (default_name, default_name)) laurent@371: default_pos += 1 laurent@371: andrej@1782: # self._default_args_handler(node, arg_names, current_klass) andrej@1756: if node.kwargs: andrej@1756: arg_names += ["pyjslib.Dict(__kwargs)"] andrej@1826: self.printo(" var __r = "+"".join(["[", ", ".join(arg_names), "]"])+";") laurent@371: if node.varargs: laurent@371: self._varargs_handler(node, "__args", arg_names, current_klass) andrej@1826: self.printo(" __r.push.apply(__r, __args.getArray())") andrej@1826: self.printo(" return __r;") andrej@1826: self.printo("};") laurent@371: laurent@371: def _function(self, node, local=False): laurent@371: if local: laurent@371: function_name = node.name laurent@371: self.add_local_arg(function_name) laurent@371: else: laurent@371: function_name = UU + self.modpfx() + node.name laurent@371: laurent@371: arg_names = list(node.argnames) laurent@371: normal_arg_names = list(arg_names) andrej@1756: if node.kwargs: andrej@1756: kwargname = normal_arg_names.pop() andrej@1756: if node.varargs: andrej@1756: varargname = normal_arg_names.pop() laurent@371: declared_arg_names = list(normal_arg_names) andrej@1756: if node.kwargs: andrej@1756: declared_arg_names.append(kwargname) laurent@371: laurent@371: function_args = "(" + ", ".join(declared_arg_names) + ")" andrej@1826: self.printo("%s = function%s {" % (function_name, function_args)) laurent@371: self._default_args_handler(node, normal_arg_names, None) laurent@371: andrej@1730: local_arg_names = normal_arg_names + declared_arg_names laurent@371: laurent@371: if node.varargs: laurent@371: self._varargs_handler(node, varargname, declared_arg_names, None) laurent@371: local_arg_names.append(varargname) laurent@371: laurent@371: # stack of local variable names for this function call laurent@371: self.local_arg_stack.append(local_arg_names) laurent@371: laurent@371: for child in node.code: laurent@371: self._stmt(child, None) laurent@371: laurent@371: # remove the top local arg names laurent@371: self.local_arg_stack.pop() laurent@371: laurent@371: # we need to return null always, so it is not undefined laurent@371: lastStmt = [p for p in node.code][-1] laurent@371: if not isinstance(lastStmt, ast.Return): laurent@371: if not self._isNativeFunc(lastStmt): andrej@1826: self.printo(" return null;") andrej@1826: andrej@1826: self.printo("};") andrej@1826: self.printo("%s.__name__ = '%s';\n" % (function_name, node.name)) laurent@371: laurent@371: self._kwargs_parser(node, function_name, normal_arg_names, None) laurent@371: laurent@371: def _return(self, node, current_klass): laurent@371: expr = self.expr(node.value, current_klass) laurent@371: # in python a function call always returns None, so we do it laurent@371: # here too andrej@1826: self.printo(" return " + expr + ";") laurent@371: laurent@371: def _break(self, node, current_klass): andrej@1826: self.printo(" break;") laurent@371: laurent@371: def _continue(self, node, current_klass): andrej@1826: self.printo(" continue;") laurent@371: laurent@371: def _callfunc(self, v, current_klass): laurent@371: laurent@371: if isinstance(v.node, ast.Name): laurent@371: if v.node.name in self.top_level_functions: laurent@371: call_name = self.modpfx() + v.node.name laurent@371: elif v.node.name in self.top_level_classes: laurent@371: call_name = self.modpfx() + v.node.name andrej@1763: elif v.node.name in self.imported_classes: laurent@371: call_name = self.imported_classes[v.node.name] + '.' + v.node.name laurent@371: elif v.node.name in PYJSLIB_BUILTIN_FUNCTIONS: laurent@371: call_name = 'pyjslib.' + v.node.name laurent@371: elif v.node.name in PYJSLIB_BUILTIN_CLASSES: laurent@371: name = pyjs_builtin_remap(v.node.name) laurent@371: call_name = 'pyjslib.' + name laurent@371: elif v.node.name == "callable": laurent@371: call_name = "pyjslib.isFunction" laurent@371: else: laurent@371: call_name = v.node.name laurent@371: call_args = [] laurent@371: elif isinstance(v.node, ast.Getattr): laurent@371: attr_name = v.node.attrname laurent@371: laurent@371: if isinstance(v.node.expr, ast.Name): laurent@371: call_name = self._name2(v.node.expr, current_klass, attr_name) laurent@371: call_args = [] laurent@371: elif isinstance(v.node.expr, ast.Getattr): laurent@371: call_name = self._getattr2(v.node.expr, current_klass, attr_name) laurent@371: call_args = [] laurent@371: elif isinstance(v.node.expr, ast.CallFunc): laurent@371: call_name = self._callfunc(v.node.expr, current_klass) + "." + v.node.attrname laurent@371: call_args = [] laurent@371: elif isinstance(v.node.expr, ast.Subscript): laurent@371: call_name = self._subscript(v.node.expr, current_klass) + "." + v.node.attrname laurent@371: call_args = [] laurent@371: elif isinstance(v.node.expr, ast.Const): laurent@371: call_name = self.expr(v.node.expr, current_klass) + "." + v.node.attrname laurent@371: call_args = [] laurent@371: else: laurent@371: raise TranslationError("unsupported type (in _callfunc)", v.node.expr) laurent@371: else: laurent@371: raise TranslationError("unsupported type (in _callfunc)", v.node) laurent@371: laurent@371: call_name = strip_py(call_name) laurent@371: laurent@371: kwargs = [] laurent@371: star_arg_name = None laurent@371: if v.star_args: laurent@371: star_arg_name = self.expr(v.star_args, current_klass) laurent@371: laurent@371: for ch4 in v.args: laurent@371: if isinstance(ch4, ast.Keyword): laurent@371: kwarg = ch4.name + ":" + self.expr(ch4.expr, current_klass) laurent@371: kwargs.append(kwarg) laurent@371: else: laurent@371: arg = self.expr(ch4, current_klass) laurent@371: call_args.append(arg) laurent@371: laurent@371: if kwargs: laurent@371: fn_args = ", ".join(['{' + ', '.join(kwargs) + '}']+call_args) laurent@371: else: laurent@371: fn_args = ", ".join(call_args) laurent@371: laurent@371: if kwargs or star_arg_name: laurent@371: if not star_arg_name: laurent@371: star_arg_name = 'null' andrej@1756: try: andrej@1756: call_this, method_name = call_name.rsplit(".", 1) laurent@371: except ValueError: laurent@371: # Must be a function call ... andrej@1785: return ("pyjs_kwargs_function_call("+call_name+", " + andrej@1785: star_arg_name + ", ["+fn_args+"]" + ")") andrej@1785: else: andrej@1785: return ("pyjs_kwargs_method_call("+call_this+", '"+method_name+"', " + andrej@1785: star_arg_name + ", ["+fn_args+"]" + ")") laurent@371: else: laurent@371: return call_name + "(" + ", ".join(call_args) + ")" laurent@371: laurent@371: def _print(self, node, current_klass): laurent@371: if self.optimize: laurent@371: return laurent@371: call_args = [] laurent@371: for ch4 in node.nodes: laurent@371: arg = self.expr(ch4, current_klass) laurent@371: call_args.append(arg) laurent@371: andrej@1826: self.printo("pyjslib.printFunc([", ', '.join(call_args), "],", int(isinstance(node, ast.Printnl)), ");") laurent@371: laurent@371: def _tryExcept(self, node, current_klass): laurent@371: if len(node.handlers) != 1: laurent@371: raise TranslationError("except statements in this form are" + laurent@371: " not supported", node) laurent@371: laurent@371: expr = node.handlers[0][0] laurent@371: as_ = node.handlers[0][1] laurent@371: if as_: laurent@371: errName = as_.name laurent@371: else: laurent@371: errName = 'err' laurent@371: laurent@371: # XXX TODO: check that this should instead be added as a _separate_ laurent@371: # local scope, temporary to the function. oh dearie me. laurent@371: self.add_local_arg(errName) laurent@371: andrej@1826: self.printo(" try {") laurent@371: for stmt in node.body.nodes: laurent@371: self._stmt(stmt, current_klass) andrej@1826: self.printo(" } catch(%s) {" % errName) laurent@371: if expr: andrej@1755: k = [] laurent@371: if isinstance(expr, ast.Tuple): laurent@371: for x in expr.nodes: andrej@1771: k.append("(%(err)s.__name__ == %(expr)s.__name__)" % dict(err=errName, expr=self.expr(x, current_klass))) andrej@1771: else: andrej@1771: k = [" (%(err)s.__name__ == %(expr)s.__name__) " % dict(err=errName, expr=self.expr(expr, current_klass))] andrej@1826: self.printo(" if(%s) {" % '||\n\t\t'.join(k)) laurent@371: for stmt in node.handlers[0][2]: laurent@371: self._stmt(stmt, current_klass) laurent@371: if expr: andrej@1826: # self.printo("} else { throw(%s); } " % errName) andrej@1826: self.printo("}") andrej@1743: if node.else_ is not None: andrej@1826: self.printo(" } finally {") laurent@371: for stmt in node.else_: laurent@371: self._stmt(stmt, current_klass) andrej@1826: self.printo(" }") laurent@371: laurent@371: # XXX: change use_getattr to True to enable "strict" compilation laurent@371: # but incurring a 100% performance penalty. oops. laurent@371: def _getattr(self, v, current_klass, use_getattr=False): laurent@371: attr_name = v.attrname laurent@371: if isinstance(v.expr, ast.Name): laurent@371: obj = self._name(v.expr, current_klass, return_none_for_module=True) andrej@1743: if obj is None and v.expr.name in self.module_imports(): laurent@371: # XXX TODO: distinguish between module import classes laurent@371: # and variables. right now, this is a hack to get laurent@371: # the sys module working. andrej@1782: # if v.expr.name == 'sys': laurent@371: return v.expr.name+'.'+attr_name andrej@1782: # return v.expr.name+'.__'+attr_name+'.prototype.__class__' laurent@371: if not use_getattr or attr_name == '__class__' or \ laurent@371: attr_name == '__name__': laurent@371: return obj + "." + attr_name laurent@371: return "pyjslib.getattr(%s, '%s')" % (obj, attr_name) laurent@371: elif isinstance(v.expr, ast.Getattr): laurent@371: return self._getattr(v.expr, current_klass) + "." + attr_name laurent@371: elif isinstance(v.expr, ast.Subscript): laurent@371: return self._subscript(v.expr, self.modpfx()) + "." + attr_name laurent@371: elif isinstance(v.expr, ast.CallFunc): laurent@371: return self._callfunc(v.expr, self.modpfx()) + "." + attr_name laurent@371: else: laurent@371: raise TranslationError("unsupported type (in _getattr)", v.expr) laurent@371: laurent@371: def modpfx(self): laurent@371: return strip_py(self.module_prefix) andrej@1730: laurent@371: def _name(self, v, current_klass, top_level=False, andrej@1767: return_none_for_module=False): laurent@371: laurent@371: if v.name == 'ilikesillynamesfornicedebugcode': andrej@1826: print(top_level, current_klass, repr(v)) andrej@1826: print(self.top_level_vars) andrej@1826: print(self.top_level_functions) andrej@1826: print(self.local_arg_stack) andrej@1826: print("error...") laurent@371: laurent@371: local_var_names = None laurent@371: las = len(self.local_arg_stack) laurent@371: if las > 0: laurent@371: local_var_names = self.local_arg_stack[-1] laurent@371: laurent@371: if v.name == "True": laurent@371: return "true" laurent@371: elif v.name == "False": laurent@371: return "false" laurent@371: elif v.name == "None": laurent@371: return "null" laurent@371: elif v.name == '__name__' and current_klass is None: laurent@371: return self.modpfx() + v.name laurent@371: elif v.name == self.method_self: laurent@371: return "this" laurent@371: elif v.name in self.top_level_functions: laurent@371: return UU+self.modpfx() + v.name laurent@371: elif v.name in self.method_imported_globals: laurent@371: return UU+self.modpfx() + v.name laurent@371: elif not current_klass and las == 1 and v.name in self.top_level_vars: laurent@371: return UU+self.modpfx() + v.name laurent@371: elif v.name in local_var_names: laurent@371: return v.name andrej@1763: elif v.name in self.imported_classes: laurent@371: return UU+self.imported_classes[v.name] + '.__' + v.name + ".prototype.__class__" laurent@371: elif v.name in self.top_level_classes: laurent@371: return UU+self.modpfx() + "__" + v.name + ".prototype.__class__" laurent@371: elif v.name in self.module_imports() and return_none_for_module: laurent@371: return None laurent@371: elif v.name in PYJSLIB_BUILTIN_CLASSES: andrej@1746: return "pyjslib." + pyjs_builtin_remap(v.name) laurent@371: elif current_klass: laurent@371: if v.name not in local_var_names and \ laurent@371: v.name not in self.top_level_vars and \ laurent@371: v.name not in PYJS_GLOBAL_VARS and \ laurent@371: v.name not in self.top_level_functions: laurent@371: laurent@371: cls_name = current_klass laurent@371: if hasattr(cls_name, "name"): laurent@371: cls_name_ = cls_name.name_ laurent@371: cls_name = cls_name.name laurent@371: else: andrej@1737: cls_name_ = current_klass + "_" # XXX ??? andrej@1767: name = UU+cls_name_ + ".prototype.__class__." + v.name laurent@371: if v.name == 'listener': laurent@371: name = 'listener+' + name laurent@371: return name laurent@371: laurent@371: return v.name laurent@371: laurent@371: def _name2(self, v, current_klass, attr_name): laurent@371: obj = v.name laurent@371: laurent@371: if obj in self.method_imported_globals: laurent@371: call_name = UU+self.modpfx() + obj + "." + attr_name andrej@1763: elif obj in self.imported_classes: andrej@1782: # attr_str = "" andrej@1782: # if attr_name != "__init__": laurent@371: attr_str = ".prototype.__class__." + attr_name laurent@371: call_name = UU+self.imported_classes[obj] + '.__' + obj + attr_str laurent@371: elif obj in self.module_imports(): laurent@371: call_name = obj + "." + attr_name andrej@1737: elif obj[0] == obj[0].upper(): # XXX HACK ALERT laurent@371: call_name = UU + self.modpfx() + "__" + obj + ".prototype.__class__." + attr_name laurent@371: else: laurent@371: call_name = UU+self._name(v, current_klass) + "." + attr_name laurent@371: laurent@371: return call_name laurent@371: laurent@371: def _getattr2(self, v, current_klass, attr_name): laurent@371: if isinstance(v.expr, ast.Getattr): laurent@371: call_name = self._getattr2(v.expr, current_klass, v.attrname + "." + attr_name) laurent@371: elif isinstance(v.expr, ast.Name) and v.expr.name in self.module_imports(): andrej@1742: call_name = UU+v.expr.name + '.__' + v.attrname+".prototype.__class__."+attr_name laurent@371: else: laurent@371: obj = self.expr(v.expr, current_klass) laurent@371: call_name = obj + "." + v.attrname + "." + attr_name laurent@371: laurent@371: return call_name laurent@371: laurent@371: def _class(self, node): laurent@371: """ laurent@371: Handle a class definition. laurent@371: laurent@371: In order to translate python semantics reasonably well, the following laurent@371: structure is used: laurent@371: laurent@371: A special object is created for the class, which inherits attributes laurent@371: from the superclass, or Object if there's no superclass. This is the laurent@371: class object; the object which you refer to when specifying the laurent@371: class by name. Static, class, and unbound methods are copied laurent@371: from the superclass object. laurent@371: laurent@371: A special constructor function is created with the same name as the laurent@371: class, which is used to create instances of that class. laurent@371: laurent@371: A javascript class (e.g. a function with a prototype attribute) is laurent@371: created which is the javascript class of created instances, and laurent@371: which inherits attributes from the class object. Bound methods are laurent@371: copied from the superclass into this class rather than inherited, laurent@371: because the class object contains unbound, class, and static methods laurent@371: that we don't necessarily want to inherit. laurent@371: laurent@371: The type of a method can now be determined by inspecting its laurent@371: static_method, unbound_method, class_method, or instance_method laurent@371: attribute; only one of these should be true. laurent@371: laurent@371: Much of this work is done in pyjs_extend, is pyjslib.py laurent@371: """ laurent@371: class_name = self.modpfx() + uuprefix(node.name, 1) laurent@371: class_name_ = self.modpfx() + uuprefix(node.name) laurent@371: current_klass = Klass(class_name, class_name_) laurent@371: init_method = None laurent@371: for child in node.code: laurent@371: if isinstance(child, ast.Function): laurent@371: current_klass.add_function(child.name) laurent@371: if child.name == "__init__": laurent@371: init_method = child laurent@371: laurent@371: if len(node.bases) == 0: laurent@371: base_class = "pyjslib.__Object" laurent@371: elif len(node.bases) == 1: laurent@371: if isinstance(node.bases[0], ast.Name): andrej@1763: if node.bases[0].name in self.imported_classes: laurent@371: base_class_ = self.imported_classes[node.bases[0].name] + '.__' + node.bases[0].name laurent@371: base_class = self.imported_classes[node.bases[0].name] + '.' + node.bases[0].name laurent@371: else: laurent@371: base_class_ = self.modpfx() + "__" + node.bases[0].name laurent@371: base_class = self.modpfx() + node.bases[0].name laurent@371: elif isinstance(node.bases[0], ast.Getattr): laurent@371: # the bases are not in scope of the class so do not laurent@371: # pass our class to self._name laurent@371: base_class_ = self._name(node.bases[0].expr, None) + \ laurent@371: ".__" + node.bases[0].attrname andrej@1767: base_class = \ andrej@1767: self._name(node.bases[0].expr, None) + \ andrej@1767: "." + node.bases[0].attrname laurent@371: else: laurent@371: raise TranslationError("unsupported type (in _class)", node.bases[0]) laurent@371: laurent@371: current_klass.set_base(base_class) laurent@371: else: laurent@371: raise TranslationError("more than one base (in _class)", node) laurent@371: andrej@1826: self.printo(UU+class_name_ + " = function () {") laurent@371: # call superconstructor andrej@1782: # if base_class: andrej@1826: # self.printo(" __" + base_class + ".call(this);") andrej@1826: self.printo("}") laurent@371: laurent@371: if not init_method: laurent@371: init_method = ast.Function([], "__init__", ["self"], [], 0, None, []) andrej@1782: # self._method(init_method, current_klass, class_name) laurent@371: laurent@371: # Generate a function which constructs the object andrej@1768: clsfunc = ast.Function( andrej@1768: [], node.name, andrej@1768: init_method.argnames[1:], andrej@1768: init_method.defaults, andrej@1768: init_method.flags, andrej@1768: None, andrej@1768: [ast.Discard(ast.CallFunc(ast.Name("JS"), [ast.Const( andrej@1768: # I attempted lazy initialization, but then you can't access static class members andrej@1768: # " if(!__"+base_class+".__was_initialized__)"+ andrej@1768: # " __" + class_name + "_initialize();\n" + andrej@1776: " var instance = new " + UU + class_name_ + "();\n" + andrej@1768: " if(instance.__init__) instance.__init__.apply(instance, arguments);\n" + andrej@1768: " return instance;" laurent@371: )]))]) laurent@371: laurent@371: self._function(clsfunc, False) andrej@1826: self.printo(UU+class_name_ + ".__initialize__ = function () {") andrej@1826: self.printo(" if("+UU+class_name_+".__was_initialized__) return;") andrej@1826: self.printo(" "+UU+class_name_+".__was_initialized__ = true;") laurent@371: cls_obj = UU+class_name_ + '.prototype.__class__' laurent@371: laurent@371: if class_name == "pyjslib.__Object": andrej@1826: self.printo(" "+cls_obj+" = {};") laurent@371: else: laurent@371: if base_class and base_class not in ("object", "pyjslib.__Object"): andrej@1826: self.printo(" if(!"+UU+base_class_+".__was_initialized__)") andrej@1826: self.printo(" "+UU+base_class_+".__initialize__();") andrej@1826: self.printo(" pyjs_extend(" + UU+class_name_ + ", "+UU+base_class_+");") andrej@1826: else: andrej@1826: self.printo(" pyjs_extend(" + UU+class_name_ + ", "+UU+"pyjslib.__Object);") andrej@1826: andrej@1826: self.printo(" "+cls_obj+".__new__ = "+UU+class_name+";") andrej@1826: self.printo(" "+cls_obj+".__name__ = '"+UU+node.name+"';") laurent@371: laurent@371: for child in node.code: laurent@371: if isinstance(child, ast.Pass): laurent@371: pass laurent@371: elif isinstance(child, ast.Function): laurent@371: self._method(child, current_klass, class_name, class_name_) laurent@371: elif isinstance(child, ast.Assign): laurent@371: self.classattr(child, current_klass) laurent@371: elif isinstance(child, ast.Discard) and isinstance(child.expr, ast.Const): laurent@371: # Probably a docstring, turf it laurent@371: pass laurent@371: else: laurent@371: raise TranslationError("unsupported type (in _class)", child) andrej@1826: self.printo("}") andrej@1826: andrej@1826: self.printo(class_name_+".__initialize__();") laurent@371: laurent@371: def classattr(self, node, current_klass): laurent@371: self._assign(node, current_klass, True) laurent@371: laurent@371: def _raise(self, node, current_klass): laurent@371: if node.expr2: laurent@371: raise TranslationError("More than one expression unsupported", laurent@371: node) andrej@1826: self.printo("throw (%s);" % self.expr( andrej@1826: node.expr1, current_klass)) laurent@371: laurent@371: def _method(self, node, current_klass, class_name, class_name_): laurent@371: # reset global var scope laurent@371: self.method_imported_globals = set() laurent@371: laurent@371: arg_names = list(node.argnames) laurent@371: laurent@371: classmethod = False laurent@371: staticmethod = False laurent@371: if node.decorators: laurent@371: for d in node.decorators: laurent@371: if d.name == "classmethod": laurent@371: classmethod = True laurent@371: elif d.name == "staticmethod": laurent@371: staticmethod = True laurent@371: laurent@371: if staticmethod: laurent@371: staticfunc = ast.Function([], class_name_+"."+node.name, node.argnames, node.defaults, node.flags, node.doc, node.code, node.lineno) laurent@371: self._function(staticfunc, True) andrej@1826: self.printo(" " + UU+class_name_ + ".prototype.__class__." + node.name + " = " + class_name_+"."+node.name+";") andrej@1826: self.printo(" " + UU+class_name_ + ".prototype.__class__." + node.name + ".static_method = true;") laurent@371: return laurent@371: else: laurent@371: if len(arg_names) == 0: laurent@371: raise TranslationError("methods must take an argument 'self' (in _method)", node) laurent@371: self.method_self = arg_names[0] laurent@371: andrej@1782: # if not classmethod and arg_names[0] != "self": laurent@371: # raise TranslationError("first arg not 'self' (in _method)", node) laurent@371: laurent@371: normal_arg_names = arg_names[1:] andrej@1756: if node.kwargs: andrej@1756: kwargname = normal_arg_names.pop() andrej@1756: if node.varargs: andrej@1756: varargname = normal_arg_names.pop() laurent@371: declared_arg_names = list(normal_arg_names) andrej@1756: if node.kwargs: andrej@1756: declared_arg_names.append(kwargname) laurent@371: laurent@371: function_args = "(" + ", ".join(declared_arg_names) + ")" laurent@371: laurent@371: if classmethod: laurent@371: fexpr = UU + class_name_ + ".prototype.__class__." + node.name laurent@371: else: laurent@371: fexpr = UU + class_name_ + ".prototype." + node.name andrej@1826: self.printo(" "+fexpr + " = function" + function_args + " {") laurent@371: laurent@371: # default arguments laurent@371: self._default_args_handler(node, normal_arg_names, current_klass) laurent@371: andrej@1730: local_arg_names = normal_arg_names + declared_arg_names laurent@371: laurent@371: if node.varargs: laurent@371: self._varargs_handler(node, varargname, declared_arg_names, current_klass) laurent@371: local_arg_names.append(varargname) laurent@371: laurent@371: # stack of local variable names for this function call laurent@371: self.local_arg_stack.append(local_arg_names) laurent@371: laurent@371: for child in node.code: laurent@371: self._stmt(child, current_klass) laurent@371: laurent@371: # remove the top local arg names laurent@371: self.local_arg_stack.pop() laurent@371: andrej@1826: self.printo(" };") laurent@371: laurent@371: self._kwargs_parser(node, fexpr, normal_arg_names, current_klass) laurent@371: laurent@371: if classmethod: laurent@371: # Have to create a version on the instances which automatically passes the laurent@371: # class as "self" laurent@371: altexpr = UU + class_name_ + ".prototype." + node.name andrej@1826: self.printo(" "+altexpr + " = function() {") andrej@1826: self.printo(" return " + fexpr + ".apply(this.__class__, arguments);") andrej@1826: self.printo(" };") andrej@1826: self.printo(" "+fexpr+".class_method = true;") andrej@1826: self.printo(" "+altexpr+".instance_method = true;") laurent@371: else: laurent@371: # For instance methods, we need an unbound version in the class object laurent@371: altexpr = UU + class_name_ + ".prototype.__class__." + node.name andrej@1826: self.printo(" "+altexpr + " = function() {") andrej@1826: self.printo(" return " + fexpr + ".call.apply("+fexpr+", arguments);") andrej@1826: self.printo(" };") andrej@1826: self.printo(" "+altexpr+".unbound_method = true;") andrej@1826: self.printo(" "+fexpr+".instance_method = true;") andrej@1826: self.printo(" "+altexpr+".__name__ = '%s';" % node.name) andrej@1826: andrej@1826: self.printo(UU + class_name_ + ".prototype.%s.__name__ = '%s';" % andrej@1826: (node.name, node.name)) laurent@371: laurent@371: if node.kwargs or len(node.defaults): andrej@1826: self.printo(" "+altexpr + ".parse_kwargs = " + fexpr + ".parse_kwargs;") laurent@371: laurent@371: self.method_self = None laurent@371: self.method_imported_globals = set() laurent@371: laurent@371: def _isNativeFunc(self, node): laurent@371: if isinstance(node, ast.Discard): laurent@371: if isinstance(node.expr, ast.CallFunc): laurent@371: if isinstance(node.expr.node, ast.Name) and \ laurent@371: node.expr.node.name == NATIVE_JS_FUNC_NAME: laurent@371: return True laurent@371: return False laurent@371: laurent@371: def _stmt(self, node, current_klass): laurent@371: debugStmt = self.debug and not self._isNativeFunc(node) laurent@371: if debugStmt: andrej@1826: self.printo(' try {') laurent@371: laurent@371: if isinstance(node, ast.Return): laurent@371: self._return(node, current_klass) laurent@371: elif isinstance(node, ast.Break): laurent@371: self._break(node, current_klass) laurent@371: elif isinstance(node, ast.Continue): laurent@371: self._continue(node, current_klass) laurent@371: elif isinstance(node, ast.Assign): laurent@371: self._assign(node, current_klass) laurent@371: elif isinstance(node, ast.AugAssign): laurent@371: self._augassign(node, current_klass) laurent@371: elif isinstance(node, ast.Discard): laurent@371: self._discard(node, current_klass) laurent@371: elif isinstance(node, ast.If): laurent@371: self._if(node, current_klass) laurent@371: elif isinstance(node, ast.For): laurent@371: self._for(node, current_klass) laurent@371: elif isinstance(node, ast.While): laurent@371: self._while(node, current_klass) laurent@371: elif isinstance(node, ast.Subscript): laurent@371: self._subscript_stmt(node, current_klass) laurent@371: elif isinstance(node, ast.Global): laurent@371: self._global(node, current_klass) laurent@371: elif isinstance(node, ast.Pass): laurent@371: pass laurent@371: elif isinstance(node, ast.Function): laurent@371: self._function(node, True) laurent@371: elif isinstance(node, ast.Printnl): andrej@1757: self._print(node, current_klass) laurent@371: elif isinstance(node, ast.Print): andrej@1757: self._print(node, current_klass) laurent@371: elif isinstance(node, ast.TryExcept): laurent@371: self._tryExcept(node, current_klass) laurent@371: elif isinstance(node, ast.Raise): laurent@371: self._raise(node, current_klass) laurent@371: else: laurent@371: raise TranslationError("unsupported type (in _stmt)", node) laurent@371: laurent@371: if debugStmt: laurent@371: laurent@371: lt = self.get_line_trace(node) laurent@371: isHaltFunction = self.module_prefix + "IsHaltException" laurent@371: andrej@1767: out = ( andrej@1767: ' } catch (__err) {', andrej@1767: ' if (' + isHaltFunction + '(__err.name)) {', andrej@1767: ' throw __err;', andrej@1767: ' } else {', andrej@1767: ' st = sys.printstack() + ' + '"%s"' % lt + "+ '\\n' ;" andrej@1785: ' alert("' + 'Error in ' + lt + '"' + andrej@1785: '+"\\n"+__err.name+": "+__err.message' + andrej@1785: '+"\\n\\nStack trace:\\n"' + '+st' + ');', andrej@1767: ' debugger;', andrej@1767: ' throw new ' + self.module_prefix + 'HaltException();', andrej@1767: ' }', andrej@1767: ' }' andrej@1767: ) andrej@1767: for s in out: andrej@1826: self.printo(s) laurent@371: laurent@371: def get_line_trace(self, node): laurent@371: lineNum = "Unknown" laurent@371: srcLine = "" laurent@371: if hasattr(node, "lineno"): andrej@1743: if node.lineno is not None: laurent@371: lineNum = node.lineno laurent@371: srcLine = self.src[min(lineNum, len(self.src))-1] laurent@371: srcLine = srcLine.replace('\\', '\\\\') laurent@371: srcLine = srcLine.replace('"', '\\"') laurent@371: srcLine = srcLine.replace("'", "\\'") laurent@371: laurent@371: return self.raw_module_name + ".py, line " \ andrej@1767: + str(lineNum) + ":"\ andrej@1767: + "\\n" \ andrej@1767: + " " + srcLine laurent@371: laurent@371: def _augassign(self, node, current_klass): laurent@371: v = node.node laurent@371: if isinstance(v, ast.Getattr): laurent@371: # XXX HACK! don't allow += on return result of getattr. laurent@371: # TODO: create a temporary variable or something. laurent@371: lhs = self._getattr(v, current_klass, False) laurent@371: else: laurent@371: lhs = self._name(node.node, current_klass) laurent@371: op = node.op laurent@371: rhs = self.expr(node.expr, current_klass) andrej@1826: self.printo(" " + lhs + " " + op + " " + rhs + ";") laurent@371: andrej@1744: def _assign(self, node, current_klass, top_level=False): laurent@371: if len(node.nodes) != 1: laurent@371: tempvar = '__temp'+str(node.lineno) laurent@371: tnode = ast.Assign([ast.AssName(tempvar, "OP_ASSIGN", node.lineno)], node.expr, node.lineno) laurent@371: self._assign(tnode, current_klass, top_level) laurent@371: for v in node.nodes: andrej@1757: tnode2 = ast.Assign([v], ast.Name(tempvar, node.lineno), node.lineno) andrej@1757: self._assign(tnode2, current_klass, top_level) laurent@371: return laurent@371: laurent@371: local_var_names = None laurent@371: if len(self.local_arg_stack) > 0: laurent@371: local_var_names = self.local_arg_stack[-1] laurent@371: laurent@371: def _lhsFromAttr(v, current_klass): laurent@371: attr_name = v.attrname laurent@371: if isinstance(v.expr, ast.Name): laurent@371: lhs = self._name(v.expr, current_klass) + "." + attr_name laurent@371: elif isinstance(v.expr, ast.Getattr): laurent@371: lhs = self._getattr(v, current_klass) laurent@371: elif isinstance(v.expr, ast.Subscript): laurent@371: lhs = self._subscript(v.expr, current_klass) + "." + attr_name laurent@371: else: laurent@371: raise TranslationError("unsupported type (in _assign)", v.expr) laurent@371: return lhs laurent@371: laurent@371: def _lhsFromName(v, top_level, current_klass): laurent@371: if top_level: laurent@371: if current_klass: laurent@371: lhs = UU+current_klass.name_ + ".prototype.__class__." \ laurent@371: + v.name laurent@371: else: laurent@371: self.top_level_vars.add(v.name) laurent@371: vname = self.modpfx() + v.name laurent@371: if not self.modpfx() and v.name not in\ andrej@1767: self.method_imported_globals: laurent@371: lhs = "var " + vname laurent@371: else: laurent@371: lhs = UU + vname laurent@371: self.add_local_arg(v.name) laurent@371: else: laurent@371: if v.name in local_var_names: laurent@371: lhs = v.name laurent@371: elif v.name in self.method_imported_globals: laurent@371: lhs = self.modpfx() + v.name laurent@371: else: laurent@371: lhs = "var " + v.name laurent@371: self.add_local_arg(v.name) laurent@371: return lhs laurent@371: laurent@371: dbg = 0 laurent@371: v = node.nodes[0] laurent@371: if isinstance(v, ast.AssAttr): laurent@371: lhs = _lhsFromAttr(v, current_klass) laurent@371: if v.flags == "OP_ASSIGN": laurent@371: op = "=" laurent@371: else: laurent@371: raise TranslationError("unsupported flag (in _assign)", v) laurent@371: laurent@371: elif isinstance(v, ast.AssName): laurent@371: lhs = _lhsFromName(v, top_level, current_klass) laurent@371: if v.flags == "OP_ASSIGN": laurent@371: op = "=" laurent@371: else: laurent@371: raise TranslationError("unsupported flag (in _assign)", v) laurent@371: elif isinstance(v, ast.Subscript): laurent@371: if v.flags == "OP_ASSIGN": laurent@371: obj = self.expr(v.expr, current_klass) laurent@371: if len(v.subs) != 1: laurent@371: raise TranslationError("must have one sub (in _assign)", v) laurent@371: idx = self.expr(v.subs[0], current_klass) laurent@371: value = self.expr(node.expr, current_klass) andrej@1826: self.printo(" " + obj + ".__setitem__(" + idx + ", " + value + ");") laurent@371: return laurent@371: else: laurent@371: raise TranslationError("unsupported flag (in _assign)", v) laurent@371: elif isinstance(v, (ast.AssList, ast.AssTuple)): laurent@371: uniqueID = self.nextTupleAssignID laurent@371: self.nextTupleAssignID += 1 laurent@371: tempName = "__tupleassign" + str(uniqueID) + "__" andrej@1826: self.printo(" var " + tempName + " = " + self.expr(node.expr, current_klass) + ";") andrej@1740: for index, child in enumerate(v.getChildNodes()): laurent@371: rhs = tempName + ".__getitem__(" + str(index) + ")" laurent@371: laurent@371: if isinstance(child, ast.AssAttr): laurent@371: lhs = _lhsFromAttr(child, current_klass) laurent@371: elif isinstance(child, ast.AssName): laurent@371: lhs = _lhsFromName(child, top_level, current_klass) laurent@371: elif isinstance(child, ast.Subscript): laurent@371: if child.flags == "OP_ASSIGN": laurent@371: obj = self.expr(child.expr, current_klass) laurent@371: if len(child.subs) != 1: laurent@371: raise TranslationError("must have one sub " + laurent@371: "(in _assign)", child) laurent@371: idx = self.expr(child.subs[0], current_klass) laurent@371: value = self.expr(node.expr, current_klass) andrej@1826: self.printo(" " + obj + ".__setitem__(" + idx + ", " + rhs + ");") laurent@371: continue andrej@1826: self.printo(" " + lhs + " = " + rhs + ";") laurent@371: return laurent@371: else: laurent@371: raise TranslationError("unsupported type (in _assign)", v) laurent@371: laurent@371: rhs = self.expr(node.expr, current_klass) laurent@371: if dbg: andrej@1826: print("b", repr(node.expr), rhs) andrej@1826: self.printo(" " + lhs + " " + op + " " + rhs + ";") laurent@371: laurent@371: def _discard(self, node, current_klass): andrej@1730: laurent@371: if isinstance(node.expr, ast.CallFunc): laurent@371: debugStmt = self.debug and not self._isNativeFunc(node) laurent@371: if debugStmt and isinstance(node.expr.node, ast.Name) and \ laurent@371: node.expr.node.name == 'import_wait': andrej@1757: debugStmt = False laurent@371: if debugStmt: laurent@371: st = self.get_line_trace(node) andrej@1826: self.printo("sys.addstack('%s');\n" % st) laurent@371: if isinstance(node.expr.node, ast.Name) and node.expr.node.name == NATIVE_JS_FUNC_NAME: laurent@371: if len(node.expr.args) != 1: laurent@371: raise TranslationError("native javascript function %s must have one arg" % NATIVE_JS_FUNC_NAME, node.expr) laurent@371: if not isinstance(node.expr.args[0], ast.Const): laurent@371: raise TranslationError("native javascript function %s must have constant arg" % NATIVE_JS_FUNC_NAME, node.expr) laurent@371: raw_js = node.expr.args[0].value andrej@1826: self.printo(raw_js) laurent@371: else: laurent@371: expr = self._callfunc(node.expr, current_klass) andrej@1826: self.printo(" " + expr + ";") laurent@371: laurent@371: if debugStmt: andrej@1826: self.printo("sys.popstack();\n") laurent@371: laurent@371: elif isinstance(node.expr, ast.Const): andrej@1737: if node.expr.value is not None: # Empty statements generate ignore None andrej@1826: self.printo(self._const(node.expr)) laurent@371: else: laurent@371: raise TranslationError("unsupported type (in _discard)", node.expr) laurent@371: laurent@371: def _if(self, node, current_klass): laurent@371: for i in range(len(node.tests)): laurent@371: test, consequence = node.tests[i] laurent@371: if i == 0: laurent@371: keyword = "if" laurent@371: else: laurent@371: keyword = "else if" laurent@371: laurent@371: self._if_test(keyword, test, consequence, current_klass) laurent@371: laurent@371: if node.else_: laurent@371: keyword = "else" laurent@371: test = None laurent@371: consequence = node.else_ laurent@371: laurent@371: self._if_test(keyword, test, consequence, current_klass) laurent@371: laurent@371: def _if_test(self, keyword, test, consequence, current_klass): laurent@371: if test: laurent@371: expr = self.expr(test, current_klass) laurent@371: andrej@1826: self.printo(" " + keyword + " (pyjslib.bool(" + expr + ")) {") andrej@1826: else: andrej@1826: self.printo(" " + keyword + " {") laurent@371: laurent@371: if isinstance(consequence, ast.Stmt): laurent@371: for child in consequence.nodes: laurent@371: self._stmt(child, current_klass) laurent@371: else: laurent@371: raise TranslationError("unsupported type (in _if_test)", consequence) laurent@371: andrej@1826: self.printo(" }") laurent@371: laurent@371: def _from(self, node): laurent@371: for name in node.names: laurent@371: # look up "hack" in AppTranslator as to how findFile gets here laurent@371: module_name = node.modname + "." + name[0] laurent@371: try: laurent@371: ff = self.findFile(module_name + ".py") laurent@371: except Exception: laurent@371: ff = None laurent@371: if ff: laurent@371: self.add_imported_module(module_name) laurent@371: else: laurent@371: self.imported_classes[name[0]] = node.modname laurent@371: laurent@371: def _compare(self, node, current_klass): laurent@371: lhs = self.expr(node.expr, current_klass) laurent@371: laurent@371: if len(node.ops) != 1: laurent@371: raise TranslationError("only one ops supported (in _compare)", node) laurent@371: laurent@371: op = node.ops[0][0] laurent@371: rhs_node = node.ops[0][1] laurent@371: rhs = self.expr(rhs_node, current_klass) laurent@371: laurent@371: if op == "==": laurent@371: return "pyjslib.eq(%s, %s)" % (lhs, rhs) laurent@371: if op == "in": laurent@371: return rhs + ".__contains__(" + lhs + ")" laurent@371: elif op == "not in": laurent@371: return "!" + rhs + ".__contains__(" + lhs + ")" laurent@371: elif op == "is": laurent@371: op = "===" laurent@371: elif op == "is not": laurent@371: op = "!==" laurent@371: laurent@371: return "(" + lhs + " " + op + " " + rhs + ")" laurent@371: laurent@371: def _not(self, node, current_klass): laurent@371: expr = self.expr(node.expr, current_klass) laurent@371: laurent@371: return "!(" + expr + ")" laurent@371: laurent@371: def _or(self, node, current_klass): laurent@371: expr = "("+(") || (".join([self.expr(child, current_klass) for child in node.nodes]))+')' laurent@371: return expr laurent@371: laurent@371: def _and(self, node, current_klass): laurent@371: expr = "("+(") && (".join([self.expr(child, current_klass) for child in node.nodes]))+")" laurent@371: return expr laurent@371: laurent@371: def _for(self, node, current_klass): laurent@371: assign_name = "" laurent@371: assign_tuple = "" laurent@371: laurent@371: # based on Bob Ippolito's Iteration in Javascript code laurent@371: if isinstance(node.assign, ast.AssName): laurent@371: assign_name = node.assign.name laurent@371: self.add_local_arg(assign_name) laurent@371: if node.assign.flags == "OP_ASSIGN": laurent@371: op = "=" laurent@371: elif isinstance(node.assign, ast.AssTuple): laurent@371: op = "=" laurent@371: i = 0 laurent@371: for child in node.assign: laurent@371: child_name = child.name laurent@371: if assign_name == "": laurent@371: assign_name = "temp_" + child_name laurent@371: self.add_local_arg(child_name) laurent@371: assign_tuple += """ laurent@371: var %(child_name)s %(op)s %(assign_name)s.__getitem__(%(i)i); laurent@371: """ % locals() laurent@371: i += 1 laurent@371: else: laurent@371: raise TranslationError("unsupported type (in _for)", node.assign) laurent@371: laurent@371: if isinstance(node.list, ast.Name): laurent@371: list_expr = self._name(node.list, current_klass) laurent@371: elif isinstance(node.list, ast.Getattr): laurent@371: list_expr = self._getattr(node.list, current_klass) laurent@371: elif isinstance(node.list, ast.CallFunc): laurent@371: list_expr = self._callfunc(node.list, current_klass) laurent@371: else: laurent@371: raise TranslationError("unsupported type (in _for)", node.list) laurent@371: laurent@371: lhs = "var " + assign_name laurent@371: iterator_name = "__" + assign_name laurent@371: andrej@1846: loc_dict = { andrej@1846: "iterator_name": iterator_name, andrej@1846: "list_expr": list_expr, andrej@1846: "lhs": lhs, andrej@1846: "op": op, andrej@1846: "assign_tuple": assign_tuple, andrej@1846: } andrej@1846: andrej@1826: self.printo(""" laurent@371: var %(iterator_name)s = %(list_expr)s.__iter__(); laurent@371: try { laurent@371: while (true) { laurent@371: %(lhs)s %(op)s %(iterator_name)s.next(); laurent@371: %(assign_tuple)s andrej@1846: """ % loc_dict) laurent@371: for node in node.body.nodes: laurent@371: self._stmt(node, current_klass) andrej@1826: self.printo(""" laurent@371: } laurent@371: } catch (e) { laurent@371: if (e.__name__ != pyjslib.StopIteration.__name__) { laurent@371: throw e; laurent@371: } laurent@371: } andrej@1846: """) laurent@371: laurent@371: def _while(self, node, current_klass): laurent@371: test = self.expr(node.test, current_klass) andrej@1826: self.printo(" while (pyjslib.bool(" + test + ")) {") laurent@371: if isinstance(node.body, ast.Stmt): laurent@371: for child in node.body.nodes: laurent@371: self._stmt(child, current_klass) laurent@371: else: laurent@371: raise TranslationError("unsupported type (in _while)", node.body) andrej@1826: self.printo(" }") laurent@371: laurent@371: def _const(self, node): laurent@371: if isinstance(node.value, int): laurent@371: return str(node.value) laurent@371: elif isinstance(node.value, float): laurent@371: return str(node.value) laurent@371: elif isinstance(node.value, basestring): laurent@371: v = node.value laurent@371: if isinstance(node.value, unicode): laurent@371: v = v.encode('utf-8') andrej@1738: return "String('%s')" % escapejs(v) laurent@371: elif node.value is None: laurent@371: return "null" laurent@371: else: laurent@371: raise TranslationError("unsupported type (in _const)", node) laurent@371: laurent@371: def _unaryadd(self, node, current_klass): laurent@371: return self.expr(node.expr, current_klass) laurent@371: laurent@371: def _unarysub(self, node, current_klass): laurent@371: return "-" + self.expr(node.expr, current_klass) laurent@371: laurent@371: def _add(self, node, current_klass): laurent@371: return self.expr(node.left, current_klass) + " + " + self.expr(node.right, current_klass) laurent@371: laurent@371: def _sub(self, node, current_klass): laurent@371: return self.expr(node.left, current_klass) + " - " + self.expr(node.right, current_klass) laurent@371: laurent@371: def _div(self, node, current_klass): laurent@371: return self.expr(node.left, current_klass) + " / " + self.expr(node.right, current_klass) laurent@371: laurent@371: def _mul(self, node, current_klass): laurent@371: return self.expr(node.left, current_klass) + " * " + self.expr(node.right, current_klass) laurent@371: laurent@371: def _mod(self, node, current_klass): laurent@371: if isinstance(node.left, ast.Const) and isinstance(node.left.value, StringType): andrej@1757: self.imported_js.add("sprintf.js") # Include the sprintf functionality if it is used andrej@1757: return "sprintf("+self.expr(node.left, current_klass) + ", " + self.expr(node.right, current_klass)+")" laurent@371: return self.expr(node.left, current_klass) + " % " + self.expr(node.right, current_klass) laurent@371: laurent@371: def _invert(self, node, current_klass): laurent@371: return "~" + self.expr(node.expr, current_klass) laurent@371: laurent@371: def _bitand(self, node, current_klass): laurent@371: return " & ".join([self.expr(child, current_klass) for child in node.nodes]) laurent@371: laurent@371: def _bitshiftleft(self, node, current_klass): laurent@371: return self.expr(node.left, current_klass) + " << " + self.expr(node.right, current_klass) laurent@371: laurent@371: def _bitshiftright(self, node, current_klass): laurent@371: return self.expr(node.left, current_klass) + " >>> " + self.expr(node.right, current_klass) laurent@371: andrej@1740: def _bitxor(self, node, current_klass): laurent@371: return " ^ ".join([self.expr(child, current_klass) for child in node.nodes]) laurent@371: laurent@371: def _bitor(self, node, current_klass): laurent@371: return " | ".join([self.expr(child, current_klass) for child in node.nodes]) laurent@371: laurent@371: def _subscript(self, node, current_klass): laurent@371: if node.flags == "OP_APPLY": laurent@371: if len(node.subs) == 1: laurent@371: return self.expr(node.expr, current_klass) + ".__getitem__(" + self.expr(node.subs[0], current_klass) + ")" laurent@371: else: laurent@371: raise TranslationError("must have one sub (in _subscript)", node) laurent@371: else: laurent@371: raise TranslationError("unsupported flag (in _subscript)", node) laurent@371: laurent@371: def _subscript_stmt(self, node, current_klass): laurent@371: if node.flags == "OP_DELETE": andrej@1826: self.printo(" " + self.expr(node.expr, current_klass) + ".__delitem__(" + self.expr(node.subs[0], current_klass) + ");") laurent@371: else: laurent@371: raise TranslationError("unsupported flag (in _subscript)", node) laurent@371: laurent@371: def _list(self, node, current_klass): laurent@371: return "new pyjslib.List([" + ", ".join([self.expr(x, current_klass) for x in node.nodes]) + "])" laurent@371: laurent@371: def _dict(self, node, current_klass): laurent@371: items = [] laurent@371: for x in node.items: laurent@371: key = self.expr(x[0], current_klass) laurent@371: value = self.expr(x[1], current_klass) laurent@371: items.append("[" + key + ", " + value + "]") laurent@371: return "new pyjslib.Dict([" + ", ".join(items) + "])" laurent@371: laurent@371: def _tuple(self, node, current_klass): laurent@371: return "new pyjslib.Tuple([" + ", ".join([self.expr(x, current_klass) for x in node.nodes]) + "])" laurent@371: laurent@371: def _lambda(self, node, current_klass): laurent@371: if node.varargs: laurent@371: raise TranslationError("varargs are not supported in Lambdas", node) laurent@371: if node.kwargs: laurent@371: raise TranslationError("kwargs are not supported in Lambdas", node) laurent@371: res = cStringIO.StringIO() laurent@371: arg_names = list(node.argnames) laurent@371: function_args = ", ".join(arg_names) laurent@371: for child in node.getChildNodes(): laurent@371: expr = self.expr(child, None) andrej@1826: print("function (%s){" % function_args, file=res) laurent@371: self._default_args_handler(node, arg_names, None, laurent@371: output=res) andrej@1826: print('return %s;}' % expr, file=res) laurent@371: return res.getvalue() laurent@371: laurent@371: def _slice(self, node, current_klass): laurent@371: if node.flags == "OP_APPLY": laurent@371: lower = "null" laurent@371: upper = "null" andrej@1743: if node.lower is not None: laurent@371: lower = self.expr(node.lower, current_klass) andrej@1743: if node.upper is not None: laurent@371: upper = self.expr(node.upper, current_klass) andrej@1738: return "pyjslib.slice(" + self.expr(node.expr, current_klass) + ", " + lower + ", " + upper + ")" laurent@371: else: laurent@371: raise TranslationError("unsupported flag (in _slice)", node) laurent@371: laurent@371: def _global(self, node, current_klass): laurent@371: for name in node.names: laurent@371: self.method_imported_globals.add(name) laurent@371: laurent@371: def expr(self, node, current_klass): laurent@371: if isinstance(node, ast.Const): laurent@371: return self._const(node) laurent@371: # @@@ not sure if the parentheses should be here or in individual operator functions - JKT laurent@371: elif isinstance(node, ast.Mul): laurent@371: return " ( " + self._mul(node, current_klass) + " ) " laurent@371: elif isinstance(node, ast.Add): laurent@371: return " ( " + self._add(node, current_klass) + " ) " laurent@371: elif isinstance(node, ast.Sub): laurent@371: return " ( " + self._sub(node, current_klass) + " ) " laurent@371: elif isinstance(node, ast.Div): laurent@371: return " ( " + self._div(node, current_klass) + " ) " laurent@371: elif isinstance(node, ast.Mod): laurent@371: return self._mod(node, current_klass) laurent@371: elif isinstance(node, ast.UnaryAdd): laurent@371: return self._unaryadd(node, current_klass) laurent@371: elif isinstance(node, ast.UnarySub): laurent@371: return self._unarysub(node, current_klass) laurent@371: elif isinstance(node, ast.Not): laurent@371: return self._not(node, current_klass) laurent@371: elif isinstance(node, ast.Or): laurent@371: return self._or(node, current_klass) laurent@371: elif isinstance(node, ast.And): laurent@371: return self._and(node, current_klass) laurent@371: elif isinstance(node, ast.Invert): laurent@371: return self._invert(node, current_klass) laurent@371: elif isinstance(node, ast.Bitand): laurent@371: return "("+self._bitand(node, current_klass)+")" andrej@1740: elif isinstance(node, ast.LeftShift): laurent@371: return self._bitshiftleft(node, current_klass) laurent@371: elif isinstance(node, ast.RightShift): laurent@371: return self._bitshiftright(node, current_klass) laurent@371: elif isinstance(node, ast.Bitxor): laurent@371: return "("+self._bitxor(node, current_klass)+")" laurent@371: elif isinstance(node, ast.Bitor): laurent@371: return "("+self._bitor(node, current_klass)+")" laurent@371: elif isinstance(node, ast.Compare): laurent@371: return self._compare(node, current_klass) laurent@371: elif isinstance(node, ast.CallFunc): laurent@371: return self._callfunc(node, current_klass) laurent@371: elif isinstance(node, ast.Name): laurent@371: return self._name(node, current_klass) laurent@371: elif isinstance(node, ast.Subscript): laurent@371: return self._subscript(node, current_klass) laurent@371: elif isinstance(node, ast.Getattr): laurent@371: return self._getattr(node, current_klass) laurent@371: elif isinstance(node, ast.List): laurent@371: return self._list(node, current_klass) laurent@371: elif isinstance(node, ast.Dict): laurent@371: return self._dict(node, current_klass) laurent@371: elif isinstance(node, ast.Tuple): laurent@371: return self._tuple(node, current_klass) laurent@371: elif isinstance(node, ast.Slice): laurent@371: return self._slice(node, current_klass) laurent@371: elif isinstance(node, ast.Lambda): laurent@371: return self._lambda(node, current_klass) laurent@371: else: laurent@371: raise TranslationError("unsupported type (in expr)", node) laurent@371: laurent@371: laurent@371: def translate(file_name, module_name, debug=False): laurent@371: f = file(file_name, "r") laurent@371: src = f.read() laurent@371: f.close() laurent@371: output = cStringIO.StringIO() laurent@371: mod = compiler.parseFile(file_name) andrej@1846: Translator(module_name, module_name, module_name, src, debug, mod, output) laurent@371: return output.getvalue() laurent@371: laurent@371: andrej@1851: class PlatformParser(object): andrej@1744: def __init__(self, platform_dir="", verbose=True): laurent@371: self.platform_dir = platform_dir laurent@371: self.parse_cache = {} laurent@371: self.platform = "" laurent@371: self.verbose = verbose laurent@371: laurent@371: def setPlatform(self, platform): laurent@371: self.platform = platform laurent@371: laurent@371: def parseModule(self, module_name, file_name): laurent@371: laurent@371: importing = False andrej@1775: if file_name not in self.parse_cache: laurent@371: importing = True laurent@371: mod = compiler.parseFile(file_name) laurent@371: self.parse_cache[file_name] = mod laurent@371: else: laurent@371: mod = self.parse_cache[file_name] laurent@371: laurent@371: override = False laurent@371: platform_file_name = self.generatePlatformFilename(file_name) laurent@371: if self.platform and os.path.isfile(platform_file_name): laurent@371: mod = copy.deepcopy(mod) laurent@371: mod_override = compiler.parseFile(platform_file_name) laurent@371: self.merge(mod, mod_override) laurent@371: override = True laurent@371: laurent@371: if self.verbose: laurent@371: if override: andrej@1826: print("Importing %s (Platform %s)" % (module_name, self.platform)) laurent@371: elif importing: andrej@1826: print("Importing %s" % (module_name)) laurent@371: laurent@371: return mod, override laurent@371: laurent@371: def generatePlatformFilename(self, file_name): laurent@371: (module_name, extension) = os.path.splitext(os.path.basename(file_name)) laurent@371: platform_file_name = module_name + self.platform + extension laurent@371: laurent@371: return os.path.join(os.path.dirname(file_name), self.platform_dir, platform_file_name) laurent@371: laurent@371: def merge(self, tree1, tree2): laurent@371: for child in tree2.node: laurent@371: if isinstance(child, ast.Function): laurent@371: self.replaceFunction(tree1, child.name, child) laurent@371: elif isinstance(child, ast.Class): laurent@371: self.replaceClassMethods(tree1, child.name, child) laurent@371: laurent@371: return tree1 laurent@371: laurent@371: def replaceFunction(self, tree, function_name, function_node): laurent@371: # find function to replace laurent@371: for child in tree.node: laurent@371: if isinstance(child, ast.Function) and child.name == function_name: laurent@371: self.copyFunction(child, function_node) laurent@371: return laurent@371: raise TranslationError("function not found: " + function_name, function_node) laurent@371: laurent@371: def replaceClassMethods(self, tree, class_name, class_node): laurent@371: # find class to replace laurent@371: old_class_node = None laurent@371: for child in tree.node: laurent@371: if isinstance(child, ast.Class) and child.name == class_name: laurent@371: old_class_node = child laurent@371: break laurent@371: laurent@371: if not old_class_node: laurent@371: raise TranslationError("class not found: " + class_name, class_node) laurent@371: laurent@371: # replace methods laurent@371: for function_node in class_node.code: laurent@371: if isinstance(function_node, ast.Function): laurent@371: found = False laurent@371: for child in old_class_node.code: laurent@371: if isinstance(child, ast.Function) and child.name == function_node.name: laurent@371: found = True laurent@371: self.copyFunction(child, function_node) laurent@371: break laurent@371: laurent@371: if not found: laurent@371: raise TranslationError("class method not found: " + class_name + "." + function_node.name, function_node) laurent@371: laurent@371: def copyFunction(self, target, source): laurent@371: target.code = source.code laurent@371: target.argnames = source.argnames laurent@371: target.defaults = source.defaults andrej@1737: target.doc = source.doc # @@@ not sure we need to do this any more laurent@371: andrej@1736: laurent@371: def dotreplace(fname): laurent@371: path, ext = os.path.splitext(fname) laurent@371: return path.replace(".", "/") + ext laurent@371: andrej@1736: andrej@1851: class AppTranslator(object): laurent@371: andrej@1852: def __init__(self, library_dirs=None, parser=None, dynamic=False, laurent@371: optimize=False, verbose=True): laurent@371: self.extension = ".py" laurent@371: self.optimize = optimize laurent@371: self.library_modules = [] laurent@371: self.overrides = {} andrej@1852: library_dirs = [] if library_dirs is None else library_dirs laurent@371: self.library_dirs = path + library_dirs laurent@371: self.dynamic = dynamic laurent@371: self.verbose = verbose laurent@371: laurent@371: if not parser: laurent@371: self.parser = PlatformParser() laurent@371: else: laurent@371: self.parser = parser laurent@371: laurent@371: self.parser.dynamic = dynamic laurent@371: laurent@371: def findFile(self, file_name): laurent@371: if os.path.isfile(file_name): laurent@371: return file_name laurent@371: laurent@371: for library_dir in self.library_dirs: laurent@371: file_name = dotreplace(file_name) laurent@371: full_file_name = os.path.join( laurent@371: os.path.abspath(os.path.dirname(__file__)), library_dir, file_name) laurent@371: if os.path.isfile(full_file_name): laurent@371: return full_file_name laurent@371: andrej@1847: fnameinit, _ext = os.path.splitext(file_name) laurent@371: fnameinit = fnameinit + "/__init__.py" laurent@371: laurent@371: full_file_name = os.path.join( laurent@371: os.path.abspath(os.path.dirname(__file__)), library_dir, fnameinit) laurent@371: if os.path.isfile(full_file_name): laurent@371: return full_file_name laurent@371: laurent@371: raise Exception("file not found: " + file_name) laurent@371: laurent@371: def _translate(self, module_name, is_app=True, debug=False, andrej@1852: imported_js=None): laurent@371: if module_name not in self.library_modules: laurent@371: self.library_modules.append(module_name) laurent@371: laurent@371: file_name = self.findFile(module_name + self.extension) laurent@371: laurent@371: output = cStringIO.StringIO() laurent@371: laurent@371: f = file(file_name, "r") laurent@371: src = f.read() laurent@371: f.close() laurent@371: laurent@371: mod, override = self.parser.parseModule(module_name, file_name) laurent@371: if override: laurent@371: override_name = "%s.%s" % (self.parser.platform.lower(), andrej@1767: module_name) laurent@371: self.overrides[override_name] = override_name laurent@371: if is_app: laurent@371: mn = '__main__' laurent@371: else: laurent@371: mn = module_name laurent@371: t = Translator(mn, module_name, module_name, laurent@371: src, debug, mod, output, self.dynamic, self.optimize, laurent@371: self.findFile) laurent@371: laurent@371: module_str = output.getvalue() andrej@1852: if imported_js is None: andrej@1852: imported_js = set() laurent@371: imported_js.update(set(t.imported_js)) laurent@371: imported_modules_str = "" laurent@371: for module in t.imported_modules: laurent@371: if module not in self.library_modules: laurent@371: self.library_modules.append(module) andrej@1782: # imported_js.update(set(t.imported_js)) andrej@1782: # imported_modules_str += self._translate( laurent@371: # module, False, debug=debug, imported_js=imported_js) laurent@371: laurent@371: return imported_modules_str + module_str laurent@371: laurent@371: def translate(self, module_name, is_app=True, debug=False, andrej@1852: library_modules=None): laurent@371: app_code = cStringIO.StringIO() laurent@371: lib_code = cStringIO.StringIO() laurent@371: imported_js = set() laurent@371: self.library_modules = [] laurent@371: self.overrides = {} andrej@1852: if library_modules is not None: andrej@1852: for library in library_modules: andrej@1852: if library.endswith(".js"): andrej@1852: imported_js.add(library) andrej@1852: continue andrej@1852: self.library_modules.append(library) andrej@1852: if self.verbose: andrej@1852: print('Including LIB', library) andrej@1852: print('\n//\n// BEGIN LIB '+library+'\n//\n', file=lib_code) andrej@1852: print(self._translate(library, False, debug=debug, imported_js=imported_js), andrej@1852: file=lib_code) andrej@1852: andrej@1852: print("/* initialize static library */", file=lib_code) andrej@1852: print("%s%s();\n" % (UU, library), file=lib_code) andrej@1852: andrej@1852: print('\n//\n// END LIB '+library+'\n//\n', file=lib_code) laurent@371: if module_name: andrej@1826: print(self._translate(module_name, is_app, debug=debug, imported_js=imported_js), andrej@1826: file=app_code) laurent@371: for js in imported_js: andrej@1757: path = self.findFile(js) andrej@1757: if os.path.isfile(path): andrej@1757: if self.verbose: andrej@1826: print('Including JS', js) andrej@1826: print('\n//\n// BEGIN JS '+js+'\n//\n', file=lib_code) andrej@1826: print(file(path).read(), file=lib_code) andrej@1826: print('\n//\n// END JS '+js+'\n//\n', file=lib_code) andrej@1826: else: andrej@1826: print('Warning: Unable to find imported javascript:', js, file=sys.stderr) laurent@371: return lib_code.getvalue(), app_code.getvalue() laurent@371: andrej@1749: laurent@371: usage = """ laurent@371: usage: %s file_name [module_name] laurent@371: """ laurent@371: andrej@1736: laurent@371: def main(): andrej@1742: if len(sys.argv) < 2: andrej@1826: print(usage % sys.argv[0], file=sys.stderr) laurent@371: sys.exit(1) laurent@371: file_name = os.path.abspath(sys.argv[1]) laurent@371: if not os.path.isfile(file_name): andrej@1826: print("File not found %s" % file_name, file=sys.stderr) laurent@371: sys.exit(1) laurent@371: if len(sys.argv) > 2: laurent@371: module_name = sys.argv[2] laurent@371: else: laurent@371: module_name = None andrej@1826: print(translate(file_name, module_name), end="") laurent@371: andrej@1749: laurent@371: if __name__ == "__main__": laurent@371: main()