# HG changeset patch # User Jaroslav Tulach # Date 1353875043 -3600 # Node ID f180d72cc7a43d6475bdf3de46cab248aea0e9fa # Parent 227bafe6ef52d23fc3f66beab2eec5241dbd3b46 Initial test to show how an incremental compilation could look like diff -r 227bafe6ef52 -r f180d72cc7a4 vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java --- a/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java Sun Nov 25 21:23:06 2012 +0100 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java Sun Nov 25 21:24:03 2012 +0100 @@ -271,12 +271,20 @@ } static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException { + return compileClass(sb, null, names); + } + static Invocable compileClass( + StringBuilder sb, ScriptEngine[] eng, String... names + ) throws ScriptException, IOException { if (sb == null) { sb = new StringBuilder(); } GenJS.compile(sb, names); ScriptEngineManager sem = new ScriptEngineManager(); ScriptEngine js = sem.getEngineByExtension("js"); + if (eng != null) { + eng[0] = js; + } try { Object res = js.eval(sb.toString()); assertTrue(js instanceof Invocable, "It is invocable object: " + res); diff -r 227bafe6ef52 -r f180d72cc7a4 vm/src/test/java/org/apidesign/vm4brwsr/VMLazy.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/VMLazy.java Sun Nov 25 21:24:03 2012 +0100 @@ -0,0 +1,46 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.vm4brwsr; + +import java.io.ByteArrayInputStream; +import java.io.IOException; + +/** + * + * @author Jaroslav Tulach + */ +class VMLazy extends ByteCodeToJavaScript { + private VMLazy(Appendable out) { + super(out); + } + + static String toJavaScript(byte[] is) throws IOException { + StringBuilder sb = new StringBuilder(); + new VMLazy(sb).compile(new ByteArrayInputStream(is)); + return sb.toString().toString(); + } + + @Override + protected boolean requireReference(String internalClassName) { + return false; + } + + @Override + protected void requireScript(String resourcePath) { + } +} diff -r 227bafe6ef52 -r f180d72cc7a4 vm/src/test/java/org/apidesign/vm4brwsr/VMLazyTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/VMLazyTest.java Sun Nov 25 21:24:03 2012 +0100 @@ -0,0 +1,124 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.vm4brwsr; + +import java.io.IOException; +import java.io.InputStream; +import javax.script.Invocable; +import javax.script.ScriptContext; +import javax.script.ScriptEngine; +import javax.script.ScriptException; +import org.testng.annotations.BeforeClass; +import static org.testng.Assert.*; +import org.testng.annotations.Test; + +/** Implements loading class by class. + * + * @author Jaroslav Tulach + */ +public class VMLazyTest { + + + private static CharSequence codeSeq; + private static Invocable code; + + @BeforeClass + public void compileTheCode() throws Exception { + StringBuilder sb = new StringBuilder(); + + sb.append("\nfunction test(clazz, as, method) {"); + sb.append("\n var l = new lazyVM();"); + sb.append("\n var c = l.loadClass(clazz, as);"); + sb.append("\n return c[method]();"); + sb.append("\n}"); + + + sb.append("\nfunction lazyVM() {"); + sb.append("\n var self = this;"); + sb.append("\n this.constructor.prototype.Identity = function(value) {"); + sb.append("\n var self = this;"); + sb.append("\n self.value = value;"); + sb.append("\n self.call = function() { return self.value; };"); + sb.append("\n };"); + sb.append("\n"); + sb.append("\n this.constructor.prototype.loadClass = function(res, name) {"); + sb.append("\n var script = org_apidesign_vm4brwsr_VMLazy_toJavaScriptLjava_lang_StringAB(loader.get(res + '.class'));"); +// sb.append("\n throw script.toString();"); + sb.append("\n new Function("); + sb.append("\n 'arguments[0][arguments[1]]=new lazyVM.prototype.Identity(' + script + ').call'"); + sb.append("\n )(self, name);"); + sb.append("\n };"); + sb.append("\n"); + sb.append("\n}\n"); + + ScriptEngine[] arr = { null }; + code = StaticMethodTest.compileClass(sb, arr, + "org/apidesign/vm4brwsr/VMLazy" + ); + arr[0].getContext().setAttribute("loader", new FindBytes(), ScriptContext.ENGINE_SCOPE); + codeSeq = sb; + } + + @Test public void invokeStaticMethod() throws Exception { + assertExec("Trying to get -1", "test", Double.valueOf(-1), + "org/apidesign/vm4brwsr/StaticMethod", "org_apidesign_vm4brwsr_StaticMethod", "minusOne" + ); + } + + + private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception { + Object ret = null; + try { + ret = code.invokeFunction(methodName, args); + } catch (ScriptException ex) { + fail("Execution failed in\n" + StaticMethodTest.dumpJS(codeSeq), ex); + } catch (NoSuchMethodException ex) { + fail("Cannot find method in\n" + StaticMethodTest.dumpJS(codeSeq), ex); + } + if (ret == null && expRes == null) { + return; + } + if (expRes.equals(ret)) { + return; + } + assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq); + } + + public static final class FindBytes { + public byte[] get(String name) throws IOException { + InputStream is = VMLazyTest.class.getClassLoader().getResourceAsStream(name); + if (is == null) { + throw new IOException("Can't find " + name); + } + byte[] arr = new byte[is.available()]; + int len = is.read(arr); + if (len != arr.length) { + throw new IOException("Read only " + len + " wanting " + arr.length); + } + System.err.print("loader['" + name + "'] = ["); + for (int i = 0; i < arr.length; i++) { + if (i > 0) { + System.err.print(", "); + } + System.err.print(arr[i]); + } + System.err.println("]"); + return arr; + } + } +}