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