Loaded classes need to have their static initializes invoked. Before accessing static field of a class, initializers need to be executed as well.
1.1 --- a/javaquery/demo-calculator/src/main/resources/org/apidesign/bck2brwsr/mavenhtml/Calculator.xhtml Thu Dec 20 16:17:31 2012 +0100
1.2 +++ b/javaquery/demo-calculator/src/main/resources/org/apidesign/bck2brwsr/mavenhtml/Calculator.xhtml Thu Dec 20 21:39:16 2012 +0100
1.3 @@ -80,7 +80,7 @@
1.4 <script src="/bck2brwsr.js"></script>
1.5 <script src="/vm.js"></script>
1.6 <script type="text/javascript">
1.7 - vm.loadClass('org.apidesign.bck2brwsr.mavenhtml.Calculator').class__V();
1.8 + vm.loadClass('org.apidesign.bck2brwsr.mavenhtml.Calculator');
1.9 </script>
1.10
1.11 <hr/>
2.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Thu Dec 20 16:17:31 2012 +0100
2.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Thu Dec 20 21:39:16 2012 +0100
2.3 @@ -1029,7 +1029,7 @@
2.4 int indx = readIntArg(byteCodes, i);
2.5 String[] fi = jc.getFieldInfoName(indx);
2.6 final int type = VarType.fromFieldType(fi[2].charAt(0));
2.7 - emit(out, "@1 = @2.@3;",
2.8 + emit(out, "@1 = @2(false).constructor.@3;",
2.9 smapper.pushT(type),
2.10 accessClass(fi[0].replace('/', '_')), fi[1]);
2.11 i += 2;
2.12 @@ -1049,7 +1049,7 @@
2.13 int indx = readIntArg(byteCodes, i);
2.14 String[] fi = jc.getFieldInfoName(indx);
2.15 final int type = VarType.fromFieldType(fi[2].charAt(0));
2.16 - emit(out, "@1.@2 = @3;",
2.17 + emit(out, "@1(false).constructor.@2 = @3;",
2.18 accessClass(fi[0].replace('/', '_')), fi[1],
2.19 smapper.popT(type));
2.20 i += 2;
3.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/VMLazy.java Thu Dec 20 16:17:31 2012 +0100
3.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/VMLazy.java Thu Dec 20 21:39:16 2012 +0100
3.3 @@ -61,10 +61,27 @@
3.4 StringBuilder out = new StringBuilder();
3.5 out.append("var loader = arguments[0];\n");
3.6 out.append("var vm = loader.vm;\n");
3.7 - new Gen(this, out).compile(new ByteArrayInputStream(arr));
3.8 + int prelude = out.length();
3.9 + String initCode = new Gen(this, out).compile(new ByteArrayInputStream(arr));
3.10 String code = out.toString().toString();
3.11 +// dump("Loading " + name);
3.12 + dump(code);
3.13 String under = name.replace('.', '_');
3.14 - return applyCode(loader, under, code, instance);
3.15 + Object fn = applyCode(loader, under, code, instance);
3.16 +
3.17 + if (!initCode.isEmpty()) {
3.18 + out.setLength(prelude);
3.19 + out.append(initCode);
3.20 + code = out.toString().toString();
3.21 + dump(code);
3.22 + applyCode(loader, null, code, false);
3.23 + }
3.24 +
3.25 + return fn;
3.26 + }
3.27 +
3.28 +// @JavaScriptBody(args = "s", body = "java.lang.System.out.println(s.toString());")
3.29 + static void dump(String s) {
3.30 }
3.31
3.32 /* possibly not needed:
3.33 @@ -82,7 +99,7 @@
3.34 "} catch (ex) {\n" +
3.35 " throw 'Cannot compile ' + ex + ' line: ' + ex.lineNumber + ' script:\\n' + script;\n" +
3.36 "}\n" +
3.37 - "return vm[name](instance);\n"
3.38 + "return name != null ? vm[name](instance) : null;\n"
3.39 )
3.40 private static native Object applyCode(Object loader, String name, String script, boolean instance);
3.41
4.1 --- a/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/CompareHashTest.java Thu Dec 20 16:17:31 2012 +0100
4.2 +++ b/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/CompareHashTest.java Thu Dec 20 21:39:16 2012 +0100
4.3 @@ -35,6 +35,10 @@
4.4 return o.hashCode() - o.hashCode();
4.5 }
4.6
4.7 + @Compare public int initializeInStatic() {
4.8 + return StaticUse.NON_NULL.hashCode() - StaticUse.NON_NULL.hashCode();
4.9 + }
4.10 +
4.11 @Factory
4.12 public static Object[] create() {
4.13 return VMTest.create(CompareHashTest.class);
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/StaticUse.java Thu Dec 20 21:39:16 2012 +0100
5.3 @@ -0,0 +1,22 @@
5.4 +/**
5.5 + * Back 2 Browser Bytecode Translator
5.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5.7 + *
5.8 + * This program is free software: you can redistribute it and/or modify
5.9 + * it under the terms of the GNU General Public License as published by
5.10 + * the Free Software Foundation, version 2 of the License.
5.11 + *
5.12 + * This program is distributed in the hope that it will be useful,
5.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5.15 + * GNU General Public License for more details.
5.16 + *
5.17 + * You should have received a copy of the GNU General Public License
5.18 + * along with this program. Look for COPYING file in the top folder.
5.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
5.20 + */
5.21 +package org.apidesign.bck2brwsr.tck;
5.22 +
5.23 +class StaticUse {
5.24 + public static final Object NON_NULL = new Object();
5.25 +}