jaroslav@708
|
1 |
/**
|
jaroslav@708
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@708
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@708
|
4 |
*
|
jaroslav@708
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@708
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@708
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@708
|
8 |
*
|
jaroslav@708
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@708
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@708
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@708
|
12 |
* GNU General Public License for more details.
|
jaroslav@708
|
13 |
*
|
jaroslav@708
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@708
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@708
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@708
|
17 |
*/
|
jaroslav@708
|
18 |
package org.apidesign.vm4brwsr;
|
jaroslav@708
|
19 |
|
jaroslav@1497
|
20 |
import java.io.ByteArrayInputStream;
|
jaroslav@708
|
21 |
import java.io.File;
|
jaroslav@708
|
22 |
import java.io.FileWriter;
|
jaroslav@708
|
23 |
import java.io.IOException;
|
jaroslav@708
|
24 |
import java.io.InputStream;
|
jaroslav@708
|
25 |
import java.net.URL;
|
jaroslav@1609
|
26 |
import java.util.ArrayList;
|
jaroslav@1609
|
27 |
import java.util.Arrays;
|
jaroslav@708
|
28 |
import java.util.Enumeration;
|
jaroslav@1611
|
29 |
import java.util.HashSet;
|
jaroslav@1609
|
30 |
import java.util.List;
|
jaroslav@1611
|
31 |
import java.util.Set;
|
jaroslav@708
|
32 |
import javax.script.Invocable;
|
jaroslav@1367
|
33 |
import javax.script.ScriptContext;
|
jaroslav@708
|
34 |
import javax.script.ScriptEngine;
|
jaroslav@708
|
35 |
import javax.script.ScriptEngineManager;
|
jaroslav@708
|
36 |
import javax.script.ScriptException;
|
jaroslav@708
|
37 |
import static org.testng.Assert.*;
|
jaroslav@708
|
38 |
|
jaroslav@1367
|
39 |
public final class TestVM {
|
jaroslav@708
|
40 |
private final Invocable code;
|
jaroslav@708
|
41 |
private final CharSequence codeSeq;
|
jaroslav@708
|
42 |
private final Object bck2brwsr;
|
jaroslav@1367
|
43 |
private BytesLoader resources;
|
jaroslav@708
|
44 |
|
jaroslav@708
|
45 |
|
jaroslav@708
|
46 |
private TestVM(Invocable code, CharSequence codeSeq) throws ScriptException, NoSuchMethodException {
|
jaroslav@708
|
47 |
this.code = code;
|
jaroslav@708
|
48 |
this.codeSeq = codeSeq;
|
jaroslav@1367
|
49 |
this.bck2brwsr = ((ScriptEngine)code).eval("bck2brwsr(function(n) { return loader.get(n); })");
|
jaroslav@1367
|
50 |
((ScriptEngine)code).getContext().setAttribute("loader", this, ScriptContext.ENGINE_SCOPE);
|
jaroslav@708
|
51 |
}
|
jaroslav@708
|
52 |
|
jaroslav@1367
|
53 |
public void register(BytesLoader res) {
|
jaroslav@1367
|
54 |
this.resources = res;
|
jaroslav@1367
|
55 |
}
|
jaroslav@1367
|
56 |
|
jaroslav@1367
|
57 |
public byte[] get(String res) throws IOException {
|
jaroslav@1367
|
58 |
return resources != null ? resources.get(res) : null;
|
jaroslav@1367
|
59 |
}
|
jaroslav@708
|
60 |
|
jaroslav@708
|
61 |
public Object execCode(
|
jaroslav@708
|
62 |
String msg, Class<?> clazz, String method,
|
jaroslav@708
|
63 |
Object expRes, Object... args
|
jaroslav@708
|
64 |
) throws Exception {
|
jaroslav@708
|
65 |
Object ret = null;
|
jaroslav@708
|
66 |
try {
|
jaroslav@708
|
67 |
ret = code.invokeMethod(bck2brwsr, "loadClass", clazz.getName());
|
jaroslav@1609
|
68 |
List<Object> ma = new ArrayList<>();
|
jaroslav@1609
|
69 |
ma.add(method);
|
jaroslav@1609
|
70 |
ma.addAll(Arrays.asList(args));
|
jaroslav@1609
|
71 |
ret = code.invokeMethod(ret, "invoke", ma.toArray());
|
jaroslav@708
|
72 |
} catch (ScriptException ex) {
|
jaroslav@782
|
73 |
fail("Execution failed in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
|
jaroslav@708
|
74 |
} catch (NoSuchMethodException ex) {
|
jaroslav@708
|
75 |
fail("Cannot find method in " + dumpJS(codeSeq), ex);
|
jaroslav@708
|
76 |
}
|
jaroslav@708
|
77 |
if (ret == null && expRes == null) {
|
jaroslav@708
|
78 |
return null;
|
jaroslav@708
|
79 |
}
|
jaroslav@747
|
80 |
if (expRes != null && expRes.equals(ret)) {
|
jaroslav@708
|
81 |
return null;
|
jaroslav@708
|
82 |
}
|
jaroslav@708
|
83 |
if (expRes instanceof Number) {
|
jaroslav@708
|
84 |
// in case of Long it is necessary convert it to number
|
jaroslav@708
|
85 |
// since the Long is represented by two numbers in JavaScript
|
jaroslav@708
|
86 |
try {
|
jaroslav@1392
|
87 |
final Object toFP = ((ScriptEngine)code).eval("Number.prototype.toFP");
|
jaroslav@1392
|
88 |
if (ret instanceof Long) {
|
jaroslav@1392
|
89 |
ret = code.invokeMethod(toFP, "call", ret);
|
jaroslav@1392
|
90 |
}
|
jaroslav@708
|
91 |
ret = code.invokeFunction("Number", ret);
|
jaroslav@708
|
92 |
} catch (ScriptException ex) {
|
jaroslav@784
|
93 |
fail("Conversion to number failed in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
|
jaroslav@708
|
94 |
} catch (NoSuchMethodException ex) {
|
jaroslav@784
|
95 |
fail("Cannot find global Number(x) function in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
|
jaroslav@708
|
96 |
}
|
jaroslav@708
|
97 |
}
|
jaroslav@708
|
98 |
return ret;
|
jaroslav@708
|
99 |
}
|
jaroslav@708
|
100 |
|
jaroslav@708
|
101 |
void assertExec(
|
jaroslav@708
|
102 |
String msg, Class clazz, String method, Object expRes, Object... args
|
jaroslav@708
|
103 |
) throws Exception {
|
jaroslav@708
|
104 |
Object ret = execCode(msg, clazz, method, expRes, args);
|
jaroslav@708
|
105 |
if (ret == null) {
|
jaroslav@708
|
106 |
return;
|
jaroslav@708
|
107 |
}
|
jaroslav@1392
|
108 |
if (expRes instanceof Integer && ret instanceof Double) {
|
jaroslav@1392
|
109 |
expRes = ((Integer)expRes).doubleValue();
|
jaroslav@1392
|
110 |
}
|
jaroslav@708
|
111 |
if (expRes != null && expRes.equals(ret)) {
|
jaroslav@708
|
112 |
return;
|
jaroslav@708
|
113 |
}
|
jaroslav@708
|
114 |
assertEquals(ret, expRes, msg + "was: " + ret + "\n" + dumpJS(codeSeq));
|
jaroslav@708
|
115 |
}
|
jaroslav@708
|
116 |
|
jaroslav@708
|
117 |
static TestVM compileClass(String... names) throws ScriptException, IOException {
|
jaroslav@708
|
118 |
return compileClass(null, names);
|
jaroslav@708
|
119 |
}
|
jaroslav@708
|
120 |
|
jaroslav@708
|
121 |
static TestVM compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
|
jaroslav@708
|
122 |
return compileClass(sb, null, names);
|
jaroslav@708
|
123 |
}
|
jaroslav@708
|
124 |
|
jaroslav@708
|
125 |
static TestVM compileClass(StringBuilder sb, ScriptEngine[] eng, String... names) throws ScriptException, IOException {
|
jaroslav@1645
|
126 |
return compileClass(sb, eng, new EmulationResources(), names);
|
jaroslav@1645
|
127 |
}
|
jaroslav@1645
|
128 |
static TestVM compileClass(
|
jaroslav@1645
|
129 |
StringBuilder sb,
|
jaroslav@1645
|
130 |
ScriptEngine[] eng,
|
jaroslav@1645
|
131 |
Bck2Brwsr.Resources resources,
|
jaroslav@1645
|
132 |
String... names
|
jaroslav@1645
|
133 |
) throws ScriptException, IOException {
|
jaroslav@708
|
134 |
if (sb == null) {
|
jaroslav@708
|
135 |
sb = new StringBuilder();
|
jaroslav@708
|
136 |
}
|
jaroslav@1645
|
137 |
Bck2Brwsr.generate(sb, resources, names);
|
jaroslav@708
|
138 |
ScriptEngineManager sem = new ScriptEngineManager();
|
jaroslav@708
|
139 |
ScriptEngine js = sem.getEngineByExtension("js");
|
jaroslav@708
|
140 |
if (eng != null) {
|
jaroslav@708
|
141 |
eng[0] = js;
|
jaroslav@708
|
142 |
}
|
jaroslav@708
|
143 |
try {
|
jaroslav@708
|
144 |
Object res = js.eval(sb.toString());
|
jaroslav@708
|
145 |
assertTrue(js instanceof Invocable, "It is invocable object: " + res);
|
jaroslav@708
|
146 |
return new TestVM((Invocable) js, sb);
|
jaroslav@708
|
147 |
} catch (Exception ex) {
|
jaroslav@708
|
148 |
if (sb.length() > 2000) {
|
jaroslav@708
|
149 |
sb = dumpJS(sb);
|
jaroslav@708
|
150 |
}
|
jaroslav@840
|
151 |
fail("Could not evaluate:" + ex.getClass() + ":" + ex.getMessage() + "\n" + sb, ex);
|
jaroslav@708
|
152 |
return null;
|
jaroslav@708
|
153 |
}
|
jaroslav@708
|
154 |
}
|
jaroslav@1487
|
155 |
|
jaroslav@1497
|
156 |
static TestVM compileClassAsExtension(
|
jaroslav@1497
|
157 |
StringBuilder sb, ScriptEngine[] eng,
|
jaroslav@1497
|
158 |
String name, final String resourceName, final String resourceContent
|
jaroslav@1497
|
159 |
) throws ScriptException, IOException {
|
jaroslav@1610
|
160 |
return compileClassesAsExtension(sb, eng, resourceName, resourceContent, name);
|
jaroslav@1610
|
161 |
}
|
jaroslav@1610
|
162 |
static TestVM compileClassesAsExtension(
|
jaroslav@1610
|
163 |
StringBuilder sb, ScriptEngine[] eng,
|
jaroslav@1610
|
164 |
final String resourceName, final String resourceContent, String... names
|
jaroslav@1610
|
165 |
) throws ScriptException, IOException {
|
jaroslav@1487
|
166 |
if (sb == null) {
|
jaroslav@1487
|
167 |
sb = new StringBuilder();
|
jaroslav@1487
|
168 |
}
|
jaroslav@1497
|
169 |
if (eng[0] == null) {
|
jaroslav@1497
|
170 |
ScriptEngineManager sem = new ScriptEngineManager();
|
jaroslav@1497
|
171 |
ScriptEngine js = sem.getEngineByExtension("js");
|
jaroslav@1497
|
172 |
eng[0] = js;
|
jaroslav@1707
|
173 |
Bck2Brwsr.newCompiler().resources(new EmulationResources())
|
jaroslav@1722
|
174 |
.obfuscation(ObfuscationLevel.NONE).generate(sb);
|
jaroslav@1497
|
175 |
}
|
jaroslav@1611
|
176 |
Set<String> exp = new HashSet<String>();
|
jaroslav@1611
|
177 |
for (String n : names) {
|
jaroslav@1611
|
178 |
int last = n.lastIndexOf('/');
|
jaroslav@1611
|
179 |
exp.add(n.substring(0, last + 1));
|
jaroslav@1611
|
180 |
}
|
jaroslav@1487
|
181 |
Bck2Brwsr b2b = Bck2Brwsr.newCompiler().
|
jaroslav@1497
|
182 |
resources(new EmulationResources() {
|
jaroslav@1497
|
183 |
@Override
|
jaroslav@1497
|
184 |
public InputStream get(String name) throws IOException {
|
jaroslav@1497
|
185 |
if (name.equals(resourceName)) {
|
jaroslav@1497
|
186 |
return new ByteArrayInputStream(resourceContent.getBytes("UTF-8"));
|
jaroslav@1497
|
187 |
}
|
jaroslav@1497
|
188 |
return super.get(name);
|
jaroslav@1497
|
189 |
}
|
jaroslav@1497
|
190 |
}).
|
jaroslav@1610
|
191 |
addClasses(names).
|
jaroslav@1587
|
192 |
addResources("org/apidesign/vm4brwsr/obj.js").
|
jaroslav@1611
|
193 |
addExported(exp.toArray(new String[0])).
|
jaroslav@1558
|
194 |
obfuscation(ObfuscationLevel.FULL).
|
jaroslav@1604
|
195 |
library();
|
jaroslav@1497
|
196 |
if (resourceName != null) {
|
jaroslav@1497
|
197 |
b2b = b2b.addResources(resourceName);
|
jaroslav@1497
|
198 |
}
|
jaroslav@1487
|
199 |
b2b.generate(sb);
|
jaroslav@1487
|
200 |
try {
|
jaroslav@1497
|
201 |
defineAtoB(eng[0]);
|
jaroslav@1497
|
202 |
Object res = eng[0].eval(sb.toString());
|
jaroslav@1497
|
203 |
assertTrue(eng[0] instanceof Invocable, "It is invocable object: " + res);
|
jaroslav@1497
|
204 |
return new TestVM((Invocable) eng[0], sb);
|
jaroslav@1487
|
205 |
} catch (Exception ex) {
|
jaroslav@1487
|
206 |
if (sb.length() > 2000) {
|
jaroslav@1487
|
207 |
sb = dumpJS(sb);
|
jaroslav@1487
|
208 |
}
|
jaroslav@1487
|
209 |
fail("Could not evaluate:" + ex.getClass() + ":" + ex.getMessage() + "\n" + sb, ex);
|
jaroslav@1487
|
210 |
return null;
|
jaroslav@1487
|
211 |
}
|
jaroslav@1487
|
212 |
}
|
jaroslav@1493
|
213 |
|
jaroslav@1493
|
214 |
static TestVM compileClassAndResources(StringBuilder sb, ScriptEngine[] eng, String name, String... resources) throws ScriptException, IOException {
|
jaroslav@1493
|
215 |
if (sb == null) {
|
jaroslav@1493
|
216 |
sb = new StringBuilder();
|
jaroslav@1493
|
217 |
}
|
jaroslav@1493
|
218 |
Bck2Brwsr b2b = Bck2Brwsr.newCompiler().
|
jaroslav@1493
|
219 |
resources(new EmulationResources()).
|
jaroslav@1493
|
220 |
addRootClasses(name).
|
jaroslav@1604
|
221 |
addResources(resources);
|
jaroslav@1493
|
222 |
b2b.generate(sb);
|
jaroslav@1493
|
223 |
ScriptEngineManager sem = new ScriptEngineManager();
|
jaroslav@1493
|
224 |
ScriptEngine js = sem.getEngineByExtension("js");
|
jaroslav@1493
|
225 |
if (eng != null) {
|
jaroslav@1493
|
226 |
eng[0] = js;
|
jaroslav@1493
|
227 |
}
|
jaroslav@1493
|
228 |
try {
|
jaroslav@1497
|
229 |
defineAtoB(js);
|
jaroslav@1495
|
230 |
|
jaroslav@1493
|
231 |
Object res = js.eval(sb.toString());
|
jaroslav@1493
|
232 |
assertTrue(js instanceof Invocable, "It is invocable object: " + res);
|
jaroslav@1493
|
233 |
return new TestVM((Invocable) js, sb);
|
jaroslav@1493
|
234 |
} catch (Exception ex) {
|
jaroslav@1493
|
235 |
if (sb.length() > 2000) {
|
jaroslav@1493
|
236 |
sb = dumpJS(sb);
|
jaroslav@1493
|
237 |
}
|
jaroslav@1493
|
238 |
fail("Could not evaluate:" + ex.getClass() + ":" + ex.getMessage() + "\n" + sb, ex);
|
jaroslav@1493
|
239 |
return null;
|
jaroslav@1493
|
240 |
}
|
jaroslav@1493
|
241 |
}
|
jaroslav@840
|
242 |
|
jaroslav@1497
|
243 |
private static void defineAtoB(ScriptEngine js) throws ScriptException {
|
jaroslav@1538
|
244 |
js.eval("atob = function(s) { return new String(org.apidesign.vm4brwsr.ResourcesTest.parseBase64Binary(s)); }");
|
jaroslav@1497
|
245 |
}
|
jaroslav@708
|
246 |
|
jaroslav@708
|
247 |
Object loadClass(String loadClass, String name) throws ScriptException, NoSuchMethodException {
|
jaroslav@708
|
248 |
return code.invokeMethod(bck2brwsr, "loadClass", Exceptions.class.getName());
|
jaroslav@708
|
249 |
}
|
jaroslav@708
|
250 |
|
jaroslav@708
|
251 |
Object invokeMethod(Object obj, String method, Object... params) throws ScriptException, NoSuchMethodException {
|
jaroslav@708
|
252 |
return code.invokeMethod(obj, method, params);
|
jaroslav@708
|
253 |
}
|
jaroslav@708
|
254 |
|
jaroslav@708
|
255 |
Object invokeFunction(String methodName, Object... args) throws ScriptException, NoSuchMethodException {
|
jaroslav@708
|
256 |
return code.invokeFunction(methodName, args);
|
jaroslav@708
|
257 |
}
|
jaroslav@708
|
258 |
|
jaroslav@708
|
259 |
static StringBuilder dumpJS(CharSequence sb) throws IOException {
|
jaroslav@708
|
260 |
File f = File.createTempFile("execution", ".js");
|
jaroslav@708
|
261 |
FileWriter w = new FileWriter(f);
|
jaroslav@708
|
262 |
w.append(sb);
|
jaroslav@708
|
263 |
w.close();
|
jaroslav@708
|
264 |
return new StringBuilder(f.getPath());
|
jaroslav@708
|
265 |
}
|
jaroslav@708
|
266 |
|
jaroslav@708
|
267 |
@Override
|
jaroslav@708
|
268 |
public String toString() {
|
jaroslav@708
|
269 |
try {
|
jaroslav@708
|
270 |
return dumpJS(codeSeq).toString();
|
jaroslav@708
|
271 |
} catch (IOException ex) {
|
jaroslav@708
|
272 |
return ex.toString();
|
jaroslav@708
|
273 |
}
|
jaroslav@708
|
274 |
}
|
jaroslav@1453
|
275 |
|
jaroslav@1453
|
276 |
final CharSequence codeSeq() {
|
jaroslav@1453
|
277 |
return codeSeq;
|
jaroslav@1453
|
278 |
}
|
jaroslav@708
|
279 |
|
jaroslav@708
|
280 |
private static class EmulationResources implements Bck2Brwsr.Resources {
|
jaroslav@708
|
281 |
@Override
|
jaroslav@708
|
282 |
public InputStream get(String name) throws IOException {
|
jaroslav@1344
|
283 |
if ("java/net/URI.class".equals(name)) {
|
jaroslav@1344
|
284 |
// skip
|
jaroslav@1344
|
285 |
return null;
|
jaroslav@1344
|
286 |
}
|
jaroslav@1404
|
287 |
if ("java/net/URLConnection.class".equals(name)) {
|
jaroslav@1404
|
288 |
// skip
|
jaroslav@1404
|
289 |
return null;
|
jaroslav@1404
|
290 |
}
|
jaroslav@1344
|
291 |
if ("java/lang/System.class".equals(name)) {
|
jaroslav@1344
|
292 |
// skip
|
jaroslav@1344
|
293 |
return null;
|
jaroslav@1344
|
294 |
}
|
jaroslav@708
|
295 |
Enumeration<URL> en = StaticMethodTest.class.getClassLoader().getResources(name);
|
jaroslav@708
|
296 |
URL u = null;
|
jaroslav@708
|
297 |
while (en.hasMoreElements()) {
|
jaroslav@708
|
298 |
u = en.nextElement();
|
jaroslav@708
|
299 |
}
|
jaroslav@708
|
300 |
if (u == null) {
|
jaroslav@708
|
301 |
throw new IOException("Can't find " + name);
|
jaroslav@708
|
302 |
}
|
jaroslav@708
|
303 |
if (u.toExternalForm().contains("rt.jar!")) {
|
jaroslav@708
|
304 |
throw new IOException("No emulation for " + u);
|
jaroslav@708
|
305 |
}
|
jaroslav@708
|
306 |
return u.openStream();
|
jaroslav@708
|
307 |
}
|
jaroslav@708
|
308 |
}
|
jaroslav@708
|
309 |
}
|