rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 26 Apr 2014 21:30:06 +0200
branchclosure
changeset 1491 4a1398eff4fb
parent 1094 36961c9a009f
child 1493 234fea368401
permissions -rw-r--r--
Different meaning of root vs. added classes. Ability to explicitly enumerate classes that should be exported and available with fully qualified name.
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@1491
    23
import org.apidesign.bck2brwsr.core.Exported;
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@1491
    58
    private final StringArray rootcls;
jaroslav@874
    59
    private final StringArray classes;
jaroslav@874
    60
    private final Resources res;
lubomir@1029
    61
    private final boolean extension;
jaroslav@874
    62
jaroslav@1491
    63
    private Bck2Brwsr(ObfuscationLevel level, StringArray rootcls, StringArray classes, Resources resources, boolean extension) {
jaroslav@874
    64
        this.level = level;
jaroslav@1491
    65
        this.rootcls = rootcls;
jaroslav@874
    66
        this.classes = classes;
jaroslav@874
    67
        this.res = resources;
lubomir@1029
    68
        this.extension = extension;
jaroslav@298
    69
    }
jaroslav@874
    70
    
jaroslav@874
    71
    /** Helper method to generate virtual machine from bytes served by a <code>resources</code>
jaroslav@298
    72
     * provider.
lubomir@849
    73
     *
jaroslav@298
    74
     * @param out the output to write the generated JavaScript to
jaroslav@298
    75
     * @param resources provider of class files to use
jaroslav@298
    76
     * @param classes additional classes to include in the generated script
jaroslav@298
    77
     * @throws IOException I/O exception can be thrown when something goes wrong
jaroslav@298
    78
     */
jaroslav@298
    79
    public static void generate(Appendable out, Resources resources, String... classes) throws IOException {
jaroslav@874
    80
        newCompiler().resources(resources).addRootClasses(classes).generate(out);
lubomir@849
    81
    }
lubomir@849
    82
jaroslav@874
    83
    /** Helper method to generate virtual machine from bytes served by a class loader.
lubomir@849
    84
     *
lubomir@849
    85
     * @param out the output to write the generated JavaScript to
lubomir@849
    86
     * @param loader class loader to load needed classes from
lubomir@849
    87
     * @param classes additional classes to include in the generated script
lubomir@849
    88
     * @throws IOException I/O exception can be thrown when something goes wrong
lubomir@849
    89
     */
lubomir@849
    90
    public static void generate(Appendable out, ClassLoader loader, String... classes) throws IOException {
jaroslav@874
    91
        newCompiler().resources(loader).addRootClasses(classes).generate(out);
jaroslav@874
    92
    }
jaroslav@874
    93
    
jaroslav@874
    94
    /** Creates new instance of Bck2Brwsr compiler which is ready to generate
jaroslav@874
    95
     * empty Bck2Brwsr virtual machine. The instance can be further
jaroslav@874
    96
     * configured by calling chain of methods. For example: 
jaroslav@874
    97
     * <pre>
jaroslav@874
    98
     * {@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
    99
     * </pre>
jaroslav@874
   100
     * 
jaroslav@874
   101
     * @return new instance of the Bck2Brwsr compiler
jaroslav@874
   102
     * @since 0.5
jaroslav@874
   103
     */
jaroslav@874
   104
    public static Bck2Brwsr newCompiler() {
jaroslav@1491
   105
        return new Bck2Brwsr(ObfuscationLevel.NONE, new StringArray(), new StringArray(), null, false);
lubomir@849
   106
    }
lubomir@849
   107
