rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java
author Lubomir Nerad <lubomir.nerad@oracle.com>
Fri, 26 Apr 2013 18:48:34 +0200
branchclosure
changeset 1029 b1fe994d4267
parent 1020 a6bacea2518f
child 1086 2ac4283ee209
permissions -rw-r--r--
Partially working extension modules
jaroslav@298
     1
/**
jaroslav@298
     2
 * Back 2 Browser Bytecode Translator
jaroslav@298
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@298
     4
 *
jaroslav@298
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@298
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@298
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@298
     8
 *
jaroslav@298
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@298
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@298
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@298
    12
 * GNU General Public License for more details.
jaroslav@298
    13
 *
jaroslav@298
    14
 * You should have received a copy of the GNU General Public License
jaroslav@298
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@298
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@298
    17
 */
jaroslav@298
    18
package org.apidesign.vm4brwsr;
jaroslav@298
    19
jaroslav@298
    20
import java.io.IOException;
jaroslav@298
    21
import java.io.InputStream;
jaroslav@298
    22
import java.net.URL;
jaroslav@298
    23
import java.util.Enumeration;
jaroslav@298
    24
jaroslav@298
    25
/** Build your own virtual machine! Use methods in this class to generate
jaroslav@298
    26
 * a skeleton JVM in JavaScript that contains pre-compiled classes of your
jaroslav@298
    27
 * choice. The generated script defines one JavaScript method that can
jaroslav@298
    28
 * be used to bootstrap and load the virtual machine: <pre>
jaroslav@298
    29
 * var vm = bck2brwsr();
jaroslav@298
    30
 * var main = vm.loadClass('org.your.pkg.Main');
jaroslav@298
    31
 * main.main__V_3Ljava_lang_String_2(null);
jaroslav@298
    32
 * </pre>
jaroslav@298
    33
 * In case one wants to initialize the virtual machine with ability to
jaroslav@298
    34
 * load classes lazily when needed, one can provide a loader function to
jaroslav@298
    35
 * when creating the virtual machine: <pre>
jaroslav@298
    36
 * var vm = bck2brwsr(function(resource) { 
jaroslav@298
    37
 *   return null; // byte[] for the resource
jaroslav@298
    38
 * });
jaroslav@298
    39
 * </pre>
jaroslav@424
    40
 * In this scenario, when a request for an unknown class is made, the loader
jaroslav@298
    41
 * function is asked for its byte code and the system dynamically transforms
jaroslav@298
    42
 * it to JavaScript.
jaroslav@729
    43
 * <p>
jaroslav@729
    44
 * Instead of a loader function, one can also provide a URL to a JAR file.
jaroslav@729
    45
 * The <code>bck2brwsr</code> system will do its best to download the file
jaroslav@729
    46
 * and provide loader function for it automatically.
jaroslav@729
    47
 * <p>
jaroslav@729
    48
 * One can provide as many loader functions and JAR URL references as necessary.
jaroslav@729
    49
 * Then the initialization code would look like:<pre>
jaroslav@729
    50
 * var vm = bck2brwsr(url1, url2, fnctn1, url3, functn2);
jaroslav@729
    51
 * </pre>
jaroslav@729
    52
 * The provided URLs and loader functions will be consulted one by one.
jaroslav@298
    53
 *
jaroslav@298
    54
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@298
    55
 */
jaroslav@298
    56
