@JavaScriptBody methods may be native classloader
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 17 Jun 2013 11:00:51 +0200
branchclassloader
changeset 11760250b8a739de
parent 1175 15c6903c8612
child 1177 006617ca6707
@JavaScriptBody methods may be native
launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/JsClassLoader.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/JsClassLoader.java	Thu Jun 13 16:48:17 2013 +0200
     1.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/JsClassLoader.java	Mon Jun 17 11:00:51 2013 +0200
     1.3 @@ -115,7 +115,7 @@
     1.4          @Override
     1.5          public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
     1.6              return new FindInMethod(name, desc,
     1.7 -                super.visitMethod(access, name, desc, signature, exceptions)
     1.8 +                super.visitMethod(access & (~Opcodes.ACC_NATIVE), name, desc, signature, exceptions)
     1.9              );
    1.10          }
    1.11          
    1.12 @@ -124,6 +124,7 @@
    1.13              private final String desc;
    1.14              private List<String> args;
    1.15              private String body;
    1.16 +            private boolean bodyGenerated;
    1.17              
    1.18              public FindInMethod(String name, String desc, MethodVisitor mv) {
    1.19                  super(Opcodes.ASM4, mv);
    1.20 @@ -150,6 +151,14 @@
    1.21                  if (body == null) {
    1.22                      return;
    1.23                  } 
    1.24 +                generateBody();
    1.25 +            }
    1.26 +            
    1.27 +            private boolean generateBody() {
    1.28 +                if (bodyGenerated) {
    1.29 +                    return false;
    1.30 +                }
    1.31 +                bodyGenerated = true;
    1.32                  
    1.33                  super.visitFieldInsn(
    1.34                      Opcodes.GETSTATIC, FindInClass.this.name, 
    1.35 @@ -252,12 +261,12 @@
    1.36                  switch (sv.returnType.getSort()) {
    1.37                  case Type.VOID: 
    1.38                      super.visitInsn(Opcodes.RETURN);
    1.39 -                    return;
    1.40 +                    break;
    1.41                  case Type.ARRAY:
    1.42                  case Type.OBJECT:
    1.43                      super.visitTypeInsn(Opcodes.CHECKCAST, sv.returnType.getInternalName());
    1.44                      super.visitInsn(Opcodes.ARETURN);
    1.45 -                    return;
    1.46 +                    break;
    1.47                  default:
    1.48                      super.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Number");
    1.49                      super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, 
    1.50 @@ -265,12 +274,17 @@
    1.51                      );
    1.52                      super.visitInsn(sv.returnType.getOpcode(Opcodes.IRETURN));
    1.53                  }
    1.54 +                return true;
    1.55              }
    1.56  
    1.57              @Override
    1.58              public void visitEnd() {
    1.59                  super.visitEnd();
    1.60                  if (body != null) {
    1.61 +                    if (generateBody()) {
    1.62 +                        // native method
    1.63 +                        super.visitMaxs(1, 0);
    1.64 +                    }
    1.65                      FindInClass.this.visitField(
    1.66                          Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC, 
    1.67                          "$$bck2brwsr$$" + name, 
     2.1 --- a/launcher/fx/src/test/java/org/apidesign/bck2brwsr/launcher/fximpl/JsMethods.java	Thu Jun 13 16:48:17 2013 +0200
     2.2 +++ b/launcher/fx/src/test/java/org/apidesign/bck2brwsr/launcher/fximpl/JsMethods.java	Mon Jun 17 11:00:51 2013 +0200
     2.3 @@ -30,7 +30,5 @@
     2.4      }
     2.5      
     2.6      @JavaScriptBody(args = {"x", "y" }, body = "return x + y;")
     2.7 -    public static int plus(int x, int y) {
     2.8 -        return 0;
     2.9 -    }
    2.10 +    public static native int plus(int x, int y);
    2.11  }