# HG changeset patch # User Lubomir Nerad # Date 1368017672 -7200 # Node ID 2ac4283ee20931e825f25b7458b641743d308070 # Parent 6a4ef883e23333f32bb9789511151f14269b7bf7 Reenabled obfuscation diff -r 6a4ef883e233 -r 2ac4283ee209 rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java Tue May 07 19:01:14 2013 +0200 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java Wed May 08 14:54:32 2013 +0200 @@ -20,7 +20,6 @@ import java.io.IOException; import java.io.InputStream; import java.net.URL; -import java.util.Enumeration; /** Build your own virtual machine! Use methods in this class to generate * a skeleton JVM in JavaScript that contains pre-compiled classes of your @@ -171,25 +170,21 @@ */ public void generate(Appendable out) throws IOException { Resources r = res != null ? res : new LdrRsrcs(Bck2Brwsr.class.getClassLoader()); -// if (level != ObfuscationLevel.NONE) { -// try { -// ClosureWrapper.produceTo(out, level, r, classes); -// return; -// } catch (IOException ex) { -// throw ex; -// } catch (Throwable ex) { -// out.append("/* Failed to obfuscate: " + ex.getMessage() -// + " */\n"); -// } -// } + if (level != ObfuscationLevel.NONE) { + try { + ClosureWrapper.produceTo(out, level, r, classes, extension); + return; + } catch (IOException ex) { + throw ex; + } catch (Throwable ex) { + out.append("/* Failed to obfuscate: " + ex.getMessage() + + " */\n"); + } + } - if (extension) { - VM.compileExtension(r, out, classes); - } else { - VM.compileStandalone(r, out, classes); - } + VM.compile(out, r, classes, extension); } - + /** Provider of resources (classes and other files). The * {@link #generate(java.lang.Appendable, org.apidesign.vm4brwsr.Bck2Brwsr.Resources, java.lang.String[]) * generator method} will call back here for all classes needed during diff -r 6a4ef883e233 -r 2ac4283ee209 rt/vm/src/main/java/org/apidesign/vm4brwsr/ClosureWrapper.java --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/ClosureWrapper.java Tue May 07 19:01:14 2013 +0200 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ClosureWrapper.java Wed May 08 14:54:32 2013 +0200 @@ -37,19 +37,23 @@ private final Bck2Brwsr.Resources res; private final StringArray classes; + private final boolean extension; private String compiledCode; private String externsCode; private ClosureWrapper(Appendable out, String compilationLevel, - Bck2Brwsr.Resources res, StringArray classes) { + Bck2Brwsr.Resources res, + StringArray classes, + boolean extension) { super( generateArguments(compilationLevel), new PrintStream(new APS(out)), System.err ); this.res = res; this.classes = classes; + this.extension = extension; } @Override @@ -92,7 +96,7 @@ if (compiledCode == null) { StringBuilder sb = new StringBuilder(); try { - VM.compileStandalone(res, sb, classes); + VM.compile(sb, res, classes, extension); compiledCode = sb.toString(); } catch (IOException ex) { compiledCode = ex.getMessage(); @@ -134,8 +138,17 @@ return finalArgs; } - static int produceTo(Appendable w, ObfuscationLevel obfuscationLevel, Bck2Brwsr.Resources resources, StringArray arr) throws IOException { - ClosureWrapper cw = create(w, obfuscationLevel, resources, arr); + static int produceTo(Appendable output, + ObfuscationLevel obfuscationLevel, + Bck2Brwsr.Resources resources, + StringArray arr, + boolean extension) throws IOException { + final ClosureWrapper cw = + new ClosureWrapper(output, + (obfuscationLevel == ObfuscationLevel.FULL) + ? "ADVANCED_OPTIMIZATIONS" + : "SIMPLE_OPTIMIZATIONS", + resources, arr, extension); try { return cw.doRun(); } catch (FlagUsageException ex) { @@ -143,24 +156,6 @@ } } - private static ClosureWrapper create(Appendable w, - ObfuscationLevel obfuscationLevel, - Bck2Brwsr.Resources resources, - StringArray arr) { - switch (obfuscationLevel) { - case MINIMAL: - return new ClosureWrapper(w, "SIMPLE_OPTIMIZATIONS", - resources, arr); - - case FULL: - return new ClosureWrapper(w, "ADVANCED_OPTIMIZATIONS", - resources, arr); - default: - throw new IllegalArgumentException( - "Unsupported level: " + obfuscationLevel); - } - } - private static final String[] FIXED_EXTERNS = { "bck2brwsr", "$class", diff -r 6a4ef883e233 -r 2ac4283ee209 rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java Tue May 07 19:01:14 2013 +0200 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java Wed May 08 14:54:32 2013 +0200 @@ -57,13 +57,8 @@ return false; } - static void compileStandalone(Bck2Brwsr.Resources l, Appendable out, StringArray names) throws IOException { - VM vm = new Standalone(out, l); - vm.doCompile(names); - } - - static void compileExtension(Bck2Brwsr.Resources l, Appendable out, StringArray names) throws IOException { - VM vm = new Extension(out, l); + static void compile(Appendable out, Bck2Brwsr.Resources l, StringArray names, boolean extension) throws IOException { + VM vm = extension ? new Extension(out, l) : new Standalone(out, l); vm.doCompile(names); }