1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/vm4brwsr/dynamic/TestVM.java Sat Aug 09 12:15:18 2014 +0200
1.3 @@ -0,0 +1,297 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012 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.vm4brwsr.dynamic;
1.22 +
1.23 +import java.io.ByteArrayInputStream;
1.24 +import java.io.File;
1.25 +import java.io.FileWriter;
1.26 +import java.io.IOException;
1.27 +import java.io.InputStream;
1.28 +import java.net.URL;
1.29 +import java.util.ArrayList;
1.30 +import java.util.Arrays;
1.31 +import java.util.Enumeration;
1.32 +import java.util.HashSet;
1.33 +import java.util.List;
1.34 +import java.util.Set;
1.35 +import javax.script.Invocable;
1.36 +import javax.script.ScriptContext;
1.37 +import javax.script.ScriptEngine;
1.38 +import javax.script.ScriptEngineManager;
1.39 +import javax.script.ScriptException;
1.40 +import org.apidesign.vm4brwsr.Bck2Brwsr;
1.41 +import org.apidesign.vm4brwsr.ObfuscationLevel;
1.42 +import static org.testng.Assert.*;
1.43 +
1.44 +public final class TestVM {
1.45 + private final Invocable code;
1.46 + private final CharSequence codeSeq;
1.47 + private final Object bck2brwsr;
1.48 +
1.49 +
1.50 + private TestVM(Invocable code, CharSequence codeSeq) throws ScriptException, NoSuchMethodException {
1.51 + this.code = code;
1.52 + this.codeSeq = codeSeq;
1.53 + this.bck2brwsr = ((ScriptEngine)code).eval("bck2brwsr(function(n) { return loader.get(n); })");
1.54 + ((ScriptEngine)code).getContext().setAttribute("loader", this, ScriptContext.ENGINE_SCOPE);
1.55 + }
1.56 +
1.57 + public Object execCode(
1.58 + String msg, Class<?> clazz, String method,
1.59 + Object expRes, Object... args
1.60 + ) throws Exception {
1.61 + Object ret = null;
1.62 + try {
1.63 + ret = code.invokeMethod(bck2brwsr, "loadClass", clazz.getName());
1.64 + List<Object> ma = new ArrayList<>();
1.65 + ma.add(method);
1.66 + ma.addAll(Arrays.asList(args));
1.67 + ret = code.invokeMethod(ret, "invoke", ma.toArray());
1.68 + } catch (ScriptException ex) {
1.69 + fail("Execution failed in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
1.70 + } catch (NoSuchMethodException ex) {
1.71 + fail("Cannot find method in " + dumpJS(codeSeq), ex);
1.72 + }
1.73 + if (ret == null && expRes == null) {
1.74 + return null;
1.75 + }
1.76 + if (expRes != null && expRes.equals(ret)) {
1.77 + return null;
1.78 + }
1.79 + if (expRes instanceof Number) {
1.80 + // in case of Long it is necessary convert it to number
1.81 + // since the Long is represented by two numbers in JavaScript
1.82 + try {
1.83 + final Object toFP = ((ScriptEngine)code).eval("Number.prototype.toFP");
1.84 + if (ret instanceof Long) {
1.85 + ret = code.invokeMethod(toFP, "call", ret);
1.86 + }
1.87 + ret = code.invokeFunction("Number", ret);
1.88 + } catch (ScriptException ex) {
1.89 + fail("Conversion to number failed in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
1.90 + } catch (NoSuchMethodException ex) {
1.91 + fail("Cannot find global Number(x) function in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
1.92 + }
1.93 + }
1.94 + return ret;
1.95 + }
1.96 +
1.97 + void assertExec(
1.98 + String msg, Class clazz, String method, Object expRes, Object... args
1.99 + ) throws Exception {
1.100 + Object ret = execCode(msg, clazz, method, expRes, args);
1.101 + if (ret == null) {
1.102 + return;
1.103 + }
1.104 + if (expRes instanceof Integer && ret instanceof Double) {
1.105 + expRes = ((Integer)expRes).doubleValue();
1.106 + }
1.107 + if (expRes != null && expRes.equals(ret)) {
1.108 + return;
1.109 + }
1.110 + assertEquals(ret, expRes, msg + "was: " + ret + "\n" + dumpJS(codeSeq));
1.111 + }
1.112 +
1.113 + static TestVM compileClass(String... names) throws ScriptException, IOException {
1.114 + return compileClass(null, names);
1.115 + }
1.116 +
1.117 + static TestVM compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
1.118 + return compileClass(sb, null, names);
1.119 + }
1.120 +
1.121 + static TestVM compileClass(StringBuilder sb, ScriptEngine[] eng, String... names) throws ScriptException, IOException {
1.122 + return compileClass(sb, eng, new EmulationResources(), names);
1.123 + }
1.124 + static TestVM compileClass(
1.125 + StringBuilder sb,
1.126 + ScriptEngine[] eng,
1.127 + Bck2Brwsr.Resources resources,
1.128 + String... names
1.129 + ) throws ScriptException, IOException {
1.130 + if (sb == null) {
1.131 + sb = new StringBuilder();
1.132 + }
1.133 + Bck2Brwsr.generate(sb, resources, names);
1.134 + ScriptEngineManager sem = new ScriptEngineManager();
1.135 + ScriptEngine js = sem.getEngineByExtension("js");
1.136 + if (eng != null) {
1.137 + eng[0] = js;
1.138 + }
1.139 + try {
1.140 + Object res = js.eval(sb.toString());
1.141 + assertTrue(js instanceof Invocable, "It is invocable object: " + res);
1.142 + return new TestVM((Invocable) js, sb);
1.143 + } catch (Exception ex) {
1.144 + if (sb.length() > 2000) {
1.145 + sb = dumpJS(sb);
1.146 + }
1.147 + fail("Could not evaluate:" + ex.getClass() + ":" + ex.getMessage() + "\n" + sb, ex);
1.148 + return null;
1.149 + }
1.150 + }
1.151 +
1.152 + static TestVM compileClassAsExtension(
1.153 + StringBuilder sb, ScriptEngine[] eng,
1.154 + String name, final String resourceName, final String resourceContent
1.155 + ) throws ScriptException, IOException {
1.156 + return compileClassesAsExtension(sb, eng, resourceName, resourceContent, name);
1.157 + }
1.158 + static TestVM compileClassesAsExtension(
1.159 + StringBuilder sb, ScriptEngine[] eng,
1.160 + final String resourceName, final String resourceContent, String... names
1.161 + ) throws ScriptException, IOException {
1.162 + if (sb == null) {
1.163 + sb = new StringBuilder();
1.164 + }
1.165 + if (eng[0] == null) {
1.166 + ScriptEngineManager sem = new ScriptEngineManager();
1.167 + ScriptEngine js = sem.getEngineByExtension("js");
1.168 + eng[0] = js;
1.169 + Bck2Brwsr.generate(sb, new EmulationResources());
1.170 + }
1.171 + Set<String> exp = new HashSet<String>();
1.172 + for (String n : names) {
1.173 + int last = n.lastIndexOf('/');
1.174 + exp.add(n.substring(0, last + 1));
1.175 + }
1.176 + Bck2Brwsr b2b = Bck2Brwsr.newCompiler().
1.177 + resources(new EmulationResources() {
1.178 + @Override
1.179 + public InputStream get(String name) throws IOException {
1.180 + if (name.equals(resourceName)) {
1.181 + return new ByteArrayInputStream(resourceContent.getBytes("UTF-8"));
1.182 + }
1.183 + return super.get(name);
1.184 + }
1.185 + }).
1.186 + addClasses(names).
1.187 + addResources("org/apidesign/vm4brwsr/obj.js").
1.188 + addExported(exp.toArray(new String[0])).
1.189 + obfuscation(ObfuscationLevel.FULL).
1.190 + library();
1.191 + if (resourceName != null) {
1.192 + b2b = b2b.addResources(resourceName);
1.193 + }
1.194 + b2b.generate(sb);
1.195 + try {
1.196 + defineAtoB(eng[0]);
1.197 + Object res = eng[0].eval(sb.toString());
1.198 + assertTrue(eng[0] instanceof Invocable, "It is invocable object: " + res);
1.199 + return new TestVM((Invocable) eng[0], sb);
1.200 + } catch (Exception ex) {
1.201 + if (sb.length() > 2000) {
1.202 + sb = dumpJS(sb);
1.203 + }
1.204 + fail("Could not evaluate:" + ex.getClass() + ":" + ex.getMessage() + "\n" + sb, ex);
1.205 + return null;
1.206 + }
1.207 + }
1.208 +
1.209 + static TestVM compileClassAndResources(StringBuilder sb, ScriptEngine[] eng, String name, String... resources) throws ScriptException, IOException {
1.210 + if (sb == null) {
1.211 + sb = new StringBuilder();
1.212 + }
1.213 + Bck2Brwsr b2b = Bck2Brwsr.newCompiler().
1.214 + resources(new EmulationResources()).
1.215 + addRootClasses(name).
1.216 + addResources(resources);
1.217 + b2b.generate(sb);
1.218 + ScriptEngineManager sem = new ScriptEngineManager();
1.219 + ScriptEngine js = sem.getEngineByExtension("js");
1.220 + if (eng != null) {
1.221 + eng[0] = js;
1.222 + }
1.223 + try {
1.224 + defineAtoB(js);
1.225 +
1.226 + Object res = js.eval(sb.toString());
1.227 + assertTrue(js instanceof Invocable, "It is invocable object: " + res);
1.228 + return new TestVM((Invocable) js, sb);
1.229 + } catch (Exception ex) {
1.230 + if (sb.length() > 2000) {
1.231 + sb = dumpJS(sb);
1.232 + }
1.233 + fail("Could not evaluate:" + ex.getClass() + ":" + ex.getMessage() + "\n" + sb, ex);
1.234 + return null;
1.235 + }
1.236 + }
1.237 +
1.238 + private static void defineAtoB(ScriptEngine js) throws ScriptException {
1.239 + js.eval("atob = function(s) { return new String(org.apidesign.vm4brwsr.ResourcesTest.parseBase64Binary(s)); }");
1.240 + }
1.241 +
1.242 + Object invokeMethod(Object obj, String method, Object... params) throws ScriptException, NoSuchMethodException {
1.243 + return code.invokeMethod(obj, method, params);
1.244 + }
1.245 +
1.246 + Object invokeFunction(String methodName, Object... args) throws ScriptException, NoSuchMethodException {
1.247 + return code.invokeFunction(methodName, args);
1.248 + }
1.249 +
1.250 + static StringBuilder dumpJS(CharSequence sb) throws IOException {
1.251 + File f = File.createTempFile("execution", ".js");
1.252 + FileWriter w = new FileWriter(f);
1.253 + w.append(sb);
1.254 + w.close();
1.255 + return new StringBuilder(f.getPath());
1.256 + }
1.257 +
1.258 + @Override
1.259 + public String toString() {
1.260 + try {
1.261 + return dumpJS(codeSeq).toString();
1.262 + } catch (IOException ex) {
1.263 + return ex.toString();
1.264 + }
1.265 + }
1.266 +
1.267 + final CharSequence codeSeq() {
1.268 + return codeSeq;
1.269 + }
1.270 +
1.271 + private static class EmulationResources implements Bck2Brwsr.Resources {
1.272 + @Override
1.273 + public InputStream get(String name) throws IOException {
1.274 + if ("java/net/URI.class".equals(name)) {
1.275 + // skip
1.276 + return null;
1.277 + }
1.278 + if ("java/net/URLConnection.class".equals(name)) {
1.279 + // skip
1.280 + return null;
1.281 + }
1.282 + if ("java/lang/System.class".equals(name)) {
1.283 + // skip
1.284 + return null;
1.285 + }
1.286 + Enumeration<URL> en = TestVM.class.getClassLoader().getResources(name);
1.287 + URL u = null;
1.288 + while (en.hasMoreElements()) {
1.289 + u = en.nextElement();
1.290 + }
1.291 + if (u == null) {
1.292 + throw new IOException("Can't find " + name);
1.293 + }
1.294 + if (u.toExternalForm().contains("rt.jar!")) {
1.295 + throw new IOException("No emulation for " + u);
1.296 + }
1.297 + return u.openStream();
1.298 + }
1.299 + }
1.300 +}