rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 27 Apr 2014 22:40:17 +0200
branchclosure
changeset 1493 234fea368401
parent 1491 4a1398eff4fb
child 1495 d5dd07b45f79
permissions -rw-r--r--
Initial infrastructure to export resources
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@1491
    22
import org.apidesign.bck2brwsr.core.Exported;
jaroslav@298
    23
jaroslav@298
    24
/** Build your own virtual machine! Use methods in this class to generate
jaroslav@298
    25
 * a skeleton JVM in JavaScript that contains pre-compiled classes of your
jaroslav@298
    26
 * choice. The generated script defines one JavaScript method that can
jaroslav@298
    27
 * be used to bootstrap and load the virtual machine: <pre>
jaroslav@298
    28
 * var vm = bck2brwsr();
jaroslav@298
    29
 * var main = vm.loadClass('org.your.pkg.Main');
jaroslav@298
    30
 * main.main__V_3Ljava_lang_String_2(null);
jaroslav@298
    31
 * </pre>
jaroslav@298
    32
 * In case one wants to initialize the virtual machine with ability to
jaroslav@298
    33
 * load classes lazily when needed, one can provide a loader function to
jaroslav@298
    34
 * when creating the virtual machine: <pre>
jaroslav@298
    35
 * var vm = bck2brwsr(function(resource) { 
jaroslav@298
    36
 *   return null; // byte[] for the resource
jaroslav@298
    37
 * });
jaroslav@298
    38
 * </pre>
jaroslav@424
    39
 * In this scenario, when a request for an unknown class is made, the loader
jaroslav@298
    40
 * function is asked for its byte code and the system dynamically transforms
jaroslav@298
    41
 * it to JavaScript.
jaroslav@729
    42
 * <p>
jaroslav@729
    43
 * Instead of a loader function, one can also provide a URL to a JAR file.
jaroslav@729
    44
 * The <code>bck2brwsr</code> system will do its best to download the file
jaroslav@729
    45
 * and provide loader function for it automatically.
jaroslav@729
    46
 * <p>
jaroslav@729
    47
 * One can provide as many loader functions and JAR URL references as necessary.
jaroslav@729
    48
 * Then the initialization code would look like:<pre>
jaroslav@729
    49
 * var vm = bck2brwsr(url1, url2, fnctn1, url3, functn2);
jaroslav@729
    50
 * </pre>
jaroslav@729
    51
 * The provided URLs and loader functions will be consulted one by one.
jaroslav@298
    52
 *
jaroslav@298
    53
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@298
    54
 */
jaroslav@298
    55
