# HG changeset patch # User Jaroslav Tulach # Date 1421753195 -3600 # Node ID e80693152d8b4b978f4ca619d8ca6f91ce18dceb # Parent 97cb2c5aa4fcbe782dcfa2251d2dbca64222ba19 #6658: Need to addReference when accessing non-static field diff -r 97cb2c5aa4fc -r e80693152d8b rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Sun Jan 11 13:20:32 2015 +0100 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Tue Jan 20 12:26:35 2015 +0100 @@ -1398,6 +1398,7 @@ "_" + fi[1], fi) ); i += 2; + addReference(fi[0]); break; } case opc_putfield: { @@ -1412,6 +1413,7 @@ accessField(mangleClassAccess + "(false)", "_" + fi[1], fi)); i += 2; + addReference(fi[0]); break; } case opc_getstatic: { diff -r 97cb2c5aa4fc -r e80693152d8b rt/vm/src/test/java/org/apidesign/vm4brwsr/ImplementFactory.java --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/ImplementFactory.java Sun Jan 11 13:20:32 2015 +0100 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/ImplementFactory.java Tue Jan 20 12:26:35 2015 +0100 @@ -18,6 +18,7 @@ package org.apidesign.vm4brwsr; import org.apidesign.vm4brwsr.extrnl.ImplementInterface; +import org.apidesign.vm4brwsr.extrnl.ImportedField; /** * @@ -35,6 +36,10 @@ ImplementInterface i = create(); return i.sayHello(); } + + public static int meaning() { + return ImportedField.Factory.create(42).x; + } private static class Impl implements ImplementInterface { diff -r 97cb2c5aa4fc -r e80693152d8b rt/vm/src/test/java/org/apidesign/vm4brwsr/ImportedFieldTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/ImportedFieldTest.java Tue Jan 20 12:26:35 2015 +0100 @@ -0,0 +1,57 @@ +/** + * 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 javax.script.ScriptEngine; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +/** Tests whether we can access public field from a class in extension. + * + * @author Jaroslav Tulach + */ +public class ImportedFieldTest { + @Test public void checkHello() throws Exception { + + code.assertExec("Can access field from extension", + ImplementFactory.class, "meaning__I", + 42 + ); + } + + private static TestVM code; + + @BeforeClass + public static void compileTheCode() throws Exception { + StringBuilder sb = new StringBuilder(); + ScriptEngine[] eng = { null }; + code = TestVM.compileClassesAsExtension(sb, eng, null, null, + "org/apidesign/vm4brwsr/extrnl/ImportedField", + "org/apidesign/vm4brwsr/extrnl/ImportedField$Factory" + ); + code = TestVM.compileClassesAsExtension(sb, eng, null, null, + "org/apidesign/vm4brwsr/ImplementFactory", + "org/apidesign/vm4brwsr/ImplementFactory$Impl" + ); + } + @AfterClass + public static void releaseTheCode() { + code = null; + } +} diff -r 97cb2c5aa4fc -r e80693152d8b rt/vm/src/test/java/org/apidesign/vm4brwsr/extrnl/ImportedField.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/extrnl/ImportedField.java Tue Jan 20 12:26:35 2015 +0100 @@ -0,0 +1,32 @@ +/** + * 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.extrnl; + +public final class ImportedField { + public final int x; + + ImportedField(int x) { + this.x = x; + } + + public static final class Factory { + public static ImportedField create(int v) { + return new ImportedField(v); + } + } +}