b=612334; have typed arrays treat length parans more consistently; r=waldo, a=b

This commit is contained in:
Vladimir Vukicevic 2010-12-22 15:31:02 -08:00
parent b255501115
commit ef45dd3c93
2 changed files with 41 additions and 11 deletions

View File

@ -66,6 +66,33 @@
using namespace js;
using namespace js::gc;
static bool
ValueIsLength(JSContext *cx, const Value &v, jsuint *len)
{
if (v.isInt32()) {
int32_t i = v.toInt32();
if (i < 0)
return false;
*len = i;
return true;
}
if (v.isDouble()) {
jsdouble d = v.toDouble();
if (JSDOUBLE_IS_NaN(d))
return false;
jsuint length = jsuint(d);
if (d != jsdouble(length))
return false;
*len = length;
return true;
}
return false;
}
/*
* ArrayBuffer
*
@ -724,18 +751,12 @@ class TypedArrayTemplate
// figure out the type of the first argument;
// no args is treated like an int arg of 0.
if (argc == 0 || argv[0].isInt32()) {
int32 len = 0;
if (argc != 0)
len = argv[0].toInt32();
if (len < 0) {
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
JSMSG_BAD_ARRAY_LENGTH);
return false;
}
jsuint len = 0;
bool hasLen = true;
if (argc > 0)
hasLen = ValueIsLength(cx, argv[0], &len);
if (hasLen) {
tarray = new ThisTypeArray();
if (!tarray) {
JS_ReportOutOfMemory(cx);

View File

@ -324,6 +324,15 @@ function test()
check(function() (new Int32Array(0)).BYTES_PER_ELEMENT == 4);
check(function() Int16Array.BYTES_PER_ELEMENT == Uint16Array.BYTES_PER_ELEMENT);
// test various types of args; Math.sqrt(4) is used to ensure that the
// 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));
print ("done");
reportCompare(0, TestFailCount, "typed array tests");