# HG changeset patch # User Jaroslav Tulach # Date 1348567863 -7200 # Node ID dcb98731b000c2bd91dc5aba0bf3d8ca925e8eeb # Parent 81ad7a739fed9657c30a8128153df8c8dbfc8c02 Main method to compile some classes diff -r 81ad7a739fed -r dcb98731b000 vm/pom.xml --- a/vm/pom.xml Tue Sep 25 09:55:34 2012 +0200 +++ b/vm/pom.xml Tue Sep 25 12:11:03 2012 +0200 @@ -45,18 +45,29 @@ scm:hg:http://source.apidesign.org/hg/bck2brwsr http://source.apidesign.org/hg/bck2brwsr - - - - com.mycila.maven-license-plugin - maven-license-plugin - 1.9.0 - -
src/header.txt
-
-
-
-
+ + + + org.apache.maven.plugins + maven-jar-plugin + + + + org.apidesign.vm4brwsr.GenJS + + + + + + com.mycila.maven-license-plugin + maven-license-plugin + 1.9.0 + +
src/header.txt
+
+
+
+
org.testng diff -r 81ad7a739fed -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 + ); + } + } + } + } + +} diff -r 81ad7a739fed -r dcb98731b000 vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java --- a/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java Tue Sep 25 09:55:34 2012 +0200 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java Tue Sep 25 12:11:03 2012 +0200 @@ -17,15 +17,7 @@ */ package org.apidesign.vm4brwsr; -import org.apidesign.vm4brwsr.ByteCodeToJavaScript; import java.io.IOException; -import java.io.InputStream; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.Set; -import java.util.TreeSet; import javax.script.Invocable; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; @@ -162,32 +154,7 @@ if (sb == null) { sb = new StringBuilder(); } - Set processed = new HashSet(); - - LinkedList toProcess = new LinkedList(Arrays.asList(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 = StaticMethodTest.class.getClassLoader().getResourceAsStream(name + ".class"); - assertNotNull(is, "Class file found"); - try { - ByteCodeToJavaScript.compile(is, sb, toProcess); - } catch (RuntimeException ex) { - int lastBlock = sb.lastIndexOf("{"); - throw new IllegalStateException( - "Error while compiling " + name + "\n" + - sb.substring(0, sb.length()), - ex - ); - } - } + GenJS.compile(sb, names); ScriptEngineManager sem = new ScriptEngineManager(); ScriptEngine js = sem.getEngineByExtension("js"); try {