launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 26 Apr 2014 19:13:56 +0200
branchclosure
changeset 1489 8d0fc428ff72
parent 1088 4b65abc39565
child 1492 e38025e9536a
permissions -rw-r--r--
Can execute VMTest in browser, if it is annotated with @Exported annotation like the BooleanTest
     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.bck2brwsr.launcher;
    19 
    20 import java.io.File;
    21 import java.io.IOException;
    22 import java.io.InputStream;
    23 import java.net.URL;
    24 import java.util.jar.JarFile;
    25 import org.apidesign.vm4brwsr.Bck2Brwsr;
    26 
    27 /**
    28  * Lightweight server to launch Bck2Brwsr applications and tests.
    29  * Supports execution in native browser as well as Java's internal 
    30  * execution engine.
    31  */
    32 final class Bck2BrwsrLauncher extends BaseHTTPLauncher {
    33     
    34     public Bck2BrwsrLauncher(String cmd) {
    35         super(cmd);
    36     }
    37     
    38     @Override
    39     String harnessResource() {
    40         return "org/apidesign/bck2brwsr/launcher/harness.xhtml";
    41     }
    42 
    43     @Override
    44     String compileJar(JarFile jar) throws IOException {
    45         return CompileCP.compileJAR(jar);
    46     }
    47 
    48     @Override String compileFromClassPath(URL f) {
    49         try {
    50             return CompileCP.compileFromClassPath(f);
    51         } catch (Exception ex) {
    52             ex.printStackTrace();
    53             return null;
    54         }
    55     }
    56     
    57     @Override
    58     void generateBck2BrwsrJS(StringBuilder sb, final Res loader) throws IOException {
    59         class R implements Bck2Brwsr.Resources {
    60             @Override
    61             public InputStream get(String resource) throws IOException {
    62                 return loader.get(resource).openStream();
    63             }
    64         }
    65 
    66         Bck2Brwsr.generate(sb, new R());
    67         sb.append(
    68               "(function WrapperVM(global) {"
    69             + "  function ldCls(res) {\n"
    70             + "    var request = new XMLHttpRequest();\n"
    71             + "    request.open('GET', '/classes/' + res, false);\n"
    72             + "    request.send();\n"
    73             + "    if (request.status !== 200) return null;\n"
    74             + "    var arr = eval(request.responseText);\n"
    75             + "    return arr;\n"
    76             + "  }\n"
    77             + "  var prevvm = global.bck2brwsr;\n"
    78             + "  global.bck2brwsr = function() {\n"
    79             + "    var args = Array.prototype.slice.apply(arguments);\n"
    80             + "    args.unshift(ldCls);\n"
    81             + "    return prevvm.apply(null, args);\n"
    82             + "  };\n"
    83             + "  global.bck2brwsr.registerExtension = prevvm.registerExtension;\n"
    84             + "})(this);\n"
    85         );
    86     }
    87 
    88 }