From 27ed1f6d5eba6411e5969df8b1719c39f4144c06 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sat, 10 Dec 2022 00:04:18 +0000 Subject: [PATCH] LibJS: Add spec comments to greater_than_equals() --- Userland/Libraries/LibJS/Runtime/Value.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibJS/Runtime/Value.cpp b/Userland/Libraries/LibJS/Runtime/Value.cpp index bbd189b456..c2a4581a6b 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.cpp +++ b/Userland/Libraries/LibJS/Runtime/Value.cpp @@ -1236,12 +1236,23 @@ ThrowCompletionOr greater_than(VM& vm, Value lhs, Value rhs) } // 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators +// RelationalExpression : RelationalExpression >= ShiftExpression ThrowCompletionOr greater_than_equals(VM& vm, Value lhs, Value rhs) { + // 1. Let lref be ? Evaluation of RelationalExpression. + // 2. Let lval be ? GetValue(lref). + // 3. Let rref be ? Evaluation of ShiftExpression. + // 4. Let rval be ? GetValue(rref). + // NOTE: This is handled in the AST or Bytecode interpreter. + + // OPTIMIZATION: If both values are i32, we can do a direct comparison without calling into IsLessThan. if (lhs.is_int32() && rhs.is_int32()) return lhs.as_i32() >= rhs.as_i32(); - TriState relation = TRY(is_less_than(vm, lhs, rhs, true)); + // 5. Let r be ? IsLessThan(lval, rval, true). + auto relation = TRY(is_less_than(vm, lhs, rhs, true)); + + // 6. If r is true or undefined, return false. Otherwise, return true. if (relation == TriState::Unknown || relation == TriState::True) return Value(false); return Value(true);