mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 22:17:45 +00:00
LibJS: Add isFinite()
This commit is contained in:
parent
8ff2881b1a
commit
7540203ae8
3 changed files with 36 additions and 0 deletions
|
@ -87,6 +87,7 @@ void GlobalObject::initialize()
|
||||||
|
|
||||||
put_native_function("gc", gc);
|
put_native_function("gc", gc);
|
||||||
put_native_function("isNaN", is_nan, 1);
|
put_native_function("isNaN", is_nan, 1);
|
||||||
|
put_native_function("isFinite", is_finite, 1);
|
||||||
|
|
||||||
// FIXME: These are read-only in ES5
|
// FIXME: These are read-only in ES5
|
||||||
put("NaN", js_nan());
|
put("NaN", js_nan());
|
||||||
|
@ -141,4 +142,10 @@ Value GlobalObject::is_nan(Interpreter& interpreter)
|
||||||
return Value(value.is_nan());
|
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 gc(Interpreter&);
|
||||||
static Value is_nan(Interpreter&);
|
static Value is_nan(Interpreter&);
|
||||||
|
static Value is_finite(Interpreter&);
|
||||||
|
|
||||||
template<typename ConstructorType>
|
template<typename ConstructorType>
|
||||||
void add_constructor(const FlyString& property_name, ConstructorType*&, Object& prototype);
|
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
Add a link
Reference in a new issue