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;
|
jaroslav@750
|
40 |
private ClosureWrapper(Appendable out, Bck2Brwsr.Resources res, StringArray classes) {
|
jaroslav@750
|
41 |
super(
|
jaroslav@750
|
42 |
ARGS,
|
jaroslav@750
|
43 |
new PrintStream(new APS(out)), System.err
|
jaroslav@750
|
44 |
);
|
jaroslav@750
|
45 |
this.res = res;
|
jaroslav@750
|
46 |
this.classes = classes;
|
jaroslav@750
|
47 |
}
|
jaroslav@750
|
48 |
|
jaroslav@750
|
49 |
@Override
|
jaroslav@750
|
50 |
protected List<SourceFile> createInputs(List<String> files, boolean allowStdIn) throws FlagUsageException, IOException {
|
jaroslav@750
|
51 |
if (files.size() != 1 || !"bck2brwsr-raw.js".equals(files.get(0))) {
|
jaroslav@750
|
52 |
throw new IOException("Unexpected files: " + files);
|
jaroslav@750
|
53 |
}
|
jaroslav@750
|
54 |
return Collections.nCopies(1, SourceFile.fromGenerator("bck2brwsr-raw.js", this));
|
jaroslav@750
|
55 |
}
|
jaroslav@750
|
56 |
|
jaroslav@750
|
57 |
@Override
|
jaroslav@750
|
58 |
public String getCode() {
|
jaroslav@750
|
59 |
if (code == null) {
|
jaroslav@750
|
60 |
StringBuilder sb = new StringBuilder();
|
jaroslav@750
|
61 |
try {
|
jaroslav@750
|
62 |
VM.compile(res, sb, classes);
|
jaroslav@750
|
63 |
} catch (IOException ex) {
|
jaroslav@750
|
64 |
code = ex.getMessage();
|
jaroslav@750
|
65 |
}
|
jaroslav@750
|
66 |
code = sb.toString();
|
jaroslav@750
|
67 |
}
|
jaroslav@750
|
68 |
return code;
|
jaroslav@750
|
69 |
}
|
jaroslav@750
|
70 |
|
jaroslav@750
|
71 |
private static final class APS extends OutputStream {
|
jaroslav@750
|
72 |
private final Appendable out;
|
jaroslav@750
|
73 |
|
jaroslav@750
|
74 |
public APS(Appendable out) {
|
jaroslav@750
|
75 |
this.out = out;
|
jaroslav@750
|
76 |
}
|
jaroslav@750
|
77 |
@Override
|
jaroslav@750
|
78 |
public void write(int b) throws IOException {
|
jaroslav@750
|
79 |
out.append((char)b);
|
jaroslav@750
|
80 |
}
|
jaroslav@750
|
81 |
}
|
jaroslav@750
|
82 |
static int produceTo(Appendable w, Bck2Brwsr.Resources resources, StringArray arr) throws IOException {
|
jaroslav@750
|
83 |
ClosureWrapper cw = new ClosureWrapper(w, resources, arr);
|
jaroslav@750
|
84 |
try {
|
jaroslav@750
|
85 |
return cw.doRun();
|
jaroslav@750
|
86 |
} catch (FlagUsageException ex) {
|
jaroslav@750
|
87 |
throw new IOException(ex);
|
jaroslav@750
|
88 |
}
|
jaroslav@750
|
89 |
}
|
jaroslav@750
|
90 |
}
|