Make sure Object is always available in each VM and each extension.
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 "--output_wrapper", "(function() {%output%})(this);",
40 "--js", "bck2brwsr-raw.js"
42 //, "--formatting", "PRETTY_PRINT"
45 private final Bck2Brwsr config;
47 private String compiledCode;
48 private String externsCode;
50 private ClosureWrapper(Appendable out,
51 String compilationLevel, Bck2Brwsr config) {
53 generateArguments(compilationLevel),
54 new PrintStream(new APS(out)), System.err
60 protected List<SourceFile> createInputs(List<String> files, boolean allowStdIn) throws FlagUsageException, IOException {
61 if (files.size() != 1 || !"bck2brwsr-raw.js".equals(files.get(0))) {
62 throw new IOException("Unexpected files: " + files);
64 return Collections.nCopies(
66 SourceFile.fromGenerator(
68 new SourceFile.Generator() {
70 public String getCode() {
71 return getCompiledCode();
78 protected List<SourceFile> createExterns()
79 throws FlagUsageException, IOException {
80 final List<SourceFile> externsFiles =
81 new ArrayList<SourceFile>(super.createExterns());
84 SourceFile.fromGenerator(
85 "bck2brwsr_externs.js",
86 new SourceFile.Generator() {
88 public String getCode() {
89 return getExternsCode();
95 private String getCompiledCode() {
96 if (compiledCode == null) {
97 StringBuilder sb = new StringBuilder();
99 VM.compile(sb, config);
100 compiledCode = sb.toString();
101 } catch (IOException ex) {
102 compiledCode = ex.getMessage();
108 private String getExternsCode() {
109 if (externsCode == null) {
110 // need compiled code at this point
113 final StringBuilder sb = new StringBuilder("function RAW() {};\n");
114 for (final String extern: FIXED_EXTERNS) {
115 sb.append("RAW.prototype.").append(extern).append(";\n");
117 externsCode = sb.toString();
122 private static final class APS extends OutputStream {
123 private final Appendable out;
125 public APS(Appendable out) {
129 public void write(int b) throws IOException {
134 private static String[] generateArguments(String compilationLevel) {
135 String[] finalArgs = ARGS.clone();
136 finalArgs[1] = compilationLevel;
141 static int produceTo(Appendable output,
142 ObfuscationLevel obfuscationLevel,
144 ) throws IOException {
145 final ClosureWrapper cw =
146 new ClosureWrapper(output,
147 (obfuscationLevel == ObfuscationLevel.FULL)
148 ? "ADVANCED_OPTIMIZATIONS"
149 : "SIMPLE_OPTIMIZATIONS",
153 } catch (FlagUsageException ex) {
154 throw new IOException(ex);
158 private static final String[] FIXED_EXTERNS = {
204 "getClass__Ljava_lang_Class_2",
205 "clone__Ljava_lang_Object_2"