1.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java Fri Dec 07 14:10:37 2012 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,229 +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 {
1.38 - // uses VMLazy to load dynamic classes
1.39 - VMLazy.init();
1.40 - }
1.41 -
1.42 - static void compile(Appendable out, String... names) throws IOException {
1.43 - compile(out, StringArray.asList(names));
1.44 - }
1.45 - static void compile(ClassLoader l, Appendable out, String... names) throws IOException {
1.46 - compile(l, out, StringArray.asList(names));
1.47 - }
1.48 - static void compile(Appendable out, StringArray names) throws IOException {
1.49 - compile(GenJS.class.getClassLoader(), out, names);
1.50 - }
1.51 - static void compile(ClassLoader l, Appendable out, StringArray names) throws IOException {
1.52 - new GenJS(out).doCompile(l, names);
1.53 - }
1.54 - protected void doCompile(ClassLoader l, StringArray names) throws IOException {
1.55 - out.append("(function VM(global) {");
1.56 - out.append("\n var vm = {};");
1.57 - StringArray processed = new StringArray();
1.58 - StringArray initCode = new StringArray();
1.59 - for (String baseClass : names.toArray()) {
1.60 - references.add(baseClass);
1.61 - for (;;) {
1.62 - String name = null;
1.63 - for (String n : references.toArray()) {
1.64 - if (processed.contains(n)) {
1.65 - continue;
1.66 - }
1.67 - name = n;
1.68 - }
1.69 - if (name == null) {
1.70 - break;
1.71 - }
1.72 - InputStream is = loadClass(l, name);
1.73 - if (is == null) {
1.74 - throw new IOException("Can't find class " + name);
1.75 - }
1.76 - try {
1.77 - String ic = compile(is);
1.78 - processed.add(name);
1.79 - initCode.add(ic == null ? "" : ic);
1.80 - } catch (RuntimeException ex) {
1.81 - if (out instanceof CharSequence) {
1.82 - CharSequence seq = (CharSequence)out;
1.83 - int lastBlock = seq.length();
1.84 - while (lastBlock-- > 0) {
1.85 - if (seq.charAt(lastBlock) == '{') {
1.86 - break;
1.87 - }
1.88 - }
1.89 - throw new IOException("Error while compiling " + name + "\n"
1.90 - + seq.subSequence(lastBlock + 1, seq.length()), ex
1.91 - );
1.92 - } else {
1.93 - throw new IOException("Error while compiling " + name + "\n"
1.94 - + out, ex
1.95 - );
1.96 - }
1.97 - }
1.98 - }
1.99 -
1.100 - for (String resource : scripts.toArray()) {
1.101 - while (resource.startsWith("/")) {
1.102 - resource = resource.substring(1);
1.103 - }
1.104 - InputStream emul = l.getResourceAsStream(resource);
1.105 - if (emul == null) {
1.106 - throw new IOException("Can't find " + resource);
1.107 - }
1.108 - readResource(emul, out);
1.109 - }
1.110 - scripts = new StringArray();
1.111 -
1.112 - StringArray toInit = StringArray.asList(references.toArray());
1.113 - toInit.reverse();
1.114 -
1.115 - for (String ic : toInit.toArray()) {
1.116 - int indx = processed.indexOf(ic);
1.117 - if (indx >= 0) {
1.118 - out.append(initCode.toArray()[indx]).append("\n");
1.119 - initCode.toArray()[indx] = "";
1.120 - }
1.121 - }
1.122 - }
1.123 - out.append(
1.124 - " global.bck2brwsr = function() {\n"
1.125 - + " var args = arguments;\n"
1.126 - + " var loader = {};\n"
1.127 - + " loader.vm = vm;\n"
1.128 - + " loader.loadClass = function(name) {\n"
1.129 - + " var attr = name.replace__Ljava_lang_String_2CC(name, '.','_');\n"
1.130 - + " var fn = vm[attr];\n"
1.131 - + " if (fn) return fn(false);\n"
1.132 - + " return vm.org_apidesign_vm4brwsr_VMLazy(false).\n"
1.133 - + " load___3Ljava_lang_Object_2Ljava_lang_Object_2Ljava_lang_String_2_3Ljava_lang_Object_2(loader, name, args);\n"
1.134 - + " }\n"
1.135 - + " return loader;\n"
1.136 - + " };\n");
1.137 - out.append("}(this));");
1.138 - }
1.139 - private static void readResource(InputStream emul, Appendable out) throws IOException {
1.140 - try {
1.141 - int state = 0;
1.142 - for (;;) {
1.143 - int ch = emul.read();
1.144 - if (ch == -1) {
1.145 - break;
1.146 - }
1.147 - if (ch < 0 || ch > 255) {
1.148 - throw new IOException("Invalid char in emulation " + ch);
1.149 - }
1.150 - switch (state) {
1.151 - case 0:
1.152 - if (ch == '/') {
1.153 - state = 1;
1.154 - } else {
1.155 - out.append((char)ch);
1.156 - }
1.157 - break;
1.158 - case 1:
1.159 - if (ch == '*') {
1.160 - state = 2;
1.161 - } else {
1.162 - out.append('/').append((char)ch);
1.163 - state = 0;
1.164 - }
1.165 - break;
1.166 - case 2:
1.167 - if (ch == '*') {
1.168 - state = 3;
1.169 - }
1.170 - break;
1.171 - case 3:
1.172 - if (ch == '/') {
1.173 - state = 0;
1.174 - } else {
1.175 - state = 2;
1.176 - }
1.177 - break;
1.178 - }
1.179 - }
1.180 - } finally {
1.181 - emul.close();
1.182 - }
1.183 - }
1.184 -
1.185 - private static InputStream loadClass(ClassLoader l, String name) throws IOException {
1.186 - Enumeration<URL> en = l.getResources(name + ".class");
1.187 - URL u = null;
1.188 - while (en.hasMoreElements()) {
1.189 - u = en.nextElement();
1.190 - }
1.191 - if (u == null) {
1.192 - throw new IOException("Can't find " + name);
1.193 - }
1.194 - if (u.toExternalForm().contains("rt.jar!")) {
1.195 - throw new IOException("No emulation for " + u);
1.196 - }
1.197 - return u.openStream();
1.198 - }
1.199 -
1.200 - static String toString(String name) throws IOException {
1.201 - StringBuilder sb = new StringBuilder();
1.202 - compile(sb, name);
1.203 - return sb.toString().toString();
1.204 - }
1.205 -
1.206 - private StringArray scripts = new StringArray();
1.207 - private StringArray references = new StringArray();
1.208 -
1.209 - @Override
1.210 - protected boolean requireReference(String cn) {
1.211 - if (references.contains(cn)) {
1.212 - return false;
1.213 - }
1.214 - references.add(cn);
1.215 - return true;
1.216 - }
1.217 -
1.218 - @Override
1.219 - protected void requireScript(String resourcePath) {
1.220 - scripts.add(resourcePath);
1.221 - }
1.222 -
1.223 - @Override
1.224 - String assignClass(String className) {
1.225 - return "vm." + className + " = ";
1.226 - }
1.227 -
1.228 - @Override
1.229 - String accessClass(String className) {
1.230 - return "vm." + className;
1.231 - }
1.232 -}