rt/vm/src/main/java/org/apidesign/vm4brwsr/ClosureWrapper.java
branchclosure
changeset 841 81cea57bf4e9
parent 750 6ac37d80ecb7
child 849 d95117153304
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ClosureWrapper.java	Tue Mar 12 13:54:26 2013 +0100
     1.3 @@ -0,0 +1,90 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package org.apidesign.vm4brwsr;
    1.22 +
    1.23 +import com.google.javascript.jscomp.CommandLineRunner;
    1.24 +import com.google.javascript.jscomp.SourceFile;
    1.25 +import java.io.IOException;
    1.26 +import java.io.OutputStream;
    1.27 +import java.io.PrintStream;
    1.28 +import java.util.Collections;
    1.29 +import java.util.List;
    1.30 +import org.apidesign.bck2brwsr.core.ExtraJavaScript;
    1.31 +
    1.32 +/**
    1.33 + *
    1.34 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.35 + */
    1.36 +@ExtraJavaScript(processByteCode = false, resource="")
    1.37 +final class ClosureWrapper extends CommandLineRunner implements SourceFile.Generator {
    1.38 +    private static final String[] ARGS = { "--compilation_level", "SIMPLE_OPTIMIZATIONS", "--js", "bck2brwsr-raw.js" };
    1.39 +
    1.40 +    private String code;
    1.41 +    private final Bck2Brwsr.Resources res;
    1.42 +    private final StringArray classes;
    1.43 +    private ClosureWrapper(Appendable out, Bck2Brwsr.Resources res, StringArray classes) {
    1.44 +        super(
    1.45 +            ARGS, 
    1.46 +            new PrintStream(new APS(out)), System.err
    1.47 +        );
    1.48 +        this.res = res;
    1.49 +        this.classes = classes;
    1.50 +    }
    1.51 +
    1.52 +    @Override
    1.53 +    protected List<SourceFile> createInputs(List<String> files, boolean allowStdIn) throws FlagUsageException, IOException {
    1.54 +        if (files.size() != 1 || !"bck2brwsr-raw.js".equals(files.get(0))) {
    1.55 +            throw new IOException("Unexpected files: " + files);
    1.56 +        }
    1.57 +        return Collections.nCopies(1, SourceFile.fromGenerator("bck2brwsr-raw.js", this));
    1.58 +    }
    1.59 +
    1.60 +    @Override
    1.61 +    public String getCode() {
    1.62 +        if (code == null) {
    1.63 +            StringBuilder sb = new StringBuilder();
    1.64 +            try {
    1.65 +                VM.compile(res, sb, classes);
    1.66 +            } catch (IOException ex) {
    1.67 +                code = ex.getMessage();
    1.68 +            }
    1.69 +            code = sb.toString();
    1.70 +        }
    1.71 +        return code;
    1.72 +    }
    1.73 +    
    1.74 +    private static final class APS extends OutputStream {
    1.75 +        private final Appendable out;
    1.76 +
    1.77 +        public APS(Appendable out) {
    1.78 +            this.out = out;
    1.79 +        }
    1.80 +        @Override
    1.81 +        public void write(int b) throws IOException {
    1.82 +            out.append((char)b);
    1.83 +        }
    1.84 +    }
    1.85 +    static int produceTo(Appendable w, Bck2Brwsr.Resources resources, StringArray arr) throws IOException {
    1.86 +        ClosureWrapper cw = new ClosureWrapper(w, resources, arr);
    1.87 +        try {
    1.88 +            return cw.doRun();
    1.89 +        } catch (FlagUsageException ex) {
    1.90 +            throw new IOException(ex);
    1.91 +        }
    1.92 +    }
    1.93 +}