Allows two isolated bck2brwsr VM in a single JavaScript page/context emul
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 11 Feb 2013 19:55:00 +0100
branchemul
changeset 711333326d65bf9
parent 710 769b77cdb7a5
child 715 7572022945a0
Allows two isolated bck2brwsr VM in a single JavaScript page/context
dew/src/main/resources/org/apidesign/bck2brwsr/dew/js/app.js
javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/ProcessPageTest.java
javaquery/demo-calculator/src/main/resources/org/apidesign/bck2brwsr/demo/calc/staticcompilation/Calculator.xhtml
vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
vm/src/main/java/org/apidesign/vm4brwsr/VM.java
vmtest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/ZipFileTest.java
     1.1 --- a/dew/src/main/resources/org/apidesign/bck2brwsr/dew/js/app.js	Mon Feb 11 19:51:35 2013 +0100
     1.2 +++ b/dew/src/main/resources/org/apidesign/bck2brwsr/dew/js/app.js	Mon Feb 11 19:55:00 2013 +0100
     1.3 @@ -112,7 +112,7 @@
     1.4  "     var arr = eval('(' + request.responseText + ')');\n" +
     1.5  "     return arr;\n" +
     1.6  "   }\n" +
     1.7 -"   var vm = new bck2brwsr(ldCls);\n" +
     1.8 +"   var vm = bck2brwsr(ldCls);\n" +
     1.9  "   vm.loadClass('${fqn}');\n" +
    1.10  " </script>\n" +
    1.11  "</body></html>";
     2.1 --- a/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/ProcessPageTest.java	Mon Feb 11 19:51:35 2013 +0100
     2.2 +++ b/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/ProcessPageTest.java	Mon Feb 11 19:55:00 2013 +0100
     2.3 @@ -128,6 +128,8 @@
     2.4              sb = new StringBuilder();
     2.5          }
     2.6          Bck2Brwsr.generate(sb, ProcessPageTest.class.getClassLoader(), names);
     2.7 +        sb.append("var vm = this.bck2brwsr();\n");
     2.8 +        
     2.9          ScriptEngineManager sem = new ScriptEngineManager();
    2.10          ScriptEngine js = sem.getEngineByExtension("js");
    2.11          try {
     3.1 --- a/javaquery/demo-calculator/src/main/resources/org/apidesign/bck2brwsr/demo/calc/staticcompilation/Calculator.xhtml	Mon Feb 11 19:51:35 2013 +0100
     3.2 +++ b/javaquery/demo-calculator/src/main/resources/org/apidesign/bck2brwsr/demo/calc/staticcompilation/Calculator.xhtml	Mon Feb 11 19:55:00 2013 +0100
     3.3 @@ -79,7 +79,7 @@
     3.4          <div data-bind="text: displayPreview"></div>
     3.5          <script src="bck2brwsr.js"/>
     3.6          <script>
     3.7 -            var vm = new bck2brwsr(
     3.8 +            var vm = bck2brwsr(
     3.9                  [ 'demo.static.calculator-0.3-SNAPSHOT.jar' ]
    3.10              );
    3.11          </script>
     4.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Mon Feb 11 19:51:35 2013 +0100
     4.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Mon Feb 11 19:55:00 2013 +0100
     4.3 @@ -113,7 +113,7 @@
     4.4          final String className = className(jc);
     4.5          out.append("\n\n").append(assignClass(className));
     4.6          out.append("function CLS() {");
     4.7 -        out.append("\n  if (!CLS.prototype.$instOf_").append(className).append(") {");
     4.8 +        out.append("\n  if (!CLS.$class) {");
     4.9          if (proto == null) {
    4.10              String sc = jc.getSuperClassName(); // with _
    4.11              out.append("\n    var pp = ").
    4.12 @@ -182,6 +182,7 @@
    4.13          for (String superInterface : jc.getSuperInterfaces()) {
    4.14              out.append("\n    c.$instOf_").append(superInterface.replace('/', '_')).append(" = true;");
    4.15          }
    4.16 +        out.append("\n    CLS.$class = 'temp';");
    4.17          out.append("\n    CLS.$class = ");
    4.18          out.append(accessClass("java_lang_Class(true);"));
    4.19          out.append("\n    CLS.$class.jvmName = '").append(jc.getClassName()).append("';");
     5.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/VM.java	Mon Feb 11 19:51:35 2013 +0100
     5.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/VM.java	Mon Feb 11 19:55:00 2013 +0100
     5.3 @@ -48,8 +48,7 @@
     5.4          new VM(out).doCompile(l, names);
     5.5      }
     5.6      protected void doCompile(Bck2Brwsr.Resources l, StringArray names) throws IOException {
     5.7 -        out.append("(function VM(global) {");
     5.8 -        out.append("\n  var vm = {};");
     5.9 +        out.append("(function VM(global) {var fillInVMSkeleton = function(vm) {");
    5.10          StringArray processed = new StringArray();
    5.11          StringArray initCode = new StringArray();
    5.12          for (String baseClass : names.toArray()) {
    5.13 @@ -117,8 +116,11 @@
    5.14              }
    5.15          }
    5.16          out.append(
    5.17 -              "  global.bck2brwsr = function() {\n"
    5.18 +              "  return vm;\n"
    5.19 +            + "  };\n"
    5.20 +            + "  global.bck2brwsr = function() {\n"
    5.21              + "    var args = arguments;\n"
    5.22 +            + "    var vm = fillInVMSkeleton({});\n"
    5.23              + "    var loader = {};\n"
    5.24              + "    var init = null;\n"
    5.25              + "    loader.vm = vm;\n"
    5.26 @@ -137,6 +139,9 @@
    5.27              + "        load__Ljava_lang_Object_2Ljava_lang_Object_2Ljava_lang_String_2_3Ljava_lang_Object_2(loader, name, args);\n"
    5.28              + "    }\n"
    5.29              + "    if (args[0]) {\n"
    5.30 +            + "      if (vm.loadClass) {\n"
    5.31 +            + "        throw 'Cannot initialize the bck2brwsr VM twice!';\n"
    5.32 +            + "      }\n"
    5.33              + "      vm.loadClass = loader.loadClass;\n"
    5.34              + "      vm.loadBytes = function(name) {\n"
    5.35              + "        if (!args[0]) throw 'bck2brwsr initialized without loader function, cannot load ' + name;\n"
     6.1 --- a/vmtest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/ZipFileTest.java	Mon Feb 11 19:51:35 2013 +0100
     6.2 +++ b/vmtest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/ZipFileTest.java	Mon Feb 11 19:55:00 2013 +0100
     6.3 @@ -54,7 +54,7 @@
     6.4      }
     6.5      
     6.6      @JavaScriptBody(args = { "res", "path" }, body = 
     6.7 -          "var myvm = new bck2brwsr(path);\n"
     6.8 +          "var myvm = bck2brwsr(path);\n"
     6.9          + "var cls = myvm.loadClass('java.lang.String');\n"
    6.10          + "return cls.getClass__Ljava_lang_Class_2().getResourceAsStream__Ljava_io_InputStream_2Ljava_lang_String_2(res);\n"
    6.11      )