rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 11 Mar 2015 18:58:39 +0100
branchflow
changeset 1812 4fef6b767f61
parent 1787 ea12a3bb4b33
child 1813 5c30fa1c8c5b
permissions -rw-r--r--
Defining API for registration of a Flow.Analyzer and getting ready for use the one from Graal
jaroslav@298
     1
/**
jaroslav@298
     2
 * Back 2 Browser Bytecode Translator
jaroslav@1787
     3
 * Copyright (C) 2012-2015 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@1783
    25
 * choice:
jaroslav@1783
    26
 * <pre>
jaroslav@1783
    27
 * Writer w = new {@link java.io.StringWriter}();
jaroslav@1783
    28
 * {@link #newCompiler() Bck2Brwsr.newCompiler}()
jaroslav@1783
    29
 *   .{@link #resources(org.apidesign.vm4brwsr.Bck2Brwsr.Resources)}
jaroslav@1783
    30
 *   .{@link #addRootClasses(java.lang.String...)}
jaroslav@1783
    31
 *   .{@link #addClasses(java.lang.String...)}
jaroslav@1783
    32
 *   .{@link #addExported(java.lang.String...)}
jaroslav@1783
    33
 *   .{@link #generate(java.lang.Appendable) generate(w)};
jaroslav@1783
    34
 * System.out.{@link java.io.PrintStream#print(java.lang.String) print(w.toString())};
jaroslav@1783
    35
 * </pre>
jaroslav@1783
    36
 * The generated script defines one JavaScript method that can
jaroslav@298
    37
 * be used to bootstrap and load the virtual machine: <pre>
jaroslav@298
    38
 * var vm = bck2brwsr();
jaroslav@298
    39
 * var main = vm.loadClass('org.your.pkg.Main');
jaroslav@1609
    40
 * main.invoke('main');
jaroslav@298
    41
 * </pre>
jaroslav@298
    42
 * In case one wants to initialize the virtual machine with ability to
jaroslav@298
    43
 * load classes lazily when needed, one can provide a loader function to
jaroslav@298
    44
 * when creating the virtual machine: <pre>
jaroslav@298
    45
 * var vm = bck2brwsr(function(resource) { 
jaroslav@298
    46
 *   return null; // byte[] for the resource
jaroslav@298
    47
 * });
jaroslav@298
    48
 * </pre>
jaroslav@424
    49
 * In this scenario, when a request for an unknown class is made, the loader
jaroslav@298
    50
 * function is asked for its byte code and the system dynamically transforms
jaroslav@298
    51
 * it to JavaScript.
jaroslav@729
    52
 * <p>
jaroslav@1609
    53
 * Instead of a loader function, one can also provide a URL to a JAR file
jaroslav@1609
    54
 * or a library JavaScript file generated with {@link #library(java.lang.String...)}
jaroslav@1609
    55
 * option on.
jaroslav@729
    56
 * The <code>bck2brwsr</code> system will do its best to download the file
jaroslav@1609
    57
 * and provide loader function for it automatically. In order to use
jaroslav@1609
    58
 * the JAR file <code>emul.zip</code> module needs to be available in the system.
jaroslav@729
    59
 * <p>
jaroslav@1609
    60
 * One can provide as many loader functions and URL references as necessary.
jaroslav@729
    61
 * Then the initialization code would look like:<pre>
jaroslav@729
    62
 * var vm = bck2brwsr(url1, url2, fnctn1, url3, functn2);
jaroslav@729
    63
 * </pre>
jaroslav@729
    64
 * The provided URLs and loader functions will be consulted one by one.
jaroslav@1609
    65
 * <p>
jaroslav@1609
    66
 * The initialization of the <b>Bck2Brwsr</b> is done asynchronously since 
jaroslav@1609
    67
 * version 0.9. E.g. call to <pre>
jaroslav@1609
    68
 * var vm = bck2brwsr('myapp.js');
jaroslav@1609
    69
 * var main = vm.loadClass('org.your.pkg.Main');
jaroslav@1609
    70
 * main.invoke('main');
jaroslav@1609
    71
 * </pre>
jaroslav@1609
    72
 * returns immediately and the call to the static main method will happen
jaroslav@1609
    73
 * once the virtual machine is initialized and the class available.
jaroslav@298
    74
 *
jaroslav@1783
    75
 * @author Jaroslav Tulach
jaroslav@298
    76
 */
