# HG changeset patch # User Jaroslav Tulach # Date 1361983847 -3600 # Node ID 8264f07b1f460d1d52e2ce9cda6e680a017de442 # Parent 6f8683517f1f186954e7acb5edfc9d27662edaf9 All JavaScript numbers will have doubleValue__D and co. methods. One can also use valueOf() on any Number to get its primitive value diff -r 6f8683517f1f -r 8264f07b1f46 rt/emul/mini/src/main/java/java/lang/Number.java --- a/rt/emul/mini/src/main/java/java/lang/Number.java Wed Feb 27 16:24:42 2013 +0100 +++ b/rt/emul/mini/src/main/java/java/lang/Number.java Wed Feb 27 17:50:47 2013 +0100 @@ -26,6 +26,9 @@ package java.lang; import org.apidesign.bck2brwsr.core.ExtraJavaScript; +import org.apidesign.bck2brwsr.core.JavaScriptBody; +import org.apidesign.bck2brwsr.core.JavaScriptOnly; +import org.apidesign.bck2brwsr.core.JavaScriptPrototype; /** * The abstract class Number is the superclass of classes @@ -52,6 +55,7 @@ resource="/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js", processByteCode=true ) +@JavaScriptPrototype(container = "Number.prototype", prototype = "new Number") public abstract class Number implements java.io.Serializable { /** * Returns the value of the specified number as an int. @@ -60,6 +64,7 @@ * @return the numeric value represented by this object after conversion * to type int. */ + @JavaScriptBody(args = {}, body = "return this | 0;") public abstract int intValue(); /** @@ -69,6 +74,7 @@ * @return the numeric value represented by this object after conversion * to type long. */ + @JavaScriptBody(args = {}, body = "return this.toLong();") public abstract long longValue(); /** @@ -78,6 +84,7 @@ * @return the numeric value represented by this object after conversion * to type float. */ + @JavaScriptBody(args = {}, body = "return this;") public abstract float floatValue(); /** @@ -87,6 +94,7 @@ * @return the numeric value represented by this object after conversion * to type double. */ + @JavaScriptBody(args = {}, body = "return this;") public abstract double doubleValue(); /** @@ -115,4 +123,15 @@ /** use serialVersionUID from JDK 1.0.2 for interoperability */ private static final long serialVersionUID = -8742448824652078965L; + + static { + // as last step of initialization, initialize valueOf method + initValueOf(); + } + @JavaScriptBody(args = { }, body = + "var p = vm.java_lang_Number(false);\n" + + "p.valueOf = function() { return this.doubleValue__D(); };\n" + + "p.toString = function() { return this.toString__Ljava_lang_String_2(); };" + ) + private native static void initValueOf(); } diff -r 6f8683517f1f -r 8264f07b1f46 rt/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java Wed Feb 27 16:24:42 2013 +0100 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java Wed Feb 27 17:50:47 2013 +0100 @@ -142,6 +142,49 @@ ); } + @Test public void everyNumberHasJavaLangNumberMethods() throws Exception { + assertExec("Can we call doubleValue?", + Numbers.class, "seven__DI", + Double.valueOf(7.0), 0 + ); + } + @Test public void everyNumberHasJavaLangNumberMethodsInt() throws Exception { + assertExec("Can we call doubleValue?", + Numbers.class, "seven__DI", + Double.valueOf(7.0), 1 + ); + } + @Test public void everyNumberHasJavaLangNumberMethodsLong() throws Exception { + assertExec("Can we call doubleValue?", + Numbers.class, "seven__DI", + Double.valueOf(7.0), 2 + ); + } + @Test public void everyNumberHasJavaLangNumberMethodsShort() throws Exception { + assertExec("Can we call doubleValue?", + Numbers.class, "seven__DI", + Double.valueOf(7.0), 3 + ); + } + @Test public void everyNumberHasJavaLangNumberMethodsByte() throws Exception { + assertExec("Can we call doubleValue?", + Numbers.class, "seven__DI", + Double.valueOf(7.0), 4 + ); + } + @Test public void valueOfNumber() throws Exception { + assertExec("Can we call JavaScripts valueOf?", + Numbers.class, "seven__DI", + Double.valueOf(7.0), 8 + ); + } + @Test public void valueOfLongNumber() throws Exception { + assertExec("Can we call JavaScripts valueOf?", + Numbers.class, "seven__DI", + Double.valueOf(Long.MAX_VALUE / 5), 9 + ); + } + private static TestVM code; @BeforeClass diff -r 6f8683517f1f -r 8264f07b1f46 rt/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java Wed Feb 27 16:24:42 2013 +0100 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java Wed Feb 27 17:50:47 2013 +0100 @@ -20,6 +20,7 @@ import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.IOException; +import org.apidesign.bck2brwsr.core.JavaScriptBody; /** * @@ -67,4 +68,23 @@ static String floatToString() { return new Float(7.0).toString().toString(); } + + static double seven(int todo) { + switch (todo) { + case 0: return sevenNew().doubleValue(); + case 1: return sevenNew().intValue(); + case 2: return sevenNew().longValue(); + case 3: return sevenNew().shortValue(); + case 4: return sevenNew().byteValue(); + case 8: return valueOf(Double.valueOf(7.0)); + case 9: return valueOf(Long.valueOf(Long.MAX_VALUE / 5)); + 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); }