# HG changeset patch # User Jaroslav Tulach # Date 1441228801 -7200 # Node ID 9d011ab3c192f79bacbb2124759f34b10979a84f # Parent f436c6bf464d4236ded868e25d8fd1b8f22f6d44 Consistent mangling of names with underscore diff -r f436c6bf464d -r 9d011ab3c192 rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Wed Sep 02 06:14:40 2015 +0200 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Wed Sep 02 23:20:01 2015 +0200 @@ -21,6 +21,15 @@ import java.io.InputStream; import org.apidesign.bck2brwsr.core.JavaScriptBody; import static org.apidesign.vm4brwsr.ByteCodeParser.*; +import org.apidesign.vm4brwsr.ByteCodeParser.AnnotationParser; +import org.apidesign.vm4brwsr.ByteCodeParser.BootMethodData; +import org.apidesign.vm4brwsr.ByteCodeParser.CPX2; +import org.apidesign.vm4brwsr.ByteCodeParser.ClassData; +import org.apidesign.vm4brwsr.ByteCodeParser.FieldData; +import org.apidesign.vm4brwsr.ByteCodeParser.MethodData; +import org.apidesign.vm4brwsr.ByteCodeParser.StackMapIterator; +import org.apidesign.vm4brwsr.ByteCodeParser.TrapData; +import org.apidesign.vm4brwsr.ByteCodeParser.TrapDataIterator; /** Translator of the code inside class files to JavaScript. * @@ -87,7 +96,7 @@ } final String accessClassFalse(String classOperation) { - if (jc.getClassName().replace('/', '_').equals(classOperation)) { + if (mangleClassName(jc.getClassName()).equals(classOperation)) { return "c"; } classRefs.addIfMissing(classOperation); @@ -294,7 +303,7 @@ } } for (String superInterface : jc.getSuperInterfaces()) { - String intrfc = superInterface.replace('/', '_'); + String intrfc = mangleClassName(superInterface); append("\n vm.").append(intrfc).append("(false)['fillInstOf'](x);"); requireReference(superInterface); } @@ -1843,7 +1852,7 @@ } if (d.charAt(0) == 'L') { assert d.charAt(d.length() - 1) == ';'; - out.append(d.replace('/', '_').substring(0, d.length() - 1)); + out.append(mangleClassName(d).substring(0, d.length() - 1)); } else { out.append(d); } @@ -1977,7 +1986,7 @@ int paramBeg = body.indexOf('(', sigEnd + 1); - sb.append("vm.").append(pkgName.replace('/', '_')).append("_$JsCallbacks$(false)._VM()."); + sb.append("vm.").append(mangleClassName(pkgName)).append("_$JsCallbacks$(false)._VM()."); sb.append(mangleJsCallbacks(fqn, method, params, false)); sb.append("(").append(refId); if (body.charAt(paramBeg + 1) != ')') { @@ -2014,7 +2023,7 @@ int paramBeg = body.indexOf('(', sigEnd + 1); - sb.append("vm.").append(pkgName.replace('/', '_')).append("_$JsCallbacks$(false)._VM()."); + sb.append("vm.").append(mangleClassName(pkgName)).append("_$JsCallbacks$(false)._VM()."); sb.append(mangleJsCallbacks(fqn, method, params, true)); sb.append("("); pos = paramBeg + 1; @@ -2258,7 +2267,7 @@ final String classInternalName = jc.getClassName(e.catch_cpx); addReference(classInternalName); append("e = vm.java_lang_Class(false).bck2BrwsrThrwrbl(e);"); - append("if (e['$instOf_" + classInternalName.replace('/', '_') + "']) {"); + append("if (e['$instOf_" + mangleClassName(classInternalName) + "']) {"); append("var stA0 = e;"); goTo(this, current, e.handler_pc, topMostLabel); append("}\n"); @@ -2406,7 +2415,7 @@ emit(smapper, this, "var @2 = @1 != null && @1['$instOf_@3'] ? 1 : 0;", smapper.popA(), smapper.pushI(), - type.replace('/', '_')); + mangleClassName(type)); } else { int cnt = 0; while (type.charAt(cnt) == '[') { @@ -2435,7 +2444,7 @@ if (!type.startsWith("[")) { emitNoFlush(smapper, "if (@1 !== null && !@1['$instOf_@2']) vm.java_lang_Class(false).castEx();", - smapper.getT(0, VarType.REFERENCE, false), type.replace('/', '_')); + smapper.getT(0, VarType.REFERENCE, false), mangleClassName(type)); } else { int cnt = 0; while (type.charAt(cnt) == '[') { diff -r f436c6bf464d -r 9d011ab3c192 rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java Wed Sep 02 06:14:40 2015 +0200 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java Wed Sep 02 23:20:01 2015 +0200 @@ -732,7 +732,9 @@ + " var vm = {};\n"); append(" function link(n, assign) {\n" + " return function() {\n" - + " var cls = n['replace__Ljava_lang_String_2CC']" + + " var no_ = n['replace__Ljava_lang_String_2Ljava_lang_CharSequence_2Ljava_lang_CharSequence_2']" + + "('_', '_1').toString();\n" + + " var cls = no_['replace__Ljava_lang_String_2CC']" + "('/', '_').toString();\n" + " var dot = n['replace__Ljava_lang_String_2CC']" + "('/', '.').toString();\n" @@ -755,7 +757,7 @@ @Override protected String generateClass(String className) throws IOException { if (isExternalClass(className)) { - final String cls = className.replace('/', '_'); + final String cls = className.replace("_", "_1").replace('/', '_'); append("\n").append(assignClass(cls)) .append("link('") .append(className) diff -r f436c6bf464d -r 9d011ab3c192 rt/vm/src/test/java/org/apidesign/vm4brwsr/Implement_Factory.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/Implement_Factory.java Wed Sep 02 23:20:01 2015 +0200 @@ -0,0 +1,54 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012-2015 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.apidesign.vm4brwsr.under_score.ImplementInterface; +import org.apidesign.vm4brwsr.under_score.ImportedField; + +/** + * + * @author Jaroslav Tulach + */ +public class Implement_Factory { + private Implement_Factory() { + } + + private static ImplementInterface create() { + return new Impl(); + } + + public static String hello() { + ImplementInterface i = create(); + return i.sayHello(); + } + + public static int meaning() { + return ImportedField.Factory.create(42).x; + } + + private static class Impl implements ImplementInterface { + + public Impl() { + } + + @Override + public String sayHello() { + return "Hello!"; + } + } +} diff -r f436c6bf464d -r 9d011ab3c192 rt/vm/src/test/java/org/apidesign/vm4brwsr/Imported_Field_Test.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/Imported_Field_Test.java Wed Sep 02 23:20:01 2015 +0200 @@ -0,0 +1,57 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012-2015 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 + * in an underscored package. + * + * @author Jaroslav Tulach + */ +public class Imported_Field_Test { + @Test public void checkHello() throws Exception { + code.assertExec("Can access field from extension", + Implement_Factory.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/under_score/ImportedField", + "org/apidesign/vm4brwsr/under_score/ImportedField$Factory" + ); + code = TestVM.compileClassesAsExtension(sb, eng, null, null, + "org/apidesign/vm4brwsr/Implement_Factory", + "org/apidesign/vm4brwsr/Implement_Factory$Impl" + ); + } + @AfterClass + public static void releaseTheCode() { + code = null; + } +} diff -r f436c6bf464d -r 9d011ab3c192 rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java Wed Sep 02 06:14:40 2015 +0200 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java Wed Sep 02 23:20:01 2015 +0200 @@ -193,7 +193,7 @@ addClasses(names). addResources("org/apidesign/vm4brwsr/obj.js"). addExported(exp.toArray(new String[0])). - obfuscation(ObfuscationLevel.FULL). + // obfuscation(ObfuscationLevel.FULL). library(); if (resourceName != null) { b2b = b2b.addResources(resourceName); diff -r f436c6bf464d -r 9d011ab3c192 rt/vm/src/test/java/org/apidesign/vm4brwsr/under_score/ImplementInterface.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/under_score/ImplementInterface.java Wed Sep 02 23:20:01 2015 +0200 @@ -0,0 +1,26 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012-2015 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.under_score; + +/** + * + * @author Jaroslav Tulach + */ +public interface ImplementInterface { + public String sayHello(); +} diff -r f436c6bf464d -r 9d011ab3c192 rt/vm/src/test/java/org/apidesign/vm4brwsr/under_score/ImportedField.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/under_score/ImportedField.java Wed Sep 02 23:20:01 2015 +0200 @@ -0,0 +1,36 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012-2015 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.under_score; + + +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 (ImportedField) createImpl(v); + } + private static Object createImpl(int v) { + return new ImportedField(v); + } + } +}