Optimize only simpleLoopTest... method and first check the generated code before compiling it as JavaScript
2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012-2015 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.bck2brwsr.flow;
20 import java.io.ByteArrayInputStream;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStreamWriter;
26 import java.io.Writer;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.Enumeration;
31 import java.util.HashSet;
32 import java.util.List;
34 import javax.script.Invocable;
35 import javax.script.ScriptContext;
36 import javax.script.ScriptEngine;
37 import javax.script.ScriptEngineManager;
38 import javax.script.ScriptException;
39 import org.apidesign.vm4brwsr.Bck2Brwsr;
40 import org.apidesign.vm4brwsr.Bck2Brwsr.Flow;
41 import org.apidesign.vm4brwsr.ObfuscationLevel;
42 import static org.testng.Assert.*;
44 public final class TestVM {
45 private final ScriptEngine js;
46 private Invocable code;
47 private final CharSequence codeSeq;
48 private Object bck2brwsr;
51 private TestVM(ScriptEngine js, CharSequence codeSeq) throws ScriptException, NoSuchMethodException {
53 this.codeSeq = codeSeq;
56 public Object execCode(
57 String msg, Class<?> clazz, String method,
58 Object expRes, Object... args
62 ret = getCode().invokeMethod(bck2brwsr, "loadClass", clazz.getName());
63 List<Object> ma = new ArrayList<Object>();
65 ma.addAll(Arrays.asList(args));
66 ret = getCode().invokeMethod(ret, "invoke", ma.toArray());
67 } catch (ScriptException ex) {
68 fail("Execution failed in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
69 } catch (NoSuchMethodException ex) {
70 fail("Cannot find method in " + dumpJS(codeSeq), ex);
72 if (ret == null && expRes == null) {
75 if (expRes != null && expRes.equals(ret)) {
78 if (expRes instanceof Number) {
79 // in case of Long it is necessary convert it to number
80 // since the Long is represented by two numbers in JavaScript
82 final Object toFP = ((ScriptEngine)getCode()).eval("Number.prototype.toFP");
83 if (ret instanceof Long) {
84 ret = getCode().invokeMethod(toFP, "call", ret);
86 ret = getCode().invokeFunction("Number", ret);
87 } catch (ScriptException ex) {
88 fail("Conversion to number failed in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
89 } catch (NoSuchMethodException ex) {
90 fail("Cannot find global Number(x) function in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
97 String msg, Class clazz, String method, Object expRes, Object... args
99 Object ret = execCode(msg, clazz, method, expRes, args);
103 if (expRes instanceof Integer && ret instanceof Double) {
104 expRes = ((Integer)expRes).doubleValue();
106 if (expRes != null && expRes.equals(ret)) {
109 assertEquals(ret, expRes, msg + "was: " + ret + "\n" + dumpJS(codeSeq));
112 static TestVM compileClass(String... names) throws ScriptException, IOException {
113 return compileClass(null, GraalFlowAnalyzer.getDefault(), names);
116 static TestVM compileClass(StringBuilder sb, Flow.Analyzer flow, String... names) throws ScriptException, IOException {
117 return compileClass(sb, null, flow, names);
120 static TestVM compileClass(StringBuilder sb,
121 ScriptEngine[] eng, Flow.Analyzer flow, String... names
122 ) throws ScriptException, IOException {
123 return compileClass(sb, eng, flow, new EmulationResources(), names);
125 static TestVM compileClass(
129 Bck2Brwsr.Resources resources,
131 ) throws ScriptException, IOException {
133 sb = new StringBuilder();
135 Bck2Brwsr.newCompiler()
136 .resources(resources)
140 ScriptEngineManager sem = new ScriptEngineManager();
141 ScriptEngine js = sem.getEngineByExtension("js");
146 return new TestVM(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 getCode().invokeMethod(bck2brwsr, "loadClass", LoopControl.class.getName());
160 Object invokeMethod(Object obj, String method, Object... params) throws ScriptException, NoSuchMethodException {
161 return getCode().invokeMethod(obj, method, params);
164 Object invokeFunction(String methodName, Object... args) throws ScriptException, NoSuchMethodException {
165 return getCode().invokeFunction(methodName, args);
168 static StringBuilder dumpJS(CharSequence sb) throws IOException {
169 File f = File.createTempFile("execution", ".js");
170 Writer w = new OutputStreamWriter(new FileOutputStream(f), "UTF-8");
173 return new StringBuilder(f.getPath());
177 public String toString() {
179 return dumpJS(codeSeq).toString();
180 } catch (IOException ex) {
181 return ex.toString();
185 final CharSequence codeSeq() {
192 private Invocable getCode() throws ScriptException {
194 Object res = js.eval(code.toString());
195 code = (Invocable) js;
196 this.bck2brwsr = ((ScriptEngine)code).eval("bck2brwsr(function(n) { return loader.get(n); })");
197 ((ScriptEngine)code).getContext().setAttribute("loader", this, ScriptContext.ENGINE_SCOPE);
199 return (Invocable)code;
202 private static class EmulationResources implements Bck2Brwsr.Resources {
204 public InputStream get(String name) throws IOException {
205 if ("java/net/URI.class".equals(name)) {
209 if ("java/net/URLConnection.class".equals(name)) {
213 if ("java/lang/System.class".equals(name)) {
217 if ("java/io/PrintStream.class".equals(name)) {
221 if ("java/io/PrintWriter.class".equals(name)) {
225 Enumeration<URL> en = LoopControlTest.class.getClassLoader().getResources(name);
227 while (en.hasMoreElements()) {
228 u = en.nextElement();
231 throw new IOException("Can't find " + name);
233 if (u.toExternalForm().contains("rt.jar!")) {
234 throw new IOException("No emulation for " + u);
236 return u.openStream();