1.1 --- a/rt/flow/src/test/java/org/apidesign/bck2brwsr/flow/TestVM.java Wed Mar 11 18:58:39 2015 +0100
1.2 +++ b/rt/flow/src/test/java/org/apidesign/bck2brwsr/flow/TestVM.java Thu Mar 12 12:07:54 2015 +0100
1.3 @@ -42,16 +42,15 @@
1.4 import static org.testng.Assert.*;
1.5
1.6 public final class TestVM {
1.7 - private final Invocable code;
1.8 + private final ScriptEngine js;
1.9 + private Invocable code;
1.10 private final CharSequence codeSeq;
1.11 - private final Object bck2brwsr;
1.12 + private Object bck2brwsr;
1.13
1.14
1.15 - private TestVM(Invocable code, CharSequence codeSeq) throws ScriptException, NoSuchMethodException {
1.16 - this.code = code;
1.17 + private TestVM(ScriptEngine js, CharSequence codeSeq) throws ScriptException, NoSuchMethodException {
1.18 + this.js = js;
1.19 this.codeSeq = codeSeq;
1.20 - this.bck2brwsr = ((ScriptEngine)code).eval("bck2brwsr(function(n) { return loader.get(n); })");
1.21 - ((ScriptEngine)code).getContext().setAttribute("loader", this, ScriptContext.ENGINE_SCOPE);
1.22 }
1.23
1.24 public Object execCode(
1.25 @@ -60,11 +59,11 @@
1.26 ) throws Exception {
1.27 Object ret = null;
1.28 try {
1.29 - ret = code.invokeMethod(bck2brwsr, "loadClass", clazz.getName());
1.30 + ret = getCode().invokeMethod(bck2brwsr, "loadClass", clazz.getName());
1.31 List<Object> ma = new ArrayList<Object>();
1.32 ma.add(method);
1.33 ma.addAll(Arrays.asList(args));
1.34 - ret = code.invokeMethod(ret, "invoke", ma.toArray());
1.35 + ret = getCode().invokeMethod(ret, "invoke", ma.toArray());
1.36 } catch (ScriptException ex) {
1.37 fail("Execution failed in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
1.38 } catch (NoSuchMethodException ex) {
1.39 @@ -80,11 +79,11 @@
1.40 // in case of Long it is necessary convert it to number
1.41 // since the Long is represented by two numbers in JavaScript
1.42 try {
1.43 - final Object toFP = ((ScriptEngine)code).eval("Number.prototype.toFP");
1.44 + final Object toFP = ((ScriptEngine)getCode()).eval("Number.prototype.toFP");
1.45 if (ret instanceof Long) {
1.46 - ret = code.invokeMethod(toFP, "call", ret);
1.47 + ret = getCode().invokeMethod(toFP, "call", ret);
1.48 }
1.49 - ret = code.invokeFunction("Number", ret);
1.50 + ret = getCode().invokeFunction("Number", ret);
1.51 } catch (ScriptException ex) {
1.52 fail("Conversion to number failed in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
1.53 } catch (NoSuchMethodException ex) {
1.54 @@ -144,96 +143,7 @@
1.55 eng[0] = js;
1.56 }
1.57 try {
1.58 - Object res = js.eval(sb.toString());
1.59 - assertTrue(js instanceof Invocable, "It is invocable object: " + res);
1.60 - return new TestVM((Invocable) js, sb);
1.61 - } catch (Exception ex) {
1.62 - if (sb.length() > 2000) {
1.63 - sb = dumpJS(sb);
1.64 - }
1.65 - fail("Could not evaluate:" + ex.getClass() + ":" + ex.getMessage() + "\n" + sb, ex);
1.66 - return null;
1.67 - }
1.68 - }
1.69 -
1.70 - static TestVM compileClassAsExtension(
1.71 - StringBuilder sb, ScriptEngine[] eng,
1.72 - String name, final String resourceName, final String resourceContent
1.73 - ) throws ScriptException, IOException {
1.74 - return compileClassesAsExtension(sb, eng, resourceName, resourceContent, name);
1.75 - }
1.76 - static TestVM compileClassesAsExtension(
1.77 - StringBuilder sb, ScriptEngine[] eng,
1.78 - final String resourceName, final String resourceContent, String... names
1.79 - ) throws ScriptException, IOException {
1.80 - if (sb == null) {
1.81 - sb = new StringBuilder();
1.82 - }
1.83 - if (eng[0] == null) {
1.84 - ScriptEngineManager sem = new ScriptEngineManager();
1.85 - ScriptEngine js = sem.getEngineByExtension("js");
1.86 - eng[0] = js;
1.87 - Bck2Brwsr.newCompiler().resources(new EmulationResources())
1.88 - .obfuscation(ObfuscationLevel.NONE).generate(sb);
1.89 - }
1.90 - Set<String> exp = new HashSet<String>();
1.91 - for (String n : names) {
1.92 - int last = n.lastIndexOf('/');
1.93 - exp.add(n.substring(0, last + 1));
1.94 - }
1.95 - Bck2Brwsr b2b = Bck2Brwsr.newCompiler().
1.96 - resources(new EmulationResources() {
1.97 - @Override
1.98 - public InputStream get(String name) throws IOException {
1.99 - if (name.equals(resourceName)) {
1.100 - return new ByteArrayInputStream(resourceContent.getBytes("UTF-8"));
1.101 - }
1.102 - return super.get(name);
1.103 - }
1.104 - }).
1.105 - addClasses(names).
1.106 - addResources("org/apidesign/vm4brwsr/obj.js").
1.107 - addExported(exp.toArray(new String[0])).
1.108 - obfuscation(ObfuscationLevel.FULL).
1.109 - library();
1.110 - if (resourceName != null) {
1.111 - b2b = b2b.addResources(resourceName);
1.112 - }
1.113 - b2b.generate(sb);
1.114 - try {
1.115 - defineAtoB(eng[0]);
1.116 - Object res = eng[0].eval(sb.toString());
1.117 - assertTrue(eng[0] instanceof Invocable, "It is invocable object: " + res);
1.118 - return new TestVM((Invocable) eng[0], sb);
1.119 - } catch (Exception ex) {
1.120 - if (sb.length() > 2000) {
1.121 - sb = dumpJS(sb);
1.122 - }
1.123 - fail("Could not evaluate:" + ex.getClass() + ":" + ex.getMessage() + "\n" + sb, ex);
1.124 - return null;
1.125 - }
1.126 - }
1.127 -
1.128 - static TestVM compileClassAndResources(StringBuilder sb, ScriptEngine[] eng, String name, String... resources) throws ScriptException, IOException {
1.129 - if (sb == null) {
1.130 - sb = new StringBuilder();
1.131 - }
1.132 - Bck2Brwsr b2b = Bck2Brwsr.newCompiler().
1.133 - resources(new EmulationResources()).
1.134 - addRootClasses(name).
1.135 - addResources(resources);
1.136 - b2b.generate(sb);
1.137 - ScriptEngineManager sem = new ScriptEngineManager();
1.138 - ScriptEngine js = sem.getEngineByExtension("js");
1.139 - if (eng != null) {
1.140 - eng[0] = js;
1.141 - }
1.142 - try {
1.143 - defineAtoB(js);
1.144 -
1.145 - Object res = js.eval(sb.toString());
1.146 - assertTrue(js instanceof Invocable, "It is invocable object: " + res);
1.147 - return new TestVM((Invocable) js, sb);
1.148 + return new TestVM(js, sb);
1.149 } catch (Exception ex) {
1.150 if (sb.length() > 2000) {
1.151 sb = dumpJS(sb);
1.152 @@ -243,20 +153,16 @@
1.153 }
1.154 }
1.155
1.156 - private static void defineAtoB(ScriptEngine js) throws ScriptException {
1.157 - js.eval("atob = function(s) { return new String(org.apidesign.vm4brwsr.ResourcesTest.parseBase64Binary(s)); }");
1.158 - }
1.159 -
1.160 Object loadClass(String loadClass, String name) throws ScriptException, NoSuchMethodException {
1.161 - return code.invokeMethod(bck2brwsr, "loadClass", LoopControl.class.getName());
1.162 + return getCode().invokeMethod(bck2brwsr, "loadClass", LoopControl.class.getName());
1.163 }
1.164
1.165 Object invokeMethod(Object obj, String method, Object... params) throws ScriptException, NoSuchMethodException {
1.166 - return code.invokeMethod(obj, method, params);
1.167 + return getCode().invokeMethod(obj, method, params);
1.168 }
1.169
1.170 Object invokeFunction(String methodName, Object... args) throws ScriptException, NoSuchMethodException {
1.171 - return code.invokeFunction(methodName, args);
1.172 + return getCode().invokeFunction(methodName, args);
1.173 }
1.174
1.175 static StringBuilder dumpJS(CharSequence sb) throws IOException {
1.176 @@ -279,6 +185,19 @@
1.177 final CharSequence codeSeq() {
1.178 return codeSeq;
1.179 }
1.180 +
1.181 + /**
1.182 + * @return the code
1.183 + */
1.184 + private Invocable getCode() throws ScriptException {
1.185 + if (code == null) {
1.186 + Object res = js.eval(code.toString());
1.187 + code = (Invocable) js;
1.188 + this.bck2brwsr = ((ScriptEngine)code).eval("bck2brwsr(function(n) { return loader.get(n); })");
1.189 + ((ScriptEngine)code).getContext().setAttribute("loader", this, ScriptContext.ENGINE_SCOPE);
1.190 + }
1.191 + return (Invocable)code;
1.192 + }
1.193
1.194 private static class EmulationResources implements Bck2Brwsr.Resources {
1.195 @Override