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 = {
37 "--compilation_level",
38 "SIMPLE_OPTIMIZATIONS",
39 "--js", "bck2brwsr-raw.js"
41 //, "--formatting", "PRETTY_PRINT"
44 private final Bck2Brwsr config;
46 private String compiledCode;
47 private String externsCode;
49 private ClosureWrapper(Appendable out,
50 String compilationLevel, Bck2Brwsr config) {
52 generateArguments(compilationLevel),
53 new PrintStream(new APS(out)), System.err
59 protected List<SourceFile> createInputs(List<String> files, boolean allowStdIn) throws FlagUsageException, IOException {
60 if (files.size() != 1 || !"bck2brwsr-raw.js".equals(files.get(0))) {
61 throw new IOException("Unexpected files: " + files);
63 return Collections.nCopies(
65 SourceFile.fromGenerator(
67 new SourceFile.Generator() {
69 public String getCode() {
70 return getCompiledCode();
77 protected List<SourceFile> createExterns()
78 throws FlagUsageException, IOException {
79 final List<SourceFile> externsFiles =
80 new ArrayList<SourceFile>(super.createExterns());
83 SourceFile.fromGenerator(
84 "bck2brwsr_externs.js",
85 new SourceFile.Generator() {
87 public String getCode() {
88 return getExternsCode();
94 private String getCompiledCode() {
95 if (compiledCode == null) {
96 StringBuilder sb = new StringBuilder();
98 VM.compile(sb, config);
99 compiledCode = sb.toString();
100 } catch (IOException ex) {
101 compiledCode = ex.getMessage();
107 private String getExternsCode() {
108 if (externsCode == null) {
109 // need compiled code at this point
112 final StringBuilder sb = new StringBuilder("function RAW() {};\n");
113 for (final String extern: FIXED_EXTERNS) {
114 sb.append("RAW.prototype.").append(extern).append(";\n");
116 externsCode = sb.toString();
121 private static final class APS extends OutputStream {
122 private final Appendable out;
124 public APS(Appendable out) {
128 public void write(int b) throws IOException {
133 private static String[] generateArguments(String compilationLevel) {
134 String[] finalArgs = ARGS.clone();
135 finalArgs[1] = compilationLevel;
140 static int produceTo(Appendable output,
141 ObfuscationLevel obfuscationLevel,
143 ) throws IOException {
144 final ClosureWrapper cw =
145 new ClosureWrapper(output,
146 (obfuscationLevel == ObfuscationLevel.FULL)
147 ? "ADVANCED_OPTIMIZATIONS"
148 : "SIMPLE_OPTIMIZATIONS",
152 } catch (FlagUsageException ex) {
153 throw new IOException(ex);
157 private static final String[] FIXED_EXTERNS = {
203 "getClass__Ljava_lang_Class_2",
204 "clone__Ljava_lang_Object_2"