public final class Bck2Brwsr {
jaroslav@874
    57
    private final ObfuscationLevel level;
jaroslav@874
    58
    private final StringArray classes;
jaroslav@874
    59
    private final Resources res;
lubomir@1029
    60
    private final boolean extension;
jaroslav@874
    61
lubomir@1029
    62
    private Bck2Brwsr(ObfuscationLevel level, StringArray classes, Resources resources, boolean extension) {
jaroslav@874
    63
        this.level = level;
jaroslav@874
    64
        this.classes = classes;
jaroslav@874
    65
        this.res = resources;
lubomir@1029
    66
        this.extension = extension;
jaroslav@298
    67
    }
jaroslav@874
    68
    
jaroslav@874
    69
    /** Helper method to generate virtual machine from bytes served by a <code>resources</code>
jaroslav@298
    70
     * provider.
lubomir@849
    71
     *
jaroslav@298
    72
     * @param out the output to write the generated JavaScript to
jaroslav@298
    73
     * @param resources provider of class files to use
jaroslav@298
    74
     * @param classes additional classes to include in the generated script
jaroslav@298
    75
     * @throws IOException I/O exception can be thrown when something goes wrong
jaroslav@298
    76
     */
jaroslav@298
    77
    public static void generate(Appendable out, Resources resources, String... classes) throws IOException {
jaroslav@874
    78
        newCompiler().resources(resources).addRootClasses(classes).generate(out);
lubomir@849
    79
    }
lubomir@849
    80
jaroslav@874
    81
    /** Helper method to generate virtual machine from bytes served by a class loader.
lubomir@849
    82
     *
lubomir@849
    83
     * @param out the output to write the generated JavaScript to
lubomir@849
    84
     * @param loader class loader to load needed classes from
lubomir@849
    85
     * @param classes additional classes to include in the generated script
lubomir@849
    86
     * @throws IOException I/O exception can be thrown when something goes wrong
lubomir@849
    87
     */
lubomir@849
    88
    public static void generate(Appendable out, ClassLoader loader, String... classes) throws IOException {
jaroslav@874
    89
        newCompiler().resources(loader).addRootClasses(classes).generate(out);
jaroslav@874
    90
    }
jaroslav@874
    91
    
jaroslav@874
    92
    /** Creates new instance of Bck2Brwsr compiler which is ready to generate
jaroslav@874
    93
     * empty Bck2Brwsr virtual machine. The instance can be further
jaroslav@874
    94
     * configured by calling chain of methods. For example: 
jaroslav@874
    95
     * <pre>
jaroslav@874
    96
     * {@link #createCompiler()}.{@link #resources(org.apidesign.vm4brwsr.Bck2Brwsr.Resources) resources(loader)}.{@link #addRootClasses(java.lang.String[]) addRootClasses("your/Clazz")}.{@link #generate(java.lang.Appendable) generate(out)};
jaroslav@874
    97
     * </pre>
jaroslav@874
    98
     * 
jaroslav@874
    99
     * @return new instance of the Bck2Brwsr compiler
jaroslav@874
   100
     * @since 0.5
jaroslav@874
   101
     */
jaroslav@874
   102
    public static Bck2Brwsr newCompiler() {
lubomir@1029
   103
        StringArray arr = StringArray.asList(
lubomir@1029
   104
                              Class.class.getName().replace('.', '/'),
lubomir@1029
   105
                              VM.class.getName().replace('.', '/'));
lubomir@1029
   106
        return new Bck2Brwsr(ObfuscationLevel.NONE, arr, null, false);
lubomir@849
   107
    }
lubomir@849
   108
jaroslav@874
   109
    /** Creates new instance of the Bck2Brwsr compiler which inherits
jaroslav@874
   110
     * all values from <code>this</code> instance and adds additional classes 
jaroslav@874
   111
     * to the list of those that should be compiled by the {@link #generate(java.lang.Appendable)} 
jaroslav@874
   112
     * method.
jaroslav@874
   113
     * 
jaroslav@874
   114
     * @param classes the classes to add to the compilation
jaroslav@874
   115
     * @return new instance of the compiler
jaroslav@874
   116
     */
jaroslav@874
   117
    public Bck2Brwsr addRootClasses(String... classes) {
jaroslav@874
   118
        if (classes.length == 0) {
jaroslav@874
   119
            return this;
jaroslav@874
   120
        } else {
lubomir@1029
   121
            return new Bck2Brwsr(level, this.classes.addAndNew(classes), res,
lubomir@1029
   122
                                 extension);
jaroslav@874
   123
        }
jaroslav@874
   124
    }
jaroslav@874
   125
    
jaroslav@874
   126
    /** Changes the obfuscation level for the compiler by creating new instance
jaroslav@874
   127
     * which inherits all values from <code>this</code> and adjust the level
jaroslav@874
   128
     * of obfuscation.
jaroslav@874
   129
     * 
jaroslav@874
   130
     * @param level the new level of obfuscation
jaroslav@874
   131
     * @return new instance of the compiler with changed level of obfuscation
jaroslav@874
   132
     * @since 0.5
jaroslav@874
   133
     */
jaroslav@874
   134
    public Bck2Brwsr obfuscation(ObfuscationLevel level) {
lubomir@1029
   135
        return new Bck2Brwsr(level, classes, res, extension);
jaroslav@874
   136
    }
jaroslav@874
   137
    
jaroslav@874
   138
    /** A way to change the provider of additional resources (classes) for the 
jaroslav@874
   139
     * compiler. 
jaroslav@874
   140
     * 
jaroslav@874
   141
     * @param res the implementation of resources provider
jaroslav@874
   142
     * @return new instance of the compiler with all values remaining the same, just 
jaroslav@874
   143
     *   with different resources provider
jaroslav@874
   144
     * @since 0.5
jaroslav@874
   145
     */
jaroslav@874
   146
    public Bck2Brwsr resources(Resources res) {
lubomir@1029
   147
        return new Bck2Brwsr(level, classes, res, extension);
lubomir@1029
   148
    }
lubomir@1029
   149
lubomir@1029
   150
    public Bck2Brwsr extension(boolean extension) {
lubomir@1029
   151
        return new Bck2Brwsr(level, classes, res, extension);
jaroslav@874
   152
    }
jaroslav@874
   153
jaroslav@874
   154
    /** A way to change the provider of additional resources (classes) for the 
jaroslav@874
   155
     * compiler by specifying classloader to use for loading them.
jaroslav@874
   156
     * 
jaroslav@874
   157
     * @param loader class loader to load the resources from
jaroslav@874
   158
     * @return new instance of the compiler with all values being the same, just 
jaroslav@874
   159
     *   different resources provider
jaroslav@874
   160
     * @since 0.5
jaroslav@874
   161
     */
jaroslav@874
   162
    public Bck2Brwsr resources(final ClassLoader loader) {
jaroslav@874
   163
        return resources(new LdrRsrcs(loader));
jaroslav@874
   164
    }
jaroslav@874
   165
    
jaroslav@874
   166
    /** Generates virtual machine based on previous configuration of the 
jaroslav@874
   167
     * compiler.
lubomir@849
   168
     * 
lubomir@849
   169
     * @param out the output to write the generated JavaScript to
lubomir@860
   170
     * @since 0.5
lubomir@849
   171
     */
jaroslav@874
   172
    public void generate(Appendable out) throws IOException {
jaroslav@874
   173
        Resources r = res != null ? res : new LdrRsrcs(Bck2Brwsr.class.getClassLoader());
lubomir@1029
   174
//        if (level != ObfuscationLevel.NONE) {
lubomir@1029
   175
//            try {
lubomir@1029
   176
//                ClosureWrapper.produceTo(out, level, r, classes);
lubomir@1029
   177
//                return;
lubomir@1029
   178
//            } catch (IOException ex) {
lubomir@1029
   179
//                throw ex;
lubomir@1029
   180
//            } catch (Throwable ex) {
lubomir@1029
   181
//                out.append("/* Failed to obfuscate: " + ex.getMessage()
lubomir@1029
   182
//                               + " */\n");
lubomir@1029
   183
//            }
lubomir@1029
   184
//        }
lubomir@1029
   185
lubomir@1029
   186
        if (extension) {
lubomir@1029
   187
            VM.compileExtension(r, out, classes);
lubomir@1029
   188
        } else {
lubomir@1029
   189
            VM.compileStandalone(r, out, classes);
jaroslav@750
   190
        }
jaroslav@298
   191
    }
jaroslav@298
   192
    
jaroslav@298
   193
    /** Provider of resources (classes and other files). The 
jaroslav@298
   194
     * {@link #generate(java.lang.Appendable, org.apidesign.vm4brwsr.Bck2Brwsr.Resources, java.lang.String[]) 
jaroslav@298
   195
     * generator method} will call back here for all classes needed during
jaroslav@298
   196
     * translation to JavaScript.
jaroslav@298
   197
     */
jaroslav@298
   198
    public interface Resources {
jaroslav@298
   199
        /** Loads given resource (class or other file like image). The 
jaroslav@298
   200
         * resource name to load bytes for the {@link String} class
jaroslav@298
   201
         * would be <code>"java/lang/String.class"</code>.
jaroslav@298
   202
         * 
jaroslav@298
   203
         * @param resource path to resource to load
jaroslav@298
   204
         * @return the input stream for the resource 
jaroslav@298
   205
         * @throws IOException can be thrown if the loading fails on some error
jaroslav@298
   206
         *   or the file cannot be found
jaroslav@298
   207
         */
jaroslav@298
   208
        public InputStream get(String resource) throws IOException;
jaroslav@298
   209
    }
jaroslav@298
   210
}