Adopting to JDK8's Nashorn differences. Most tests should now pass with Nashorn now.
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 final Object toFP = ((ScriptEngine)code).eval("Number.prototype.toFP");
79 if (ret instanceof Long) {
80 ret = code.invokeMethod(toFP, "call", ret);
82 ret = code.invokeFunction("Number", ret);
83 } catch (ScriptException ex) {
84 fail("Conversion to number failed in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
85 } catch (NoSuchMethodException ex) {
86 fail("Cannot find global Number(x) function in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
93 String msg, Class clazz, String method, Object expRes, Object... args
95 Object ret = execCode(msg, clazz, method, expRes, args);
99 if (expRes instanceof Integer && ret instanceof Double) {
100 expRes = ((Integer)expRes).doubleValue();
102 if (expRes != null && expRes.equals(ret)) {
105 assertEquals(ret, expRes, msg + "was: " + ret + "\n" + dumpJS(codeSeq));
108 static TestVM compileClass(String... names) throws ScriptException, IOException {
109 return compileClass(null, names);
112 static TestVM compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
113 return compileClass(sb, null, names);
116 static TestVM compileClass(StringBuilder sb, ScriptEngine[] eng, String... names) throws ScriptException, IOException {
118 sb = new StringBuilder();
120 Bck2Brwsr.generate(sb, new EmulationResources(), names);
121 ScriptEngineManager sem = new ScriptEngineManager();
122 ScriptEngine js = sem.getEngineByExtension("js");
127 Object res = js.eval(sb.toString());
128 assertTrue(js instanceof Invocable, "It is invocable object: " + res);
129 return new TestVM((Invocable) js, sb);
130 } catch (Exception ex) {
131 if (sb.length() > 2000) {
134 fail("Could not evaluate:" + ex.getClass() + ":" + ex.getMessage() + "\n" + sb, ex);
139 Object loadClass(String loadClass, String name) throws ScriptException, NoSuchMethodException {
140 return code.invokeMethod(bck2brwsr, "loadClass", Exceptions.class.getName());
143 Object invokeMethod(Object obj, String method, Object... params) throws ScriptException, NoSuchMethodException {
144 return code.invokeMethod(obj, method, params);
147 Object invokeFunction(String methodName, Object... args) throws ScriptException, NoSuchMethodException {
148 return code.invokeFunction(methodName, args);
151 static StringBuilder dumpJS(CharSequence sb) throws IOException {
152 File f = File.createTempFile("execution", ".js");
153 FileWriter w = new FileWriter(f);
156 return new StringBuilder(f.getPath());
160 public String toString() {
162 return dumpJS(codeSeq).toString();
163 } catch (IOException ex) {
164 return ex.toString();
169 private static class EmulationResources implements Bck2Brwsr.Resources {
171 public InputStream get(String name) throws IOException {
172 if ("java/net/URI.class".equals(name)) {
176 if ("java/lang/System.class".equals(name)) {
180 Enumeration<URL> en = StaticMethodTest.class.getClassLoader().getResources(name);
182 while (en.hasMoreElements()) {
183 u = en.nextElement();
186 throw new IOException("Can't find " + name);
188 if (u.toExternalForm().contains("rt.jar!")) {
189 throw new IOException("No emulation for " + u);
191 return u.openStream();