mirror of
https://github.com/RGBCube/serenity
synced 2025-05-28 08:35:09 +00:00
LibJS: Implement the GetMethod() abstract operation as a Value method
This was a standalone function previously (get_method()), but instead of passing a Value to it, we can just make it a method. Also add spec step comments and fix the receiver value by using GetV().
This commit is contained in:
parent
31f5797e89
commit
337ad6d15c
8 changed files with 48 additions and 41 deletions
|
@ -413,7 +413,7 @@ Value Value::to_primitive(GlobalObject& global_object, PreferredType preferred_t
|
|||
};
|
||||
if (is_object()) {
|
||||
auto& vm = global_object.vm();
|
||||
auto to_primitive_method = get_method(global_object, *this, *vm.well_known_symbol_to_primitive());
|
||||
auto to_primitive_method = get_method(global_object, *vm.well_known_symbol_to_primitive());
|
||||
if (vm.exception())
|
||||
return {};
|
||||
if (to_primitive_method) {
|
||||
|
@ -805,6 +805,32 @@ Value Value::get(GlobalObject& global_object, PropertyName const& property_name)
|
|||
return object->get(property_name, *this);
|
||||
}
|
||||
|
||||
// 7.3.10 GetMethod ( V, P ), https://tc39.es/ecma262/#sec-getmethod
|
||||
Function* Value::get_method(GlobalObject& global_object, PropertyName const& property_name) const
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
||||
// 1. Assert: IsPropertyKey(P) is true.
|
||||
|
||||
// 2. Let func be ? GetV(V, P).
|
||||
auto function = get(global_object, property_name).value_or(js_undefined());
|
||||
if (vm.exception())
|
||||
return nullptr;
|
||||
|
||||
// 3. If func is either undefined or null, return undefined.
|
||||
if (function.is_nullish())
|
||||
return nullptr;
|
||||
|
||||
// 4. If IsCallable(func) is false, throw a TypeError exception.
|
||||
if (!function.is_function()) {
|
||||
vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, function.to_string_without_side_effects());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// 5. Return func.
|
||||
return &function.as_function();
|
||||
}
|
||||
|
||||
// 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators
|
||||
Value greater_than(GlobalObject& global_object, Value lhs, Value rhs)
|
||||
{
|
||||
|
@ -1204,7 +1230,7 @@ Value instance_of(GlobalObject& global_object, Value lhs, Value rhs)
|
|||
vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, rhs.to_string_without_side_effects());
|
||||
return {};
|
||||
}
|
||||
auto has_instance_method = get_method(global_object, Value(&rhs.as_object()), *vm.well_known_symbol_has_instance());
|
||||
auto has_instance_method = rhs.get_method(global_object, *vm.well_known_symbol_has_instance());
|
||||
if (vm.exception())
|
||||
return {};
|
||||
if (has_instance_method) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue