rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 28 Apr 2014 10:59:12 +0200
branchclosure
changeset 1495 d5dd07b45f79
parent 1493 234fea368401
child 1496 d3df935aff70
permissions -rw-r--r--
Being able to compile resources into the generated JavaScript file and use them.
     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 = function Invoker() {"
    90                     + "\n    return Invoker.target[Invoker.method]"
    91                                       + ".apply(Invoker.target, arguments);"
    92                     + "\n  };");
    93         generateBody(names);
    94         for (String invokerMethod: invokerMethods.toArray()) {
    95             out.append("\n  invoker." + invokerMethod + " = function(target) {"
    96                            + "\n    invoker.target = target;"
    97                            + "\n    invoker.method = '" + invokerMethod + "';"
    98                            + "\n    return invoker;"
    99                            + "\n  };");
   100         }
   101         
   102         for (String r : asBinary.toArray()) {
   103             out.append("\n  ").append(getExportsObject()).append(".registerResource('");
   104             out.append(r).append("', '");
   105             InputStream is = this.resources.get(r);
   106             byte[] arr = new byte[is.available()];
   107             int len = is.read(arr);
   108             if (len != arr.length) {
   109                 throw new IOException("Not read as much as expected for " + r + " expected: " + arr.length + " was: " + len);
   110             }
   111             out.append(btoa(arr));
   112             out.append("');");
   113         }
   114         
   115         out.append("\n");
   116         generateEpilogue();
   117     }
   118 
   119     @JavaScriptBody(args = { "arr" }, body = "return btoa(arr);")
   120     private static String btoa(byte[] arr) {
   121         return javax.xml.bind.DatatypeConverter.printBase64Binary(arr);
   122     }
   123 
   124     protected abstract void generatePrologue() throws IOException;
   125 
   126     protected abstract void generateEpilogue() throws IOException;
   127 
   128     protected abstract String getExportsObject();
   129 
   130     protected abstract boolean isExternalClass(String className);
   131 
   132     @Override
   133     protected final void declaredClass(ClassData classData, String mangledName)
   134             throws IOException {
   135         if (exportedSymbols.isExported(classData)) {
   136             out.append("\n").append(getExportsObject()).append("['")
   137                                                .append(mangledName)
   138                                                .append("'] = ")
   139                             .append(accessClass(mangledName))
   140                .append(";\n");
   141         }
   142     }
   143 
   144     protected String generateClass(String className) throws IOException {
   145         ClassData classData = classDataCache.getClassData(className);
   146         if (classData == null) {
   147             throw new IOException("Can't find class " + className);
   148         }
   149         return compile(classData);
   150     }
   151 
   152     @Override
   153     protected void declaredField(FieldData fieldData,
   154                                  String destObject,
   155                                  String mangledName) throws IOException {
   156         if (exportedSymbols.isExported(fieldData)) {
   157             exportMember(destObject, mangledName);
   158         }
   159     }
   160 
   161     @Override
   162     protected void declaredMethod(MethodData methodData,
   163                                   String destObject,
   164                                   String mangledName) throws IOException {
   165         if (isHierarchyExported(methodData)) {
   166             exportMember(destObject, mangledName);
   167         }
   168     }
   169 
   170     private void exportMember(String destObject, String memberName)
   171             throws IOException {
   172         out.append("\n").append(destObject).append("['")
   173                                            .append(memberName)
   174                                            .append("'] = ")
   175                         .append(destObject).append(".").append(memberName)
   176            .append(";\n");
   177     }
   178 
   179     private void generateBody(StringArray names) throws IOException {
   180         StringArray processed = new StringArray();
   181         StringArray initCode = new StringArray();
   182         for (String baseClass : names.toArray()) {
   183             references.add(baseClass);
   184             for (;;) {
   185                 String name = null;
   186                 for (String n : references.toArray()) {
   187                     if (processed.contains(n)) {
   188                         continue;
   189                     }
   190                     name = n;
   191                 }
   192                 if (name == null) {
   193                     break;
   194                 }
   195 
   196                 try {
   197                     String ic = generateClass(name);
   198                     processed.add(name);
   199                     initCode.add(ic == null ? "" : ic);
   200                 } catch (RuntimeException ex) {
   201                     if (out instanceof CharSequence) {
   202                         CharSequence seq = (CharSequence)out;
   203                         int lastBlock = seq.length();
   204                         while (lastBlock-- > 0) {
   205                             if (seq.charAt(lastBlock) == '{') {
   206                                 break;
   207                             }
   208                         }
   209                         throw new IOException("Error while compiling " + name + "\n"
   210                             + seq.subSequence(lastBlock + 1, seq.length()), ex
   211                         );
   212                     } else {
   213                         throw new IOException("Error while compiling " + name + "\n"
   214                             + out, ex
   215                         );
   216                     }
   217                 }
   218             }
   219 
   220             for (String resource : scripts.toArray()) {
   221                 while (resource.startsWith("/")) {
   222                     resource = resource.substring(1);
   223                 }
   224                 InputStream emul = resources.get(resource);
   225                 if (emul == null) {
   226                     throw new IOException("Can't find " + resource);
   227                 }
   228                 readResource(emul, out);
   229             }
   230             scripts = new StringArray();
   231 
   232             StringArray toInit = StringArray.asList(references.toArray());
   233             toInit.reverse();
   234 
   235             for (String ic : toInit.toArray()) {
   236                 int indx = processed.indexOf(ic);
   237                 if (indx >= 0) {
   238                     final String theCode = initCode.toArray()[indx];
   239                     if (!theCode.isEmpty()) {
   240                         out.append(theCode).append("\n");
   241                     }
   242                     initCode.toArray()[indx] = "";
   243                 }
   244             }
   245         }
   246     }
   247 
   248     private static void readResource(InputStream emul, Appendable out) throws IOException {
   249         try {
   250             int state = 0;
   251             for (;;) {
   252                 int ch = emul.read();
   253                 if (ch == -1) {
   254                     break;
   255                 }
   256                 if (ch < 0 || ch > 255) {
   257                     throw new IOException("Invalid char in emulation " + ch);
   258                 }
   259                 switch (state) {
   260                     case 0: 
   261                         if (ch == '/') {
   262                             state = 1;
   263                         } else {
   264                             out.append((char)ch);
   265                         }
   266                         break;
   267                     case 1:
   268                         if (ch == '*') {
   269                             state = 2;
   270                         } else {
   271                             out.append('/').append((char)ch);
   272                             state = 0;
   273                         }
   274                         break;
   275                     case 2:
   276                         if (ch == '*') {
   277                             state = 3;
   278                         }
   279                         break;
   280                     case 3:
   281                         if (ch == '/') {
   282                             state = 0;
   283                         } else {
   284                             state = 2;
   285                         }
   286                         break;
   287                 }
   288             }
   289         } finally {
   290             emul.close();
   291         }
   292     }
   293 
   294     static String toString(String name) throws IOException {
   295         StringBuilder sb = new StringBuilder();
   296 //        compile(sb, name);
   297         return sb.toString().toString();
   298     }
   299 
   300     private StringArray scripts = new StringArray();
   301     private StringArray references = new StringArray();
   302     
   303     @Override
   304     protected boolean requireReference(String cn) {
   305         if (references.contains(cn)) {
   306             return false;
   307         }
   308         references.add(cn);
   309         return true;
   310     }
   311 
   312     @Override
   313     protected void requireScript(String resourcePath) {
   314         scripts.add(resourcePath);
   315     }
   316 
   317     @Override
   318     String assignClass(String className) {
   319         return "vm." + className + " = ";
   320     }
   321     
   322     @Override
   323     String accessClass(String className) {
   324         return "vm." + className;
   325     }
   326 
   327     @Override
   328     protected String accessField(String object, String mangledName,
   329                                  String[] fieldInfoName) throws IOException {
   330         final FieldData field =
   331                 classDataCache.findField(fieldInfoName[0],
   332                                          fieldInfoName[1],
   333                                          fieldInfoName[2]);
   334         return accessNonVirtualMember(object, mangledName,
   335                                       (field != null) ? field.cls : null);
   336     }
   337 
   338     @Override
   339     protected String accessStaticMethod(
   340                              String object,
   341                              String mangledName,
   342                              String[] fieldInfoName) throws IOException {
   343         final MethodData method =
   344                 classDataCache.findMethod(fieldInfoName[0],
   345                                           fieldInfoName[1],
   346                                           fieldInfoName[2]);
   347         return accessNonVirtualMember(object, mangledName,
   348                                       (method != null) ? method.cls : null);
   349     }
   350 
   351     @Override
   352     protected String accessVirtualMethod(
   353                              String object,
   354                              String mangledName,
   355                              String[] fieldInfoName) throws IOException {
   356         final ClassData referencedClass =
   357                 classDataCache.getClassData(fieldInfoName[0]);
   358         final MethodData method =
   359                 classDataCache.findMethod(referencedClass,
   360                                           fieldInfoName[1],
   361                                           fieldInfoName[2]);
   362 
   363         if ((method != null)
   364                 && !isExternalClass(method.cls.getClassName())
   365                 && (((method.access & ByteCodeParser.ACC_FINAL) != 0)
   366                         || ((referencedClass.getAccessFlags()
   367                                  & ByteCodeParser.ACC_FINAL) != 0)
   368                         || !isHierarchyExported(method))) {
   369             return object + "." + mangledName;
   370         }
   371 
   372         return accessThroughInvoker(object, mangledName);
   373     }
   374 
   375     private String accessThroughInvoker(String object, String mangledName) {
   376         if (!invokerMethods.contains(mangledName)) {
   377             invokerMethods.add(mangledName);
   378         }
   379         return "invoker." + mangledName + '(' + object + ')';
   380     }
   381 
   382     private boolean isHierarchyExported(final MethodData methodData)
   383             throws IOException {
   384         if (exportedSymbols.isExported(methodData)) {
   385             return true;
   386         }
   387         if ((methodData.access & (ByteCodeParser.ACC_PRIVATE
   388                                       | ByteCodeParser.ACC_STATIC)) != 0) {
   389             return false;
   390         }
   391 
   392         final ExportedMethodFinder exportedMethodFinder =
   393                 new ExportedMethodFinder(exportedSymbols);
   394 
   395         classDataCache.findMethods(
   396                 methodData.cls,
   397                 methodData.getName(),
   398                 methodData.getInternalSig(),
   399                 exportedMethodFinder);
   400 
   401         return (exportedMethodFinder.getFound() != null);
   402     }
   403 
   404     private String accessNonVirtualMember(String object,
   405                                           String mangledName,
   406                                           ClassData declaringClass) {
   407         return ((declaringClass != null)
   408                     && !isExternalClass(declaringClass.getClassName()))
   409                             ? object + "." + mangledName
   410                             : object + "['" + mangledName + "']";
   411     }
   412 
   413     private static final class ExportedMethodFinder
   414             implements ClassDataCache.TraversalCallback<MethodData> {
   415         private final ExportedSymbols exportedSymbols;
   416         private MethodData found;
   417 
   418         public ExportedMethodFinder(final ExportedSymbols exportedSymbols) {
   419             this.exportedSymbols = exportedSymbols;
   420         }
   421 
   422         @Override
   423         public boolean traverse(final MethodData methodData) {
   424             try {
   425                 if (exportedSymbols.isExported(methodData)) {
   426                     found = methodData;
   427                     return false;
   428                 }
   429             } catch (final IOException e) {
   430             }
   431 
   432             return true;
   433         }
   434 
   435         public MethodData getFound() {
   436             return found;
   437         }
   438     }
   439 
   440     private static final class Standalone extends VM {
   441         private Standalone(Appendable out, Bck2Brwsr.Resources resources, StringArray explicitlyExported) {
   442             super(out, resources, explicitlyExported);
   443         }
   444 
   445         @Override
   446         protected void generatePrologue() throws IOException {
   447             out.append("(function VM(global) {var fillInVMSkeleton = function(vm) {");
   448         }
   449 
   450         @Override
   451         protected void generateEpilogue() throws IOException {
   452             out.append(
   453                   "  return vm;\n"
   454                 + "  };\n"
   455                 + "  var extensions = [];\n"
   456                 + "  global.bck2brwsr = function() {\n"
   457                 + "    var args = Array.prototype.slice.apply(arguments);\n"
   458                 + "    var resources = {};\n"
   459                 + "    function registerResource(n, a64) {\n"
   460                 + "      var str = atob(a64);\n"
   461                 + "      var arr = [];\n"
   462                 + "      for (var i = 0; i < str.length; i++) arr.push(str.charCodeAt(i) & 0xff);\n"
   463                 + "      if (!resources[n]) resources[n] = [arr];\n"
   464                 + "      else resources[n].push(arr);\n"
   465                 + "    }\n"
   466                 + "    var vm = fillInVMSkeleton({ 'registerResource' : registerResource });\n"
   467                 + "    for (var i = 0; i < extensions.length; ++i) {\n"
   468                 + "      extensions[i](vm);\n"
   469                 + "    }\n"
   470                 + "    vm.registerResource = null;\n"
   471                 + "    var knownExtensions = extensions.length;\n"
   472                 + "    var loader = {};\n"
   473                 + "    loader.vm = vm;\n"
   474                 + "    loader.loadClass = function(name) {\n"
   475                 + "      var attr = name.replace__Ljava_lang_String_2CC('.','_');\n"
   476                 + "      var fn = vm[attr];\n"
   477                 + "      if (fn) return fn(false);\n"
   478                 + "      try {\n"
   479                 + "        return vm.org_apidesign_vm4brwsr_VMLazy(false).\n"
   480                 + "          load__Ljava_lang_Object_2Ljava_lang_Object_2Ljava_lang_String_2_3Ljava_lang_Object_2(loader, name, args);\n"
   481                 + "      } catch (err) {\n"
   482                 + "        while (knownExtensions < extensions.length) {\n"
   483                 + "          vm.registerResource = registerResource;\n"
   484                 + "          extensions[knownExtensions++](vm);\n"
   485                 + "          vm.registerResource = null;\n"
   486                 + "        }\n"
   487                 + "        fn = vm[attr];\n"
   488                 + "        if (fn) return fn(false);\n"
   489                 + "        throw err;\n"
   490                 + "      }\n"
   491                 + "    }\n"
   492                 + "    if (vm.loadClass) {\n"
   493                 + "      throw 'Cannot initialize the bck2brwsr VM twice!';\n"
   494                 + "    }\n"
   495                 + "    vm.loadClass = loader.loadClass;\n"
   496                 + "    vm.loadBytes = function(name) {\n"
   497                 + "      if (resources[name]) return resources[name][0];\n"
   498                 + "      return vm.org_apidesign_vm4brwsr_VMLazy(false).\n"
   499                 + "        loadBytes___3BLjava_lang_Object_2Ljava_lang_String_2_3Ljava_lang_Object_2(loader, name, args);\n"
   500                 + "    }\n"
   501                 + "    vm.java_lang_reflect_Array(false);\n"
   502                 + "    vm.org_apidesign_vm4brwsr_VMLazy(false).\n"
   503                 + "      loadBytes___3BLjava_lang_Object_2Ljava_lang_String_2_3Ljava_lang_Object_2(loader, null, args);\n"
   504                 + "    return loader;\n"
   505                 + "  };\n");
   506             out.append(
   507                   "  global.bck2brwsr.registerExtension = function(extension) {\n"
   508                 + "    extensions.push(extension);\n"
   509                 + "    return null;\n"
   510                 + "  };\n");
   511             out.append("}(this));");
   512         }
   513 
   514         @Override
   515         protected String getExportsObject() {
   516             return "vm";
   517         }
   518 
   519         @Override
   520         protected boolean isExternalClass(String className) {
   521             return false;
   522         }
   523     }
   524 
   525     private static final class Extension extends VM {
   526         private final StringArray extensionClasses;
   527 
   528         private Extension(Appendable out, Bck2Brwsr.Resources resources,
   529                           String[] extClassesArray, StringArray explicitlyExported) {
   530             super(out, resources, explicitlyExported);
   531             this.extensionClasses = StringArray.asList(extClassesArray);
   532         }
   533 
   534         @Override
   535         protected void generatePrologue() throws IOException {
   536             out.append("bck2brwsr.registerExtension(function(exports) {\n"
   537                            + "  var vm = {};\n");
   538             out.append("  function link(n, inst) {\n"
   539                            + "    var cls = n['replace__Ljava_lang_String_2CC']"
   540                                                   + "('/', '_').toString();\n"
   541                            + "    var dot = n['replace__Ljava_lang_String_2CC']"
   542                                                   + "('/', '.').toString();\n"
   543                            + "    exports.loadClass(dot);\n"
   544                            + "    vm[cls] = exports[cls];\n"
   545                            + "    return vm[cls](inst);\n"
   546                            + "  };\n");
   547         }
   548 
   549         @Override
   550         protected void generateEpilogue() throws IOException {
   551             out.append("});");
   552         }
   553 
   554         @Override
   555         protected String generateClass(String className) throws IOException {
   556             if (isExternalClass(className)) {
   557                 out.append("\n").append(assignClass(
   558                                             className.replace('/', '_')))
   559                    .append("function() {\n  return link('")
   560                    .append(className)
   561                    .append("', arguments.length == 0 || arguments[0] === true);"
   562                                + "\n};");
   563 
   564                 return null;
   565             }
   566 
   567             return super.generateClass(className);
   568         }
   569 
   570         @Override
   571         protected String getExportsObject() {
   572             return "exports";
   573         }
   574 
   575         @Override
   576         protected boolean isExternalClass(String className) {
   577             return !extensionClasses.contains(className);
   578         }
   579     }
   580 }