jaroslav@1491
   108
    /** Adds additional classes 
jaroslav@1491
   109
     * to the list of those that should be included in the generated
jaroslav@1491
   110
     * JavaScript file.
jaroslav@1491
   111
     * These classes are guaranteed to be available in the
jaroslav@1491
   112
     * generated virtual machine code accessible using their fully 
jaroslav@1491
   113
     * qualified name. This brings the same behavior as if the
jaroslav@1491
   114
     * classes were added by {@link #addClasses(java.lang.String...) } and
jaroslav@1491
   115
     * were annotated with {@link Exported} annotation.
jaroslav@874
   116
     * 
jaroslav@874
   117
     * @param classes the classes to add to the compilation
jaroslav@1491
   118
     * @return new instance of the Bck2Brwsr compiler which inherits
jaroslav@1491
   119
     * all values from <code>this</code>
jaroslav@874
   120
     */
jaroslav@874
   121
    public Bck2Brwsr addRootClasses(String... classes) {
jaroslav@874
   122
        if (classes.length == 0) {
jaroslav@874
   123
            return this;
jaroslav@874
   124
        } else {
jaroslav@1491
   125
            return new Bck2Brwsr(level, rootcls.addAndNew(classes), this.classes, res,
lubomir@1029
   126
                                 extension);
jaroslav@874
   127
        }
jaroslav@874
   128
    }
jaroslav@874
   129
    
jaroslav@1491
   130
    /** Adds additional classes 
jaroslav@1491
   131
     * to the list of those that should be included in the generated
jaroslav@1491
   132
     * JavaScript file. These classes are guaranteed to be present,
jaroslav@1491
   133
     * but they may not be accessible through their fully qualified
jaroslav@1491
   134
     * name.
jaroslav@1491
   135
     * 
jaroslav@1491
   136
     * @param classes the classes to add to the compilation
jaroslav@1491
   137
     * @return new instance of the Bck2Brwsr compiler which inherits
jaroslav@1491
   138
     * all values from <code>this</code>
jaroslav@1491
   139
     * @since 0.9
jaroslav@1491
   140
     */
jaroslav@1491
   141
    public Bck2Brwsr addClasses(String... classes) {
jaroslav@1491
   142
        if (classes.length == 0) {
jaroslav@1491
   143
            return this;
jaroslav@1491
   144
        } else {
jaroslav@1491
   145
            return new Bck2Brwsr(level, rootcls, this.classes.addAndNew(classes), res,
jaroslav@1491
   146
                extension);
jaroslav@1491
   147
        }
jaroslav@1491
   148
    }
jaroslav@1491
   149
    
jaroslav@874
   150
    /** Changes the obfuscation level for the compiler by creating new instance
jaroslav@874
   151
     * which inherits all values from <code>this</code> and adjust the level
jaroslav@874
   152
     * of obfuscation.
jaroslav@874
   153
     * 
jaroslav@874
   154
     * @param level the new level of obfuscation
jaroslav@874
   155
     * @return new instance of the compiler with changed level of obfuscation
jaroslav@874
   156
     * @since 0.5
jaroslav@874
   157
     */
jaroslav@874
   158
    public Bck2Brwsr obfuscation(ObfuscationLevel level) {
jaroslav@1491
   159
        return new Bck2Brwsr(level, rootcls, classes, res, extension);
jaroslav@874
   160
    }
jaroslav@874
   161
    
jaroslav@874
   162
    /** A way to change the provider of additional resources (classes) for the 
jaroslav@874
   163
     * compiler. 
jaroslav@874
   164
     * 
jaroslav@874
   165
     * @param res the implementation of resources provider
jaroslav@874
   166
     * @return new instance of the compiler with all values remaining the same, just 
jaroslav@874
   167
     *   with different resources provider
jaroslav@874
   168
     * @since 0.5
jaroslav@874
   169
     */
jaroslav@874
   170
    public Bck2Brwsr resources(Resources res) {
jaroslav@1491
   171
        return new Bck2Brwsr(level, rootcls, classes, res, extension);
lubomir@1029
   172
    }
lubomir@1029
   173
