mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-05-02 01:08:48 +00:00
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 :^)
53 lines
1.9 KiB
C++
53 lines
1.9 KiB
C++
/*
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <AK/Function.h>
|
|
#include <LibJS/Runtime/FunctionObject.h>
|
|
|
|
namespace JS {
|
|
|
|
class NativeFunction : public FunctionObject {
|
|
JS_OBJECT(NativeFunction, FunctionObject);
|
|
|
|
public:
|
|
static NativeFunction* create(GlobalObject&, const FlyString& name, Function<Value(VM&, GlobalObject&)>);
|
|
|
|
explicit NativeFunction(FlyString name, Function<Value(VM&, GlobalObject&)>, Object& prototype);
|
|
virtual void initialize(GlobalObject&) override { }
|
|
virtual ~NativeFunction() override;
|
|
|
|
virtual ThrowCompletionOr<Value> internal_call(Value this_argument, MarkedValueList arguments_list) override;
|
|
virtual ThrowCompletionOr<Object*> internal_construct(MarkedValueList arguments_list, FunctionObject& new_target) override;
|
|
|
|
// Used for [[Call]] / [[Construct]]'s "...result of evaluating F in a manner that conforms to the specification of F".
|
|
// Needs to be overridden by all NativeFunctions without an m_native_function.
|
|
virtual Value call();
|
|
virtual Value construct(FunctionObject& new_target);
|
|
|
|
virtual const FlyString& name() const override { return m_name; };
|
|
virtual bool is_strict_mode() const override;
|
|
virtual bool has_constructor() const override { return false; }
|
|
virtual Realm* realm() const override { return m_realm; }
|
|
|
|
protected:
|
|
NativeFunction(FlyString name, Object& prototype);
|
|
explicit NativeFunction(Object& prototype);
|
|
|
|
private:
|
|
virtual FunctionEnvironment* new_function_environment(Object* new_target) override final;
|
|
virtual bool is_native_function() const final { return true; }
|
|
|
|
FlyString m_name;
|
|
Function<Value(VM&, GlobalObject&)> m_native_function;
|
|
Realm* m_realm { nullptr };
|
|
};
|
|
|
|
template<>
|
|
inline bool Object::fast_is<NativeFunction>() const { return is_native_function(); }
|
|
|
|
}
|