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;
|
lubomir@869
|
25 |
import java.util.ArrayList;
|
lubomir@869
|
26 |
import java.util.Arrays;
|
lubomir@869
|
27 |
import java.util.Collection;
|
jaroslav@750
|
28 |
import java.util.Collections;
|
jaroslav@750
|
29 |
import java.util.List;
|
jaroslav@750
|
30 |
import org.apidesign.bck2brwsr.core.ExtraJavaScript;
|
lubomir@869
|
31 |
import org.apidesign.vm4brwsr.ByteCodeParser.ClassData;
|
lubomir@869
|
32 |
import org.apidesign.vm4brwsr.ByteCodeParser.FieldData;
|
lubomir@869
|
33 |
import org.apidesign.vm4brwsr.ByteCodeParser.MethodData;
|
jaroslav@750
|
34 |
|
jaroslav@750
|
35 |
/**
|
jaroslav@750
|
36 |
*
|
jaroslav@750
|
37 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@750
|
38 |
*/
|
jaroslav@750
|
39 |
@ExtraJavaScript(processByteCode = false, resource="")
|
lubomir@869
|
40 |
final class ClosureWrapper extends CommandLineRunner {
|
lubomir@869
|
41 |
private static final String[] ARGS = { "--compilation_level", "SIMPLE_OPTIMIZATIONS", "--js", "bck2brwsr-raw.js" /*, "--debug", "--formatting", "PRETTY_PRINT" */ };
|
jaroslav@750
|
42 |
|
lubomir@869
|
43 |
private final ClosuresObfuscationDelegate obfuscationDelegate;
|
jaroslav@750
|
44 |
private final Bck2Brwsr.Resources res;
|
jaroslav@750
|
45 |
private final StringArray classes;
|
lubomir@869
|
46 |
|
lubomir@869
|
47 |
private String compiledCode;
|
lubomir@869
|
48 |
private String externsCode;
|
lubomir@869
|
49 |
|
lubomir@869
|
50 |
private ClosureWrapper(Appendable out,
|
lubomir@869
|
51 |
String compilationLevel,
|
lubomir@869
|
52 |
ClosuresObfuscationDelegate obfuscationDelegate,
|
lubomir@849
|
53 |
Bck2Brwsr.Resources res, StringArray classes) {
|
jaroslav@750
|
54 |
super(
|
lubomir@869
|
55 |
generateArguments(compilationLevel),
|
jaroslav@750
|
56 |
new PrintStream(new APS(out)), System.err
|
jaroslav@750
|
57 |
);
|
lubomir@869
|
58 |
this.obfuscationDelegate = obfuscationDelegate;
|
jaroslav@750
|
59 |
this.res = res;
|
jaroslav@750
|
60 |
this.classes = classes;
|
jaroslav@750
|
61 |
}
|
jaroslav@750
|
62 |
|
jaroslav@750
|
63 |
@Override
|
jaroslav@750
|
64 |
protected List<SourceFile> createInputs(List<String> files, boolean allowStdIn) throws FlagUsageException, IOException {
|
jaroslav@750
|
65 |
if (files.size() != 1 || !"bck2brwsr-raw.js".equals(files.get(0))) {
|
jaroslav@750
|
66 |
throw new IOException("Unexpected files: " + files);
|
jaroslav@750
|
67 |
}
|
lubomir@869
|
68 |
return Collections.nCopies(
|
lubomir@869
|
69 |
1,
|
lubomir@869
|
70 |
SourceFile.fromGenerator(
|
lubomir@869
|
71 |
"bck2brwsr-raw.js",
|
lubomir@869
|
72 |
new SourceFile.Generator() {
|
lubomir@869
|
73 |
@Override
|
lubomir@869
|
74 |
public String getCode() {
|
lubomir@869
|
75 |
return getCompiledCode();
|
lubomir@869
|
76 |
}
|
lubomir@869
|
77 |
}));
|
jaroslav@750
|
78 |
}
|
jaroslav@750
|
79 |
|
lubomir@869
|
80 |
|
jaroslav@750
|
81 |
@Override
|
lubomir@869
|
82 |
protected List<SourceFile> createExterns()
|
lubomir@869
|
83 |
throws FlagUsageException, IOException {
|
lubomir@869
|
84 |
final List<SourceFile> externsFiles =
|
lubomir@869
|
85 |
new ArrayList<SourceFile>(super.createExterns());
|
lubomir@869
|
86 |
|
lubomir@869
|
87 |
externsFiles.add(
|
lubomir@869
|
88 |
SourceFile.fromGenerator(
|
lubomir@869
|
89 |
"bck2brwsr_externs.js",
|
lubomir@869
|
90 |
new SourceFile.Generator() {
|
lubomir@869
|
91 |
@Override
|
lubomir@869
|
92 |
public String getCode() {
|
lubomir@869
|
93 |
return getExternsCode();
|
lubomir@869
|
94 |
}
|
lubomir@869
|
95 |
}));
|
lubomir@869
|
96 |
return externsFiles;
|
lubomir@869
|
97 |
}
|
lubomir@869
|
98 |
|
lubomir@869
|
99 |
private String getCompiledCode() {
|
lubomir@869
|
100 |
if (compiledCode == null) {
|
jaroslav@750
|
101 |
StringBuilder sb = new StringBuilder();
|
jaroslav@750
|
102 |
try {
|
lubomir@869
|
103 |
VM.compile(res, sb, classes, obfuscationDelegate);
|
lubomir@869
|
104 |
compiledCode = sb.toString();
|
jaroslav@750
|
105 |
} catch (IOException ex) {
|
lubomir@869
|
106 |
compiledCode = ex.getMessage();
|
jaroslav@750
|
107 |
}
|
jaroslav@750
|
108 |
}
|
lubomir@869
|
109 |
return compiledCode;
|
lubomir@869
|
110 |
}
|
lubomir@869
|
111 |
|
lubomir@869
|
112 |
private String getExternsCode() {
|
lubomir@869
|
113 |
if (externsCode == null) {
|
lubomir@869
|
114 |
// need compiled code at this point
|
lubomir@869
|
115 |
getCompiledCode();
|
lubomir@869
|
116 |
|
lubomir@869
|
117 |
final StringBuilder sb = new StringBuilder("function RAW() {};\n");
|
lubomir@869
|
118 |
for (final String extern: obfuscationDelegate.getExterns()) {
|
lubomir@869
|
119 |
sb.append("RAW.prototype.").append(extern).append(";\n");
|
lubomir@869
|
120 |
}
|
lubomir@869
|
121 |
externsCode = sb.toString();
|
lubomir@869
|
122 |
}
|
lubomir@869
|
123 |
return externsCode;
|
jaroslav@750
|
124 |
}
|
lubomir@849
|
125 |
|
jaroslav@750
|
126 |
private static final class APS extends OutputStream {
|
jaroslav@750
|
127 |
private final Appendable out;
|
jaroslav@750
|
128 |
|
jaroslav@750
|
129 |
public APS(Appendable out) {
|
jaroslav@750
|
130 |
this.out = out;
|
jaroslav@750
|
131 |
}
|
jaroslav@750
|
132 |
@Override
|
jaroslav@750
|
133 |
public void write(int b) throws IOException {
|
jaroslav@750
|
134 |
out.append((char)b);
|
jaroslav@750
|
135 |
}
|
jaroslav@750
|
136 |
}
|
lubomir@849
|
137 |
|
lubomir@869
|
138 |
private static String[] generateArguments(String compilationLevel) {
|
lubomir@849
|
139 |
String[] finalArgs = ARGS.clone();
|
lubomir@869
|
140 |
finalArgs[1] = compilationLevel;
|
lubomir@849
|
141 |
|
lubomir@849
|
142 |
return finalArgs;
|
lubomir@849
|
143 |
}
|
lubomir@849
|
144 |
|
lubomir@849
|
145 |
static int produceTo(Appendable w, ObfuscationLevel obfuscationLevel, Bck2Brwsr.Resources resources, StringArray arr) throws IOException {
|
lubomir@869
|
146 |
ClosureWrapper cw = create(w, obfuscationLevel, resources, arr);
|
jaroslav@750
|
147 |
try {
|
jaroslav@750
|
148 |
return cw.doRun();
|
jaroslav@750
|
149 |
} catch (FlagUsageException ex) {
|
jaroslav@750
|
150 |
throw new IOException(ex);
|
jaroslav@750
|
151 |
}
|
jaroslav@750
|
152 |
}
|
lubomir@869
|
153 |
|
lubomir@869
|
154 |
private static ClosureWrapper create(Appendable w,
|
lubomir@869
|
155 |
ObfuscationLevel obfuscationLevel,
|
lubomir@869
|
156 |
Bck2Brwsr.Resources resources,
|
lubomir@869
|
157 |
StringArray arr) {
|
lubomir@869
|
158 |
switch (obfuscationLevel) {
|
lubomir@869
|
159 |
case MINIMAL:
|
lubomir@869
|
160 |
return new ClosureWrapper(w, "SIMPLE_OPTIMIZATIONS",
|
lubomir@869
|
161 |
new SimpleObfuscationDelegate(),
|
lubomir@869
|
162 |
resources, arr);
|
jaroslav@882
|
163 |
/*
|
lubomir@869
|
164 |
case MEDIUM:
|
lubomir@869
|
165 |
return new ClosureWrapper(w, "ADVANCED_OPTIMIZATIONS",
|
lubomir@869
|
166 |
new MediumObfuscationDelegate(),
|
lubomir@869
|
167 |
resources, arr);
|
jaroslav@882
|
168 |
*/
|
lubomir@869
|
169 |
case FULL:
|
lubomir@869
|
170 |
return new ClosureWrapper(w, "ADVANCED_OPTIMIZATIONS",
|
lubomir@869
|
171 |
new FullObfuscationDelegate(),
|
lubomir@869
|
172 |
resources, arr);
|
lubomir@869
|
173 |
default:
|
lubomir@869
|
174 |
throw new IllegalArgumentException(
|
lubomir@869
|
175 |
"Unsupported level: " + obfuscationLevel);
|
lubomir@869
|
176 |
}
|
lubomir@869
|
177 |
}
|
lubomir@869
|
178 |
|
lubomir@869
|
179 |
private static abstract class ClosuresObfuscationDelegate
|
lubomir@869
|
180 |
extends ObfuscationDelegate {
|
lubomir@869
|
181 |
public abstract Collection<String> getExterns();
|
lubomir@869
|
182 |
}
|
lubomir@869
|
183 |
|
lubomir@869
|
184 |
private static final class SimpleObfuscationDelegate
|
lubomir@869
|
185 |
extends ClosuresObfuscationDelegate {
|
lubomir@869
|
186 |
@Override
|
lubomir@869
|
187 |
public void exportJSProperty(Appendable out,
|
lubomir@869
|
188 |
String destObject,
|
lubomir@869
|
189 |
String propertyName) throws IOException {
|
lubomir@869
|
190 |
}
|
lubomir@869
|
191 |
|
lubomir@869
|
192 |
@Override
|
lubomir@869
|
193 |
public void exportClass(Appendable out,
|
lubomir@869
|
194 |
String destObject,
|
lubomir@869
|
195 |
String mangledName,
|
lubomir@869
|
196 |
ClassData classData) throws IOException {
|
lubomir@869
|
197 |
}
|
lubomir@869
|
198 |
|
lubomir@869
|
199 |
@Override
|
lubomir@869
|
200 |
public void exportMethod(Appendable out,
|
lubomir@869
|
201 |
String destObject,
|
lubomir@869
|
202 |
String mangledName,
|
lubomir@869
|
203 |
MethodData methodData) throws IOException {
|
lubomir@869
|
204 |
}
|
lubomir@869
|
205 |
|
lubomir@869
|
206 |
@Override
|
lubomir@869
|
207 |
public void exportField(Appendable out,
|
lubomir@869
|
208 |
String destObject,
|
lubomir@869
|
209 |
String mangledName,
|
lubomir@869
|
210 |
FieldData fieldData) throws IOException {
|
lubomir@869
|
211 |
}
|
lubomir@869
|
212 |
|
lubomir@869
|
213 |
@Override
|
lubomir@869
|
214 |
public Collection<String> getExterns() {
|
lubomir@869
|
215 |
return Collections.EMPTY_LIST;
|
lubomir@869
|
216 |
}
|
lubomir@869
|
217 |
}
|
lubomir@869
|
218 |
|
lubomir@869
|
219 |
private static abstract class AdvancedObfuscationDelegate
|
lubomir@869
|
220 |
extends ClosuresObfuscationDelegate {
|
lubomir@869
|
221 |
private static final String[] INITIAL_EXTERNS = {
|
lubomir@869
|
222 |
"bck2brwsr",
|
lubomir@869
|
223 |
"$class",
|
lubomir@869
|
224 |
"anno",
|
lubomir@869
|
225 |
"array",
|
lubomir@869
|
226 |
"access",
|
lubomir@869
|
227 |
"cls",
|
lubomir@869
|
228 |
"vm",
|
lubomir@869
|
229 |
"loadClass",
|
lubomir@869
|
230 |
"loadBytes",
|
lubomir@869
|
231 |
"jvmName",
|
lubomir@869
|
232 |
"primitive",
|
lubomir@869
|
233 |
"superclass",
|
lubomir@869
|
234 |
"cnstr",
|
lubomir@869
|
235 |
"add32",
|
lubomir@869
|
236 |
"sub32",
|
lubomir@869
|
237 |
"mul32",
|
lubomir@869
|
238 |
"neg32",
|
lubomir@869
|
239 |
"toInt8",
|
lubomir@869
|
240 |
"toInt16",
|
lubomir@869
|
241 |
"next32",
|
lubomir@869
|
242 |
"high32",
|
lubomir@869
|
243 |
"toInt32",
|
lubomir@869
|
244 |
"toFP",
|
lubomir@869
|
245 |
"toLong",
|
lubomir@869
|
246 |
"toExactString",
|
lubomir@869
|
247 |
"add64",
|
lubomir@869
|
248 |
"sub64",
|
lubomir@869
|
249 |
"mul64",
|
lubomir@869
|
250 |
"and64",
|
lubomir@869
|
251 |
"or64",
|
lubomir@869
|
252 |
"xor64",
|
lubomir@869
|
253 |
"shl64",
|
lubomir@869
|
254 |
"shr64",
|
lubomir@869
|
255 |
"ushr64",
|
lubomir@869
|
256 |
"compare64",
|
lubomir@869
|
257 |
"neg64",
|
lubomir@869
|
258 |
"div32",
|
lubomir@869
|
259 |
"mod32",
|
lubomir@869
|
260 |
"div64",
|
lubomir@869
|
261 |
"mod64",
|
lubomir@869
|
262 |
"at",
|
lubomir@869
|
263 |
"getClass__Ljava_lang_Class_2",
|
lubomir@869
|
264 |
"clone__Ljava_lang_Object_2"
|
lubomir@869
|
265 |
};
|
lubomir@869
|
266 |
|
lubomir@869
|
267 |
private final Collection<String> externs;
|
lubomir@869
|
268 |
|
lubomir@869
|
269 |
protected AdvancedObfuscationDelegate() {
|
lubomir@869
|
270 |
externs = new ArrayList<String>(Arrays.asList(INITIAL_EXTERNS));
|
lubomir@869
|
271 |
}
|
lubomir@869
|
272 |
|
lubomir@869
|
273 |
@Override
|
lubomir@869
|
274 |
public void exportClass(Appendable out,
|
lubomir@869
|
275 |
String destObject,
|
lubomir@869
|
276 |
String mangledName,
|
lubomir@869
|
277 |
ClassData classData) throws IOException {
|
lubomir@869
|
278 |
exportJSProperty(out, destObject, mangledName);
|
lubomir@869
|
279 |
}
|
lubomir@869
|
280 |
|
lubomir@869
|
281 |
@Override
|
lubomir@869
|
282 |
public void exportMethod(Appendable out,
|
lubomir@869
|
283 |
String destObject,
|
lubomir@869
|
284 |
String mangledName,
|
lubomir@869
|
285 |
MethodData methodData) throws IOException {
|
lubomir@869
|
286 |
if ((methodData.access & ByteCodeParser.ACC_PRIVATE) == 0) {
|
lubomir@869
|
287 |
exportJSProperty(out, destObject, mangledName);
|
lubomir@869
|
288 |
}
|
lubomir@869
|
289 |
}
|
lubomir@869
|
290 |
|
lubomir@869
|
291 |
@Override
|
lubomir@869
|
292 |
public void exportField(Appendable out,
|
lubomir@869
|
293 |
String destObject,
|
lubomir@869
|
294 |
String mangledName,
|
lubomir@869
|
295 |
FieldData fieldData) throws IOException {
|
lubomir@869
|
296 |
if ((fieldData.access & ByteCodeParser.ACC_PRIVATE) == 0) {
|
lubomir@869
|
297 |
exportJSProperty(out, destObject, mangledName);
|
lubomir@869
|
298 |
}
|
lubomir@869
|
299 |
}
|
lubomir@869
|
300 |
|
lubomir@869
|
301 |
@Override
|
lubomir@869
|
302 |
public Collection<String> getExterns() {
|
lubomir@869
|
303 |
return externs;
|
lubomir@869
|
304 |
}
|
lubomir@869
|
305 |
|
lubomir@869
|
306 |
protected void addExtern(String extern) {
|
lubomir@869
|
307 |
externs.add(extern);
|
lubomir@869
|
308 |
}
|
lubomir@869
|
309 |
}
|
lubomir@869
|
310 |
|
lubomir@869
|
311 |
private static final class MediumObfuscationDelegate
|
lubomir@869
|
312 |
extends AdvancedObfuscationDelegate {
|
lubomir@869
|
313 |
@Override
|
lubomir@869
|
314 |
public void exportJSProperty(Appendable out,
|
lubomir@869
|
315 |
String destObject,
|
lubomir@869
|
316 |
String propertyName) {
|
lubomir@869
|
317 |
addExtern(propertyName);
|
lubomir@869
|
318 |
}
|
lubomir@869
|
319 |
}
|
lubomir@869
|
320 |
|
lubomir@869
|
321 |
private static final class FullObfuscationDelegate
|
lubomir@869
|
322 |
extends AdvancedObfuscationDelegate {
|
lubomir@869
|
323 |
@Override
|
lubomir@869
|
324 |
public void exportJSProperty(Appendable out,
|
lubomir@869
|
325 |
String destObject,
|
lubomir@869
|
326 |
String propertyName) throws IOException {
|
lubomir@869
|
327 |
out.append("\n").append(destObject).append("['")
|
lubomir@869
|
328 |
.append(propertyName)
|
lubomir@869
|
329 |
.append("'] = ")
|
lubomir@869
|
330 |
.append(destObject).append(".").append(propertyName)
|
lubomir@869
|
331 |
.append(";\n");
|
lubomir@869
|
332 |
}
|
lubomir@869
|
333 |
}
|
jaroslav@750
|
334 |
}
|