jaroslav@1491
   174
    /** Should one generate a library? By default the system generates
jaroslav@1491
   175
     * all transitive classes needed by the the transitive closure of
jaroslav@1491
   176
     * {@link #addRootClasses(java.lang.String...)} and {@link #addClasses(java.lang.String...)}.
jaroslav@1491
   177
     * By turning on the library mode, only classes explicitly listed
jaroslav@1491
   178
     * will be included in the archive. The others will be referenced
jaroslav@1491
   179
     * as external ones.
jaroslav@1491
   180
     * 
jaroslav@1491
   181
     * @param library turn on the library mode?
jaroslav@1491
   182
     * @return new instance of the compiler with library flag changed
jaroslav@1491
   183
     * @since 0.9
jaroslav@1491
   184
     */
jaroslav@1491
   185
    public Bck2Brwsr library(boolean library) {
jaroslav@1491
   186
        return new Bck2Brwsr(level, rootcls, classes, res, library);
jaroslav@874
   187
    }
jaroslav@874
   188
jaroslav@874
   189
    /** A way to change the provider of additional resources (classes) for the 
jaroslav@874
   190
     * compiler by specifying classloader to use for loading them.
jaroslav@874
   191
     * 
jaroslav@874
   192
     * @param loader class loader to load the resources from
jaroslav@874
   193
     * @return new instance of the compiler with all values being the same, just 
jaroslav@874
   194
     *   different resources provider
jaroslav@874
   195
     * @since 0.5
jaroslav@874
   196
     */
jaroslav@874
   197
    public Bck2Brwsr resources(final ClassLoader loader) {
jaroslav@874
   198
        return resources(new LdrRsrcs(loader));
jaroslav@874
   199
    }
jaroslav@874
   200
    
jaroslav@874
   201
    /** Generates virtual machine based on previous configuration of the 
jaroslav@874
   202
     * compiler.
lubomir@849
   203
     * 
lubomir@849
   204
     * @param out the output to write the generated JavaScript to
lubomir@860
   205
     * @since 0.5
lubomir@849
   206
     */
jaroslav@874
   207
    public void generate(Appendable out) throws IOException {
jaroslav@874
   208
        Resources r = res != null ? res : new LdrRsrcs(Bck2Brwsr.class.getClassLoader());
lubomir@1086
   209
        if (level != ObfuscationLevel.NONE) {
lubomir@1086
   210
            try {
jaroslav@1491
   211
                ClosureWrapper.produceTo(out, level, r, rootcls, classes, extension);
lubomir@1086
   212
                return;
lubomir@1086
   213
            } catch (IOException ex) {
lubomir@1086
   214
                throw ex;
lubomir@1086
   215
            } catch (Throwable ex) {
lubomir@1086
   216
                out.append("/* Failed to obfuscate: " + ex.getMessage()
lubomir@1086
   217
                               + " */\n");
lubomir@1086
   218
            }
lubomir@1086
   219
        }
lubomir@1029
   220
jaroslav@1491
   221
        VM.compile(out, r, rootcls, classes, extension);
jaroslav@298
   222
    }
lubomir@1086
   223
jaroslav@298
   224
    /** Provider of resources (classes and other files). The 
jaroslav@298
   225
     * {@link #generate(java.lang.Appendable, org.apidesign.vm4brwsr.Bck2Brwsr.Resources, java.lang.String[]) 
jaroslav@298
   226
     * generator method} will call back here for all classes needed during
jaroslav@298
   227
     * translation to JavaScript.
jaroslav@298
   228
     */
jaroslav@298
   229
    public interface Resources {
jaroslav@298
   230
        /** Loads given resource (class or other file like image). The 
jaroslav@298
   231
         * resource name to load bytes for the {@link String} class
jaroslav@298
   232
         * would be <code>"java/lang/String.class"</code>.
jaroslav@298
   233
         * 
jaroslav@298
   234
         * @param resource path to resource to load
jaroslav@298
   235
         * @return the input stream for the resource 
jaroslav@298
   236
         * @throws IOException can be thrown if the loading fails on some error
jaroslav@298
   237
         *   or the file cannot be found
jaroslav@298
   238
         */
jaroslav@298
   239
        public InputStream get(String resource) throws IOException;
jaroslav@298
   240
    }
jaroslav@298
   241
}