Don't initialize @JavaScriptBody resources sooner than their methods are called.
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 09 Nov 2014 10:36:08 +0100
changeset 1722fd3a354d6e8f
parent 1721 15f7116b57b6
child 1723 3a1f262311cf
Don't initialize @JavaScriptBody resources sooner than their methods are called.
rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/HtmlAnnotationsTest.java
rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java
rt/vm/src/main/java/org/apidesign/vm4brwsr/VMLazy.java
rt/vm/src/test/java/org/apidesign/vm4brwsr/Resources.java
rt/vm/src/test/java/org/apidesign/vm4brwsr/ResourcesWithExtensionsTest.java
rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java
rt/vm/src/test/java/org/apidesign/vm4brwsr/VMinVM.java
rt/vm/src/test/resources/org/apidesign/vm4brwsr/obj.js
rt/vm/src/test/resources/org/apidesign/vm4brwsr/var.js
     1.1 --- a/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/HtmlAnnotationsTest.java	Sun Nov 09 10:35:21 2014 +0100
     1.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/HtmlAnnotationsTest.java	Sun Nov 09 10:36:08 2014 +0100
     1.3 @@ -17,6 +17,7 @@
     1.4   */
     1.5  package org.apidesign.bck2brwsr.vmtest.impl;
     1.6  
     1.7 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
     1.8  import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
     1.9  import org.apidesign.bck2brwsr.vmtest.VMTest;
    1.10  import org.testng.annotations.Factory;
    1.11 @@ -26,29 +27,44 @@
    1.12   * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.13   */
    1.14  public class HtmlAnnotationsTest {
    1.15 +    static int firstCheck;
    1.16 +    
    1.17 +    private void assertMulNotDefinedForTheFirstTime() {
    1.18 +        if (firstCheck++ == 0) {
    1.19 +            Object mul = windowMul();
    1.20 +            assert mul == null : "htmlannotations.js should not be processed before first call to HtmlAnnotations class";
    1.21 +        }
    1.22 +    }
    1.23 +    
    1.24      @BrwsrTest public void fourtyTwo() throws Exception {
    1.25 +        assertMulNotDefinedForTheFirstTime();
    1.26          assertEquals(HtmlAnnotations.fourtyTwo(), 42);
    1.27      }
    1.28      
    1.29      @BrwsrTest public void externalMul() throws Exception {
    1.30 +        assertMulNotDefinedForTheFirstTime();
    1.31          assertEquals(HtmlAnnotations.useExternalMul(7, 6), 42);
    1.32      }
    1.33  
    1.34      @BrwsrTest public void callRunnableFromJS() throws Exception {
    1.35 +        assertMulNotDefinedForTheFirstTime();
    1.36          assertEquals(HtmlAnnotations.callback(), 1);
    1.37      }
    1.38  
    1.39      @BrwsrTest public void callStaticMethodFromJS() throws Exception {
    1.40 +        assertMulNotDefinedForTheFirstTime();
    1.41          assertEquals(HtmlAnnotations.staticCallback(), 1);
    1.42      }
    1.43  
    1.44      @BrwsrTest public void callbackWithFourParamsAndReturnType() throws Exception {
    1.45 +        assertMulNotDefinedForTheFirstTime();
    1.46          Object instance = HtmlAnnotations.create();
    1.47          assertNotNull(instance, "Instance created");
    1.48          assertEquals(HtmlAnnotations.first(instance, 42, 31), 42);
    1.49      }
    1.50  
    1.51      @BrwsrTest public void callbackWithObjectParamsAndReturnType() throws Exception {
    1.52 +        assertMulNotDefinedForTheFirstTime();
    1.53          Object instance = HtmlAnnotations.create();
    1.54          assertNotNull(instance, "Instance created");
    1.55          assertEquals(HtmlAnnotations.onError(instance, 42.0), 42.0);
    1.56 @@ -65,6 +81,9 @@
    1.57          assert obj != null : msg;
    1.58      }
    1.59      
    1.60 +    @JavaScriptBody(args = {}, body = "return window.mul ? window.mul : null;")
    1.61 +    private static native Object windowMul();
    1.62 +    
    1.63      @Factory public static Object[] create() {
    1.64          return VMTest.create(HtmlAnnotationsTest.class);
    1.65      }
     2.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Sun Nov 09 10:35:21 2014 +0100
     2.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Sun Nov 09 10:36:08 2014 +0100
     2.3 @@ -70,6 +70,8 @@
     2.4       */
     2.5      protected abstract void requireScript(String resourcePath) throws IOException;
     2.6      
     2.7 +    protected abstract void requireResource(String resourcePath) throws IOException;
     2.8 +    
     2.9      /** Allows subclasses to redefine what field a function representing a
    2.10       * class gets assigned. By default it returns the suggested name followed
    2.11       * by <code>" = "</code>;
    2.12 @@ -165,6 +167,7 @@
    2.13                  }
    2.14              }
    2.15          }
    2.16 +        final String jsResource;
    2.17          {
    2.18              String[] arr = findAnnotation(arrData, jc, 
    2.19                  "net.java.html.js.JavaScriptResource", 
    2.20 @@ -172,13 +175,13 @@
    2.21              );
    2.22              if (arr != null) {
    2.23                  if (arr[0].startsWith("/")) {
    2.24 -                    requireScript(arr[0]);
    2.25 +                    jsResource = arr[0];
    2.26                  } else {
    2.27                      int last = jc.getClassName().lastIndexOf('/');
    2.28 -                    requireScript(
    2.29 -                        jc.getClassName().substring(0, last + 1).replace('.', '/') + arr[0]
    2.30 -                    );
    2.31 +                    jsResource = jc.getClassName().substring(0, last + 1).replace('.', '/') + arr[0];
    2.32                  }
    2.33 +            } else {
    2.34 +                jsResource = null;
    2.35              }
    2.36          }
    2.37          String[] proto = findAnnotation(arrData, jc, 
    2.38 @@ -303,6 +306,11 @@
    2.39          for (String init : toInitilize.toArray()) {
    2.40              append("\n    ").append(init).append("();");
    2.41          }
    2.42 +        
    2.43 +        if (jsResource != null) {
    2.44 +            requireResource(jsResource);
    2.45 +        }
    2.46 +        
    2.47          append("\n  }");
    2.48          append("\n  if (arguments.length === 0) {");
    2.49          append("\n    if (!(this instanceof CLS)) {");
     3.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java	Sun Nov 09 10:35:21 2014 +0100
     3.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java	Sun Nov 09 10:36:08 2014 +0100
     3.3 @@ -235,11 +235,7 @@
     3.4                  while (resource.startsWith("/")) {
     3.5                      resource = resource.substring(1);
     3.6                  }
     3.7 -                InputStream emul = resources.get(resource);
     3.8 -                if (emul == null) {
     3.9 -                    throw new IOException("Can't find " + resource);
    3.10 -                }
    3.11 -                readResource(emul, this);
    3.12 +                requireResourceImpl(resource);
    3.13                  asBinary.remove(resource);
    3.14              }
    3.15              scripts = new StringArray();
    3.16 @@ -260,6 +256,14 @@
    3.17          }
    3.18      }
    3.19  
    3.20 +    final void requireResourceImpl(String resource) throws IOException {
    3.21 +        InputStream emul = resources.get(resource);
    3.22 +        if (emul == null) {
    3.23 +            throw new IOException("Can't find " + resource);
    3.24 +        }
    3.25 +        readResource(emul, this);
    3.26 +    }
    3.27 +
    3.28      private static void readResource(InputStream emul, Appendable out) throws IOException {
    3.29          try {
    3.30              int state = 0;
    3.31 @@ -703,6 +707,11 @@
    3.32              out.append("\n  return vm.").append(cls).append("(instance);");
    3.33              out.append("\n}");
    3.34          }
    3.35 +
    3.36 +        @Override
    3.37 +        protected void requireResource(String resourcePath) throws IOException {
    3.38 +            requireResourceImpl(resourcePath);
    3.39 +        }
    3.40      }
    3.41  
    3.42      private static final class Extension extends VM {
    3.43 @@ -795,5 +804,10 @@
    3.44              out.append(cls).append(" = f;})(instance);");
    3.45              out.append("\n}");
    3.46          }
    3.47 +
    3.48 +        @Override
    3.49 +        protected void requireResource(String resourcePath) throws IOException {
    3.50 +            requireResourceImpl(resourcePath);
    3.51 +        }
    3.52      }
    3.53  }
     4.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/VMLazy.java	Sun Nov 09 10:35:21 2014 +0100
     4.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/VMLazy.java	Sun Nov 09 10:36:08 2014 +0100
     4.3 @@ -146,5 +146,10 @@
     4.4          String accessClass(String classOperation) {
     4.5              return "vm." + classOperation;
     4.6          }
     4.7 +
     4.8 +        @Override
     4.9 +        protected void requireResource(String resourcePath) throws IOException {
    4.10 +            requireReference(resourcePath);
    4.11 +        }
    4.12      }
    4.13  }
     5.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/Resources.java	Sun Nov 09 10:35:21 2014 +0100
     5.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/Resources.java	Sun Nov 09 10:36:08 2014 +0100
     5.3 @@ -23,11 +23,13 @@
     5.4  import java.util.Enumeration;
     5.5  import net.java.html.js.JavaScriptBody;
     5.6  import net.java.html.js.JavaScriptResource;
     5.7 +import org.apidesign.bck2brwsr.core.ExtraJavaScript;
     5.8  
     5.9  /**
    5.10   *
    5.11   * @author Jaroslav Tulach <jtulach@netbeans.org>
    5.12   */
    5.13 +@ExtraJavaScript(resource = "org/apidesign/vm4brwsr/var.js", processByteCode = true)
    5.14  @JavaScriptResource("obj.js")
    5.15  public class Resources {
    5.16      @JavaScriptBody(args = {}, body = "return obj;")
    5.17 @@ -38,8 +40,8 @@
    5.18      public static boolean isObj() {
    5.19          return retObj() != null;
    5.20      }
    5.21 -    public static boolean isResource() {
    5.22 -        return Resources.class.getResource("obj.js") != null;
    5.23 +    public static boolean isResource(String name) {
    5.24 +        return Resources.class.getResource(name) != null;
    5.25      }
    5.26      
    5.27      public static String loadKO() throws IOException {
     6.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/ResourcesWithExtensionsTest.java	Sun Nov 09 10:35:21 2014 +0100
     6.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/ResourcesWithExtensionsTest.java	Sun Nov 09 10:36:08 2014 +0100
     6.3 @@ -54,7 +54,13 @@
     6.4      }
     6.5      @Test public void objJSIsFound() throws Exception {
     6.6          assertExec("The resources used as @JavaScriptResource aren't available",
     6.7 -            Resources.class, "isResource__Z", 0.0
     6.8 +            Resources.class, "isResource__ZLjava_lang_String_2", 0.0, "obj"
     6.9 +        );
    6.10 +    }
    6.11 +
    6.12 +    @Test public void thisObjJSIsFound() throws Exception {
    6.13 +        assertExec("The resources used as @JavaScriptResource aren't available",
    6.14 +            Resources.class, "isResource__ZLjava_lang_String_2", 0.0, "thisObj"
    6.15          );
    6.16      }
    6.17  
     7.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java	Sun Nov 09 10:35:21 2014 +0100
     7.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java	Sun Nov 09 10:36:08 2014 +0100
     7.3 @@ -171,7 +171,7 @@
     7.4              ScriptEngine js = sem.getEngineByExtension("js");
     7.5              eng[0] = js;
     7.6              Bck2Brwsr.newCompiler().resources(new EmulationResources())
     7.7 -                .obfuscation(ObfuscationLevel.FULL).generate(sb);
     7.8 +                .obfuscation(ObfuscationLevel.NONE).generate(sb);
     7.9          }
    7.10          Set<String> exp = new HashSet<String>();
    7.11          for (String n : names) {
     8.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/VMinVM.java	Sun Nov 09 10:35:21 2014 +0100
     8.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/VMinVM.java	Sun Nov 09 10:36:08 2014 +0100
     8.3 @@ -43,4 +43,8 @@
     8.4      @Override
     8.5      protected void requireScript(String resourcePath) {
     8.6      }
     8.7 +
     8.8 +    @Override
     8.9 +    protected void requireResource(String resourcePath) throws IOException {
    8.10 +    }
    8.11  }
     9.1 --- a/rt/vm/src/test/resources/org/apidesign/vm4brwsr/obj.js	Sun Nov 09 10:35:21 2014 +0100
     9.2 +++ b/rt/vm/src/test/resources/org/apidesign/vm4brwsr/obj.js	Sun Nov 09 10:36:08 2014 +0100
     9.3 @@ -15,6 +15,6 @@
     9.4   * along with this program. Look for COPYING file in the top folder.
     9.5   * If not, see http://opensource.org/licenses/GPL-2.0.
     9.6   */
     9.7 -var obj = {};
     9.8 +this.thisObj = {};
     9.9  
    9.10  
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/rt/vm/src/test/resources/org/apidesign/vm4brwsr/var.js	Sun Nov 09 10:36:08 2014 +0100
    10.3 @@ -0,0 +1,20 @@
    10.4 +/*
    10.5 + * Back 2 Browser Bytecode Translator
    10.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    10.7 + *
    10.8 + * This program is free software: you can redistribute it and/or modify
    10.9 + * it under the terms of the GNU General Public License as published by
   10.10 + * the Free Software Foundation, version 2 of the License.
   10.11 + *
   10.12 + * This program is distributed in the hope that it will be useful,
   10.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
   10.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   10.15 + * GNU General Public License for more details.
   10.16 + *
   10.17 + * You should have received a copy of the GNU General Public License
   10.18 + * along with this program. Look for COPYING file in the top folder.
   10.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
   10.20 + */
   10.21 +var obj = {};
   10.22 +
   10.23 +