rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 29 Apr 2014 15:25:58 +0200
branchclosure
changeset 1508 e995e8d39240
parent 1496 d3df935aff70
child 1513 ba912ef24b27
permissions -rw-r--r--
Stateless invoker. Don't use any static variables, that has to confuse any JavaScript VM a lot.
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.vm4brwsr;
    19 
    20 import java.io.IOException;
    21 import java.io.InputStream;
    22 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    23 import org.apidesign.vm4brwsr.ByteCodeParser.ClassData;
    24 import org.apidesign.vm4brwsr.ByteCodeParser.FieldData;
    25 import org.apidesign.vm4brwsr.ByteCodeParser.MethodData;
    26 
    27 /** Generator of JavaScript from bytecode of classes on classpath of the VM.
    28  *
    29  * @author Jaroslav Tulach <jtulach@netbeans.org>
    30  */
    31 abstract class VM extends ByteCodeToJavaScript {
    32     protected final ClassDataCache classDataCache;
    33 
    34     private final Bck2Brwsr.Resources resources;
    35     private final ExportedSymbols exportedSymbols;
    36     private final StringArray invokerMethods;
    37 
    38     private static final Class<?> FIXED_DEPENDENCIES[] = {
    39             Class.class,
    40             ArithmeticException.class,
    41             VM.class
    42         };
    43 
    44     private VM(Appendable out, Bck2Brwsr.Resources resources, StringArray explicitlyExported) {
    45         super(out);
    46         this.resources = resources;
    47         this.classDataCache = new ClassDataCache(resources);
    48         this.exportedSymbols = new ExportedSymbols(resources, explicitlyExported);
    49         this.invokerMethods = new StringArray();
    50     }
    51 
    52     static {
    53         // uses VMLazy to load dynamic classes
    54         boolean assertsOn = false;
    55         assert assertsOn = true;
    56         if (assertsOn) {
    57             VMLazy.init();
    58             Zips.init();
    59         }
    60     }
    61 
    62     @Override
    63     boolean debug(String msg) throws IOException {
    64         return false;
    65     }
    66     
    67     static void compile(Appendable out, 
    68         Bck2Brwsr config
    69     ) throws IOException {
    70         String[] both = config.allClasses();
    71         
    72         VM vm = config.isExtension() ? 
    73             new Extension(out, config.getResources(), both, config.rootClasses())
    74             : 
    75             new Standalone(out, config.getResources(), config.rootClasses());
    76 
    77         final StringArray fixedNames = new StringArray();
    78 
    79         for (final Class<?> fixedClass: FIXED_DEPENDENCIES) {
    80             fixedNames.add(fixedClass.getName().replace('.', '/'));
    81         }
    82 
    83         vm.doCompile(fixedNames.addAndNew(both), config.allResources());
    84     }
    85 
    86     private void doCompile(StringArray names, StringArray asBinary) throws IOException {
    87         generatePrologue();
    88         out.append(
    89                 "\n  var invoker = {};");
    90         generateBody(names);
    91         for (String invokerMethod: invokerMethods.toArray()) {
    92             out.append("\n  invoker." + invokerMethod + " = function(target) {"
    93                 + "\n    return function() {"
    94                 + "\n      return target['" + invokerMethod + "'].apply(target, arguments);"
    95                 + "\n    };"
    96                 + "\n  };"
    97             );
    98         }
    99         
   100         for (String r : asBinary.toArray()) {
   101             out.append("\n  ").append(getExportsObject()).append(".registerResource('");
   102             out.append(r).append("', '");
   103             InputStream is = this.resources.get(r);
   104             byte[] arr = new byte[is.available()];
   105             int len = is.read(arr);
   106             if (len != arr.length) {
   107                 throw new IOException("Not read as much as expected for " + r + " expected: " + arr.length + " was: " + len);
   108             }
   109             out.append(btoa(arr));
   110             out.append("');");
   111         }
   112         
   113         out.append("\n");
   114         generateEpilogue();
   115     }
   116 
   117     @JavaScriptBody(args = { "arr" }, body = "return btoa(arr);")
   118     private static String btoa(byte[] arr) {
   119         return javax.xml.bind.DatatypeConverter.printBase64Binary(arr);
   120     }
   121 
   122     protected abstract void generatePrologue() throws IOException;
   123 
   124     protected abstract void generateEpilogue() throws IOException;
   125 
   126     protected abstract String getExportsObject();
   127 
   128     protected abstract boolean isExternalClass(String className);
   129 
   130     @Override
   131     protected final void declaredClass(ClassData classData, String mangledName)
   132             throws IOException {
   133         if (exportedSymbols.isExported(classData)) {
   134             out.append("\n").append(getExportsObject()).append("['")
   135                                                .append(mangledName)
   136                                                .append("'] = ")
   137                             .append(accessClass(mangledName))
   138                .append(";\n");
   139         }
   140     }
   141 
   142     protected String generateClass(String className) throws IOException {
   143         ClassData classData = classDataCache.getClassData(className);
   144         if (classData == null) {
   145             throw new IOException("Can't find class " + className);
   146         }
   147         return compile(classData);
   148     }
   149 
   150     @Override
   151     protected void declaredField(FieldData fieldData,
   152                                  String destObject,
   153                                  String mangledName) throws IOException {
   154         if (exportedSymbols.isExported(fieldData)) {
   155             exportMember(destObject, mangledName);
   156         }
   157     }
   158 
   159     @Override
   160     protected void declaredMethod(MethodData methodData,
   161                                   String destObject,
   162                                   String mangledName) throws IOException {
   163         if (isHierarchyExported(methodData)) {
   164             exportMember(destObject, mangledName);
   165         }
   166     }
   167 
   168     private void exportMember(String destObject, String memberName)
   169             throws IOException {
   170         out.append("\n").append(destObject).append("['")
   171                                            .append(memberName)
   172                                            .append("'] = ")
   173                         .append(destObject).append(".").append(memberName)
   174            .append(";\n");
   175     }
   176 
   177     private void generateBody(StringArray names) throws IOException {
   178         StringArray processed = new StringArray();
   179         StringArray initCode = new StringArray();
   180         for (String baseClass : names.toArray()) {
   181             references.add(baseClass);
   182             for (;;) {
   183                 String name = null;
   184                 for (String n : references.toArray()) {
   185                     if (processed.contains(n)) {
   186                         continue;
   187                     }
   188                     name = n;
   189                 }
   190                 if (name == null) {
   191                     break;
   192                 }
   193 
   194                 try {
   195                     String ic = generateClass(name);
   196                     processed.add(name);
   197                     initCode.add(ic == null ? "" : ic);
   198                 } catch (RuntimeException ex) {
   199                     if (out instanceof CharSequence) {
   200                         CharSequence seq = (CharSequence)out;
   201                         int lastBlock = seq.length();
   202                         while (lastBlock-- > 0) {
   203                             if (seq.charAt(lastBlock) == '{') {
   204                                 break;
   205                             }
   206                         }
   207                         throw new IOException("Error while compiling " + name + "\n"
   208                             + seq.subSequence(lastBlock + 1, seq.length()), ex
   209                         );
   210                     } else {
   211                         throw new IOException("Error while compiling " + name + "\n"
   212                             + out, ex
   213                         );
   214                     }
   215                 }
   216             }
   217 
   218             for (String resource : scripts.toArray()) {
   219                 while (resource.startsWith("/")) {
   220                     resource = resource.substring(1);
   221                 }
   222                 InputStream emul = resources.get(resource);
   223                 if (emul == null) {
   224                     throw new IOException("Can't find " + resource);
   225                 }
   226                 readResource(emul, out);
   227             }
   228             scripts = new StringArray();
   229 
   230             StringArray toInit = StringArray.asList(references.toArray());
   231             toInit.reverse();
   232 
   233             for (String ic : toInit.toArray()) {
   234                 int indx = processed.indexOf(ic);
   235                 if (indx >= 0) {
   236                     final String theCode = initCode.toArray()[indx];
   237                     if (!theCode.isEmpty()) {
   238                         out.append(theCode).append("\n");
   239                     }
   240                     initCode.toArray()[indx] = "";
   241                 }
   242             }
   243         }
   244     }
   245 
   246     private static void readResource(InputStream emul, Appendable out) throws IOException {
   247         try {
   248             int state = 0;
   249             for (;;) {
   250                 int ch = emul.read();
   251                 if (ch == -1) {
   252                     break;
   253                 }
   254                 if (ch < 0 || ch > 255) {
   255                     throw new IOException("Invalid char in emulation " + ch);
   256                 }
   257                 switch (state) {
   258                     case 0: 
   259                         if (ch == '/') {
   260                             state = 1;
   261                         } else {
   262                             out.append((char)ch);
   263                         }
   264                         break;
   265                     case 1:
   266                         if (ch == '*') {
   267                             state = 2;
   268                         } else {
   269                             out.append('/').append((char)ch);
   270                             state = 0;
   271                         }
   272                         break;
   273                     case 2:
   274                         if (ch == '*') {
   275                             state = 3;
   276                         }
   277                         break;
   278                     case 3:
   279                         if (ch == '/') {
   280                             state = 0;
   281                         } else {
   282                             state = 2;
   283                         }
   284                         break;
   285                 }
   286             }
   287         } finally {
   288             emul.close();
   289         }
   290     }
   291 
   292     static String toString(String name) throws IOException {
   293         StringBuilder sb = new StringBuilder();
   294 //        compile(sb, name);
   295         return sb.toString().toString();
   296     }
   297 
   298     private StringArray scripts = new StringArray();
   299     private StringArray references = new StringArray();
   300     
   301     @Override
   302     protected boolean requireReference(String cn) {
   303         if (references.contains(cn)) {
   304             return false;
   305         }
   306         references.add(cn);
   307         return true;
   308     }
   309 
   310     @Override
   311     protected void requireScript(String resourcePath) {
   312         scripts.add(resourcePath);
   313     }
   314 
   315     @Override
   316     String assignClass(String className) {
   317         return "vm." + className + " = ";
   318     }
   319     
   320     @Override
   321     String accessClass(String className) {
   322         return "vm." + className;
   323     }
   324 
   325     @Override
   326     protected String accessField(String object, String mangledName,
   327                                  String[] fieldInfoName) throws IOException {
   328         final FieldData field =
   329                 classDataCache.findField(fieldInfoName[0],
   330                                          fieldInfoName[1],
   331                                          fieldInfoName[2]);
   332         return accessNonVirtualMember(object, mangledName,
   333                                       (field != null) ? field.cls : null);
   334     }
   335 
   336     @Override
   337     protected String accessStaticMethod(
   338                              String object,
   339                              String mangledName,
   340                              String[] fieldInfoName) throws IOException {
   341         final MethodData method =
   342                 classDataCache.findMethod(fieldInfoName[0],
   343                                           fieldInfoName[1],
   344                                           fieldInfoName[2]);
   345         return accessNonVirtualMember(object, mangledName,
   346                                       (method != null) ? method.cls : null);
   347     }
   348 
   349     @Override
   350     protected String accessVirtualMethod(
   351                              String object,
   352                              String mangledName,
   353                              String[] fieldInfoName) throws IOException {
   354         final ClassData referencedClass =
   355                 classDataCache.getClassData(fieldInfoName[0]);
   356         final MethodData method =
   357                 classDataCache.findMethod(referencedClass,
   358                                           fieldInfoName[1],
   359                                           fieldInfoName[2]);
   360 
   361         if ((method != null)
   362                 && !isExternalClass(method.cls.getClassName())
   363                 && (((method.access & ByteCodeParser.ACC_FINAL) != 0)
   364                         || ((referencedClass.getAccessFlags()
   365                                  & ByteCodeParser.ACC_FINAL) != 0)
   366                         || !isHierarchyExported(method))) {
   367             return object + "." + mangledName;
   368         }
   369 
   370         return accessThroughInvoker(object, mangledName);
   371     }
   372 
   373     private String accessThroughInvoker(String object, String mangledName) {
   374         if (!invokerMethods.contains(mangledName)) {
   375             invokerMethods.add(mangledName);
   376         }
   377         return "invoker." + mangledName + '(' + object + ')';
   378     }
   379 
   380     private boolean isHierarchyExported(final MethodData methodData)
   381             throws IOException {
   382         if (exportedSymbols.isExported(methodData)) {
   383             return true;
   384         }
   385         if ((methodData.access & (ByteCodeParser.ACC_PRIVATE
   386                                       | ByteCodeParser.ACC_STATIC)) != 0) {
   387             return false;
   388         }
   389 
   390         final ExportedMethodFinder exportedMethodFinder =
   391                 new ExportedMethodFinder(exportedSymbols);
   392 
   393         classDataCache.findMethods(
   394                 methodData.cls,
   395                 methodData.getName(),
   396                 methodData.getInternalSig(),
   397                 exportedMethodFinder);
   398 
   399         return (exportedMethodFinder.getFound() != null);
   400     }
   401 
   402     private String accessNonVirtualMember(String object,
   403                                           String mangledName,
   404                                           ClassData declaringClass) {
   405         return ((declaringClass != null)
   406                     && !isExternalClass(declaringClass.getClassName()))
   407                             ? object + "." + mangledName
   408                             : object + "['" + mangledName + "']";
   409     }
   410 
   411     private static final class ExportedMethodFinder
   412             implements ClassDataCache.TraversalCallback<MethodData> {
   413         private final ExportedSymbols exportedSymbols;
   414         private MethodData found;
   415 
   416         public ExportedMethodFinder(final ExportedSymbols exportedSymbols) {
   417             this.exportedSymbols = exportedSymbols;
   418         }
   419 
   420         @Override
   421         public boolean traverse(final MethodData methodData) {
   422             try {
   423                 if (exportedSymbols.isExported(methodData)) {
   424                     found = methodData;
   425                     return false;
   426                 }
   427             } catch (final IOException e) {
   428             }
   429 
   430             return true;
   431         }
   432 
   433         public MethodData getFound() {
   434             return found;
   435         }
   436     }
   437 
   438     private static final class Standalone extends VM {
   439         private Standalone(Appendable out, Bck2Brwsr.Resources resources, StringArray explicitlyExported) {
   440             super(out, resources, explicitlyExported);
   441         }
   442 
   443         @Override
   444         protected void generatePrologue() throws IOException {
   445             out.append("(function VM(global) {var fillInVMSkeleton = function(vm) {");
   446         }
   447 
   448         @Override
   449         protected void generateEpilogue() throws IOException {
   450             out.append(
   451                   "  return vm;\n"
   452                 + "  };\n"
   453                 + "  var extensions = [];\n"
   454                 + "  global.bck2brwsr = function() {\n"
   455                 + "    var args = Array.prototype.slice.apply(arguments);\n"
   456                 + "    var resources = {};\n"
   457                 + "    function registerResource(n, a64) {\n"
   458                 + "      var str = atob(a64);\n"
   459                 + "      var arr = [];\n"
   460                 + "      for (var i = 0; i < str.length; i++) {\n"
   461                 + "        var ch = str.charCodeAt(i) & 0xff;\n"
   462                 + "        if (ch > 127) ch -= 256;\n"
   463                 + "        arr.push(ch);\n"
   464                 + "      }\n"
   465                 + "      if (!resources[n]) resources[n] = [arr];\n"
   466                 + "      else resources[n].push(arr);\n"
   467                 + "    }\n"
   468                 + "    var vm = fillInVMSkeleton({ 'registerResource' : registerResource });\n"
   469                 + "    for (var i = 0; i < extensions.length; ++i) {\n"
   470                 + "      extensions[i](vm);\n"
   471                 + "    }\n"
   472                 + "    vm.registerResource = null;\n"
   473                 + "    var knownExtensions = extensions.length;\n"
   474                 + "    var loader = {};\n"
   475                 + "    loader.vm = vm;\n"
   476                 + "    loader.loadClass = function(name) {\n"
   477                 + "      var attr = name.replace__Ljava_lang_String_2CC('.','_');\n"
   478                 + "      var fn = vm[attr];\n"
   479                 + "      if (fn) return fn(false);\n"
   480                 + "      try {\n"
   481                 + "        return vm.org_apidesign_vm4brwsr_VMLazy(false).\n"
   482                 + "          load__Ljava_lang_Object_2Ljava_lang_Object_2Ljava_lang_String_2_3Ljava_lang_Object_2(loader, name, args);\n"
   483                 + "      } catch (err) {\n"
   484                 + "        while (knownExtensions < extensions.length) {\n"
   485                 + "          vm.registerResource = registerResource;\n"
   486                 + "          extensions[knownExtensions++](vm);\n"
   487                 + "          vm.registerResource = null;\n"
   488                 + "        }\n"
   489                 + "        fn = vm[attr];\n"
   490                 + "        if (fn) return fn(false);\n"
   491                 + "        throw err;\n"
   492                 + "      }\n"
   493                 + "    }\n"
   494                 + "    if (vm.loadClass) {\n"
   495                 + "      throw 'Cannot initialize the bck2brwsr VM twice!';\n"
   496                 + "    }\n"
   497                 + "    vm.loadClass = loader.loadClass;\n"
   498                 + "    vm.loadBytes = function(name) {\n"
   499                 + "      if (resources[name]) return resources[name][0];\n"
   500                 + "      return vm.org_apidesign_vm4brwsr_VMLazy(false).\n"
   501                 + "        loadBytes___3BLjava_lang_Object_2Ljava_lang_String_2_3Ljava_lang_Object_2(loader, name, args);\n"
   502                 + "    }\n"
   503                 + "    vm.java_lang_reflect_Array(false);\n"
   504                 + "    vm.org_apidesign_vm4brwsr_VMLazy(false).\n"
   505                 + "      loadBytes___3BLjava_lang_Object_2Ljava_lang_String_2_3Ljava_lang_Object_2(loader, null, args);\n"
   506                 + "    return loader;\n"
   507                 + "  };\n");
   508             out.append(
   509                   "  global.bck2brwsr.registerExtension = function(extension) {\n"
   510                 + "    extensions.push(extension);\n"
   511                 + "    return null;\n"
   512                 + "  };\n");
   513             out.append("}(this));");
   514         }
   515 
   516         @Override
   517         protected String getExportsObject() {
   518             return "vm";
   519         }
   520 
   521         @Override
   522         protected boolean isExternalClass(String className) {
   523             return false;
   524         }
   525     }
   526 
   527     private static final class Extension extends VM {
   528         private final StringArray extensionClasses;
   529 
   530         private Extension(Appendable out, Bck2Brwsr.Resources resources,
   531                           String[] extClassesArray, StringArray explicitlyExported) {
   532             super(out, resources, explicitlyExported);
   533             this.extensionClasses = StringArray.asList(extClassesArray);
   534         }
   535 
   536         @Override
   537         protected void generatePrologue() throws IOException {
   538             out.append("bck2brwsr.registerExtension(function(exports) {\n"
   539                            + "  var vm = {};\n");
   540             out.append("  function link(n, inst) {\n"
   541                            + "    var cls = n['replace__Ljava_lang_String_2CC']"
   542                                                   + "('/', '_').toString();\n"
   543                            + "    var dot = n['replace__Ljava_lang_String_2CC']"
   544                                                   + "('/', '.').toString();\n"
   545                            + "    exports.loadClass(dot);\n"
   546                            + "    vm[cls] = exports[cls];\n"
   547                            + "    return vm[cls](inst);\n"
   548                            + "  };\n");
   549         }
   550 
   551         @Override
   552         protected void generateEpilogue() throws IOException {
   553             out.append("});");
   554         }
   555 
   556         @Override
   557         protected String generateClass(String className) throws IOException {
   558             if (isExternalClass(className)) {
   559                 out.append("\n").append(assignClass(
   560                                             className.replace('/', '_')))
   561                    .append("function() {\n  return link('")
   562                    .append(className)
   563                    .append("', arguments.length == 0 || arguments[0] === true);"
   564                                + "\n};");
   565 
   566                 return null;
   567             }
   568 
   569             return super.generateClass(className);
   570         }
   571 
   572         @Override
   573         protected String getExportsObject() {
   574             return "exports";
   575         }
   576 
   577         @Override
   578         protected boolean isExternalClass(String className) {
   579             return !extensionClasses.contains(className);
   580         }
   581     }
   582 }