rt/vm/src/main/java/org/apidesign/vm4brwsr/ClosureWrapper.java
author Lubomir Nerad <lubomir.nerad@oracle.com>
Tue, 12 Mar 2013 13:54:26 +0100
branchclosure
changeset 841 81cea57bf4e9
parent 750 vm/src/main/java/org/apidesign/vm4brwsr/ClosureWrapper.java@6ac37d80ecb7
child 849 d95117153304
permissions -rw-r--r--
Merge with trunk
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.vm4brwsr;
    19 
    20 import com.google.javascript.jscomp.CommandLineRunner;
    21 import com.google.javascript.jscomp.SourceFile;
    22 import java.io.IOException;
    23 import java.io.OutputStream;
    24 import java.io.PrintStream;
    25 import java.util.Collections;
    26 import java.util.List;
    27 import org.apidesign.bck2brwsr.core.ExtraJavaScript;
    28 
    29 /**
    30  *
    31  * @author Jaroslav Tulach <jtulach@netbeans.org>
    32  */
    33 @ExtraJavaScript(processByteCode = false, resource="")
    34 final class ClosureWrapper extends CommandLineRunner implements SourceFile.Generator {
    35     private static final String[] ARGS = { "--compilation_level", "SIMPLE_OPTIMIZATIONS", "--js", "bck2brwsr-raw.js" };
    36 
    37     private String code;
    38     private final Bck2Brwsr.Resources res;
    39     private final StringArray classes;
    40     private ClosureWrapper(Appendable out, Bck2Brwsr.Resources res, StringArray classes) {
    41         super(
    42             ARGS, 
    43             new PrintStream(new APS(out)), System.err
    44         );
    45         this.res = res;
    46         this.classes = classes;
    47     }
    48 
    49     @Override
    50     protected List<SourceFile> createInputs(List<String> files, boolean allowStdIn) throws FlagUsageException, IOException {
    51         if (files.size() != 1 || !"bck2brwsr-raw.js".equals(files.get(0))) {
    52             throw new IOException("Unexpected files: " + files);
    53         }
    54         return Collections.nCopies(1, SourceFile.fromGenerator("bck2brwsr-raw.js", this));
    55     }
    56 
    57     @Override
    58     public String getCode() {
    59         if (code == null) {
    60             StringBuilder sb = new StringBuilder();
    61             try {
    62                 VM.compile(res, sb, classes);
    63             } catch (IOException ex) {
    64                 code = ex.getMessage();
    65             }
    66             code = sb.toString();
    67         }
    68         return code;
    69     }
    70     
    71     private static final class APS extends OutputStream {
    72         private final Appendable out;
    73 
    74         public APS(Appendable out) {
    75             this.out = out;
    76         }
    77         @Override
    78         public void write(int b) throws IOException {
    79             out.append((char)b);
    80         }
    81     }
    82     static int produceTo(Appendable w, Bck2Brwsr.Resources resources, StringArray arr) throws IOException {
    83         ClosureWrapper cw = new ClosureWrapper(w, resources, arr);
    84         try {
    85             return cw.doRun();
    86         } catch (FlagUsageException ex) {
    87             throw new IOException(ex);
    88         }
    89     }
    90 }