rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java
branchclosure
changeset 849 d95117153304
parent 841 81cea57bf4e9
child 860 35507d1a5069
     1.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java	Tue Mar 12 13:54:26 2013 +0100
     1.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java	Wed Mar 13 16:17:47 2013 +0100
     1.3 @@ -56,35 +56,69 @@
     1.4  public final class Bck2Brwsr {
     1.5      private Bck2Brwsr() {
     1.6      }
     1.7 -    
     1.8 +
     1.9      /** Generates virtual machine from bytes served by a <code>resources</code>
    1.10       * provider.
    1.11 -     * 
    1.12 +     *
    1.13       * @param out the output to write the generated JavaScript to
    1.14       * @param resources provider of class files to use
    1.15       * @param classes additional classes to include in the generated script
    1.16       * @throws IOException I/O exception can be thrown when something goes wrong
    1.17       */
    1.18      public static void generate(Appendable out, Resources resources, String... classes) throws IOException {
    1.19 +        generate(out, ObfuscationLevel.NONE, resources, classes);
    1.20 +    }
    1.21 +
    1.22 +    /** Generates virtual machine from bytes served by a class loader.
    1.23 +     *
    1.24 +     * @param out the output to write the generated JavaScript to
    1.25 +     * @param loader class loader to load needed classes from
    1.26 +     * @param classes additional classes to include in the generated script
    1.27 +     * @throws IOException I/O exception can be thrown when something goes wrong
    1.28 +     */
    1.29 +    public static void generate(Appendable out, ClassLoader loader, String... classes) throws IOException {
    1.30 +        generate(out, ObfuscationLevel.NONE, loader, classes);
    1.31 +    }
    1.32 +
    1.33 +    /** Generates virtual machine from bytes served by a <code>resources</code>
    1.34 +     * provider.
    1.35 +     * 
    1.36 +     * @param out the output to write the generated JavaScript to
    1.37 +     * @param obfuscationLevel the obfuscation level for the generated
    1.38 +     *                         JavaScript
    1.39 +     * @param resources provider of class files to use
    1.40 +     * @param classes additional classes to include in the generated script
    1.41 +     * @throws IOException I/O exception can be thrown when something goes wrong
    1.42 +     */
    1.43 +    public static void generate(Appendable out, ObfuscationLevel obfuscationLevel, Resources resources, String... classes) throws IOException {
    1.44          StringArray arr = StringArray.asList(classes);
    1.45          arr.add(VM.class.getName().replace('.', '/'));
    1.46 -        try {
    1.47 -            ClosureWrapper.produceTo(out, resources, arr);
    1.48 -        } catch (IOException ex) {
    1.49 -            throw ex;
    1.50 -        } catch (Throwable ex) {
    1.51 -            VM.compile(resources, out, arr);
    1.52 +
    1.53 +        if (obfuscationLevel != ObfuscationLevel.NONE) {
    1.54 +            try {
    1.55 +                ClosureWrapper.produceTo(out, obfuscationLevel, resources, arr);
    1.56 +                return;
    1.57 +            } catch (IOException ex) {
    1.58 +                throw ex;
    1.59 +            } catch (Throwable ex) {
    1.60 +                out.append("/* Failed to obfuscate: " + ex.getMessage()
    1.61 +                               + " */\n");
    1.62 +            }
    1.63          }
    1.64 +
    1.65 +        VM.compile(resources, out, arr);
    1.66      }
    1.67      
    1.68      /** Generates virtual machine from bytes served by a class loader.
    1.69       * 
    1.70       * @param out the output to write the generated JavaScript to
    1.71 +     * @param obfuscationLevel the obfuscation level for the generated
    1.72 +     *                         JavaScript
    1.73       * @param loader class loader to load needed classes from
    1.74       * @param classes additional classes to include in the generated script
    1.75       * @throws IOException I/O exception can be thrown when something goes wrong
    1.76       */
    1.77 -    public static void generate(Appendable out, final ClassLoader loader, String... classes) throws IOException {
    1.78 +    public static void generate(Appendable out, ObfuscationLevel obfuscationLevel, final ClassLoader loader, String... classes) throws IOException {
    1.79          class R implements Resources {
    1.80              @Override
    1.81              public InputStream get(String name) throws IOException {
    1.82 @@ -99,7 +133,7 @@
    1.83                  return u.openStream();
    1.84              }
    1.85          }
    1.86 -        generate(out, new R(), classes);
    1.87 +        generate(out, obfuscationLevel, new R(), classes);
    1.88      }
    1.89      
    1.90      /** Provider of resources (classes and other files). The