1.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java Mon Dec 10 12:03:22 2012 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,198 +0,0 @@
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 java.io.IOException;
1.24 -import java.io.InputStream;
1.25 -import java.net.URL;
1.26 -import java.util.Enumeration;
1.27 -
1.28 -/** Generator of JavaScript from bytecode of classes on classpath of the VM.
1.29 - *
1.30 - * @author Jaroslav Tulach <jtulach@netbeans.org>
1.31 - */
1.32 -class GenJS extends ByteCodeToJavaScript {
1.33 - public GenJS(Appendable out) {
1.34 - super(out);
1.35 - }
1.36 -
1.37 - static void compile(Appendable out, String... names) throws IOException {
1.38 - compile(out, StringArray.asList(names));
1.39 - }
1.40 - static void compile(ClassLoader l, Appendable out, String... names) throws IOException {
1.41 - compile(l, out, StringArray.asList(names));
1.42 - }
1.43 - static void compile(Appendable out, StringArray names) throws IOException {
1.44 - compile(GenJS.class.getClassLoader(), out, names);
1.45 - }
1.46 - static void compile(ClassLoader l, Appendable out, StringArray names) throws IOException {
1.47 - new GenJS(out).doCompile(l, names);
1.48 - }
1.49 - protected void doCompile(ClassLoader l, StringArray names) throws IOException {
1.50 - StringArray processed = new StringArray();
1.51 - StringArray initCode = new StringArray();
1.52 - for (String baseClass : names.toArray()) {
1.53 - references.add(baseClass);
1.54 - for (;;) {
1.55 - String name = null;
1.56 - for (String n : references.toArray()) {
1.57 - if (processed.contains(n)) {
1.58 - continue;
1.59 - }
1.60 - name = n;
1.61 - }
1.62 - if (name == null) {
1.63 - break;
1.64 - }
1.65 - InputStream is = loadClass(l, name);
1.66 - if (is == null) {
1.67 - throw new IOException("Can't find class " + name);
1.68 - }
1.69 - try {
1.70 - String ic = compile(is);
1.71 - processed.add(name);
1.72 - initCode.add(ic == null ? "" : ic);
1.73 - } catch (RuntimeException ex) {
1.74 - if (out instanceof CharSequence) {
1.75 - CharSequence seq = (CharSequence)out;
1.76 - int lastBlock = seq.length();
1.77 - while (lastBlock-- > 0) {
1.78 - if (seq.charAt(lastBlock) == '{') {
1.79 - break;
1.80 - }
1.81 - }
1.82 - throw new IOException("Error while compiling " + name + "\n"
1.83 - + seq.subSequence(lastBlock + 1, seq.length()), ex
1.84 - );
1.85 - } else {
1.86 - throw new IOException("Error while compiling " + name + "\n"
1.87 - + out, ex
1.88 - );
1.89 - }
1.90 - }
1.91 - }
1.92 -
1.93 - for (String resource : scripts.toArray()) {
1.94 - while (resource.startsWith("/")) {
1.95 - resource = resource.substring(1);
1.96 - }
1.97 - InputStream emul = l.getResourceAsStream(resource);
1.98 - if (emul == null) {
1.99 - throw new IOException("Can't find " + resource);
1.100 - }
1.101 - readResource(emul, out);
1.102 - }
1.103 - scripts = new StringArray();
1.104 -
1.105 - StringArray toInit = StringArray.asList(references.toArray());
1.106 - toInit.reverse();
1.107 -
1.108 - for (String ic : toInit.toArray()) {
1.109 - int indx = processed.indexOf(ic);
1.110 - if (indx >= 0) {
1.111 - out.append(initCode.toArray()[indx]).append("\n");
1.112 - initCode.toArray()[indx] = "";
1.113 - }
1.114 - }
1.115 -
1.116 - }
1.117 - }
1.118 - private static void readResource(InputStream emul, Appendable out) throws IOException {
1.119 - try {
1.120 - int state = 0;
1.121 - for (;;) {
1.122 - int ch = emul.read();
1.123 - if (ch == -1) {
1.124 - break;
1.125 - }
1.126 - if (ch < 0 || ch > 255) {
1.127 - throw new IOException("Invalid char in emulation " + ch);
1.128 - }
1.129 - switch (state) {
1.130 - case 0:
1.131 - if (ch == '/') {
1.132 - state = 1;
1.133 - } else {
1.134 - out.append((char)ch);
1.135 - }
1.136 - break;
1.137 - case 1:
1.138 - if (ch == '*') {
1.139 - state = 2;
1.140 - } else {
1.141 - out.append('/').append((char)ch);
1.142 - state = 0;
1.143 - }
1.144 - break;
1.145 - case 2:
1.146 - if (ch == '*') {
1.147 - state = 3;
1.148 - }
1.149 - break;
1.150 - case 3:
1.151 - if (ch == '/') {
1.152 - state = 0;
1.153 - } else {
1.154 - state = 2;
1.155 - }
1.156 - break;
1.157 - }
1.158 - }
1.159 - } finally {
1.160 - emul.close();
1.161 - }
1.162 - }
1.163 -
1.164 - private static InputStream loadClass(ClassLoader l, String name) throws IOException {
1.165 - Enumeration<URL> en = l.getResources(name + ".class");
1.166 - URL u = null;
1.167 - while (en.hasMoreElements()) {
1.168 - u = en.nextElement();
1.169 - }
1.170 - if (u == null) {
1.171 - throw new IOException("Can't find " + name);
1.172 - }
1.173 - if (u.toExternalForm().contains("rt.jar!")) {
1.174 - throw new IOException("No emulation for " + u);
1.175 - }
1.176 - return u.openStream();
1.177 - }
1.178 -
1.179 - static String toString(String name) throws IOException {
1.180 - StringBuilder sb = new StringBuilder();
1.181 - compile(sb, name);
1.182 - return sb.toString().toString();
1.183 - }
1.184 -
1.185 - private StringArray scripts = new StringArray();
1.186 - private StringArray references = new StringArray();
1.187 -
1.188 - @Override
1.189 - protected boolean requireReference(String cn) {
1.190 - if (references.contains(cn)) {
1.191 - return false;
1.192 - }
1.193 - references.add(cn);
1.194 - return true;
1.195 - }
1.196 -
1.197 - @Override
1.198 - protected void requireScript(String resourcePath) {
1.199 - scripts.add(resourcePath);
1.200 - }
1.201 -}