summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime/FunctionObject.h
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-10-08 20:37:21 +0100
committerLinus Groh <mail@linusgroh.de>2021-10-09 14:29:20 +0100
commitcf168fac5002a1f66559dcf96719368d167596bc (patch)
treeed32522fdaed63d35cb2ac76b7b22bcde9c24fc4 /Userland/Libraries/LibJS/Runtime/FunctionObject.h
parent58c34012dd5fd48e2e3800d6f374ecc82e91da1e (diff)
downloadserenity-cf168fac5002a1f66559dcf96719368d167596bc.zip
LibJS: Implement [[Call]] and [[Construct]] internal slots properly
This patch implements: - Spec compliant [[Call]] and [[Construct]] internal slots, as virtual FunctionObject::internal_{call,construct}(). These effectively replace the old virtual FunctionObject::{call,construct}(), but with several advantages: - Clear and consistent naming, following the object internal methods - Use of completions - internal_construct() returns an Object, and not Value! This has been a source of confusion for a long time, since in the spec there's always an Object returned but the Value return type in LibJS meant that this could not be fully trusted and something could screw you over. - Arguments are passed explicitly in form of a MarkedValueList, allowing manipulation (BoundFunction). We still put them on the execution context as a lot of code depends on it (VM::arguments()), but not from the Call() / Construct() AOs anymore, which now allows for bypassing them and invoking [[Call]] / [[Construct]] directly. Nothing but Call() / Construct() themselves do that at the moment, but future additions to ECMA262 or already existing web specs might. - Spec compliant, standalone Call() and Construct() AOs: currently the closest we have is VM::{call,construct}(), but those try to cater to all the different function object subclasses at once, resulting in a horrible mess and calling AOs with functions they should never be called with; most prominently PrepareForOrdinaryCall and OrdinaryCallBindThis, which are only for ECMAScriptFunctionObject. As a result this also contains an implicit optimization: we no longer need to create a new function environment for NativeFunctions - which, worth mentioning, is what started this whole crusade in the first place :^)
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime/FunctionObject.h')
-rw-r--r--Userland/Libraries/LibJS/Runtime/FunctionObject.h7
1 files changed, 5 insertions, 2 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/FunctionObject.h b/Userland/Libraries/LibJS/Runtime/FunctionObject.h
index 659f6373b6..6746b0f670 100644
--- a/Userland/Libraries/LibJS/Runtime/FunctionObject.h
+++ b/Userland/Libraries/LibJS/Runtime/FunctionObject.h
@@ -18,8 +18,11 @@ public:
virtual ~FunctionObject();
virtual void initialize(GlobalObject&) override { }
- virtual Value call() = 0;
- virtual Value construct(FunctionObject& new_target) = 0;
+ // Table 7: Additional Essential Internal Methods of Function Objects, https://tc39.es/ecma262/#table-additional-essential-internal-methods-of-function-objects
+
+ virtual ThrowCompletionOr<Value> internal_call(Value this_argument, MarkedValueList arguments_list) = 0;
+ virtual ThrowCompletionOr<Object*> internal_construct([[maybe_unused]] MarkedValueList arguments_list, [[maybe_unused]] FunctionObject& new_target) { VERIFY_NOT_REACHED(); }
+
virtual const FlyString& name() const = 0;
virtual FunctionEnvironment* new_function_environment(Object* new_target) = 0;