Skip to content

Commit

Permalink
LibJS: Convert is_less_than() to ThrowCompletionOr
Browse files Browse the repository at this point in the history
  • Loading branch information
IdanHo authored and linusg committed Oct 18, 2021
1 parent c15a3b0 commit b5e2841
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Userland/Libraries/LibJS/Runtime/ArrayPrototype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -982,9 +982,9 @@ static void array_merge_sort(VM& vm, GlobalObject& global_object, FunctionObject

// Because they are called with primitive strings, these is_less_than calls
// should never result in a VM exception.
auto x_lt_y_relation = is_less_than(global_object, true, x_string_value, y_string_value);
auto x_lt_y_relation = MUST(is_less_than(global_object, true, x_string_value, y_string_value));
VERIFY(x_lt_y_relation != TriState::Unknown);
auto y_lt_x_relation = is_less_than(global_object, true, y_string_value, x_string_value);
auto y_lt_x_relation = MUST(is_less_than(global_object, true, y_string_value, x_string_value));
VERIFY(y_lt_x_relation != TriState::Unknown);

if (x_lt_y_relation == TriState::True) {
Expand Down
22 changes: 11 additions & 11 deletions Userland/Libraries/LibJS/Runtime/Value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ ThrowCompletionOr<FunctionObject*> Value::get_method(GlobalObject& global_object
// 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators
Value greater_than(GlobalObject& global_object, Value lhs, Value rhs)
{
TriState relation = is_less_than(global_object, false, lhs, rhs);
TriState relation = TRY_OR_DISCARD(is_less_than(global_object, false, lhs, rhs));
if (relation == TriState::Unknown)
return Value(false);
return Value(relation == TriState::True);
Expand All @@ -780,7 +780,7 @@ Value greater_than(GlobalObject& global_object, Value lhs, Value rhs)
// 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators
Value greater_than_equals(GlobalObject& global_object, Value lhs, Value rhs)
{
TriState relation = is_less_than(global_object, true, lhs, rhs);
TriState relation = TRY_OR_DISCARD(is_less_than(global_object, true, lhs, rhs));
if (relation == TriState::Unknown || relation == TriState::True)
return Value(false);
return Value(true);
Expand All @@ -789,7 +789,7 @@ Value greater_than_equals(GlobalObject& global_object, Value lhs, Value rhs)
// 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators
Value less_than(GlobalObject& global_object, Value lhs, Value rhs)
{
TriState relation = is_less_than(global_object, true, lhs, rhs);
TriState relation = TRY_OR_DISCARD(is_less_than(global_object, true, lhs, rhs));
if (relation == TriState::Unknown)
return Value(false);
return Value(relation == TriState::True);
Expand All @@ -798,7 +798,7 @@ Value less_than(GlobalObject& global_object, Value lhs, Value rhs)
// 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators
Value less_than_equals(GlobalObject& global_object, Value lhs, Value rhs)
{
TriState relation = is_less_than(global_object, false, lhs, rhs);
TriState relation = TRY_OR_DISCARD(is_less_than(global_object, false, lhs, rhs));
if (relation == TriState::Unknown || relation == TriState::True)
return Value(false);
return Value(true);
Expand Down Expand Up @@ -1376,17 +1376,17 @@ bool is_loosely_equal(GlobalObject& global_object, Value lhs, Value rhs)
}

// 7.2.13 IsLessThan ( x, y, LeftFirst ), https://tc39.es/ecma262/#sec-islessthan
TriState is_less_than(GlobalObject& global_object, bool left_first, Value lhs, Value rhs)
ThrowCompletionOr<TriState> is_less_than(GlobalObject& global_object, bool left_first, Value lhs, Value rhs)
{
Value x_primitive;
Value y_primitive;

if (left_first) {
x_primitive = TRY_OR_DISCARD(lhs.to_primitive(global_object, Value::PreferredType::Number));
y_primitive = TRY_OR_DISCARD(rhs.to_primitive(global_object, Value::PreferredType::Number));
x_primitive = TRY(lhs.to_primitive(global_object, Value::PreferredType::Number));
y_primitive = TRY(rhs.to_primitive(global_object, Value::PreferredType::Number));
} else {
y_primitive = TRY_OR_DISCARD(lhs.to_primitive(global_object, Value::PreferredType::Number));
x_primitive = TRY_OR_DISCARD(rhs.to_primitive(global_object, Value::PreferredType::Number));
y_primitive = TRY(lhs.to_primitive(global_object, Value::PreferredType::Number));
x_primitive = TRY(rhs.to_primitive(global_object, Value::PreferredType::Number));
}

if (x_primitive.is_string() && y_primitive.is_string()) {
Expand Down Expand Up @@ -1435,8 +1435,8 @@ TriState is_less_than(GlobalObject& global_object, bool left_first, Value lhs, V
return TriState::False;
}

auto x_numeric = TRY_OR_DISCARD(x_primitive.to_numeric(global_object));
auto y_numeric = TRY_OR_DISCARD(y_primitive.to_numeric(global_object));
auto x_numeric = TRY(x_primitive.to_numeric(global_object));
auto y_numeric = TRY(y_primitive.to_numeric(global_object));

if (x_numeric.is_nan() || y_numeric.is_nan())
return TriState::Unknown;
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibJS/Runtime/Value.h
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ bool is_strictly_equal(Value lhs, Value rhs);
bool same_value(Value lhs, Value rhs);
bool same_value_zero(Value lhs, Value rhs);
bool same_value_non_numeric(Value lhs, Value rhs);
TriState is_less_than(GlobalObject&, bool left_first, Value lhs, Value rhs);
ThrowCompletionOr<TriState> is_less_than(GlobalObject&, bool left_first, Value lhs, Value rhs);

inline bool Value::operator==(Value const& value) const { return same_value(*this, value); }

Expand Down

0 comments on commit b5e2841

Please sign in to comment.