jaroslav@750
|
1 |
/**
|
jaroslav@750
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@750
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@750
|
4 |
*
|
jaroslav@750
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@750
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@750
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@750
|
8 |
*
|
jaroslav@750
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@750
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@750
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@750
|
12 |
* GNU General Public License for more details.
|
jaroslav@750
|
13 |
*
|
jaroslav@750
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@750
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@750
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@750
|
17 |
*/
|
jaroslav@750
|
18 |
package org.apidesign.vm4brwsr;
|
jaroslav@750
|
19 |
|
jaroslav@750
|
20 |
import com.google.javascript.jscomp.CommandLineRunner;
|
jaroslav@750
|
21 |
import com.google.javascript.jscomp.SourceFile;
|
jaroslav@750
|
22 |
import java.io.IOException;
|
jaroslav@750
|
23 |
import java.io.OutputStream;
|
jaroslav@750
|
24 |
import java.io.PrintStream;
|
jaroslav@750
|
25 |
import java.util.Collections;
|
jaroslav@750
|
26 |
import java.util.List;
|
jaroslav@750
|
27 |
import org.apidesign.bck2brwsr.core.ExtraJavaScript;
|
jaroslav@750
|
28 |
|
jaroslav@750
|
29 |
/**
|
jaroslav@750
|
30 |
*
|
jaroslav@750
|
31 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@750
|
32 |
*/
|
jaroslav@750
|
33 |
@ExtraJavaScript(processByteCode = false, resource="")
|
jaroslav@750
|
34 |
final class ClosureWrapper extends CommandLineRunner implements SourceFile.Generator {
|
jaroslav@750
|
35 |
private static final String[] ARGS = { "--compilation_level", "SIMPLE_OPTIMIZATIONS", "--js", "bck2brwsr-raw.js" };
|
jaroslav@750
|
36 |
|
jaroslav@750
|
37 |
private String code;
|
jaroslav@750
|
38 |
private final Bck2Brwsr.Resources res;
|
jaroslav@750
|
39 |
private final StringArray classes;
|
lubomir@849
|
40 |
private ClosureWrapper(Appendable out, ObfuscationLevel obfuscationLevel,
|
lubomir@849
|
41 |
Bck2Brwsr.Resources res, StringArray classes) {
|
jaroslav@750
|
42 |
super(
|
lubomir@849
|
43 |
generateArguments(obfuscationLevel),
|
jaroslav@750
|
44 |
new PrintStream(new APS(out)), System.err
|
jaroslav@750
|
45 |
);
|
jaroslav@750
|
46 |
this.res = res;
|
jaroslav@750
|
47 |
this.classes = classes;
|
jaroslav@750
|
48 |
}
|
jaroslav@750
|
49 |
|
jaroslav@750
|
50 |
@Override
|
jaroslav@750
|
51 |
protected List<SourceFile> createInputs(List<String> files, boolean allowStdIn) throws FlagUsageException, IOException {
|
jaroslav@750
|
52 |
if (files.size() != 1 || !"bck2brwsr-raw.js".equals(files.get(0))) {
|
jaroslav@750
|
53 |
throw new IOException("Unexpected files: " + files);
|
jaroslav@750
|
54 |
}
|
jaroslav@750
|
55 |
return Collections.nCopies(1, SourceFile.fromGenerator("bck2brwsr-raw.js", this));
|
jaroslav@750
|
56 |
}
|
jaroslav@750
|
57 |
|
jaroslav@750
|
58 |
@Override
|
jaroslav@750
|
59 |
public String getCode() {
|
jaroslav@750
|
60 |
if (code == null) {
|
jaroslav@750
|
61 |
StringBuilder sb = new StringBuilder();
|
jaroslav@750
|
62 |
try {
|
jaroslav@750
|
63 |
VM.compile(res, sb, classes);
|
jaroslav@750
|
64 |
} catch (IOException ex) {
|
jaroslav@750
|
65 |
code = ex.getMessage();
|
jaroslav@750
|
66 |
}
|
jaroslav@750
|
67 |
code = sb.toString();
|
jaroslav@750
|
68 |
}
|
jaroslav@750
|
69 |
return code;
|
jaroslav@750
|
70 |
}
|
lubomir@849
|
71 |
|
jaroslav@750
|
72 |
private static final class APS extends OutputStream {
|
jaroslav@750
|
73 |
private final Appendable out;
|
jaroslav@750
|
74 |
|
jaroslav@750
|
75 |
public APS(Appendable out) {
|
jaroslav@750
|
76 |
this.out = out;
|
jaroslav@750
|
77 |
}
|
jaroslav@750
|
78 |
@Override
|
jaroslav@750
|
79 |
public void write(int b) throws IOException {
|
jaroslav@750
|
80 |
out.append((char)b);
|
jaroslav@750
|
81 |
}
|
jaroslav@750
|
82 |
}
|
lubomir@849
|
83 |
|
lubomir@849
|
84 |
private static String[] generateArguments(
|
lubomir@849
|
85 |
ObfuscationLevel obfuscationLevel) {
|
lubomir@849
|
86 |
String[] finalArgs = ARGS.clone();
|
lubomir@849
|
87 |
finalArgs[1] = obfuscationLevel.toString();
|
lubomir@849
|
88 |
|
lubomir@849
|
89 |
return finalArgs;
|
lubomir@849
|
90 |
}
|
lubomir@849
|
91 |
|
lubomir@849
|
92 |
static int produceTo(Appendable w, ObfuscationLevel obfuscationLevel, Bck2Brwsr.Resources resources, StringArray arr) throws IOException {
|
lubomir@849
|
93 |
ClosureWrapper cw = new ClosureWrapper(w, obfuscationLevel, resources,
|
lubomir@849
|
94 |
arr);
|
jaroslav@750
|
95 |
try {
|
jaroslav@750
|
96 |
return cw.doRun();
|
jaroslav@750
|
97 |
} catch (FlagUsageException ex) {
|
jaroslav@750
|
98 |
throw new IOException(ex);
|
jaroslav@750
|
99 |
}
|
jaroslav@750
|
100 |
}
|
jaroslav@750
|
101 |
}
|