# HG changeset patch # User Jaroslav Tulach # Date 1465361438 -7200 # Node ID 80851e48a68f2f8adaf1431297dbdd4644237f19 # Parent 973e52d4cabb4b9a1fd8fb0827b7b33f6de8b7f5 InstanceOf and cast for direct libraries diff -r 973e52d4cabb -r 80851e48a68f rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Tue Jun 07 06:20:20 2016 +0200 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Wed Jun 08 06:50:38 2016 +0200 @@ -1823,7 +1823,7 @@ in.equals("org/netbeans/html/boot/spi/Fn") )) { mcn = "java_lang_Class"; - } else if (in.startsWith("net/java/html/lib/") && in.endsWith("/Exports")) { + } else if (DirectlLibraries.isScriptLibrary(in) && in.endsWith("/Exports")) { append(mi[1]); append('('); mcn = null; @@ -2394,6 +2394,13 @@ private void generateInstanceOf(int indx, final StackMapper smapper) throws IOException { String type = jc.getClassName(indx); + if (DirectlLibraries.isScriptLibrary(type)) { + emit(smapper, this, + "var @2 = @1 !== null && @1 !== undefined", + smapper.popA(), smapper.pushI(), + mangleClassName(type)); + return; + } if (!type.startsWith("[")) { emit(smapper, this, "var @2 = @1 != null && @1['$instOf_@3'] ? 1 : 0;", @@ -2424,6 +2431,12 @@ private void generateCheckcast(int indx, final StackMapper smapper) throws IOException { String type = jc.getClassName(indx); + if (DirectlLibraries.isScriptLibrary(type)) { + emitNoFlush(smapper, + "", + smapper.getT(0, VarType.REFERENCE, false), mangleClassName(type), type.replace('/', '.')); + return; + } if (!type.startsWith("[")) { emitNoFlush(smapper, "if (@1 !== null && !@1['$instOf_@2']) vm.java_lang_Class(false).castEx(@1, '@3');", diff -r 973e52d4cabb -r 80851e48a68f rt/vm/src/main/java/org/apidesign/vm4brwsr/DirectlLibraries.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/DirectlLibraries.java Wed Jun 08 06:50:38 2016 +0200 @@ -0,0 +1,26 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012-2015 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.vm4brwsr; + +final class DirectlLibraries { + + static boolean isScriptLibrary(String type) { + return type.startsWith("net/java/html/lib/"); + } + +} diff -r 973e52d4cabb -r 80851e48a68f rt/vm/src/test/java/org/apidesign/vm4brwsr/LibUse.java --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/LibUse.java Tue Jun 07 06:20:20 2016 +0200 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/LibUse.java Wed Jun 08 06:50:38 2016 +0200 @@ -17,11 +17,24 @@ */ package org.apidesign.vm4brwsr; -import net.java.html.lib.Exports; +import static net.java.html.lib.Exports.eval; +import net.java.html.lib.Objs; public class LibUse { public static int fourtyTwo() { - Number n = (Number) Exports.eval("6 * 7"); + Number n = (Number) eval("6 * 7"); return n.intValue(); } + + public static String hiProperty() { + final Object obj = eval("var x = {}; x.x = 'Hi'; x"); + if (obj == null) { + throw new IllegalStateException("Some Value returned " + obj); + } + if (!(obj instanceof Objs)) { + throw new IllegalStateException("The result is Objs: " + obj.getClass()); + } + Objs js = (Objs) obj; + return (String) js.$get("x"); + } } diff -r 973e52d4cabb -r 80851e48a68f rt/vm/src/test/java/org/apidesign/vm4brwsr/LibUseTest.java --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/LibUseTest.java Tue Jun 07 06:20:20 2016 +0200 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/LibUseTest.java Wed Jun 08 06:50:38 2016 +0200 @@ -37,4 +37,9 @@ public void fourtyTwo() throws Exception { code.assertExec("Fourty two", LibUse.class, "fourtyTwo__I", 42); } + + @Test + public void readAHiProperty() throws Exception { + code.assertExec("Object has property x", LibUse.class, "hiProperty__Ljava_lang_String_2", "Hi"); + } }