diff --git a/Libraries/LibJS/Runtime/GlobalObject.cpp b/Libraries/LibJS/Runtime/GlobalObject.cpp index a0f30666296..0612da4669e 100644 --- a/Libraries/LibJS/Runtime/GlobalObject.cpp +++ b/Libraries/LibJS/Runtime/GlobalObject.cpp @@ -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()); } } diff --git a/Libraries/LibJS/Runtime/NumberConstructor.cpp b/Libraries/LibJS/Runtime/NumberConstructor.cpp index 1713253d0c1..d1e2d8b245b 100644 --- a/Libraries/LibJS/Runtime/NumberConstructor.cpp +++ b/Libraries/LibJS/Runtime/NumberConstructor.cpp @@ -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()) diff --git a/Libraries/LibJS/Runtime/NumberConstructor.h b/Libraries/LibJS/Runtime/NumberConstructor.h index 34d7b5d4ed5..23557009b05 100644 --- a/Libraries/LibJS/Runtime/NumberConstructor.h +++ b/Libraries/LibJS/Runtime/NumberConstructor.h @@ -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&); }; diff --git a/Libraries/LibJS/Tests/Number.isFinite.js b/Libraries/LibJS/Tests/Number.isFinite.js new file mode 100644 index 00000000000..09ae0cd9ec8 --- /dev/null +++ b/Libraries/LibJS/Tests/Number.isFinite.js @@ -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); +} diff --git a/Libraries/LibJS/Tests/Number.isSafeInteger.js b/Libraries/LibJS/Tests/Number.isSafeInteger.js index 407b999d9ae..ffa66a1a64b 100644 --- a/Libraries/LibJS/Tests/Number.isSafeInteger.js +++ b/Libraries/LibJS/Tests/Number.isSafeInteger.js @@ -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); diff --git a/Libraries/LibJS/Tests/isFinite.js b/Libraries/LibJS/Tests/isFinite.js index 095cab40180..03d4f3d8b68 100644 --- a/Libraries/LibJS/Tests/isFinite.js +++ b/Libraries/LibJS/Tests/isFinite.js @@ -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);