/* * Copyright (c) 2020, Andreas Kling * Copyright (c) 2021-2022, Linus Groh * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include namespace JS { class NativeFunction : public FunctionObject { JS_OBJECT(NativeFunction, FunctionObject); public: static NativeFunction* create(Realm&, Function(VM&, GlobalObject&)> behaviour, i32 length, PropertyKey const& name, Optional = {}, Optional prototype = {}, Optional const& prefix = {}); static NativeFunction* create(Realm&, FlyString const& name, Function(VM&, GlobalObject&)>); NativeFunction(Function(VM&, GlobalObject&)>, Object* prototype, Realm& realm); NativeFunction(FlyString name, Function(VM&, GlobalObject&)>, Object& prototype); virtual void initialize(Realm&) override { } virtual ~NativeFunction() override = default; virtual ThrowCompletionOr internal_call(Value this_argument, MarkedVector arguments_list) override; virtual ThrowCompletionOr internal_construct(MarkedVector 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 ThrowCompletionOr call(); virtual ThrowCompletionOr construct(FunctionObject& new_target); virtual FlyString const& 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; } Optional const& initial_name() const { return m_initial_name; } void set_initial_name(Badge, FlyString initial_name) { m_initial_name = move(initial_name); } protected: NativeFunction(FlyString name, Object& prototype); explicit NativeFunction(Object& prototype); private: virtual bool is_native_function() const final { return true; } FlyString m_name; Optional m_initial_name; // [[InitialName]] Function(VM&, GlobalObject&)> m_native_function; Realm* m_realm { nullptr }; }; template<> inline bool Object::fast_is() const { return is_native_function(); } }