1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/flow/src/test/java/org/apidesign/bck2brwsr/flow/TestVM.java Wed Mar 11 18:58:39 2015 +0100
1.3 @@ -0,0 +1,320 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. Look for COPYING file in the top folder.
1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 + */
1.21 +package org.apidesign.bck2brwsr.flow;
1.22 +
1.23 +import java.io.ByteArrayInputStream;
1.24 +import java.io.File;
1.25 +import java.io.FileOutputStream;
1.26 +import java.io.IOException;
1.27 +import java.io.InputStream;
1.28 +import java.io.OutputStreamWriter;
1.29 +import java.io.Writer;
1.30 +import java.net.URL;
1.31 +import java.util.ArrayList;
1.32 +import java.util.Arrays;
1.33 +import java.util.Enumeration;
1.34 +import java.util.HashSet;
1.35 +import java.util.List;
1.36 +import java.util.Set;
1.37 +import javax.script.Invocable;
1.38 +import javax.script.ScriptContext;
1.39 +import javax.script.ScriptEngine;
1.40 +import javax.script.ScriptEngineManager;
1.41 +import javax.script.ScriptException;
1.42 +import org.apidesign.vm4brwsr.Bck2Brwsr;
1.43 +import org.apidesign.vm4brwsr.Bck2Brwsr.Flow;
1.44 +import org.apidesign.vm4brwsr.ObfuscationLevel;
1.45 +import static org.testng.Assert.*;
1.46 +
1.47 +public final class TestVM {
1.48 + private final Invocable code;
1.49 + private final CharSequence codeSeq;
1.50 + private final Object bck2brwsr;
1.51 +
1.52 +
1.53 + private TestVM(Invocable code, CharSequence codeSeq) throws ScriptException, NoSuchMethodException {
1.54 + this.code = code;
1.55 + this.codeSeq = codeSeq;
1.56 + this.bck2brwsr = ((ScriptEngine)code).eval("bck2brwsr(function(n) { return loader.get(n); })");
1.57 + ((ScriptEngine)code).getContext().setAttribute("loader", this, ScriptContext.ENGINE_SCOPE);
1.58 + }
1.59 +
1.60 + public Object execCode(
1.61 + String msg, Class<?> clazz, String method,
1.62 + Object expRes, Object... args
1.63 + ) throws Exception {
1.64 + Object ret = null;
1.65 + try {
1.66 + ret = code.invokeMethod(bck2brwsr, "loadClass", clazz.getName());
1.67 + List<Object> ma = new ArrayList<Object>();
1.68 + ma.add(method);
1.69 + ma.addAll(Arrays.asList(args));
1.70 + ret = code.invokeMethod(ret, "invoke", ma.toArray());
1.71 + } catch (ScriptException ex) {
1.72 + fail("Execution failed in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
1.73 + } catch (NoSuchMethodException ex) {
1.74 + fail("Cannot find method in " + dumpJS(codeSeq), ex);
1.75 + }
1.76 + if (ret == null && expRes == null) {
1.77 + return null;
1.78 + }
1.79 + if (expRes != null && expRes.equals(ret)) {
1.80 + return null;
1.81 + }
1.82 + if (expRes instanceof Number) {
1.83 + // in case of Long it is necessary convert it to number
1.84 + // since the Long is represented by two numbers in JavaScript
1.85 + try {
1.86 + final Object toFP = ((ScriptEngine)code).eval("Number.prototype.toFP");
1.87 + if (ret instanceof Long) {
1.88 + ret = code.invokeMethod(toFP, "call", ret);
1.89 + }
1.90 + ret = code.invokeFunction("Number", ret);
1.91 + } catch (ScriptException ex) {
1.92 + fail("Conversion to number failed in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
1.93 + } catch (NoSuchMethodException ex) {
1.94 + fail("Cannot find global Number(x) function in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
1.95 + }
1.96 + }
1.97 + return ret;
1.98 + }
1.99 +
1.100 + void assertExec(
1.101 + String msg, Class clazz, String method, Object expRes, Object... args
1.102 + ) throws Exception {
1.103 + Object ret = execCode(msg, clazz, method, expRes, args);
1.104 + if (ret == null) {
1.105 + return;
1.106 + }
1.107 + if (expRes instanceof Integer && ret instanceof Double) {
1.108 + expRes = ((Integer)expRes).doubleValue();
1.109 + }
1.110 + if (expRes != null && expRes.equals(ret)) {
1.111 + return;
1.112 + }
1.113 + assertEquals(ret, expRes, msg + "was: " + ret + "\n" + dumpJS(codeSeq));
1.114 + }
1.115 +
1.116 + static TestVM compileClass(String... names) throws ScriptException, IOException {
1.117 + return compileClass(null, GraalFlowAnalyzer.getDefault(), names);
1.118 + }
1.119 +
1.120 + static TestVM compileClass(StringBuilder sb, Flow.Analyzer flow, String... names) throws ScriptException, IOException {
1.121 + return compileClass(sb, null, flow, names);
1.122 + }
1.123 +
1.124 + static TestVM compileClass(StringBuilder sb,
1.125 + ScriptEngine[] eng, Flow.Analyzer flow, String... names
1.126 + ) throws ScriptException, IOException {
1.127 + return compileClass(sb, eng, flow, new EmulationResources(), names);
1.128 + }
1.129 + static TestVM compileClass(
1.130 + StringBuilder sb,
1.131 + ScriptEngine[] eng,
1.132 + Flow.Analyzer flow,
1.133 + Bck2Brwsr.Resources resources,
1.134 + String... names
1.135 + ) throws ScriptException, IOException {
1.136 + if (sb == null) {
1.137 + sb = new StringBuilder();
1.138 + }
1.139 + Bck2Brwsr.newCompiler()
1.140 + .resources(resources)
1.141 + .addClasses(names)
1.142 + .flowAnalyzer(flow)
1.143 + .generate(sb);
1.144 + ScriptEngineManager sem = new ScriptEngineManager();
1.145 + ScriptEngine js = sem.getEngineByExtension("js");
1.146 + if (eng != null) {
1.147 + eng[0] = js;
1.148 + }
1.149 + try {
1.150 + Object res = js.eval(sb.toString());
1.151 + assertTrue(js instanceof Invocable, "It is invocable object: " + res);
1.152 + return new TestVM((Invocable) js, sb);
1.153 + } catch (Exception ex) {
1.154 + if (sb.length() > 2000) {
1.155 + sb = dumpJS(sb);
1.156 + }
1.157 + fail("Could not evaluate:" + ex.getClass() + ":" + ex.getMessage() + "\n" + sb, ex);
1.158 + return null;
1.159 + }
1.160 + }
1.161 +
1.162 + static TestVM compileClassAsExtension(
1.163 + StringBuilder sb, ScriptEngine[] eng,
1.164 + String name, final String resourceName, final String resourceContent
1.165 + ) throws ScriptException, IOException {
1.166 + return compileClassesAsExtension(sb, eng, resourceName, resourceContent, name);
1.167 + }
1.168 + static TestVM compileClassesAsExtension(
1.169 + StringBuilder sb, ScriptEngine[] eng,
1.170 + final String resourceName, final String resourceContent, String... names
1.171 + ) throws ScriptException, IOException {
1.172 + if (sb == null) {
1.173 + sb = new StringBuilder();
1.174 + }
1.175 + if (eng[0] == null) {
1.176 + ScriptEngineManager sem = new ScriptEngineManager();
1.177 + ScriptEngine js = sem.getEngineByExtension("js");
1.178 + eng[0] = js;
1.179 + Bck2Brwsr.newCompiler().resources(new EmulationResources())
1.180 + .obfuscation(ObfuscationLevel.NONE).generate(sb);
1.181 + }
1.182 + Set<String> exp = new HashSet<String>();
1.183 + for (String n : names) {
1.184 + int last = n.lastIndexOf('/');
1.185 + exp.add(n.substring(0, last + 1));
1.186 + }
1.187 + Bck2Brwsr b2b = Bck2Brwsr.newCompiler().
1.188 + resources(new EmulationResources() {
1.189 + @Override
1.190 + public InputStream get(String name) throws IOException {
1.191 + if (name.equals(resourceName)) {
1.192 + return new ByteArrayInputStream(resourceContent.getBytes("UTF-8"));
1.193 + }
1.194 + return super.get(name);
1.195 + }
1.196 + }).
1.197 + addClasses(names).
1.198 + addResources("org/apidesign/vm4brwsr/obj.js").
1.199 + addExported(exp.toArray(new String[0])).
1.200 + obfuscation(ObfuscationLevel.FULL).
1.201 + library();
1.202 + if (resourceName != null) {
1.203 + b2b = b2b.addResources(resourceName);
1.204 + }
1.205 + b2b.generate(sb);
1.206 + try {
1.207 + defineAtoB(eng[0]);
1.208 + Object res = eng[0].eval(sb.toString());
1.209 + assertTrue(eng[0] instanceof Invocable, "It is invocable object: " + res);
1.210 + return new TestVM((Invocable) eng[0], sb);
1.211 + } catch (Exception ex) {
1.212 + if (sb.length() > 2000) {
1.213 + sb = dumpJS(sb);
1.214 + }
1.215 + fail("Could not evaluate:" + ex.getClass() + ":" + ex.getMessage() + "\n" + sb, ex);
1.216 + return null;
1.217 + }
1.218 + }
1.219 +
1.220 + static TestVM compileClassAndResources(StringBuilder sb, ScriptEngine[] eng, String name, String... resources) throws ScriptException, IOException {
1.221 + if (sb == null) {
1.222 + sb = new StringBuilder();
1.223 + }
1.224 + Bck2Brwsr b2b = Bck2Brwsr.newCompiler().
1.225 + resources(new EmulationResources()).
1.226 + addRootClasses(name).
1.227 + addResources(resources);
1.228 + b2b.generate(sb);
1.229 + ScriptEngineManager sem = new ScriptEngineManager();
1.230 + ScriptEngine js = sem.getEngineByExtension("js");
1.231 + if (eng != null) {
1.232 + eng[0] = js;
1.233 + }
1.234 + try {
1.235 + defineAtoB(js);
1.236 +
1.237 + Object res = js.eval(sb.toString());
1.238 + assertTrue(js instanceof Invocable, "It is invocable object: " + res);
1.239 + return new TestVM((Invocable) js, sb);
1.240 + } catch (Exception ex) {
1.241 + if (sb.length() > 2000) {
1.242 + sb = dumpJS(sb);
1.243 + }
1.244 + fail("Could not evaluate:" + ex.getClass() + ":" + ex.getMessage() + "\n" + sb, ex);
1.245 + return null;
1.246 + }
1.247 + }
1.248 +
1.249 + private static void defineAtoB(ScriptEngine js) throws ScriptException {
1.250 + js.eval("atob = function(s) { return new String(org.apidesign.vm4brwsr.ResourcesTest.parseBase64Binary(s)); }");
1.251 + }
1.252 +
1.253 + Object loadClass(String loadClass, String name) throws ScriptException, NoSuchMethodException {
1.254 + return code.invokeMethod(bck2brwsr, "loadClass", LoopControl.class.getName());
1.255 + }
1.256 +
1.257 + Object invokeMethod(Object obj, String method, Object... params) throws ScriptException, NoSuchMethodException {
1.258 + return code.invokeMethod(obj, method, params);
1.259 + }
1.260 +
1.261 + Object invokeFunction(String methodName, Object... args) throws ScriptException, NoSuchMethodException {
1.262 + return code.invokeFunction(methodName, args);
1.263 + }
1.264 +
1.265 + static StringBuilder dumpJS(CharSequence sb) throws IOException {
1.266 + File f = File.createTempFile("execution", ".js");
1.267 + Writer w = new OutputStreamWriter(new FileOutputStream(f), "UTF-8");
1.268 + w.append(sb);
1.269 + w.close();
1.270 + return new StringBuilder(f.getPath());
1.271 + }
1.272 +
1.273 + @Override
1.274 + public String toString() {
1.275 + try {
1.276 + return dumpJS(codeSeq).toString();
1.277 + } catch (IOException ex) {
1.278 + return ex.toString();
1.279 + }
1.280 + }
1.281 +
1.282 + final CharSequence codeSeq() {
1.283 + return codeSeq;
1.284 + }
1.285 +
1.286 + private static class EmulationResources implements Bck2Brwsr.Resources {
1.287 + @Override
1.288 + public InputStream get(String name) throws IOException {
1.289 + if ("java/net/URI.class".equals(name)) {
1.290 + // skip
1.291 + return null;
1.292 + }
1.293 + if ("java/net/URLConnection.class".equals(name)) {
1.294 + // skip
1.295 + return null;
1.296 + }
1.297 + if ("java/lang/System.class".equals(name)) {
1.298 + // skip
1.299 + return null;
1.300 + }
1.301 + if ("java/io/PrintStream.class".equals(name)) {
1.302 + // skip
1.303 + return null;
1.304 + }
1.305 + if ("java/io/PrintWriter.class".equals(name)) {
1.306 + // skip
1.307 + return null;
1.308 + }
1.309 + Enumeration<URL> en = LoopControlTest.class.getClassLoader().getResources(name);
1.310 + URL u = null;
1.311 + while (en.hasMoreElements()) {
1.312 + u = en.nextElement();
1.313 + }
1.314 + if (u == null) {
1.315 + throw new IOException("Can't find " + name);
1.316 + }
1.317 + if (u.toExternalForm().contains("rt.jar!")) {
1.318 + throw new IOException("No emulation for " + u);
1.319 + }
1.320 + return u.openStream();
1.321 + }
1.322 + }
1.323 +}