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 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 dumpConstants(self): self.dumpCodes() stmts = [] decls = [] for type, label, value in self.constants: decls.append(jast.Declare("private static " + type, label)) stmts.append(jast.Set(label, value)) setconstants = jast.Method("initConstants", "private static", ["void"], jast.Block(stmts)) decls.append(setconstants) return decls
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 addClassDictInit(self): self.interfaces.append(org.python.core.ClassDictInit) namelist = jast.InvokeStatic( "Py", "java2py", [jast.StringArray(self.supermethods.keys())]) code = jast.Invoke(jast.Identifier("dict"), "__setitem__", [jast.StringConstant("__supernames__"), namelist]) code = jast.Block([code]) self.statements.append( jast.Method("classDictInit", "static public", ["void", ("PyObject", "dict")], code))
def dumpInitModule(self): meths = [] dict = jast.Identifier("dict") sargs = [ jast.StringConstant("__name__"), jast.New("PyString", [jast.StringConstant(self.name)]) ] rargs = [ jast.Invoke(jast.New("_PyInner", []), "getMain", []), dict, dict ] code = jast.Block([ jast.Invoke(dict, "__setitem__", sargs), jast.InvokeStatic("Py", "runCode", rargs) ]) meths.append( jast.Method("moduleDictInit", "public static", ["void", ("PyObject", "dict")], code)) return meths
def dumpMain(self): meths = [] if self.javamain: code = [] newargs = jast.Identifier("newargs") code.append( jast.Declare("String[]", newargs, jast.NewArray("String", ["args.length+1"]))) code.append( jast.Set(jast.Identifier("newargs[0]"), jast.StringConstant(self.name))) args = [ jast.Identifier('args'), jast.IntegerConstant(0), jast.Identifier('newargs'), jast.IntegerConstant(1), jast.Identifier('args.length') ] code.append( jast.InvokeStatic("java.lang.System", "arraycopy", args)) args = [ jast.GetStaticAttribute( self.getclassname(self.name + '.' + self.pyinner.name), "class"), jast.Identifier('newargs'), self.getPackages(qual=1), self.getMainProperties(qual=1), self.getFrozen(), jast.StringArray(self.modules.keys()) ] code.append([jast.InvokeStatic("Py", "runMain", args)]) maincode = jast.Block(code) meths.append( jast.Method("main", "public static", ["void", ("String[]", "args")], maincode, ["java.lang.Exception"])) return meths
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)