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(this);");
46 sb.append("\n var c = l.loadClass(clazz, as);");
47 sb.append("\n return c[method]();");
51 sb.append("\nfunction lazyVM(global) {");
52 sb.append("\n var self = this;");
53 sb.append("\n var glb = global;");
54 sb.append("\n lazyVM.prototype.loadClass = function(res, name) {");
55 sb.append("\n var script = org_apidesign_vm4brwsr_VMLazy(true)."
56 + "toJavaScript__Ljava_lang_String_2Ljava_lang_Object_2Ljava_lang_Object_2_3B("
58 + " loader.get(res + '.class')"
60 sb.append("\n try {");
61 sb.append("\n new Function(script)(glb, name);");
62 sb.append("\n } catch (ex) {");
63 sb.append("\n throw 'Cannot compile ' + res + ' error: ' + ex + ' script:\\n' + script;");
65 sb.append("\n return glb[name](true);");
70 ScriptEngine[] arr = { null };
71 code = StaticMethodTest.compileClass(sb, arr,
72 "org/apidesign/vm4brwsr/VMLazy"
74 arr[0].getContext().setAttribute("loader", new FindBytes(), ScriptContext.ENGINE_SCOPE);
78 @Test public void invokeStaticMethod() throws Exception {
79 assertExec("Trying to get -1", "test", Double.valueOf(-1),
80 "org/apidesign/vm4brwsr/StaticMethod", "org_apidesign_vm4brwsr_StaticMethod", "minusOne__I"
84 @Test public void loadDependantClass() throws Exception {
85 assertExec("Trying to get zero", "test", Double.valueOf(0),
86 "org/apidesign/vm4brwsr/InstanceSub", "org_apidesign_vm4brwsr_InstanceSub", "recallDbl__D"
90 private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
93 ret = code.invokeFunction(methodName, args);
94 } catch (ScriptException ex) {
95 fail("Execution failed in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
96 } catch (NoSuchMethodException ex) {
97 fail("Cannot find method in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
99 if (ret == null && expRes == null) {
102 if (expRes.equals(ret)) {
105 assertEquals(ret, expRes, msg + "was: " + ret + "\n" + StaticMethodTest.dumpJS(codeSeq));
108 public static final class FindBytes {
109 public byte[] get(String name) throws IOException {
110 InputStream is = VMLazyTest.class.getClassLoader().getResourceAsStream(name);
112 throw new IOException("Can't find " + name);
114 byte[] arr = new byte[is.available()];
115 int len = is.read(arr);
116 if (len != arr.length) {
117 throw new IOException("Read only " + len + " wanting " + arr.length);
120 System.err.print("loader['" + name + "'] = [");
121 for (int i = 0; i < arr.length; i++) {
123 System.err.print(", ");
125 System.err.print(arr[i]);
127 System.err.println("]");