Don't include @JavaScriptResource resources in generated JavaScript closure
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 22 May 2014 19:06:44 +0200
branchclosure
changeset 1587bf08bd96d408
parent 1586 d4ee65642d8d
child 1588 c1b6db1bdd87
Don't include @JavaScriptResource resources in generated JavaScript
rt/vm/src/main/java/org/apidesign/vm4brwsr/StringArray.java
rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.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/resources/org/apidesign/vm4brwsr/obj.js
     1.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/StringArray.java	Thu May 22 15:29:40 2014 +0200
     1.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/StringArray.java	Thu May 22 19:06:44 2014 +0200
     1.3 @@ -103,6 +103,12 @@
     1.4          }
     1.5          arr = tmp;
     1.6      }
     1.7 +    void remove(String item) {
     1.8 +        int f = indexOf(item);
     1.9 +        if (f != -1) {
    1.10 +            delete(f);
    1.11 +        }
    1.12 +    }
    1.13  
    1.14      int indexOf(String ic) {
    1.15          if (arr != null) for (int i = 0; i < arr.length; i++) {
     2.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java	Thu May 22 15:29:40 2014 +0200
     2.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java	Thu May 22 19:06:44 2014 +0200
     2.3 @@ -34,13 +34,18 @@
     2.4      private final Bck2Brwsr.Resources resources;
     2.5      private final ExportedSymbols exportedSymbols;
     2.6      private final StringArray invokerMethods;
     2.7 +    private final StringArray asBinary;
     2.8  
     2.9 -    private VM(Appendable out, Bck2Brwsr.Resources resources, StringArray explicitlyExported) {
    2.10 +    private VM(
    2.11 +        Appendable out, Bck2Brwsr.Resources resources, 
    2.12 +        StringArray explicitlyExported, StringArray asBinary
    2.13 +    ) {
    2.14          super(out);
    2.15          this.resources = resources;
    2.16          this.classDataCache = new ClassDataCache(resources);
    2.17          this.exportedSymbols = new ExportedSymbols(resources, explicitlyExported);
    2.18          this.invokerMethods = new StringArray();
    2.19 +        this.asBinary = asBinary;
    2.20      }
    2.21  
    2.22      static {
    2.23 @@ -70,17 +75,23 @@
    2.24          VM vm;
    2.25          if (config.isExtension()) {
    2.26              fixedNames.add(VM.class.getName().replace('.', '/'));
    2.27 -            vm = new Extension(out, config.getResources(), both, config.exported());
    2.28 +            vm = new Extension(out, 
    2.29 +                config.getResources(), both, config.exported(),
    2.30 +                config.allResources()
    2.31 +            );
    2.32          } else {
    2.33              if (config.includeVM()) {
    2.34                  fixedNames.add(VM.class.getName().replace('.', '/'));
    2.35              }
    2.36 -            vm = new Standalone(out, config.getResources(), config.exported());
    2.37 +            vm = new Standalone(out, 
    2.38 +                config.getResources(), config.exported(),
    2.39 +                config.allResources()
    2.40 +            );
    2.41          }            
    2.42 -        vm.doCompile(fixedNames.addAndNew(both), config.allResources());
    2.43 +        vm.doCompile(fixedNames.addAndNew(both));
    2.44      }
    2.45  
    2.46 -    private void doCompile(StringArray names, StringArray asBinary) throws IOException {
    2.47 +    private void doCompile(StringArray names) throws IOException {
    2.48          generatePrologue();
    2.49          append(
    2.50                  "\n  var invoker = {};");
    2.51 @@ -229,6 +240,7 @@
    2.52                      throw new IOException("Can't find " + resource);
    2.53                  }
    2.54                  readResource(emul, this);
    2.55 +                asBinary.remove(resource);
    2.56              }
    2.57              scripts = new StringArray();
    2.58  
    2.59 @@ -441,8 +453,11 @@
    2.60      }
    2.61  
    2.62      private static final class Standalone extends VM {
    2.63 -        private Standalone(Appendable out, Bck2Brwsr.Resources resources, StringArray explicitlyExported) {
    2.64 -            super(out, resources, explicitlyExported);
    2.65 +        private Standalone(Appendable out,
    2.66 +            Bck2Brwsr.Resources resources, 
    2.67 +            StringArray explicitlyExported, StringArray asBinary
    2.68 +        ) {
    2.69 +            super(out, resources, explicitlyExported, asBinary);
    2.70          }
    2.71  
    2.72          @Override
    2.73 @@ -578,8 +593,10 @@
    2.74          private final StringArray extensionClasses;
    2.75  
    2.76          private Extension(Appendable out, Bck2Brwsr.Resources resources,
    2.77 -                          String[] extClassesArray, StringArray explicitlyExported) {
    2.78 -            super(out, resources, explicitlyExported);
    2.79 +            String[] extClassesArray, StringArray explicitlyExported,
    2.80 +            StringArray asBinary
    2.81 +        ) {
    2.82 +            super(out, resources, explicitlyExported, asBinary);
    2.83              this.extensionClasses = StringArray.asList(extClassesArray);
    2.84          }
    2.85  
     3.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/Resources.java	Thu May 22 15:29:40 2014 +0200
     3.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/Resources.java	Thu May 22 19:06:44 2014 +0200
     3.3 @@ -21,12 +21,27 @@
     3.4  import java.io.InputStream;
     3.5  import java.net.URL;
     3.6  import java.util.Enumeration;
     3.7 +import net.java.html.js.JavaScriptBody;
     3.8 +import net.java.html.js.JavaScriptResource;
     3.9  
    3.10  /**
    3.11   *
    3.12   * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.13   */
    3.14 +@JavaScriptResource("obj.js")
    3.15  public class Resources {
    3.16 +    @JavaScriptBody(args = {}, body = "return obj;")
    3.17 +    static Object retObj() {
    3.18 +        return null;
    3.19 +    }
    3.20 +    
    3.21 +    public static boolean isObj() {
    3.22 +        return retObj() != null;
    3.23 +    }
    3.24 +    public static boolean isResource() {
    3.25 +        return Resources.class.getResource("obj.js") != null;
    3.26 +    }
    3.27 +    
    3.28      public static String loadKO() throws IOException {
    3.29          InputStream is = Resources.class.getResourceAsStream("ko.js");
    3.30          return readIS(is, false);
     4.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/ResourcesWithExtensionsTest.java	Thu May 22 15:29:40 2014 +0200
     4.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/ResourcesWithExtensionsTest.java	Thu May 22 19:06:44 2014 +0200
     4.3 @@ -45,6 +45,17 @@
     4.4              exp
     4.5          );
     4.6      }
     4.7 +    
     4.8 +    @Test public void objJSResourceIsNotFound() throws Exception {
     4.9 +        assertExec("Objects from @JavaScriptResource resources are available",
    4.10 +            Resources.class, "isObj__Z", 1.0
    4.11 +        );
    4.12 +    }
    4.13 +    @Test public void objJSIsFound() throws Exception {
    4.14 +        assertExec("The resources used as @JavaScriptResource aren't available",
    4.15 +            Resources.class, "isResource__Z", 0.0
    4.16 +        );
    4.17 +    }
    4.18  
    4.19      private static TestVM code;
    4.20      
     5.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java	Thu May 22 15:29:40 2014 +0200
     5.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java	Thu May 22 19:06:44 2014 +0200
     5.3 @@ -161,6 +161,7 @@
     5.4                  }
     5.5              }).
     5.6              addRootClasses(name).
     5.7 +            addResources("org/apidesign/vm4brwsr/obj.js").
     5.8              obfuscation(ObfuscationLevel.FULL).
     5.9              library(true);
    5.10          if (resourceName != null) {
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/rt/vm/src/test/resources/org/apidesign/vm4brwsr/obj.js	Thu May 22 19:06:44 2014 +0200
     6.3 @@ -0,0 +1,20 @@
     6.4 +/*
     6.5 + * Back 2 Browser Bytecode Translator
     6.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     6.7 + *
     6.8 + * This program is free software: you can redistribute it and/or modify
     6.9 + * it under the terms of the GNU General Public License as published by
    6.10 + * the Free Software Foundation, version 2 of the License.
    6.11 + *
    6.12 + * This program is distributed in the hope that it will be useful,
    6.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    6.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    6.15 + * GNU General Public License for more details.
    6.16 + *
    6.17 + * You should have received a copy of the GNU General Public License
    6.18 + * along with this program. Look for COPYING file in the top folder.
    6.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    6.20 + */
    6.21 +var obj = {};
    6.22 +
    6.23 +