public final class Bck2Brwsr {
jaroslav@874
    56
    private final ObfuscationLevel level;
jaroslav@1491
    57
    private final StringArray rootcls;
jaroslav@874
    58
    private final StringArray classes;
jaroslav@1493
    59
    private final StringArray resources;
jaroslav@874
    60
    private final Resources res;
lubomir@1029
    61
    private final boolean extension;
jaroslav@874
    62
jaroslav@1493
    63
    private Bck2Brwsr(ObfuscationLevel level, StringArray rootcls, StringArray classes, StringArray resources, Resources res, boolean extension) {
jaroslav@874
    64
        this.level = level;
jaroslav@1491
    65
        this.rootcls = rootcls;
jaroslav@874
    66
        this.classes = classes;
jaroslav@1493
    67
        this.resources = resources;
jaroslav@1493
    68
        this.res = res;
lubomir@1029
    69
        this.extension = extension;
jaroslav@298
    70
    }
jaroslav@874
    71
    
jaroslav@874
    72
    /** Helper method to generate virtual machine from bytes served by a <code>resources</code>
jaroslav@298
    73
     * provider.
lubomir@849
    74
     *
jaroslav@298
    75
     * @param out the output to write the generated JavaScript to
jaroslav@298
    76
     * @param resources provider of class files to use
jaroslav@298
    77
     * @param classes additional classes to include in the generated script
jaroslav@298
    78
     * @throws IOException I/O exception can be thrown when something goes wrong
jaroslav@298
    79
     */
jaroslav@298
    80
    public static void generate(Appendable out, Resources resources, String... classes) throws IOException {
jaroslav@874
    81
        newCompiler().resources(resources).addRootClasses(classes).generate(out);
lubomir@849
    82
    }
lubomir@849
    83
jaroslav@874
    84
    /** Helper method to generate virtual machine from bytes served by a class loader.
lubomir@849
    85
     *
lubomir@849
    86
     * @param out the output to write the generated JavaScript to
lubomir@849
    87
     * @param loader class loader to load needed classes from
lubomir@849
    88
     * @param classes additional classes to include in the generated script
lubomir@849
    89
     * @throws IOException I/O exception can be thrown when something goes wrong
lubomir@849
    90
     */
lubomir@849
    91
    public static void generate(Appendable out, ClassLoader loader, String... classes) throws IOException {
jaroslav@874
    92
        newCompiler().resources(loader).addRootClasses(classes).generate(out);
jaroslav@874
    93
    }
jaroslav@874
    94
    
jaroslav@874
    95
    /** Creates new instance of Bck2Brwsr compiler which is ready to generate
jaroslav@874
    96
     * empty Bck2Brwsr virtual machine. The instance can be further
jaroslav@874
    97
     * configured by calling chain of methods. For example: 
jaroslav@874
    98
     * <pre>
jaroslav@874
    99
     * {@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
   100
     * </pre>
jaroslav@874
   101
     * 
jaroslav@874
   102
     * @return new instance of the Bck2Brwsr compiler
jaroslav@874
   103
     * @since 0.5
jaroslav@874
   104
     */
jaroslav@874
   105
    public static Bck2Brwsr newCompiler() {
jaroslav@1493
   106
        return new Bck2Brwsr(ObfuscationLevel.NONE, new StringArray(), new StringArray(), new StringArray(), null, false);
lubomir@849
   107
    }
lubomir@849
   108
jaroslav@1491
   109
    /** Adds additional classes 
jaroslav@1491
   110
     * to the list of those that should be included in the generated
jaroslav@1491
   111
     * JavaScript file.
jaroslav@1491
   112
     * These classes are guaranteed to be available in the
jaroslav@1491
   113
     * generated virtual machine code accessible using their fully 
jaroslav@1491
   114
     * qualified name. This brings the same behavior as if the
jaroslav@1491
   115
     * classes were added by {@link #addClasses(java.lang.String...) } and
jaroslav@1491
   116
     * were annotated with {@link Exported} annotation.
jaroslav@874
   117
     * 
jaroslav@874
   118
     * @param classes the classes to add to the compilation
jaroslav@1491
   119
     * @return new instance of the Bck2Brwsr compiler which inherits
jaroslav@1491
   120
     * all values from <code>this</code>
jaroslav@874
   121
     */
jaroslav@874
   122
    public Bck2Brwsr addRootClasses(String... classes) {
jaroslav@874
   123
        if (classes.length == 0) {
jaroslav@874
   124
            return this;
jaroslav@874
   125
        } else {
jaroslav@1493
   126
            return new Bck2Brwsr(level, rootcls.addAndNew(classes), this.classes, resources, res,
lubomir@1029
   127
                                 extension);
jaroslav@874
   128
        }
jaroslav@874
   129
    }
jaroslav@874
   130
    
jaroslav@1491
   131
    /** Adds additional classes 
jaroslav@1491
   132
     * to the list of those that should be included in the generated
jaroslav@1491
   133
     * JavaScript file. These classes are guaranteed to be present,
jaroslav@1491
   134
     * but they may not be accessible through their fully qualified
jaroslav@1491
   135
     * name.
jaroslav@1491
   136
     * 
jaroslav@1491
   137
     * @param classes the classes to add to the compilation
jaroslav@1491
   138
     * @return new instance of the Bck2Brwsr compiler which inherits
jaroslav@1491
   139
     * all values from <code>this</code>
jaroslav@1491
   140
     * @since 0.9
jaroslav@1491
   141
     */
jaroslav@1491
   142
    public Bck2Brwsr addClasses(String... classes) {
jaroslav@1491
   143
        if (classes.length == 0) {
jaroslav@1491
   144
            return this;
jaroslav@1491
   145
        } else {
jaroslav@1493
   146
            return new Bck2Brwsr(level, rootcls, this.classes.addAndNew(classes), resources, res,
jaroslav@1491
   147
                extension);
jaroslav@1491
   148
        }
jaroslav@1491
   149
    }
jaroslav@1491
   150
    
jaroslav@1493
   151
    /** These resources should be made available in the compiled file in
jaroslav@1493
   152
     * binary form. These resources can then be loaded
jaroslav@1493
   153
     * by {@link ClassLoader#getResource(java.lang.String)} and similar 
jaroslav@1493
   154
     * methods.
jaroslav@1493
   155
     * 
jaroslav@1493
   156
     * @param resources names of the resources to be loaded by {@link Resources#get(java.lang.String)}
jaroslav@1493
   157
     * @return new instance of the Bck2Brwsr compiler which inherits
jaroslav@1493
   158
     *   all values from <code>this</code> just adds few more resource names
jaroslav@1493
   159
     *   for processing
jaroslav@1493
   160
     * @since 0.9
jaroslav@1493
   161
     */
jaroslav@1493
   162
    public Bck2Brwsr addResources(String... resources) {
jaroslav@1493
   163
        if (resources.length == 0) {
jaroslav@1493
   164
            return this;
jaroslav@1493
   165
        } else {
jaroslav@1493
   166
            return new Bck2Brwsr(level, rootcls, this.classes, 
jaroslav@1493
   167
                this.resources.addAndNew(resources), res, extension
jaroslav@1493
   168
            );
jaroslav@1493
   169
        }
jaroslav@1493
   170
    }
jaroslav@1493
   171
    
jaroslav@874
   172
    /** Changes the obfuscation level for the compiler by creating new instance
jaroslav@874
   173
     * which inherits all values from <code>this</code> and adjust the level
jaroslav@874
   174
     * of obfuscation.
jaroslav@874
   175
     * 
jaroslav@874
   176
     * @param level the new level of obfuscation
jaroslav@874
   177
     * @return new instance of the compiler with changed level of obfuscation
jaroslav@874
   178
     * @since 0.5
jaroslav@874
   179
     */
jaroslav@874
   180
    public Bck2Brwsr obfuscation(ObfuscationLevel level) {
jaroslav@1493
   181
        return new Bck2Brwsr(level, rootcls, classes, resources, res, extension);
jaroslav@874
   182
    }
jaroslav@874
   183
    
jaroslav@874
   184
    /** A way to change the provider of additional resources (classes) for the 
jaroslav@874
   185
     * compiler. 
jaroslav@874
   186
     * 
jaroslav@874
   187
     * @param res the implementation of resources provider
jaroslav@874
   188
     * @return new instance of the compiler with all values remaining the same, just 
jaroslav@874
   189
     *   with different resources provider
jaroslav@874
   190
     * @since 0.5
jaroslav@874
   191
     */
jaroslav@874
   192
    public Bck2Brwsr resources(Resources res) {
jaroslav@1493
   193
        return new Bck2Brwsr(level, rootcls, classes, resources, res, extension);
lubomir@1029
   194
    }
lubomir@1029
   195
jaroslav@1491
   196
    /** Should one generate a library? By default the system generates
jaroslav@1491
   197
     * all transitive classes needed by the the transitive closure of
jaroslav@1491
   198
     * {@link #addRootClasses(java.lang.String...)} and {@link #addClasses(java.lang.String...)}.
jaroslav@1491
   199
     * By turning on the library mode, only classes explicitly listed
jaroslav@1491
   200
     * will be included in the archive. The others will be referenced
jaroslav@1491
   201
     * as external ones.
jaroslav@1491
   202
     * 
jaroslav@1491
   203
     * @param library turn on the library mode?
jaroslav@1491
   204
     * @return new instance of the compiler with library flag changed
jaroslav@1491
   205
     * @since 0.9
jaroslav@1491
   206
     */
jaroslav@1491
   207
    public Bck2Brwsr library(boolean library) {
jaroslav@1493
   208
        return new Bck2Brwsr(level, rootcls, classes, resources, res, library);
jaroslav@874
   209
    }
jaroslav@874
   210
jaroslav@874
   211
    /** A way to change the provider of additional resources (classes) for the 
jaroslav@874
   212
     * compiler by specifying classloader to use for loading them.
jaroslav@874
   213
     * 
jaroslav@874
   214
     * @param loader class loader to load the resources from
jaroslav@874
   215
     * @return new instance of the compiler with all values being the same, just 
jaroslav@874
   216
     *   different resources provider
jaroslav@874
   217
     * @since 0.5
jaroslav@874
   218
     */
jaroslav@874
   219
    public Bck2Brwsr resources(final ClassLoader loader) {
jaroslav@874
   220
        return resources(new LdrRsrcs(loader));
jaroslav@874
   221
    }
jaroslav@874
   222
    
jaroslav@874
   223
    /** Generates virtual machine based on previous configuration of the 
jaroslav@874
   224
     * compiler.
lubomir@849
   225
     * 
lubomir@849
   226
     * @param out the output to write the generated JavaScript to
lubomir@860
   227
     * @since 0.5
lubomir@849
   228
     */
jaroslav@874
   229
    public void generate(Appendable out) throws IOException {
lubomir@1086
   230
        if (level != ObfuscationLevel.NONE) {
lubomir@1086
   231
            try {
jaroslav@1493
   232
                ClosureWrapper.produceTo(out, level, this);
lubomir@1086
   233
                return;
lubomir@1086
   234
            } catch (IOException ex) {
lubomir@1086
   235
                throw ex;
lubomir@1086
   236
            } catch (Throwable ex) {
lubomir@1086
   237
                out.append("/* Failed to obfuscate: " + ex.getMessage()
lubomir@1086
   238
                               + " */\n");
lubomir@1086
   239
            }
lubomir@1086
   240
        }
lubomir@1029
   241
jaroslav@1493
   242
        VM.compile(out, this);
jaroslav@1493
   243
    }
jaroslav@1493
   244
    
jaroslav@1493
   245
    //
jaroslav@1493
   246
    // Internal getters
jaroslav@1493
   247
    // 
jaroslav@1493
   248
    
jaroslav@1493
   249
    Resources getResources() {
jaroslav@1493
   250
        return res != null ? res : new LdrRsrcs(Bck2Brwsr.class.getClassLoader());
jaroslav@1493
   251
    }
jaroslav@1493
   252
    
jaroslav@1493
   253
    String[] allClasses() {
jaroslav@1493
   254
        return classes.addAndNew(rootcls.toArray()).toArray();
jaroslav@1493
   255
    }
jaroslav@1493
   256
    
jaroslav@1493
   257
    StringArray rootClasses() {
jaroslav@1493
   258
        return rootcls;
jaroslav@1493
   259
    }
jaroslav@1493
   260
    
jaroslav@1493
   261
    boolean isExtension() {
jaroslav@1493
   262
        return extension;
jaroslav@298
   263
    }
lubomir@1086
   264
jaroslav@298
   265
    /** Provider of resources (classes and other files). The 
jaroslav@298
   266
     * {@link #generate(java.lang.Appendable, org.apidesign.vm4brwsr.Bck2Brwsr.Resources, java.lang.String[]) 
jaroslav@298
   267
     * generator method} will call back here for all classes needed during
jaroslav@298
   268
     * translation to JavaScript.
jaroslav@298
   269
     */
jaroslav@298
   270
    public interface Resources {
jaroslav@298
   271
        /** Loads given resource (class or other file like image). The 
jaroslav@298
   272
         * resource name to load bytes for the {@link String} class
jaroslav@298
   273
         * would be <code>"java/lang/String.class"</code>.
jaroslav@298
   274
         * 
jaroslav@298
   275
         * @param resource path to resource to load
jaroslav@298
   276
         * @return the input stream for the resource 
jaroslav@298
   277
         * @throws IOException can be thrown if the loading fails on some error
jaroslav@298
   278
         *   or the file cannot be found
jaroslav@298
   279
         */
jaroslav@298
   280
        public InputStream get(String resource) throws IOException;
jaroslav@298
   281
    }
jaroslav@298
   282
}