From: <fwi...@us...> - 2009-07-17 15:47:50
|
Revision: 6542 http://jython.svn.sourceforge.net/jython/?rev=6542&view=rev Author: fwierzbicki Date: 2009-07-17 15:47:45 +0000 (Fri, 17 Jul 2009) Log Message: ----------- Really call into Jython with invokedynamic -- based on Nicholas Riley's github code InvokeDynamicModuleSample.java from git://github.com/nriley/jython.git. Modified Paths: -------------- branches/indy/src/org/python/compiler/IndyTest.java Modified: branches/indy/src/org/python/compiler/IndyTest.java =================================================================== --- branches/indy/src/org/python/compiler/IndyTest.java 2009-07-17 05:27:51 UTC (rev 6541) +++ branches/indy/src/org/python/compiler/IndyTest.java 2009-07-17 15:47:45 UTC (rev 6542) @@ -4,24 +4,49 @@ import java.dyn.InvokeDynamic; import java.dyn.Linkage; import java.dyn.MethodType; +import java.dyn.MethodHandle; import java.dyn.MethodHandles; import java.dyn.MethodType; import org.python.core.Py; +import org.python.core.PyCode; +import org.python.core.PyFunction; +import org.python.core.PyInteger; +import org.python.core.PyObject; import org.python.core.PyString; +import org.python.core.PyTuple; +import org.python.core.ThreadState; public class IndyTest { public static void run() { - PyString result = InvokeDynamic.<PyString>sayHello("Hello Indy!"); - System.out.println(result); + ThreadState ts = Py.getThreadState(); + System.out.println("foo = " + ts.frame.getname("foo")); + PyObject result = InvokeDynamic.<PyObject>foo(ts, new PyInteger(4), Py.None, + Py.EmptyObjects, new PyTuple()); + System.out.println("result = " + result); } static { Linkage.registerBootstrapMethod("linkDynamic"); } private static CallSite linkDynamic(Class<?> caller, String name, MethodType type) { - CallSite c = new CallSite(caller, name, type); - c.setTarget(MethodHandles.lookup().findStatic(Py.class, "newString", - MethodType.make(PyString.class, String.class))); - return c; + System.out.println("linkDynamic..."); + CallSite site = new CallSite(caller, name, type); + ThreadState ts = Py.getThreadState(); + PyCode func_code = ((PyFunction)ts.frame.getname(name)).func_code; + + MethodType oneArgCall = MethodType.make( + PyObject.class, //return type + ThreadState.class, // state + PyObject.class, // arg1 + PyObject.class, // globals + PyObject[].class, // defaults + PyObject.class // closure + ); + MethodHandle call = MethodHandles.lookup().findVirtual(PyCode.class, "call", oneArgCall); + call = MethodHandles.insertArguments(call, 0, func_code); + call = MethodHandles.convertArguments(call, type); + site.setTarget(call); + + return site; } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |