java.lang.Boolean is subclass of JavaScript's Boolean. true and false are instances of java.lang.Boolean
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 16 May 2013 08:49:55 +0200
changeset 10978e42a376da73
parent 1096 d2ac5b50eb3e
child 1098 fcf1d05b0d39
java.lang.Boolean is subclass of JavaScript's Boolean. true and false are instances of java.lang.Boolean
rt/emul/brwsrtest/src/test/java/org/apidesign/bck2brwsr/brwsrtest/BooleanTest.java
rt/emul/mini/src/main/java/java/lang/Boolean.java
rt/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java
rt/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/brwsrtest/src/test/java/org/apidesign/bck2brwsr/brwsrtest/BooleanTest.java	Thu May 16 08:49:55 2013 +0200
     1.3 @@ -0,0 +1,49 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package org.apidesign.bck2brwsr.brwsrtest;
    1.22 +
    1.23 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
    1.24 +import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
    1.25 +import org.apidesign.bck2brwsr.vmtest.Compare;
    1.26 +import org.apidesign.bck2brwsr.vmtest.VMTest;
    1.27 +import org.testng.annotations.Factory;
    1.28 +
    1.29 +/**
    1.30 + *
    1.31 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.32 + */
    1.33 +public class BooleanTest {
    1.34 +    @JavaScriptBody(args = { "tr" }, body = "return tr ? true : false;")
    1.35 +    private static native Object trueFalse(boolean tr);
    1.36 +    
    1.37 +    @BrwsrTest public void isTrueInstanceOfBoolean() {
    1.38 +        Object t = trueFalse(true);
    1.39 +        assert t instanceof Boolean : "Should be boolean: " + t;
    1.40 +        assert ((boolean)t) : "and is true";
    1.41 +    }
    1.42 +    
    1.43 +    @BrwsrTest public void isFalseInstanceOfBoolean() {
    1.44 +        Object t = trueFalse(false);
    1.45 +        assert t instanceof Boolean : "Should be boolean: " + t;
    1.46 +        assert !((boolean)t) : "and is false: " + t;
    1.47 +    }
    1.48 +
    1.49 +    @Factory public static Object[] create() {
    1.50 +        return VMTest.create(BooleanTest.class);
    1.51 +    }
    1.52 +}
     2.1 --- a/rt/emul/mini/src/main/java/java/lang/Boolean.java	Wed May 15 09:57:43 2013 +0200
     2.2 +++ b/rt/emul/mini/src/main/java/java/lang/Boolean.java	Thu May 16 08:49:55 2013 +0200
     2.3 @@ -26,6 +26,7 @@
     2.4  package java.lang;
     2.5  
     2.6  import org.apidesign.bck2brwsr.core.JavaScriptBody;
     2.7 +import org.apidesign.bck2brwsr.core.JavaScriptPrototype;
     2.8  
     2.9  /**
    2.10   * The Boolean class wraps a value of the primitive type
    2.11 @@ -42,6 +43,7 @@
    2.12   * @author  Arthur van Hoff
    2.13   * @since   JDK1.0
    2.14   */
    2.15 +@JavaScriptPrototype(container = "Boolean.prototype", prototype = "new Boolean")
    2.16  public final class Boolean implements java.io.Serializable,
    2.17                                        Comparable<Boolean>
    2.18  {
    2.19 @@ -129,6 +131,7 @@
    2.20       *
    2.21       * @return  the primitive {@code boolean} value of this object.
    2.22       */
    2.23 +    @JavaScriptBody(args = {}, body = "return this.valueOf();")
    2.24      public boolean booleanValue() {
    2.25          return value;
    2.26      }
    2.27 @@ -287,7 +290,7 @@
    2.28      }
    2.29      @JavaScriptBody(args = {  }, body = 
    2.30          "vm.java_lang_Boolean(false)" +
    2.31 -        ".valueOf = function() { return this._value(); };"
    2.32 +        ".valueOf = function() { return this._value() ? true : false; };"
    2.33      )
    2.34      private native static void initValueOf();
    2.35  }
     3.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java	Wed May 15 09:57:43 2013 +0200
     3.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java	Thu May 16 08:49:55 2013 +0200
     3.3 @@ -194,14 +194,14 @@
     3.4  
     3.5      @Test public void valueOfLongBooleanTrue() throws Exception {
     3.6          assertExec("Can we call JavaScripts valueOf on Boolean?", 
     3.7 -            Numbers.class, "seven__DI", 
     3.8 -            Double.valueOf(1), 31
     3.9 +            Numbers.class, "bseven__ZI", 
    3.10 +            true, 31
    3.11          );
    3.12      }
    3.13      @Test public void valueOfLongBooleanFalse() throws Exception {
    3.14          assertExec("Can we call JavaScripts valueOf on Boolean?", 
    3.15 -            Numbers.class, "seven__DI", 
    3.16 -            Double.valueOf(0), 30
    3.17 +            Numbers.class, "bseven__ZI", 
    3.18 +            false, 30
    3.19          );
    3.20      }
    3.21  
     4.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java	Wed May 15 09:57:43 2013 +0200
     4.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java	Thu May 16 08:49:55 2013 +0200
     4.3 @@ -84,10 +84,20 @@
     4.4              default: throw new IllegalStateException();
     4.5          }
     4.6      }
     4.7 +    static boolean bseven(int todo) {
     4.8 +        switch (todo) {
     4.9 +            case 30: return bvalueOf(Boolean.FALSE);
    4.10 +            case 31: return bvalueOf(Boolean.TRUE);
    4.11 +            default: throw new IllegalStateException();
    4.12 +        }
    4.13 +    }
    4.14      
    4.15      @JavaScriptBody(args = {}, body = "return 7;")
    4.16      private static native Number sevenNew();
    4.17 +
    4.18 +    @JavaScriptBody(args = { "o" }, body = "return o.valueOf();")
    4.19 +    private static native double valueOf(Object o);
    4.20      
    4.21      @JavaScriptBody(args = { "o" }, body = "return o.valueOf();")
    4.22 -    private static native double valueOf(Object o);
    4.23 +    private static native boolean bvalueOf(Object o);
    4.24  }