launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 27 Apr 2014 08:33:38 +0200
branchclosure
changeset 1492 e38025e9536a
parent 1489 8d0fc428ff72
child 1504 d058edd87424
permissions -rw-r--r--
Need to include all emul-mini classes in the generated bck2brwsr.js
jaroslav@1033
     1
/**
jaroslav@1033
     2
 * Back 2 Browser Bytecode Translator
jaroslav@1033
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@1033
     4
 *
jaroslav@1033
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@1033
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@1033
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@1033
     8
 *
jaroslav@1033
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@1033
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@1033
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@1033
    12
 * GNU General Public License for more details.
jaroslav@1033
    13
 *
jaroslav@1033
    14
 * You should have received a copy of the GNU General Public License
jaroslav@1033
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@1033
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@1033
    17
 */
jaroslav@1033
    18
package org.apidesign.bck2brwsr.launcher;
jaroslav@1033
    19
jaroslav@1489
    20
import java.io.File;
jaroslav@1033
    21
import java.io.IOException;
jaroslav@1033
    22
import java.io.InputStream;
jaroslav@1489
    23
import java.net.URL;
jaroslav@1489
    24
import java.util.jar.JarFile;
jaroslav@1033
    25
import org.apidesign.vm4brwsr.Bck2Brwsr;
jaroslav@1033
    26
jaroslav@1033
    27
/**
jaroslav@1033
    28
 * Lightweight server to launch Bck2Brwsr applications and tests.
jaroslav@1033
    29
 * Supports execution in native browser as well as Java's internal 
jaroslav@1033
    30
 * execution engine.
jaroslav@1033
    31
 */
jaroslav@1088
    32
final class Bck2BrwsrLauncher extends BaseHTTPLauncher {
jaroslav@1033
    33
    
jaroslav@1033
    34
    public Bck2BrwsrLauncher(String cmd) {
jaroslav@1088
    35
        super(cmd);
jaroslav@1033
    36
    }
jaroslav@1033
    37
    
jaroslav@1033
    38
    @Override
jaroslav@1088
    39
    String harnessResource() {
jaroslav@1088
    40
        return "org/apidesign/bck2brwsr/launcher/harness.xhtml";
jaroslav@1033
    41
    }
jaroslav@1489
    42
jaroslav@1489
    43
    @Override
jaroslav@1489
    44
    String compileJar(JarFile jar) throws IOException {
jaroslav@1489
    45
        return CompileCP.compileJAR(jar);
jaroslav@1489
    46
    }
jaroslav@1489
    47
jaroslav@1489
    48
    @Override String compileFromClassPath(URL f) {
jaroslav@1489
    49
        try {
jaroslav@1489
    50
            return CompileCP.compileFromClassPath(f);
jaroslav@1489
    51
        } catch (Exception ex) {
jaroslav@1489
    52
            ex.printStackTrace();
jaroslav@1489
    53
            return null;
jaroslav@1489
    54
        }
jaroslav@1489
    55
    }
jaroslav@1033
    56
    
jaroslav@1033
    57
    @Override
jaroslav@1088
    58
    void generateBck2BrwsrJS(StringBuilder sb, final Res loader) throws IOException {
jaroslav@1492
    59
        CompileCP.compileVM(sb, loader);
jaroslav@1088
    60
        sb.append(
jaroslav@1088
    61
              "(function WrapperVM(global) {"
jaroslav@1088
    62
            + "  function ldCls(res) {\n"
jaroslav@1088
    63
            + "    var request = new XMLHttpRequest();\n"
jaroslav@1088
    64
            + "    request.open('GET', '/classes/' + res, false);\n"
jaroslav@1088
    65
            + "    request.send();\n"
jaroslav@1088
    66
            + "    if (request.status !== 200) return null;\n"
jaroslav@1489
    67
            + "    var arr = eval(request.responseText);\n"
jaroslav@1088
    68
            + "    return arr;\n"
jaroslav@1088
    69
            + "  }\n"
jaroslav@1088
    70
            + "  var prevvm = global.bck2brwsr;\n"
jaroslav@1088
    71
            + "  global.bck2brwsr = function() {\n"
jaroslav@1088
    72
            + "    var args = Array.prototype.slice.apply(arguments);\n"
jaroslav@1088
    73
            + "    args.unshift(ldCls);\n"
jaroslav@1088
    74
            + "    return prevvm.apply(null, args);\n"
jaroslav@1088
    75
            + "  };\n"
jaroslav@1489
    76
            + "  global.bck2brwsr.registerExtension = prevvm.registerExtension;\n"
jaroslav@1088
    77
            + "})(this);\n"
jaroslav@1088
    78
        );
jaroslav@1033
    79
    }
jaroslav@1033
    80
jaroslav@1033
    81
}