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);
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 ret = code.invokeMethod(ret, "toFP");
69 ret = code.invokeFunction("Number", ret);
70 } catch (ScriptException ex) {
71 fail("Conversion to number failed in " + dumpJS(codeSeq), ex);
72 } catch (NoSuchMethodException ex) {
73 fail("Cannot find global Number(x) function in " + dumpJS(codeSeq), ex);
80 String msg, Class clazz, String method, Object expRes, Object... args
82 Object ret = execCode(msg, clazz, method, expRes, args);
86 if (expRes != null && expRes.equals(ret)) {
89 assertEquals(ret, expRes, msg + "was: " + ret + "\n" + dumpJS(codeSeq));
92 static TestVM compileClass(String... names) throws ScriptException, IOException {
93 return compileClass(null, names);
96 static TestVM compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
97 return compileClass(sb, null, names);
100 static TestVM compileClass(StringBuilder sb, ScriptEngine[] eng, String... names) throws ScriptException, IOException {
102 sb = new StringBuilder();
104 Bck2Brwsr.generate(sb, new EmulationResources(), names);
105 ScriptEngineManager sem = new ScriptEngineManager();
106 ScriptEngine js = sem.getEngineByExtension("js");
111 Object res = js.eval(sb.toString());
112 assertTrue(js instanceof Invocable, "It is invocable object: " + res);
113 return new TestVM((Invocable) js, sb);
114 } catch (Exception ex) {
115 if (sb.length() > 2000) {
118 fail("Could not evaluate:\n" + sb, ex);
123 Object loadClass(String loadClass, String name) throws ScriptException, NoSuchMethodException {
124 return code.invokeMethod(bck2brwsr, "loadClass", Exceptions.class.getName());
127 Object invokeMethod(Object obj, String method, Object... params) throws ScriptException, NoSuchMethodException {
128 return code.invokeMethod(obj, method, params);
131 Object invokeFunction(String methodName, Object... args) throws ScriptException, NoSuchMethodException {
132 return code.invokeFunction(methodName, args);
135 static StringBuilder dumpJS(CharSequence sb) throws IOException {
136 File f = File.createTempFile("execution", ".js");
137 FileWriter w = new FileWriter(f);
140 return new StringBuilder(f.getPath());
144 public String toString() {
146 return dumpJS(codeSeq).toString();
147 } catch (IOException ex) {
148 return ex.toString();
153 private static class EmulationResources implements Bck2Brwsr.Resources {
155 public InputStream get(String name) throws IOException {
156 Enumeration<URL> en = StaticMethodTest.class.getClassLoader().getResources(name);
158 while (en.hasMoreElements()) {
159 u = en.nextElement();
162 throw new IOException("Can't find " + name);
164 if (u.toExternalForm().contains("rt.jar!")) {
165 throw new IOException("No emulation for " + u);
167 return u.openStream();