1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/VMLazyTest.java Sun Nov 25 21:24:03 2012 +0100
1.3 @@ -0,0 +1,124 @@
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;
1.22 +
1.23 +import java.io.IOException;
1.24 +import java.io.InputStream;
1.25 +import javax.script.Invocable;
1.26 +import javax.script.ScriptContext;
1.27 +import javax.script.ScriptEngine;
1.28 +import javax.script.ScriptException;
1.29 +import org.testng.annotations.BeforeClass;
1.30 +import static org.testng.Assert.*;
1.31 +import org.testng.annotations.Test;
1.32 +
1.33 +/** Implements loading class by class.
1.34 + *
1.35 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.36 + */
1.37 +public class VMLazyTest {
1.38 +
1.39 +
1.40 + private static CharSequence codeSeq;
1.41 + private static Invocable code;
1.42 +
1.43 + @BeforeClass
1.44 + public void compileTheCode() throws Exception {
1.45 + StringBuilder sb = new StringBuilder();
1.46 +
1.47 + sb.append("\nfunction test(clazz, as, method) {");
1.48 + sb.append("\n var l = new lazyVM();");
1.49 + sb.append("\n var c = l.loadClass(clazz, as);");
1.50 + sb.append("\n return c[method]();");
1.51 + sb.append("\n}");
1.52 +
1.53 +
1.54 + sb.append("\nfunction lazyVM() {");
1.55 + sb.append("\n var self = this;");
1.56 + sb.append("\n this.constructor.prototype.Identity = function(value) {");
1.57 + sb.append("\n var self = this;");
1.58 + sb.append("\n self.value = value;");
1.59 + sb.append("\n self.call = function() { return self.value; };");
1.60 + sb.append("\n };");
1.61 + sb.append("\n");
1.62 + sb.append("\n this.constructor.prototype.loadClass = function(res, name) {");
1.63 + sb.append("\n var script = org_apidesign_vm4brwsr_VMLazy_toJavaScriptLjava_lang_StringAB(loader.get(res + '.class'));");
1.64 +// sb.append("\n throw script.toString();");
1.65 + sb.append("\n new Function(");
1.66 + sb.append("\n 'arguments[0][arguments[1]]=new lazyVM.prototype.Identity(' + script + ').call'");
1.67 + sb.append("\n )(self, name);");
1.68 + sb.append("\n };");
1.69 + sb.append("\n");
1.70 + sb.append("\n}\n");
1.71 +
1.72 + ScriptEngine[] arr = { null };
1.73 + code = StaticMethodTest.compileClass(sb, arr,
1.74 + "org/apidesign/vm4brwsr/VMLazy"
1.75 + );
1.76 + arr[0].getContext().setAttribute("loader", new FindBytes(), ScriptContext.ENGINE_SCOPE);
1.77 + codeSeq = sb;
1.78 + }
1.79 +
1.80 + @Test public void invokeStaticMethod() throws Exception {
1.81 + assertExec("Trying to get -1", "test", Double.valueOf(-1),
1.82 + "org/apidesign/vm4brwsr/StaticMethod", "org_apidesign_vm4brwsr_StaticMethod", "minusOne"
1.83 + );
1.84 + }
1.85 +
1.86 +
1.87 + private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
1.88 + Object ret = null;
1.89 + try {
1.90 + ret = code.invokeFunction(methodName, args);
1.91 + } catch (ScriptException ex) {
1.92 + fail("Execution failed in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
1.93 + } catch (NoSuchMethodException ex) {
1.94 + fail("Cannot find method in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
1.95 + }
1.96 + if (ret == null && expRes == null) {
1.97 + return;
1.98 + }
1.99 + if (expRes.equals(ret)) {
1.100 + return;
1.101 + }
1.102 + assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
1.103 + }
1.104 +
1.105 + public static final class FindBytes {
1.106 + public byte[] get(String name) throws IOException {
1.107 + InputStream is = VMLazyTest.class.getClassLoader().getResourceAsStream(name);
1.108 + if (is == null) {
1.109 + throw new IOException("Can't find " + name);
1.110 + }
1.111 + byte[] arr = new byte[is.available()];
1.112 + int len = is.read(arr);
1.113 + if (len != arr.length) {
1.114 + throw new IOException("Read only " + len + " wanting " + arr.length);
1.115 + }
1.116 + System.err.print("loader['" + name + "'] = [");
1.117 + for (int i = 0; i < arr.length; i++) {
1.118 + if (i > 0) {
1.119 + System.err.print(", ");
1.120 + }
1.121 + System.err.print(arr[i]);
1.122 + }
1.123 + System.err.println("]");
1.124 + return arr;
1.125 + }
1.126 + }
1.127 +}