jaroslav@170
|
1 |
/**
|
jaroslav@170
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@170
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@170
|
4 |
*
|
jaroslav@170
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@170
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@170
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@170
|
8 |
*
|
jaroslav@170
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@170
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@170
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@170
|
12 |
* GNU General Public License for more details.
|
jaroslav@170
|
13 |
*
|
jaroslav@170
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@170
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@170
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@170
|
17 |
*/
|
jaroslav@170
|
18 |
package org.apidesign.vm4brwsr;
|
jaroslav@170
|
19 |
|
jaroslav@170
|
20 |
import java.io.ByteArrayInputStream;
|
jaroslav@170
|
21 |
import java.io.IOException;
|
jaroslav@214
|
22 |
import org.apidesign.bck2brwsr.core.JavaScriptBody;
|
jaroslav@170
|
23 |
|
jaroslav@170
|
24 |
/**
|
jaroslav@170
|
25 |
*
|
jaroslav@170
|
26 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@170
|
27 |
*/
|
jaroslav@201
|
28 |
class VMLazy extends ByteCodeToJavaScript {
|
jaroslav@214
|
29 |
private final Object vm;
|
jaroslav@214
|
30 |
private final Object global;
|
jaroslav@214
|
31 |
|
jaroslav@214
|
32 |
private VMLazy(Object global, Object vm, Appendable out) {
|
jaroslav@170
|
33 |
super(out);
|
jaroslav@214
|
34 |
this.vm = vm;
|
jaroslav@214
|
35 |
this.global = global;
|
jaroslav@170
|
36 |
}
|
jaroslav@170
|
37 |
|
jaroslav@214
|
38 |
static String toJavaScript(Object global, Object vm, byte[] is) throws IOException {
|
jaroslav@170
|
39 |
StringBuilder sb = new StringBuilder();
|
jaroslav@214
|
40 |
new VMLazy(global, vm, sb).compile(new ByteArrayInputStream(is));
|
jaroslav@170
|
41 |
return sb.toString().toString();
|
jaroslav@170
|
42 |
}
|
jaroslav@170
|
43 |
|
jaroslav@214
|
44 |
@JavaScriptBody(args = { "self", "n" },
|
jaroslav@214
|
45 |
body=
|
jaroslav@214
|
46 |
"var cls = n.replaceLjava_lang_StringCC(n,'/','_').toString();"
|
jaroslav@214
|
47 |
+ "var glb = self.fld_global;"
|
jaroslav@214
|
48 |
+ "var vm = self.fld_vm;"
|
jaroslav@214
|
49 |
+ "if (glb[cls]) return false;"
|
jaroslav@214
|
50 |
+ "glb[cls] = function() {"
|
jaroslav@214
|
51 |
+ " return vm.loadClass(n,cls);"
|
jaroslav@214
|
52 |
+ "};"
|
jaroslav@214
|
53 |
+ "return true;"
|
jaroslav@214
|
54 |
)
|
jaroslav@170
|
55 |
@Override
|
jaroslav@170
|
56 |
protected boolean requireReference(String internalClassName) {
|
jaroslav@214
|
57 |
throw new UnsupportedOperationException();
|
jaroslav@170
|
58 |
}
|
jaroslav@170
|
59 |
|
jaroslav@170
|
60 |
@Override
|
jaroslav@170
|
61 |
protected void requireScript(String resourcePath) {
|
jaroslav@170
|
62 |
}
|
jaroslav@213
|
63 |
|
jaroslav@213
|
64 |
@Override
|
jaroslav@213
|
65 |
protected String assignClass(String className) {
|
jaroslav@213
|
66 |
return "arguments[0][arguments[1]]=";
|
jaroslav@213
|
67 |
}
|
jaroslav@170
|
68 |
}
|