Using @Compare tests to make sure the classes are processed by the AOT infrastructure.
1.1 --- a/rt/vm8/pom.xml Sat Sep 13 14:19:43 2014 +0200
1.2 +++ b/rt/vm8/pom.xml Sat Sep 13 14:37:49 2014 +0200
1.3 @@ -63,5 +63,18 @@
1.4 <version>${project.version}</version>
1.5 <scope>test</scope>
1.6 </dependency>
1.7 + <dependency>
1.8 + <groupId>org.apidesign.bck2brwsr</groupId>
1.9 + <artifactId>vmtest</artifactId>
1.10 + <version>${project.version}</version>
1.11 + <scope>test</scope>
1.12 + <type>jar</type>
1.13 + </dependency>
1.14 + <dependency>
1.15 + <groupId>${project.groupId}</groupId>
1.16 + <artifactId>launcher.http</artifactId>
1.17 + <version>${project.version}</version>
1.18 + <scope>test</scope>
1.19 + </dependency>
1.20 </dependencies>
1.21 </project>
1.22 \ No newline at end of file
2.1 --- a/rt/vm8/src/test/java/org/apidesign/bck2brwsr/vm8/DefaultsTest.java Sat Sep 13 14:19:43 2014 +0200
2.2 +++ b/rt/vm8/src/test/java/org/apidesign/bck2brwsr/vm8/DefaultsTest.java Sat Sep 13 14:37:49 2014 +0200
2.3 @@ -17,40 +17,24 @@
2.4 */
2.5 package org.apidesign.bck2brwsr.vm8;
2.6
2.7 +import org.apidesign.bck2brwsr.vmtest.Compare;
2.8 +import org.apidesign.bck2brwsr.vmtest.VMTest;
2.9 import org.testng.annotations.AfterClass;
2.10 import org.testng.annotations.BeforeClass;
2.11 +import org.testng.annotations.Factory;
2.12 import org.testng.annotations.Test;
2.13
2.14 public class DefaultsTest {
2.15 - private static TestVM code;
2.16 -
2.17 - @Test public void callStatic() throws Exception {
2.18 - Object js = code.execCode("Value from static method",
2.19 - Defaults.class, "staticValue__I", 42
2.20 - );
2.21 + @Compare public int callStatic() throws Exception {
2.22 + return Defaults.defaultValue();
2.23 }
2.24
2.25 - @Test public void defaultValue() throws Exception {
2.26 - Object js = code.execCode("Value from interface",
2.27 - Defaults.class, "defaultValue__I", 42
2.28 - );
2.29 + @Compare public int overridenValue() throws Exception {
2.30 + return Defaults.myValue();
2.31 }
2.32
2.33 - @Test public void overridenValue() throws Exception {
2.34 - Object js = code.execCode("Value from class",
2.35 - Defaults.class, "myValue__I", 7
2.36 - );
2.37 - }
2.38 -
2.39 - @BeforeClass
2.40 - public static void compileTheCode() throws Exception {
2.41 - code = TestVM.compileClass(
2.42 - "org/apidesign/bck2brwsr/vm8/Defaults");
2.43 - }
2.44 -
2.45 - @AfterClass
2.46 - public static void releaseTheCode() {
2.47 - code = null;
2.48 + @Factory public static Object[] create() {
2.49 + return VMTest.create(DefaultsTest.class);
2.50 }
2.51
2.52 }
3.1 --- a/rt/vm8/src/test/java/org/apidesign/bck2brwsr/vm8/InvokeDynamic.java Sat Sep 13 14:19:43 2014 +0200
3.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
3.3 @@ -1,59 +0,0 @@
3.4 -/**
3.5 - * Back 2 Browser Bytecode Translator
3.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
3.7 - *
3.8 - * This program is free software: you can redistribute it and/or modify
3.9 - * it under the terms of the GNU General Public License as published by
3.10 - * the Free Software Foundation, version 2 of the License.
3.11 - *
3.12 - * This program is distributed in the hope that it will be useful,
3.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
3.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3.15 - * GNU General Public License for more details.
3.16 - *
3.17 - * You should have received a copy of the GNU General Public License
3.18 - * along with this program. Look for COPYING file in the top folder.
3.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
3.20 - */
3.21 -package org.apidesign.bck2brwsr.vm8;
3.22 -
3.23 -import java.lang.invoke.CallSite;
3.24 -import java.lang.invoke.ConstantCallSite;
3.25 -import java.lang.invoke.MethodHandles;
3.26 -import java.lang.invoke.MethodType;
3.27 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
3.28 -
3.29 -public class InvokeDynamic {
3.30 -
3.31 - public static String dynamicSay() {
3.32 - return TEST_dynamic_boot1(new InvokeDynamic());
3.33 - }
3.34 -
3.35 - private static String TEST_dynamic_boot1(InvokeDynamic instance) {
3.36 - throw new IllegalStateException("Can't touch this");
3.37 - }
3.38 -
3.39 - public static CallSite boot1(MethodHandles.Lookup lookup, String name, MethodType type) {
3.40 - assertReal("1st parameter lookup", lookup);
3.41 - assertReal("2nd parameter name", name);
3.42 - assertReal("3rd parameter type", type);
3.43 - assert lookup.lookupClass() == InvokeDynamic.class : "We are making the lookup: " + lookup.lookupClass();
3.44 - try {
3.45 - return new ConstantCallSite(lookup.findVirtual(InvokeDynamic.class, "instance_sayHello", MethodType.methodType(String.class)));
3.46 - } catch (NoSuchMethodException | IllegalAccessException e) {
3.47 - throw new IllegalStateException(e);
3.48 - }
3.49 - }
3.50 -
3.51 - @JavaScriptBody(args = { "msg", "value" }, body =
3.52 - "if (!value) throw msg + ' value: ' + value;"
3.53 - )
3.54 - private static void assertReal(String msg, Object value) {
3.55 - assert value != null : msg;
3.56 - System.err.println(msg + " value: " + value);
3.57 - }
3.58 -
3.59 - public String instance_sayHello() {
3.60 - return "Hello from Dynamic!";
3.61 - }
3.62 -}
3.63 \ No newline at end of file
4.1 --- a/rt/vm8/src/test/java/org/apidesign/bck2brwsr/vm8/InvokeDynamicTest.java Sat Sep 13 14:19:43 2014 +0200
4.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
4.3 @@ -1,189 +0,0 @@
4.4 -/**
4.5 - * Back 2 Browser Bytecode Translator
4.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
4.7 - *
4.8 - * This program is free software: you can redistribute it and/or modify
4.9 - * it under the terms of the GNU General Public License as published by
4.10 - * the Free Software Foundation, version 2 of the License.
4.11 - *
4.12 - * This program is distributed in the hope that it will be useful,
4.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
4.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4.15 - * GNU General Public License for more details.
4.16 - *
4.17 - * You should have received a copy of the GNU General Public License
4.18 - * along with this program. Look for COPYING file in the top folder.
4.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
4.20 - */
4.21 -package org.apidesign.bck2brwsr.vm8;
4.22 -
4.23 -import java.io.ByteArrayInputStream;
4.24 -import java.io.IOException;
4.25 -import java.io.InputStream;
4.26 -import java.lang.invoke.CallSite;
4.27 -import java.lang.invoke.MethodHandles;
4.28 -import java.lang.invoke.MethodType;
4.29 -import java.lang.reflect.Method;
4.30 -import java.net.URL;
4.31 -import java.util.Enumeration;
4.32 -import org.apidesign.vm4brwsr.Bck2Brwsr;
4.33 -import org.objectweb.asm.ClassReader;
4.34 -import org.objectweb.asm.ClassVisitor;
4.35 -import org.objectweb.asm.ClassWriter;
4.36 -import org.objectweb.asm.Handle;
4.37 -import org.objectweb.asm.MethodVisitor;
4.38 -import org.objectweb.asm.Opcodes;
4.39 -import static org.objectweb.asm.Opcodes.ASM4;
4.40 -import static org.objectweb.asm.Opcodes.INVOKESTATIC;
4.41 -import static org.testng.Assert.*;
4.42 -import org.testng.annotations.AfterClass;
4.43 -import org.testng.annotations.BeforeClass;
4.44 -import org.testng.annotations.Test;
4.45 -
4.46 -/**
4.47 - *
4.48 - * @author Jaroslav Tulach <jtulach@netbeans.org>
4.49 - */
4.50 -public class InvokeDynamicTest {
4.51 - private static Class<?> invokeDynamicClass;
4.52 - private static byte[] invokeDynamicBytes;
4.53 - private static TestVM code;
4.54 -
4.55 - @Test public void simpleDynamicInJava() throws Exception {
4.56 - Method m = invokeDynamicClass.getMethod("dynamicSay");
4.57 - Object ret = m.invoke(m);
4.58 - assertEquals(ret, "Hello from Dynamic!");
4.59 - }
4.60 -
4.61 - /* Well, supporting general invokeDynamic is
4.62 - huge hassle, so giving up. More at
4.63 - http://wiki.apidesign.org/wiki/InvokeDynamic
4.64 -
4.65 - @Test public void simpleDynamicInJS() throws Exception {
4.66 - code().assertExec(
4.67 - "Invoke dynamic can return a value", InvokeDynamic.class,
4.68 - "dynamicSay__Ljava_lang_String_2",
4.69 - "Hello from Dynamic!"
4.70 - );
4.71 - }
4.72 - */
4.73 -
4.74 - private TestVM code() throws Exception {
4.75 - if (code == null) {
4.76 - final EmulResWithInvDyn emul = new EmulResWithInvDyn();
4.77 - code = TestVM.compileClass(
4.78 - null, null, emul,
4.79 - InvokeDynamic.class.getName().replace('.', '/')
4.80 - );
4.81 -
4.82 - assertTrue(emul.loaded, "InvokeDynamic class should be processed!");
4.83 - }
4.84 - return code;
4.85 - }
4.86 -
4.87 - @AfterClass
4.88 - public static void releaseTheCode() {
4.89 - code = null;
4.90 - }
4.91 -
4.92 - //
4.93 - // the following code is inspired by
4.94 - // https://code.google.com/p/indy-maven-plugin/
4.95 - // which I don't want to use, as it is not in a public repository
4.96 - //
4.97 - @BeforeClass
4.98 - public static void prepareClass() throws Exception {
4.99 - InputStream is = InvokeDynamic.class.getResourceAsStream("InvokeDynamic.class");
4.100 - assertNotNull(is, "Class found");
4.101 -
4.102 - ClassReader reader = new ClassReader(is) {
4.103 - @Override
4.104 - public short readShort(int index) {
4.105 - if (index == 6) {
4.106 - return Opcodes.V1_7;
4.107 - }
4.108 - return super.readShort(index);
4.109 - }
4.110 - };
4.111 - ClassWriter writer = new ClassWriter(reader, 0);
4.112 -
4.113 - reader.accept(
4.114 - new ClassVisitor(ASM4, writer) {
4.115 - @Override
4.116 - public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions
4.117 - ) {
4.118 - MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
4.119 - return new InvokeDynamicProcessor(mv);
4.120 - }
4.121 - },
4.122 - 0);
4.123 - is.close();
4.124 - invokeDynamicBytes = writer.toByteArray();
4.125 - final boolean[] loaded = { false };
4.126 - ClassLoader l = new ClassLoader() {
4.127 - @Override
4.128 - public Class<?> loadClass(String name) throws ClassNotFoundException {
4.129 - if (name.equals(InvokeDynamic.class.getName())) {
4.130 - loaded[0] = true;
4.131 - return defineClass(name, invokeDynamicBytes, 0, invokeDynamicBytes.length);
4.132 - }
4.133 - return super.loadClass(name);
4.134 - }
4.135 - };
4.136 - invokeDynamicClass = l.loadClass(InvokeDynamic.class.getName());
4.137 - assertTrue(loaded[0], "InvokeDynamic class should be loaded!");
4.138 - }
4.139 -
4.140 -
4.141 - private static class InvokeDynamicProcessor extends MethodVisitor {
4.142 - InvokeDynamicProcessor(MethodVisitor mv) {
4.143 - super(ASM4, mv);
4.144 - }
4.145 -
4.146 - @Override
4.147 - public void visitMethodInsn(int opcode, String owner, String name, String desc) {
4.148 - if (opcode == INVOKESTATIC) {
4.149 - if (name.startsWith("TEST_dynamic_")) {
4.150 - final String shortName = name.substring(13);
4.151 - Handle mh = new Handle(
4.152 - Opcodes.H_INVOKESTATIC, owner, shortName,
4.153 - MethodType.methodType(
4.154 - CallSite.class,
4.155 - MethodHandles.Lookup.class,
4.156 - String.class,
4.157 - MethodType.class
4.158 - ).toMethodDescriptorString()
4.159 - );
4.160 - super.visitInvokeDynamicInsn(shortName, desc, mh);
4.161 - return;
4.162 - }
4.163 - }
4.164 - super.visitMethodInsn(opcode, owner, name, desc);
4.165 - }
4.166 - }
4.167 -
4.168 - private static class EmulResWithInvDyn implements Bck2Brwsr.Resources {
4.169 - boolean loaded;
4.170 -
4.171 - @Override
4.172 - public InputStream get(String name) throws IOException {
4.173 - if ("org/apidesign/bck2brwsr/vm8/InvokeDynamic.class".equals(name)) {
4.174 - loaded = true;
4.175 - return new ByteArrayInputStream(invokeDynamicBytes);
4.176 - }
4.177 - Enumeration<URL> en = InvokeDynamicTest.class.getClassLoader().getResources(name);
4.178 - URL u = null;
4.179 - while (en.hasMoreElements()) {
4.180 - u = en.nextElement();
4.181 - }
4.182 - if (u == null) {
4.183 - throw new IOException("Can't find " + name);
4.184 - }
4.185 - if (u.toExternalForm().contains("rt.jar!")) {
4.186 - throw new IOException("No emulation for " + u);
4.187 - }
4.188 - return u.openStream();
4.189 - }
4.190 - }
4.191 -
4.192 -}
5.1 --- a/rt/vm8/src/test/java/org/apidesign/bck2brwsr/vm8/LambdasTest.java Sat Sep 13 14:19:43 2014 +0200
5.2 +++ b/rt/vm8/src/test/java/org/apidesign/bck2brwsr/vm8/LambdasTest.java Sat Sep 13 14:37:49 2014 +0200
5.3 @@ -17,36 +17,21 @@
5.4 */
5.5 package org.apidesign.bck2brwsr.vm8;
5.6
5.7 -import org.testng.annotations.BeforeClass;
5.8 -import org.testng.annotations.AfterClass;
5.9 -import org.testng.annotations.Test;
5.10 +import org.apidesign.bck2brwsr.vmtest.Compare;
5.11 +import org.apidesign.bck2brwsr.vmtest.VMTest;
5.12 +import org.testng.annotations.Factory;
5.13
5.14 /**
5.15 *
5.16 * @author Jaroslav Tulach <jtulach@netbeans.org>
5.17 */
5.18 public class LambdasTest {
5.19 - private static TestVM code;
5.20 -
5.21 - @Test public void verifyJSTime() throws Exception {
5.22 - String exp = Lambdas.compound();
5.23 -
5.24 - Object js = code.execCode("Get js time",
5.25 - Lambdas.class, "compound__Ljava_lang_String_2",
5.26 - exp
5.27 - );
5.28 + @Compare public String StringverifyJSTime() throws Exception {
5.29 + return Lambdas.compound();
5.30 }
5.31
5.32 -
5.33 - @BeforeClass
5.34 - public static void compileTheCode() throws Exception {
5.35 - code = TestVM.compileClass(
5.36 - "org/apidesign/bck2brwsr/vm8/Lambdas");
5.37 + @Factory public static Object[] create() {
5.38 + return VMTest.create(LambdasTest.class);
5.39 }
5.40 - @AfterClass
5.41 - public static void releaseTheCode() {
5.42 - code = null;
5.43 - }
5.44 -
5.45 }
5.46
6.1 --- a/rt/vm8/src/test/java/org/apidesign/bck2brwsr/vm8/TestVM.java Sat Sep 13 14:19:43 2014 +0200
6.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
6.3 @@ -1,296 +0,0 @@
6.4 -/**
6.5 - * Back 2 Browser Bytecode Translator
6.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
6.7 - *
6.8 - * This program is free software: you can redistribute it and/or modify
6.9 - * it under the terms of the GNU General Public License as published by
6.10 - * the Free Software Foundation, version 2 of the License.
6.11 - *
6.12 - * This program is distributed in the hope that it will be useful,
6.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
6.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6.15 - * GNU General Public License for more details.
6.16 - *
6.17 - * You should have received a copy of the GNU General Public License
6.18 - * along with this program. Look for COPYING file in the top folder.
6.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
6.20 - */
6.21 -package org.apidesign.bck2brwsr.vm8;
6.22 -
6.23 -import java.io.ByteArrayInputStream;
6.24 -import java.io.File;
6.25 -import java.io.FileWriter;
6.26 -import java.io.IOException;
6.27 -import java.io.InputStream;
6.28 -import java.net.URL;
6.29 -import java.util.ArrayList;
6.30 -import java.util.Arrays;
6.31 -import java.util.Enumeration;
6.32 -import java.util.HashSet;
6.33 -import java.util.List;
6.34 -import java.util.Set;
6.35 -import javax.script.Invocable;
6.36 -import javax.script.ScriptContext;
6.37 -import javax.script.ScriptEngine;
6.38 -import javax.script.ScriptEngineManager;
6.39 -import javax.script.ScriptException;
6.40 -import org.apidesign.vm4brwsr.Bck2Brwsr;
6.41 -import org.apidesign.vm4brwsr.ObfuscationLevel;
6.42 -import static org.testng.Assert.*;
6.43 -
6.44 -public final class TestVM {
6.45 - private final Invocable code;
6.46 - private final CharSequence codeSeq;
6.47 - private final Object bck2brwsr;
6.48 - private BytesLoader resources;
6.49 -
6.50 -
6.51 - private TestVM(Invocable code, CharSequence codeSeq) throws ScriptException, NoSuchMethodException {
6.52 - this.code = code;
6.53 - this.codeSeq = codeSeq;
6.54 - this.bck2brwsr = ((ScriptEngine)code).eval("bck2brwsr(function(n) { return loader.get(n); })");
6.55 - ((ScriptEngine)code).getContext().setAttribute("loader", this, ScriptContext.ENGINE_SCOPE);
6.56 - }
6.57 -
6.58 - public void register(BytesLoader res) {
6.59 - this.resources = res;
6.60 - }
6.61 -
6.62 - public byte[] get(String res) throws IOException {
6.63 - return resources != null ? resources.get(res) : null;
6.64 - }
6.65 -
6.66 - public Object execCode(
6.67 - String msg, Class<?> clazz, String method,
6.68 - Object expRes, Object... args
6.69 - ) throws Exception {
6.70 - Object ret = null;
6.71 - try {
6.72 - ret = code.invokeMethod(bck2brwsr, "loadClass", clazz.getName());
6.73 - List<Object> ma = new ArrayList<>();
6.74 - ma.add(method);
6.75 - ma.addAll(Arrays.asList(args));
6.76 - ret = code.invokeMethod(ret, "invoke", ma.toArray());
6.77 - } catch (ScriptException ex) {
6.78 - fail("Execution failed in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
6.79 - } catch (NoSuchMethodException ex) {
6.80 - fail("Cannot find method in " + dumpJS(codeSeq), ex);
6.81 - }
6.82 - if (ret == null && expRes == null) {
6.83 - return null;
6.84 - }
6.85 - if (expRes != null && expRes.equals(ret)) {
6.86 - return null;
6.87 - }
6.88 - if (expRes instanceof Number) {
6.89 - // in case of Long it is necessary convert it to number
6.90 - // since the Long is represented by two numbers in JavaScript
6.91 - try {
6.92 - final Object toFP = ((ScriptEngine)code).eval("Number.prototype.toFP");
6.93 - if (ret instanceof Long) {
6.94 - ret = code.invokeMethod(toFP, "call", ret);
6.95 - }
6.96 - ret = code.invokeFunction("Number", ret);
6.97 - } catch (ScriptException ex) {
6.98 - fail("Conversion to number failed in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
6.99 - } catch (NoSuchMethodException ex) {
6.100 - fail("Cannot find global Number(x) function in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
6.101 - }
6.102 - }
6.103 - return ret;
6.104 - }
6.105 -
6.106 - void assertExec(
6.107 - String msg, Class clazz, String method, Object expRes, Object... args
6.108 - ) throws Exception {
6.109 - Object ret = execCode(msg, clazz, method, expRes, args);
6.110 - if (ret == null) {
6.111 - return;
6.112 - }
6.113 - if (expRes instanceof Integer && ret instanceof Double) {
6.114 - expRes = ((Integer)expRes).doubleValue();
6.115 - }
6.116 - if (expRes != null && expRes.equals(ret)) {
6.117 - return;
6.118 - }
6.119 - assertEquals(ret, expRes, msg + "was: " + ret + "\n" + dumpJS(codeSeq));
6.120 - }
6.121 -
6.122 - static TestVM compileClass(String... names) throws ScriptException, IOException {
6.123 - return compileClass(null, names);
6.124 - }
6.125 -
6.126 - static TestVM compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
6.127 - return compileClass(sb, null, names);
6.128 - }
6.129 -
6.130 - static TestVM compileClass(StringBuilder sb, ScriptEngine[] eng, String... names) throws ScriptException, IOException {
6.131 - return compileClass(sb, eng, new EmulationResources(), names);
6.132 - }
6.133 - static TestVM compileClass(
6.134 - StringBuilder sb, ScriptEngine[] eng,
6.135 - Bck2Brwsr.Resources resources, String... names
6.136 - ) throws ScriptException, IOException {
6.137 - if (sb == null) {
6.138 - sb = new StringBuilder();
6.139 - }
6.140 - Bck2Brwsr.generate(sb, resources, names);
6.141 - ScriptEngineManager sem = new ScriptEngineManager();
6.142 - ScriptEngine js = sem.getEngineByExtension("js");
6.143 - if (eng != null) {
6.144 - eng[0] = js;
6.145 - }
6.146 - try {
6.147 - Object res = js.eval(sb.toString());
6.148 - assertTrue(js instanceof Invocable, "It is invocable object: " + res);
6.149 - return new TestVM((Invocable) js, sb);
6.150 - } catch (Exception ex) {
6.151 - if (sb.length() > 2000) {
6.152 - sb = dumpJS(sb);
6.153 - }
6.154 - fail("Could not evaluate:" + ex.getClass() + ":" + ex.getMessage() + "\n" + sb, ex);
6.155 - return null;
6.156 - }
6.157 - }
6.158 -
6.159 - static TestVM compileClassAsExtension(
6.160 - StringBuilder sb, ScriptEngine[] eng,
6.161 - String name, final String resourceName, final String resourceContent
6.162 - ) throws ScriptException, IOException {
6.163 - return compileClassesAsExtension(sb, eng, resourceName, resourceContent, name);
6.164 - }
6.165 - static TestVM compileClassesAsExtension(
6.166 - StringBuilder sb, ScriptEngine[] eng,
6.167 - final String resourceName, final String resourceContent, String... names
6.168 - ) throws ScriptException, IOException {
6.169 - if (sb == null) {
6.170 - sb = new StringBuilder();
6.171 - }
6.172 - if (eng[0] == null) {
6.173 - ScriptEngineManager sem = new ScriptEngineManager();
6.174 - ScriptEngine js = sem.getEngineByExtension("js");
6.175 - eng[0] = js;
6.176 - Bck2Brwsr.generate(sb, new EmulationResources());
6.177 - }
6.178 - Set<String> exp = new HashSet<String>();
6.179 - for (String n : names) {
6.180 - int last = n.lastIndexOf('/');
6.181 - exp.add(n.substring(0, last + 1));
6.182 - }
6.183 - Bck2Brwsr b2b = Bck2Brwsr.newCompiler().
6.184 - resources(new EmulationResources() {
6.185 - @Override
6.186 - public InputStream get(String name) throws IOException {
6.187 - if (name.equals(resourceName)) {
6.188 - return new ByteArrayInputStream(resourceContent.getBytes("UTF-8"));
6.189 - }
6.190 - return super.get(name);
6.191 - }
6.192 - }).
6.193 - addClasses(names).
6.194 - addResources("org/apidesign/vm4brwsr/obj.js").
6.195 - addExported(exp.toArray(new String[0])).
6.196 - obfuscation(ObfuscationLevel.FULL).
6.197 - library();
6.198 - if (resourceName != null) {
6.199 - b2b = b2b.addResources(resourceName);
6.200 - }
6.201 - b2b.generate(sb);
6.202 - try {
6.203 - defineAtoB(eng[0]);
6.204 - Object res = eng[0].eval(sb.toString());
6.205 - assertTrue(eng[0] instanceof Invocable, "It is invocable object: " + res);
6.206 - return new TestVM((Invocable) eng[0], sb);
6.207 - } catch (Exception ex) {
6.208 - if (sb.length() > 2000) {
6.209 - sb = dumpJS(sb);
6.210 - }
6.211 - fail("Could not evaluate:" + ex.getClass() + ":" + ex.getMessage() + "\n" + sb, ex);
6.212 - return null;
6.213 - }
6.214 - }
6.215 -
6.216 - static TestVM compileClassAndResources(StringBuilder sb, ScriptEngine[] eng, String name, String... resources) throws ScriptException, IOException {
6.217 - if (sb == null) {
6.218 - sb = new StringBuilder();
6.219 - }
6.220 - Bck2Brwsr b2b = Bck2Brwsr.newCompiler().
6.221 - resources(new EmulationResources()).
6.222 - addRootClasses(name).
6.223 - addResources(resources);
6.224 - b2b.generate(sb);
6.225 - ScriptEngineManager sem = new ScriptEngineManager();
6.226 - ScriptEngine js = sem.getEngineByExtension("js");
6.227 - if (eng != null) {
6.228 - eng[0] = js;
6.229 - }
6.230 - try {
6.231 - defineAtoB(js);
6.232 -
6.233 - Object res = js.eval(sb.toString());
6.234 - assertTrue(js instanceof Invocable, "It is invocable object: " + res);
6.235 - return new TestVM((Invocable) js, sb);
6.236 - } catch (Exception ex) {
6.237 - if (sb.length() > 2000) {
6.238 - sb = dumpJS(sb);
6.239 - }
6.240 - fail("Could not evaluate:" + ex.getClass() + ":" + ex.getMessage() + "\n" + sb, ex);
6.241 - return null;
6.242 - }
6.243 - }
6.244 -
6.245 - private static void defineAtoB(ScriptEngine js) throws ScriptException {
6.246 - js.eval("atob = function(s) { return new String(org.apidesign.vm4brwsr.ResourcesTest.parseBase64Binary(s)); }");
6.247 - }
6.248 -
6.249 -// Object loadClass(String loadClass, String name) throws ScriptException, NoSuchMethodException {
6.250 -// return code.invokeMethod(bck2brwsr, "loadClass", Exceptions.class.getName());
6.251 -// }
6.252 -
6.253 - Object invokeMethod(Object obj, String method, Object... params) throws ScriptException, NoSuchMethodException {
6.254 - return code.invokeMethod(obj, method, params);
6.255 - }
6.256 -
6.257 - Object invokeFunction(String methodName, Object... args) throws ScriptException, NoSuchMethodException {
6.258 - return code.invokeFunction(methodName, args);
6.259 - }
6.260 -
6.261 - static StringBuilder dumpJS(CharSequence sb) throws IOException {
6.262 - File f = File.createTempFile("execution", ".js");
6.263 - FileWriter w = new FileWriter(f);
6.264 - w.append(sb);
6.265 - w.close();
6.266 - return new StringBuilder(f.getPath());
6.267 - }
6.268 -
6.269 - @Override
6.270 - public String toString() {
6.271 - try {
6.272 - return dumpJS(codeSeq).toString();
6.273 - } catch (IOException ex) {
6.274 - return ex.toString();
6.275 - }
6.276 - }
6.277 -
6.278 - final CharSequence codeSeq() {
6.279 - return codeSeq;
6.280 - }
6.281 -
6.282 - private static class EmulationResources implements Bck2Brwsr.Resources {
6.283 - @Override
6.284 - public InputStream get(String name) throws IOException {
6.285 - Enumeration<URL> en = TestVM.class.getClassLoader().getResources(name);
6.286 - URL u = null;
6.287 - while (en.hasMoreElements()) {
6.288 - u = en.nextElement();
6.289 - }
6.290 - if (u == null) {
6.291 - throw new IOException("Can't find " + name);
6.292 - }
6.293 - if (u.toExternalForm().contains("rt.jar!")) {
6.294 - throw new IOException("No emulation for " + u);
6.295 - }
6.296 - return u.openStream();
6.297 - }
6.298 - }
6.299 -}