diff options
-rw-r--r-- | Libraries/LibJS/Runtime/GlobalObject.cpp | 7 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/GlobalObject.h | 1 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/isFinite.js | 28 |
3 files changed, 36 insertions, 0 deletions
diff --git a/Libraries/LibJS/Runtime/GlobalObject.cpp b/Libraries/LibJS/Runtime/GlobalObject.cpp index 7a1f062675..a0f3066629 100644 --- a/Libraries/LibJS/Runtime/GlobalObject.cpp +++ b/Libraries/LibJS/Runtime/GlobalObject.cpp @@ -87,6 +87,7 @@ void GlobalObject::initialize() put_native_function("gc", gc); put_native_function("isNaN", is_nan, 1); + put_native_function("isFinite", is_finite, 1); // FIXME: These are read-only in ES5 put("NaN", js_nan()); @@ -141,4 +142,10 @@ Value GlobalObject::is_nan(Interpreter& interpreter) return Value(value.is_nan()); } +Value GlobalObject::is_finite(Interpreter& interpreter) +{ + auto value = interpreter.argument(0).to_number(); + return Value(!value.is_infinity() && !value.is_nan()); +} + } diff --git a/Libraries/LibJS/Runtime/GlobalObject.h b/Libraries/LibJS/Runtime/GlobalObject.h index 52ca09a3a0..d28383fac9 100644 --- a/Libraries/LibJS/Runtime/GlobalObject.h +++ b/Libraries/LibJS/Runtime/GlobalObject.h @@ -53,6 +53,7 @@ private: static Value gc(Interpreter&); static Value is_nan(Interpreter&); + static Value is_finite(Interpreter&); template<typename ConstructorType> void add_constructor(const FlyString& property_name, ConstructorType*&, Object& prototype); diff --git a/Libraries/LibJS/Tests/isFinite.js b/Libraries/LibJS/Tests/isFinite.js new file mode 100644 index 0000000000..095cab4018 --- /dev/null +++ b/Libraries/LibJS/Tests/isFinite.js @@ -0,0 +1,28 @@ +load("test-common.js"); + +try { + assert(isFinite.length === 1); + + assert(isFinite(0) === true); + assert(isFinite(42) === true); + assert(isFinite("") === true); + assert(isFinite("0") === true); + assert(isFinite("42") === true); + assert(isFinite(true) === true); + assert(isFinite(false) === true); + assert(isFinite(null) === true); + assert(isFinite([]) === true); + + assert(isFinite() === false); + assert(isFinite(NaN) === false); + assert(isFinite(undefined) === false); + assert(isFinite(Infinity) === false); + assert(isFinite(-Infinity) === false); + assert(isFinite("foo") === false); + assert(isFinite({}) === false); + assert(isFinite([1, 2, 3]) === false); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e.message); +} |