jaroslav@1033: /** jaroslav@1033: * Back 2 Browser Bytecode Translator jaroslav@1033: * Copyright (C) 2012 Jaroslav Tulach jaroslav@1033: * jaroslav@1033: * This program is free software: you can redistribute it and/or modify jaroslav@1033: * it under the terms of the GNU General Public License as published by jaroslav@1033: * the Free Software Foundation, version 2 of the License. jaroslav@1033: * jaroslav@1033: * This program is distributed in the hope that it will be useful, jaroslav@1033: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@1033: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@1033: * GNU General Public License for more details. jaroslav@1033: * jaroslav@1033: * You should have received a copy of the GNU General Public License jaroslav@1033: * along with this program. Look for COPYING file in the top folder. jaroslav@1033: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@1033: */ jaroslav@1033: package org.apidesign.bck2brwsr.launcher; jaroslav@1033: jaroslav@1033: import java.io.IOException; jaroslav@1033: import java.io.InputStream; jaroslav@1033: import org.apidesign.vm4brwsr.Bck2Brwsr; jaroslav@1033: jaroslav@1033: /** jaroslav@1033: * Lightweight server to launch Bck2Brwsr applications and tests. jaroslav@1033: * Supports execution in native browser as well as Java's internal jaroslav@1033: * execution engine. jaroslav@1033: */ jaroslav@1088: final class Bck2BrwsrLauncher extends BaseHTTPLauncher { jaroslav@1033: jaroslav@1033: public Bck2BrwsrLauncher(String cmd) { jaroslav@1088: super(cmd); jaroslav@1033: } jaroslav@1033: jaroslav@1033: @Override jaroslav@1088: String harnessResource() { jaroslav@1088: return "org/apidesign/bck2brwsr/launcher/harness.xhtml"; jaroslav@1033: } jaroslav@1033: jaroslav@1033: @Override jaroslav@1088: void generateBck2BrwsrJS(StringBuilder sb, final Res loader) throws IOException { jaroslav@1088: class R implements Bck2Brwsr.Resources { jaroslav@1088: @Override jaroslav@1088: public InputStream get(String resource) throws IOException { jaroslav@1088: return loader.get(resource); jaroslav@1033: } jaroslav@1033: } jaroslav@1033: jaroslav@1088: Bck2Brwsr.generate(sb, new R()); jaroslav@1088: sb.append( jaroslav@1088: "(function WrapperVM(global) {" jaroslav@1088: + " function ldCls(res) {\n" jaroslav@1088: + " var request = new XMLHttpRequest();\n" jaroslav@1295: + " console.log('Loading ' + res);\n" jaroslav@1088: + " request.open('GET', '/classes/' + res, false);\n" jaroslav@1088: + " request.send();\n" jaroslav@1295: + " if (request.status !== 200) {\n" jaroslav@1295: + " console.warn(' cannot load ' + res);\n" jaroslav@1295: + " return null;\n" jaroslav@1295: + " }\n" jaroslav@1088: + " var arr = eval('(' + request.responseText + ')');\n" jaroslav@1088: + " return arr;\n" jaroslav@1088: + " }\n" jaroslav@1088: + " var prevvm = global.bck2brwsr;\n" jaroslav@1088: + " global.bck2brwsr = function() {\n" jaroslav@1088: + " var args = Array.prototype.slice.apply(arguments);\n" jaroslav@1088: + " args.unshift(ldCls);\n" jaroslav@1088: + " return prevvm.apply(null, args);\n" jaroslav@1088: + " };\n" jaroslav@1088: + "})(this);\n" jaroslav@1088: ); jaroslav@1033: } jaroslav@1033: jaroslav@1033: }