rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java
branchclosure
changeset 1491 4a1398eff4fb
parent 1094 36961c9a009f
child 1493 234fea368401
     1.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java	Mon May 13 18:54:50 2013 +0200
     1.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java	Sat Apr 26 21:30:06 2014 +0200
     1.3 @@ -20,6 +20,7 @@
     1.4  import java.io.IOException;
     1.5  import java.io.InputStream;
     1.6  import java.net.URL;
     1.7 +import org.apidesign.bck2brwsr.core.Exported;
     1.8  
     1.9  /** Build your own virtual machine! Use methods in this class to generate
    1.10   * a skeleton JVM in JavaScript that contains pre-compiled classes of your
    1.11 @@ -54,12 +55,14 @@
    1.12   */
    1.13  public final class Bck2Brwsr {
    1.14      private final ObfuscationLevel level;
    1.15 +    private final StringArray rootcls;
    1.16      private final StringArray classes;
    1.17      private final Resources res;
    1.18      private final boolean extension;
    1.19  
    1.20 -    private Bck2Brwsr(ObfuscationLevel level, StringArray classes, Resources resources, boolean extension) {
    1.21 +    private Bck2Brwsr(ObfuscationLevel level, StringArray rootcls, StringArray classes, Resources resources, boolean extension) {
    1.22          this.level = level;
    1.23 +        this.rootcls = rootcls;
    1.24          this.classes = classes;
    1.25          this.res = resources;
    1.26          this.extension = extension;
    1.27 @@ -99,26 +102,51 @@
    1.28       * @since 0.5
    1.29       */
    1.30      public static Bck2Brwsr newCompiler() {
    1.31 -        return new Bck2Brwsr(ObfuscationLevel.NONE, new StringArray(), null, false);
    1.32 +        return new Bck2Brwsr(ObfuscationLevel.NONE, new StringArray(), new StringArray(), null, false);
    1.33      }
    1.34  
    1.35 -    /** Creates new instance of the Bck2Brwsr compiler which inherits
    1.36 -     * all values from <code>this</code> instance and adds additional classes 
    1.37 -     * to the list of those that should be compiled by the {@link #generate(java.lang.Appendable)} 
    1.38 -     * method.
    1.39 +    /** Adds additional classes 
    1.40 +     * to the list of those that should be included in the generated
    1.41 +     * JavaScript file.
    1.42 +     * These classes are guaranteed to be available in the
    1.43 +     * generated virtual machine code accessible using their fully 
    1.44 +     * qualified name. This brings the same behavior as if the
    1.45 +     * classes were added by {@link #addClasses(java.lang.String...) } and
    1.46 +     * were annotated with {@link Exported} annotation.
    1.47       * 
    1.48       * @param classes the classes to add to the compilation
    1.49 -     * @return new instance of the compiler
    1.50 +     * @return new instance of the Bck2Brwsr compiler which inherits
    1.51 +     * all values from <code>this</code>
    1.52       */
    1.53      public Bck2Brwsr addRootClasses(String... classes) {
    1.54          if (classes.length == 0) {
    1.55              return this;
    1.56          } else {
    1.57 -            return new Bck2Brwsr(level, this.classes.addAndNew(classes), res,
    1.58 +            return new Bck2Brwsr(level, rootcls.addAndNew(classes), this.classes, res,
    1.59                                   extension);
    1.60          }
    1.61      }
    1.62      
    1.63 +    /** Adds additional classes 
    1.64 +     * to the list of those that should be included in the generated
    1.65 +     * JavaScript file. These classes are guaranteed to be present,
    1.66 +     * but they may not be accessible through their fully qualified
    1.67 +     * name.
    1.68 +     * 
    1.69 +     * @param classes the classes to add to the compilation
    1.70 +     * @return new instance of the Bck2Brwsr compiler which inherits
    1.71 +     * all values from <code>this</code>
    1.72 +     * @since 0.9
    1.73 +     */
    1.74 +    public Bck2Brwsr addClasses(String... classes) {
    1.75 +        if (classes.length == 0) {
    1.76 +            return this;
    1.77 +        } else {
    1.78 +            return new Bck2Brwsr(level, rootcls, this.classes.addAndNew(classes), res,
    1.79 +                extension);
    1.80 +        }
    1.81 +    }
    1.82 +    
    1.83      /** Changes the obfuscation level for the compiler by creating new instance
    1.84       * which inherits all values from <code>this</code> and adjust the level
    1.85       * of obfuscation.
    1.86 @@ -128,7 +156,7 @@
    1.87       * @since 0.5
    1.88       */
    1.89      public Bck2Brwsr obfuscation(ObfuscationLevel level) {
    1.90 -        return new Bck2Brwsr(level, classes, res, extension);
    1.91 +        return new Bck2Brwsr(level, rootcls, classes, res, extension);
    1.92      }
    1.93      
    1.94      /** A way to change the provider of additional resources (classes) for the 
    1.95 @@ -140,11 +168,22 @@
    1.96       * @since 0.5
    1.97       */
    1.98      public Bck2Brwsr resources(Resources res) {
    1.99 -        return new Bck2Brwsr(level, classes, res, extension);
   1.100 +        return new Bck2Brwsr(level, rootcls, classes, res, extension);
   1.101      }
   1.102  
   1.103 -    public Bck2Brwsr extension(boolean extension) {
   1.104 -        return new Bck2Brwsr(level, classes, res, extension);
   1.105 +    /** Should one generate a library? By default the system generates
   1.106 +     * all transitive classes needed by the the transitive closure of
   1.107 +     * {@link #addRootClasses(java.lang.String...)} and {@link #addClasses(java.lang.String...)}.
   1.108 +     * By turning on the library mode, only classes explicitly listed
   1.109 +     * will be included in the archive. The others will be referenced
   1.110 +     * as external ones.
   1.111 +     * 
   1.112 +     * @param library turn on the library mode?
   1.113 +     * @return new instance of the compiler with library flag changed
   1.114 +     * @since 0.9
   1.115 +     */
   1.116 +    public Bck2Brwsr library(boolean library) {
   1.117 +        return new Bck2Brwsr(level, rootcls, classes, res, library);
   1.118      }
   1.119  
   1.120      /** A way to change the provider of additional resources (classes) for the 
   1.121 @@ -169,7 +208,7 @@
   1.122          Resources r = res != null ? res : new LdrRsrcs(Bck2Brwsr.class.getClassLoader());
   1.123          if (level != ObfuscationLevel.NONE) {
   1.124              try {
   1.125 -                ClosureWrapper.produceTo(out, level, r, classes, extension);
   1.126 +                ClosureWrapper.produceTo(out, level, r, rootcls, classes, extension);
   1.127                  return;
   1.128              } catch (IOException ex) {
   1.129                  throw ex;
   1.130 @@ -179,7 +218,7 @@
   1.131              }
   1.132          }
   1.133  
   1.134 -        VM.compile(out, r, classes, extension);
   1.135 +        VM.compile(out, r, rootcls, classes, extension);
   1.136      }
   1.137  
   1.138      /** Provider of resources (classes and other files). The