# HG changeset patch # User Jaroslav Tulach # Date 1400778404 -7200 # Node ID bf08bd96d4082be80f2b6831ed905d66851fa5a3 # Parent d4ee65642d8d88cd399b91613deefdefa2394af8 Don't include @JavaScriptResource resources in generated JavaScript diff -r d4ee65642d8d -r bf08bd96d408 rt/vm/src/main/java/org/apidesign/vm4brwsr/StringArray.java --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/StringArray.java Thu May 22 15:29:40 2014 +0200 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/StringArray.java Thu May 22 19:06:44 2014 +0200 @@ -103,6 +103,12 @@ } arr = tmp; } + void remove(String item) { + int f = indexOf(item); + if (f != -1) { + delete(f); + } + } int indexOf(String ic) { if (arr != null) for (int i = 0; i < arr.length; i++) { diff -r d4ee65642d8d -r bf08bd96d408 rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java Thu May 22 15:29:40 2014 +0200 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java Thu May 22 19:06:44 2014 +0200 @@ -34,13 +34,18 @@ private final Bck2Brwsr.Resources resources; private final ExportedSymbols exportedSymbols; private final StringArray invokerMethods; + private final StringArray asBinary; - private VM(Appendable out, Bck2Brwsr.Resources resources, StringArray explicitlyExported) { + private VM( + Appendable out, Bck2Brwsr.Resources resources, + StringArray explicitlyExported, StringArray asBinary + ) { super(out); this.resources = resources; this.classDataCache = new ClassDataCache(resources); this.exportedSymbols = new ExportedSymbols(resources, explicitlyExported); this.invokerMethods = new StringArray(); + this.asBinary = asBinary; } static { @@ -70,17 +75,23 @@ VM vm; if (config.isExtension()) { fixedNames.add(VM.class.getName().replace('.', '/')); - vm = new Extension(out, config.getResources(), both, config.exported()); + vm = new Extension(out, + config.getResources(), both, config.exported(), + config.allResources() + ); } else { if (config.includeVM()) { fixedNames.add(VM.class.getName().replace('.', '/')); } - vm = new Standalone(out, config.getResources(), config.exported()); + vm = new Standalone(out, + config.getResources(), config.exported(), + config.allResources() + ); } - vm.doCompile(fixedNames.addAndNew(both), config.allResources()); + vm.doCompile(fixedNames.addAndNew(both)); } - private void doCompile(StringArray names, StringArray asBinary) throws IOException { + private void doCompile(StringArray names) throws IOException { generatePrologue(); append( "\n var invoker = {};"); @@ -229,6 +240,7 @@ throw new IOException("Can't find " + resource); } readResource(emul, this); + asBinary.remove(resource); } scripts = new StringArray(); @@ -441,8 +453,11 @@ } private static final class Standalone extends VM { - private Standalone(Appendable out, Bck2Brwsr.Resources resources, StringArray explicitlyExported) { - super(out, resources, explicitlyExported); + private Standalone(Appendable out, + Bck2Brwsr.Resources resources, + StringArray explicitlyExported, StringArray asBinary + ) { + super(out, resources, explicitlyExported, asBinary); } @Override @@ -578,8 +593,10 @@ private final StringArray extensionClasses; private Extension(Appendable out, Bck2Brwsr.Resources resources, - String[] extClassesArray, StringArray explicitlyExported) { - super(out, resources, explicitlyExported); + String[] extClassesArray, StringArray explicitlyExported, + StringArray asBinary + ) { + super(out, resources, explicitlyExported, asBinary); this.extensionClasses = StringArray.asList(extClassesArray); } diff -r d4ee65642d8d -r bf08bd96d408 rt/vm/src/test/java/org/apidesign/vm4brwsr/Resources.java --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/Resources.java Thu May 22 15:29:40 2014 +0200 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/Resources.java Thu May 22 19:06:44 2014 +0200 @@ -21,12 +21,27 @@ import java.io.InputStream; import java.net.URL; import java.util.Enumeration; +import net.java.html.js.JavaScriptBody; +import net.java.html.js.JavaScriptResource; /** * * @author Jaroslav Tulach */ +@JavaScriptResource("obj.js") public class Resources { + @JavaScriptBody(args = {}, body = "return obj;") + static Object retObj() { + return null; + } + + public static boolean isObj() { + return retObj() != null; + } + public static boolean isResource() { + return Resources.class.getResource("obj.js") != null; + } + public static String loadKO() throws IOException { InputStream is = Resources.class.getResourceAsStream("ko.js"); return readIS(is, false); diff -r d4ee65642d8d -r bf08bd96d408 rt/vm/src/test/java/org/apidesign/vm4brwsr/ResourcesWithExtensionsTest.java --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/ResourcesWithExtensionsTest.java Thu May 22 15:29:40 2014 +0200 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/ResourcesWithExtensionsTest.java Thu May 22 19:06:44 2014 +0200 @@ -45,6 +45,17 @@ exp ); } + + @Test public void objJSResourceIsNotFound() throws Exception { + assertExec("Objects from @JavaScriptResource resources are available", + Resources.class, "isObj__Z", 1.0 + ); + } + @Test public void objJSIsFound() throws Exception { + assertExec("The resources used as @JavaScriptResource aren't available", + Resources.class, "isResource__Z", 0.0 + ); + } private static TestVM code; diff -r d4ee65642d8d -r bf08bd96d408 rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java Thu May 22 15:29:40 2014 +0200 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java Thu May 22 19:06:44 2014 +0200 @@ -161,6 +161,7 @@ } }). addRootClasses(name). + addResources("org/apidesign/vm4brwsr/obj.js"). obfuscation(ObfuscationLevel.FULL). library(true); if (resourceName != null) { diff -r d4ee65642d8d -r bf08bd96d408 rt/vm/src/test/resources/org/apidesign/vm4brwsr/obj.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/vm/src/test/resources/org/apidesign/vm4brwsr/obj.js Thu May 22 19:06:44 2014 +0200 @@ -0,0 +1,20 @@ +/* + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +var obj = {}; + +