2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012-2015 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.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.io.OutputStream;
25 import java.io.PrintStream;
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.List;
29 import org.apidesign.bck2brwsr.core.ExtraJavaScript;
33 * @author Jaroslav Tulach <jtulach@netbeans.org>
35 @ExtraJavaScript(processByteCode = false, resource="")
36 final class ClosureWrapper extends CommandLineRunner {
37 private static final String[] ARGS = {
38 "--compilation_level",
39 "SIMPLE_OPTIMIZATIONS",
40 "--output_wrapper", "(function() {%output%})(this);",
41 "--js", "bck2brwsr-raw.js"
43 //, "--formatting", "PRETTY_PRINT"
46 private final Bck2Brwsr config;
48 private String compiledCode;
49 private String externsCode;
51 private ClosureWrapper(Appendable out,
52 String compilationLevel, Bck2Brwsr config, PrintStream err) {
54 generateArguments(compilationLevel),
55 new PrintStream(new APS(out)), err
61 protected List<SourceFile> createInputs(List<String> files, boolean allowStdIn) throws FlagUsageException, IOException {
62 if (files.size() != 1 || !"bck2brwsr-raw.js".equals(files.get(0))) {
63 throw new IOException("Unexpected files: " + files);
65 return Collections.nCopies(
67 SourceFile.fromGenerator(
69 new SourceFile.Generator() {
71 public String getCode() {
72 return getCompiledCode();
79 protected List<SourceFile> createExterns()
80 throws FlagUsageException, IOException {
81 final List<SourceFile> externsFiles =
82 new ArrayList<SourceFile>(super.createExterns());
85 SourceFile.fromGenerator(
86 "bck2brwsr_externs.js",
87 new SourceFile.Generator() {
89 public String getCode() {
90 return getExternsCode();
96 private String getCompiledCode() {
97 if (compiledCode == null) {
98 StringBuilder sb = new StringBuilder();
100 VM.compile(sb, config);
101 compiledCode = sb.toString();
102 } catch (IOException ex) {
103 compiledCode = ex.getMessage();
109 private String getExternsCode() {
110 if (externsCode == null) {
111 // need compiled code at this point
114 final StringBuilder sb = new StringBuilder("function RAW() {};\n");
115 for (final String extern: FIXED_EXTERNS) {
116 sb.append("RAW.prototype.").append(extern).append(";\n");
118 externsCode = sb.toString();
123 private static final class APS extends OutputStream {
124 private final Appendable out;
126 public APS(Appendable out) {
130 public void write(int b) throws IOException {
135 private static String[] generateArguments(String compilationLevel) {
136 String[] finalArgs = ARGS.clone();
137 finalArgs[1] = compilationLevel;
142 static int produceTo(Appendable output,
143 ObfuscationLevel obfuscationLevel,
145 ) throws IOException {
146 ByteArrayOutputStream out = new ByteArrayOutputStream();
147 PrintStream err = new PrintStream(out);
148 final ClosureWrapper cw = new ClosureWrapper(output,
149 (obfuscationLevel == ObfuscationLevel.FULL) ? "ADVANCED_OPTIMIZATIONS" : "SIMPLE_OPTIMIZATIONS",
154 } catch (FlagUsageException ex) {
155 throw new IOException(out.toString(), ex);
159 private static final String[] FIXED_EXTERNS = {
206 "getClass__Ljava_lang_Class_2",
207 "clone__Ljava_lang_Object_2"
210 // workarounding export errors in ko4j @ 1.0