rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 21 May 2014 23:42:24 +0200
branchclosure
changeset 1583 89b6b369c13d
parent 1513 ba912ef24b27
child 1584 7b6295731c30
permissions -rw-r--r--
Support for exporting whole packages. addRootClasses defined in terms of addClasses and addExported.
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
jaroslav@298
    23
/** Build your own virtual machine! Use methods in this class to generate
jaroslav@298
    24
 * a skeleton JVM in JavaScript that contains pre-compiled classes of your
jaroslav@298
    25
 * choice. The generated script defines one JavaScript method that can
jaroslav@298
    26
 * be used to bootstrap and load the virtual machine: <pre>
jaroslav@298
    27
 * var vm = bck2brwsr();
jaroslav@298
    28
 * var main = vm.loadClass('org.your.pkg.Main');
jaroslav@298
    29
 * main.main__V_3Ljava_lang_String_2(null);
jaroslav@298
    30
 * </pre>
jaroslav@298
    31
 * In case one wants to initialize the virtual machine with ability to
jaroslav@298
    32
 * load classes lazily when needed, one can provide a loader function to
jaroslav@298
    33
 * when creating the virtual machine: <pre>
jaroslav@298
    34
 * var vm = bck2brwsr(function(resource) { 
jaroslav@298
    35
 *   return null; // byte[] for the resource
jaroslav@298
    36
 * });
jaroslav@298
    37
 * </pre>
jaroslav@424
    38
 * In this scenario, when a request for an unknown class is made, the loader
jaroslav@298
    39
 * function is asked for its byte code and the system dynamically transforms
jaroslav@298
    40
 * it to JavaScript.
jaroslav@729
    41
 * <p>
jaroslav@729
    42
 * Instead of a loader function, one can also provide a URL to a JAR file.
jaroslav@729
    43
 * The <code>bck2brwsr</code> system will do its best to download the file
jaroslav@729
    44
 * and provide loader function for it automatically.
jaroslav@729
    45
 * <p>
jaroslav@729
    46
 * One can provide as many loader functions and JAR URL references as necessary.
jaroslav@729
    47
 * Then the initialization code would look like:<pre>
jaroslav@729
    48
 * var vm = bck2brwsr(url1, url2, fnctn1, url3, functn2);
jaroslav@729
    49
 * </pre>
jaroslav@729
    50
 * The provided URLs and loader functions will be consulted one by one.
jaroslav@298
    51
 *
jaroslav@298
    52
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@298
    53
 */
jaroslav@298
    54
