Consistent mangling of names with underscore
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 02 Sep 2015 23:20:01 +0200
changeset 18409d011ab3c192
parent 1839 f436c6bf464d
child 1843 781d931e0a04
Consistent mangling of names with underscore
rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java
rt/vm/src/test/java/org/apidesign/vm4brwsr/Implement_Factory.java
rt/vm/src/test/java/org/apidesign/vm4brwsr/Imported_Field_Test.java
rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java
rt/vm/src/test/java/org/apidesign/vm4brwsr/under_score/ImplementInterface.java
rt/vm/src/test/java/org/apidesign/vm4brwsr/under_score/ImportedField.java
     1.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Wed Sep 02 06:14:40 2015 +0200
     1.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Wed Sep 02 23:20:01 2015 +0200
     1.3 @@ -21,6 +21,15 @@
     1.4  import java.io.InputStream;
     1.5  import org.apidesign.bck2brwsr.core.JavaScriptBody;
     1.6  import static org.apidesign.vm4brwsr.ByteCodeParser.*;
     1.7 +import org.apidesign.vm4brwsr.ByteCodeParser.AnnotationParser;
     1.8 +import org.apidesign.vm4brwsr.ByteCodeParser.BootMethodData;
     1.9 +import org.apidesign.vm4brwsr.ByteCodeParser.CPX2;
    1.10 +import org.apidesign.vm4brwsr.ByteCodeParser.ClassData;
    1.11 +import org.apidesign.vm4brwsr.ByteCodeParser.FieldData;
    1.12 +import org.apidesign.vm4brwsr.ByteCodeParser.MethodData;
    1.13 +import org.apidesign.vm4brwsr.ByteCodeParser.StackMapIterator;
    1.14 +import org.apidesign.vm4brwsr.ByteCodeParser.TrapData;
    1.15 +import org.apidesign.vm4brwsr.ByteCodeParser.TrapDataIterator;
    1.16  
    1.17  /** Translator of the code inside class files to JavaScript.
    1.18   *
    1.19 @@ -87,7 +96,7 @@
    1.20      }
    1.21      
    1.22      final String accessClassFalse(String classOperation) {
    1.23 -        if (jc.getClassName().replace('/', '_').equals(classOperation)) {
    1.24 +        if (mangleClassName(jc.getClassName()).equals(classOperation)) {
    1.25              return "c";
    1.26          }
    1.27          classRefs.addIfMissing(classOperation);
    1.28 @@ -294,7 +303,7 @@
    1.29              }
    1.30          }
    1.31          for (String superInterface : jc.getSuperInterfaces()) {
    1.32 -            String intrfc = superInterface.replace('/', '_');
    1.33 +            String intrfc = mangleClassName(superInterface);
    1.34              append("\n      vm.").append(intrfc).append("(false)['fillInstOf'](x);");
    1.35              requireReference(superInterface);
    1.36          }
    1.37 @@ -1843,7 +1852,7 @@
    1.38          }
    1.39          if (d.charAt(0) == 'L') {
    1.40              assert d.charAt(d.length() - 1) == ';';
    1.41 -            out.append(d.replace('/', '_').substring(0, d.length() - 1));
    1.42 +            out.append(mangleClassName(d).substring(0, d.length() - 1));
    1.43          } else {
    1.44              out.append(d);
    1.45          }
    1.46 @@ -1977,7 +1986,7 @@
    1.47  
    1.48              int paramBeg = body.indexOf('(', sigEnd + 1);
    1.49              
    1.50 -            sb.append("vm.").append(pkgName.replace('/', '_')).append("_$JsCallbacks$(false)._VM().");
    1.51 +            sb.append("vm.").append(mangleClassName(pkgName)).append("_$JsCallbacks$(false)._VM().");
    1.52              sb.append(mangleJsCallbacks(fqn, method, params, false));
    1.53              sb.append("(").append(refId);
    1.54              if (body.charAt(paramBeg + 1) != ')') {
    1.55 @@ -2014,7 +2023,7 @@
    1.56  
    1.57              int paramBeg = body.indexOf('(', sigEnd + 1);
    1.58              
    1.59 -            sb.append("vm.").append(pkgName.replace('/', '_')).append("_$JsCallbacks$(false)._VM().");
    1.60 +            sb.append("vm.").append(mangleClassName(pkgName)).append("_$JsCallbacks$(false)._VM().");
    1.61              sb.append(mangleJsCallbacks(fqn, method, params, true));
    1.62              sb.append("(");
    1.63              pos = paramBeg + 1;
    1.64 @@ -2258,7 +2267,7 @@
    1.65                  final String classInternalName = jc.getClassName(e.catch_cpx);
    1.66                  addReference(classInternalName);
    1.67                  append("e = vm.java_lang_Class(false).bck2BrwsrThrwrbl(e);");
    1.68 -                append("if (e['$instOf_" + classInternalName.replace('/', '_') + "']) {");
    1.69 +                append("if (e['$instOf_" + mangleClassName(classInternalName) + "']) {");
    1.70                  append("var stA0 = e;");
    1.71                  goTo(this, current, e.handler_pc, topMostLabel);
    1.72                  append("}\n");
    1.73 @@ -2406,7 +2415,7 @@
    1.74              emit(smapper, this, 
    1.75                      "var @2 = @1 != null && @1['$instOf_@3'] ? 1 : 0;",
    1.76                   smapper.popA(), smapper.pushI(),
    1.77 -                 type.replace('/', '_'));
    1.78 +                 mangleClassName(type));
    1.79          } else {
    1.80              int cnt = 0;
    1.81              while (type.charAt(cnt) == '[') {
    1.82 @@ -2435,7 +2444,7 @@
    1.83          if (!type.startsWith("[")) {
    1.84              emitNoFlush(smapper, 
    1.85                   "if (@1 !== null && !@1['$instOf_@2']) vm.java_lang_Class(false).castEx();",
    1.86 -                 smapper.getT(0, VarType.REFERENCE, false), type.replace('/', '_'));
    1.87 +                 smapper.getT(0, VarType.REFERENCE, false), mangleClassName(type));
    1.88          } else {
    1.89              int cnt = 0;
    1.90              while (type.charAt(cnt) == '[') {
     2.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java	Wed Sep 02 06:14:40 2015 +0200
     2.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java	Wed Sep 02 23:20:01 2015 +0200
     2.3 @@ -732,7 +732,9 @@
     2.4                  + "  var vm = {};\n");
     2.5              append("  function link(n, assign) {\n"
     2.6                  + "    return function() {\n"
     2.7 -                + "      var cls = n['replace__Ljava_lang_String_2CC']"
     2.8 +                + "      var no_ = n['replace__Ljava_lang_String_2Ljava_lang_CharSequence_2Ljava_lang_CharSequence_2']"
     2.9 +                                       + "('_', '_1').toString();\n"
    2.10 +                + "      var cls = no_['replace__Ljava_lang_String_2CC']"
    2.11                                         + "('/', '_').toString();\n"
    2.12                  + "      var dot = n['replace__Ljava_lang_String_2CC']"
    2.13                                         + "('/', '.').toString();\n"
    2.14 @@ -755,7 +757,7 @@
    2.15          @Override
    2.16          protected String generateClass(String className) throws IOException {
    2.17              if (isExternalClass(className)) {
    2.18 -                final String cls = className.replace('/', '_');
    2.19 +                final String cls = className.replace("_", "_1").replace('/', '_');
    2.20                  append("\n").append(assignClass(cls))
    2.21                     .append("link('")
    2.22                     .append(className)
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/Implement_Factory.java	Wed Sep 02 23:20:01 2015 +0200
     3.3 @@ -0,0 +1,54 @@
     3.4 +/**
     3.5 + * Back 2 Browser Bytecode Translator
     3.6 + * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     3.7 + *
     3.8 + * This program is free software: you can redistribute it and/or modify
     3.9 + * it under the terms of the GNU General Public License as published by
    3.10 + * the Free Software Foundation, version 2 of the License.
    3.11 + *
    3.12 + * This program is distributed in the hope that it will be useful,
    3.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    3.15 + * GNU General Public License for more details.
    3.16 + *
    3.17 + * You should have received a copy of the GNU General Public License
    3.18 + * along with this program. Look for COPYING file in the top folder.
    3.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    3.20 + */
    3.21 +package org.apidesign.vm4brwsr;
    3.22 +
    3.23 +import org.apidesign.vm4brwsr.under_score.ImplementInterface;
    3.24 +import org.apidesign.vm4brwsr.under_score.ImportedField;
    3.25 +
    3.26 +/**
    3.27 + *
    3.28 + * @author Jaroslav Tulach
    3.29 + */
    3.30 +public class Implement_Factory {
    3.31 +    private Implement_Factory() {
    3.32 +    }
    3.33 +    
    3.34 +    private static ImplementInterface create() {
    3.35 +        return new Impl();
    3.36 +    }
    3.37 +    
    3.38 +    public static String hello() {
    3.39 +        ImplementInterface i = create();
    3.40 +        return i.sayHello();
    3.41 +    }
    3.42 +    
    3.43 +    public static int meaning() {
    3.44 +        return ImportedField.Factory.create(42).x;
    3.45 +    }
    3.46 +
    3.47 +    private static class Impl implements ImplementInterface {
    3.48 +
    3.49 +        public Impl() {
    3.50 +        }
    3.51 +
    3.52 +        @Override
    3.53 +        public String sayHello() {
    3.54 +            return "Hello!";
    3.55 +        }
    3.56 +    }
    3.57 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/Imported_Field_Test.java	Wed Sep 02 23:20:01 2015 +0200
     4.3 @@ -0,0 +1,57 @@
     4.4 +/**
     4.5 + * Back 2 Browser Bytecode Translator
     4.6 + * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4.7 + *
     4.8 + * This program is free software: you can redistribute it and/or modify
     4.9 + * it under the terms of the GNU General Public License as published by
    4.10 + * the Free Software Foundation, version 2 of the License.
    4.11 + *
    4.12 + * This program is distributed in the hope that it will be useful,
    4.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    4.15 + * GNU General Public License for more details.
    4.16 + *
    4.17 + * You should have received a copy of the GNU General Public License
    4.18 + * along with this program. Look for COPYING file in the top folder.
    4.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    4.20 + */
    4.21 +package org.apidesign.vm4brwsr;
    4.22 +
    4.23 +import javax.script.ScriptEngine;
    4.24 +import org.testng.annotations.AfterClass;
    4.25 +import org.testng.annotations.BeforeClass;
    4.26 +import org.testng.annotations.Test;
    4.27 +
    4.28 +/** Tests whether we can access public field from a class in extension
    4.29 + * in an underscored package.
    4.30 + *
    4.31 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    4.32 + */
    4.33 +public class Imported_Field_Test {
    4.34 +    @Test public void checkHello() throws Exception {
    4.35 +        code.assertExec("Can access field from extension",
    4.36 +            Implement_Factory.class, "meaning__I",
    4.37 +            42
    4.38 +        );
    4.39 +    }
    4.40 +
    4.41 +    private static TestVM code;
    4.42 +    
    4.43 +    @BeforeClass 
    4.44 +    public static void compileTheCode() throws Exception {
    4.45 +        StringBuilder sb = new StringBuilder();
    4.46 +        ScriptEngine[] eng = { null };
    4.47 +        code = TestVM.compileClassesAsExtension(sb, eng, null, null,
    4.48 +            "org/apidesign/vm4brwsr/under_score/ImportedField",
    4.49 +            "org/apidesign/vm4brwsr/under_score/ImportedField$Factory"
    4.50 +        );
    4.51 +        code = TestVM.compileClassesAsExtension(sb, eng, null, null,
    4.52 +            "org/apidesign/vm4brwsr/Implement_Factory",
    4.53 +            "org/apidesign/vm4brwsr/Implement_Factory$Impl"
    4.54 +        );
    4.55 +    }
    4.56 +    @AfterClass
    4.57 +    public static void releaseTheCode() {
    4.58 +        code = null;
    4.59 +    }
    4.60 +}
     5.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java	Wed Sep 02 06:14:40 2015 +0200
     5.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java	Wed Sep 02 23:20:01 2015 +0200
     5.3 @@ -193,7 +193,7 @@
     5.4              addClasses(names).
     5.5              addResources("org/apidesign/vm4brwsr/obj.js").
     5.6              addExported(exp.toArray(new String[0])).
     5.7 -            obfuscation(ObfuscationLevel.FULL).
     5.8 +       //     obfuscation(ObfuscationLevel.FULL).
     5.9              library();
    5.10          if (resourceName != null) {
    5.11              b2b = b2b.addResources(resourceName);
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/under_score/ImplementInterface.java	Wed Sep 02 23:20:01 2015 +0200
     6.3 @@ -0,0 +1,26 @@
     6.4 +/**
     6.5 + * Back 2 Browser Bytecode Translator
     6.6 + * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     6.7 + *
     6.8 + * This program is free software: you can redistribute it and/or modify
     6.9 + * it under the terms of the GNU General Public License as published by
    6.10 + * the Free Software Foundation, version 2 of the License.
    6.11 + *
    6.12 + * This program is distributed in the hope that it will be useful,
    6.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    6.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    6.15 + * GNU General Public License for more details.
    6.16 + *
    6.17 + * You should have received a copy of the GNU General Public License
    6.18 + * along with this program. Look for COPYING file in the top folder.
    6.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    6.20 + */
    6.21 +package org.apidesign.vm4brwsr.under_score;
    6.22 +
    6.23 +/**
    6.24 + *
    6.25 + * @author Jaroslav Tulach
    6.26 + */
    6.27 +public interface ImplementInterface {
    6.28 +    public String sayHello();
    6.29 +}
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/under_score/ImportedField.java	Wed Sep 02 23:20:01 2015 +0200
     7.3 @@ -0,0 +1,36 @@
     7.4 +/**
     7.5 + * Back 2 Browser Bytecode Translator
     7.6 + * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     7.7 + *
     7.8 + * This program is free software: you can redistribute it and/or modify
     7.9 + * it under the terms of the GNU General Public License as published by
    7.10 + * the Free Software Foundation, version 2 of the License.
    7.11 + *
    7.12 + * This program is distributed in the hope that it will be useful,
    7.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    7.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    7.15 + * GNU General Public License for more details.
    7.16 + *
    7.17 + * You should have received a copy of the GNU General Public License
    7.18 + * along with this program. Look for COPYING file in the top folder.
    7.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    7.20 + */
    7.21 +package org.apidesign.vm4brwsr.under_score;
    7.22 +
    7.23 +
    7.24 +public final class ImportedField {
    7.25 +    public final int x;
    7.26 +
    7.27 +    ImportedField(int x) {
    7.28 +        this.x = x;
    7.29 +    }
    7.30 +    
    7.31 +    public static final class Factory {
    7.32 +        public static ImportedField create(int v) {
    7.33 +            return (ImportedField) createImpl(v);
    7.34 +        }
    7.35 +        private static Object createImpl(int v) {
    7.36 +            return new ImportedField(v);
    7.37 +        }
    7.38 +    }
    7.39 +}