1.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java Wed Dec 12 12:37:41 2012 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,203 +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 -final 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 - out.append("Array.prototype.fillNulls = function() {\n" +
1.48 - " for(var i = 0; i < this.length; i++) {\n" +
1.49 - " this[i] = null;\n" +
1.50 - " }\n" +
1.51 - " return this;\n" +
1.52 - "};");
1.53 -
1.54 -
1.55 - StringArray processed = new StringArray();
1.56 - StringArray initCode = new StringArray();
1.57 - for (String baseClass : names.toArray()) {
1.58 - GenJS js = new GenJS(out);
1.59 - js.references.add(baseClass);
1.60 - for (;;) {
1.61 - String name = 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 - }
1.68 - if (name == null) {
1.69 - break;
1.70 - }
1.71 - InputStream is = loadClass(l, name);
1.72 - if (is == null) {
1.73 - throw new IOException("Can't find class " + name);
1.74 - }
1.75 - try {
1.76 - String ic = js.compile(is);
1.77 - processed.add(name);
1.78 - initCode.add(ic == null ? "" : ic);
1.79 - } catch (RuntimeException ex) {
1.80 - if (out instanceof CharSequence) {
1.81 - CharSequence seq = (CharSequence)out;
1.82 - int lastBlock = seq.length();
1.83 - while (lastBlock-- > 0) {
1.84 - if (seq.charAt(lastBlock) == '{') {
1.85 - break;
1.86 - }
1.87 - }
1.88 - throw new IOException("Error while compiling " + name + "\n"
1.89 - + seq.subSequence(lastBlock + 1, seq.length()), ex
1.90 - );
1.91 - } else {
1.92 - throw new IOException("Error while compiling " + name + "\n"
1.93 - + out, ex
1.94 - );
1.95 - }
1.96 - }
1.97 - for (String resource : js.scripts.toArray()) {
1.98 - while (resource.startsWith("/")) {
1.99 - resource = resource.substring(1);
1.100 - }
1.101 - InputStream emul = l.getResourceAsStream(resource);
1.102 - if (emul == null) {
1.103 - throw new IOException("Can't find " + resource);
1.104 - }
1.105 - readResource(emul, out);
1.106 - }
1.107 - js.scripts = new StringArray();
1.108 - }
1.109 -
1.110 - StringArray toInit = StringArray.asList(js.references.toArray());
1.111 - toInit.reverse();
1.112 -
1.113 - for (String ic : toInit.toArray()) {
1.114 - int indx = processed.indexOf(ic);
1.115 - if (indx >= 0) {
1.116 - out.append(initCode.toArray()[indx]).append("\n");
1.117 - initCode.toArray()[indx] = "";
1.118 - }
1.119 - }
1.120 -
1.121 - }
1.122 - }
1.123 - private static void readResource(InputStream emul, Appendable out) throws IOException {
1.124 - try {
1.125 - int state = 0;
1.126 - for (;;) {
1.127 - int ch = emul.read();
1.128 - if (ch == -1) {
1.129 - break;
1.130 - }
1.131 - if (ch < 0 || ch > 255) {
1.132 - throw new IOException("Invalid char in emulation " + ch);
1.133 - }
1.134 - switch (state) {
1.135 - case 0:
1.136 - if (ch == '/') {
1.137 - state = 1;
1.138 - } else {
1.139 - out.append((char)ch);
1.140 - }
1.141 - break;
1.142 - case 1:
1.143 - if (ch == '*') {
1.144 - state = 2;
1.145 - } else {
1.146 - out.append('/').append((char)ch);
1.147 - state = 0;
1.148 - }
1.149 - break;
1.150 - case 2:
1.151 - if (ch == '*') {
1.152 - state = 3;
1.153 - }
1.154 - break;
1.155 - case 3:
1.156 - if (ch == '/') {
1.157 - state = 0;
1.158 - } else {
1.159 - state = 2;
1.160 - }
1.161 - break;
1.162 - }
1.163 - }
1.164 - } finally {
1.165 - emul.close();
1.166 - }
1.167 - }
1.168 -
1.169 - private static InputStream loadClass(ClassLoader l, String name) throws IOException {
1.170 - Enumeration<URL> en = l.getResources(name + ".class");
1.171 - URL u = null;
1.172 - while (en.hasMoreElements()) {
1.173 - u = en.nextElement();
1.174 - }
1.175 - if (u == null) {
1.176 - throw new IOException("Can't find " + name);
1.177 - }
1.178 - if (u.toExternalForm().contains("rt.jar!")) {
1.179 - throw new IOException("No emulation for " + u);
1.180 - }
1.181 - return u.openStream();
1.182 - }
1.183 -
1.184 - static String toString(String name) throws IOException {
1.185 - StringBuilder sb = new StringBuilder();
1.186 - compile(sb, name);
1.187 - return sb.toString().toString();
1.188 - }
1.189 -
1.190 - private StringArray scripts = new StringArray();
1.191 - private StringArray references = new StringArray();
1.192 -
1.193 - @Override
1.194 - protected boolean requireReference(String cn) {
1.195 - if (references.contains(cn)) {
1.196 - return false;
1.197 - }
1.198 - references.add(cn);
1.199 - return true;
1.200 - }
1.201 -
1.202 - @Override
1.203 - protected void requireScript(String resourcePath) {
1.204 - scripts.add(resourcePath);
1.205 - }
1.206 -}