b=625503; convert NaN correctly with typed array array conversion; r=waldo, a=dvander
authorVladimir Vukicevic <vladimir@pobox.com>
Fri, 14 Jan 2011 10:46:22 -0800
changeset 60606 9caa011615ff3def0e33205f3aacb990fb301445
parent 60605 2793d141b019024fd4c9e5f7273f0727c5c1f585
child 60607 1eb45ae169fab5062fdd9ffba2cb54e7afdbdd5d
push idunknown
push userunknown
push dateunknown
reviewerswaldo, dvander
bugs625503
milestone2.0b10pre
b=625503; convert NaN correctly with typed array array conversion; r=waldo, a=dvander
js/src/jstypedarray.cpp
js/src/tests/js1_8_5/extensions/typedarray.js
--- a/js/src/jstypedarray.cpp
+++ b/js/src/jstypedarray.cpp
@@ -1111,18 +1111,22 @@ class TypedArrayTemplate
 
   protected:
     static NativeType
     nativeFromValue(JSContext *cx, const Value &v)
     {
         if (v.isInt32())
             return NativeType(v.toInt32());
 
-        if (v.isDouble())
-            return NativeType(v.toDouble());
+        if (v.isDouble()) {
+            double d = v.toDouble();
+            if (!ArrayTypeIsFloatingPoint() && JS_UNLIKELY(JSDOUBLE_IS_NaN(d)))
+                return NativeType(int32(0));
+            return NativeType(d);
+        }
 
         if (v.isPrimitive() && !v.isMagic()) {
             jsdouble dval;
             ValueToNumber(cx, v, &dval);
             return NativeType(dval);
         }
 
         if (ArrayTypeIsFloatingPoint())
--- a/js/src/tests/js1_8_5/extensions/typedarray.js
+++ b/js/src/tests/js1_8_5/extensions/typedarray.js
@@ -328,14 +328,18 @@ function test()
     // function gets a double, and not a demoted int
     check(function() (new Float32Array(Math.sqrt(4))).length == 2);
     check(function() (new Float32Array({ length: 10 })).length == 10);
     check(function() (new Float32Array({})).length == 0);
     checkThrows(function() new Float32Array("3"));
     checkThrows(function() new Float32Array(null));
     checkThrows(function() new Float32Array(undefined));
 
+    // check that NaN conversions happen correctly with array conversions
+    check(function() (new Int32Array([NaN])[0]) == 0);
+    check(function() { var q = new Float32Array([NaN])[0]; return q != q; });
+
     print ("done");
 
     reportCompare(0, TestFailCount, "typed array tests");
 
     exitFunc ('test');
 }