jaroslav@750: /** jaroslav@750: * Back 2 Browser Bytecode Translator jaroslav@750: * Copyright (C) 2012 Jaroslav Tulach jaroslav@750: * jaroslav@750: * This program is free software: you can redistribute it and/or modify jaroslav@750: * it under the terms of the GNU General Public License as published by jaroslav@750: * the Free Software Foundation, version 2 of the License. jaroslav@750: * jaroslav@750: * This program is distributed in the hope that it will be useful, jaroslav@750: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@750: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@750: * GNU General Public License for more details. jaroslav@750: * jaroslav@750: * You should have received a copy of the GNU General Public License jaroslav@750: * along with this program. Look for COPYING file in the top folder. jaroslav@750: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@750: */ jaroslav@750: package org.apidesign.vm4brwsr; jaroslav@750: jaroslav@750: import com.google.javascript.jscomp.CommandLineRunner; jaroslav@750: import com.google.javascript.jscomp.SourceFile; jaroslav@750: import java.io.IOException; jaroslav@750: import java.io.OutputStream; jaroslav@750: import java.io.PrintStream; jaroslav@750: import java.util.Collections; jaroslav@750: import java.util.List; jaroslav@750: import org.apidesign.bck2brwsr.core.ExtraJavaScript; jaroslav@750: jaroslav@750: /** jaroslav@750: * jaroslav@750: * @author Jaroslav Tulach jaroslav@750: */ jaroslav@750: @ExtraJavaScript(processByteCode = false, resource="") jaroslav@750: final class ClosureWrapper extends CommandLineRunner implements SourceFile.Generator { jaroslav@750: private static final String[] ARGS = { "--compilation_level", "SIMPLE_OPTIMIZATIONS", "--js", "bck2brwsr-raw.js" }; jaroslav@750: jaroslav@750: private String code; jaroslav@750: private final Bck2Brwsr.Resources res; jaroslav@750: private final StringArray classes; lubomir@849: private ClosureWrapper(Appendable out, ObfuscationLevel obfuscationLevel, lubomir@849: Bck2Brwsr.Resources res, StringArray classes) { jaroslav@750: super( lubomir@849: generateArguments(obfuscationLevel), jaroslav@750: new PrintStream(new APS(out)), System.err jaroslav@750: ); jaroslav@750: this.res = res; jaroslav@750: this.classes = classes; jaroslav@750: } jaroslav@750: jaroslav@750: @Override jaroslav@750: protected List createInputs(List files, boolean allowStdIn) throws FlagUsageException, IOException { jaroslav@750: if (files.size() != 1 || !"bck2brwsr-raw.js".equals(files.get(0))) { jaroslav@750: throw new IOException("Unexpected files: " + files); jaroslav@750: } jaroslav@750: return Collections.nCopies(1, SourceFile.fromGenerator("bck2brwsr-raw.js", this)); jaroslav@750: } jaroslav@750: jaroslav@750: @Override jaroslav@750: public String getCode() { jaroslav@750: if (code == null) { jaroslav@750: StringBuilder sb = new StringBuilder(); jaroslav@750: try { jaroslav@750: VM.compile(res, sb, classes); jaroslav@750: } catch (IOException ex) { jaroslav@750: code = ex.getMessage(); jaroslav@750: } jaroslav@750: code = sb.toString(); jaroslav@750: } jaroslav@750: return code; jaroslav@750: } lubomir@849: jaroslav@750: private static final class APS extends OutputStream { jaroslav@750: private final Appendable out; jaroslav@750: jaroslav@750: public APS(Appendable out) { jaroslav@750: this.out = out; jaroslav@750: } jaroslav@750: @Override jaroslav@750: public void write(int b) throws IOException { jaroslav@750: out.append((char)b); jaroslav@750: } jaroslav@750: } lubomir@849: lubomir@849: private static String[] generateArguments( lubomir@849: ObfuscationLevel obfuscationLevel) { lubomir@849: String[] finalArgs = ARGS.clone(); lubomir@849: finalArgs[1] = obfuscationLevel.toString(); lubomir@849: lubomir@849: return finalArgs; lubomir@849: } lubomir@849: lubomir@849: static int produceTo(Appendable w, ObfuscationLevel obfuscationLevel, Bck2Brwsr.Resources resources, StringArray arr) throws IOException { lubomir@849: ClosureWrapper cw = new ClosureWrapper(w, obfuscationLevel, resources, lubomir@849: arr); jaroslav@750: try { jaroslav@750: return cw.doRun(); jaroslav@750: } catch (FlagUsageException ex) { jaroslav@750: throw new IOException(ex); jaroslav@750: } jaroslav@750: } jaroslav@750: }