# HG changeset patch # User Jaroslav Tulach # Date 1368686995 -7200 # Node ID 8e42a376da73709f700e94aeb6ab8535c6600c87 # Parent d2ac5b50eb3e2167d5115a0e74ddadbdf0d358db java.lang.Boolean is subclass of JavaScript's Boolean. true and false are instances of java.lang.Boolean diff -r d2ac5b50eb3e -r 8e42a376da73 rt/emul/brwsrtest/src/test/java/org/apidesign/bck2brwsr/brwsrtest/BooleanTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/emul/brwsrtest/src/test/java/org/apidesign/bck2brwsr/brwsrtest/BooleanTest.java Thu May 16 08:49:55 2013 +0200 @@ -0,0 +1,49 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 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.bck2brwsr.brwsrtest; + +import org.apidesign.bck2brwsr.core.JavaScriptBody; +import org.apidesign.bck2brwsr.vmtest.BrwsrTest; +import org.apidesign.bck2brwsr.vmtest.Compare; +import org.apidesign.bck2brwsr.vmtest.VMTest; +import org.testng.annotations.Factory; + +/** + * + * @author Jaroslav Tulach + */ +public class BooleanTest { + @JavaScriptBody(args = { "tr" }, body = "return tr ? true : false;") + private static native Object trueFalse(boolean tr); + + @BrwsrTest public void isTrueInstanceOfBoolean() { + Object t = trueFalse(true); + assert t instanceof Boolean : "Should be boolean: " + t; + assert ((boolean)t) : "and is true"; + } + + @BrwsrTest public void isFalseInstanceOfBoolean() { + Object t = trueFalse(false); + assert t instanceof Boolean : "Should be boolean: " + t; + assert !((boolean)t) : "and is false: " + t; + } + + @Factory public static Object[] create() { + return VMTest.create(BooleanTest.class); + } +} diff -r d2ac5b50eb3e -r 8e42a376da73 rt/emul/mini/src/main/java/java/lang/Boolean.java --- a/rt/emul/mini/src/main/java/java/lang/Boolean.java Wed May 15 09:57:43 2013 +0200 +++ b/rt/emul/mini/src/main/java/java/lang/Boolean.java Thu May 16 08:49:55 2013 +0200 @@ -26,6 +26,7 @@ package java.lang; import org.apidesign.bck2brwsr.core.JavaScriptBody; +import org.apidesign.bck2brwsr.core.JavaScriptPrototype; /** * The Boolean class wraps a value of the primitive type @@ -42,6 +43,7 @@ * @author Arthur van Hoff * @since JDK1.0 */ +@JavaScriptPrototype(container = "Boolean.prototype", prototype = "new Boolean") public final class Boolean implements java.io.Serializable, Comparable { @@ -129,6 +131,7 @@ * * @return the primitive {@code boolean} value of this object. */ + @JavaScriptBody(args = {}, body = "return this.valueOf();") public boolean booleanValue() { return value; } @@ -287,7 +290,7 @@ } @JavaScriptBody(args = { }, body = "vm.java_lang_Boolean(false)" + - ".valueOf = function() { return this._value(); };" + ".valueOf = function() { return this._value() ? true : false; };" ) private native static void initValueOf(); } diff -r d2ac5b50eb3e -r 8e42a376da73 rt/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java Wed May 15 09:57:43 2013 +0200 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java Thu May 16 08:49:55 2013 +0200 @@ -194,14 +194,14 @@ @Test public void valueOfLongBooleanTrue() throws Exception { assertExec("Can we call JavaScripts valueOf on Boolean?", - Numbers.class, "seven__DI", - Double.valueOf(1), 31 + Numbers.class, "bseven__ZI", + true, 31 ); } @Test public void valueOfLongBooleanFalse() throws Exception { assertExec("Can we call JavaScripts valueOf on Boolean?", - Numbers.class, "seven__DI", - Double.valueOf(0), 30 + Numbers.class, "bseven__ZI", + false, 30 ); } diff -r d2ac5b50eb3e -r 8e42a376da73 rt/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java Wed May 15 09:57:43 2013 +0200 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java Thu May 16 08:49:55 2013 +0200 @@ -84,10 +84,20 @@ default: throw new IllegalStateException(); } } + static boolean bseven(int todo) { + switch (todo) { + case 30: return bvalueOf(Boolean.FALSE); + case 31: return bvalueOf(Boolean.TRUE); + default: throw new IllegalStateException(); + } + } @JavaScriptBody(args = {}, body = "return 7;") private static native Number sevenNew(); + + @JavaScriptBody(args = { "o" }, body = "return o.valueOf();") + private static native double valueOf(Object o); @JavaScriptBody(args = { "o" }, body = "return o.valueOf();") - private static native double valueOf(Object o); + private static native boolean bvalueOf(Object o); }