InstanceOf and cast for direct libraries Libraries
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 08 Jun 2016 06:50:38 +0200
branchLibraries
changeset 196680851e48a68f
parent 1965 973e52d4cabb
child 1967 f807d02bb3d0
InstanceOf and cast for direct libraries
rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
rt/vm/src/main/java/org/apidesign/vm4brwsr/DirectlLibraries.java
rt/vm/src/test/java/org/apidesign/vm4brwsr/LibUse.java
rt/vm/src/test/java/org/apidesign/vm4brwsr/LibUseTest.java
     1.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Tue Jun 07 06:20:20 2016 +0200
     1.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Wed Jun 08 06:50:38 2016 +0200
     1.3 @@ -1823,7 +1823,7 @@
     1.4              in.equals("org/netbeans/html/boot/spi/Fn")
     1.5          )) {
     1.6              mcn = "java_lang_Class";
     1.7 -        } else if (in.startsWith("net/java/html/lib/") && in.endsWith("/Exports")) {
     1.8 +        } else if (DirectlLibraries.isScriptLibrary(in) && in.endsWith("/Exports")) {
     1.9              append(mi[1]);
    1.10              append('(');
    1.11              mcn = null;
    1.12 @@ -2394,6 +2394,13 @@
    1.13  
    1.14      private void generateInstanceOf(int indx, final StackMapper smapper) throws IOException {
    1.15          String type = jc.getClassName(indx);
    1.16 +        if (DirectlLibraries.isScriptLibrary(type)) {
    1.17 +            emit(smapper, this,
    1.18 +                    "var @2 = @1 !== null && @1 !== undefined",
    1.19 +                 smapper.popA(), smapper.pushI(),
    1.20 +                 mangleClassName(type));
    1.21 +            return;
    1.22 +        }
    1.23          if (!type.startsWith("[")) {
    1.24              emit(smapper, this, 
    1.25                      "var @2 = @1 != null && @1['$instOf_@3'] ? 1 : 0;",
    1.26 @@ -2424,6 +2431,12 @@
    1.27  
    1.28      private void generateCheckcast(int indx, final StackMapper smapper) throws IOException {
    1.29          String type = jc.getClassName(indx);
    1.30 +        if (DirectlLibraries.isScriptLibrary(type)) {
    1.31 +            emitNoFlush(smapper,
    1.32 +                 "",
    1.33 +                 smapper.getT(0, VarType.REFERENCE, false), mangleClassName(type), type.replace('/', '.'));
    1.34 +            return;
    1.35 +        }
    1.36          if (!type.startsWith("[")) {
    1.37              emitNoFlush(smapper, 
    1.38                   "if (@1 !== null && !@1['$instOf_@2']) vm.java_lang_Class(false).castEx(@1, '@3');",
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/DirectlLibraries.java	Wed Jun 08 06:50:38 2016 +0200
     2.3 @@ -0,0 +1,26 @@
     2.4 +/**
     2.5 + * Back 2 Browser Bytecode Translator
     2.6 + * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     2.7 + *
     2.8 + * This program is free software: you can redistribute it and/or modify
     2.9 + * it under the terms of the GNU General Public License as published by
    2.10 + * the Free Software Foundation, version 2 of the License.
    2.11 + *
    2.12 + * This program is distributed in the hope that it will be useful,
    2.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    2.15 + * GNU General Public License for more details.
    2.16 + *
    2.17 + * You should have received a copy of the GNU General Public License
    2.18 + * along with this program. Look for COPYING file in the top folder.
    2.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    2.20 + */
    2.21 +package org.apidesign.vm4brwsr;
    2.22 +
    2.23 +final class DirectlLibraries {
    2.24 +
    2.25 +    static boolean isScriptLibrary(String type) {
    2.26 +        return type.startsWith("net/java/html/lib/");
    2.27 +    }
    2.28 +
    2.29 +}
     3.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/LibUse.java	Tue Jun 07 06:20:20 2016 +0200
     3.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/LibUse.java	Wed Jun 08 06:50:38 2016 +0200
     3.3 @@ -17,11 +17,24 @@
     3.4   */
     3.5  package org.apidesign.vm4brwsr;
     3.6  
     3.7 -import net.java.html.lib.Exports;
     3.8 +import static net.java.html.lib.Exports.eval;
     3.9 +import net.java.html.lib.Objs;
    3.10  
    3.11  public class LibUse {
    3.12      public static int fourtyTwo() {
    3.13 -        Number n = (Number) Exports.eval("6 * 7");
    3.14 +        Number n = (Number) eval("6 * 7");
    3.15          return n.intValue();
    3.16      }
    3.17 +
    3.18 +    public static String hiProperty() {
    3.19 +        final Object obj = eval("var x = {}; x.x = 'Hi'; x");
    3.20 +        if (obj == null) {
    3.21 +            throw new IllegalStateException("Some Value returned " + obj);
    3.22 +        }
    3.23 +        if (!(obj instanceof Objs)) {
    3.24 +            throw new IllegalStateException("The result is Objs: " + obj.getClass());
    3.25 +        }
    3.26 +        Objs js = (Objs) obj;
    3.27 +        return (String) js.$get("x");
    3.28 +    }
    3.29  }
     4.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/LibUseTest.java	Tue Jun 07 06:20:20 2016 +0200
     4.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/LibUseTest.java	Wed Jun 08 06:50:38 2016 +0200
     4.3 @@ -37,4 +37,9 @@
     4.4      public void fourtyTwo() throws Exception {
     4.5          code.assertExec("Fourty two", LibUse.class, "fourtyTwo__I", 42);
     4.6      }
     4.7 +
     4.8 +    @Test
     4.9 +    public void readAHiProperty() throws Exception {
    4.10 +        code.assertExec("Object has property x", LibUse.class, "hiProperty__Ljava_lang_String_2", "Hi");
    4.11 +    }
    4.12  }