diff --git a/Userland/Libraries/LibJS/Forward.h b/Userland/Libraries/LibJS/Forward.h index 7f9f6ec86e..0eb6ce6efd 100644 --- a/Userland/Libraries/LibJS/Forward.h +++ b/Userland/Libraries/LibJS/Forward.h @@ -139,6 +139,7 @@ class MarkedValueList; class NativeFunction; class NativeProperty; class PrimitiveString; +class PropertyName; class Reference; class ScopeNode; class ScopeObject; diff --git a/Userland/Libraries/LibJS/Runtime/Value.cpp b/Userland/Libraries/LibJS/Runtime/Value.cpp index 8362c832df..224670d97f 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.cpp +++ b/Userland/Libraries/LibJS/Runtime/Value.cpp @@ -1231,6 +1231,26 @@ TriState abstract_relation(GlobalObject& global_object, bool left_first, Value l return TriState::False; } +Function* get_method(GlobalObject& global_object, Value value, const PropertyName& property_name) +{ + // 7.3.10 GetMethod, https://tc39.es/ecma262/#sec-getmethod + + auto& vm = global_object.vm(); + auto* object = value.to_object(global_object); + if (vm.exception()) + return nullptr; + auto property_value = object->get(property_name); + if (vm.exception()) + return nullptr; + if (property_value.is_empty() || property_value.is_nullish()) + return nullptr; + if (!property_value.is_function()) { + vm.throw_exception(global_object, ErrorType::NotAFunction, property_value.to_string_without_side_effects()); + return nullptr; + } + return &property_value.as_function(); +} + size_t length_of_array_like(GlobalObject& global_object, const Object& object) { // 7.3.18 LengthOfArrayLike, https://tc39.es/ecma262/#sec-lengthofarraylike diff --git a/Userland/Libraries/LibJS/Runtime/Value.h b/Userland/Libraries/LibJS/Runtime/Value.h index 781d996c5d..3914232306 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.h +++ b/Userland/Libraries/LibJS/Runtime/Value.h @@ -340,6 +340,7 @@ 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 abstract_relation(GlobalObject&, bool left_first, Value lhs, Value rhs); +Function* get_method(GlobalObject& global_object, Value, const PropertyName&); size_t length_of_array_like(GlobalObject&, const Object&); }