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.Enumeration;
27 import javax.script.Invocable;
28 import javax.script.ScriptContext;
29 import javax.script.ScriptEngine;
30 import javax.script.ScriptEngineManager;
31 import javax.script.ScriptException;
32 import static org.testng.Assert.*;
34 public final class TestVM {
35 private final Invocable code;
36 private final CharSequence codeSeq;
37 private final Object bck2brwsr;
38 private BytesLoader resources;
41 private TestVM(Invocable code, CharSequence codeSeq) throws ScriptException, NoSuchMethodException {
43 this.codeSeq = codeSeq;
44 this.bck2brwsr = ((ScriptEngine)code).eval("bck2brwsr(function(n) { return loader.get(n); })");
45 ((ScriptEngine)code).getContext().setAttribute("loader", this, ScriptContext.ENGINE_SCOPE);
48 public void register(BytesLoader res) {
52 public byte[] get(String res) throws IOException {
53 return resources != null ? resources.get(res) : null;
56 public Object execCode(
57 String msg, Class<?> clazz, String method,
58 Object expRes, Object... args
62 ret = code.invokeMethod(bck2brwsr, "loadClass", clazz.getName());
63 ret = code.invokeMethod(ret, method, args);
64 } catch (ScriptException ex) {
65 fail("Execution failed in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
66 } catch (NoSuchMethodException ex) {
67 fail("Cannot find method in " + dumpJS(codeSeq), ex);
69 if (ret == null && expRes == null) {
72 if (expRes != null && expRes.equals(ret)) {
75 if (expRes instanceof Number) {
76 // in case of Long it is necessary convert it to number
77 // since the Long is represented by two numbers in JavaScript
79 final Object toFP = ((ScriptEngine)code).eval("Number.prototype.toFP");
80 if (ret instanceof Long) {
81 ret = code.invokeMethod(toFP, "call", ret);
83 ret = code.invokeFunction("Number", ret);
84 } catch (ScriptException ex) {
85 fail("Conversion to number failed in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
86 } catch (NoSuchMethodException ex) {
87 fail("Cannot find global Number(x) function in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
94 String msg, Class clazz, String method, Object expRes, Object... args
96 Object ret = execCode(msg, clazz, method, expRes, args);
100 if (expRes instanceof Integer && ret instanceof Double) {
101 expRes = ((Integer)expRes).doubleValue();
103 if (expRes != null && expRes.equals(ret)) {
106 assertEquals(ret, expRes, msg + "was: " + ret + "\n" + dumpJS(codeSeq));
109 static TestVM compileClass(String... names) throws ScriptException, IOException {
110 return compileClass(null, names);
113 static TestVM compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
114 return compileClass(sb, null, names);
117 static TestVM compileClass(StringBuilder sb, ScriptEngine[] eng, String... names) throws ScriptException, IOException {
119 sb = new StringBuilder();
121 Bck2Brwsr.generate(sb, new EmulationResources(), names);
122 ScriptEngineManager sem = new ScriptEngineManager();
123 ScriptEngine js = sem.getEngineByExtension("js");
128 Object res = js.eval(sb.toString());
129 assertTrue(js instanceof Invocable, "It is invocable object: " + res);
130 return new TestVM((Invocable) js, sb);
131 } catch (Exception ex) {
132 if (sb.length() > 2000) {
135 fail("Could not evaluate:" + ex.getClass() + ":" + ex.getMessage() + "\n" + sb, ex);
140 static TestVM compileClassAsExtension(
141 StringBuilder sb, ScriptEngine[] eng,
142 String name, final String resourceName, final String resourceContent
143 ) throws ScriptException, IOException {
145 sb = new StringBuilder();
147 if (eng[0] == null) {
148 ScriptEngineManager sem = new ScriptEngineManager();
149 ScriptEngine js = sem.getEngineByExtension("js");
151 Bck2Brwsr.generate(sb, new EmulationResources());
153 Bck2Brwsr b2b = Bck2Brwsr.newCompiler().
154 resources(new EmulationResources() {
156 public InputStream get(String name) throws IOException {
157 if (name.equals(resourceName)) {
158 return new ByteArrayInputStream(resourceContent.getBytes("UTF-8"));
160 return super.get(name);
163 addRootClasses(name).
164 addResources("org/apidesign/vm4brwsr/obj.js").
165 obfuscation(ObfuscationLevel.FULL).
167 if (resourceName != null) {
168 b2b = b2b.addResources(resourceName);
173 Object res = eng[0].eval(sb.toString());
174 assertTrue(eng[0] instanceof Invocable, "It is invocable object: " + res);
175 return new TestVM((Invocable) eng[0], sb);
176 } catch (Exception ex) {
177 if (sb.length() > 2000) {
180 fail("Could not evaluate:" + ex.getClass() + ":" + ex.getMessage() + "\n" + sb, ex);
185 static TestVM compileClassAndResources(StringBuilder sb, ScriptEngine[] eng, String name, String... resources) throws ScriptException, IOException {
187 sb = new StringBuilder();
189 Bck2Brwsr b2b = Bck2Brwsr.newCompiler().
190 resources(new EmulationResources()).
191 addRootClasses(name).
192 addResources(resources).
195 ScriptEngineManager sem = new ScriptEngineManager();
196 ScriptEngine js = sem.getEngineByExtension("js");
203 Object res = js.eval(sb.toString());
204 assertTrue(js instanceof Invocable, "It is invocable object: " + res);
205 return new TestVM((Invocable) js, sb);
206 } catch (Exception ex) {
207 if (sb.length() > 2000) {
210 fail("Could not evaluate:" + ex.getClass() + ":" + ex.getMessage() + "\n" + sb, ex);
215 private static void defineAtoB(ScriptEngine js) throws ScriptException {
216 js.eval("atob = function(s) { return new String(org.apidesign.vm4brwsr.ResourcesTest.parseBase64Binary(s)); }");
219 Object loadClass(String loadClass, String name) throws ScriptException, NoSuchMethodException {
220 return code.invokeMethod(bck2brwsr, "loadClass", Exceptions.class.getName());
223 Object invokeMethod(Object obj, String method, Object... params) throws ScriptException, NoSuchMethodException {
224 return code.invokeMethod(obj, method, params);
227 Object invokeFunction(String methodName, Object... args) throws ScriptException, NoSuchMethodException {
228 return code.invokeFunction(methodName, args);
231 static StringBuilder dumpJS(CharSequence sb) throws IOException {
232 File f = File.createTempFile("execution", ".js");
233 FileWriter w = new FileWriter(f);
236 return new StringBuilder(f.getPath());
240 public String toString() {
242 return dumpJS(codeSeq).toString();
243 } catch (IOException ex) {
244 return ex.toString();
248 final CharSequence codeSeq() {
252 private static class EmulationResources implements Bck2Brwsr.Resources {
254 public InputStream get(String name) throws IOException {
255 if ("java/net/URI.class".equals(name)) {
259 if ("java/net/URLConnection.class".equals(name)) {
263 if ("java/lang/System.class".equals(name)) {
267 Enumeration<URL> en = StaticMethodTest.class.getClassLoader().getResources(name);
269 while (en.hasMoreElements()) {
270 u = en.nextElement();
273 throw new IOException("Can't find " + name);
275 if (u.toExternalForm().contains("rt.jar!")) {
276 throw new IOException("No emulation for " + u);
278 return u.openStream();