# HG changeset patch # User Jaroslav Tulach # Date 1371490584 -7200 # Node ID b703d9d71f252beffedf3ed36a4452ba3f0958f6 # Parent 80affbdece2890aaf33e0125a27e82a925cbcb61 Initial attempt to pass this into non-static methods diff -r 80affbdece28 -r b703d9d71f25 launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/Fn.java --- a/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/Fn.java Mon Jun 17 18:27:15 2013 +0200 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/Fn.java Mon Jun 17 19:36:24 2013 +0200 @@ -27,6 +27,6 @@ return cl.defineFn(code, names); } - public abstract Object invoke(Object... args) throws Exception; + public abstract Object invoke(Object thiz, Object... args) throws Exception; } diff -r 80affbdece28 -r b703d9d71f25 launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/JVMBridge.java --- a/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/JVMBridge.java Mon Jun 17 18:27:15 2013 +0200 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/JVMBridge.java Mon Jun 17 19:36:24 2013 +0200 @@ -125,7 +125,7 @@ } @Override - public Object invoke(Object... args) throws Exception { + public Object invoke(Object thiz, Object... args) throws Exception { try { return fn.call("fn", args); // NOI18N } catch (Error t) { diff -r 80affbdece28 -r b703d9d71f25 launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/JsClassLoader.java --- a/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/JsClassLoader.java Mon Jun 17 18:27:15 2013 +0200 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/JsClassLoader.java Mon Jun 17 19:36:24 2013 +0200 @@ -132,7 +132,7 @@ @Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { - return new FindInMethod(name, desc, + return new FindInMethod(access, name, desc, super.visitMethod(access & (~Opcodes.ACC_NATIVE), name, desc, signature, exceptions) ); } @@ -140,12 +140,14 @@ private final class FindInMethod extends MethodVisitor { private final String name; private final String desc; + private final int access; private List args; private String body; private boolean bodyGenerated; - public FindInMethod(String name, String desc, MethodVisitor mv) { + public FindInMethod(int access, String name, String desc, MethodVisitor mv) { super(Opcodes.ASM4, mv); + this.access = access; this.name = name; this.desc = desc; } @@ -207,6 +209,16 @@ // end of Fn init super.visitLabel(ifNotNull); + + final int offset; + if ((access & Opcodes.ACC_STATIC) == 0) { + offset = 1; + super.visitIntInsn(Opcodes.ALOAD, 0); + } else { + offset = 0; + super.visitInsn(Opcodes.ACONST_NULL); + } + super.visitIntInsn(Opcodes.SIPUSH, args.size()); super.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object"); @@ -228,7 +240,7 @@ } FindInMethod.super.visitInsn(Opcodes.DUP); FindInMethod.super.visitIntInsn(Opcodes.SIPUSH, index); - FindInMethod.super.visitVarInsn(t.getOpcode(Opcodes.ILOAD), index); + FindInMethod.super.visitVarInsn(t.getOpcode(Opcodes.ILOAD), index + offset); String factory; switch (descriptor) { case 'I': factory = "java/lang/Integer"; break; @@ -256,7 +268,7 @@ } FindInMethod.super.visitInsn(Opcodes.DUP); FindInMethod.super.visitIntInsn(Opcodes.SIPUSH, index); - FindInMethod.super.visitVarInsn(Opcodes.ALOAD, index); + FindInMethod.super.visitVarInsn(Opcodes.ALOAD, index + offset); FindInMethod.super.visitInsn(Opcodes.AASTORE); index++; } @@ -274,7 +286,7 @@ sr.accept(sv); super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, - "org/apidesign/bck2brwsr/launcher/fximpl/Fn", "invoke", "([Ljava/lang/Object;)Ljava/lang/Object;" + "org/apidesign/bck2brwsr/launcher/fximpl/Fn", "invoke", "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;" ); switch (sv.returnType.getSort()) { case Type.VOID: diff -r 80affbdece28 -r b703d9d71f25 launcher/fx/src/test/java/org/apidesign/bck2brwsr/launcher/fximpl/JsClassLoaderTest.java --- a/launcher/fx/src/test/java/org/apidesign/bck2brwsr/launcher/fximpl/JsClassLoaderTest.java Mon Jun 17 18:27:15 2013 +0200 +++ b/launcher/fx/src/test/java/org/apidesign/bck2brwsr/launcher/fximpl/JsClassLoaderTest.java Mon Jun 17 19:36:24 2013 +0200 @@ -76,7 +76,7 @@ final Object val = eng.eval(sb.toString()); return new Fn() { @Override - public Object invoke(Object... args) throws Exception { + public Object invoke(Object thiz, Object... args) throws Exception { Invocable inv = (Invocable)eng; return inv.invokeMethod(val, "fn", args); } @@ -123,4 +123,34 @@ throw ex.getTargetException(); } } + + @Test public void instanceMethod() throws Throwable { + Method plus = methodClass.getMethod("plusInst", int.class); + Object inst = methodClass.newInstance(); + try { + assertEquals(plus.invoke(inst, 10), 10); + } catch (InvocationTargetException ex) { + throw ex.getTargetException(); + } + } + + @Test public void staticThis() throws Throwable { + Method st = methodClass.getMethod("staticThis"); + try { + assertNull(st.invoke(null)); + } catch (InvocationTargetException ex) { + throw ex.getTargetException(); + } + } + + @Test public void getThis() throws Throwable { + Object th = methodClass.newInstance(); + Method st = methodClass.getMethod("getThis"); + try { + assertEquals(st.invoke(th), th); + } catch (InvocationTargetException ex) { + throw ex.getTargetException(); + } + } + } \ No newline at end of file diff -r 80affbdece28 -r b703d9d71f25 launcher/fx/src/test/java/org/apidesign/bck2brwsr/launcher/fximpl/JsMethods.java --- a/launcher/fx/src/test/java/org/apidesign/bck2brwsr/launcher/fximpl/JsMethods.java Mon Jun 17 18:27:15 2013 +0200 +++ b/launcher/fx/src/test/java/org/apidesign/bck2brwsr/launcher/fximpl/JsMethods.java Mon Jun 17 19:36:24 2013 +0200 @@ -34,4 +34,12 @@ @JavaScriptBody(args = {"x"}, body = "return x;") public static native int plus(int x); + + @JavaScriptBody(args = {}, body = "return this;") + public static native Object staticThis(); + + @JavaScriptBody(args = {}, body = "return this;") + public native Object getThis(); + @JavaScriptBody(args = {"x"}, body = "return x;") + public native int plusInst(int x); }