vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java
branchlazyvm
changeset 298 885acca2fa0b
child 405 e41809be6106
child 424 aef4fd91e99c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java	Tue Dec 11 09:36:44 2012 +0100
     1.3 @@ -0,0 +1,106 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package org.apidesign.vm4brwsr;
    1.22 +
    1.23 +import java.io.IOException;
    1.24 +import java.io.InputStream;
    1.25 +import java.net.URL;
    1.26 +import java.util.Enumeration;
    1.27 +
    1.28 +/** Build your own virtual machine! Use methods in this class to generate
    1.29 + * a skeleton JVM in JavaScript that contains pre-compiled classes of your
    1.30 + * choice. The generated script defines one JavaScript method that can
    1.31 + * be used to bootstrap and load the virtual machine: <pre>
    1.32 + * var vm = bck2brwsr();
    1.33 + * var main = vm.loadClass('org.your.pkg.Main');
    1.34 + * main.main__V_3Ljava_lang_String_2(null);
    1.35 + * </pre>
    1.36 + * In case one wants to initialize the virtual machine with ability to
    1.37 + * load classes lazily when needed, one can provide a loader function to
    1.38 + * when creating the virtual machine: <pre>
    1.39 + * var vm = bck2brwsr(function(resource) { 
    1.40 + *   return null; // byte[] for the resource
    1.41 + * });
    1.42 + * </pre>
    1.43 + * In this scenario, when a request for a unknown class is made, the loader
    1.44 + * function is asked for its byte code and the system dynamically transforms
    1.45 + * it to JavaScript.
    1.46 + *
    1.47 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.48 + */
    1.49 +public final class Bck2Brwsr {
    1.50 +    private Bck2Brwsr() {
    1.51 +    }
    1.52 +    
    1.53 +    /** Generates virtual machine from bytes served by a <code>resources</code>
    1.54 +     * provider.
    1.55 +     * 
    1.56 +     * @param out the output to write the generated JavaScript to
    1.57 +     * @param resources provider of class files to use
    1.58 +     * @param classes additional classes to include in the generated script
    1.59 +     * @throws IOException I/O exception can be thrown when something goes wrong
    1.60 +     */
    1.61 +    public static void generate(Appendable out, Resources resources, String... classes) throws IOException {
    1.62 +        StringArray arr = StringArray.asList(classes);
    1.63 +        arr.add(VM.class.getName().replace('.', '/'));
    1.64 +        VM.compile(resources, out, arr);
    1.65 +    }
    1.66 +    
    1.67 +    /** Generates virtual machine from bytes served by a class loader.
    1.68 +     * 
    1.69 +     * @param out the output to write the generated JavaScript to
    1.70 +     * @param loader class loader to load needed classes from
    1.71 +     * @param classes additional classes to include in the generated script
    1.72 +     * @throws IOException I/O exception can be thrown when something goes wrong
    1.73 +     */
    1.74 +    public static void generate(Appendable out, final ClassLoader loader, String... classes) throws IOException {
    1.75 +        class R implements Resources {
    1.76 +            @Override
    1.77 +            public InputStream get(String name) throws IOException {
    1.78 +                Enumeration<URL> en = loader.getResources(name);
    1.79 +                URL u = null;
    1.80 +                while (en.hasMoreElements()) {
    1.81 +                    u = en.nextElement();
    1.82 +                }
    1.83 +                if (u == null) {
    1.84 +                    throw new IOException("Can't find " + name);
    1.85 +                }
    1.86 +                return u.openStream();
    1.87 +            }
    1.88 +        }
    1.89 +        generate(out, new R(), classes);
    1.90 +    }
    1.91 +    
    1.92 +    /** Provider of resources (classes and other files). The 
    1.93 +     * {@link #generate(java.lang.Appendable, org.apidesign.vm4brwsr.Bck2Brwsr.Resources, java.lang.String[]) 
    1.94 +     * generator method} will call back here for all classes needed during
    1.95 +     * translation to JavaScript.
    1.96 +     */
    1.97 +    public interface Resources {
    1.98 +        /** Loads given resource (class or other file like image). The 
    1.99 +         * resource name to load bytes for the {@link String} class
   1.100 +         * would be <code>"java/lang/String.class"</code>.
   1.101 +         * 
   1.102 +         * @param resource path to resource to load
   1.103 +         * @return the input stream for the resource 
   1.104 +         * @throws IOException can be thrown if the loading fails on some error
   1.105 +         *   or the file cannot be found
   1.106 +         */
   1.107 +        public InputStream get(String resource) throws IOException;
   1.108 +    }
   1.109 +}