rt/vm/src/main/java/org/apidesign/vm4brwsr/ClosureWrapper.java
author Lubomir Nerad <lubomir.nerad@oracle.com>
Wed, 13 Mar 2013 16:17:47 +0100
branchclosure
changeset 849 d95117153304
parent 841 81cea57bf4e9
child 869 151f4ccd7673
permissions -rw-r--r--
Allow to set obfuscation level for j2js
     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, ObfuscationLevel obfuscationLevel,
    41                            Bck2Brwsr.Resources res, StringArray classes) {
    42         super(
    43             generateArguments(obfuscationLevel),
    44             new PrintStream(new APS(out)), System.err
    45         );
    46         this.res = res;
    47         this.classes = classes;
    48     }
    49 
    50     @Override
    51     protected List<SourceFile> createInputs(List<String> files, boolean allowStdIn) throws FlagUsageException, IOException {
    52         if (files.size() != 1 || !"bck2brwsr-raw.js".equals(files.get(0))) {
    53             throw new IOException("Unexpected files: " + files);
    54         }
    55         return Collections.nCopies(1, SourceFile.fromGenerator("bck2brwsr-raw.js", this));
    56     }
    57 
    58     @Override
    59     public String getCode() {
    60         if (code == null) {
    61             StringBuilder sb = new StringBuilder();
    62             try {
    63                 VM.compile(res, sb, classes);
    64             } catch (IOException ex) {
    65                 code = ex.getMessage();
    66             }
    67             code = sb.toString();
    68         }
    69         return code;
    70     }
    71 
    72     private static final class APS extends OutputStream {
    73         private final Appendable out;
    74 
    75         public APS(Appendable out) {
    76             this.out = out;
    77         }
    78         @Override
    79         public void write(int b) throws IOException {
    80             out.append((char)b);
    81         }
    82     }
    83 
    84     private static String[] generateArguments(
    85             ObfuscationLevel obfuscationLevel) {
    86         String[] finalArgs = ARGS.clone();
    87         finalArgs[1] = obfuscationLevel.toString();
    88 
    89         return finalArgs;
    90     }
    91 
    92     static int produceTo(Appendable w, ObfuscationLevel obfuscationLevel, Bck2Brwsr.Resources resources, StringArray arr) throws IOException {
    93         ClosureWrapper cw = new ClosureWrapper(w, obfuscationLevel, resources,
    94                                                arr);
    95         try {
    96             return cw.doRun();
    97         } catch (FlagUsageException ex) {
    98             throw new IOException(ex);
    99         }
   100     }
   101 }