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.Collections;
26 import java.util.List;
27 import org.apidesign.bck2brwsr.core.ExtraJavaScript;
31 * @author Jaroslav Tulach <jtulach@netbeans.org>
33 @ExtraJavaScript(processByteCode = false, resource="")
34 final class ClosureWrapper extends CommandLineRunner implements SourceFile.Generator {
35 private static final String[] ARGS = { "--compilation_level", "SIMPLE_OPTIMIZATIONS", "--js", "bck2brwsr-raw.js" };
38 private final Bck2Brwsr.Resources res;
39 private final StringArray classes;
40 private ClosureWrapper(Appendable out, Bck2Brwsr.Resources res, StringArray classes) {
43 new PrintStream(new APS(out)), System.err
46 this.classes = classes;
50 protected List<SourceFile> createInputs(List<String> files, boolean allowStdIn) throws FlagUsageException, IOException {
51 if (files.size() != 1 || !"bck2brwsr-raw.js".equals(files.get(0))) {
52 throw new IOException("Unexpected files: " + files);
54 return Collections.nCopies(1, SourceFile.fromGenerator("bck2brwsr-raw.js", this));
58 public String getCode() {
60 StringBuilder sb = new StringBuilder();
62 VM.compile(res, sb, classes);
63 } catch (IOException ex) {
64 code = ex.getMessage();
71 private static final class APS extends OutputStream {
72 private final Appendable out;
74 public APS(Appendable out) {
78 public void write(int b) throws IOException {
82 static int produceTo(Appendable w, Bck2Brwsr.Resources resources, StringArray arr) throws IOException {
83 ClosureWrapper cw = new ClosureWrapper(w, resources, arr);
86 } catch (FlagUsageException ex) {
87 throw new IOException(ex);