diff -r 000000000000 -r dcb98731b000 vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java Tue Sep 25 12:11:03 2012 +0200 @@ -0,0 +1,76 @@ +package org.apidesign.vm4brwsr; + +import java.io.BufferedWriter; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.Writer; +import java.util.Arrays; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Set; + +/** Generator of JavaScript from bytecode of classes on classpath of the VM. + * + * @author Jaroslav Tulach + */ +final class GenJS { + private GenJS() {} + + public static void main(String... args) throws IOException { + if (args.length < 2) { + System.err.println("Usage: java -cp ... -jar ... java/lang/Class org/your/App ..."); + return; + } + + Writer w = new BufferedWriter(new FileWriter(args[0])); + List classes = Arrays.asList(args).subList(1, args.length); + compile(w, classes); + w.close(); + } + + static void compile(Appendable out, String... names) throws IOException { + compile(out, Arrays.asList(names)); + } + static void compile(Appendable out, List names) throws IOException { + Set processed = new HashSet(); + LinkedList toProcess = new LinkedList(names); + for (;;) { + toProcess.removeAll(processed); + if (toProcess.isEmpty()) { + break; + } + String name = toProcess.getFirst(); + processed.add(name); + if (name.startsWith("java/") && !name.equals("java/lang/Object")) { + continue; + } + InputStream is = GenJS.class.getClassLoader().getResourceAsStream(name + ".class"); + if (is == null) { + throw new IOException("Can't find class " + name); + } + try { + ByteCodeToJavaScript.compile(is, out, toProcess); + } catch (RuntimeException ex) { + if (out instanceof CharSequence) { + CharSequence seq = (CharSequence)out; + int lastBlock = seq.length(); + while (lastBlock-- >= 0) { + if (seq.charAt(lastBlock) == '{') { + break; + } + } + throw new IOException("Error while compiling " + name + "\n" + + seq.subSequence(lastBlock + 1, seq.length()), ex + ); + } else { + throw new IOException("Error while compiling " + name + "\n" + + out, ex + ); + } + } + } + } + +}