If a class is not available during Ahead-Of-Time compilation it needs to be ready for dynamic loading
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 java.io.InputStream;
23 import org.apidesign.bck2brwsr.core.JavaScriptBody;
27 * @author Jaroslav Tulach <jtulach@netbeans.org>
30 private final Object loader;
31 private final Object[] args;
33 private VMLazy(Object loader, Object[] args) {
41 static Object load(Object loader, String name, Object[] arguments)
42 throws IOException, ClassNotFoundException {
43 return new VMLazy(loader, arguments).load(name, false);
46 static byte[] loadBytes(Object loader, String name, Object[] arguments) throws Exception {
47 return Zips.loadFromCp(arguments, name);
50 private Object load(String name, boolean instance)
51 throws IOException, ClassNotFoundException {
52 String res = name.replace('.', '/') + ".class";
53 byte[] arr = Zips.loadFromCp(args, res);
55 throw new ClassNotFoundException(name);
57 // beingDefined(loader, name);
58 StringBuilder out = new StringBuilder(65535);
59 out.append("var loader = arguments[0];\n");
60 out.append("var vm = loader.vm;\n");
61 int prelude = out.length();
62 String initCode = new Gen(this, out).compile(new ByteArrayInputStream(arr));
63 String code = out.toString().toString();
64 String under = name.replace('.', '_');
65 Object fn = applyCode(loader, under, code, instance);
67 if (!initCode.isEmpty()) {
68 out.setLength(prelude);
70 code = out.toString().toString();
71 applyCode(loader, null, code, false);
77 @JavaScriptBody(args = {"loader", "name", "script", "instance" }, body =
79 " new Function(script)(loader, name);\n" +
81 " throw 'Cannot compile ' + name + ' ' + ex + ' line: ' + ex.lineNumber + ' script:\\n' + script;\n" +
83 "return name != null ? vm[name](instance) : null;\n"
85 private static native Object applyCode(Object loader, String name, String script, boolean instance);
88 private static final class Gen extends ByteCodeToJavaScript {
89 private final VMLazy lazy;
91 public Gen(VMLazy vm, Appendable out) {
96 @JavaScriptBody(args = {"n"},
98 "var cls = n.replace__Ljava_lang_String_2CC('/','_').toString();"
99 + "\nvar dot = n.replace__Ljava_lang_String_2CC('/','.').toString();"
100 + "\nvar lazy = this._lazy();"
101 + "\nvar loader = lazy._loader();"
102 + "\nvar vm = loader.vm;"
103 + "\nif (vm[cls]) return false;"
104 + "\nvm[cls] = function() {"
105 + "\n var instance = arguments.length == 0 || arguments[0] === true;"
106 + "\n return lazy.load__Ljava_lang_Object_2Ljava_lang_String_2Z(dot, instance);"
110 protected boolean requireReference(String internalClassName) {
111 throw new UnsupportedOperationException();
115 protected void requireScript(String resourcePath) throws IOException {
116 if (!resourcePath.startsWith("/")) {
117 resourcePath = "/" + resourcePath;
119 String code = readCode(resourcePath);
120 applyCode(lazy.loader, null, code, false);
123 private String readCode(String resourcePath) throws IOException {
124 InputStream is = getClass().getResourceAsStream(resourcePath);
125 StringBuilder sb = new StringBuilder();
133 return sb.toString();
137 String assignClass(String className) {
138 return "vm[arguments[1]]=";
142 String accessClass(String classOperation) {
143 return "vm." + classOperation;
147 String getVMObject() {