public final class Bck2Brwsr {
jaroslav@874
    55
    private final ObfuscationLevel level;
jaroslav@1583
    56
    private final StringArray exported;
jaroslav@874
    57
    private final StringArray classes;
jaroslav@1493
    58
    private final StringArray resources;
jaroslav@874
    59
    private final Resources res;
lubomir@1029
    60
    private final boolean extension;
jaroslav@874
    61
jaroslav@1583
    62
    private Bck2Brwsr(ObfuscationLevel level, StringArray exported, StringArray classes, StringArray resources, Resources res, boolean extension) {
jaroslav@874
    63
        this.level = level;
jaroslav@1583
    64
        this.exported = exported;
jaroslav@874
    65
        this.classes = classes;
jaroslav@1493
    66
        this.resources = resources;
jaroslav@1493
    67
        this.res = res;
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@1493
   105
        return new Bck2Brwsr(ObfuscationLevel.NONE, new StringArray(), new StringArray(), new StringArray(), null, false);
lubomir@849
   106
    }
jaroslav@1583
   107
    
jaroslav@1583
   108
    /** Adds exported classes or packages. If the string ends 
jaroslav@1583
   109
     * with slash, it is considered a name of package. If it does not,
jaroslav@1583
   110
     * it is a name of a class (without <code>.class</code> suffix).
jaroslav@1583
   111
     * The exported classes are prevented from being obfuscated. 
jaroslav@1583
   112
     * All public classes in exported packages are prevented from
jaroslav@1583
   113
     * being obfuscated. By listing the packages or classes in this 
jaroslav@1583
   114
     * method, these classes are not guaranteed to be included in
jaroslav@1583
   115
     * the generated script. Use {@link #addClasses} to include
jaroslav@1583
   116
     * the classes.
jaroslav@1583
   117
     * 
jaroslav@1583
   118
     * @param exported names of classes and packages to treat as exported
jaroslav@1583
   119
     * @return new instances of the Bck2Brwsr compiler which inherits
jaroslav@1583
   120
     *   all values from <code>this</code> except list of exported classes
jaroslav@1583
   121
     */
jaroslav@1583
   122
    public Bck2Brwsr addExported(String... exported) {
jaroslav@1583
   123
        return new Bck2Brwsr(
jaroslav@1583
   124
            level, this.exported.addAndNew(exported), 
jaroslav@1583
   125
            classes, resources, res, extension
jaroslav@1583
   126
        );
jaroslav@1583
   127
    }
lubomir@849
   128
jaroslav@1491
   129
    /** Adds additional classes 
jaroslav@1491
   130
     * to the list of those that should be included in the generated
jaroslav@1491
   131
     * JavaScript file.
jaroslav@1491
   132
     * These classes are guaranteed to be available in the
jaroslav@1491
   133
     * generated virtual machine code accessible using their fully 
jaroslav@1491
   134
     * qualified name. This brings the same behavior as if the
jaroslav@1491
   135
     * classes were added by {@link #addClasses(java.lang.String...) } and
jaroslav@1583
   136
     * exported via {@link #addExported(java.lang.String...)}.
jaroslav@874
   137
     * 
jaroslav@874
   138
     * @param classes the classes to add to the compilation
jaroslav@1491
   139
     * @return new instance of the Bck2Brwsr compiler which inherits
jaroslav@1491
   140
     * all values from <code>this</code>
jaroslav@874
   141
     */
jaroslav@874
   142
    public Bck2Brwsr addRootClasses(String... classes) {
jaroslav@874
   143
        if (classes.length == 0) {
jaroslav@874
   144
            return this;
jaroslav@1583
   145
        } 
jaroslav@1583
   146
        return addExported(classes).addClasses(classes);
jaroslav@874
   147
    }
jaroslav@874
   148
    
jaroslav@1491
   149
    /** Adds additional classes 
jaroslav@1491
   150
     * to the list of those that should be included in the generated
jaroslav@1491
   151
     * JavaScript file. These classes are guaranteed to be present,
jaroslav@1491
   152
     * but they may not be accessible through their fully qualified
jaroslav@1491
   153
     * name.
jaroslav@1491
   154
     * 
jaroslav@1491
   155
     * @param classes the classes to add to the compilation
jaroslav@1491
   156
     * @return new instance of the Bck2Brwsr compiler which inherits
jaroslav@1491
   157
     * all values from <code>this</code>
jaroslav@1491
   158
     * @since 0.9
jaroslav@1491
   159
     */
jaroslav@1491
   160
    public Bck2Brwsr addClasses(String... classes) {
jaroslav@1491
   161
        if (classes.length == 0) {
jaroslav@1491
   162
            return this;
jaroslav@1491
   163
        } else {
jaroslav@1583
   164
            return new Bck2Brwsr(level, exported, 
jaroslav@1583
   165
                this.classes.addAndNew(classes), resources, res,
jaroslav@1491
   166
                extension);
jaroslav@1491
   167
        }
jaroslav@1491
   168
    }
jaroslav@1491
   169
    
jaroslav@1493
   170
    /** These resources should be made available in the compiled file in
jaroslav@1493
   171
     * binary form. These resources can then be loaded
jaroslav@1493
   172
     * by {@link ClassLoader#getResource(java.lang.String)} and similar 
jaroslav@1493
   173
     * methods.
jaroslav@1493
   174
     * 
jaroslav@1493
   175
     * @param resources names of the resources to be loaded by {@link Resources#get(java.lang.String)}
jaroslav@1493
   176
     * @return new instance of the Bck2Brwsr compiler which inherits
jaroslav@1493
   177
     *   all values from <code>this</code> just adds few more resource names
jaroslav@1493
   178
     *   for processing
jaroslav@1493
   179
     * @since 0.9
jaroslav@1493
   180
     */
jaroslav@1493
   181
    public Bck2Brwsr addResources(String... resources) {
jaroslav@1493
   182
        if (resources.length == 0) {
jaroslav@1493
   183
            return this;
jaroslav@1493
   184
        } else {
jaroslav@1583
   185
            return new Bck2Brwsr(level, exported, this.classes, 
jaroslav@1493
   186
                this.resources.addAndNew(resources), res, extension
jaroslav@1493
   187
            );
jaroslav@874
   188
        }
jaroslav@874
   189
    }
jaroslav@874
   190
    
jaroslav@874
   191
    /** Changes the obfuscation level for the compiler by creating new instance
jaroslav@874
   192
     * which inherits all values from <code>this</code> and adjust the level
jaroslav@874
   193
     * of obfuscation.
jaroslav@874
   194
     * 
jaroslav@874
   195
     * @param level the new level of obfuscation
jaroslav@874
   196
     * @return new instance of the compiler with changed level of obfuscation
jaroslav@874
   197
     * @since 0.5
jaroslav@874
   198
     */
jaroslav@874
   199
    public Bck2Brwsr obfuscation(ObfuscationLevel level) {
jaroslav@1583
   200
        return new Bck2Brwsr(level, exported, classes, resources, res, extension);
jaroslav@874
   201
    }
jaroslav@874
   202
    
jaroslav@874
   203
    /** A way to change the provider of additional resources (classes) for the 
jaroslav@874
   204
     * compiler. 
jaroslav@874
   205
     * 
jaroslav@874
   206
     * @param res the implementation of resources provider
jaroslav@874
   207
     * @return new instance of the compiler with all values remaining the same, just 
jaroslav@874
   208
     *   with different resources provider
jaroslav@874
   209
     * @since 0.5
jaroslav@874
   210
     */
jaroslav@874
   211
    public Bck2Brwsr resources(Resources res) {
jaroslav@1583
   212
        return new Bck2Brwsr(level, exported, classes, resources, res, extension);
lubomir@1029
   213
    }
lubomir@1029
   214
jaroslav@1491
   215
    /** Should one generate a library? By default the system generates
jaroslav@1491
   216
     * all transitive classes needed by the the transitive closure of
jaroslav@1491
   217
     * {@link #addRootClasses(java.lang.String...)} and {@link #addClasses(java.lang.String...)}.
jaroslav@1491
   218
     * By turning on the library mode, only classes explicitly listed
jaroslav@1491
   219
     * will be included in the archive. The others will be referenced
jaroslav@1491
   220
     * as external ones.
jaroslav@1491
   221
     * 
jaroslav@1491
   222
     * @param library turn on the library mode?
jaroslav@1491
   223
     * @return new instance of the compiler with library flag changed
jaroslav@1491
   224
     * @since 0.9
jaroslav@1491
   225
     */
jaroslav@1491
   226
    public Bck2Brwsr library(boolean library) {
jaroslav@1583
   227
        return new Bck2Brwsr(level, exported, classes, resources, res, library);
jaroslav@874
   228
    }
jaroslav@874
   229
jaroslav@874
   230
    /** A way to change the provider of additional resources (classes) for the 
jaroslav@874
   231
     * compiler by specifying classloader to use for loading them.
jaroslav@874
   232
     * 
jaroslav@874
   233
     * @param loader class loader to load the resources from
jaroslav@874
   234
     * @return new instance of the compiler with all values being the same, just 
jaroslav@874
   235
     *   different resources provider
jaroslav@874
   236
     * @since 0.5
jaroslav@874
   237
     */
jaroslav@874
   238
    public Bck2Brwsr resources(final ClassLoader loader) {
jaroslav@1365
   239
        return resources(loader, false);
jaroslav@1365
   240
    }
jaroslav@1365
   241
jaroslav@1365
   242
    /** A way to change the provider of additional resources (classes) for the 
jaroslav@1365
   243
     * compiler by specifying classloader to use for loading them.
jaroslav@1365
   244
     * 
jaroslav@1365
   245
     * @param loader class loader to load the resources from
jaroslav@1365
   246
     * @param ignoreBootClassPath <code>true</code> if classes loaded
jaroslav@1365
   247
     *    from <code>rt.jar</code> 
jaroslav@1365
   248
     * @return new instance of the compiler with all values being the same, just 
jaroslav@1365
   249
     *   different resources provider
jaroslav@1365
   250
     * @since 0.9
jaroslav@1365
   251
     */
jaroslav@1365
   252
    public Bck2Brwsr resources(final ClassLoader loader, boolean ignoreBootClassPath) {
jaroslav@1365
   253
        return resources(new LdrRsrcs(loader, ignoreBootClassPath));
jaroslav@874
   254
    }
jaroslav@874
   255
    
jaroslav@874
   256
    /** Generates virtual machine based on previous configuration of the 
jaroslav@874
   257
     * compiler.
lubomir@849
   258
     * 
lubomir@849
   259
     * @param out the output to write the generated JavaScript to
lubomir@860
   260
     * @since 0.5
lubomir@849
   261
     */
jaroslav@874
   262
    public void generate(Appendable out) throws IOException {
jaroslav@874
   263
        if (level != ObfuscationLevel.NONE) {
lubomir@849
   264
            try {
jaroslav@1493
   265
                ClosureWrapper.produceTo(out, level, this);
lubomir@849
   266
                return;
lubomir@849
   267
            } catch (IOException ex) {
lubomir@849
   268
                throw ex;
lubomir@849
   269
            } catch (Throwable ex) {
lubomir@849
   270
                out.append("/* Failed to obfuscate: " + ex.getMessage()
lubomir@849
   271
                               + " */\n");
lubomir@849
   272
            }
jaroslav@750
   273
        }
lubomir@849
   274
jaroslav@1493
   275
        VM.compile(out, this);
jaroslav@298
   276
    }
jaroslav@298
   277
    
jaroslav@1493
   278
    //
jaroslav@1493
   279
    // Internal getters
jaroslav@1493
   280
    // 
jaroslav@1493
   281
    
jaroslav@1493
   282
    Resources getResources() {
jaroslav@1513
   283
        return res != null ? res : new LdrRsrcs(Bck2Brwsr.class.getClassLoader(), false);
jaroslav@1493
   284
    }
jaroslav@1493
   285
    
jaroslav@1495
   286
    StringArray allResources() {
jaroslav@1495
   287
        return resources;
jaroslav@1495
   288
    }
jaroslav@1495
   289
jaroslav@1583
   290
    StringArray classes() {
jaroslav@1583
   291
        return classes;
jaroslav@1583
   292
    }
jaroslav@1583
   293
jaroslav@1583
   294
    StringArray exported() {
jaroslav@1583
   295
        return exported;
jaroslav@1493
   296
    }
jaroslav@1493
   297
    
jaroslav@1493
   298
    boolean isExtension() {
jaroslav@1493
   299
        return extension;
jaroslav@874
   300
    }
lubomir@1086
   301
jaroslav@298
   302
    /** Provider of resources (classes and other files). The 
jaroslav@298
   303
     * {@link #generate(java.lang.Appendable, org.apidesign.vm4brwsr.Bck2Brwsr.Resources, java.lang.String[]) 
jaroslav@298
   304
     * generator method} will call back here for all classes needed during
jaroslav@298
   305
     * translation to JavaScript.
jaroslav@298
   306
     */
jaroslav@298
   307
    public interface Resources {
jaroslav@298
   308
        /** Loads given resource (class or other file like image). The 
jaroslav@298
   309
         * resource name to load bytes for the {@link String} class
jaroslav@298
   310
         * would be <code>"java/lang/String.class"</code>.
jaroslav@298
   311
         * 
jaroslav@298
   312
         * @param resource path to resource to load
jaroslav@298
   313
         * @return the input stream for the resource 
jaroslav@298
   314
         * @throws IOException can be thrown if the loading fails on some error
jaroslav@298
   315
         *   or the file cannot be found
jaroslav@298
   316
         */
jaroslav@298
   317
        public InputStream get(String resource) throws IOException;
jaroslav@298
   318
    }
jaroslav@298
   319
}