Hiding all generated class methods from sight of external users. Exposing only bck2brwsr entry point function lazyvm
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 06 Dec 2012 21:31:09 +0100
branchlazyvm
changeset 27481f6e7778135
parent 273 59b9f9f5364f
child 275 6e8422eca1ed
Hiding all generated class methods from sight of external users. Exposing only bck2brwsr entry point function
emul/src/main/resources/org/apidesign/vm4brwsr/emul/java_lang_String.js
javap/src/main/java/org/apidesign/javap/ClassData.java
vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java
vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java
vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java
     1.1 --- a/emul/src/main/resources/org/apidesign/vm4brwsr/emul/java_lang_String.js	Thu Dec 06 16:13:03 2012 +0100
     1.2 +++ b/emul/src/main/resources/org/apidesign/vm4brwsr/emul/java_lang_String.js	Thu Dec 06 21:31:09 2012 +0100
     1.3 @@ -1,5 +1,5 @@
     1.4  // initialize methods on String constants
     1.5 -java_lang_String(false);
     1.6 +vm.java_lang_String(false);
     1.7  
     1.8  // we need initialized arrays
     1.9  Array.prototype.fillNulls = function() {
     2.1 --- a/javap/src/main/java/org/apidesign/javap/ClassData.java	Thu Dec 06 16:13:03 2012 +0100
     2.2 +++ b/javap/src/main/java/org/apidesign/javap/ClassData.java	Thu Dec 06 21:31:09 2012 +0100
     2.3 @@ -559,7 +559,7 @@
     2.4                  if (refs != null) {
     2.5                      refs[0] = jn;
     2.6                  }
     2.7 -                return jn.replace('/', '_') + "(false).constructor.$class";
     2.8 +                return jn;
     2.9              }
    2.10              return jn;
    2.11          case CONSTANT_STRING:
     3.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Thu Dec 06 16:13:03 2012 +0100
     3.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Thu Dec 06 21:31:09 2012 +0100
     3.3 @@ -56,9 +56,12 @@
     3.4       * 
     3.5       * @param className suggested name of the class
     3.6       */
     3.7 -    protected String assignClass(String className) {
     3.8 +    /* protected */ String assignClass(String className) {
     3.9          return className + " = ";
    3.10      }
    3.11 +    /* protected */ String accessClass(String classOperation) {
    3.12 +        return classOperation;
    3.13 +    }
    3.14  
    3.15      /**
    3.16       * Converts a given class file to a JavaScript version.
    3.17 @@ -99,7 +102,7 @@
    3.18          if (proto == null) {
    3.19              String sc = jc.getSuperClassName(); // with _
    3.20              out.append("\n    var pp = ").
    3.21 -                append(sc.replace('/', '_')).append("(true);");
    3.22 +                append(accessClass(sc.replace('/', '_'))).append("(true);");
    3.23              out.append("\n    var p = CLS.prototype = pp;");
    3.24              out.append("\n    var c = p;");
    3.25              out.append("\n    var sprcls = pp.constructor.$class;");
    3.26 @@ -139,7 +142,8 @@
    3.27          for (String superInterface : jc.getSuperInterfaces()) {
    3.28              out.append("\n    c.$instOf_").append(superInterface.replace('/', '_')).append(" = true;");
    3.29          }
    3.30 -        out.append("\n    CLS.$class = java_lang_Class(true);");
    3.31 +        out.append("\n    CLS.$class = ");
    3.32 +        out.append(accessClass("java_lang_Class(true);"));
    3.33          out.append("\n    CLS.$class.jvmName = '").append(jc.getClassName()).append("';");
    3.34          out.append("\n    CLS.$class.superclass = sprcls;");
    3.35          out.append("\n    CLS.$class.cnstr = CLS;");
    3.36 @@ -191,7 +195,7 @@
    3.37          final String mn = findMethodName(m, argsCnt);
    3.38          out.append(prefix).append(mn).append(" = function");
    3.39          if (mn.equals("class__V")) {
    3.40 -            toInitilize.add(className(jc) + "(false)." + mn);
    3.41 +            toInitilize.add(accessClass(className(jc)) + "(false)." + mn);
    3.42          }
    3.43          out.append('(');
    3.44          String space = "";
    3.45 @@ -638,7 +642,7 @@
    3.46                      int indx = readIntArg(byteCodes, i);
    3.47                      String ci = jc.getClassName(indx);
    3.48                      out.append("s.push(new ");
    3.49 -                    out.append(ci.replace('/','_'));
    3.50 +                    out.append(accessClass(ci.replace('/','_')));
    3.51                      out.append("());");
    3.52                      addReference(ci);
    3.53                      i += 2;
    3.54 @@ -731,7 +735,7 @@
    3.55                  case opc_getstatic: {
    3.56                      int indx = readIntArg(byteCodes, i);
    3.57                      String[] fi = jc.getFieldInfoName(indx);
    3.58 -                    out.append("s.push(").append(fi[0].replace('/', '_'));
    3.59 +                    out.append("s.push(").append(accessClass(fi[0].replace('/', '_')));
    3.60                      out.append('.').append(fi[1]).append(");");
    3.61                      i += 2;
    3.62                      addReference(fi[0]);
    3.63 @@ -740,7 +744,7 @@
    3.64                  case opc_putstatic: {
    3.65                      int indx = readIntArg(byteCodes, i);
    3.66                      String[] fi = jc.getFieldInfoName(indx);
    3.67 -                    out.append(fi[0].replace('/', '_'));
    3.68 +                    out.append(accessClass(fi[0].replace('/', '_')));
    3.69                      out.append('.').append(fi[1]).append(" = s.pop();");
    3.70                      i += 2;
    3.71                      addReference(fi[0]);
    3.72 @@ -951,7 +955,7 @@
    3.73              out.append("s.push(");
    3.74          }
    3.75          final String in = mi[0];
    3.76 -        out.append(in.replace('/', '_'));
    3.77 +        out.append(accessClass(in.replace('/', '_')));
    3.78          out.append("(false).");
    3.79          out.append(mn);
    3.80          out.append('(');
    3.81 @@ -1031,6 +1035,7 @@
    3.82          String s = jc.stringValue(entryIndex, classRef);
    3.83          if (classRef[0] != null) {
    3.84              addReference(classRef[0]);
    3.85 +            s = accessClass(s.replace('/', '_')) + "(false).constructor.$class";
    3.86          }
    3.87          return s;
    3.88      }
     4.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java	Thu Dec 06 16:13:03 2012 +0100
     4.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java	Thu Dec 06 21:31:09 2012 +0100
     4.3 @@ -44,6 +44,8 @@
     4.4          new GenJS(out).doCompile(l, names);
     4.5      }
     4.6      protected void doCompile(ClassLoader l, StringArray names) throws IOException {
     4.7 +        out.append("(function VM(global) {");
     4.8 +        out.append("\n  var vm = {};");
     4.9          StringArray processed = new StringArray();
    4.10          StringArray initCode = new StringArray();
    4.11          for (String baseClass : names.toArray()) {
    4.12 @@ -109,8 +111,12 @@
    4.13                      initCode.toArray()[indx] = "";
    4.14                  }
    4.15              }
    4.16 -
    4.17          }
    4.18 +        out.append(
    4.19 +              "  global.bck2brwsr = function() { return {\n"
    4.20 +            + "    loadClass : function(name) { return vm[name](true); }\n"
    4.21 +            + "  };\n};\n");
    4.22 +        out.append("}(this));");
    4.23      }
    4.24      private static void readResource(InputStream emul, Appendable out) throws IOException {
    4.25          try {
    4.26 @@ -195,4 +201,14 @@
    4.27      protected void requireScript(String resourcePath) {
    4.28          scripts.add(resourcePath);
    4.29      }
    4.30 +
    4.31 +    @Override
    4.32 +    String assignClass(String className) {
    4.33 +        return "vm." + className + " = ";
    4.34 +    }
    4.35 +    
    4.36 +    @Override
    4.37 +    String accessClass(String className) {
    4.38 +        return "vm." + className;
    4.39 +    }
    4.40  }
     5.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java	Thu Dec 06 16:13:03 2012 +0100
     5.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java	Thu Dec 06 21:31:09 2012 +0100
     5.3 @@ -161,7 +161,8 @@
     5.4  
     5.5          Object ret = null;
     5.6          try {
     5.7 -            ret = code.invokeFunction(clazz.getName().replace('.', '_'), true);
     5.8 +            ret = code.invokeFunction("bck2brwsr");
     5.9 +            ret = code.invokeMethod(ret, "loadClass", clazz.getName().replace('.', '_'));
    5.10              ret = code.invokeMethod(ret, method, args);
    5.11          } catch (ScriptException ex) {
    5.12              fail("Execution failed in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
     6.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java	Thu Dec 06 16:13:03 2012 +0100
     6.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java	Thu Dec 06 21:31:09 2012 +0100
     6.3 @@ -262,7 +262,8 @@
     6.4      ) throws Exception {
     6.5          Object ret = null;
     6.6          try {
     6.7 -            ret = toRun.invokeFunction(clazz.getName().replace('.', '_'), true);
     6.8 +            ret = toRun.invokeFunction("bck2brwsr");
     6.9 +            ret = toRun.invokeMethod(ret, "loadClass", clazz.getName().replace('.', '_'));
    6.10              ret = toRun.invokeMethod(ret, method, args);
    6.11          } catch (ScriptException ex) {
    6.12              fail("Execution failed in\n" + dumpJS(theCode), ex);