summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2020-04-22 18:07:24 +0100
committerAndreas Kling <kling@serenityos.org>2020-04-23 11:03:42 +0200
commit7540203ae8c34efa58b6a9bfe369ae96b9106a28 (patch)
treea109dec624a560a5828afe301a3c1276c146ecaa
parent8ff2881b1a1aaf2225c90751b8880564e1bd9125 (diff)
downloadserenity-7540203ae8c34efa58b6a9bfe369ae96b9106a28.zip
LibJS: Add isFinite()
-rw-r--r--Libraries/LibJS/Runtime/GlobalObject.cpp7
-rw-r--r--Libraries/LibJS/Runtime/GlobalObject.h1
-rw-r--r--Libraries/LibJS/Tests/isFinite.js28
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);
+}