1.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java Thu Oct 11 06:15:22 2012 -0700
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,179 +0,0 @@
1.4 -package org.apidesign.vm4brwsr;
1.5 -
1.6 -import java.io.BufferedWriter;
1.7 -import java.io.FileWriter;
1.8 -import java.io.IOException;
1.9 -import java.io.InputStream;
1.10 -import java.io.Writer;
1.11 -import java.net.URL;
1.12 -import java.util.Arrays;
1.13 -import java.util.Collections;
1.14 -import java.util.Enumeration;
1.15 -import java.util.HashMap;
1.16 -import java.util.Iterator;
1.17 -import java.util.LinkedList;
1.18 -import java.util.List;
1.19 -import java.util.Map;
1.20 -
1.21 -/** Generator of JavaScript from bytecode of classes on classpath of the VM.
1.22 - *
1.23 - * @author Jaroslav Tulach <jtulach@netbeans.org>
1.24 - */
1.25 -final class GenJS {
1.26 - private GenJS() {}
1.27 -
1.28 - public static void main(String... args) throws IOException {
1.29 - if (args.length < 2) {
1.30 - System.err.println("Usage: java -cp ... -jar ... <file_to_generate_js_code_to> java/lang/Class org/your/App ...");
1.31 - return;
1.32 - }
1.33 -
1.34 - Writer w = new BufferedWriter(new FileWriter(args[0]));
1.35 - List<String> classes = Arrays.asList(args).subList(1, args.length);
1.36 - compile(w, classes);
1.37 - w.close();
1.38 - }
1.39 -
1.40 - static void compile(Appendable out, String... names) throws IOException {
1.41 - compile(out, Arrays.asList(names));
1.42 - }
1.43 - static void compile(Appendable out, List<String> names) throws IOException {
1.44 - for (String baseClass : names) {
1.45 - Map<String,String> processed = new HashMap<String, String>();
1.46 - LinkedList<String> toProcess = new LinkedList<String>();
1.47 - toProcess.add(baseClass);
1.48 - for (;;) {
1.49 - String name = null;
1.50 - Iterator<String> it = toProcess.iterator();
1.51 - while (it.hasNext() && name == null) {
1.52 - String n = it.next();
1.53 - if (processed.get(n) != null) {
1.54 - continue;
1.55 - }
1.56 - name = n;
1.57 - }
1.58 - if (name == null) {
1.59 - break;
1.60 - }
1.61 - if (name.startsWith("java/")
1.62 - && !name.equals("java/lang/Object")
1.63 - && !name.equals("java/lang/Class")
1.64 - && !name.equals("java/lang/Number")
1.65 - && !name.equals("java/lang/Integer")
1.66 - && !name.equals("java/lang/Throwable")
1.67 - && !name.equals("java/lang/Exception")
1.68 - && !name.equals("java/lang/RuntimeException")
1.69 - && !name.equals("java/lang/UnsupportedOperationException")
1.70 - && !name.equals("java/lang/String")
1.71 - && !name.equals("java/lang/String$CaseInsensitiveComparator")
1.72 - && !name.equals("java/lang/StringBuilder")
1.73 - && !name.equals("java/lang/AbstractStringBuilder")
1.74 - ) {
1.75 - processed.put(name, "");
1.76 - continue;
1.77 - }
1.78 - InputStream is = loadClass(name);
1.79 - if (is == null) {
1.80 - throw new IOException("Can't find class " + name);
1.81 - }
1.82 - LinkedList<String> scripts = new LinkedList<String>();
1.83 - try {
1.84 - String initCode = ByteCodeToJavaScript.compile(is, out, toProcess, scripts);
1.85 - processed.put(name, initCode == null ? "" : initCode);
1.86 - } catch (RuntimeException ex) {
1.87 - if (out instanceof CharSequence) {
1.88 - CharSequence seq = (CharSequence)out;
1.89 - int lastBlock = seq.length();
1.90 - while (lastBlock-- >= 0) {
1.91 - if (seq.charAt(lastBlock) == '{') {
1.92 - break;
1.93 - }
1.94 - }
1.95 - throw new IOException("Error while compiling " + name + "\n"
1.96 - + seq.subSequence(lastBlock + 1, seq.length()), ex
1.97 - );
1.98 - } else {
1.99 - throw new IOException("Error while compiling " + name + "\n"
1.100 - + out, ex
1.101 - );
1.102 - }
1.103 - }
1.104 - for (String resource : scripts) {
1.105 - InputStream emul = GenJS.class.getResourceAsStream(resource);
1.106 - if (emul == null) {
1.107 - throw new IOException("Can't find " + resource);
1.108 - }
1.109 - readResource(emul, out);
1.110 - }
1.111 - }
1.112 -
1.113 - Collections.reverse(toProcess);
1.114 -
1.115 - for (String clazz : toProcess) {
1.116 - String initCode = processed.remove(clazz);
1.117 - if (initCode != null) {
1.118 - out.append(initCode).append("\n");
1.119 - }
1.120 - }
1.121 - }
1.122 -
1.123 - }
1.124 - private static void readResource(InputStream emul, Appendable out) throws IOException {
1.125 - try {
1.126 - int state = 0;
1.127 - for (;;) {
1.128 - int ch = emul.read();
1.129 - if (ch == -1) {
1.130 - break;
1.131 - }
1.132 - if (ch < 0 || ch > 255) {
1.133 - throw new IOException("Invalid char in emulation " + ch);
1.134 - }
1.135 - switch (state) {
1.136 - case 0:
1.137 - if (ch == '/') {
1.138 - state = 1;
1.139 - } else {
1.140 - out.append((char)ch);
1.141 - }
1.142 - break;
1.143 - case 1:
1.144 - if (ch == '*') {
1.145 - state = 2;
1.146 - } else {
1.147 - out.append('/').append((char)ch);
1.148 - state = 0;
1.149 - }
1.150 - break;
1.151 - case 2:
1.152 - if (ch == '*') {
1.153 - state = 3;
1.154 - }
1.155 - break;
1.156 - case 3:
1.157 - if (ch == '/') {
1.158 - state = 0;
1.159 - } else {
1.160 - state = 2;
1.161 - }
1.162 - break;
1.163 - }
1.164 - }
1.165 - } finally {
1.166 - emul.close();
1.167 - }
1.168 - }
1.169 -
1.170 - private static InputStream loadClass(String name) throws IOException {
1.171 - Enumeration<URL> en = ClassLoader.getSystemClassLoader().getResources(name + ".class");
1.172 - URL u = null;
1.173 - while (en.hasMoreElements()) {
1.174 - u = en.nextElement();
1.175 - }
1.176 - if (u == null) {
1.177 - throw new IOException("Can't find " + name);
1.178 - }
1.179 - return u.openStream();
1.180 - }
1.181 -
1.182 -}