launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 29 Apr 2014 14:28:52 +0200
branchclosure
changeset 1504 d058edd87424
parent 1492 e38025e9536a
child 1505 706b66d8c481
permissions -rw-r--r--
Use provided loader to load libraries
     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.IOException;
    21 import java.net.URL;
    22 import java.util.jar.JarFile;
    23 
    24 /**
    25  * Lightweight server to launch Bck2Brwsr applications and tests.
    26  * Supports execution in native browser as well as Java's internal 
    27  * execution engine.
    28  */
    29 final class Bck2BrwsrLauncher extends BaseHTTPLauncher {
    30     
    31     public Bck2BrwsrLauncher(String cmd) {
    32         super(cmd);
    33     }
    34     
    35     @Override
    36     String harnessResource() {
    37         return "org/apidesign/bck2brwsr/launcher/harness.xhtml";
    38     }
    39 
    40     @Override
    41     String compileJar(JarFile jar) throws IOException {
    42         return CompileCP.compileJAR(jar);
    43     }
    44 
    45     @Override String compileFromClassPath(URL f, Res loader) {
    46         try {
    47             return CompileCP.compileFromClassPath(f, loader);
    48         } catch (Exception ex) {
    49             ex.printStackTrace();
    50             return null;
    51         }
    52     }
    53     
    54     @Override
    55     void generateBck2BrwsrJS(StringBuilder sb, final Res loader) throws IOException {
    56         CompileCP.compileVM(sb, loader);
    57         sb.append(
    58               "(function WrapperVM(global) {"
    59             + "  function ldCls(res) {\n"
    60             + "    var request = new XMLHttpRequest();\n"
    61             + "    request.open('GET', '/classes/' + res, false);\n"
    62             + "    request.send();\n"
    63             + "    if (request.status !== 200) return null;\n"
    64             + "    var arr = eval(request.responseText);\n"
    65             + "    return arr;\n"
    66             + "  }\n"
    67             + "  var prevvm = global.bck2brwsr;\n"
    68             + "  global.bck2brwsr = function() {\n"
    69             + "    var args = Array.prototype.slice.apply(arguments);\n"
    70             + "    args.unshift(ldCls);\n"
    71             + "    return prevvm.apply(null, args);\n"
    72             + "  };\n"
    73             + "  global.bck2brwsr.registerExtension = prevvm.registerExtension;\n"
    74             + "})(this);\n"
    75         );
    76     }
    77 
    78 }