def dumpFuncs(self): meths = [] cases = [] args = ["PyObject", ("PyFrame", "frame")] access = "private static" callargs = [jast.Identifier("frame")] for name, funcid, code in self.funccodes: funcname = self.uniquename(name) meths.append(jast.Method(funcname, access, args, clean(code))) body = jast.Return(jast.InvokeStatic(self.name, funcname, callargs)) cases.append( [jast.IntegerConstant(funcid), jast.FreeBlock([body])]) defaultCase = jast.FreeBlock([jast.Return(jast.Null)]) switch = jast.Block( [jast.Switch(jast.Identifier('index'), cases, defaultCase)]) meths.insert( 0, jast.Method("call_function", "public", ["PyObject", ("int", "index"), ("PyFrame", "frame")], switch)) self.superclass = "PyFunctionTable" return meths
def nullReturn(ret): if ret == Void.TYPE: return jast.Return() if Class.isPrimitive(ret): if ret.__name__ == 'boolean': value = jast. False elif ret.__name__ == 'char': value = jast.CharacterConstant('x') else: value = jast.IntegerConstant(0) else: value = jast.Null return jast.Return(value)
def clean(node): if not hasattr(node, 'exits'): print node if node.exits(): return node return jast.Block( [node, jast.Return(jast.GetStaticAttribute('Py', 'None'))])
def callSuperMethod(self, name, supername, access, ret, sig, throws=[]): if self.issuperproxy: return self.supermethods[supername] = supername args = [typeName(ret)] argids = [] throws = filterThrows(throws) for c in sig: if isinstance(c, TupleType): argname = c[1] c = c[0] else: argname = "arg" + str(len(argids)) args.append((typeName(c), argname)) argid = jast.Identifier(argname) argids.append(argid) supercall = jast.Invoke(jast.Identifier("super"), name, argids) if ret != Void.TYPE: supercall = jast.Return(supercall) supermethod = jast.Method(supername, jast.Modifier.ModifierString(access), args, jast.Block([supercall]), throws) self.statements.append(supermethod) return
def addPyProxyInterface(self): self.statements.append( jast.Declare('private PyInstance', jast.Identifier('__proxy'))) code = jast.Set(jast.Identifier("__proxy"), jast.Identifier("inst")) code = jast.Block([code]) self.statements.append( jast.Method("_setPyInstance", "public", ["void", ("PyInstance", "inst")], code)) code = jast.Block([jast.Return(jast.Identifier("__proxy"))]) self.statements.append( jast.Method("_getPyInstance", "public", ["PyInstance"], code)) self.statements.append( jast.Declare('private PySystemState', jast.Identifier('__sysstate'))) code = jast.Set(jast.Identifier("__sysstate"), jast.Identifier("inst")) code = jast.Block([code]) self.statements.append( jast.Method("_setPySystemState", "public", ["void", ("PySystemState", "inst")], code)) code = jast.Block([jast.Return(jast.Identifier("__sysstate"))]) self.statements.append( jast.Method("_getPySystemState", "public", ["PySystemState"], code)) frozen = self.module.getFrozen() this = jast.Identifier("this") initargs = [ this, jast.StringConstant(self.modname), jast.StringConstant(self.name), jast.Identifier("args"), self.packages, self.properties, frozen, jast.StringArray(self.modules) ] initproxy = jast.InvokeStatic("Py", "initProxy", initargs) code = jast.Block([initproxy]) self.statements.append( jast.Method("__initProxy__", "public", ["void", ("Object[]", "args")], code)) self.interfaces.append(org.python.core.PyProxy)
def makeCode(self): comp = self.factory.getCompiler(self.def_compiler, SrcGenCompiler.ClassFrame, self.scope, self.name) code = jast.Block([comp.parse(ast.Suite(self.body)), jast.Return(jast.Invoke(comp.frame.frame, "getf_locals", []))]) self.frame = comp.frame self.pycode = self.def_compiler.top_compiler.module.getCodeConstant( self.name, code, comp.frame) return self.pycode
def dumpMain(self): if not hasattr(self, 'mainCode'): return [] meths = [] self.interfaces.append("PyRunnable") getmain = jast.Block([ jast.If(jast.Operation("==", self.mainCode, jast.Null), jast.InvokeStatic(self.name, "initConstants", [])), jast.Return(self.mainCode) ]) meths.append(jast.Method("getMain", "public", ["PyCode"], getmain)) return meths
def makeReturn(code, ret): if ret == Void.TYPE: return code if Class.isPrimitive(ret): r = jast.InvokeStatic("Py", "py2" + ret.__name__, [code]) else: typname = typeName(ret) r = jast.InvokeStatic( "Py", "tojava", [code, jast.GetStaticAttribute(typname, 'class')]) r = jast.Cast(typname, r) return jast.Return(r)
def visitListComp(self, node): # Since this code generated here is placed in its own # java method, we need a new set of temp vrbls. oldtmps = self.frame.temporaries self.frame.temporaries = {} lst = self.factory.makeList([]) lsttmp, lstcode = self.makeTemp(lst) append = self.factory.makePyObject(jast.Invoke(lsttmp.asAny(), "__getattr__", [jast.StringConstant("append")])) appendtmp, appendcode = self.makeTemp(append) self.list_comprehension_count += 1 tmp_append = "_[%d]" % self.list_comprehension_count #tmp_append = "_[1]" n = ast.Expr(ast.Call(ast.Name(tmp_append, ast.Name.Load, node), [ node.elt ], [], None, None, node), node); for lc in node.generators[::-1]: for ifs in lc.ifs[::-1]: n = ast.If(ifs, [ n ], None, ifs); n = ast.For(lc.target, lc.iter, [ n ], None, lc); #visit(new Delete(new exprType[] { new Name(tmp_append, Name.Del) })); stmts = [ lstcode ] stmts.append(self.set_name(tmp_append, append)) #stmts.append(appendcode) stmts.append(self.visit(n)) stmts.append(jast.Return(lsttmp.asAny())) decs = self.frame.getDeclarations() if len(decs) != 0: stmts.insert(0, decs) idx = self.module.addFunctionCode("__listcomprehension", jast.Block(stmts)) self.freeTemp(lsttmp) self.freeTemp(appendtmp) self.frame.temporaries = oldtmps return self.factory.makePyObject( jast.InvokeLocal("__listcomprehension$%d" % (idx+1), [jast.Identifier("frame")]))
def list_comprehension(self, node): # Since this code generated here is placed in its own # java method, we need a new set of temp vrbls. oldtmps = self.frame.temporaries self.frame.temporaries = {} expr = node.getChild(0) suite = node.getChild(1) lst = self.factory.makeList([]) lsttmp, lstcode = self.makeTemp(lst) append = self.factory.makePyObject( jast.Invoke(lsttmp.asAny(), "__getattr__", [jast.StringConstant("append")])) appendtmp, appendcode = self.makeTemp(append) self.listComprehensionStack.append((appendtmp, expr)) stmts = [lstcode, appendcode] stmts.append(self.visit(suite)) stmts.append(jast.Return(lsttmp.asAny())) decs = self.frame.getDeclarations() if len(decs) != 0: stmts.insert(0, decs) self.listComprehensionStack.pop(-1) idx = self.module.addFunctionCode("__listcomprehension", jast.Block(stmts)) self.freeTemp(lsttmp) self.freeTemp(appendtmp) self.frame.temporaries = oldtmps return self.factory.makePyObject( jast.InvokeLocal("__listcomprehension$%d" % (idx + 1), [jast.Identifier("frame")]))
def callMethod(self, name, access, ret, sig, throws=[], dosuper=1): args = [typeName(ret)] argids = [] objects = [] throws = filterThrows(throws) for c in sig: if isinstance(c, TupleType): argname = c[1] c = c[0] else: argname = "arg" + str(len(argids)) args.append((typeName(c), argname)) argid = jast.Identifier(argname) argids.append(argid) objects.append(makeObject(argid, c)) objects = jast.FilledArray("Object", objects) stmts = [] this = jast.Identifier("this") if isinstance(access, IntType) and isAbstract(access): dosuper = 0 access = access & ~ABSTRACT if not dosuper and not self.isAdapter: getattr = jast.InvokeStatic("Py", "jgetattr", [this, jast.StringConstant(name)]) else: getattr = jast.InvokeStatic("Py", "jfindattr", [this, jast.StringConstant(name)]) inst = jast.Identifier("inst") if len(throws) == 0: jcall = "_jcall" else: jcall = "_jcallexc" jcall = makeReturn(jast.Invoke(inst, jcall, [objects]), ret) jcall = wrapThrows(jcall, throws, ret) if dosuper: supercall = jast.Invoke(jast.Identifier("super"), name, argids) if ret != Void.TYPE: supercall = jast.Return(supercall) supermethod = None if not self.issuperproxy and name not in self.getCandSupermethods( incl=0): supermethod = jast.Method("super__" + name, jast.Modifier.ModifierString(access), args, jast.Block([supercall]), throws) self.supermethods["super__" + name] = "super__" + name else: if self.isAdapter: supercall = nullReturn(ret) else: supercall = None supermethod = None if not dosuper and not self.isAdapter: test = jcall else: test = jast.If(jast.Operation("!=", inst, jast.Null), jcall, supercall) code = jast.Block([jast.Declare("PyObject", inst, getattr), test]) meth = jast.Method(name, jast.Modifier.ModifierString(access), args, code, throws) if supermethod is not None: self.statements.append(supermethod) self.statements.append(meth)
def visitReturn(self, node): if node.value is None: return jast.Return(jast.GetStaticAttribute("Py", "None")) else: return jast.Return(self.visit(node.value).asAny())
def return_stmt(self, value=None): if value is None: return jast.Return(jast.GetStaticAttribute("Py", "None")) else: return jast.Return(self.visit(value).asAny())