1.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java Wed Dec 05 10:03:58 2012 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,204 +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 - }
1.98 -
1.99 - for (String resource : js.scripts.toArray()) {
1.100 - while (resource.startsWith("/")) {
1.101 - resource = resource.substring(1);
1.102 - }
1.103 - InputStream emul = l.getResourceAsStream(resource);
1.104 - if (emul == null) {
1.105 - throw new IOException("Can't find " + resource);
1.106 - }
1.107 - readResource(emul, out);
1.108 - }
1.109 - js.scripts = new StringArray();
1.110 -
1.111 - StringArray toInit = StringArray.asList(js.references.toArray());
1.112 - toInit.reverse();
1.113 -
1.114 - for (String ic : toInit.toArray()) {
1.115 - int indx = processed.indexOf(ic);
1.116 - if (indx >= 0) {
1.117 - out.append(initCode.toArray()[indx]).append("\n");
1.118 - initCode.toArray()[indx] = "";
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(ClassLoader l, String name) throws IOException {
1.171 - Enumeration<URL> en = l.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 - if (u.toExternalForm().contains("rt.jar!")) {
1.180 - throw new IOException("No emulation for " + u);
1.181 - }
1.182 - return u.openStream();
1.183 - }
1.184 -
1.185 - static String toString(String name) throws IOException {
1.186 - StringBuilder sb = new StringBuilder();
1.187 - compile(sb, name);
1.188 - return sb.toString().toString();
1.189 - }
1.190 -
1.191 - private StringArray scripts = new StringArray();
1.192 - private StringArray references = new StringArray();
1.193 -
1.194 - @Override
1.195 - protected boolean requireReference(String cn) {
1.196 - if (references.contains(cn)) {
1.197 - return false;
1.198 - }
1.199 - references.add(cn);
1.200 - return true;
1.201 - }
1.202 -
1.203 - @Override
1.204 - protected void requireScript(String resourcePath) {
1.205 - scripts.add(resourcePath);
1.206 - }
1.207 -}