From 67e02f6ca60b110ba642bb3b325cd6fd208da582 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Tue, 25 Jan 2022 15:04:45 -0500 Subject: [PATCH] LibJS: Add templated overloads for the construct AO to create its MVL Instead of requiring callers to construct a MarkedValueList, add a variadic templated overload to construct the MVL on the caller's behalf. --- .../LibJS/Runtime/AbstractOperations.cpp | 2 +- .../LibJS/Runtime/AbstractOperations.h | 25 ++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp index 4047dc2b75..569fe838bb 100644 --- a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp @@ -74,7 +74,7 @@ ThrowCompletionOr call_impl(GlobalObject& global_object, FunctionObject& } // 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) +ThrowCompletionOr construct_impl(GlobalObject& global_object, FunctionObject& function, Optional arguments_list, FunctionObject* new_target) { // 1. If newTarget is not present, set newTarget to F. if (!new_target) diff --git a/Userland/Libraries/LibJS/Runtime/AbstractOperations.h b/Userland/Libraries/LibJS/Runtime/AbstractOperations.h index 0eabffc0ad..46ce41edb0 100644 --- a/Userland/Libraries/LibJS/Runtime/AbstractOperations.h +++ b/Userland/Libraries/LibJS/Runtime/AbstractOperations.h @@ -27,7 +27,7 @@ ThrowCompletionOr make_super_property_reference(GlobalObject&, Value 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 construct_impl(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)> = {}); ThrowCompletionOr species_constructor(GlobalObject&, Object const&, FunctionObject& default_constructor); @@ -98,6 +98,29 @@ ALWAYS_INLINE ThrowCompletionOr call(GlobalObject& global_object, Functio return call_impl(global_object, function, this_value); } +// 7.3.15 Construct ( F [ , argumentsList [ , newTarget ] ] ), https://tc39.es/ecma262/#sec-construct +template +ALWAYS_INLINE ThrowCompletionOr construct(GlobalObject& global_object, FunctionObject& function, Args&&... args) +{ + if constexpr (sizeof...(Args) > 0) { + MarkedValueList arguments_list { global_object.heap() }; + (..., arguments_list.append(forward(args))); + return construct_impl(global_object, function, move(arguments_list)); + } + + return construct_impl(global_object, function); +} + +ALWAYS_INLINE ThrowCompletionOr construct(GlobalObject& global_object, FunctionObject& function, MarkedValueList arguments_list, FunctionObject* new_target = nullptr) +{ + return construct_impl(global_object, function, move(arguments_list), new_target); +} + +ALWAYS_INLINE ThrowCompletionOr construct(GlobalObject& global_object, FunctionObject& function, Optional arguments_list, FunctionObject* new_target = nullptr) +{ + return construct_impl(global_object, function, move(arguments_list), new_target); +} + // 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)