mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 18:17:44 +00:00
LibJS: Implement standard semantics for relational operators (#2417)
Previously, the relational operators where casting any value to double and comparing the results according to C++ semantics. This patch makes the relational operators in JS behave according to the standard specification. Since we don't have BigInt yet, the implementation doesn't take it into account. Moved PreferredType from Object to Value. Value::to_primitive now passes preferred_type to Object::to_primitive.
This commit is contained in:
parent
cbe506020b
commit
eadce65e04
5 changed files with 641 additions and 44 deletions
|
@ -29,6 +29,7 @@
|
|||
#include <AK/Assertions.h>
|
||||
#include <AK/Forward.h>
|
||||
#include <AK/LogStream.h>
|
||||
#include <AK/Types.h>
|
||||
#include <LibJS/Forward.h>
|
||||
#include <LibJS/Runtime/Symbol.h>
|
||||
|
||||
|
@ -51,6 +52,12 @@ public:
|
|||
Accessor,
|
||||
};
|
||||
|
||||
enum class PreferredType {
|
||||
Default,
|
||||
String,
|
||||
Number,
|
||||
};
|
||||
|
||||
bool is_empty() const { return m_type == Type::Empty; }
|
||||
bool is_undefined() const { return m_type == Type::Undefined; }
|
||||
bool is_null() const { return m_type == Type::Null; }
|
||||
|
@ -66,6 +73,8 @@ public:
|
|||
|
||||
bool is_nan() const { return is_number() && __builtin_isnan(as_double()); }
|
||||
bool is_infinity() const { return is_number() && __builtin_isinf(as_double()); }
|
||||
bool is_positive_infinity() const { return is_number() && __builtin_isinf_sign(as_double()) > 0; }
|
||||
bool is_negative_infinity() const { return is_number() && __builtin_isinf_sign(as_double()) < 0; }
|
||||
bool is_positive_zero() const { return is_number() && 1.0 / as_double() == __builtin_huge_val(); }
|
||||
bool is_negative_zero() const { return is_number() && 1.0 / as_double() == -__builtin_huge_val(); }
|
||||
bool is_integer() const { return is_finite_number() && (i32)as_double() == as_double(); }
|
||||
|
@ -201,7 +210,7 @@ public:
|
|||
|
||||
String to_string(Interpreter&) const;
|
||||
PrimitiveString* to_primitive_string(Interpreter&);
|
||||
Value to_primitive(Interpreter&) const;
|
||||
Value to_primitive(Interpreter&, PreferredType preferred_type = PreferredType::Default) const;
|
||||
Object* to_object(Interpreter&) const;
|
||||
Value to_number(Interpreter&) const;
|
||||
double to_double(Interpreter&) const;
|
||||
|
@ -282,6 +291,7 @@ bool strict_eq(Interpreter&, Value lhs, Value rhs);
|
|||
bool same_value(Interpreter&, Value lhs, Value rhs);
|
||||
bool same_value_zero(Interpreter&, Value lhs, Value rhs);
|
||||
bool same_value_non_numeric(Interpreter&, Value lhs, Value rhs);
|
||||
TriState abstract_relation(Interpreter& interpreter, bool left_first, Value lhs, Value rhs);
|
||||
|
||||
const LogStream& operator<<(const LogStream&, const Value&);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue