From: <fwi...@us...> - 2009-09-08 19:18:38
|
Revision: 6765 http://jython.svn.sourceforge.net/jython/?rev=6765&view=rev Author: fwierzbicki Date: 2009-09-08 19:18:26 +0000 (Tue, 08 Sep 2009) Log Message: ----------- Cleanup, add guard and custome call site for experimentation. 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-09-08 18:21:38 UTC (rev 6764) +++ branches/indy/src/org/python/compiler/IndyTest.java 2009-09-08 19:18:26 UTC (rev 6765) @@ -2,6 +2,7 @@ import java.dyn.CallSite; import java.dyn.InvokeDynamic; +import java.dyn.JavaMethodHandle; import java.dyn.Linkage; import java.dyn.MethodType; import java.dyn.MethodHandle; @@ -18,9 +19,19 @@ import org.python.core.ThreadState; public class IndyTest { + private static MethodType oneArgCall = MethodType.make( + PyObject.class, //return type + ThreadState.class, // state + PyObject.class, // arg1 + PyObject.class, // globals + PyObject[].class, // defaults + PyObject.class // closure + ); public static void run() throws Throwable { - PyObject result = InvokeDynamic.<PyObject>foo(new PyInteger(4), Py.None, + ThreadState ts = Py.getThreadState(); + PyCode func_code = ((PyFunction)ts.frame.getname("foo")).func_code; + PyObject result = InvokeDynamic.<PyObject>foo(func_code, ts, new PyInteger(4), Py.None, Py.EmptyObjects, new PyTuple()); System.out.println("result = " + result); } @@ -29,22 +40,47 @@ private static CallSite linkDynamic(Class<?> caller, String name, MethodType type) { 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, ts); call = MethodHandles.convertArguments(call, type); site.setTarget(call); return site; } + + private static class Guard extends JavaMethodHandle { + MethodHandle target; + CSite site; + + Guard(CSite site, MethodHandle target) { + super(INVOKE); + this.target = target; + this.site = site; + } + + PyObject invoke(ThreadState t, PyObject arg1, PyObject globals, PyObject[] defaults, PyObject closure) throws Throwable { + return target.<PyObject>invoke(arg1, globals, defaults, closure); + } + + private static final MethodHandle INVOKE = + MethodHandles.lookup().findVirtual(Guard.class, "invoke", oneArgCall); + + public String toString() { + return "Guard:" + target; + } + } + + // Use a local call site subclass. (These are optional but fun.) + private static class CSite extends CallSite { + public CSite(Class caller, String name, MethodType type) { + super(caller, name, type); + System.out.println("[link] new call site: "+this); + setTarget(new Guard(this, /*FIXME*/null)); + } + + // this is just for the noise value: + public void setTarget(MethodHandle t) { + System.out.println("[link] set target to "+t); + super.setTarget(t); + } + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |