Skip to content

Commit

Permalink
LibJS: Add some overloads for JS::call() and JS::call_impl()
Browse files Browse the repository at this point in the history
Overloads added:
- JS::call for FunctionObject&
- JS::call_impl for FunctionObject&
  • Loading branch information
mjz19910 authored and linusg committed Jan 23, 2022
1 parent cf45370 commit d436746
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ ThrowCompletionOr<Value> call_impl(GlobalObject& global_object, Value function,
return function.as_function().internal_call(this_value, move(*arguments_list));
}

ThrowCompletionOr<Value> call_impl(GlobalObject& global_object, FunctionObject& function, Value this_value, Optional<MarkedValueList> 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<Object*> construct(GlobalObject& global_object, FunctionObject& function, Optional<MarkedValueList> arguments_list, FunctionObject* new_target)
{
Expand Down
32 changes: 32 additions & 0 deletions Userland/Libraries/LibJS/Runtime/AbstractOperations.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <LibCrypto/Forward.h>
#include <LibJS/AST.h>
#include <LibJS/Forward.h>
#include <LibJS/Runtime/FunctionObject.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/PrivateEnvironment.h>
#include <LibJS/Runtime/Value.h>
Expand All @@ -25,6 +26,7 @@ Object* get_super_constructor(VM&);
ThrowCompletionOr<Reference> make_super_property_reference(GlobalObject&, Value actual_this, PropertyKey const&, bool strict);
ThrowCompletionOr<Value> require_object_coercible(GlobalObject&, Value);
ThrowCompletionOr<Value> call_impl(GlobalObject&, Value function, Value this_value, Optional<MarkedValueList> = {});
ThrowCompletionOr<Value> call_impl(GlobalObject&, FunctionObject& function, Value this_value, Optional<MarkedValueList> = {});
ThrowCompletionOr<Object*> construct(GlobalObject&, FunctionObject&, Optional<MarkedValueList> = {}, FunctionObject* new_target = nullptr);
ThrowCompletionOr<size_t> length_of_array_like(GlobalObject&, Object const&);
ThrowCompletionOr<MarkedValueList> create_list_from_array_like(GlobalObject&, Value, Function<ThrowCompletionOr<void>(Value)> = {});
Expand Down Expand Up @@ -58,6 +60,12 @@ ALWAYS_INLINE ThrowCompletionOr<Value> call(GlobalObject& global_object, Value f
return call_impl(global_object, function, this_value, move(arguments_list));
}

template<typename... Args>
ALWAYS_INLINE ThrowCompletionOr<Value> call(GlobalObject& global_object, Value function, Value this_value, Optional<MarkedValueList> arguments_list)
{
return call_impl(global_object, function, this_value, move(arguments_list));
}

template<typename... Args>
ALWAYS_INLINE ThrowCompletionOr<Value> call(GlobalObject& global_object, Value function, Value this_value, Args... args)
{
Expand All @@ -70,6 +78,30 @@ ALWAYS_INLINE ThrowCompletionOr<Value> call(GlobalObject& global_object, Value f
return call_impl(global_object, function, this_value);
}

template<typename... Args>
ALWAYS_INLINE ThrowCompletionOr<Value> call(GlobalObject& global_object, FunctionObject& function, Value this_value, MarkedValueList arguments_list)
{
return call_impl(global_object, function, this_value, move(arguments_list));
}

template<typename... Args>
ALWAYS_INLINE ThrowCompletionOr<Value> call(GlobalObject& global_object, FunctionObject& function, Value this_value, Optional<MarkedValueList> arguments_list)
{
return call_impl(global_object, function, this_value, move(arguments_list));
}

template<typename... Args>
ALWAYS_INLINE ThrowCompletionOr<Value> 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<typename T, typename... Args>
ThrowCompletionOr<T*> ordinary_create_from_constructor(GlobalObject& global_object, FunctionObject const& constructor, Object* (GlobalObject::*intrinsic_default_prototype)(), Args&&... args)
Expand Down

0 comments on commit d436746

Please sign in to comment.