Initial attempt to use Google Closure Compiler to compress the size of the Bck2Brwsr VM
1.1 --- a/vm/pom.xml Sat Feb 16 08:24:45 2013 +0100
1.2 +++ b/vm/pom.xml Sat Feb 16 09:26:20 2013 +0100
1.3 @@ -76,18 +76,18 @@
1.4 <execution>
1.5 <id>generate-js</id>
1.6 <phase>process-classes</phase>
1.7 + <configuration>
1.8 + <mainClass>org.apidesign.vm4brwsr.Main</mainClass>
1.9 + <arguments>
1.10 + <argument>${project.build.directory}/bck2brwsr.js</argument>
1.11 + <argument>org/apidesign/vm4brwsr/Bck2Brwsr</argument>
1.12 + </arguments>
1.13 + </configuration>
1.14 <goals>
1.15 <goal>java</goal>
1.16 </goals>
1.17 </execution>
1.18 </executions>
1.19 - <configuration>
1.20 - <mainClass>org.apidesign.vm4brwsr.Main</mainClass>
1.21 - <arguments>
1.22 - <argument>${project.build.directory}/bck2brwsr.js</argument>
1.23 - <argument>org/apidesign/vm4brwsr/Bck2Brwsr</argument>
1.24 - </arguments>
1.25 - </configuration>
1.26 </plugin>
1.27 <plugin>
1.28 <artifactId>maven-assembly-plugin</artifactId>
1.29 @@ -139,5 +139,11 @@
1.30 <version>${project.version}</version>
1.31 <scope>compile</scope>
1.32 </dependency>
1.33 + <dependency>
1.34 + <groupId>com.google.javascript</groupId>
1.35 + <artifactId>closure-compiler</artifactId>
1.36 + <version>r2388</version>
1.37 + <scope>compile</scope>
1.38 + </dependency>
1.39 </dependencies>
1.40 </project>
2.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java Sat Feb 16 08:24:45 2013 +0100
2.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java Sat Feb 16 09:26:20 2013 +0100
2.3 @@ -68,7 +68,13 @@
2.4 public static void generate(Appendable out, Resources resources, String... classes) throws IOException {
2.5 StringArray arr = StringArray.asList(classes);
2.6 arr.add(VM.class.getName().replace('.', '/'));
2.7 - VM.compile(resources, out, arr);
2.8 + try {
2.9 + ClosureWrapper.produceTo(out, resources, arr);
2.10 + } catch (IOException ex) {
2.11 + throw ex;
2.12 + } catch (Throwable ex) {
2.13 + VM.compile(resources, out, arr);
2.14 + }
2.15 }
2.16
2.17 /** Generates virtual machine from bytes served by a class loader.
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ClosureWrapper.java Sat Feb 16 09:26:20 2013 +0100
3.3 @@ -0,0 +1,90 @@
3.4 +/**
3.5 + * Back 2 Browser Bytecode Translator
3.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
3.7 + *
3.8 + * This program is free software: you can redistribute it and/or modify
3.9 + * it under the terms of the GNU General Public License as published by
3.10 + * the Free Software Foundation, version 2 of the License.
3.11 + *
3.12 + * This program is distributed in the hope that it will be useful,
3.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3.15 + * GNU General Public License for more details.
3.16 + *
3.17 + * You should have received a copy of the GNU General Public License
3.18 + * along with this program. Look for COPYING file in the top folder.
3.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
3.20 + */
3.21 +package org.apidesign.vm4brwsr;
3.22 +
3.23 +import com.google.javascript.jscomp.CommandLineRunner;
3.24 +import com.google.javascript.jscomp.SourceFile;
3.25 +import java.io.IOException;
3.26 +import java.io.OutputStream;
3.27 +import java.io.PrintStream;
3.28 +import java.util.Collections;
3.29 +import java.util.List;
3.30 +import org.apidesign.bck2brwsr.core.ExtraJavaScript;
3.31 +
3.32 +/**
3.33 + *
3.34 + * @author Jaroslav Tulach <jtulach@netbeans.org>
3.35 + */
3.36 +@ExtraJavaScript(processByteCode = false, resource="")
3.37 +final class ClosureWrapper extends CommandLineRunner implements SourceFile.Generator {
3.38 + private static final String[] ARGS = { "--compilation_level", "SIMPLE_OPTIMIZATIONS", "--js", "bck2brwsr-raw.js" };
3.39 +
3.40 + private String code;
3.41 + private final Bck2Brwsr.Resources res;
3.42 + private final StringArray classes;
3.43 + private ClosureWrapper(Appendable out, Bck2Brwsr.Resources res, StringArray classes) {
3.44 + super(
3.45 + ARGS,
3.46 + new PrintStream(new APS(out)), System.err
3.47 + );
3.48 + this.res = res;
3.49 + this.classes = classes;
3.50 + }
3.51 +
3.52 + @Override
3.53 + protected List<SourceFile> createInputs(List<String> files, boolean allowStdIn) throws FlagUsageException, IOException {
3.54 + if (files.size() != 1 || !"bck2brwsr-raw.js".equals(files.get(0))) {
3.55 + throw new IOException("Unexpected files: " + files);
3.56 + }
3.57 + return Collections.nCopies(1, SourceFile.fromGenerator("bck2brwsr-raw.js", this));
3.58 + }
3.59 +
3.60 + @Override
3.61 + public String getCode() {
3.62 + if (code == null) {
3.63 + StringBuilder sb = new StringBuilder();
3.64 + try {
3.65 + VM.compile(res, sb, classes);
3.66 + } catch (IOException ex) {
3.67 + code = ex.getMessage();
3.68 + }
3.69 + code = sb.toString();
3.70 + }
3.71 + return code;
3.72 + }
3.73 +
3.74 + private static final class APS extends OutputStream {
3.75 + private final Appendable out;
3.76 +
3.77 + public APS(Appendable out) {
3.78 + this.out = out;
3.79 + }
3.80 + @Override
3.81 + public void write(int b) throws IOException {
3.82 + out.append((char)b);
3.83 + }
3.84 + }
3.85 + static int produceTo(Appendable w, Bck2Brwsr.Resources resources, StringArray arr) throws IOException {
3.86 + ClosureWrapper cw = new ClosureWrapper(w, resources, arr);
3.87 + try {
3.88 + return cw.doRun();
3.89 + } catch (FlagUsageException ex) {
3.90 + throw new IOException(ex);
3.91 + }
3.92 + }
3.93 +}