2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.vm4brwsr;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import javax.script.Invocable;
23 import javax.script.ScriptContext;
24 import javax.script.ScriptEngine;
25 import javax.script.ScriptException;
26 import org.testng.annotations.BeforeClass;
27 import static org.testng.Assert.*;
28 import org.testng.annotations.Test;
30 /** Implements loading class by class.
32 * @author Jaroslav Tulach <jtulach@netbeans.org>
34 public class VMLazyTest {
37 private static CharSequence codeSeq;
38 private static Invocable code;
41 public void compileTheCode() throws Exception {
42 StringBuilder sb = new StringBuilder();
44 sb.append("\nfunction test(clazz, as, method) {");
45 sb.append("\n var l = new lazyVM();");
46 sb.append("\n var c = l.loadClass(clazz, as);");
47 sb.append("\n return c[method]();");
51 sb.append("\nfunction lazyVM() {");
52 sb.append("\n var self = this;");
53 sb.append("\n this.constructor.prototype.Identity = function(value) {");
54 sb.append("\n var self = this;");
55 sb.append("\n self.value = value;");
56 sb.append("\n self.call = function() { return self.value; };");
59 sb.append("\n this.constructor.prototype.loadClass = function(res, name) {");
60 sb.append("\n var script = org_apidesign_vm4brwsr_VMLazy_toJavaScriptLjava_lang_StringAB(loader.get(res + '.class'));");
61 sb.append("\n try {");
62 sb.append("\n new Function(");
63 sb.append("\n 'arguments[0][arguments[1]]=new lazyVM.prototype.Identity(' + script + ').call'");
64 sb.append("\n )(self, name);");
65 sb.append("\n } catch (ex) {");
66 sb.append("\n throw 'Cannot compile ' + res + ' error: ' + ex + ' script:\\n' + script;");
72 ScriptEngine[] arr = { null };
73 code = StaticMethodTest.compileClass(sb, arr,
74 "org/apidesign/vm4brwsr/VMLazy"
76 arr[0].getContext().setAttribute("loader", new FindBytes(), ScriptContext.ENGINE_SCOPE);
80 @Test public void invokeStaticMethod() throws Exception {
81 assertExec("Trying to get -1", "test", Double.valueOf(-1),
82 "org/apidesign/vm4brwsr/StaticMethod", "org_apidesign_vm4brwsr_StaticMethod", "minusOne"
87 private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
90 ret = code.invokeFunction(methodName, args);
91 } catch (ScriptException ex) {
92 fail("Execution failed in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
93 } catch (NoSuchMethodException ex) {
94 fail("Cannot find method in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
96 if (ret == null && expRes == null) {
99 if (expRes.equals(ret)) {
102 assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
105 public static final class FindBytes {
106 public byte[] get(String name) throws IOException {
107 InputStream is = VMLazyTest.class.getClassLoader().getResourceAsStream(name);
109 throw new IOException("Can't find " + name);
111 byte[] arr = new byte[is.available()];
112 int len = is.read(arr);
113 if (len != arr.length) {
114 throw new IOException("Read only " + len + " wanting " + arr.length);
116 System.err.print("loader['" + name + "'] = [");
117 for (int i = 0; i < arr.length; i++) {
119 System.err.print(", ");
121 System.err.print(arr[i]);
123 System.err.println("]");