If a class is not available during Ahead-Of-Time compilation it needs to be ready for dynamic loading
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;
21 import java.io.FileWriter;
22 import java.io.IOException;
23 import java.io.InputStream;
25 import java.util.Enumeration;
26 import javax.script.Invocable;
27 import javax.script.ScriptContext;
28 import javax.script.ScriptEngine;
29 import javax.script.ScriptEngineManager;
30 import javax.script.ScriptException;
31 import static org.testng.Assert.*;
33 public final class TestVM {
34 private final Invocable code;
35 private final CharSequence codeSeq;
36 private final Object bck2brwsr;
37 private BytesLoader resources;
40 private TestVM(Invocable code, CharSequence codeSeq) throws ScriptException, NoSuchMethodException {
42 this.codeSeq = codeSeq;
43 this.bck2brwsr = ((ScriptEngine)code).eval("bck2brwsr(function(n) { return loader.get(n); })");
44 ((ScriptEngine)code).getContext().setAttribute("loader", this, ScriptContext.ENGINE_SCOPE);
47 public void register(BytesLoader res) {
51 public byte[] get(String res) throws IOException {
52 return resources != null ? resources.get(res) : null;
55 public Object execCode(
56 String msg, Class<?> clazz, String method,
57 Object expRes, Object... args
61 ret = code.invokeMethod(bck2brwsr, "loadClass", clazz.getName());
62 ret = code.invokeMethod(ret, method, args);
63 } catch (ScriptException ex) {
64 fail("Execution failed in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
65 } catch (NoSuchMethodException ex) {
66 fail("Cannot find method in " + dumpJS(codeSeq), ex);
68 if (ret == null && expRes == null) {
71 if (expRes != null && expRes.equals(ret)) {
74 if (expRes instanceof Number) {
75 // in case of Long it is necessary convert it to number
76 // since the Long is represented by two numbers in JavaScript
78 ret = code.invokeMethod(ret, "toFP");
79 ret = code.invokeFunction("Number", ret);
80 } catch (ScriptException ex) {
81 fail("Conversion to number failed in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
82 } catch (NoSuchMethodException ex) {
83 fail("Cannot find global Number(x) function in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
90 String msg, Class clazz, String method, Object expRes, Object... args
92 Object ret = execCode(msg, clazz, method, expRes, args);
96 if (expRes != null && expRes.equals(ret)) {
99 assertEquals(ret, expRes, msg + "was: " + ret + "\n" + dumpJS(codeSeq));
102 static TestVM compileClass(String... names) throws ScriptException, IOException {
103 return compileClass(null, names);
106 static TestVM compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
107 return compileClass(sb, null, names);
110 static TestVM compileClass(StringBuilder sb, ScriptEngine[] eng, String... names) throws ScriptException, IOException {
112 sb = new StringBuilder();
114 Bck2Brwsr.generate(sb, new EmulationResources(), names);
115 ScriptEngineManager sem = new ScriptEngineManager();
116 ScriptEngine js = sem.getEngineByExtension("js");
121 Object res = js.eval(sb.toString());
122 assertTrue(js instanceof Invocable, "It is invocable object: " + res);
123 return new TestVM((Invocable) js, sb);
124 } catch (Exception ex) {
125 if (sb.length() > 2000) {
128 fail("Could not evaluate:" + ex.getClass() + ":" + ex.getMessage() + "\n" + sb, ex);
133 Object loadClass(String loadClass, String name) throws ScriptException, NoSuchMethodException {
134 return code.invokeMethod(bck2brwsr, "loadClass", Exceptions.class.getName());
137 Object invokeMethod(Object obj, String method, Object... params) throws ScriptException, NoSuchMethodException {
138 return code.invokeMethod(obj, method, params);
141 Object invokeFunction(String methodName, Object... args) throws ScriptException, NoSuchMethodException {
142 return code.invokeFunction(methodName, args);
145 static StringBuilder dumpJS(CharSequence sb) throws IOException {
146 File f = File.createTempFile("execution", ".js");
147 FileWriter w = new FileWriter(f);
150 return new StringBuilder(f.getPath());
154 public String toString() {
156 return dumpJS(codeSeq).toString();
157 } catch (IOException ex) {
158 return ex.toString();
163 private static class EmulationResources implements Bck2Brwsr.Resources {
165 public InputStream get(String name) throws IOException {
166 if ("java/net/URI.class".equals(name)) {
170 if ("java/lang/System.class".equals(name)) {
174 Enumeration<URL> en = StaticMethodTest.class.getClassLoader().getResources(name);
176 while (en.hasMoreElements()) {
177 u = en.nextElement();
180 throw new IOException("Can't find " + name);
182 if (u.toExternalForm().contains("rt.jar!")) {
183 throw new IOException("No emulation for " + u);
185 return u.openStream();