# HG changeset patch # User Jaroslav Tulach # Date 1361003180 -3600 # Node ID 6ac37d80ecb76ac01eff15bd4b254227e3afd608 # Parent 3d1585c82d674964b877cdb5bdc8d0f3b55170d9 Initial attempt to use Google Closure Compiler to compress the size of the Bck2Brwsr VM diff -r 3d1585c82d67 -r 6ac37d80ecb7 vm/pom.xml --- a/vm/pom.xml Sat Feb 16 08:24:45 2013 +0100 +++ b/vm/pom.xml Sat Feb 16 09:26:20 2013 +0100 @@ -76,18 +76,18 @@ generate-js process-classes + + org.apidesign.vm4brwsr.Main + + ${project.build.directory}/bck2brwsr.js + org/apidesign/vm4brwsr/Bck2Brwsr + + java - - org.apidesign.vm4brwsr.Main - - ${project.build.directory}/bck2brwsr.js - org/apidesign/vm4brwsr/Bck2Brwsr - - maven-assembly-plugin @@ -139,5 +139,11 @@ ${project.version} compile + + com.google.javascript + closure-compiler + r2388 + compile + diff -r 3d1585c82d67 -r 6ac37d80ecb7 vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java --- a/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java Sat Feb 16 08:24:45 2013 +0100 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java Sat Feb 16 09:26:20 2013 +0100 @@ -68,7 +68,13 @@ public static void generate(Appendable out, Resources resources, String... classes) throws IOException { StringArray arr = StringArray.asList(classes); arr.add(VM.class.getName().replace('.', '/')); - VM.compile(resources, out, arr); + try { + ClosureWrapper.produceTo(out, resources, arr); + } catch (IOException ex) { + throw ex; + } catch (Throwable ex) { + VM.compile(resources, out, arr); + } } /** Generates virtual machine from bytes served by a class loader. diff -r 3d1585c82d67 -r 6ac37d80ecb7 vm/src/main/java/org/apidesign/vm4brwsr/ClosureWrapper.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ClosureWrapper.java Sat Feb 16 09:26:20 2013 +0100 @@ -0,0 +1,90 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.vm4brwsr; + +import com.google.javascript.jscomp.CommandLineRunner; +import com.google.javascript.jscomp.SourceFile; +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintStream; +import java.util.Collections; +import java.util.List; +import org.apidesign.bck2brwsr.core.ExtraJavaScript; + +/** + * + * @author Jaroslav Tulach + */ +@ExtraJavaScript(processByteCode = false, resource="") +final class ClosureWrapper extends CommandLineRunner implements SourceFile.Generator { + private static final String[] ARGS = { "--compilation_level", "SIMPLE_OPTIMIZATIONS", "--js", "bck2brwsr-raw.js" }; + + private String code; + private final Bck2Brwsr.Resources res; + private final StringArray classes; + private ClosureWrapper(Appendable out, Bck2Brwsr.Resources res, StringArray classes) { + super( + ARGS, + new PrintStream(new APS(out)), System.err + ); + this.res = res; + this.classes = classes; + } + + @Override + protected List createInputs(List files, boolean allowStdIn) throws FlagUsageException, IOException { + if (files.size() != 1 || !"bck2brwsr-raw.js".equals(files.get(0))) { + throw new IOException("Unexpected files: " + files); + } + return Collections.nCopies(1, SourceFile.fromGenerator("bck2brwsr-raw.js", this)); + } + + @Override + public String getCode() { + if (code == null) { + StringBuilder sb = new StringBuilder(); + try { + VM.compile(res, sb, classes); + } catch (IOException ex) { + code = ex.getMessage(); + } + code = sb.toString(); + } + return code; + } + + private static final class APS extends OutputStream { + private final Appendable out; + + public APS(Appendable out) { + this.out = out; + } + @Override + public void write(int b) throws IOException { + out.append((char)b); + } + } + static int produceTo(Appendable w, Bck2Brwsr.Resources resources, StringArray arr) throws IOException { + ClosureWrapper cw = new ClosureWrapper(w, resources, arr); + try { + return cw.doRun(); + } catch (FlagUsageException ex) { + throw new IOException(ex); + } + } +}