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 config;
40 private String compiledCode;
41 private String externsCode;
43 private ClosureWrapper(Appendable out,
44 String compilationLevel, Bck2Brwsr config) {
46 generateArguments(compilationLevel),
47 new PrintStream(new APS(out)), System.err
53 protected List<SourceFile> createInputs(List<String> files, boolean allowStdIn) throws FlagUsageException, IOException {
54 if (files.size() != 1 || !"bck2brwsr-raw.js".equals(files.get(0))) {
55 throw new IOException("Unexpected files: " + files);
57 return Collections.nCopies(
59 SourceFile.fromGenerator(
61 new SourceFile.Generator() {
63 public String getCode() {
64 return getCompiledCode();
71 protected List<SourceFile> createExterns()
72 throws FlagUsageException, IOException {
73 final List<SourceFile> externsFiles =
74 new ArrayList<SourceFile>(super.createExterns());
77 SourceFile.fromGenerator(
78 "bck2brwsr_externs.js",
79 new SourceFile.Generator() {
81 public String getCode() {
82 return getExternsCode();
88 private String getCompiledCode() {
89 if (compiledCode == null) {
90 StringBuilder sb = new StringBuilder();
92 VM.compile(sb, config);
93 compiledCode = sb.toString();
94 } catch (IOException ex) {
95 compiledCode = ex.getMessage();
101 private String getExternsCode() {
102 if (externsCode == null) {
103 // need compiled code at this point
106 final StringBuilder sb = new StringBuilder("function RAW() {};\n");
107 for (final String extern: FIXED_EXTERNS) {
108 sb.append("RAW.prototype.").append(extern).append(";\n");
110 externsCode = sb.toString();
115 private static final class APS extends OutputStream {
116 private final Appendable out;
118 public APS(Appendable out) {
122 public void write(int b) throws IOException {
127 private static String[] generateArguments(String compilationLevel) {
128 String[] finalArgs = ARGS.clone();
129 finalArgs[1] = compilationLevel;
134 static int produceTo(Appendable output,
135 ObfuscationLevel obfuscationLevel,
137 ) throws IOException {
138 final ClosureWrapper cw =
139 new ClosureWrapper(output,
140 (obfuscationLevel == ObfuscationLevel.FULL)
141 ? "ADVANCED_OPTIMIZATIONS"
142 : "SIMPLE_OPTIMIZATIONS",
146 } catch (FlagUsageException ex) {
147 throw new IOException(ex);
151 private static final String[] FIXED_EXTERNS = {
195 "getClass__Ljava_lang_Class_2",
196 "clone__Ljava_lang_Object_2"