Different meaning of root vs. added classes. Ability to explicitly enumerate classes that should be exported and available with fully qualified name.
1.1 --- a/launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/CompileCP.java Sat Apr 26 21:16:06 2014 +0200
1.2 +++ b/launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/CompileCP.java Sat Apr 26 21:30:06 2014 +0200
1.3 @@ -51,8 +51,8 @@
1.4 }
1.5
1.6 Bck2Brwsr.newCompiler()
1.7 - .addRootClasses(classes.toArray(new String[0]))
1.8 - .extension(true)
1.9 + .addClasses(classes.toArray(new String[0]))
1.10 + .library(true)
1.11 .resources(new JarRes())
1.12 .generate(w);
1.13 w.flush();
1.14 @@ -78,7 +78,7 @@
1.15 try {
1.16 Bck2Brwsr.newCompiler()
1.17 .addRootClasses(classes.toArray(new String[0]))
1.18 - .extension(true)
1.19 + .library(true)
1.20 .resources(new EmulationResources())
1.21 .generate(w);
1.22 w.flush();
2.1 --- a/rt/emul/brwsrtest/src/test/java/org/apidesign/bck2brwsr/brwsrtest/BooleanTest.java Sat Apr 26 21:16:06 2014 +0200
2.2 +++ b/rt/emul/brwsrtest/src/test/java/org/apidesign/bck2brwsr/brwsrtest/BooleanTest.java Sat Apr 26 21:30:06 2014 +0200
2.3 @@ -26,7 +26,6 @@
2.4 *
2.5 * @author Jaroslav Tulach <jtulach@netbeans.org>
2.6 */
2.7 -@org.apidesign.bck2brwsr.core.Exported
2.8 public class BooleanTest {
2.9 @JavaScriptBody(args = { "tr" }, body = "return tr ? true : false;")
2.10 private static native Object trueFalse(boolean tr);
3.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java Sat Apr 26 21:16:06 2014 +0200
3.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java Sat Apr 26 21:30:06 2014 +0200
3.3 @@ -20,6 +20,7 @@
3.4 import java.io.IOException;
3.5 import java.io.InputStream;
3.6 import java.net.URL;
3.7 +import org.apidesign.bck2brwsr.core.Exported;
3.8
3.9 /** Build your own virtual machine! Use methods in this class to generate
3.10 * a skeleton JVM in JavaScript that contains pre-compiled classes of your
3.11 @@ -54,12 +55,14 @@
3.12 */
3.13 public final class Bck2Brwsr {
3.14 private final ObfuscationLevel level;
3.15 + private final StringArray rootcls;
3.16 private final StringArray classes;
3.17 private final Resources res;
3.18 private final boolean extension;
3.19
3.20 - private Bck2Brwsr(ObfuscationLevel level, StringArray classes, Resources resources, boolean extension) {
3.21 + private Bck2Brwsr(ObfuscationLevel level, StringArray rootcls, StringArray classes, Resources resources, boolean extension) {
3.22 this.level = level;
3.23 + this.rootcls = rootcls;
3.24 this.classes = classes;
3.25 this.res = resources;
3.26 this.extension = extension;
3.27 @@ -99,26 +102,51 @@
3.28 * @since 0.5
3.29 */
3.30 public static Bck2Brwsr newCompiler() {
3.31 - return new Bck2Brwsr(ObfuscationLevel.NONE, new StringArray(), null, false);
3.32 + return new Bck2Brwsr(ObfuscationLevel.NONE, new StringArray(), new StringArray(), null, false);
3.33 }
3.34
3.35 - /** Creates new instance of the Bck2Brwsr compiler which inherits
3.36 - * all values from <code>this</code> instance and adds additional classes
3.37 - * to the list of those that should be compiled by the {@link #generate(java.lang.Appendable)}
3.38 - * method.
3.39 + /** Adds additional classes
3.40 + * to the list of those that should be included in the generated
3.41 + * JavaScript file.
3.42 + * These classes are guaranteed to be available in the
3.43 + * generated virtual machine code accessible using their fully
3.44 + * qualified name. This brings the same behavior as if the
3.45 + * classes were added by {@link #addClasses(java.lang.String...) } and
3.46 + * were annotated with {@link Exported} annotation.
3.47 *
3.48 * @param classes the classes to add to the compilation
3.49 - * @return new instance of the compiler
3.50 + * @return new instance of the Bck2Brwsr compiler which inherits
3.51 + * all values from <code>this</code>
3.52 */
3.53 public Bck2Brwsr addRootClasses(String... classes) {
3.54 if (classes.length == 0) {
3.55 return this;
3.56 } else {
3.57 - return new Bck2Brwsr(level, this.classes.addAndNew(classes), res,
3.58 + return new Bck2Brwsr(level, rootcls.addAndNew(classes), this.classes, res,
3.59 extension);
3.60 }
3.61 }
3.62
3.63 + /** Adds additional classes
3.64 + * to the list of those that should be included in the generated
3.65 + * JavaScript file. These classes are guaranteed to be present,
3.66 + * but they may not be accessible through their fully qualified
3.67 + * name.
3.68 + *
3.69 + * @param classes the classes to add to the compilation
3.70 + * @return new instance of the Bck2Brwsr compiler which inherits
3.71 + * all values from <code>this</code>
3.72 + * @since 0.9
3.73 + */
3.74 + public Bck2Brwsr addClasses(String... classes) {
3.75 + if (classes.length == 0) {
3.76 + return this;
3.77 + } else {
3.78 + return new Bck2Brwsr(level, rootcls, this.classes.addAndNew(classes), res,
3.79 + extension);
3.80 + }
3.81 + }
3.82 +
3.83 /** Changes the obfuscation level for the compiler by creating new instance
3.84 * which inherits all values from <code>this</code> and adjust the level
3.85 * of obfuscation.
3.86 @@ -128,7 +156,7 @@
3.87 * @since 0.5
3.88 */
3.89 public Bck2Brwsr obfuscation(ObfuscationLevel level) {
3.90 - return new Bck2Brwsr(level, classes, res, extension);
3.91 + return new Bck2Brwsr(level, rootcls, classes, res, extension);
3.92 }
3.93
3.94 /** A way to change the provider of additional resources (classes) for the
3.95 @@ -140,11 +168,22 @@
3.96 * @since 0.5
3.97 */
3.98 public Bck2Brwsr resources(Resources res) {
3.99 - return new Bck2Brwsr(level, classes, res, extension);
3.100 + return new Bck2Brwsr(level, rootcls, classes, res, extension);
3.101 }
3.102
3.103 - public Bck2Brwsr extension(boolean extension) {
3.104 - return new Bck2Brwsr(level, classes, res, extension);
3.105 + /** Should one generate a library? By default the system generates
3.106 + * all transitive classes needed by the the transitive closure of
3.107 + * {@link #addRootClasses(java.lang.String...)} and {@link #addClasses(java.lang.String...)}.
3.108 + * By turning on the library mode, only classes explicitly listed
3.109 + * will be included in the archive. The others will be referenced
3.110 + * as external ones.
3.111 + *
3.112 + * @param library turn on the library mode?
3.113 + * @return new instance of the compiler with library flag changed
3.114 + * @since 0.9
3.115 + */
3.116 + public Bck2Brwsr library(boolean library) {
3.117 + return new Bck2Brwsr(level, rootcls, classes, res, library);
3.118 }
3.119
3.120 /** A way to change the provider of additional resources (classes) for the
3.121 @@ -169,7 +208,7 @@
3.122 Resources r = res != null ? res : new LdrRsrcs(Bck2Brwsr.class.getClassLoader());
3.123 if (level != ObfuscationLevel.NONE) {
3.124 try {
3.125 - ClosureWrapper.produceTo(out, level, r, classes, extension);
3.126 + ClosureWrapper.produceTo(out, level, r, rootcls, classes, extension);
3.127 return;
3.128 } catch (IOException ex) {
3.129 throw ex;
3.130 @@ -179,7 +218,7 @@
3.131 }
3.132 }
3.133
3.134 - VM.compile(out, r, classes, extension);
3.135 + VM.compile(out, r, rootcls, classes, extension);
3.136 }
3.137
3.138 /** Provider of resources (classes and other files). The
4.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/ClosureWrapper.java Sat Apr 26 21:16:06 2014 +0200
4.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ClosureWrapper.java Sat Apr 26 21:30:06 2014 +0200
4.3 @@ -38,6 +38,7 @@
4.4 private final Bck2Brwsr.Resources res;
4.5 private final StringArray classes;
4.6 private final boolean extension;
4.7 + private final StringArray rootClasses;
4.8
4.9 private String compiledCode;
4.10 private String externsCode;
4.11 @@ -45,6 +46,7 @@
4.12 private ClosureWrapper(Appendable out,
4.13 String compilationLevel,
4.14 Bck2Brwsr.Resources res,
4.15 + StringArray rootClasses,
4.16 StringArray classes,
4.17 boolean extension) {
4.18 super(
4.19 @@ -52,6 +54,7 @@
4.20 new PrintStream(new APS(out)), System.err
4.21 );
4.22 this.res = res;
4.23 + this.rootClasses = rootClasses;
4.24 this.classes = classes;
4.25 this.extension = extension;
4.26 }
4.27 @@ -96,7 +99,7 @@
4.28 if (compiledCode == null) {
4.29 StringBuilder sb = new StringBuilder();
4.30 try {
4.31 - VM.compile(sb, res, classes, extension);
4.32 + VM.compile(sb, res, rootClasses, classes, extension);
4.33 compiledCode = sb.toString();
4.34 } catch (IOException ex) {
4.35 compiledCode = ex.getMessage();
4.36 @@ -141,6 +144,7 @@
4.37 static int produceTo(Appendable output,
4.38 ObfuscationLevel obfuscationLevel,
4.39 Bck2Brwsr.Resources resources,
4.40 + StringArray rootArr,
4.41 StringArray arr,
4.42 boolean extension) throws IOException {
4.43 final ClosureWrapper cw =
4.44 @@ -148,7 +152,7 @@
4.45 (obfuscationLevel == ObfuscationLevel.FULL)
4.46 ? "ADVANCED_OPTIMIZATIONS"
4.47 : "SIMPLE_OPTIMIZATIONS",
4.48 - resources, arr, extension);
4.49 + resources, rootArr, arr, extension);
4.50 try {
4.51 return cw.doRun();
4.52 } catch (FlagUsageException ex) {
5.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/ExportedSymbols.java Sat Apr 26 21:16:06 2014 +0200
5.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ExportedSymbols.java Sat Apr 26 21:30:06 2014 +0200
5.3 @@ -30,15 +30,20 @@
5.4 @ExtraJavaScript(processByteCode = false, resource="")
5.5 final class ExportedSymbols {
5.6 private final Bck2Brwsr.Resources resources;
5.7 + private final StringArray exported;
5.8 private final Map<Object, Boolean> isMarkedAsExportedCache;
5.9
5.10 - ExportedSymbols(final Bck2Brwsr.Resources resources) {
5.11 + ExportedSymbols(final Bck2Brwsr.Resources resources, StringArray explicitlyExported) {
5.12 this.resources = resources;
5.13 + this.exported = explicitlyExported;
5.14
5.15 isMarkedAsExportedCache = new HashMap<Object, Boolean>();
5.16 }
5.17
5.18 boolean isExported(ClassData classData) throws IOException {
5.19 + if (exported.contains(classData.getClassName())) {
5.20 + return true;
5.21 + }
5.22 return classData.isPublic() && isMarkedAsExportedPackage(
5.23 classData.getPkgName())
5.24 || isMarkedAsExported(classData);
6.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/Main.java Sat Apr 26 21:16:06 2014 +0200
6.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/Main.java Sat Apr 26 21:30:06 2014 +0200
6.3 @@ -105,8 +105,7 @@
6.4 }
6.5
6.6 try (Writer w = new BufferedWriter(new FileWriter(gt))) {
6.7 - Bck2Brwsr.newCompiler().
6.8 - extension(createExtension).
6.9 + Bck2Brwsr.newCompiler().library(createExtension).
6.10 obfuscation(obfLevel).
6.11 addRootClasses(classes.toArray()).
6.12 resources(new LdrRsrcs(mainClassLoader)).
7.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java Sat Apr 26 21:16:06 2014 +0200
7.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java Sat Apr 26 21:30:06 2014 +0200
7.3 @@ -40,11 +40,11 @@
7.4 VM.class
7.5 };
7.6
7.7 - private VM(Appendable out, Bck2Brwsr.Resources resources) {
7.8 + private VM(Appendable out, Bck2Brwsr.Resources resources, StringArray explicitlyExported) {
7.9 super(out);
7.10 this.resources = resources;
7.11 this.classDataCache = new ClassDataCache(resources);
7.12 - this.exportedSymbols = new ExportedSymbols(resources);
7.13 + this.exportedSymbols = new ExportedSymbols(resources, explicitlyExported);
7.14 this.invokerMethods = new StringArray();
7.15 }
7.16
7.17 @@ -63,9 +63,16 @@
7.18 return false;
7.19 }
7.20
7.21 - static void compile(Appendable out, Bck2Brwsr.Resources l, StringArray names, boolean extension) throws IOException {
7.22 - VM vm = extension ? new Extension(out, l, names.toArray())
7.23 - : new Standalone(out, l);
7.24 + static void compile(Appendable out,
7.25 + Bck2Brwsr.Resources l,
7.26 + StringArray rootNames,
7.27 + StringArray names,
7.28 + boolean extension
7.29 + ) throws IOException {
7.30 + StringArray both = names.addAndNew(rootNames.toArray());
7.31 +
7.32 + VM vm = extension ? new Extension(out, l, both.toArray(), rootNames)
7.33 + : new Standalone(out, l, rootNames);
7.34
7.35 final StringArray fixedNames = new StringArray();
7.36
7.37 @@ -73,7 +80,7 @@
7.38 fixedNames.add(fixedClass.getName().replace('.', '/'));
7.39 }
7.40
7.41 - vm.doCompile(fixedNames.addAndNew(names.toArray()));
7.42 + vm.doCompile(fixedNames.addAndNew(both.toArray()));
7.43 }
7.44
7.45 private void doCompile(StringArray names) throws IOException {
7.46 @@ -412,8 +419,8 @@
7.47 }
7.48
7.49 private static final class Standalone extends VM {
7.50 - private Standalone(Appendable out, Bck2Brwsr.Resources resources) {
7.51 - super(out, resources);
7.52 + private Standalone(Appendable out, Bck2Brwsr.Resources resources, StringArray explicitlyExported) {
7.53 + super(out, resources, explicitlyExported);
7.54 }
7.55
7.56 @Override
7.57 @@ -488,8 +495,8 @@
7.58 private final StringArray extensionClasses;
7.59
7.60 private Extension(Appendable out, Bck2Brwsr.Resources resources,
7.61 - String[] extClassesArray) {
7.62 - super(out, resources);
7.63 + String[] extClassesArray, StringArray explicitlyExported) {
7.64 + super(out, resources, explicitlyExported);
7.65 this.extensionClasses = StringArray.asList(extClassesArray);
7.66 }
7.67
8.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java Sat Apr 26 21:16:06 2014 +0200
8.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java Sat Apr 26 21:30:06 2014 +0200
8.3 @@ -26,7 +26,6 @@
8.4 *
8.5 * @author Jaroslav Tulach <jtulach@netbeans.org>
8.6 */
8.7 -@org.apidesign.bck2brwsr.core.Exported
8.8 public class Numbers {
8.9 private static Double autoboxDbl() {
8.10 return 3.3;
9.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java Sat Apr 26 21:16:06 2014 +0200
9.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java Sat Apr 26 21:30:06 2014 +0200
9.3 @@ -133,8 +133,7 @@
9.4 Bck2Brwsr.generate(sb, new EmulationResources());
9.5 Bck2Brwsr b2b = Bck2Brwsr.newCompiler().
9.6 resources(new EmulationResources()).
9.7 - addRootClasses(names).
9.8 - extension(true);
9.9 + addRootClasses(names).library(true);
9.10 b2b.generate(sb);
9.11 ScriptEngineManager sem = new ScriptEngineManager();
9.12 ScriptEngine js = sem.getEngineByExtension("js");