From 78f56a0908c48fa3323a8f211681b1c72da180ce Mon Sep 17 00:00:00 2001 From: Aliaksandr Kalenik Date: Mon, 25 Sep 2023 19:32:47 +0200 Subject: [PATCH] LibJS: Use JS::HeapFunction in NativeFunction --- .../Libraries/LibJS/Runtime/NativeFunction.cpp | 15 ++++++++------- .../Libraries/LibJS/Runtime/NativeFunction.h | 18 ++++++++++++------ 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/NativeFunction.cpp b/Userland/Libraries/LibJS/Runtime/NativeFunction.cpp index b3cc81d928a..fe89e713de1 100644 --- a/Userland/Libraries/LibJS/Runtime/NativeFunction.cpp +++ b/Userland/Libraries/LibJS/Runtime/NativeFunction.cpp @@ -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::create(Realm& allocating_realm, SafeFunction(VM&)> behaviour, i32 length, PropertyKey const& name, Optional realm, Optional prototype, Optional const& prefix) +NonnullGCPtr NativeFunction::create(Realm& allocating_realm, Function(VM&)> behaviour, i32 length, PropertyKey const& name, Optional realm, Optional prototype, Optional const& prefix) { auto& vm = allocating_realm.vm(); @@ -37,7 +37,7 @@ NonnullGCPtr 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(allocating_realm, move(behaviour), prototype.value(), *realm.value()); + auto function = allocating_realm.heap().allocate(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::create(Realm& allocating_realm, Saf return function; } -NonnullGCPtr NativeFunction::create(Realm& realm, DeprecatedFlyString const& name, SafeFunction(VM&)> function) +NonnullGCPtr NativeFunction::create(Realm& realm, DeprecatedFlyString const& name, Function(VM&)> function) { - return realm.heap().allocate(realm, name, move(function), realm.intrinsics().function_prototype()); + return realm.heap().allocate(realm, name, JS::create_heap_function(realm.heap(), move(function)), realm.intrinsics().function_prototype()); } -NativeFunction::NativeFunction(SafeFunction(VM&)> native_function, Object* prototype, Realm& realm) +NativeFunction::NativeFunction(JS::GCPtr(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(VM&)> native_function, Object& prototype) +NativeFunction::NativeFunction(DeprecatedFlyString name, JS::GCPtr(VM&)>> native_function, Object& prototype) : FunctionObject(prototype) , m_name(move(name)) , m_native_function(move(native_function)) @@ -219,7 +219,8 @@ ThrowCompletionOr> NativeFunction::internal_construct(Marke ThrowCompletionOr NativeFunction::call() { - return m_native_function(vm()); + VERIFY(m_native_function); + return m_native_function->function()(vm()); } ThrowCompletionOr> NativeFunction::construct(FunctionObject&) diff --git a/Userland/Libraries/LibJS/Runtime/NativeFunction.h b/Userland/Libraries/LibJS/Runtime/NativeFunction.h index d4eb48596fb..a402a63857c 100644 --- a/Userland/Libraries/LibJS/Runtime/NativeFunction.h +++ b/Userland/Libraries/LibJS/Runtime/NativeFunction.h @@ -9,10 +9,10 @@ #include #include +#include #include #include #include -#include namespace JS { @@ -20,8 +20,8 @@ class NativeFunction : public FunctionObject { JS_OBJECT(NativeFunction, FunctionObject); public: - static NonnullGCPtr create(Realm&, SafeFunction(VM&)> behaviour, i32 length, PropertyKey const& name, Optional = {}, Optional prototype = {}, Optional const& prefix = {}); - static NonnullGCPtr create(Realm&, DeprecatedFlyString const& name, SafeFunction(VM&)>); + static NonnullGCPtr create(Realm&, Function(VM&)> behaviour, i32 length, PropertyKey const& name, Optional = {}, Optional prototype = {}, Optional const& prefix = {}); + static NonnullGCPtr create(Realm&, DeprecatedFlyString const& name, Function(VM&)>); virtual ~NativeFunction() override = default; @@ -43,16 +43,22 @@ public: protected: NativeFunction(DeprecatedFlyString name, Object& prototype); - NativeFunction(SafeFunction(VM&)>, Object* prototype, Realm& realm); - NativeFunction(DeprecatedFlyString name, SafeFunction(VM&)>, Object& prototype); + NativeFunction(JS::GCPtr(VM&)>>, Object* prototype, Realm& realm); + NativeFunction(DeprecatedFlyString name, JS::GCPtr(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 m_initial_name; // [[InitialName]] - SafeFunction(VM&)> m_native_function; + JS::GCPtr(VM&)>> m_native_function; GCPtr m_realm; };