mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 15:40:19 +00:00
LibJS: Implement Number.isFinite()
Like the global isFinite() without the number coercion.
This commit is contained in:
parent
b094c064d8
commit
c350f5ae67
Notes:
sideshowbarker
2024-07-19 07:16:24 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/c350f5ae67f Pull-request: https://github.com/SerenityOS/serenity/pull/1973
6 changed files with 40 additions and 2 deletions
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -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&);
|
||||
};
|
||||
|
||||
|
|
29
Libraries/LibJS/Tests/Number.isFinite.js
Normal file
29
Libraries/LibJS/Tests/Number.isFinite.js
Normal 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);
|
||||
}
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue