# HG changeset patch # User Jaroslav Tulach # Date 1361972296 -3600 # Node ID a13e33fd5c2eb2511147bd59421fb023468b1202 # Parent 406faa8bc64f688ea3f62699c63b340b89b3807c Need to use method accessors to access static fields diff -r 406faa8bc64f -r a13e33fd5c2e rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Wed Feb 27 08:37:02 2013 +0100 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Wed Feb 27 14:38:16 2013 +0100 @@ -132,6 +132,10 @@ for (FieldData v : jc.getFields()) { if (v.isStatic()) { out.append("\n CLS.").append(v.getName()).append(initField(v)); + out.append("\n c._").append(v.getName()).append(" = function (v) {") + .append(" if (arguments.length == 1) CLS.").append(v.getName()) + .append(" = v; return CLS."). + append(v.getName()).append("; };"); } else { out.append("\n c._").append(v.getName()).append(" = function (v) {") .append(" if (arguments.length == 1) this.fld_"). @@ -1244,7 +1248,7 @@ int indx = readIntArg(byteCodes, i); String[] fi = jc.getFieldInfoName(indx); final int type = VarType.fromFieldType(fi[2].charAt(0)); - emit(out, "var @1 = @2(false).constructor.@3;", + emit(out, "var @1 = @2(false)._@3();", smapper.pushT(type), accessClass(fi[0].replace('/', '_')), fi[1]); i += 2; @@ -1255,7 +1259,7 @@ int indx = readIntArg(byteCodes, i); String[] fi = jc.getFieldInfoName(indx); final int type = VarType.fromFieldType(fi[2].charAt(0)); - emit(out, "@1(false).constructor.@2 = @3;", + emit(out, "@1(false)._@2(@3);", accessClass(fi[0].replace('/', '_')), fi[1], smapper.popT(type)); i += 2; diff -r 406faa8bc64f -r a13e33fd5c2e rt/vm/src/test/java/org/apidesign/vm4brwsr/StaticUseSub.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/StaticUseSub.java Wed Feb 27 14:38:16 2013 +0100 @@ -0,0 +1,24 @@ +/** + * 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; + +public class StaticUseSub extends StaticUse { + public static String getNonNull() { + return NON_NULL.getClass().getName(); + } +} diff -r 406faa8bc64f -r a13e33fd5c2e rt/vm/src/test/java/org/apidesign/vm4brwsr/StaticUseSubTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/StaticUseSubTest.java Wed Feb 27 14:38:16 2013 +0100 @@ -0,0 +1,43 @@ +/** + * 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.Test; + +/** + * + * @author Jaroslav Tulach + */ +public class StaticUseSubTest { + private static TestVM code; + + @BeforeClass + public static void compileTheCode() throws Exception { + StringBuilder sb = new StringBuilder(); + code = TestVM.compileClass(sb, "org/apidesign/vm4brwsr/StaticUseSub"); + } + + @Test public void getInheritedStaticField() throws Exception { + code.assertExec( + "Obtains non-null", StaticUseSub.class, + "getNonNull__Ljava_lang_String_2", + "java.lang.Object" + ); + } +} diff -r 406faa8bc64f -r a13e33fd5c2e rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionTest.java --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionTest.java Wed Feb 27 08:37:02 2013 +0100 +++ b/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionTest.java Wed Feb 27 14:38:16 2013 +0100 @@ -123,7 +123,7 @@ @Compare public String newInstanceFails() throws InstantiationException { try { - return "success: " + StaticUse.class.newInstance(); + return "success: " + StaticUseSub.class.newInstance(); } catch (IllegalAccessException ex) { return ex.getClass().getName(); } diff -r 406faa8bc64f -r a13e33fd5c2e rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/StaticUse.java --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/StaticUse.java Wed Feb 27 08:37:02 2013 +0100 +++ b/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/StaticUse.java Wed Feb 27 14:38:16 2013 +0100 @@ -19,7 +19,7 @@ class StaticUse { public static final Object NON_NULL = new Object(); - private StaticUse() { + StaticUse() { } public void instanceMethod() { diff -r 406faa8bc64f -r a13e33fd5c2e rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/StaticUseSub.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/StaticUseSub.java Wed Feb 27 14:38:16 2013 +0100 @@ -0,0 +1,27 @@ +/** + * 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.bck2brwsr.tck; + +public class StaticUseSub extends StaticUse { + private StaticUseSub() { + } + + public static Object getNonNull() { + return NON_NULL; + } +} diff -r 406faa8bc64f -r a13e33fd5c2e rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/StaticUseSubTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/StaticUseSubTest.java Wed Feb 27 14:38:16 2013 +0100 @@ -0,0 +1,37 @@ +/** + * 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.bck2brwsr.tck; + +import org.apidesign.bck2brwsr.vmtest.Compare; +import org.apidesign.bck2brwsr.vmtest.VMTest; +import org.testng.annotations.Factory; + +/** + * + * @author Jaroslav Tulach + */ +public class StaticUseSubTest { + @Compare public String staticFieldInitializationInSuperClass() throws Exception { + Object ret = StaticUseSub.getNonNull(); + return ret.getClass().getName(); + } + + @Factory public static Object[] create() { + return VMTest.create(StaticUseSubTest.class); + } +}