
This is a continuation of the previous two commits. As allocating a JS cell already primarily involves a realm instead of a global object, and we'll need to pass one to the allocate() function itself eventually (it's bridged via the global object right now), the create() functions need to receive a realm as well. The plan is for this to be the highest-level function that actually receives a realm and passes it around, AOs on an even higher level will use the "current realm" concept via VM::current_realm() as that's what the spec assumes; passing around realms (or global objects, for that matter) on higher AO levels is pointless and unlike for allocating individual objects, which may happen outside of regular JS execution, we don't need control over the specific realm that is being used there.
63 lines
2.6 KiB
C++
63 lines
2.6 KiB
C++
/*
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
* Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <AK/Badge.h>
|
|
#include <AK/Function.h>
|
|
#include <AK/Optional.h>
|
|
#include <LibJS/Runtime/Completion.h>
|
|
#include <LibJS/Runtime/FunctionObject.h>
|
|
#include <LibJS/Runtime/PropertyKey.h>
|
|
|
|
namespace JS {
|
|
|
|
class NativeFunction : public FunctionObject {
|
|
JS_OBJECT(NativeFunction, FunctionObject);
|
|
|
|
public:
|
|
static NativeFunction* create(Realm&, Function<ThrowCompletionOr<Value>(VM&, GlobalObject&)> behaviour, i32 length, PropertyKey const& name, Optional<Realm*> = {}, Optional<Object*> prototype = {}, Optional<StringView> const& prefix = {});
|
|
static NativeFunction* create(Realm&, FlyString const& name, Function<ThrowCompletionOr<Value>(VM&, GlobalObject&)>);
|
|
|
|
NativeFunction(Function<ThrowCompletionOr<Value>(VM&, GlobalObject&)>, Object* prototype, Realm& realm);
|
|
NativeFunction(FlyString name, Function<ThrowCompletionOr<Value>(VM&, GlobalObject&)>, Object& prototype);
|
|
virtual void initialize(Realm&) override { }
|
|
virtual ~NativeFunction() override = default;
|
|
|
|
virtual ThrowCompletionOr<Value> internal_call(Value this_argument, MarkedVector<Value> arguments_list) override;
|
|
virtual ThrowCompletionOr<Object*> internal_construct(MarkedVector<Value> 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<Value> call();
|
|
virtual ThrowCompletionOr<Object*> 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<FlyString> const& initial_name() const { return m_initial_name; }
|
|
void set_initial_name(Badge<FunctionObject>, 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<FlyString> m_initial_name; // [[InitialName]]
|
|
Function<ThrowCompletionOr<Value>(VM&, GlobalObject&)> m_native_function;
|
|
Realm* m_realm { nullptr };
|
|
};
|
|
|
|
template<>
|
|
inline bool Object::fast_is<NativeFunction>() const { return is_native_function(); }
|
|
|
|
}
|