Reenabled obfuscation closure
authorLubomir Nerad <lubomir.nerad@oracle.com>
Wed, 08 May 2013 14:54:32 +0200
branchclosure
changeset 10862ac4283ee209
parent 1085 6a4ef883e233
child 1087 d868b5a67b9b
Reenabled obfuscation
rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java
rt/vm/src/main/java/org/apidesign/vm4brwsr/ClosureWrapper.java
rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java
     1.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java	Tue May 07 19:01:14 2013 +0200
     1.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java	Wed May 08 14:54:32 2013 +0200
     1.3 @@ -20,7 +20,6 @@
     1.4  import java.io.IOException;
     1.5  import java.io.InputStream;
     1.6  import java.net.URL;
     1.7 -import java.util.Enumeration;
     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 @@ -171,25 +170,21 @@
    1.12       */
    1.13      public void generate(Appendable out) throws IOException {
    1.14          Resources r = res != null ? res : new LdrRsrcs(Bck2Brwsr.class.getClassLoader());
    1.15 -//        if (level != ObfuscationLevel.NONE) {
    1.16 -//            try {
    1.17 -//                ClosureWrapper.produceTo(out, level, r, classes);
    1.18 -//                return;
    1.19 -//            } catch (IOException ex) {
    1.20 -//                throw ex;
    1.21 -//            } catch (Throwable ex) {
    1.22 -//                out.append("/* Failed to obfuscate: " + ex.getMessage()
    1.23 -//                               + " */\n");
    1.24 -//            }
    1.25 -//        }
    1.26 +        if (level != ObfuscationLevel.NONE) {
    1.27 +            try {
    1.28 +                ClosureWrapper.produceTo(out, level, r, classes, extension);
    1.29 +                return;
    1.30 +            } catch (IOException ex) {
    1.31 +                throw ex;
    1.32 +            } catch (Throwable ex) {
    1.33 +                out.append("/* Failed to obfuscate: " + ex.getMessage()
    1.34 +                               + " */\n");
    1.35 +            }
    1.36 +        }
    1.37  
    1.38 -        if (extension) {
    1.39 -            VM.compileExtension(r, out, classes);
    1.40 -        } else {
    1.41 -            VM.compileStandalone(r, out, classes);
    1.42 -        }
    1.43 +        VM.compile(out, r, classes, extension);
    1.44      }
    1.45 -    
    1.46 +
    1.47      /** Provider of resources (classes and other files). The 
    1.48       * {@link #generate(java.lang.Appendable, org.apidesign.vm4brwsr.Bck2Brwsr.Resources, java.lang.String[]) 
    1.49       * generator method} will call back here for all classes needed during
     2.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/ClosureWrapper.java	Tue May 07 19:01:14 2013 +0200
     2.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ClosureWrapper.java	Wed May 08 14:54:32 2013 +0200
     2.3 @@ -37,19 +37,23 @@
     2.4  
     2.5      private final Bck2Brwsr.Resources res;
     2.6      private final StringArray classes;
     2.7 +    private final boolean extension;
     2.8  
     2.9      private String compiledCode;
    2.10      private String externsCode;
    2.11  
    2.12      private ClosureWrapper(Appendable out, 
    2.13                             String compilationLevel,
    2.14 -                           Bck2Brwsr.Resources res, StringArray classes) {
    2.15 +                           Bck2Brwsr.Resources res,
    2.16 +                           StringArray classes,
    2.17 +                           boolean extension) {
    2.18          super(
    2.19              generateArguments(compilationLevel),
    2.20              new PrintStream(new APS(out)), System.err
    2.21          );
    2.22          this.res = res;
    2.23          this.classes = classes;
    2.24 +        this.extension = extension;
    2.25      }
    2.26  
    2.27      @Override
    2.28 @@ -92,7 +96,7 @@
    2.29          if (compiledCode == null) {
    2.30              StringBuilder sb = new StringBuilder();
    2.31              try {
    2.32 -                VM.compileStandalone(res, sb, classes);
    2.33 +                VM.compile(sb, res, classes, extension);
    2.34                  compiledCode = sb.toString();
    2.35              } catch (IOException ex) {
    2.36                  compiledCode = ex.getMessage();
    2.37 @@ -134,8 +138,17 @@
    2.38          return finalArgs;
    2.39      }
    2.40  
    2.41 -    static int produceTo(Appendable w, ObfuscationLevel obfuscationLevel, Bck2Brwsr.Resources resources, StringArray arr) throws IOException {
    2.42 -        ClosureWrapper cw = create(w, obfuscationLevel, resources, arr);
    2.43 +    static int produceTo(Appendable output,
    2.44 +                         ObfuscationLevel obfuscationLevel,
    2.45 +                         Bck2Brwsr.Resources resources,
    2.46 +                         StringArray arr,
    2.47 +                         boolean extension) throws IOException {
    2.48 +        final ClosureWrapper cw =
    2.49 +                new ClosureWrapper(output,
    2.50 +                                   (obfuscationLevel == ObfuscationLevel.FULL)
    2.51 +                                           ? "ADVANCED_OPTIMIZATIONS"
    2.52 +                                           : "SIMPLE_OPTIMIZATIONS",
    2.53 +                                   resources, arr, extension);
    2.54          try {
    2.55              return cw.doRun();
    2.56          } catch (FlagUsageException ex) {
    2.57 @@ -143,24 +156,6 @@
    2.58          }
    2.59      }
    2.60  
    2.61 -    private static ClosureWrapper create(Appendable w,
    2.62 -                                         ObfuscationLevel obfuscationLevel,
    2.63 -                                         Bck2Brwsr.Resources resources,
    2.64 -                                         StringArray arr) {
    2.65 -        switch (obfuscationLevel) {
    2.66 -            case MINIMAL:
    2.67 -                return new ClosureWrapper(w, "SIMPLE_OPTIMIZATIONS",
    2.68 -                                          resources, arr);
    2.69 -
    2.70 -            case FULL:
    2.71 -                return new ClosureWrapper(w, "ADVANCED_OPTIMIZATIONS",
    2.72 -                                          resources, arr);
    2.73 -            default:
    2.74 -                throw new IllegalArgumentException(
    2.75 -                        "Unsupported level: " + obfuscationLevel);
    2.76 -        }
    2.77 -    }
    2.78 -
    2.79      private static final String[] FIXED_EXTERNS = {
    2.80          "bck2brwsr",
    2.81          "$class",
     3.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java	Tue May 07 19:01:14 2013 +0200
     3.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java	Wed May 08 14:54:32 2013 +0200
     3.3 @@ -57,13 +57,8 @@
     3.4          return false;
     3.5      }
     3.6      
     3.7 -    static void compileStandalone(Bck2Brwsr.Resources l, Appendable out, StringArray names) throws IOException {
     3.8 -        VM vm = new Standalone(out, l);
     3.9 -        vm.doCompile(names);
    3.10 -    }
    3.11 -
    3.12 -    static void compileExtension(Bck2Brwsr.Resources l, Appendable out, StringArray names) throws IOException {
    3.13 -        VM vm = new Extension(out, l);
    3.14 +    static void compile(Appendable out, Bck2Brwsr.Resources l, StringArray names, boolean extension) throws IOException {
    3.15 +        VM vm = extension ? new Extension(out, l) : new Standalone(out, l);
    3.16          vm.doCompile(names);
    3.17      }
    3.18