jaroslav@106
|
1 |
/**
|
jaroslav@106
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@106
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@106
|
4 |
*
|
jaroslav@106
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@106
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@106
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@106
|
8 |
*
|
jaroslav@106
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@106
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@106
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@106
|
12 |
* GNU General Public License for more details.
|
jaroslav@106
|
13 |
*
|
jaroslav@106
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@106
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@106
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@106
|
17 |
*/
|
jaroslav@29
|
18 |
package org.apidesign.vm4brwsr;
|
jaroslav@29
|
19 |
|
jaroslav@29
|
20 |
import java.io.IOException;
|
jaroslav@29
|
21 |
import java.io.InputStream;
|
jaroslav@93
|
22 |
import java.net.URL;
|
jtulach@129
|
23 |
import java.util.ArrayList;
|
jaroslav@29
|
24 |
import java.util.Arrays;
|
jaroslav@97
|
25 |
import java.util.Collections;
|
jaroslav@93
|
26 |
import java.util.Enumeration;
|
jaroslav@97
|
27 |
import java.util.HashMap;
|
jaroslav@97
|
28 |
import java.util.Iterator;
|
jtulach@129
|
29 |
import java.util.LinkedHashSet;
|
jaroslav@29
|
30 |
import java.util.LinkedList;
|
jaroslav@29
|
31 |
import java.util.List;
|
jaroslav@97
|
32 |
import java.util.Map;
|
jaroslav@29
|
33 |
|
jaroslav@29
|
34 |
/** Generator of JavaScript from bytecode of classes on classpath of the VM.
|
jaroslav@29
|
35 |
*
|
jaroslav@29
|
36 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@29
|
37 |
*/
|
jaroslav@29
|
38 |
final class GenJS {
|
jaroslav@29
|
39 |
private GenJS() {}
|
jaroslav@29
|
40 |
|
jaroslav@29
|
41 |
static void compile(Appendable out, String... names) throws IOException {
|
jaroslav@29
|
42 |
compile(out, Arrays.asList(names));
|
jaroslav@29
|
43 |
}
|
jaroslav@29
|
44 |
static void compile(Appendable out, List<String> names) throws IOException {
|
jaroslav@109
|
45 |
compile(GenJS.class.getClassLoader(), out, names);
|
jaroslav@109
|
46 |
}
|
jaroslav@109
|
47 |
static void compile(ClassLoader l, Appendable out, List<String> names) throws IOException {
|
jaroslav@133
|
48 |
final Map<String,String> processed = new HashMap<String, String>();
|
jaroslav@97
|
49 |
for (String baseClass : names) {
|
jtulach@129
|
50 |
LinkedHashSet<String> toProcess = new LinkedHashSet<String>() {
|
jtulach@129
|
51 |
@Override
|
jtulach@129
|
52 |
public boolean add(String e) {
|
jtulach@129
|
53 |
if (processed.containsKey(e)) {
|
jtulach@129
|
54 |
return false;
|
jtulach@129
|
55 |
}
|
jtulach@129
|
56 |
return super.add(e);
|
jtulach@129
|
57 |
}
|
jtulach@129
|
58 |
};
|
jaroslav@97
|
59 |
toProcess.add(baseClass);
|
jaroslav@97
|
60 |
for (;;) {
|
jaroslav@97
|
61 |
String name = null;
|
jaroslav@97
|
62 |
Iterator<String> it = toProcess.iterator();
|
jaroslav@97
|
63 |
while (it.hasNext() && name == null) {
|
jaroslav@97
|
64 |
String n = it.next();
|
jaroslav@97
|
65 |
if (processed.get(n) != null) {
|
jaroslav@97
|
66 |
continue;
|
jaroslav@97
|
67 |
}
|
jaroslav@97
|
68 |
name = n;
|
jaroslav@97
|
69 |
}
|
jaroslav@97
|
70 |
if (name == null) {
|
jaroslav@97
|
71 |
break;
|
jaroslav@97
|
72 |
}
|
jtulach@129
|
73 |
if (name.startsWith("sun/")) {
|
jaroslav@97
|
74 |
processed.put(name, "");
|
jaroslav@97
|
75 |
continue;
|
jaroslav@97
|
76 |
}
|
jaroslav@109
|
77 |
InputStream is = loadClass(l, name);
|
jaroslav@97
|
78 |
if (is == null) {
|
jaroslav@97
|
79 |
throw new IOException("Can't find class " + name);
|
jaroslav@97
|
80 |
}
|
jaroslav@97
|
81 |
LinkedList<String> scripts = new LinkedList<String>();
|
jaroslav@97
|
82 |
try {
|
jaroslav@97
|
83 |
String initCode = ByteCodeToJavaScript.compile(is, out, toProcess, scripts);
|
jaroslav@97
|
84 |
processed.put(name, initCode == null ? "" : initCode);
|
jaroslav@97
|
85 |
} catch (RuntimeException ex) {
|
jaroslav@97
|
86 |
if (out instanceof CharSequence) {
|
jaroslav@97
|
87 |
CharSequence seq = (CharSequence)out;
|
jaroslav@97
|
88 |
int lastBlock = seq.length();
|
jaroslav@151
|
89 |
while (lastBlock-- > 0) {
|
jaroslav@97
|
90 |
if (seq.charAt(lastBlock) == '{') {
|
jaroslav@97
|
91 |
break;
|
jaroslav@97
|
92 |
}
|
jaroslav@29
|
93 |
}
|
jaroslav@97
|
94 |
throw new IOException("Error while compiling " + name + "\n"
|
jaroslav@97
|
95 |
+ seq.subSequence(lastBlock + 1, seq.length()), ex
|
jaroslav@97
|
96 |
);
|
jaroslav@97
|
97 |
} else {
|
jaroslav@97
|
98 |
throw new IOException("Error while compiling " + name + "\n"
|
jaroslav@97
|
99 |
+ out, ex
|
jaroslav@97
|
100 |
);
|
jaroslav@29
|
101 |
}
|
jaroslav@97
|
102 |
}
|
jaroslav@97
|
103 |
for (String resource : scripts) {
|
jaroslav@109
|
104 |
while (resource.startsWith("/")) {
|
jaroslav@109
|
105 |
resource = resource.substring(1);
|
jaroslav@109
|
106 |
}
|
jaroslav@109
|
107 |
InputStream emul = l.getResourceAsStream(resource);
|
jaroslav@97
|
108 |
if (emul == null) {
|
jaroslav@97
|
109 |
throw new IOException("Can't find " + resource);
|
jaroslav@97
|
110 |
}
|
jaroslav@97
|
111 |
readResource(emul, out);
|
jaroslav@29
|
112 |
}
|
jaroslav@29
|
113 |
}
|
jaroslav@97
|
114 |
|
jtulach@129
|
115 |
List<String> toInit = new ArrayList<String>(toProcess);
|
jtulach@129
|
116 |
Collections.reverse(toInit);
|
jtulach@129
|
117 |
|
jtulach@129
|
118 |
for (String clazz : toInit) {
|
jaroslav@133
|
119 |
String initCode = processed.get(clazz);
|
jaroslav@133
|
120 |
if (initCode != null && !initCode.isEmpty()) {
|
jaroslav@97
|
121 |
out.append(initCode).append("\n");
|
jaroslav@133
|
122 |
processed.put(clazz, "");
|
jaroslav@91
|
123 |
}
|
jaroslav@91
|
124 |
}
|
jaroslav@133
|
125 |
|
jaroslav@29
|
126 |
}
|
jaroslav@29
|
127 |
}
|
jaroslav@90
|
128 |
private static void readResource(InputStream emul, Appendable out) throws IOException {
|
jaroslav@90
|
129 |
try {
|
jaroslav@90
|
130 |
int state = 0;
|
jaroslav@90
|
131 |
for (;;) {
|
jaroslav@90
|
132 |
int ch = emul.read();
|
jaroslav@90
|
133 |
if (ch == -1) {
|
jaroslav@90
|
134 |
break;
|
jaroslav@90
|
135 |
}
|
jaroslav@90
|
136 |
if (ch < 0 || ch > 255) {
|
jaroslav@90
|
137 |
throw new IOException("Invalid char in emulation " + ch);
|
jaroslav@90
|
138 |
}
|
jaroslav@90
|
139 |
switch (state) {
|
jaroslav@90
|
140 |
case 0:
|
jaroslav@90
|
141 |
if (ch == '/') {
|
jaroslav@90
|
142 |
state = 1;
|
jaroslav@90
|
143 |
} else {
|
jaroslav@90
|
144 |
out.append((char)ch);
|
jaroslav@90
|
145 |
}
|
jaroslav@90
|
146 |
break;
|
jaroslav@90
|
147 |
case 1:
|
jaroslav@90
|
148 |
if (ch == '*') {
|
jaroslav@90
|
149 |
state = 2;
|
jaroslav@90
|
150 |
} else {
|
jaroslav@90
|
151 |
out.append('/').append((char)ch);
|
jaroslav@90
|
152 |
state = 0;
|
jaroslav@90
|
153 |
}
|
jaroslav@90
|
154 |
break;
|
jaroslav@90
|
155 |
case 2:
|
jaroslav@90
|
156 |
if (ch == '*') {
|
jaroslav@90
|
157 |
state = 3;
|
jaroslav@90
|
158 |
}
|
jaroslav@90
|
159 |
break;
|
jaroslav@90
|
160 |
case 3:
|
jaroslav@90
|
161 |
if (ch == '/') {
|
jaroslav@90
|
162 |
state = 0;
|
jaroslav@90
|
163 |
} else {
|
jaroslav@90
|
164 |
state = 2;
|
jaroslav@90
|
165 |
}
|
jaroslav@90
|
166 |
break;
|
jaroslav@90
|
167 |
}
|
jaroslav@90
|
168 |
}
|
jaroslav@90
|
169 |
} finally {
|
jaroslav@90
|
170 |
emul.close();
|
jaroslav@90
|
171 |
}
|
jaroslav@90
|
172 |
}
|
jaroslav@93
|
173 |
|
jaroslav@109
|
174 |
private static InputStream loadClass(ClassLoader l, String name) throws IOException {
|
jaroslav@109
|
175 |
Enumeration<URL> en = l.getResources(name + ".class");
|
jaroslav@93
|
176 |
URL u = null;
|
jaroslav@93
|
177 |
while (en.hasMoreElements()) {
|
jaroslav@93
|
178 |
u = en.nextElement();
|
jaroslav@93
|
179 |
}
|
jaroslav@93
|
180 |
if (u == null) {
|
jaroslav@93
|
181 |
throw new IOException("Can't find " + name);
|
jaroslav@93
|
182 |
}
|
jaroslav@93
|
183 |
return u.openStream();
|
jaroslav@93
|
184 |
}
|
jaroslav@136
|
185 |
|
jaroslav@136
|
186 |
static String toString(String name) throws IOException {
|
jaroslav@136
|
187 |
StringBuilder sb = new StringBuilder();
|
jaroslav@136
|
188 |
compile(sb, name);
|
jaroslav@136
|
189 |
return sb.toString().toString();
|
jaroslav@136
|
190 |
}
|
jaroslav@29
|
191 |
}
|