# HG changeset patch # User Jaroslav Tulach # Date 1381761895 -7200 # Node ID c4e57ec5f0dfad2506eb5c16b1cb0708e461481b # Parent cc58b30499e509392c45eab56c0c5d1c14b28793 VM.reload can now reload a class in existing virtual machine diff -r cc58b30499e5 -r c4e57ec5f0df rt/emul/mini/src/main/java/org/apidesign/vm4brwsr/api/VM.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/emul/mini/src/main/java/org/apidesign/vm4brwsr/api/VM.java Mon Oct 14 16:44:55 2013 +0200 @@ -0,0 +1,47 @@ +/** + * 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.api; + +import java.io.IOException; +import org.apidesign.bck2brwsr.core.JavaScriptBody; + +/** Utility methods to talk to the Bck2Brwsr virtual machine. + * + * @author Jaroslav Tulach + * @since 0.9 + */ +public final class VM { + private VM() { + } + + /** Takes an existing class and replaces its existing byte code + * with new one. + * + * @param clazz existing class to reload + * @param byteCode new bytecode + * @throws IOException an exception is something goes wrong + */ + public static void reload(Class clazz, byte[] byteCode) throws IOException { + reloadImpl(clazz.getName(), byteCode); + } + + @JavaScriptBody(args = { "name", "byteCode" }, body = "vm._reload(name, byteCode);") + private static void reloadImpl(String name, byte[] byteCode) throws IOException { + throw new IOException(); + } +} diff -r cc58b30499e5 -r c4e57ec5f0df rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java Sat Oct 12 19:26:45 2013 +0200 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java Mon Oct 14 16:44:55 2013 +0200 @@ -153,6 +153,12 @@ + " throw 'Cannot initialize the bck2brwsr VM twice!';\n" + " }\n" + " vm.loadClass = loader.loadClass;\n" + + " vm._reload = function(name, byteCode) {;\n" + + " var attr = name.replace__Ljava_lang_String_2CC('.','_');\n" + + " delete vm[attr];\n" + + " return vm.org_apidesign_vm4brwsr_VMLazy(false).\n" + + " reload__Ljava_lang_Object_2Ljava_lang_Object_2Ljava_lang_String_2_3Ljava_lang_Object_2_3B(loader, name, args, byteCode);\n" + + " };\n" + " vm.loadBytes = function(name) {\n" + " return vm.org_apidesign_vm4brwsr_VMLazy(false).\n" + " loadBytes___3BLjava_lang_Object_2Ljava_lang_String_2_3Ljava_lang_Object_2(loader, name, args);\n" diff -r cc58b30499e5 -r c4e57ec5f0df rt/vm/src/main/java/org/apidesign/vm4brwsr/VMLazy.java --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/VMLazy.java Sat Oct 12 19:26:45 2013 +0200 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/VMLazy.java Mon Oct 14 16:44:55 2013 +0200 @@ -42,6 +42,11 @@ throws IOException, ClassNotFoundException { return new VMLazy(loader, arguments).load(name, false); } + + static Object reload(Object loader, String name, Object[] arguments, byte[] arr) + throws IOException, ClassNotFoundException { + return new VMLazy(loader, arguments).defineClass(arr, name, false); + } static byte[] loadBytes(Object loader, String name, Object[] arguments) throws Exception { return Zips.loadFromCp(arguments, name); @@ -54,7 +59,11 @@ if (arr == null) { throw new ClassNotFoundException(name); } -// beingDefined(loader, name); + + return defineClass(arr, name, instance); + } + + private Object defineClass(byte[] arr, String name, boolean instance) throws IOException { StringBuilder out = new StringBuilder(65535); out.append("var loader = arguments[0];\n"); out.append("var vm = loader.vm;\n"); diff -r cc58b30499e5 -r c4e57ec5f0df rt/vm/src/test/java/org/apidesign/vm4brwsr/BytesLoader.java --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/BytesLoader.java Sat Oct 12 19:26:45 2013 +0200 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/BytesLoader.java Mon Oct 14 16:44:55 2013 +0200 @@ -21,20 +21,19 @@ import java.io.InputStream; import java.net.URL; import java.util.Enumeration; -import java.util.Set; -import java.util.TreeSet; /** * * @author Jaroslav Tulach */ public final class BytesLoader { - private static Set requested = new TreeSet(); + private static final StringArray requested = new StringArray(); public byte[] get(String name) throws IOException { - if (!requested.add(name)) { + if (requested.contains(name)) { throw new IllegalStateException("Requested for second time: " + name); } + requested.add(name); byte[] arr = readClass(name); /* System.err.print("loader['" + name + "'] = ["); diff -r cc58b30499e5 -r c4e57ec5f0df rt/vm/src/test/java/org/apidesign/vm4brwsr/Hello.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/Hello.java Mon Oct 14 16:44:55 2013 +0200 @@ -0,0 +1,35 @@ +/** + * 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; + +/** + * + * @author Jaroslav Tulach + */ +public class Hello { + public static String hello() { + return "Hello World!"; + } + + public static Object reloadYourSelf(byte[] arr) throws IOException { + org.apidesign.vm4brwsr.api.VM.reload(Hello.class, arr); + return null; + } +} diff -r cc58b30499e5 -r c4e57ec5f0df rt/vm/src/test/java/org/apidesign/vm4brwsr/ReloadingTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/ReloadingTest.java Mon Oct 14 16:44:55 2013 +0200 @@ -0,0 +1,72 @@ +/** + * 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 org.testng.annotations.BeforeClass; +import org.testng.annotations.AfterClass; +import org.testng.annotations.Test; + +/** + * + * @author Jaroslav Tulach + */ +public class ReloadingTest { + private static TestVM code; + + @Test public void verifyUsageOf() throws Exception { + code.execCode("First hello", + Hello.class, "hello__Ljava_lang_String_2", + "Hello World!" + ); + + byte[] arr = BytesLoader.readClass("org/apidesign/vm4brwsr/Hello.class"); + for (int i = 0; i < arr.length; i++) { + if (arr[i] == 'H' && arr[i + 1] == 'e' && arr[i + 2] == 'l') { + arr[i] = 'A'; + arr[i + 1] = 'h'; + arr[i + 2] = 'o'; + arr[i + 3] = 'y'; + arr[i + 4] = ' '; + break; + } + } + + code.execCode("Redefine class", + Hello.class, "reloadYourSelf__Ljava_lang_Object_2_3B", + null, arr + ); + + code.execCode("Second hello", + Hello.class, "hello__Ljava_lang_String_2", + "Ahoy World!" + ); + } + + + @BeforeClass + public static void compileTheCode() throws Exception { + code = TestVM.compileClass( + "org/apidesign/vm4brwsr/Hello"); + } + @AfterClass + public static void releaseTheCode() { + code = null; + } + +} +