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