Different meaning of root vs. added classes. Ability to explicitly enumerate classes that should be exported and available with fully qualified name.
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.ScriptEngine;
28 import javax.script.ScriptEngineManager;
29 import javax.script.ScriptException;
30 import static org.testng.Assert.*;
33 private final Invocable code;
34 private final CharSequence codeSeq;
35 private final Object bck2brwsr;
38 private TestVM(Invocable code, CharSequence codeSeq) throws ScriptException, NoSuchMethodException {
40 this.codeSeq = codeSeq;
41 this.bck2brwsr = code.invokeFunction("bck2brwsr");
45 public Object execCode(
46 String msg, Class<?> clazz, String method,
47 Object expRes, Object... args
51 ret = code.invokeMethod(bck2brwsr, "loadClass", clazz.getName());
52 ret = code.invokeMethod(ret, method, args);
53 } catch (ScriptException ex) {
54 fail("Execution failed in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
55 } catch (NoSuchMethodException ex) {
56 fail("Cannot find method in " + dumpJS(codeSeq), ex);
58 if (ret == null && expRes == null) {
61 if (expRes != null && expRes.equals(ret)) {
64 if (expRes instanceof Number) {
65 // in case of Long it is necessary convert it to number
66 // since the Long is represented by two numbers in JavaScript
68 final Object toFP = ((ScriptEngine)code).eval("Number.prototype.toFP");
69 if (ret instanceof Long) {
70 ret = code.invokeMethod(toFP, "call", ret);
72 ret = code.invokeFunction("Number", ret);
73 } catch (ScriptException ex) {
74 fail("Conversion to number failed in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
75 } catch (NoSuchMethodException ex) {
76 fail("Cannot find global Number(x) function in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
83 String msg, Class clazz, String method, Object expRes, Object... args
85 Object ret = execCode(msg, clazz, method, expRes, args);
89 if (expRes instanceof Integer && ret instanceof Double) {
90 expRes = ((Integer)expRes).doubleValue();
92 if (expRes != null && expRes.equals(ret)) {
95 assertEquals(ret, expRes, msg + "was: " + ret + "\n" + dumpJS(codeSeq));
98 static TestVM compileClass(String... names) throws ScriptException, IOException {
99 return compileClass(null, names);
102 static TestVM compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
103 return compileClass(sb, null, names);
106 static TestVM compileClass(StringBuilder sb, ScriptEngine[] eng, String... names) throws ScriptException, IOException {
108 sb = new StringBuilder();
110 Bck2Brwsr.generate(sb, new EmulationResources(), names);
111 ScriptEngineManager sem = new ScriptEngineManager();
112 ScriptEngine js = sem.getEngineByExtension("js");
117 Object res = js.eval(sb.toString());
118 assertTrue(js instanceof Invocable, "It is invocable object: " + res);
119 return new TestVM((Invocable) js, sb);
120 } catch (Exception ex) {
121 if (sb.length() > 2000) {
124 fail("Could not evaluate:" + ex.getClass() + ":" + ex.getMessage() + "\n" + sb, ex);
129 static TestVM compileClassAsExtension(StringBuilder sb, ScriptEngine[] eng, String... names) throws ScriptException, IOException {
131 sb = new StringBuilder();
133 Bck2Brwsr.generate(sb, new EmulationResources());
134 Bck2Brwsr b2b = Bck2Brwsr.newCompiler().
135 resources(new EmulationResources()).
136 addRootClasses(names).library(true);
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 Object loadClass(String loadClass, String name) throws ScriptException, NoSuchMethodException {
157 return code.invokeMethod(bck2brwsr, "loadClass", Exceptions.class.getName());
160 Object invokeMethod(Object obj, String method, Object... params) throws ScriptException, NoSuchMethodException {
161 return code.invokeMethod(obj, method, params);
164 Object invokeFunction(String methodName, Object... args) throws ScriptException, NoSuchMethodException {
165 return code.invokeFunction(methodName, args);
168 static StringBuilder dumpJS(CharSequence sb) throws IOException {
169 File f = File.createTempFile("execution", ".js");
170 FileWriter w = new FileWriter(f);
173 return new StringBuilder(f.getPath());
177 public String toString() {
179 return dumpJS(codeSeq).toString();
180 } catch (IOException ex) {
181 return ex.toString();
186 private static class EmulationResources implements Bck2Brwsr.Resources {
188 public InputStream get(String name) throws IOException {
189 Enumeration<URL> en = StaticMethodTest.class.getClassLoader().getResources(name);
191 while (en.hasMoreElements()) {
192 u = en.nextElement();
195 throw new IOException("Can't find " + name);
197 if (u.toExternalForm().contains("rt.jar!")) {
198 throw new IOException("No emulation for " + u);
200 return u.openStream();