2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.vm4brwsr;
20 import java.io.ByteArrayInputStream;
21 import java.io.IOException;
22 import org.apidesign.bck2brwsr.core.JavaScriptBody;
26 * @author Jaroslav Tulach <jtulach@netbeans.org>
28 class VMLazy extends ByteCodeToJavaScript {
29 private final Object loader;
31 private VMLazy(Object loader, Appendable out) {
39 @JavaScriptBody(args={"res", "args" }, body = "return args[0](res.toString());")
40 private static native byte[] read(String res, Object[] args);
42 static Object load(Object loader, String name, Object[] arguments)
43 throws IOException, ClassNotFoundException {
44 String res = name.replace('.', '/') + ".class";
45 byte[] arr = read(res, arguments);
47 throw new ClassNotFoundException(name);
49 String code = toJavaScript(loader, arr);
50 return applyCode(loader, name, code);
53 @JavaScriptBody(args = {"loader", "name", "script" }, body =
55 " new Function(script)(loader, name);\n" +
57 " throw 'Cannot compile ' + ex + ' script:\\\\n' + script;\n" +
59 "return vm[name](false);\n"
61 private static native Object applyCode(Object loader, String name, String script);
63 private static String toJavaScript(Object loader, byte[] is) throws IOException {
64 StringBuilder sb = new StringBuilder();
65 sb.append("var loader = arguments[0];\n");
66 sb.append("var vm = loader.vm;\n");
67 new VMLazy(loader, sb).compile(new ByteArrayInputStream(is));
68 return sb.toString().toString();
71 @JavaScriptBody(args = { "self", "n" },
73 "var cls = n.replace__Ljava_lang_String_2CC(n,'/','_').toString();"
74 + "var loader = self.fld_loader;"
75 + "var vm = loader.vm;"
76 + "if (vm[cls]) return false;"
77 + "vm[cls] = function() {"
78 + " return loader.loadClass(n,cls);"
83 protected boolean requireReference(String internalClassName) {
84 throw new UnsupportedOperationException();
88 protected void requireScript(String resourcePath) {
92 String assignClass(String className) {
93 return "vm[arguments[1]]=";
97 String accessClass(String classOperation) {
98 return "vm." + classOperation;