Bug 761480: add Number.isFinite. r=jorendorff.

This commit is contained in:
Benjamin Peterson 2012-06-06 21:50:20 -05:00
parent 95ee5094b8
commit 154c5ff8a3
2 changed files with 30 additions and 0 deletions

View File

@ -0,0 +1,15 @@
assertEq(Number.isFinite(Infinity), false);
assertEq(Number.isFinite(-Infinity), false);
assertEq(Number.isFinite(NaN), false);
assertEq(Number.isFinite(4), true);
assertEq(Number.isFinite(4.5), true);
assertEq(Number.isFinite("hi"), false);
assertEq(Number.isFinite("1.3"), false);
assertEq(Number.isFinite("51"), false);
assertEq(Number.isFinite(0), true);
assertEq(Number.isFinite(-0), true);
assertEq(Number.isFinite({valueOf: function () { return 3; }}), false);
assertEq(Number.isFinite({valueOf: function () { return 0/0; }}), false);
assertEq(Number.isFinite({ valueOf: function() { throw 17; } }), false);
assertEq(Number.isFinite({ toString: function() { throw 17; } }), false);
assertEq(Number.isFinite({ valueOf: function() { throw 17; }, toString: function() { throw 42; } }), false);

View File

@ -850,8 +850,23 @@ Number_isNaN(JSContext *cx, unsigned argc, Value *vp)
return true;
}
// ES6 draft ES6 15.7.3.11
static JSBool
Number_isFinite(JSContext *cx, unsigned argc, Value *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
if (args.length() < 1 || !args[0].isNumber()) {
args.rval().setBoolean(false);
return true;
}
args.rval().setBoolean(args[0].isInt32() ||
MOZ_DOUBLE_IS_FINITE(args[0].toDouble()));
return true;
}
static JSFunctionSpec number_static_methods[] = {
JS_FN("isFinite", Number_isFinite, 1, 0),
JS_FN("isNaN", Number_isNaN, 1, 0),
JS_FS_END
};