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.Exported;
24 import org.apidesign.bck2brwsr.core.JavaScriptBody;
28 * @author Jaroslav Tulach <jtulach@netbeans.org>
31 private final Object vm;
32 private final Object[] args;
34 private VMLazy(Object vm, Object[] args) {
43 static Object load(Object loader, String name, Object[] arguments, byte[] arr)
44 throws IOException, ClassNotFoundException {
46 throw new ClassNotFoundException(name);
48 return new VMLazy(loader, arguments).defineClass(arr, name, false);
52 Object load(String name, boolean instance)
53 throws IOException, ClassNotFoundException {
54 String res = name.replace('.', '/') + ".class";
55 byte[] arr = ClassPath.loadBytes(res, args, 0);
57 throw new ClassNotFoundException(name);
60 return defineClass(arr, name, instance);
63 private Object defineClass(byte[] arr, String name, boolean instance) throws IOException {
64 StringBuilder out = new StringBuilder(65535);
65 out.append("var vm = arguments[0];\n");
66 int prelude = out.length();
67 String initCode = new Gen(this, out).compile(new ByteArrayInputStream(arr));
68 String code = out.toString().toString();
69 String under = name.replace('.', '_');
70 Object fn = applyCode(vm, under, code, instance);
72 if (!initCode.isEmpty()) {
73 out.setLength(prelude);
75 code = out.toString().toString();
76 applyCode(vm, null, code, false);
82 @JavaScriptBody(args = {"vm", "name", "script", "instance" }, body =
84 " new Function(script)(vm, name);\n" +
86 " throw 'Cannot compile ' + name + ' ' + ex + ' line: ' + ex.lineNumber + ' script:\\n' + script;\n" +
88 "return name != null ? vm[name](instance) : null;\n"
90 private static native Object applyCode(Object vm, String name, String script, boolean instance);
93 private static final class Gen extends ByteCodeToJavaScript {
94 private final VMLazy lazy;
96 public Gen(VMLazy vm, Appendable out) {
101 @JavaScriptBody(args = {"n"},
103 "var cls = n.replace__Ljava_lang_String_2CC('/','_').toString();"
104 + "\nvar dot = n.replace__Ljava_lang_String_2CC('/','.').toString();"
105 + "\nvar lazy = this._lazy();"
106 + "\nvar vm = lazy._vm();"
107 + "\nif (vm[cls]) return false;"
108 + "\nvm[cls] = function() {"
109 + "\n var instance = arguments.length == 0 || arguments[0] === true;"
110 + "\n return lazy.load__Ljava_lang_Object_2Ljava_lang_String_2Z(dot, instance);"
114 protected boolean requireReference(String internalClassName) {
115 throw new UnsupportedOperationException();
119 protected void requireScript(String resourcePath) throws IOException {
120 if (!resourcePath.startsWith("/")) {
121 resourcePath = "/" + resourcePath;
123 String code = readCode(resourcePath);
124 applyCode(lazy.vm, null, code, false);
127 private String readCode(String resourcePath) throws IOException {
128 InputStream is = getClass().getResourceAsStream(resourcePath);
129 StringBuilder sb = new StringBuilder();
137 return sb.toString();
141 String assignClass(String className) {
142 return "vm[arguments[1]]=";
146 String accessClass(String classOperation) {
147 return "vm." + classOperation;