2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.vm4brwsr;
20 import com.google.javascript.jscomp.CommandLineRunner;
21 import com.google.javascript.jscomp.SourceFile;
22 import java.io.IOException;
23 import java.io.OutputStream;
24 import java.io.PrintStream;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28 import org.apidesign.bck2brwsr.core.ExtraJavaScript;
32 * @author Jaroslav Tulach <jtulach@netbeans.org>
34 @ExtraJavaScript(processByteCode = false, resource="")
35 final class ClosureWrapper extends CommandLineRunner {
36 private static final String[] ARGS = { "--compilation_level", "SIMPLE_OPTIMIZATIONS", "--js", "bck2brwsr-raw.js" /*, "--debug", "--formatting", "PRETTY_PRINT" */ };
38 private final Bck2Brwsr.Resources res;
39 private final StringArray classes;
41 private String compiledCode;
42 private String externsCode;
44 private ClosureWrapper(Appendable out,
45 String compilationLevel,
46 Bck2Brwsr.Resources res, StringArray classes) {
48 generateArguments(compilationLevel),
49 new PrintStream(new APS(out)), System.err
52 this.classes = classes;
56 protected List<SourceFile> createInputs(List<String> files, boolean allowStdIn) throws FlagUsageException, IOException {
57 if (files.size() != 1 || !"bck2brwsr-raw.js".equals(files.get(0))) {
58 throw new IOException("Unexpected files: " + files);
60 return Collections.nCopies(
62 SourceFile.fromGenerator(
64 new SourceFile.Generator() {
66 public String getCode() {
67 return getCompiledCode();
74 protected List<SourceFile> createExterns()
75 throws FlagUsageException, IOException {
76 final List<SourceFile> externsFiles =
77 new ArrayList<SourceFile>(super.createExterns());
80 SourceFile.fromGenerator(
81 "bck2brwsr_externs.js",
82 new SourceFile.Generator() {
84 public String getCode() {
85 return getExternsCode();
91 private String getCompiledCode() {
92 if (compiledCode == null) {
93 StringBuilder sb = new StringBuilder();
95 VM.compileStandalone(res, sb, classes);
96 compiledCode = sb.toString();
97 } catch (IOException ex) {
98 compiledCode = ex.getMessage();
104 private String getExternsCode() {
105 if (externsCode == null) {
106 // need compiled code at this point
109 final StringBuilder sb = new StringBuilder("function RAW() {};\n");
110 for (final String extern: FIXED_EXTERNS) {
111 sb.append("RAW.prototype.").append(extern).append(";\n");
113 externsCode = sb.toString();
118 private static final class APS extends OutputStream {
119 private final Appendable out;
121 public APS(Appendable out) {
125 public void write(int b) throws IOException {
130 private static String[] generateArguments(String compilationLevel) {
131 String[] finalArgs = ARGS.clone();
132 finalArgs[1] = compilationLevel;
137 static int produceTo(Appendable w, ObfuscationLevel obfuscationLevel, Bck2Brwsr.Resources resources, StringArray arr) throws IOException {
138 ClosureWrapper cw = create(w, obfuscationLevel, resources, arr);
141 } catch (FlagUsageException ex) {
142 throw new IOException(ex);
146 private static ClosureWrapper create(Appendable w,
147 ObfuscationLevel obfuscationLevel,
148 Bck2Brwsr.Resources resources,
150 switch (obfuscationLevel) {
152 return new ClosureWrapper(w, "SIMPLE_OPTIMIZATIONS",
156 return new ClosureWrapper(w, "ADVANCED_OPTIMIZATIONS",
159 throw new IllegalArgumentException(
160 "Unsupported level: " + obfuscationLevel);
164 private static final String[] FIXED_EXTERNS = {
206 "getClass__Ljava_lang_Class_2",
207 "clone__Ljava_lang_Object_2"