Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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.
18 package org.apidesign.vm4brwsr;
20 import java.io.IOException;
21 import java.io.InputStream;
23 import java.util.Enumeration;
25 /** Build your own virtual machine! Use methods in this class to generate
26 * a skeleton JVM in JavaScript that contains pre-compiled classes of your
27 * choice. The generated script defines one JavaScript method that can
28 * be used to bootstrap and load the virtual machine: <pre>
29 * var vm = bck2brwsr();
30 * var main = vm.loadClass('org.your.pkg.Main');
31 * main.main__V_3Ljava_lang_String_2(null);
33 * In case one wants to initialize the virtual machine with ability to
34 * load classes lazily when needed, one can provide a loader function to
35 * when creating the virtual machine: <pre>
36 * var vm = bck2brwsr(function(resource) {
37 * return null; // byte[] for the resource
40 * In this scenario, when a request for an unknown class is made, the loader
41 * function is asked for its byte code and the system dynamically transforms
44 * Instead of a loader function, one can also provide a URL to a JAR file.
45 * The <code>bck2brwsr</code> system will do its best to download the file
46 * and provide loader function for it automatically.
48 * One can provide as many loader functions and JAR URL references as necessary.
49 * Then the initialization code would look like:<pre>
50 * var vm = bck2brwsr(url1, url2, fnctn1, url3, functn2);
52 * The provided URLs and loader functions will be consulted one by one.
54 * @author Jaroslav Tulach <jtulach@netbeans.org>
56 public final class Bck2Brwsr {
60 /** Generates virtual machine from bytes served by a <code>resources</code>
63 * @param out the output to write the generated JavaScript to
64 * @param resources provider of class files to use
65 * @param classes additional classes to include in the generated script
66 * @throws IOException I/O exception can be thrown when something goes wrong
68 public static void generate(Appendable out, Resources resources, String... classes) throws IOException {
69 StringArray arr = StringArray.asList(classes);
70 arr.add(VM.class.getName().replace('.', '/'));
71 VM.compile(resources, out, arr);
74 /** Generates virtual machine from bytes served by a class loader.
76 * @param out the output to write the generated JavaScript to
77 * @param loader class loader to load needed classes from
78 * @param classes additional classes to include in the generated script
79 * @throws IOException I/O exception can be thrown when something goes wrong
81 public static void generate(Appendable out, final ClassLoader loader, String... classes) throws IOException {
82 class R implements Resources {
84 public InputStream get(String name) throws IOException {
85 Enumeration<URL> en = loader.getResources(name);
87 while (en.hasMoreElements()) {
91 throw new IOException("Can't find " + name);
93 return u.openStream();
96 generate(out, new R(), classes);
99 /** Provider of resources (classes and other files). The
100 * {@link #generate(java.lang.Appendable, org.apidesign.vm4brwsr.Bck2Brwsr.Resources, java.lang.String[])
101 * generator method} will call back here for all classes needed during
102 * translation to JavaScript.
104 public interface Resources {
105 /** Loads given resource (class or other file like image). The
106 * resource name to load bytes for the {@link String} class
107 * would be <code>"java/lang/String.class"</code>.
109 * @param resource path to resource to load
110 * @return the input stream for the resource
111 * @throws IOException can be thrown if the loading fails on some error
112 * or the file cannot be found
114 public InputStream get(String resource) throws IOException;