All JavaScript numbers will have doubleValue__D and co. methods. One can also use valueOf() on any Number to get its primitive value
1.1 --- a/rt/emul/mini/src/main/java/java/lang/Number.java Wed Feb 27 16:24:42 2013 +0100
1.2 +++ b/rt/emul/mini/src/main/java/java/lang/Number.java Wed Feb 27 17:50:47 2013 +0100
1.3 @@ -26,6 +26,9 @@
1.4 package java.lang;
1.5
1.6 import org.apidesign.bck2brwsr.core.ExtraJavaScript;
1.7 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.8 +import org.apidesign.bck2brwsr.core.JavaScriptOnly;
1.9 +import org.apidesign.bck2brwsr.core.JavaScriptPrototype;
1.10
1.11 /**
1.12 * The abstract class <code>Number</code> is the superclass of classes
1.13 @@ -52,6 +55,7 @@
1.14 resource="/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js",
1.15 processByteCode=true
1.16 )
1.17 +@JavaScriptPrototype(container = "Number.prototype", prototype = "new Number")
1.18 public abstract class Number implements java.io.Serializable {
1.19 /**
1.20 * Returns the value of the specified number as an <code>int</code>.
1.21 @@ -60,6 +64,7 @@
1.22 * @return the numeric value represented by this object after conversion
1.23 * to type <code>int</code>.
1.24 */
1.25 + @JavaScriptBody(args = {}, body = "return this | 0;")
1.26 public abstract int intValue();
1.27
1.28 /**
1.29 @@ -69,6 +74,7 @@
1.30 * @return the numeric value represented by this object after conversion
1.31 * to type <code>long</code>.
1.32 */
1.33 + @JavaScriptBody(args = {}, body = "return this.toLong();")
1.34 public abstract long longValue();
1.35
1.36 /**
1.37 @@ -78,6 +84,7 @@
1.38 * @return the numeric value represented by this object after conversion
1.39 * to type <code>float</code>.
1.40 */
1.41 + @JavaScriptBody(args = {}, body = "return this;")
1.42 public abstract float floatValue();
1.43
1.44 /**
1.45 @@ -87,6 +94,7 @@
1.46 * @return the numeric value represented by this object after conversion
1.47 * to type <code>double</code>.
1.48 */
1.49 + @JavaScriptBody(args = {}, body = "return this;")
1.50 public abstract double doubleValue();
1.51
1.52 /**
1.53 @@ -115,4 +123,15 @@
1.54
1.55 /** use serialVersionUID from JDK 1.0.2 for interoperability */
1.56 private static final long serialVersionUID = -8742448824652078965L;
1.57 +
1.58 + static {
1.59 + // as last step of initialization, initialize valueOf method
1.60 + initValueOf();
1.61 + }
1.62 + @JavaScriptBody(args = { }, body =
1.63 + "var p = vm.java_lang_Number(false);\n" +
1.64 + "p.valueOf = function() { return this.doubleValue__D(); };\n" +
1.65 + "p.toString = function() { return this.toString__Ljava_lang_String_2(); };"
1.66 + )
1.67 + private native static void initValueOf();
1.68 }
2.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java Wed Feb 27 16:24:42 2013 +0100
2.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java Wed Feb 27 17:50:47 2013 +0100
2.3 @@ -142,6 +142,49 @@
2.4 );
2.5 }
2.6
2.7 + @Test public void everyNumberHasJavaLangNumberMethods() throws Exception {
2.8 + assertExec("Can we call doubleValue?",
2.9 + Numbers.class, "seven__DI",
2.10 + Double.valueOf(7.0), 0
2.11 + );
2.12 + }
2.13 + @Test public void everyNumberHasJavaLangNumberMethodsInt() throws Exception {
2.14 + assertExec("Can we call doubleValue?",
2.15 + Numbers.class, "seven__DI",
2.16 + Double.valueOf(7.0), 1
2.17 + );
2.18 + }
2.19 + @Test public void everyNumberHasJavaLangNumberMethodsLong() throws Exception {
2.20 + assertExec("Can we call doubleValue?",
2.21 + Numbers.class, "seven__DI",
2.22 + Double.valueOf(7.0), 2
2.23 + );
2.24 + }
2.25 + @Test public void everyNumberHasJavaLangNumberMethodsShort() throws Exception {
2.26 + assertExec("Can we call doubleValue?",
2.27 + Numbers.class, "seven__DI",
2.28 + Double.valueOf(7.0), 3
2.29 + );
2.30 + }
2.31 + @Test public void everyNumberHasJavaLangNumberMethodsByte() throws Exception {
2.32 + assertExec("Can we call doubleValue?",
2.33 + Numbers.class, "seven__DI",
2.34 + Double.valueOf(7.0), 4
2.35 + );
2.36 + }
2.37 + @Test public void valueOfNumber() throws Exception {
2.38 + assertExec("Can we call JavaScripts valueOf?",
2.39 + Numbers.class, "seven__DI",
2.40 + Double.valueOf(7.0), 8
2.41 + );
2.42 + }
2.43 + @Test public void valueOfLongNumber() throws Exception {
2.44 + assertExec("Can we call JavaScripts valueOf?",
2.45 + Numbers.class, "seven__DI",
2.46 + Double.valueOf(Long.MAX_VALUE / 5), 9
2.47 + );
2.48 + }
2.49 +
2.50 private static TestVM code;
2.51
2.52 @BeforeClass
3.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java Wed Feb 27 16:24:42 2013 +0100
3.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java Wed Feb 27 17:50:47 2013 +0100
3.3 @@ -20,6 +20,7 @@
3.4 import java.io.ByteArrayInputStream;
3.5 import java.io.DataInputStream;
3.6 import java.io.IOException;
3.7 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
3.8
3.9 /**
3.10 *
3.11 @@ -67,4 +68,23 @@
3.12 static String floatToString() {
3.13 return new Float(7.0).toString().toString();
3.14 }
3.15 +
3.16 + static double seven(int todo) {
3.17 + switch (todo) {
3.18 + case 0: return sevenNew().doubleValue();
3.19 + case 1: return sevenNew().intValue();
3.20 + case 2: return sevenNew().longValue();
3.21 + case 3: return sevenNew().shortValue();
3.22 + case 4: return sevenNew().byteValue();
3.23 + case 8: return valueOf(Double.valueOf(7.0));
3.24 + case 9: return valueOf(Long.valueOf(Long.MAX_VALUE / 5));
3.25 + default: throw new IllegalStateException();
3.26 + }
3.27 + }
3.28 +
3.29 + @JavaScriptBody(args = {}, body = "return 7;")
3.30 + private static native Number sevenNew();
3.31 +
3.32 + @JavaScriptBody(args = { "o" }, body = "return o.valueOf();")
3.33 + private static native double valueOf(Object o);
3.34 }