LibJS: Implement Number.isFinite()

Like the global isFinite() without the number coercion.
This commit is contained in:
Linus Groh 2020-04-26 18:24:58 +01:00 committed by Andreas Kling
parent b094c064d8
commit c350f5ae67
Notes: sideshowbarker 2024-07-19 07:16:24 +09:00
6 changed files with 40 additions and 2 deletions

View file

@ -144,8 +144,7 @@ Value GlobalObject::is_nan(Interpreter& interpreter)
Value GlobalObject::is_finite(Interpreter& interpreter)
{
auto value = interpreter.argument(0).to_number();
return Value(!value.is_infinity() && !value.is_nan());
return Value(interpreter.argument(0).to_number().is_finite_number());
}
}

View file

@ -40,6 +40,7 @@ namespace JS {
NumberConstructor::NumberConstructor()
: NativeFunction("Number", *interpreter().global_object().function_prototype())
{
put_native_function("isFinite", is_finite, 1);
put_native_function("isSafeInteger", is_safe_integer, 1);
put("prototype", interpreter().global_object().number_prototype());
@ -73,6 +74,11 @@ Value NumberConstructor::construct(Interpreter& interpreter)
return NumberObject::create(interpreter.global_object(), number);
}
Value NumberConstructor::is_finite(Interpreter& interpreter)
{
return Value(interpreter.argument(0).is_finite_number());
}
Value NumberConstructor::is_safe_integer(Interpreter& interpreter)
{
if (!interpreter.argument(0).is_number())

View file

@ -42,6 +42,7 @@ private:
virtual bool has_constructor() const override { return true; }
virtual const char* class_name() const override { return "NumberConstructor"; }
static Value is_finite(Interpreter&);
static Value is_safe_integer(Interpreter&);
};

View file

@ -0,0 +1,29 @@
load("test-common.js");
try {
assert(Number.isFinite.length === 1);
assert(Number.isFinite(0) === true);
assert(Number.isFinite(1.23) === true);
assert(Number.isFinite(42) === true);
assert(Number.isFinite("") === false);
assert(Number.isFinite("0") === false);
assert(Number.isFinite("42") === false);
assert(Number.isFinite(true) === false);
assert(Number.isFinite(false) === false);
assert(Number.isFinite(null) === false);
assert(Number.isFinite([]) === false);
assert(Number.isFinite() === false);
assert(Number.isFinite(NaN) === false);
assert(Number.isFinite(undefined) === false);
assert(Number.isFinite(Infinity) === false);
assert(Number.isFinite(-Infinity) === false);
assert(Number.isFinite("foo") === false);
assert(Number.isFinite({}) === false);
assert(Number.isFinite([1, 2, 3]) === false);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e.message);
}

View file

@ -2,12 +2,14 @@ load("test-common.js");
try {
assert(Number.isSafeInteger.length === 1);
assert(Number.isSafeInteger(0) === true);
assert(Number.isSafeInteger(1) === true);
assert(Number.isSafeInteger(2.0) === true);
assert(Number.isSafeInteger(42) === true);
assert(Number.isSafeInteger(Number.MAX_SAFE_INTEGER) === true);
assert(Number.isSafeInteger(Number.MIN_SAFE_INTEGER) === true);
assert(Number.isSafeInteger() === false);
assert(Number.isSafeInteger("1") === false);
assert(Number.isSafeInteger(2.1) === false);

View file

@ -4,6 +4,7 @@ try {
assert(isFinite.length === 1);
assert(isFinite(0) === true);
assert(isFinite(1.23) === true);
assert(isFinite(42) === true);
assert(isFinite("") === true);
assert(isFinite("0") === true);