mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-20 19:45:12 +00:00
LibJS: Add isFinite()
This commit is contained in:
parent
8ff2881b1a
commit
7540203ae8
Notes:
sideshowbarker
2024-07-19 07:23:13 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/7540203ae8c Pull-request: https://github.com/SerenityOS/serenity/pull/1923
3 changed files with 36 additions and 0 deletions
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
28
Libraries/LibJS/Tests/isFinite.js
Normal file
28
Libraries/LibJS/Tests/isFinite.js
Normal file
|
@ -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);
|
||||
}
|
Loading…
Add table
Reference in a new issue