jaroslav@298: /** jaroslav@298: * Back 2 Browser Bytecode Translator jaroslav@298: * Copyright (C) 2012 Jaroslav Tulach jaroslav@298: * jaroslav@298: * This program is free software: you can redistribute it and/or modify jaroslav@298: * it under the terms of the GNU General Public License as published by jaroslav@298: * the Free Software Foundation, version 2 of the License. jaroslav@298: * jaroslav@298: * This program is distributed in the hope that it will be useful, jaroslav@298: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@298: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@298: * GNU General Public License for more details. jaroslav@298: * jaroslav@298: * You should have received a copy of the GNU General Public License jaroslav@298: * along with this program. Look for COPYING file in the top folder. jaroslav@298: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@298: */ jaroslav@298: package org.apidesign.vm4brwsr; jaroslav@298: jaroslav@298: import java.io.IOException; jaroslav@298: import java.io.InputStream; jaroslav@298: import java.net.URL; jaroslav@298: import java.util.Enumeration; jaroslav@298: jaroslav@298: /** Build your own virtual machine! Use methods in this class to generate jaroslav@298: * a skeleton JVM in JavaScript that contains pre-compiled classes of your jaroslav@298: * choice. The generated script defines one JavaScript method that can jaroslav@298: * be used to bootstrap and load the virtual machine:
jaroslav@298:  * var vm = bck2brwsr();
jaroslav@298:  * var main = vm.loadClass('org.your.pkg.Main');
jaroslav@298:  * main.main__V_3Ljava_lang_String_2(null);
jaroslav@298:  * 
jaroslav@298: * In case one wants to initialize the virtual machine with ability to jaroslav@298: * load classes lazily when needed, one can provide a loader function to jaroslav@298: * when creating the virtual machine:
jaroslav@298:  * var vm = bck2brwsr(function(resource) { 
jaroslav@298:  *   return null; // byte[] for the resource
jaroslav@298:  * });
jaroslav@298:  * 
jaroslav@298: * In this scenario, when a request for a unknown class is made, the loader jaroslav@298: * function is asked for its byte code and the system dynamically transforms jaroslav@298: * it to JavaScript. jaroslav@298: * jaroslav@298: * @author Jaroslav Tulach jaroslav@298: */ jaroslav@298: public final class Bck2Brwsr { jaroslav@298: private Bck2Brwsr() { jaroslav@298: } jaroslav@298: jaroslav@298: /** Generates virtual machine from bytes served by a resources jaroslav@298: * provider. jaroslav@298: * jaroslav@298: * @param out the output to write the generated JavaScript to jaroslav@298: * @param resources provider of class files to use jaroslav@298: * @param classes additional classes to include in the generated script jaroslav@298: * @throws IOException I/O exception can be thrown when something goes wrong jaroslav@298: */ jaroslav@298: public static void generate(Appendable out, Resources resources, String... classes) throws IOException { jaroslav@298: StringArray arr = StringArray.asList(classes); jaroslav@298: arr.add(VM.class.getName().replace('.', '/')); jaroslav@298: VM.compile(resources, out, arr); jaroslav@298: } jaroslav@298: jaroslav@298: /** Generates virtual machine from bytes served by a class loader. jaroslav@298: * jaroslav@298: * @param out the output to write the generated JavaScript to jaroslav@298: * @param loader class loader to load needed classes from jaroslav@298: * @param classes additional classes to include in the generated script jaroslav@298: * @throws IOException I/O exception can be thrown when something goes wrong jaroslav@298: */ jaroslav@298: public static void generate(Appendable out, final ClassLoader loader, String... classes) throws IOException { jaroslav@298: class R implements Resources { jaroslav@298: @Override jaroslav@298: public InputStream get(String name) throws IOException { jaroslav@298: Enumeration en = loader.getResources(name); jaroslav@298: URL u = null; jaroslav@298: while (en.hasMoreElements()) { jaroslav@298: u = en.nextElement(); jaroslav@298: } jaroslav@298: if (u == null) { jaroslav@298: throw new IOException("Can't find " + name); jaroslav@298: } jaroslav@298: return u.openStream(); jaroslav@298: } jaroslav@298: } jaroslav@298: generate(out, new R(), classes); jaroslav@298: } jaroslav@298: jaroslav@298: /** Provider of resources (classes and other files). The jaroslav@298: * {@link #generate(java.lang.Appendable, org.apidesign.vm4brwsr.Bck2Brwsr.Resources, java.lang.String[]) jaroslav@298: * generator method} will call back here for all classes needed during jaroslav@298: * translation to JavaScript. jaroslav@298: */ jaroslav@298: public interface Resources { jaroslav@298: /** Loads given resource (class or other file like image). The jaroslav@298: * resource name to load bytes for the {@link String} class jaroslav@298: * would be "java/lang/String.class". jaroslav@298: * jaroslav@298: * @param resource path to resource to load jaroslav@298: * @return the input stream for the resource jaroslav@298: * @throws IOException can be thrown if the loading fails on some error jaroslav@298: * or the file cannot be found jaroslav@298: */ jaroslav@298: public InputStream get(String resource) throws IOException; jaroslav@298: } jaroslav@298: }