mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 07:08:10 +00:00
LibJS: Implement Infinity
This commit is contained in:
parent
40b3203941
commit
543c6e00db
5 changed files with 40 additions and 0 deletions
|
@ -18,7 +18,10 @@ GlobalObject::GlobalObject()
|
||||||
put_native_function("gc", gc);
|
put_native_function("gc", gc);
|
||||||
put_native_function("isNaN", is_nan);
|
put_native_function("isNaN", is_nan);
|
||||||
|
|
||||||
|
// FIXME: These are read-only in ES5
|
||||||
put("NaN", js_nan());
|
put("NaN", js_nan());
|
||||||
|
put("Infinity", js_infinity());
|
||||||
|
|
||||||
put("console", heap().allocate<ConsoleObject>());
|
put("console", heap().allocate<ConsoleObject>());
|
||||||
put("Date", heap().allocate<DateConstructor>());
|
put("Date", heap().allocate<DateConstructor>());
|
||||||
put("Error", heap().allocate<ErrorConstructor>());
|
put("Error", heap().allocate<ErrorConstructor>());
|
||||||
|
|
|
@ -57,6 +57,9 @@ String Value::to_string() const
|
||||||
if (is_nan())
|
if (is_nan())
|
||||||
return "NaN";
|
return "NaN";
|
||||||
|
|
||||||
|
if (is_infinity())
|
||||||
|
return as_double() < 0 ? "-Infinity" : "Infinity";
|
||||||
|
|
||||||
// FIXME: This needs improvement.
|
// FIXME: This needs improvement.
|
||||||
if ((double)to_i32() == as_double())
|
if ((double)to_i32() == as_double())
|
||||||
return String::number(to_i32());
|
return String::number(to_i32());
|
||||||
|
|
|
@ -54,6 +54,7 @@ public:
|
||||||
bool is_array() const;
|
bool is_array() const;
|
||||||
|
|
||||||
bool is_nan() const { return is_number() && __builtin_isnan(as_double()); }
|
bool is_nan() const { return is_number() && __builtin_isnan(as_double()); }
|
||||||
|
bool is_infinity() const { return is_number() && __builtin_isinf(as_double()); }
|
||||||
|
|
||||||
Value()
|
Value()
|
||||||
: m_type(Type::Undefined)
|
: m_type(Type::Undefined)
|
||||||
|
@ -173,6 +174,11 @@ inline Value js_nan()
|
||||||
return Value(__builtin_nan(""));
|
return Value(__builtin_nan(""));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline Value js_infinity()
|
||||||
|
{
|
||||||
|
return Value(__builtin_huge_val());
|
||||||
|
}
|
||||||
|
|
||||||
Value greater_than(Value lhs, Value rhs);
|
Value greater_than(Value lhs, Value rhs);
|
||||||
Value greater_than_equals(Value lhs, Value rhs);
|
Value greater_than_equals(Value lhs, Value rhs);
|
||||||
Value less_than(Value lhs, Value rhs);
|
Value less_than(Value lhs, Value rhs);
|
||||||
|
|
27
Libraries/LibJS/Tests/Infinity-basic.js
Normal file
27
Libraries/LibJS/Tests/Infinity-basic.js
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
function assert(x) { if (!x) throw 1; }
|
||||||
|
|
||||||
|
try {
|
||||||
|
assert(Infinity + "" === "Infinity");
|
||||||
|
assert(-Infinity + "" === "-Infinity");
|
||||||
|
assert(Infinity === Infinity);
|
||||||
|
assert(Infinity - 1 === Infinity);
|
||||||
|
assert(Infinity + 1 === Infinity);
|
||||||
|
assert(-Infinity === -Infinity);
|
||||||
|
assert(-Infinity - 1 === -Infinity);
|
||||||
|
assert(-Infinity + 1 === -Infinity);
|
||||||
|
assert(1 / Infinity === 0);
|
||||||
|
assert(1 / -Infinity === 0);
|
||||||
|
assert(1 / 0 === Infinity);
|
||||||
|
assert(-1 / 0 === -Infinity);
|
||||||
|
assert(-100 < Infinity);
|
||||||
|
assert(0 < Infinity);
|
||||||
|
assert(100 < Infinity);
|
||||||
|
assert(-Infinity < Infinity);
|
||||||
|
assert(-100 > -Infinity);
|
||||||
|
assert(0 > -Infinity);
|
||||||
|
assert(100 > -Infinity);
|
||||||
|
assert(Infinity > -Infinity);
|
||||||
|
console.log("PASS");
|
||||||
|
} catch (e) {
|
||||||
|
console.log("FAIL: " + e);
|
||||||
|
}
|
|
@ -11,6 +11,7 @@ try {
|
||||||
assert(isNaN(0) === false);
|
assert(isNaN(0) === false);
|
||||||
assert(isNaN(undefined) === true);
|
assert(isNaN(undefined) === true);
|
||||||
assert(isNaN(null) === false);
|
assert(isNaN(null) === false);
|
||||||
|
assert(isNaN(Infinity) === false);
|
||||||
console.log("PASS");
|
console.log("PASS");
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log("FAIL: " + e);
|
console.log("FAIL: " + e);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue