1.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java Sun Nov 11 13:23:52 2012 +0100
1.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java Fri Nov 16 07:36:32 2012 +0100
1.3 @@ -20,49 +20,33 @@
1.4 import java.io.IOException;
1.5 import java.io.InputStream;
1.6 import java.net.URL;
1.7 -import java.util.ArrayList;
1.8 -import java.util.Arrays;
1.9 -import java.util.Collections;
1.10 import java.util.Enumeration;
1.11 -import java.util.HashMap;
1.12 -import java.util.Iterator;
1.13 -import java.util.LinkedHashSet;
1.14 -import java.util.LinkedList;
1.15 -import java.util.List;
1.16 -import java.util.Map;
1.17
1.18 /** Generator of JavaScript from bytecode of classes on classpath of the VM.
1.19 *
1.20 * @author Jaroslav Tulach <jtulach@netbeans.org>
1.21 */
1.22 -final class GenJS {
1.23 - private GenJS() {}
1.24 +final class GenJS extends ByteCodeToJavaScript {
1.25 + public GenJS(Appendable out) {
1.26 + super(out);
1.27 + }
1.28
1.29 static void compile(Appendable out, String... names) throws IOException {
1.30 - compile(out, Arrays.asList(names));
1.31 + compile(out, StringArray.asList(names));
1.32 }
1.33 - static void compile(Appendable out, List<String> names) throws IOException {
1.34 + static void compile(Appendable out, StringArray names) throws IOException {
1.35 compile(GenJS.class.getClassLoader(), out, names);
1.36 }
1.37 - static void compile(ClassLoader l, Appendable out, List<String> names) throws IOException {
1.38 - final Map<String,String> processed = new HashMap<String, String>();
1.39 - for (String baseClass : names) {
1.40 - LinkedHashSet<String> toProcess = new LinkedHashSet<String>() {
1.41 - @Override
1.42 - public boolean add(String e) {
1.43 - if (processed.containsKey(e)) {
1.44 - return false;
1.45 - }
1.46 - return super.add(e);
1.47 - }
1.48 - };
1.49 - toProcess.add(baseClass);
1.50 + static void compile(ClassLoader l, Appendable out, StringArray names) throws IOException {
1.51 + StringArray processed = new StringArray();
1.52 + StringArray initCode = new StringArray();
1.53 + for (String baseClass : names.toArray()) {
1.54 + GenJS js = new GenJS(out);
1.55 + js.references.add(baseClass);
1.56 for (;;) {
1.57 String name = null;
1.58 - Iterator<String> it = toProcess.iterator();
1.59 - while (it.hasNext() && name == null) {
1.60 - String n = it.next();
1.61 - if (processed.get(n) != null) {
1.62 + for (String n : js.references.toArray()) {
1.63 + if (processed.contains(n)) {
1.64 continue;
1.65 }
1.66 name = n;
1.67 @@ -70,18 +54,14 @@
1.68 if (name == null) {
1.69 break;
1.70 }
1.71 - if (name.startsWith("sun/")) {
1.72 - processed.put(name, "");
1.73 - continue;
1.74 - }
1.75 InputStream is = loadClass(l, name);
1.76 if (is == null) {
1.77 throw new IOException("Can't find class " + name);
1.78 }
1.79 - LinkedList<String> scripts = new LinkedList<String>();
1.80 try {
1.81 - String initCode = ByteCodeToJavaScript.compile(is, out, toProcess, scripts);
1.82 - processed.put(name, initCode == null ? "" : initCode);
1.83 + String ic = js.compile(is);
1.84 + processed.add(name);
1.85 + initCode.add(ic == null ? "" : ic);
1.86 } catch (RuntimeException ex) {
1.87 if (out instanceof CharSequence) {
1.88 CharSequence seq = (CharSequence)out;
1.89 @@ -100,7 +80,7 @@
1.90 );
1.91 }
1.92 }
1.93 - for (String resource : scripts) {
1.94 + for (String resource : js.scripts.toArray()) {
1.95 while (resource.startsWith("/")) {
1.96 resource = resource.substring(1);
1.97 }
1.98 @@ -112,14 +92,14 @@
1.99 }
1.100 }
1.101
1.102 - List<String> toInit = new ArrayList<String>(toProcess);
1.103 - Collections.reverse(toInit);
1.104 + StringArray toInit = StringArray.asList(js.references.toArray());
1.105 + toInit.reverse();
1.106
1.107 - for (String clazz : toInit) {
1.108 - String initCode = processed.get(clazz);
1.109 - if (initCode != null && !initCode.isEmpty()) {
1.110 - out.append(initCode).append("\n");
1.111 - processed.put(clazz, "");
1.112 + for (String ic : toInit.toArray()) {
1.113 + int indx = processed.indexOf(ic);
1.114 + if (indx >= 0) {
1.115 + out.append(initCode.toArray()[indx]).append("\n");
1.116 + initCode.toArray()[indx] = "";
1.117 }
1.118 }
1.119
1.120 @@ -180,6 +160,9 @@
1.121 if (u == null) {
1.122 throw new IOException("Can't find " + name);
1.123 }
1.124 + if (u.toExternalForm().contains("rt.jar!")) {
1.125 + throw new IOException("No emulation for " + u);
1.126 + }
1.127 return u.openStream();
1.128 }
1.129
1.130 @@ -188,4 +171,21 @@
1.131 compile(sb, name);
1.132 return sb.toString().toString();
1.133 }
1.134 +
1.135 + private StringArray scripts = new StringArray();
1.136 + private StringArray references = new StringArray();
1.137 +
1.138 + @Override
1.139 + protected boolean requireReference(String cn) {
1.140 + if (references.contains(cn)) {
1.141 + return false;
1.142 + }
1.143 + references.add(cn);
1.144 + return true;
1.145 + }
1.146 +
1.147 + @Override
1.148 + protected void requireScript(String resourcePath) {
1.149 + scripts.add(resourcePath);
1.150 + }
1.151 }