Don't initialize @JavaScriptBody resources sooner than their methods are called.
2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.vm4brwsr;
20 import java.io.ByteArrayInputStream;
22 import java.io.FileWriter;
23 import java.io.IOException;
24 import java.io.InputStream;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.Enumeration;
29 import java.util.HashSet;
30 import java.util.List;
32 import javax.script.Invocable;
33 import javax.script.ScriptContext;
34 import javax.script.ScriptEngine;
35 import javax.script.ScriptEngineManager;
36 import javax.script.ScriptException;
37 import static org.testng.Assert.*;
39 public final class TestVM {
40 private final Invocable code;
41 private final CharSequence codeSeq;
42 private final Object bck2brwsr;
43 private BytesLoader resources;
46 private TestVM(Invocable code, CharSequence codeSeq) throws ScriptException, NoSuchMethodException {
48 this.codeSeq = codeSeq;
49 this.bck2brwsr = ((ScriptEngine)code).eval("bck2brwsr(function(n) { return loader.get(n); })");
50 ((ScriptEngine)code).getContext().setAttribute("loader", this, ScriptContext.ENGINE_SCOPE);
53 public void register(BytesLoader res) {
57 public byte[] get(String res) throws IOException {
58 return resources != null ? resources.get(res) : null;
61 public Object execCode(
62 String msg, Class<?> clazz, String method,
63 Object expRes, Object... args
67 ret = code.invokeMethod(bck2brwsr, "loadClass", clazz.getName());
68 List<Object> ma = new ArrayList<>();
70 ma.addAll(Arrays.asList(args));
71 ret = code.invokeMethod(ret, "invoke", ma.toArray());
72 } catch (ScriptException ex) {
73 fail("Execution failed in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
74 } catch (NoSuchMethodException ex) {
75 fail("Cannot find method in " + dumpJS(codeSeq), ex);
77 if (ret == null && expRes == null) {
80 if (expRes != null && expRes.equals(ret)) {
83 if (expRes instanceof Number) {
84 // in case of Long it is necessary convert it to number
85 // since the Long is represented by two numbers in JavaScript
87 final Object toFP = ((ScriptEngine)code).eval("Number.prototype.toFP");
88 if (ret instanceof Long) {
89 ret = code.invokeMethod(toFP, "call", ret);
91 ret = code.invokeFunction("Number", ret);
92 } catch (ScriptException ex) {
93 fail("Conversion to number failed in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
94 } catch (NoSuchMethodException ex) {
95 fail("Cannot find global Number(x) function in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
102 String msg, Class clazz, String method, Object expRes, Object... args
104 Object ret = execCode(msg, clazz, method, expRes, args);
108 if (expRes instanceof Integer && ret instanceof Double) {
109 expRes = ((Integer)expRes).doubleValue();
111 if (expRes != null && expRes.equals(ret)) {
114 assertEquals(ret, expRes, msg + "was: " + ret + "\n" + dumpJS(codeSeq));
117 static TestVM compileClass(String... names) throws ScriptException, IOException {
118 return compileClass(null, names);
121 static TestVM compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
122 return compileClass(sb, null, names);
125 static TestVM compileClass(StringBuilder sb, ScriptEngine[] eng, String... names) throws ScriptException, IOException {
126 return compileClass(sb, eng, new EmulationResources(), names);
128 static TestVM compileClass(
131 Bck2Brwsr.Resources resources,
133 ) throws ScriptException, IOException {
135 sb = new StringBuilder();
137 Bck2Brwsr.generate(sb, resources, names);
138 ScriptEngineManager sem = new ScriptEngineManager();
139 ScriptEngine js = sem.getEngineByExtension("js");
144 Object res = js.eval(sb.toString());
145 assertTrue(js instanceof Invocable, "It is invocable object: " + res);
146 return new TestVM((Invocable) js, sb);
147 } catch (Exception ex) {
148 if (sb.length() > 2000) {
151 fail("Could not evaluate:" + ex.getClass() + ":" + ex.getMessage() + "\n" + sb, ex);
156 static TestVM compileClassAsExtension(
157 StringBuilder sb, ScriptEngine[] eng,
158 String name, final String resourceName, final String resourceContent
159 ) throws ScriptException, IOException {
160 return compileClassesAsExtension(sb, eng, resourceName, resourceContent, name);
162 static TestVM compileClassesAsExtension(
163 StringBuilder sb, ScriptEngine[] eng,
164 final String resourceName, final String resourceContent, String... names
165 ) throws ScriptException, IOException {
167 sb = new StringBuilder();
169 if (eng[0] == null) {
170 ScriptEngineManager sem = new ScriptEngineManager();
171 ScriptEngine js = sem.getEngineByExtension("js");
173 Bck2Brwsr.newCompiler().resources(new EmulationResources())
174 .obfuscation(ObfuscationLevel.NONE).generate(sb);
176 Set<String> exp = new HashSet<String>();
177 for (String n : names) {
178 int last = n.lastIndexOf('/');
179 exp.add(n.substring(0, last + 1));
181 Bck2Brwsr b2b = Bck2Brwsr.newCompiler().
182 resources(new EmulationResources() {
184 public InputStream get(String name) throws IOException {
185 if (name.equals(resourceName)) {
186 return new ByteArrayInputStream(resourceContent.getBytes("UTF-8"));
188 return super.get(name);
192 addResources("org/apidesign/vm4brwsr/obj.js").
193 addExported(exp.toArray(new String[0])).
194 obfuscation(ObfuscationLevel.FULL).
196 if (resourceName != null) {
197 b2b = b2b.addResources(resourceName);
202 Object res = eng[0].eval(sb.toString());
203 assertTrue(eng[0] instanceof Invocable, "It is invocable object: " + res);
204 return new TestVM((Invocable) eng[0], sb);
205 } catch (Exception ex) {
206 if (sb.length() > 2000) {
209 fail("Could not evaluate:" + ex.getClass() + ":" + ex.getMessage() + "\n" + sb, ex);
214 static TestVM compileClassAndResources(StringBuilder sb, ScriptEngine[] eng, String name, String... resources) throws ScriptException, IOException {
216 sb = new StringBuilder();
218 Bck2Brwsr b2b = Bck2Brwsr.newCompiler().
219 resources(new EmulationResources()).
220 addRootClasses(name).
221 addResources(resources);
223 ScriptEngineManager sem = new ScriptEngineManager();
224 ScriptEngine js = sem.getEngineByExtension("js");
231 Object res = js.eval(sb.toString());
232 assertTrue(js instanceof Invocable, "It is invocable object: " + res);
233 return new TestVM((Invocable) js, sb);
234 } catch (Exception ex) {
235 if (sb.length() > 2000) {
238 fail("Could not evaluate:" + ex.getClass() + ":" + ex.getMessage() + "\n" + sb, ex);
243 private static void defineAtoB(ScriptEngine js) throws ScriptException {
244 js.eval("atob = function(s) { return new String(org.apidesign.vm4brwsr.ResourcesTest.parseBase64Binary(s)); }");
247 Object loadClass(String loadClass, String name) throws ScriptException, NoSuchMethodException {
248 return code.invokeMethod(bck2brwsr, "loadClass", Exceptions.class.getName());
251 Object invokeMethod(Object obj, String method, Object... params) throws ScriptException, NoSuchMethodException {
252 return code.invokeMethod(obj, method, params);
255 Object invokeFunction(String methodName, Object... args) throws ScriptException, NoSuchMethodException {
256 return code.invokeFunction(methodName, args);
259 static StringBuilder dumpJS(CharSequence sb) throws IOException {
260 File f = File.createTempFile("execution", ".js");
261 FileWriter w = new FileWriter(f);
264 return new StringBuilder(f.getPath());
268 public String toString() {
270 return dumpJS(codeSeq).toString();
271 } catch (IOException ex) {
272 return ex.toString();
276 final CharSequence codeSeq() {
280 private static class EmulationResources implements Bck2Brwsr.Resources {
282 public InputStream get(String name) throws IOException {
283 if ("java/net/URI.class".equals(name)) {
287 if ("java/net/URLConnection.class".equals(name)) {
291 if ("java/lang/System.class".equals(name)) {
295 Enumeration<URL> en = StaticMethodTest.class.getClassLoader().getResources(name);
297 while (en.hasMoreElements()) {
298 u = en.nextElement();
301 throw new IOException("Can't find " + name);
303 if (u.toExternalForm().contains("rt.jar!")) {
304 throw new IOException("No emulation for " + u);
306 return u.openStream();