jaroslav@298
|
1 |
/**
|
jaroslav@298
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@1787
|
3 |
* Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@298
|
4 |
*
|
jaroslav@298
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@298
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@298
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@298
|
8 |
*
|
jaroslav@298
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@298
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@298
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@298
|
12 |
* GNU General Public License for more details.
|
jaroslav@298
|
13 |
*
|
jaroslav@298
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@298
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@298
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@298
|
17 |
*/
|
jaroslav@298
|
18 |
package org.apidesign.vm4brwsr;
|
jaroslav@298
|
19 |
|
jaroslav@298
|
20 |
import java.io.IOException;
|
jaroslav@298
|
21 |
import java.io.InputStream;
|
jaroslav@1813
|
22 |
import org.apidesign.vm4brwsr.ByteCodeParser.MethodData;
|
jaroslav@298
|
23 |
|
jaroslav@298
|
24 |
/** Build your own virtual machine! Use methods in this class to generate
|
jaroslav@298
|
25 |
* a skeleton JVM in JavaScript that contains pre-compiled classes of your
|
jaroslav@1783
|
26 |
* choice:
|
jaroslav@1783
|
27 |
* <pre>
|
jaroslav@1783
|
28 |
* Writer w = new {@link java.io.StringWriter}();
|
jaroslav@1783
|
29 |
* {@link #newCompiler() Bck2Brwsr.newCompiler}()
|
jaroslav@1783
|
30 |
* .{@link #resources(org.apidesign.vm4brwsr.Bck2Brwsr.Resources)}
|
jaroslav@1783
|
31 |
* .{@link #addRootClasses(java.lang.String...)}
|
jaroslav@1783
|
32 |
* .{@link #addClasses(java.lang.String...)}
|
jaroslav@1783
|
33 |
* .{@link #addExported(java.lang.String...)}
|
jaroslav@1783
|
34 |
* .{@link #generate(java.lang.Appendable) generate(w)};
|
jaroslav@1783
|
35 |
* System.out.{@link java.io.PrintStream#print(java.lang.String) print(w.toString())};
|
jaroslav@1783
|
36 |
* </pre>
|
jaroslav@1783
|
37 |
* The generated script defines one JavaScript method that can
|
jaroslav@298
|
38 |
* be used to bootstrap and load the virtual machine: <pre>
|
jaroslav@298
|
39 |
* var vm = bck2brwsr();
|
jaroslav@298
|
40 |
* var main = vm.loadClass('org.your.pkg.Main');
|
jaroslav@1609
|
41 |
* main.invoke('main');
|
jaroslav@298
|
42 |
* </pre>
|
jaroslav@298
|
43 |
* In case one wants to initialize the virtual machine with ability to
|
jaroslav@298
|
44 |
* load classes lazily when needed, one can provide a loader function to
|
jaroslav@298
|
45 |
* when creating the virtual machine: <pre>
|
jaroslav@298
|
46 |
* var vm = bck2brwsr(function(resource) {
|
jaroslav@298
|
47 |
* return null; // byte[] for the resource
|
jaroslav@298
|
48 |
* });
|
jaroslav@298
|
49 |
* </pre>
|
jaroslav@424
|
50 |
* In this scenario, when a request for an unknown class is made, the loader
|
jaroslav@298
|
51 |
* function is asked for its byte code and the system dynamically transforms
|
jaroslav@298
|
52 |
* it to JavaScript.
|
jaroslav@729
|
53 |
* <p>
|
jaroslav@1609
|
54 |
* Instead of a loader function, one can also provide a URL to a JAR file
|
jaroslav@1609
|
55 |
* or a library JavaScript file generated with {@link #library(java.lang.String...)}
|
jaroslav@1609
|
56 |
* option on.
|
jaroslav@729
|
57 |
* The <code>bck2brwsr</code> system will do its best to download the file
|
jaroslav@1609
|
58 |
* and provide loader function for it automatically. In order to use
|
jaroslav@1609
|
59 |
* the JAR file <code>emul.zip</code> module needs to be available in the system.
|
jaroslav@729
|
60 |
* <p>
|
jaroslav@1609
|
61 |
* One can provide as many loader functions and URL references as necessary.
|
jaroslav@729
|
62 |
* Then the initialization code would look like:<pre>
|
jaroslav@729
|
63 |
* var vm = bck2brwsr(url1, url2, fnctn1, url3, functn2);
|
jaroslav@729
|
64 |
* </pre>
|
jaroslav@729
|
65 |
* The provided URLs and loader functions will be consulted one by one.
|
jaroslav@1609
|
66 |
* <p>
|
jaroslav@1609
|
67 |
* The initialization of the <b>Bck2Brwsr</b> is done asynchronously since
|
jaroslav@1609
|
68 |
* version 0.9. E.g. call to <pre>
|
jaroslav@1609
|
69 |
* var vm = bck2brwsr('myapp.js');
|
jaroslav@1609
|
70 |
* var main = vm.loadClass('org.your.pkg.Main');
|
jaroslav@1609
|
71 |
* main.invoke('main');
|
jaroslav@1609
|
72 |
* </pre>
|
jaroslav@1609
|
73 |
* returns immediately and the call to the static main method will happen
|
jaroslav@1609
|
74 |
* once the virtual machine is initialized and the class available.
|
jaroslav@298
|
75 |
*
|
jaroslav@1783
|
76 |
* @author Jaroslav Tulach
|
jaroslav@298
|
77 |
*/
|
jaroslav@298
|
78 |
public final class Bck2Brwsr {
|
jaroslav@874
|
79 |
private final ObfuscationLevel level;
|
jaroslav@1583
|
80 |
private final StringArray exported;
|
jaroslav@874
|
81 |
private final StringArray classes;
|
jaroslav@1493
|
82 |
private final StringArray resources;
|
jaroslav@874
|
83 |
private final Resources res;
|
jaroslav@1584
|
84 |
private final Boolean extension;
|
jaroslav@1604
|
85 |
private final StringArray classpath;
|
jaroslav@1812
|
86 |
private final Flow.Analyzer flow;
|
jaroslav@874
|
87 |
|
jaroslav@1584
|
88 |
private Bck2Brwsr(
|
jaroslav@1812
|
89 |
ObfuscationLevel level, StringArray exported,
|
jaroslav@1812
|
90 |
StringArray classes, StringArray resources, Resources res,
|
jaroslav@1812
|
91 |
Boolean extension, StringArray classpath, Flow.Analyzer flow
|
jaroslav@1584
|
92 |
) {
|
jaroslav@874
|
93 |
this.level = level;
|
jaroslav@1583
|
94 |
this.exported = exported;
|
jaroslav@874
|
95 |
this.classes = classes;
|
jaroslav@1493
|
96 |
this.resources = resources;
|
jaroslav@1493
|
97 |
this.res = res;
|
lubomir@1029
|
98 |
this.extension = extension;
|
jaroslav@1604
|
99 |
this.classpath = classpath;
|
jaroslav@1812
|
100 |
this.flow = flow;
|
jaroslav@298
|
101 |
}
|
jaroslav@874
|
102 |
|
jaroslav@874
|
103 |
/** Helper method to generate virtual machine from bytes served by a <code>resources</code>
|
jaroslav@298
|
104 |
* provider.
|
lubomir@849
|
105 |
*
|
jaroslav@298
|
106 |
* @param out the output to write the generated JavaScript to
|
jaroslav@298
|
107 |
* @param resources provider of class files to use
|
jaroslav@298
|
108 |
* @param classes additional classes to include in the generated script
|
jaroslav@298
|
109 |
* @throws IOException I/O exception can be thrown when something goes wrong
|
jaroslav@298
|
110 |
*/
|
jaroslav@298
|
111 |
public static void generate(Appendable out, Resources resources, String... classes) throws IOException {
|
jaroslav@874
|
112 |
newCompiler().resources(resources).addRootClasses(classes).generate(out);
|
lubomir@849
|
113 |
}
|
lubomir@849
|
114 |
|
jaroslav@874
|
115 |
/** Helper method to generate virtual machine from bytes served by a class loader.
|
lubomir@849
|
116 |
*
|
lubomir@849
|
117 |
* @param out the output to write the generated JavaScript to
|
lubomir@849
|
118 |
* @param loader class loader to load needed classes from
|
lubomir@849
|
119 |
* @param classes additional classes to include in the generated script
|
lubomir@849
|
120 |
* @throws IOException I/O exception can be thrown when something goes wrong
|
lubomir@849
|
121 |
*/
|
lubomir@849
|
122 |
public static void generate(Appendable out, ClassLoader loader, String... classes) throws IOException {
|
jaroslav@874
|
123 |
newCompiler().resources(loader).addRootClasses(classes).generate(out);
|
jaroslav@874
|
124 |
}
|
jaroslav@874
|
125 |
|
jaroslav@874
|
126 |
/** Creates new instance of Bck2Brwsr compiler which is ready to generate
|
jaroslav@874
|
127 |
* empty Bck2Brwsr virtual machine. The instance can be further
|
jaroslav@874
|
128 |
* configured by calling chain of methods. For example:
|
jaroslav@874
|
129 |
* <pre>
|
jaroslav@1783
|
130 |
* {@link #newCompiler()}.{@link #resources(org.apidesign.vm4brwsr.Bck2Brwsr.Resources) resources(loader)}.{@link #addRootClasses(java.lang.String[]) addRootClasses("your/Clazz")}.{@link #generate(java.lang.Appendable) generate(out)};
|
jaroslav@874
|
131 |
* </pre>
|
jaroslav@874
|
132 |
*
|
jaroslav@874
|
133 |
* @return new instance of the Bck2Brwsr compiler
|
jaroslav@874
|
134 |
* @since 0.5
|
jaroslav@874
|
135 |
*/
|
jaroslav@874
|
136 |
public static Bck2Brwsr newCompiler() {
|
jaroslav@1604
|
137 |
return new Bck2Brwsr(
|
jaroslav@1604
|
138 |
ObfuscationLevel.NONE,
|
jaroslav@1604
|
139 |
new StringArray(), new StringArray(), new StringArray(),
|
jaroslav@1812
|
140 |
null, false, null, null
|
jaroslav@1604
|
141 |
);
|
lubomir@849
|
142 |
}
|
jaroslav@1583
|
143 |
|
jaroslav@1583
|
144 |
/** Adds exported classes or packages. If the string ends
|
jaroslav@1583
|
145 |
* with slash, it is considered a name of package. If it does not,
|
jaroslav@1583
|
146 |
* it is a name of a class (without <code>.class</code> suffix).
|
jaroslav@1583
|
147 |
* The exported classes are prevented from being obfuscated.
|
jaroslav@1583
|
148 |
* All public classes in exported packages are prevented from
|
jaroslav@1583
|
149 |
* being obfuscated. By listing the packages or classes in this
|
jaroslav@1583
|
150 |
* method, these classes are not guaranteed to be included in
|
jaroslav@1583
|
151 |
* the generated script. Use {@link #addClasses} to include
|
jaroslav@1583
|
152 |
* the classes.
|
jaroslav@1583
|
153 |
*
|
jaroslav@1583
|
154 |
* @param exported names of classes and packages to treat as exported
|
jaroslav@1583
|
155 |
* @return new instances of the Bck2Brwsr compiler which inherits
|
jaroslav@1583
|
156 |
* all values from <code>this</code> except list of exported classes
|
jaroslav@1583
|
157 |
*/
|
jaroslav@1583
|
158 |
public Bck2Brwsr addExported(String... exported) {
|
jaroslav@1583
|
159 |
return new Bck2Brwsr(
|
jaroslav@1583
|
160 |
level, this.exported.addAndNew(exported),
|
jaroslav@1812
|
161 |
classes, resources, res, extension, classpath, flow
|
jaroslav@1583
|
162 |
);
|
jaroslav@1583
|
163 |
}
|
lubomir@849
|
164 |
|
jaroslav@1491
|
165 |
/** Adds additional classes
|
jaroslav@1491
|
166 |
* to the list of those that should be included in the generated
|
jaroslav@1491
|
167 |
* JavaScript file.
|
jaroslav@1491
|
168 |
* These classes are guaranteed to be available in the
|
jaroslav@1491
|
169 |
* generated virtual machine code accessible using their fully
|
jaroslav@1491
|
170 |
* qualified name. This brings the same behavior as if the
|
jaroslav@1491
|
171 |
* classes were added by {@link #addClasses(java.lang.String...) } and
|
jaroslav@1583
|
172 |
* exported via {@link #addExported(java.lang.String...)}.
|
jaroslav@874
|
173 |
*
|
jaroslav@874
|
174 |
* @param classes the classes to add to the compilation
|
jaroslav@1491
|
175 |
* @return new instance of the Bck2Brwsr compiler which inherits
|
jaroslav@1491
|
176 |
* all values from <code>this</code>
|
jaroslav@874
|
177 |
*/
|
jaroslav@874
|
178 |
public Bck2Brwsr addRootClasses(String... classes) {
|
jaroslav@874
|
179 |
if (classes.length == 0) {
|
jaroslav@874
|
180 |
return this;
|
jaroslav@1583
|
181 |
}
|
jaroslav@1583
|
182 |
return addExported(classes).addClasses(classes);
|
jaroslav@874
|
183 |
}
|
jaroslav@874
|
184 |
|
jaroslav@1491
|
185 |
/** Adds additional classes
|
jaroslav@1491
|
186 |
* to the list of those that should be included in the generated
|
jaroslav@1491
|
187 |
* JavaScript file. These classes are guaranteed to be present,
|
jaroslav@1491
|
188 |
* but they may not be accessible through their fully qualified
|
jaroslav@1491
|
189 |
* name.
|
jaroslav@1491
|
190 |
*
|
jaroslav@1491
|
191 |
* @param classes the classes to add to the compilation
|
jaroslav@1491
|
192 |
* @return new instance of the Bck2Brwsr compiler which inherits
|
jaroslav@1491
|
193 |
* all values from <code>this</code>
|
jaroslav@1491
|
194 |
* @since 0.9
|
jaroslav@1491
|
195 |
*/
|
jaroslav@1491
|
196 |
public Bck2Brwsr addClasses(String... classes) {
|
jaroslav@1491
|
197 |
if (classes.length == 0) {
|
jaroslav@1491
|
198 |
return this;
|
jaroslav@1491
|
199 |
} else {
|
jaroslav@1583
|
200 |
return new Bck2Brwsr(level, exported,
|
jaroslav@1583
|
201 |
this.classes.addAndNew(classes), resources, res,
|
jaroslav@1812
|
202 |
extension, classpath, flow);
|
jaroslav@1491
|
203 |
}
|
jaroslav@1491
|
204 |
}
|
jaroslav@1491
|
205 |
|
jaroslav@1493
|
206 |
/** These resources should be made available in the compiled file in
|
jaroslav@1493
|
207 |
* binary form. These resources can then be loaded
|
jaroslav@1493
|
208 |
* by {@link ClassLoader#getResource(java.lang.String)} and similar
|
jaroslav@1493
|
209 |
* methods.
|
jaroslav@1493
|
210 |
*
|
jaroslav@1493
|
211 |
* @param resources names of the resources to be loaded by {@link Resources#get(java.lang.String)}
|
jaroslav@1493
|
212 |
* @return new instance of the Bck2Brwsr compiler which inherits
|
jaroslav@1493
|
213 |
* all values from <code>this</code> just adds few more resource names
|
jaroslav@1493
|
214 |
* for processing
|
jaroslav@1493
|
215 |
* @since 0.9
|
jaroslav@1493
|
216 |
*/
|
jaroslav@1493
|
217 |
public Bck2Brwsr addResources(String... resources) {
|
jaroslav@1493
|
218 |
if (resources.length == 0) {
|
jaroslav@1493
|
219 |
return this;
|
jaroslav@1493
|
220 |
} else {
|
jaroslav@1583
|
221 |
return new Bck2Brwsr(level, exported, this.classes,
|
jaroslav@1812
|
222 |
this.resources.addAndNew(resources), res, extension, classpath, flow
|
jaroslav@1493
|
223 |
);
|
jaroslav@874
|
224 |
}
|
jaroslav@874
|
225 |
}
|
jaroslav@874
|
226 |
|
jaroslav@874
|
227 |
/** Changes the obfuscation level for the compiler by creating new instance
|
jaroslav@874
|
228 |
* which inherits all values from <code>this</code> and adjust the level
|
jaroslav@874
|
229 |
* of obfuscation.
|
jaroslav@874
|
230 |
*
|
jaroslav@874
|
231 |
* @param level the new level of obfuscation
|
jaroslav@874
|
232 |
* @return new instance of the compiler with changed level of obfuscation
|
jaroslav@874
|
233 |
* @since 0.5
|
jaroslav@874
|
234 |
*/
|
jaroslav@874
|
235 |
public Bck2Brwsr obfuscation(ObfuscationLevel level) {
|
jaroslav@1812
|
236 |
return new Bck2Brwsr(level, exported, classes, resources, res, extension, classpath, flow);
|
jaroslav@874
|
237 |
}
|
jaroslav@874
|
238 |
|
jaroslav@874
|
239 |
/** A way to change the provider of additional resources (classes) for the
|
jaroslav@874
|
240 |
* compiler.
|
jaroslav@874
|
241 |
*
|
jaroslav@874
|
242 |
* @param res the implementation of resources provider
|
jaroslav@874
|
243 |
* @return new instance of the compiler with all values remaining the same, just
|
jaroslav@874
|
244 |
* with different resources provider
|
jaroslav@874
|
245 |
* @since 0.5
|
jaroslav@874
|
246 |
*/
|
jaroslav@874
|
247 |
public Bck2Brwsr resources(Resources res) {
|
jaroslav@1604
|
248 |
return new Bck2Brwsr(
|
jaroslav@1604
|
249 |
level, exported, classes, resources,
|
jaroslav@1812
|
250 |
res, extension, classpath, flow
|
jaroslav@1604
|
251 |
);
|
lubomir@1029
|
252 |
}
|
lubomir@1029
|
253 |
|
jaroslav@1491
|
254 |
/** Should one generate a library? By default the system generates
|
jaroslav@1491
|
255 |
* all transitive classes needed by the the transitive closure of
|
jaroslav@1491
|
256 |
* {@link #addRootClasses(java.lang.String...)} and {@link #addClasses(java.lang.String...)}.
|
jaroslav@1491
|
257 |
* By turning on the library mode, only classes explicitly listed
|
jaroslav@1491
|
258 |
* will be included in the archive. The others will be referenced
|
jaroslav@1491
|
259 |
* as external ones.
|
jaroslav@1604
|
260 |
* <p>
|
jaroslav@1604
|
261 |
* A library archive may specify its <em>classpath</em> - e.g. link to
|
jaroslav@1604
|
262 |
* other libraries that should also be included in the application.
|
jaroslav@1604
|
263 |
* One can specify the list of libraries as vararg to this method.
|
jaroslav@1604
|
264 |
* These are relative URL with respect to location of this library.
|
jaroslav@1604
|
265 |
* The runtime system then prefers seek for ".js" suffix of the library
|
jaroslav@1604
|
266 |
* and only then seeks for the classical ".jar" path.
|
jaroslav@1491
|
267 |
*
|
jaroslav@1604
|
268 |
* @param classpath the array of JARs that are referenced by this library -
|
jaroslav@1604
|
269 |
* by default gets turned into
|
jaroslav@1491
|
270 |
* @return new instance of the compiler with library flag changed
|
jaroslav@1491
|
271 |
* @since 0.9
|
jaroslav@1491
|
272 |
*/
|
jaroslav@1604
|
273 |
public Bck2Brwsr library(String... classpath) {
|
jaroslav@1604
|
274 |
return new Bck2Brwsr(
|
jaroslav@1604
|
275 |
level, exported, classes,
|
jaroslav@1604
|
276 |
resources, res, true,
|
jaroslav@1812
|
277 |
StringArray.asList(classpath), flow
|
jaroslav@1604
|
278 |
);
|
jaroslav@874
|
279 |
}
|
jaroslav@1584
|
280 |
|
jaroslav@1783
|
281 |
/** Turns on the standalone mode. E.g. does the opposite of
|
jaroslav@1783
|
282 |
* calling {@link #library(java.lang.String...)},
|
jaroslav@1584
|
283 |
* but also allows to specify whether the <em>Bck2Brwsr VM</em> should
|
jaroslav@1584
|
284 |
* be included at all. If not, only the skeleton of the launcher is
|
jaroslav@1584
|
285 |
* generated without any additional VM classes referenced.
|
jaroslav@1584
|
286 |
*
|
jaroslav@1584
|
287 |
* @param includeVM should the VM be compiled in, or left out
|
jaroslav@1584
|
288 |
* @return new instance of the compiler with standalone mode on
|
jaroslav@1584
|
289 |
* @since 0.9
|
jaroslav@1584
|
290 |
*/
|
jaroslav@1584
|
291 |
public Bck2Brwsr standalone(boolean includeVM) {
|
jaroslav@1604
|
292 |
return new Bck2Brwsr(
|
jaroslav@1604
|
293 |
level, exported, classes, resources,
|
jaroslav@1812
|
294 |
res, includeVM ? false : null, null, flow
|
jaroslav@1604
|
295 |
);
|
jaroslav@1584
|
296 |
}
|
jaroslav@874
|
297 |
|
jaroslav@874
|
298 |
/** A way to change the provider of additional resources (classes) for the
|
jaroslav@874
|
299 |
* compiler by specifying classloader to use for loading them.
|
jaroslav@874
|
300 |
*
|
jaroslav@874
|
301 |
* @param loader class loader to load the resources from
|
jaroslav@874
|
302 |
* @return new instance of the compiler with all values being the same, just
|
jaroslav@874
|
303 |
* different resources provider
|
jaroslav@874
|
304 |
* @since 0.5
|
jaroslav@874
|
305 |
*/
|
jaroslav@874
|
306 |
public Bck2Brwsr resources(final ClassLoader loader) {
|
jaroslav@1365
|
307 |
return resources(loader, false);
|
jaroslav@1365
|
308 |
}
|
jaroslav@1365
|
309 |
|
jaroslav@1365
|
310 |
/** A way to change the provider of additional resources (classes) for the
|
jaroslav@1365
|
311 |
* compiler by specifying classloader to use for loading them.
|
jaroslav@1365
|
312 |
*
|
jaroslav@1365
|
313 |
* @param loader class loader to load the resources from
|
jaroslav@1365
|
314 |
* @param ignoreBootClassPath <code>true</code> if classes loaded
|
jaroslav@1365
|
315 |
* from <code>rt.jar</code>
|
jaroslav@1365
|
316 |
* @return new instance of the compiler with all values being the same, just
|
jaroslav@1365
|
317 |
* different resources provider
|
jaroslav@1365
|
318 |
* @since 0.9
|
jaroslav@1365
|
319 |
*/
|
jaroslav@1365
|
320 |
public Bck2Brwsr resources(final ClassLoader loader, boolean ignoreBootClassPath) {
|
jaroslav@1365
|
321 |
return resources(new LdrRsrcs(loader, ignoreBootClassPath));
|
jaroslav@874
|
322 |
}
|
jaroslav@874
|
323 |
|
jaroslav@1812
|
324 |
/** A way to register flow analyzer. Such analyzer can optimize the
|
jaroslav@1812
|
325 |
* representation of cycles inside of method bodies.
|
jaroslav@1812
|
326 |
*
|
jaroslav@1812
|
327 |
* @param flow the analyzer to be consulted with each method body
|
jaroslav@1812
|
328 |
* @return new instance of the compiler with all values being the same, just
|
jaroslav@1812
|
329 |
* different flow analyzer
|
jaroslav@1812
|
330 |
* @since 0.15
|
jaroslav@1812
|
331 |
*/
|
jaroslav@1812
|
332 |
public Bck2Brwsr flowAnalyzer(Flow.Analyzer flow) {
|
jaroslav@1812
|
333 |
return new Bck2Brwsr(
|
jaroslav@1812
|
334 |
level, exported, classes, resources, res,
|
jaroslav@1812
|
335 |
extension, classpath, flow
|
jaroslav@1812
|
336 |
);
|
jaroslav@1812
|
337 |
}
|
jaroslav@1812
|
338 |
|
jaroslav@874
|
339 |
/** Generates virtual machine based on previous configuration of the
|
jaroslav@874
|
340 |
* compiler.
|
lubomir@849
|
341 |
*
|
lubomir@849
|
342 |
* @param out the output to write the generated JavaScript to
|
jaroslav@1783
|
343 |
* @throws IOException I/O exception can be thrown when something goes wrong
|
lubomir@860
|
344 |
* @since 0.5
|
lubomir@849
|
345 |
*/
|
jaroslav@874
|
346 |
public void generate(Appendable out) throws IOException {
|
jaroslav@874
|
347 |
if (level != ObfuscationLevel.NONE) {
|
lubomir@849
|
348 |
try {
|
jaroslav@1493
|
349 |
ClosureWrapper.produceTo(out, level, this);
|
lubomir@849
|
350 |
return;
|
lubomir@849
|
351 |
} catch (IOException ex) {
|
lubomir@849
|
352 |
throw ex;
|
lubomir@849
|
353 |
} catch (Throwable ex) {
|
lubomir@849
|
354 |
out.append("/* Failed to obfuscate: " + ex.getMessage()
|
lubomir@849
|
355 |
+ " */\n");
|
lubomir@849
|
356 |
}
|
jaroslav@750
|
357 |
}
|
lubomir@849
|
358 |
|
jaroslav@1493
|
359 |
VM.compile(out, this);
|
jaroslav@298
|
360 |
}
|
jaroslav@298
|
361 |
|
jaroslav@1493
|
362 |
//
|
jaroslav@1493
|
363 |
// Internal getters
|
jaroslav@1493
|
364 |
//
|
jaroslav@1493
|
365 |
|
jaroslav@1493
|
366 |
Resources getResources() {
|
jaroslav@1513
|
367 |
return res != null ? res : new LdrRsrcs(Bck2Brwsr.class.getClassLoader(), false);
|
jaroslav@1493
|
368 |
}
|
jaroslav@1493
|
369 |
|
jaroslav@1495
|
370 |
StringArray allResources() {
|
jaroslav@1495
|
371 |
return resources;
|
jaroslav@1495
|
372 |
}
|
jaroslav@1495
|
373 |
|
jaroslav@1583
|
374 |
StringArray classes() {
|
jaroslav@1583
|
375 |
return classes;
|
jaroslav@1583
|
376 |
}
|
jaroslav@1583
|
377 |
|
jaroslav@1583
|
378 |
StringArray exported() {
|
jaroslav@1583
|
379 |
return exported;
|
jaroslav@1493
|
380 |
}
|
jaroslav@1493
|
381 |
|
jaroslav@1493
|
382 |
boolean isExtension() {
|
jaroslav@1584
|
383 |
return Boolean.TRUE.equals(extension);
|
jaroslav@1584
|
384 |
}
|
jaroslav@1584
|
385 |
|
jaroslav@1584
|
386 |
boolean includeVM() {
|
jaroslav@1584
|
387 |
return extension != null;
|
jaroslav@874
|
388 |
}
|
jaroslav@1604
|
389 |
|
jaroslav@1604
|
390 |
StringArray classpath() {
|
jaroslav@1604
|
391 |
return classpath;
|
jaroslav@1604
|
392 |
}
|
jaroslav@1812
|
393 |
|
jaroslav@1812
|
394 |
Flow.Analyzer flow() {
|
jaroslav@1812
|
395 |
return flow;
|
jaroslav@1812
|
396 |
}
|
lubomir@1086
|
397 |
|
jaroslav@298
|
398 |
/** Provider of resources (classes and other files). The
|
jaroslav@298
|
399 |
* {@link #generate(java.lang.Appendable, org.apidesign.vm4brwsr.Bck2Brwsr.Resources, java.lang.String[])
|
jaroslav@298
|
400 |
* generator method} will call back here for all classes needed during
|
jaroslav@298
|
401 |
* translation to JavaScript.
|
jaroslav@298
|
402 |
*/
|
jaroslav@298
|
403 |
public interface Resources {
|
jaroslav@298
|
404 |
/** Loads given resource (class or other file like image). The
|
jaroslav@298
|
405 |
* resource name to load bytes for the {@link String} class
|
jaroslav@298
|
406 |
* would be <code>"java/lang/String.class"</code>.
|
jaroslav@298
|
407 |
*
|
jaroslav@298
|
408 |
* @param resource path to resource to load
|
jaroslav@298
|
409 |
* @return the input stream for the resource
|
jaroslav@298
|
410 |
* @throws IOException can be thrown if the loading fails on some error
|
jaroslav@298
|
411 |
* or the file cannot be found
|
jaroslav@298
|
412 |
*/
|
jaroslav@298
|
413 |
public InputStream get(String resource) throws IOException;
|
jaroslav@298
|
414 |
}
|
jaroslav@1812
|
415 |
|
jaroslav@1812
|
416 |
/** Represents control flow inside single method.
|
jaroslav@1812
|
417 |
* Passed into {@link Analyzer#analyze(byte[], org.apidesign.vm4brwsr.Bck2Brwsr.Flow)}
|
jaroslav@1812
|
418 |
* method that can be registed via {@link Bck2Brwsr#flowAnalyzer(org.apidesign.vm4brwsr.Bck2Brwsr.Flow.Analyzer)}
|
jaroslav@1812
|
419 |
* method.
|
jaroslav@1812
|
420 |
*
|
jaroslav@1812
|
421 |
* @since 0.15
|
jaroslav@1812
|
422 |
*/
|
jaroslav@1812
|
423 |
public static final class Flow {
|
jaroslav@1813
|
424 |
private final MethodData m;
|
jaroslav@1842
|
425 |
private final Appendable emit;
|
jaroslav@1842
|
426 |
Flow(MethodData m, Appendable emit) {
|
jaroslav@1813
|
427 |
this.m = m;
|
jaroslav@1842
|
428 |
this.emit = emit;
|
jaroslav@1812
|
429 |
}
|
jaroslav@1812
|
430 |
|
jaroslav@1812
|
431 |
/** Access to bytecode of the method to analyse.
|
jaroslav@1812
|
432 |
*
|
jaroslav@1812
|
433 |
* @return unmodifiable bytecode of the instructions in the method body
|
jaroslav@1812
|
434 |
*/
|
jaroslav@1812
|
435 |
public byte[] getMethodByteCode() {
|
jaroslav@1813
|
436 |
return m.getCode();
|
jaroslav@1813
|
437 |
}
|
jaroslav@1813
|
438 |
|
jaroslav@1813
|
439 |
/** Name of the method that is being processed.
|
jaroslav@1813
|
440 |
* @return simple method name
|
jaroslav@1813
|
441 |
*/
|
jaroslav@1813
|
442 |
public String getMethodName() {
|
jaroslav@1813
|
443 |
return m.getName();
|
jaroslav@1812
|
444 |
}
|
jaroslav@1816
|
445 |
|
jaroslav@1842
|
446 |
/** Emits JavaScript to the final JavaScript output.
|
jaroslav@1842
|
447 |
* @param seq text to emit
|
jaroslav@1842
|
448 |
*/
|
jaroslav@1842
|
449 |
public void emit(CharSequence seq) throws IOException {
|
jaroslav@1842
|
450 |
this.emit.append(seq);
|
jaroslav@1812
|
451 |
}
|
jaroslav@1812
|
452 |
|
jaroslav@1812
|
453 |
/** Provider of advanced analysis of the code flow inside of
|
jaroslav@1812
|
454 |
* method bodies. Register via {@link Bck2Brwsr#flowAnalyzer(org.apidesign.vm4brwsr.Bck2Brwsr.Flow.Analyzer)}
|
jaroslav@1812
|
455 |
* when constructing the {@link Bck2Brwsr#newCompiler() compiler}.
|
jaroslav@1812
|
456 |
*
|
jaroslav@1812
|
457 |
* @since 0.15
|
jaroslav@1812
|
458 |
*/
|
jaroslav@1812
|
459 |
public interface Analyzer {
|
jaroslav@1812
|
460 |
/** Called to analyze method bodies and offer better control flow.
|
jaroslav@1812
|
461 |
*
|
jaroslav@1812
|
462 |
*
|
jaroslav@1812
|
463 |
* @param request flow computation request and also a
|
jaroslav@1812
|
464 |
* callback interface with methods to define the flow
|
jaroslav@1812
|
465 |
* @return <code>true</code> if the analysis was successful,
|
jaroslav@1812
|
466 |
* <code>false</code> otherwise
|
jaroslav@1812
|
467 |
*/
|
jaroslav@1842
|
468 |
public boolean analyze(Flow request) throws IOException;
|
jaroslav@1812
|
469 |
}
|
jaroslav@1812
|
470 |
}
|
jaroslav@1812
|
471 |
|
jaroslav@298
|
472 |
}
|