jaroslav@170: /** jaroslav@170: * Back 2 Browser Bytecode Translator jaroslav@170: * Copyright (C) 2012 Jaroslav Tulach jaroslav@170: * jaroslav@170: * This program is free software: you can redistribute it and/or modify jaroslav@170: * it under the terms of the GNU General Public License as published by jaroslav@170: * the Free Software Foundation, version 2 of the License. jaroslav@170: * jaroslav@170: * This program is distributed in the hope that it will be useful, jaroslav@170: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@170: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@170: * GNU General Public License for more details. jaroslav@170: * jaroslav@170: * You should have received a copy of the GNU General Public License jaroslav@170: * along with this program. Look for COPYING file in the top folder. jaroslav@170: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@170: */ jaroslav@170: package org.apidesign.vm4brwsr; jaroslav@170: jaroslav@170: import java.io.ByteArrayInputStream; jaroslav@170: import java.io.IOException; jaroslav@214: import org.apidesign.bck2brwsr.core.JavaScriptBody; jaroslav@170: jaroslav@170: /** jaroslav@170: * jaroslav@170: * @author Jaroslav Tulach jaroslav@170: */ jaroslav@201: class VMLazy extends ByteCodeToJavaScript { jaroslav@214: private final Object vm; jaroslav@214: private final Object global; jaroslav@214: jaroslav@214: private VMLazy(Object global, Object vm, Appendable out) { jaroslav@170: super(out); jaroslav@214: this.vm = vm; jaroslav@214: this.global = global; jaroslav@170: } jaroslav@170: jaroslav@214: static String toJavaScript(Object global, Object vm, byte[] is) throws IOException { jaroslav@170: StringBuilder sb = new StringBuilder(); jaroslav@214: new VMLazy(global, vm, sb).compile(new ByteArrayInputStream(is)); jaroslav@170: return sb.toString().toString(); jaroslav@170: } jaroslav@170: jaroslav@214: @JavaScriptBody(args = { "self", "n" }, jaroslav@214: body= jaroslav@214: "var cls = n.replaceLjava_lang_StringCC(n,'/','_').toString();" jaroslav@214: + "var glb = self.fld_global;" jaroslav@214: + "var vm = self.fld_vm;" jaroslav@214: + "if (glb[cls]) return false;" jaroslav@214: + "glb[cls] = function() {" jaroslav@214: + " return vm.loadClass(n,cls);" jaroslav@214: + "};" jaroslav@214: + "return true;" jaroslav@214: ) jaroslav@170: @Override jaroslav@170: protected boolean requireReference(String internalClassName) { jaroslav@214: throw new UnsupportedOperationException(); jaroslav@170: } jaroslav@170: jaroslav@170: @Override jaroslav@170: protected void requireScript(String resourcePath) { jaroslav@170: } jaroslav@213: jaroslav@213: @Override jaroslav@213: protected String assignClass(String className) { jaroslav@213: return "arguments[0][arguments[1]]="; jaroslav@213: } jaroslav@170: }