jaroslav@298
    77
public final class Bck2Brwsr {
jaroslav@874
    78
    private final ObfuscationLevel level;
jaroslav@1583
    79
    private final StringArray exported;
jaroslav@874
    80
    private final StringArray classes;
jaroslav@1493
    81
    private final StringArray resources;
jaroslav@874
    82
    private final Resources res;
jaroslav@1584
    83
    private final Boolean extension;
jaroslav@1604
    84
    private final StringArray classpath;
jaroslav@1812
    85
    private final Flow.Analyzer flow;
jaroslav@874
    86
jaroslav@1584
    87
    private Bck2Brwsr(
jaroslav@1812
    88
        ObfuscationLevel level, StringArray exported,
jaroslav@1812
    89
        StringArray classes, StringArray resources, Resources res,
jaroslav@1812
    90
        Boolean extension, StringArray classpath, Flow.Analyzer flow
jaroslav@1584
    91
    ) {
jaroslav@874
    92
        this.level = level;
jaroslav@1583
    93
        this.exported = exported;
jaroslav@874
    94
        this.classes = classes;
jaroslav@1493
    95
        this.resources = resources;
jaroslav@1493
    96
        this.res = res;
lubomir@1029
    97
        this.extension = extension;
jaroslav@1604
    98
        this.classpath = classpath;
jaroslav@1812
    99
        this.flow = flow;
jaroslav@298
   100
    }
jaroslav@874
   101
    
jaroslav@874
   102
    /** Helper method to generate virtual machine from bytes served by a <code>resources</code>
jaroslav@298
   103
     * provider.
lubomir@849
   104
     *
jaroslav@298
   105
     * @param out the output to write the generated JavaScript to
jaroslav@298
   106
     * @param resources provider of class files to use
jaroslav@298
   107
     * @param classes additional classes to include in the generated script
jaroslav@298
   108
     * @throws IOException I/O exception can be thrown when something goes wrong
jaroslav@298
   109
     */
jaroslav@298
   110
    public static void generate(Appendable out, Resources resources, String... classes) throws IOException {
jaroslav@874
   111
        newCompiler().resources(resources).addRootClasses(classes).generate(out);
lubomir@849
   112
    }
lubomir@849
   113
jaroslav@874
   114
    /** Helper method to generate virtual machine from bytes served by a class loader.
lubomir@849
   115
     *
lubomir@849
   116
     * @param out the output to write the generated JavaScript to
lubomir@849
   117
     * @param loader class loader to load needed classes from
lubomir@849
   118
     * @param classes additional classes to include in the generated script
lubomir@849
   119
     * @throws IOException I/O exception can be thrown when something goes wrong
lubomir@849
   120
     */
lubomir@849
   121
    public static void generate(Appendable out, ClassLoader loader, String... classes) throws IOException {
jaroslav@874
   122
        newCompiler().resources(loader).addRootClasses(classes).generate(out);
jaroslav@874
   123
    }
jaroslav@874
   124
    
jaroslav@874
   125
    /** Creates new instance of Bck2Brwsr compiler which is ready to generate
jaroslav@874
   126
     * empty Bck2Brwsr virtual machine. The instance can be further
jaroslav@874
   127
     * configured by calling chain of methods. For example: 
jaroslav@874
   128
     * <pre>
jaroslav@1783
   129
     * {@link #newCompiler()}.{@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
   130
     * </pre>
jaroslav@874
   131
     * 
jaroslav@874
   132
     * @return new instance of the Bck2Brwsr compiler
jaroslav@874
   133
     * @since 0.5
jaroslav@874
   134
     */
jaroslav@874
   135
    public static Bck2Brwsr newCompiler() {
jaroslav@1604
   136
        return new Bck2Brwsr(
jaroslav@1604
   137
            ObfuscationLevel.NONE, 
jaroslav@1604
   138
            new StringArray(), new StringArray(), new StringArray(), 
jaroslav@1812
   139
            null, false, null, null
jaroslav@1604
   140
        );
lubomir@849
   141
    }
jaroslav@1583
   142
    
jaroslav@1583
   143
    /** Adds exported classes or packages. If the string ends 
jaroslav@1583
   144
     * with slash, it is considered a name of package. If it does not,
jaroslav@1583
   145
     * it is a name of a class (without <code>.class</code> suffix).
jaroslav@1583
   146
     * The exported classes are prevented from being obfuscated. 
jaroslav@1583
   147
     * All public classes in exported packages are prevented from
jaroslav@1583
   148
     * being obfuscated. By listing the packages or classes in this 
jaroslav@1583
   149
     * method, these classes are not guaranteed to be included in
jaroslav@1583
   150
     * the generated script. Use {@link #addClasses} to include
jaroslav@1583
   151
     * the classes.
jaroslav@1583
   152
     * 
jaroslav@1583
   153
     * @param exported names of classes and packages to treat as exported
jaroslav@1583
   154
     * @return new instances of the Bck2Brwsr compiler which inherits
jaroslav@1583
   155
     *   all values from <code>this</code> except list of exported classes
jaroslav@1583
   156
     */
jaroslav@1583
   157
    public Bck2Brwsr addExported(String... exported) {
jaroslav@1583
   158
        return new Bck2Brwsr(
jaroslav@1583
   159
            level, this.exported.addAndNew(exported), 
jaroslav@1812
   160
            classes, resources, res, extension, classpath, flow
jaroslav@1583
   161
        );
jaroslav@1583
   162
    }
lubomir@849
   163
jaroslav@1491
   164
    /** Adds additional classes 
jaroslav@1491
   165
     * to the list of those that should be included in the generated
jaroslav@1491
   166
     * JavaScript file.
jaroslav@1491
   167
     * These classes are guaranteed to be available in the
jaroslav@1491
   168
     * generated virtual machine code accessible using their fully 
jaroslav@1491
   169
     * qualified name. This brings the same behavior as if the
jaroslav@1491
   170
     * classes were added by {@link #addClasses(java.lang.String...) } and
jaroslav@1583
   171
     * exported via {@link #addExported(java.lang.String...)}.
jaroslav@874
   172
     * 
jaroslav@874
   173
     * @param classes the classes to add to the compilation
jaroslav@1491
   174
     * @return new instance of the Bck2Brwsr compiler which inherits
jaroslav@1491
   175
     * all values from <code>this</code>
jaroslav@874
   176
     */
jaroslav@874
   177
    public Bck2Brwsr addRootClasses(String... classes) {
jaroslav@874
   178
        if (classes.length == 0) {
jaroslav@874
   179
            return this;
jaroslav@1583
   180
        } 
jaroslav@1583
   181
        return addExported(classes).addClasses(classes);
jaroslav@874
   182
    }
jaroslav@874
   183
    
jaroslav@1491
   184
    /** Adds additional classes 
jaroslav@1491
   185
     * to the list of those that should be included in the generated
jaroslav@1491
   186
     * JavaScript file. These classes are guaranteed to be present,
jaroslav@1491
   187
     * but they may not be accessible through their fully qualified
jaroslav@1491
   188
     * name.
jaroslav@1491
   189
     * 
jaroslav@1491
   190
     * @param classes the classes to add to the compilation
jaroslav@1491
   191
     * @return new instance of the Bck2Brwsr compiler which inherits
jaroslav@1491
   192
     * all values from <code>this</code>
jaroslav@1491
   193
     * @since 0.9
jaroslav@1491
   194
     */
jaroslav@1491
   195
    public Bck2Brwsr addClasses(String... classes) {
jaroslav@1491
   196
        if (classes.length == 0) {
jaroslav@1491
   197
            return this;
jaroslav@1491
   198
        } else {
jaroslav@1583
   199
            return new Bck2Brwsr(level, exported, 
jaroslav@1583
   200
                this.classes.addAndNew(classes), resources, res,
jaroslav@1812
   201
                extension, classpath, flow);
jaroslav@1491
   202
        }
jaroslav@1491
   203
    }
jaroslav@1491
   204
    
jaroslav@1493
   205
    /** These resources should be made available in the compiled file in
jaroslav@1493
   206
     * binary form. These resources can then be loaded
jaroslav@1493
   207
     * by {@link ClassLoader#getResource(java.lang.String)} and similar 
jaroslav@1493
   208
     * methods.
jaroslav@1493
   209
     * 
jaroslav@1493
   210
     * @param resources names of the resources to be loaded by {@link Resources#get(java.lang.String)}
jaroslav@1493
   211
     * @return new instance of the Bck2Brwsr compiler which inherits
jaroslav@1493
   212
     *   all values from <code>this</code> just adds few more resource names
jaroslav@1493
   213
     *   for processing
jaroslav@1493
   214
     * @since 0.9
jaroslav@1493
   215
     */
jaroslav@1493
   216
    public Bck2Brwsr addResources(String... resources) {
jaroslav@1493
   217
        if (resources.length == 0) {
jaroslav@1493
   218
            return this;
jaroslav@1493
   219
        } else {
jaroslav@1583
   220
            return new Bck2Brwsr(level, exported, this.classes, 
jaroslav@1812
   221
                this.resources.addAndNew(resources), res, extension, classpath, flow
jaroslav@1493
   222
            );
jaroslav@874
   223
        }
jaroslav@874
   224
    }
jaroslav@874
   225
    
jaroslav@874
   226
    /** Changes the obfuscation level for the compiler by creating new instance
jaroslav@874
   227
     * which inherits all values from <code>this</code> and adjust the level
jaroslav@874
   228
     * of obfuscation.
jaroslav@874
   229
     * 
jaroslav@874
   230
     * @param level the new level of obfuscation
jaroslav@874
   231
     * @return new instance of the compiler with changed level of obfuscation
jaroslav@874
   232
     * @since 0.5
jaroslav@874
   233
     */
jaroslav@874
   234
    public Bck2Brwsr obfuscation(ObfuscationLevel level) {
jaroslav@1812
   235
        return new Bck2Brwsr(level, exported, classes, resources, res, extension, classpath, flow);
jaroslav@874
   236
    }
jaroslav@874
   237
    
jaroslav@874
   238
    /** A way to change the provider of additional resources (classes) for the 
jaroslav@874
   239
     * compiler. 
jaroslav@874
   240
     * 
jaroslav@874
   241
     * @param res the implementation of resources provider
jaroslav@874
   242
     * @return new instance of the compiler with all values remaining the same, just 
jaroslav@874
   243
     *   with different resources provider
jaroslav@874
   244
     * @since 0.5
jaroslav@874
   245
     */
jaroslav@874
   246
    public Bck2Brwsr resources(Resources res) {
jaroslav@1604
   247
        return new Bck2Brwsr(
jaroslav@1604
   248
            level, exported, classes, resources, 
jaroslav@1812
   249
            res, extension, classpath, flow
jaroslav@1604
   250
        );
lubomir@1029
   251
    }
lubomir@1029
   252
jaroslav@1491
   253
    /** Should one generate a library? By default the system generates
jaroslav@1491
   254
     * all transitive classes needed by the the transitive closure of
jaroslav@1491
   255
     * {@link #addRootClasses(java.lang.String...)} and {@link #addClasses(java.lang.String...)}.
jaroslav@1491
   256
     * By turning on the library mode, only classes explicitly listed
jaroslav@1491
   257
     * will be included in the archive. The others will be referenced
jaroslav@1491
   258
     * as external ones.
jaroslav@1604
   259
     * <p>
jaroslav@1604
   260
     * A library archive may specify its <em>classpath</em> - e.g. link to
jaroslav@1604
   261
     * other libraries that should also be included in the application. 
jaroslav@1604
   262
     * One can specify the list of libraries as vararg to this method.
jaroslav@1604
   263
     * These are relative URL with respect to location of this library.
jaroslav@1604
   264
     * The runtime system then prefers seek for ".js" suffix of the library
jaroslav@1604
   265
     * and only then seeks for the classical ".jar" path.
jaroslav@1491
   266
     * 
jaroslav@1604
   267
     * @param classpath the array of JARs that are referenced by this library -
jaroslav@1604
   268
     *   by default gets turned into 
jaroslav@1491
   269
     * @return new instance of the compiler with library flag changed
jaroslav@1491
   270
     * @since 0.9
jaroslav@1491
   271
     */
jaroslav@1604
   272
    public Bck2Brwsr library(String... classpath) {
jaroslav@1604
   273
        return new Bck2Brwsr(
jaroslav@1604
   274
            level, exported, classes, 
jaroslav@1604
   275
            resources, res, true, 
jaroslav@1812
   276
            StringArray.asList(classpath), flow
jaroslav@1604
   277
        );
jaroslav@874
   278
    }
jaroslav@1584
   279
    
jaroslav@1783
   280
    /** Turns on the standalone mode. E.g. does the opposite of
jaroslav@1783
   281
     * calling {@link #library(java.lang.String...)},
jaroslav@1584
   282
     * but also allows to specify whether the <em>Bck2Brwsr VM</em> should
jaroslav@1584
   283
     * be included at all. If not, only the skeleton of the launcher is
jaroslav@1584
   284
     * generated without any additional VM classes referenced.
jaroslav@1584
   285
     * 
jaroslav@1584
   286
     * @param includeVM should the VM be compiled in, or left out
jaroslav@1584
   287
     * @return new instance of the compiler with standalone mode on
jaroslav@1584
   288
     * @since 0.9
jaroslav@1584
   289
     */
jaroslav@1584
   290
    public Bck2Brwsr standalone(boolean includeVM) {
jaroslav@1604
   291
        return new Bck2Brwsr(
jaroslav@1604
   292
            level, exported, classes, resources, 
jaroslav@1812
   293
            res, includeVM ? false : null, null, flow
jaroslav@1604
   294
        );
jaroslav@1584
   295
    }
jaroslav@874
   296
jaroslav@874
   297
    /** A way to change the provider of additional resources (classes) for the 
jaroslav@874
   298
     * compiler by specifying classloader to use for loading them.
jaroslav@874
   299
     * 
jaroslav@874
   300
     * @param loader class loader to load the resources from
jaroslav@874
   301
     * @return new instance of the compiler with all values being the same, just 
jaroslav@874
   302
     *   different resources provider
jaroslav@874
   303
     * @since 0.5
jaroslav@874
   304
     */
jaroslav@874
   305
    public Bck2Brwsr resources(final ClassLoader loader) {
jaroslav@1365
   306
        return resources(loader, false);
jaroslav@1365
   307
    }
jaroslav@1365
   308
jaroslav@1365
   309
    /** A way to change the provider of additional resources (classes) for the 
jaroslav@1365
   310
     * compiler by specifying classloader to use for loading them.
jaroslav@1365
   311
     * 
jaroslav@1365
   312
     * @param loader class loader to load the resources from
jaroslav@1365
   313
     * @param ignoreBootClassPath <code>true</code> if classes loaded
jaroslav@1365
   314
     *    from <code>rt.jar</code> 
jaroslav@1365
   315
     * @return new instance of the compiler with all values being the same, just 
jaroslav@1365
   316
     *   different resources provider
jaroslav@1365
   317
     * @since 0.9
jaroslav@1365
   318
     */
jaroslav@1365
   319
    public Bck2Brwsr resources(final ClassLoader loader, boolean ignoreBootClassPath) {
jaroslav@1365
   320
        return resources(new LdrRsrcs(loader, ignoreBootClassPath));
jaroslav@874
   321
    }
jaroslav@874
   322
    
jaroslav@1812
   323
    /** A way to register flow analyzer. Such analyzer can optimize the
jaroslav@1812
   324
     * representation of cycles inside of method bodies.
jaroslav@1812
   325
     * 
jaroslav@1812
   326
     * @param flow the analyzer to be consulted with each method body
jaroslav@1812
   327
     * @return new instance of the compiler with all values being the same, just 
jaroslav@1812
   328
     *   different flow analyzer
jaroslav@1812
   329
     * @since 0.15
jaroslav@1812
   330
     */
jaroslav@1812
   331
    public Bck2Brwsr flowAnalyzer(Flow.Analyzer flow) {
jaroslav@1812
   332
        return new Bck2Brwsr(
jaroslav@1812
   333
            level, exported, classes, resources, res, 
jaroslav@1812
   334
            extension, classpath, flow
jaroslav@1812
   335
        );
jaroslav@1812
   336
    }
jaroslav@1812
   337
    
jaroslav@874
   338
    /** Generates virtual machine based on previous configuration of the 
jaroslav@874
   339
     * compiler.
lubomir@849
   340
     * 
lubomir@849
   341
     * @param out the output to write the generated JavaScript to
jaroslav@1783
   342
     * @throws IOException I/O exception can be thrown when something goes wrong
lubomir@860
   343
     * @since 0.5
lubomir@849
   344
     */
jaroslav@874
   345
    public void generate(Appendable out) throws IOException {
jaroslav@874
   346
        if (level != ObfuscationLevel.NONE) {
lubomir@849
   347
            try {
jaroslav@1493
   348
                ClosureWrapper.produceTo(out, level, this);
lubomir@849
   349
                return;
lubomir@849
   350
            } catch (IOException ex) {
lubomir@849
   351
                throw ex;
lubomir@849
   352
            } catch (Throwable ex) {
lubomir@849
   353
                out.append("/* Failed to obfuscate: " + ex.getMessage()
lubomir@849
   354
                               + " */\n");
lubomir@849
   355
            }
jaroslav@750
   356
        }
lubomir@849
   357
jaroslav@1493
   358
        VM.compile(out, this);
jaroslav@298
   359
    }
jaroslav@298
   360
    
jaroslav@1493
   361
    //
jaroslav@1493
   362
    // Internal getters
jaroslav@1493
   363
    // 
jaroslav@1493
   364
    
jaroslav@1493
   365
    Resources getResources() {
jaroslav@1513
   366
        return res != null ? res : new LdrRsrcs(Bck2Brwsr.class.getClassLoader(), false);
jaroslav@1493
   367
    }
jaroslav@1493
   368
    
jaroslav@1495
   369
    StringArray allResources() {
jaroslav@1495
   370
        return resources;
jaroslav@1495
   371
    }
jaroslav@1495
   372
jaroslav@1583
   373
    StringArray classes() {
jaroslav@1583
   374
        return classes;
jaroslav@1583
   375
    }
jaroslav@1583
   376
jaroslav@1583
   377
    StringArray exported() {
jaroslav@1583
   378
        return exported;
jaroslav@1493
   379
    }
jaroslav@1493
   380
    
jaroslav@1493
   381
    boolean isExtension() {
jaroslav@1584
   382
        return Boolean.TRUE.equals(extension);
jaroslav@1584
   383
    }
jaroslav@1584
   384
    
jaroslav@1584
   385
    boolean includeVM() {
jaroslav@1584
   386
        return extension != null;
jaroslav@874
   387
    }
jaroslav@1604
   388
    
jaroslav@1604
   389
    StringArray classpath() {
jaroslav@1604
   390
        return classpath;
jaroslav@1604
   391
    }
jaroslav@1812
   392
    
jaroslav@1812
   393
    Flow.Analyzer flow() {
jaroslav@1812
   394
        return flow;
jaroslav@1812
   395
    }
lubomir@1086
   396
jaroslav@298
   397
    /** Provider of resources (classes and other files). The 
jaroslav@298
   398
     * {@link #generate(java.lang.Appendable, org.apidesign.vm4brwsr.Bck2Brwsr.Resources, java.lang.String[]) 
jaroslav@298
   399
     * generator method} will call back here for all classes needed during
jaroslav@298
   400
     * translation to JavaScript.
jaroslav@298
   401
     */
jaroslav@298
   402
    public interface Resources {
jaroslav@298
   403
        /** Loads given resource (class or other file like image). The 
jaroslav@298
   404
         * resource name to load bytes for the {@link String} class
jaroslav@298
   405
         * would be <code>"java/lang/String.class"</code>.
jaroslav@298
   406
         * 
jaroslav@298
   407
         * @param resource path to resource to load
jaroslav@298
   408
         * @return the input stream for the resource 
jaroslav@298
   409
         * @throws IOException can be thrown if the loading fails on some error
jaroslav@298
   410
         *   or the file cannot be found
jaroslav@298
   411
         */
jaroslav@298
   412
        public InputStream get(String resource) throws IOException;
jaroslav@298
   413
    }
jaroslav@1812
   414
jaroslav@1812
   415
    /** Represents control flow inside single method.
jaroslav@1812
   416
     * Passed into {@link Analyzer#analyze(byte[], org.apidesign.vm4brwsr.Bck2Brwsr.Flow)}
jaroslav@1812
   417
     * method that can be registed via {@link Bck2Brwsr#flowAnalyzer(org.apidesign.vm4brwsr.Bck2Brwsr.Flow.Analyzer)}
jaroslav@1812
   418
     * method.
jaroslav@1812
   419
     * 
jaroslav@1812
   420
     * @since 0.15
jaroslav@1812
   421
     */
jaroslav@1812
   422
    public static final class Flow {
jaroslav@1812
   423
        private final byte[] byteCode;
jaroslav@1812
   424
        Flow(byte[] byteCode) {
jaroslav@1812
   425
            this.byteCode = byteCode;
jaroslav@1812
   426
        }
jaroslav@1812
   427
        
jaroslav@1812
   428
        /** Access to bytecode of the method to analyse.
jaroslav@1812
   429
         * 
jaroslav@1812
   430
         * @return unmodifiable bytecode of the instructions in the method body
jaroslav@1812
   431
         */
jaroslav@1812
   432
        public byte[] getMethodByteCode() {
jaroslav@1812
   433
            return byteCode;
jaroslav@1812
   434
        }
jaroslav@1812
   435
        
jaroslav@1812
   436
        public void registerCycle(int offset) {
jaroslav@1812
   437
        }
jaroslav@1812
   438
        
jaroslav@1812
   439
        public void registerIf(int offset) {
jaroslav@1812
   440
        }
jaroslav@1812
   441
        
jaroslav@1812
   442
        /** Provider of advanced analysis of the code flow inside of
jaroslav@1812
   443
         * method bodies. Register via {@link Bck2Brwsr#flowAnalyzer(org.apidesign.vm4brwsr.Bck2Brwsr.Flow.Analyzer)}
jaroslav@1812
   444
         * when constructing the {@link Bck2Brwsr#newCompiler() compiler}.
jaroslav@1812
   445
         * 
jaroslav@1812
   446
         * @since 0.15
jaroslav@1812
   447
         */
jaroslav@1812
   448
        public interface Analyzer {
jaroslav@1812
   449
            /** Called to analyze method bodies and offer better control flow.
jaroslav@1812
   450
             * 
jaroslav@1812
   451
             * 
jaroslav@1812
   452
             * @param request flow computation request and also a
jaroslav@1812
   453
             *    callback interface with methods to define the flow
jaroslav@1812
   454
             * @return <code>true</code> if the analysis was successful,
jaroslav@1812
   455
             *   <code>false</code> otherwise
jaroslav@1812
   456
             */
jaroslav@1812
   457
            public boolean analyze(Flow request);
jaroslav@1812
   458
        }
jaroslav@1812
   459
    }
jaroslav@1812
   460
    
jaroslav@298
   461
}