LibJS: Use JS::HeapFunction in NativeFunction

This commit is contained in:
Aliaksandr Kalenik 2023-09-25 19:32:47 +02:00 committed by Andreas Kling
parent 0c46d79e78
commit 78f56a0908
Notes: sideshowbarker 2024-07-17 03:14:39 +09:00
2 changed files with 20 additions and 13 deletions

View file

@ -17,7 +17,7 @@ namespace JS {
// 10.3.3 CreateBuiltinFunction ( behaviour, length, name, additionalInternalSlotsList [ , realm [ , prototype [ , prefix ] ] ] ), https://tc39.es/ecma262/#sec-createbuiltinfunction
// NOTE: This doesn't consider additionalInternalSlotsList, which is rarely used, and can either be implemented using only the `function` lambda, or needs a NativeFunction subclass.
NonnullGCPtr<NativeFunction> NativeFunction::create(Realm& allocating_realm, SafeFunction<ThrowCompletionOr<Value>(VM&)> behaviour, i32 length, PropertyKey const& name, Optional<Realm*> realm, Optional<Object*> prototype, Optional<StringView> const& prefix)
NonnullGCPtr<NativeFunction> NativeFunction::create(Realm& allocating_realm, Function<ThrowCompletionOr<Value>(VM&)> behaviour, i32 length, PropertyKey const& name, Optional<Realm*> realm, Optional<Object*> prototype, Optional<StringView> const& prefix)
{
auto& vm = allocating_realm.vm();
@ -37,7 +37,7 @@ NonnullGCPtr<NativeFunction> NativeFunction::create(Realm& allocating_realm, Saf
// 7. Set func.[[Extensible]] to true.
// 8. Set func.[[Realm]] to realm.
// 9. Set func.[[InitialName]] to null.
auto function = allocating_realm.heap().allocate<NativeFunction>(allocating_realm, move(behaviour), prototype.value(), *realm.value());
auto function = allocating_realm.heap().allocate<NativeFunction>(allocating_realm, JS::create_heap_function(vm.heap(), move(behaviour)), prototype.value(), *realm.value());
// 10. Perform SetFunctionLength(func, length).
function->set_function_length(length);
@ -52,12 +52,12 @@ NonnullGCPtr<NativeFunction> NativeFunction::create(Realm& allocating_realm, Saf
return function;
}
NonnullGCPtr<NativeFunction> NativeFunction::create(Realm& realm, DeprecatedFlyString const& name, SafeFunction<ThrowCompletionOr<Value>(VM&)> function)
NonnullGCPtr<NativeFunction> NativeFunction::create(Realm& realm, DeprecatedFlyString const& name, Function<ThrowCompletionOr<Value>(VM&)> function)
{
return realm.heap().allocate<NativeFunction>(realm, name, move(function), realm.intrinsics().function_prototype());
return realm.heap().allocate<NativeFunction>(realm, name, JS::create_heap_function(realm.heap(), move(function)), realm.intrinsics().function_prototype());
}
NativeFunction::NativeFunction(SafeFunction<ThrowCompletionOr<Value>(VM&)> native_function, Object* prototype, Realm& realm)
NativeFunction::NativeFunction(JS::GCPtr<JS::HeapFunction<ThrowCompletionOr<Value>(VM&)>> native_function, Object* prototype, Realm& realm)
: FunctionObject(realm, prototype)
, m_native_function(move(native_function))
, m_realm(&realm)
@ -74,7 +74,7 @@ NativeFunction::NativeFunction(Object& prototype)
{
}
NativeFunction::NativeFunction(DeprecatedFlyString name, SafeFunction<ThrowCompletionOr<Value>(VM&)> native_function, Object& prototype)
NativeFunction::NativeFunction(DeprecatedFlyString name, JS::GCPtr<JS::HeapFunction<ThrowCompletionOr<Value>(VM&)>> native_function, Object& prototype)
: FunctionObject(prototype)
, m_name(move(name))
, m_native_function(move(native_function))
@ -219,7 +219,8 @@ ThrowCompletionOr<NonnullGCPtr<Object>> NativeFunction::internal_construct(Marke
ThrowCompletionOr<Value> NativeFunction::call()
{
return m_native_function(vm());
VERIFY(m_native_function);
return m_native_function->function()(vm());
}
ThrowCompletionOr<NonnullGCPtr<Object>> NativeFunction::construct(FunctionObject&)

View file

@ -9,10 +9,10 @@
#include <AK/Badge.h>
#include <AK/Optional.h>
#include <LibJS/Heap/HeapFunction.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/FunctionObject.h>
#include <LibJS/Runtime/PropertyKey.h>
#include <LibJS/SafeFunction.h>
namespace JS {
@ -20,8 +20,8 @@ class NativeFunction : public FunctionObject {
JS_OBJECT(NativeFunction, FunctionObject);
public:
static NonnullGCPtr<NativeFunction> create(Realm&, SafeFunction<ThrowCompletionOr<Value>(VM&)> behaviour, i32 length, PropertyKey const& name, Optional<Realm*> = {}, Optional<Object*> prototype = {}, Optional<StringView> const& prefix = {});
static NonnullGCPtr<NativeFunction> create(Realm&, DeprecatedFlyString const& name, SafeFunction<ThrowCompletionOr<Value>(VM&)>);
static NonnullGCPtr<NativeFunction> create(Realm&, Function<ThrowCompletionOr<Value>(VM&)> behaviour, i32 length, PropertyKey const& name, Optional<Realm*> = {}, Optional<Object*> prototype = {}, Optional<StringView> const& prefix = {});
static NonnullGCPtr<NativeFunction> create(Realm&, DeprecatedFlyString const& name, Function<ThrowCompletionOr<Value>(VM&)>);
virtual ~NativeFunction() override = default;
@ -43,16 +43,22 @@ public:
protected:
NativeFunction(DeprecatedFlyString name, Object& prototype);
NativeFunction(SafeFunction<ThrowCompletionOr<Value>(VM&)>, Object* prototype, Realm& realm);
NativeFunction(DeprecatedFlyString name, SafeFunction<ThrowCompletionOr<Value>(VM&)>, Object& prototype);
NativeFunction(JS::GCPtr<JS::HeapFunction<ThrowCompletionOr<Value>(VM&)>>, Object* prototype, Realm& realm);
NativeFunction(DeprecatedFlyString name, JS::GCPtr<JS::HeapFunction<ThrowCompletionOr<Value>(VM&)>>, Object& prototype);
explicit NativeFunction(Object& prototype);
virtual void visit_edges(Cell::Visitor& visitor) override
{
Base::visit_edges(visitor);
visitor.visit(m_native_function);
}
private:
virtual bool is_native_function() const final { return true; }
DeprecatedFlyString m_name;
Optional<DeprecatedFlyString> m_initial_name; // [[InitialName]]
SafeFunction<ThrowCompletionOr<Value>(VM&)> m_native_function;
JS::GCPtr<JS::HeapFunction<ThrowCompletionOr<Value>(VM&)>> m_native_function;
GCPtr<Realm> m_realm;
};