From d436746c952fa904e53413279134266ce965a30e Mon Sep 17 00:00:00 2001 From: mjz19910 Date: Sun, 23 Jan 2022 02:06:51 -0700 Subject: [PATCH] LibJS: Add some overloads for JS::call() and JS::call_impl() Overloads added: - JS::call for FunctionObject& - JS::call_impl for FunctionObject& --- .../LibJS/Runtime/AbstractOperations.cpp | 13 ++++++++ .../LibJS/Runtime/AbstractOperations.h | 32 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp index 3dc90bc7e4..4047dc2b75 100644 --- a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp @@ -60,6 +60,19 @@ ThrowCompletionOr call_impl(GlobalObject& global_object, Value function, return function.as_function().internal_call(this_value, move(*arguments_list)); } +ThrowCompletionOr call_impl(GlobalObject& global_object, FunctionObject& function, Value this_value, Optional arguments_list) +{ + // 1. If argumentsList is not present, set argumentsList to a new empty List. + if (!arguments_list.has_value()) + arguments_list = MarkedValueList { global_object.heap() }; + + // 2. If IsCallable(F) is false, throw a TypeError exception. + // Note: Called with a FunctionObject ref + + // 3. Return ? F.[[Call]](V, argumentsList). + return function.internal_call(this_value, move(*arguments_list)); +} + // 7.3.15 Construct ( F [ , argumentsList [ , newTarget ] ] ), https://tc39.es/ecma262/#sec-construct ThrowCompletionOr construct(GlobalObject& global_object, FunctionObject& function, Optional arguments_list, FunctionObject* new_target) { diff --git a/Userland/Libraries/LibJS/Runtime/AbstractOperations.h b/Userland/Libraries/LibJS/Runtime/AbstractOperations.h index 989cc9bfa7..98c4beb624 100644 --- a/Userland/Libraries/LibJS/Runtime/AbstractOperations.h +++ b/Userland/Libraries/LibJS/Runtime/AbstractOperations.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -25,6 +26,7 @@ Object* get_super_constructor(VM&); ThrowCompletionOr make_super_property_reference(GlobalObject&, Value actual_this, PropertyKey const&, bool strict); ThrowCompletionOr require_object_coercible(GlobalObject&, Value); ThrowCompletionOr call_impl(GlobalObject&, Value function, Value this_value, Optional = {}); +ThrowCompletionOr call_impl(GlobalObject&, FunctionObject& function, Value this_value, Optional = {}); ThrowCompletionOr construct(GlobalObject&, FunctionObject&, Optional = {}, FunctionObject* new_target = nullptr); ThrowCompletionOr length_of_array_like(GlobalObject&, Object const&); ThrowCompletionOr create_list_from_array_like(GlobalObject&, Value, Function(Value)> = {}); @@ -58,6 +60,12 @@ ALWAYS_INLINE ThrowCompletionOr call(GlobalObject& global_object, Value f return call_impl(global_object, function, this_value, move(arguments_list)); } +template +ALWAYS_INLINE ThrowCompletionOr call(GlobalObject& global_object, Value function, Value this_value, Optional arguments_list) +{ + return call_impl(global_object, function, this_value, move(arguments_list)); +} + template ALWAYS_INLINE ThrowCompletionOr call(GlobalObject& global_object, Value function, Value this_value, Args... args) { @@ -70,6 +78,30 @@ ALWAYS_INLINE ThrowCompletionOr call(GlobalObject& global_object, Value f return call_impl(global_object, function, this_value); } +template +ALWAYS_INLINE ThrowCompletionOr call(GlobalObject& global_object, FunctionObject& function, Value this_value, MarkedValueList arguments_list) +{ + return call_impl(global_object, function, this_value, move(arguments_list)); +} + +template +ALWAYS_INLINE ThrowCompletionOr call(GlobalObject& global_object, FunctionObject& function, Value this_value, Optional arguments_list) +{ + return call_impl(global_object, function, this_value, move(arguments_list)); +} + +template +ALWAYS_INLINE ThrowCompletionOr call(GlobalObject& global_object, FunctionObject& function, Value this_value, Args... args) +{ + if constexpr (sizeof...(Args) > 0) { + MarkedValueList arguments_list { global_object.heap() }; + (..., arguments_list.append(move(args))); + return call_impl(global_object, function, this_value, move(arguments_list)); + } + + return call_impl(global_object, function, this_value); +} + // 10.1.13 OrdinaryCreateFromConstructor ( constructor, intrinsicDefaultProto [ , internalSlotsList ] ), https://tc39.es/ecma262/#sec-ordinarycreatefromconstructor template ThrowCompletionOr ordinary_create_from_constructor(GlobalObject& global_object, FunctionObject const& constructor, Object* (GlobalObject::*intrinsic_default_prototype)(), Args&&... args)