summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2022-12-10 00:10:01 +0000
committerLinus Groh <mail@linusgroh.de>2022-12-10 11:23:23 +0000
commitc892b1c2b6d6ddec0b951aa95c17ebc00bf6da70 (patch)
treefd32a1641fb6f656ffbd4a0d86dbca2321e4afe1 /Userland/Libraries/LibJS
parent0b35c24c9c95fd17879af9a7589dc2caefb4c995 (diff)
downloadserenity-c892b1c2b6d6ddec0b951aa95c17ebc00bf6da70.zip
LibJS: Add spec comments to ordinary_has_instance()
Diffstat (limited to 'Userland/Libraries/LibJS')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Value.cpp22
1 files changed, 20 insertions, 2 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Value.cpp b/Userland/Libraries/LibJS/Runtime/Value.cpp
index 3a71ec56b3..1389b0ca80 100644
--- a/Userland/Libraries/LibJS/Runtime/Value.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Value.cpp
@@ -2045,26 +2045,44 @@ ThrowCompletionOr<Value> instance_of(VM& vm, Value value, Value target)
// 7.3.22 OrdinaryHasInstance ( C, O ), https://tc39.es/ecma262/#sec-ordinaryhasinstance
ThrowCompletionOr<Value> ordinary_has_instance(VM& vm, Value lhs, Value rhs)
{
+ // 1. If IsCallable(C) is false, return false.
if (!rhs.is_function())
return Value(false);
+
auto& rhs_function = rhs.as_function();
+ // 2. If C has a [[BoundTargetFunction]] internal slot, then
if (is<BoundFunction>(rhs_function)) {
- auto& bound_target = static_cast<BoundFunction const&>(rhs_function);
+ auto const& bound_target = static_cast<BoundFunction const&>(rhs_function);
+
+ // a. Let BC be C.[[BoundTargetFunction]].
+ // b. Return ? InstanceofOperator(O, BC).
return instance_of(vm, lhs, Value(&bound_target.bound_target_function()));
}
+ // 3. If O is not an Object, return false.
if (!lhs.is_object())
return Value(false);
- Object* lhs_object = &lhs.as_object();
+ auto* lhs_object = &lhs.as_object();
+
+ // 4. Let P be ? Get(C, "prototype").
auto rhs_prototype = TRY(rhs_function.get(vm.names.prototype));
+
+ // 5. If P is not an Object, throw a TypeError exception.
if (!rhs_prototype.is_object())
return vm.throw_completion<TypeError>(ErrorType::InstanceOfOperatorBadPrototype, rhs.to_string_without_side_effects());
+
+ // 6. Repeat,
while (true) {
+ // a. Set O to ? O.[[GetPrototypeOf]]().
lhs_object = TRY(lhs_object->internal_get_prototype_of());
+
+ // b. If O is null, return false.
if (!lhs_object)
return Value(false);
+
+ // c. If SameValue(P, O) is true, return true.
if (same_value(rhs_prototype, lhs_object))
return Value(true);
}