Initial attempt to pass this into non-static methods classloader
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 17 Jun 2013 19:36:24 +0200
branchclassloader
changeset 1181b703d9d71f25
parent 1180 80affbdece28
child 1182 743f2fe4f0bc
Initial attempt to pass this into non-static methods
launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/Fn.java
launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/JVMBridge.java
launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/JsClassLoader.java
launcher/fx/src/test/java/org/apidesign/bck2brwsr/launcher/fximpl/JsClassLoaderTest.java
launcher/fx/src/test/java/org/apidesign/bck2brwsr/launcher/fximpl/JsMethods.java
     1.1 --- a/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/Fn.java	Mon Jun 17 18:27:15 2013 +0200
     1.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/Fn.java	Mon Jun 17 19:36:24 2013 +0200
     1.3 @@ -27,6 +27,6 @@
     1.4          return cl.defineFn(code, names);
     1.5      }
     1.6  
     1.7 -    public abstract Object invoke(Object... args) throws Exception;
     1.8 +    public abstract Object invoke(Object thiz, Object... args) throws Exception;
     1.9      
    1.10  }
     2.1 --- a/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/JVMBridge.java	Mon Jun 17 18:27:15 2013 +0200
     2.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/JVMBridge.java	Mon Jun 17 19:36:24 2013 +0200
     2.3 @@ -125,7 +125,7 @@
     2.4          }
     2.5          
     2.6          @Override
     2.7 -        public Object invoke(Object... args) throws Exception {
     2.8 +        public Object invoke(Object thiz, Object... args) throws Exception {
     2.9              try {
    2.10                  return fn.call("fn", args); // NOI18N
    2.11              } catch (Error t) {
     3.1 --- a/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/JsClassLoader.java	Mon Jun 17 18:27:15 2013 +0200
     3.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/JsClassLoader.java	Mon Jun 17 19:36:24 2013 +0200
     3.3 @@ -132,7 +132,7 @@
     3.4  
     3.5          @Override
     3.6          public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
     3.7 -            return new FindInMethod(name, desc,
     3.8 +            return new FindInMethod(access, name, desc,
     3.9                  super.visitMethod(access & (~Opcodes.ACC_NATIVE), name, desc, signature, exceptions)
    3.10              );
    3.11          }
    3.12 @@ -140,12 +140,14 @@
    3.13          private final class FindInMethod extends MethodVisitor {
    3.14              private final String name;
    3.15              private final String desc;
    3.16 +            private final int access;
    3.17              private List<String> args;
    3.18              private String body;
    3.19              private boolean bodyGenerated;
    3.20              
    3.21 -            public FindInMethod(String name, String desc, MethodVisitor mv) {
    3.22 +            public FindInMethod(int access, String name, String desc, MethodVisitor mv) {
    3.23                  super(Opcodes.ASM4, mv);
    3.24 +                this.access = access;
    3.25                  this.name = name;
    3.26                  this.desc = desc;
    3.27              }
    3.28 @@ -207,6 +209,16 @@
    3.29                  // end of Fn init
    3.30                  
    3.31                  super.visitLabel(ifNotNull);
    3.32 +                
    3.33 +                final int offset;
    3.34 +                if ((access & Opcodes.ACC_STATIC) == 0) {
    3.35 +                    offset = 1;
    3.36 +                    super.visitIntInsn(Opcodes.ALOAD, 0);
    3.37 +                } else {
    3.38 +                    offset = 0;
    3.39 +                    super.visitInsn(Opcodes.ACONST_NULL);
    3.40 +                }
    3.41 +                
    3.42                  super.visitIntInsn(Opcodes.SIPUSH, args.size());
    3.43                  super.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
    3.44                  
    3.45 @@ -228,7 +240,7 @@
    3.46                          }
    3.47                          FindInMethod.super.visitInsn(Opcodes.DUP);
    3.48                          FindInMethod.super.visitIntInsn(Opcodes.SIPUSH, index);
    3.49 -                        FindInMethod.super.visitVarInsn(t.getOpcode(Opcodes.ILOAD), index);
    3.50 +                        FindInMethod.super.visitVarInsn(t.getOpcode(Opcodes.ILOAD), index + offset);
    3.51                          String factory;
    3.52                          switch (descriptor) {
    3.53                          case 'I': factory = "java/lang/Integer"; break;
    3.54 @@ -256,7 +268,7 @@
    3.55                          }
    3.56                          FindInMethod.super.visitInsn(Opcodes.DUP);
    3.57                          FindInMethod.super.visitIntInsn(Opcodes.SIPUSH, index);
    3.58 -                        FindInMethod.super.visitVarInsn(Opcodes.ALOAD, index);
    3.59 +                        FindInMethod.super.visitVarInsn(Opcodes.ALOAD, index + offset);
    3.60                          FindInMethod.super.visitInsn(Opcodes.AASTORE);
    3.61                          index++;
    3.62                      }
    3.63 @@ -274,7 +286,7 @@
    3.64                  sr.accept(sv);
    3.65                  
    3.66                  super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, 
    3.67 -                    "org/apidesign/bck2brwsr/launcher/fximpl/Fn", "invoke", "([Ljava/lang/Object;)Ljava/lang/Object;"
    3.68 +                    "org/apidesign/bck2brwsr/launcher/fximpl/Fn", "invoke", "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;"
    3.69                  );
    3.70                  switch (sv.returnType.getSort()) {
    3.71                  case Type.VOID: 
     4.1 --- a/launcher/fx/src/test/java/org/apidesign/bck2brwsr/launcher/fximpl/JsClassLoaderTest.java	Mon Jun 17 18:27:15 2013 +0200
     4.2 +++ b/launcher/fx/src/test/java/org/apidesign/bck2brwsr/launcher/fximpl/JsClassLoaderTest.java	Mon Jun 17 19:36:24 2013 +0200
     4.3 @@ -76,7 +76,7 @@
     4.4                      final Object val = eng.eval(sb.toString());
     4.5                      return new Fn() {
     4.6                          @Override
     4.7 -                        public Object invoke(Object... args) throws Exception {
     4.8 +                        public Object invoke(Object thiz, Object... args) throws Exception {
     4.9                              Invocable inv = (Invocable)eng;
    4.10                              return inv.invokeMethod(val, "fn", args);
    4.11                          }
    4.12 @@ -123,4 +123,34 @@
    4.13              throw ex.getTargetException();
    4.14          }
    4.15      }
    4.16 +    
    4.17 +    @Test public void instanceMethod() throws Throwable {
    4.18 +        Method plus = methodClass.getMethod("plusInst", int.class);
    4.19 +        Object inst = methodClass.newInstance();
    4.20 +        try {
    4.21 +            assertEquals(plus.invoke(inst, 10), 10);
    4.22 +        } catch (InvocationTargetException ex) {
    4.23 +            throw ex.getTargetException();
    4.24 +        }
    4.25 +    }
    4.26 +    
    4.27 +    @Test public void staticThis() throws Throwable {
    4.28 +        Method st = methodClass.getMethod("staticThis");
    4.29 +        try {
    4.30 +            assertNull(st.invoke(null));
    4.31 +        } catch (InvocationTargetException ex) {
    4.32 +            throw ex.getTargetException();
    4.33 +        }
    4.34 +    }
    4.35 +
    4.36 +    @Test public void getThis() throws Throwable {
    4.37 +        Object th = methodClass.newInstance();
    4.38 +        Method st = methodClass.getMethod("getThis");
    4.39 +        try {
    4.40 +            assertEquals(st.invoke(th), th);
    4.41 +        } catch (InvocationTargetException ex) {
    4.42 +            throw ex.getTargetException();
    4.43 +        }
    4.44 +    }
    4.45 +    
    4.46  }
    4.47 \ No newline at end of file
     5.1 --- a/launcher/fx/src/test/java/org/apidesign/bck2brwsr/launcher/fximpl/JsMethods.java	Mon Jun 17 18:27:15 2013 +0200
     5.2 +++ b/launcher/fx/src/test/java/org/apidesign/bck2brwsr/launcher/fximpl/JsMethods.java	Mon Jun 17 19:36:24 2013 +0200
     5.3 @@ -34,4 +34,12 @@
     5.4      
     5.5      @JavaScriptBody(args = {"x"}, body = "return x;")
     5.6      public static native int plus(int x);
     5.7 +    
     5.8 +    @JavaScriptBody(args = {}, body = "return this;")
     5.9 +    public static native Object staticThis();
    5.10 +    
    5.11 +    @JavaScriptBody(args = {}, body = "return this;")
    5.12 +    public native Object getThis();
    5.13 +    @JavaScriptBody(args = {"x"}, body = "return x;")
    5.14 +    public native int plusInst(int x);
    5.15  }