瀏覽代碼

LibJS: Convert NativeFunction::create() to NonnullGCPtr

Linus Groh 2 年之前
父節點
當前提交
b42e293ddd

+ 2 - 2
Userland/Libraries/LibJS/CyclicModule.cpp

@@ -473,7 +473,7 @@ void CyclicModule::execute_async_module(VM& vm)
     };
     };
 
 
     // 5. Let onFulfilled be CreateBuiltinFunction(fulfilledClosure, 0, "", « »).
     // 5. Let onFulfilled be CreateBuiltinFunction(fulfilledClosure, 0, "", « »).
-    auto* on_fulfilled = NativeFunction::create(realm, move(fulfilled_closure), 0, "");
+    auto on_fulfilled = NativeFunction::create(realm, move(fulfilled_closure), 0, "");
 
 
     // 6. Let rejectedClosure be a new Abstract Closure with parameters (error) that captures module and performs the following steps when called:
     // 6. Let rejectedClosure be a new Abstract Closure with parameters (error) that captures module and performs the following steps when called:
     auto rejected_closure = [&](VM& vm) -> ThrowCompletionOr<Value> {
     auto rejected_closure = [&](VM& vm) -> ThrowCompletionOr<Value> {
@@ -487,7 +487,7 @@ void CyclicModule::execute_async_module(VM& vm)
     };
     };
 
 
     // 7. Let onRejected be CreateBuiltinFunction(rejectedClosure, 0, "", « »).
     // 7. Let onRejected be CreateBuiltinFunction(rejectedClosure, 0, "", « »).
-    auto* on_rejected = NativeFunction::create(realm, move(rejected_closure), 0, "");
+    auto on_rejected = NativeFunction::create(realm, move(rejected_closure), 0, "");
 
 
     // 8. Perform PerformPromiseThen(capability.[[Promise]], onFulfilled, onRejected).
     // 8. Perform PerformPromiseThen(capability.[[Promise]], onFulfilled, onRejected).
     verify_cast<Promise>(capability->promise().ptr())->perform_then(on_fulfilled, on_rejected, {});
     verify_cast<Promise>(capability->promise().ptr())->perform_then(on_fulfilled, on_rejected, {});

+ 1 - 1
Userland/Libraries/LibJS/Runtime/AsyncFromSyncIteratorPrototype.cpp

@@ -56,7 +56,7 @@ static Object* async_from_sync_iterator_continuation(VM& vm, Object& result, Pro
 
 
     // 9. Let onFulfilled be CreateBuiltinFunction(unwrap, 1, "", « »).
     // 9. Let onFulfilled be CreateBuiltinFunction(unwrap, 1, "", « »).
     // 10. NOTE: onFulfilled is used when processing the "value" property of an IteratorResult object in order to wait for its value if it is a promise and re-package the result in a new "unwrapped" IteratorResult object.
     // 10. NOTE: onFulfilled is used when processing the "value" property of an IteratorResult object in order to wait for its value if it is a promise and re-package the result in a new "unwrapped" IteratorResult object.
-    auto* on_fulfilled = NativeFunction::create(realm, move(unwrap), 1, "");
+    auto on_fulfilled = NativeFunction::create(realm, move(unwrap), 1, "");
 
 
     // 11. Perform PerformPromiseThen(valueWrapper, onFulfilled, undefined, promiseCapability).
     // 11. Perform PerformPromiseThen(valueWrapper, onFulfilled, undefined, promiseCapability).
     verify_cast<Promise>(value_wrapper)->perform_then(move(on_fulfilled), js_undefined(), &promise_capability);
     verify_cast<Promise>(value_wrapper)->perform_then(move(on_fulfilled), js_undefined(), &promise_capability);

+ 2 - 2
Userland/Libraries/LibJS/Runtime/Completion.cpp

@@ -62,7 +62,7 @@ ThrowCompletionOr<Value> await(VM& vm, Value value)
     };
     };
 
 
     // 4. Let onFulfilled be CreateBuiltinFunction(fulfilledClosure, 1, "", « »).
     // 4. Let onFulfilled be CreateBuiltinFunction(fulfilledClosure, 1, "", « »).
-    auto* on_fulfilled = NativeFunction::create(realm, move(fulfilled_closure), 1, "");
+    auto on_fulfilled = NativeFunction::create(realm, move(fulfilled_closure), 1, "");
 
 
     // 5. Let rejectedClosure be a new Abstract Closure with parameters (reason) that captures asyncContext and performs the following steps when called:
     // 5. Let rejectedClosure be a new Abstract Closure with parameters (reason) that captures asyncContext and performs the following steps when called:
     auto rejected_closure = [&success, &result](VM& vm) -> ThrowCompletionOr<Value> {
     auto rejected_closure = [&success, &result](VM& vm) -> ThrowCompletionOr<Value> {
@@ -86,7 +86,7 @@ ThrowCompletionOr<Value> await(VM& vm, Value value)
     };
     };
 
 
     // 6. Let onRejected be CreateBuiltinFunction(rejectedClosure, 1, "", « »).
     // 6. Let onRejected be CreateBuiltinFunction(rejectedClosure, 1, "", « »).
-    auto* on_rejected = NativeFunction::create(realm, move(rejected_closure), 1, "");
+    auto on_rejected = NativeFunction::create(realm, move(rejected_closure), 1, "");
 
 
     // 7. Perform PerformPromiseThen(promise, onFulfilled, onRejected).
     // 7. Perform PerformPromiseThen(promise, onFulfilled, onRejected).
     auto* promise = verify_cast<Promise>(promise_object);
     auto* promise = verify_cast<Promise>(promise_object);

+ 1 - 1
Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.cpp

@@ -736,7 +736,7 @@ void async_block_start(VM& vm, NonnullRefPtr<Statement> const& async_body, Promi
     auto& running_context = vm.running_execution_context();
     auto& running_context = vm.running_execution_context();
 
 
     // 3. Set the code evaluation state of asyncContext such that when evaluation is resumed for that execution context the following steps will be performed:
     // 3. Set the code evaluation state of asyncContext such that when evaluation is resumed for that execution context the following steps will be performed:
-    auto* execution_steps = NativeFunction::create(realm, "", [&async_body, &promise_capability](auto& vm) -> ThrowCompletionOr<Value> {
+    auto execution_steps = NativeFunction::create(realm, "", [&async_body, &promise_capability](auto& vm) -> ThrowCompletionOr<Value> {
         // a. Let result be the result of evaluating asyncBody.
         // a. Let result be the result of evaluating asyncBody.
         auto result = async_body->execute(vm.interpreter());
         auto result = async_body->execute(vm.interpreter());
 
 

+ 4 - 4
Userland/Libraries/LibJS/Runtime/NativeFunction.cpp

@@ -16,7 +16,7 @@ namespace JS {
 
 
 // 10.3.3 CreateBuiltinFunction ( behaviour, length, name, additionalInternalSlotsList [ , realm [ , prototype [ , prefix ] ] ] ), https://tc39.es/ecma262/#sec-createbuiltinfunction
 // 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.
 // 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.
-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, SafeFunction<ThrowCompletionOr<Value>(VM&)> behaviour, i32 length, PropertyKey const& name, Optional<Realm*> realm, Optional<Object*> prototype, Optional<StringView> const& prefix)
 {
 {
     auto& vm = allocating_realm.vm();
     auto& vm = allocating_realm.vm();
 
 
@@ -48,12 +48,12 @@ NativeFunction* NativeFunction::create(Realm& allocating_realm, SafeFunction<Thr
     function->set_function_name(name, prefix);
     function->set_function_name(name, prefix);
 
 
     // 13. Return func.
     // 13. Return func.
-    return function;
+    return *function;
 }
 }
 
 
-NativeFunction* NativeFunction::create(Realm& realm, FlyString const& name, SafeFunction<ThrowCompletionOr<Value>(VM&)> function)
+NonnullGCPtr<NativeFunction> NativeFunction::create(Realm& realm, FlyString const& name, SafeFunction<ThrowCompletionOr<Value>(VM&)> function)
 {
 {
-    return realm.heap().allocate<NativeFunction>(realm, name, move(function), *realm.intrinsics().function_prototype());
+    return *realm.heap().allocate<NativeFunction>(realm, name, move(function), *realm.intrinsics().function_prototype());
 }
 }
 
 
 NativeFunction::NativeFunction(SafeFunction<ThrowCompletionOr<Value>(VM&)> native_function, Object* prototype, Realm& realm)
 NativeFunction::NativeFunction(SafeFunction<ThrowCompletionOr<Value>(VM&)> native_function, Object* prototype, Realm& realm)

+ 2 - 2
Userland/Libraries/LibJS/Runtime/NativeFunction.h

@@ -20,8 +20,8 @@ class NativeFunction : public FunctionObject {
     JS_OBJECT(NativeFunction, FunctionObject);
     JS_OBJECT(NativeFunction, FunctionObject);
 
 
 public:
 public:
-    static NativeFunction* create(Realm&, SafeFunction<ThrowCompletionOr<Value>(VM&)> behaviour, i32 length, PropertyKey const& name, Optional<Realm*> = {}, Optional<Object*> prototype = {}, Optional<StringView> const& prefix = {});
-    static NativeFunction* create(Realm&, FlyString const& name, SafeFunction<ThrowCompletionOr<Value>(VM&)>);
+    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&, FlyString const& name, SafeFunction<ThrowCompletionOr<Value>(VM&)>);
 
 
     virtual void initialize(Realm&) override { }
     virtual void initialize(Realm&) override { }
     virtual ~NativeFunction() override = default;
     virtual ~NativeFunction() override = default;

+ 1 - 1
Userland/Libraries/LibJS/Runtime/Object.cpp

@@ -1191,7 +1191,7 @@ Value Object::get_without_side_effects(PropertyKey const& property_key) const
 
 
 void Object::define_native_function(Realm& realm, PropertyKey const& property_key, SafeFunction<ThrowCompletionOr<Value>(VM&)> native_function, i32 length, PropertyAttributes attribute)
 void Object::define_native_function(Realm& realm, PropertyKey const& property_key, SafeFunction<ThrowCompletionOr<Value>(VM&)> native_function, i32 length, PropertyAttributes attribute)
 {
 {
-    auto* function = NativeFunction::create(realm, move(native_function), length, property_key, &realm);
+    auto function = NativeFunction::create(realm, move(native_function), length, property_key, &realm);
     define_direct_property(property_key, function, attribute);
     define_direct_property(property_key, function, attribute);
 }
 }
 
 

+ 1 - 1
Userland/Libraries/LibJS/Runtime/PromiseCapability.cpp

@@ -75,7 +75,7 @@ ThrowCompletionOr<NonnullGCPtr<PromiseCapability>> new_promise_capability(VM& vm
     };
     };
 
 
     // 5. Let executor be CreateBuiltinFunction(executorClosure, 2, "", « »).
     // 5. Let executor be CreateBuiltinFunction(executorClosure, 2, "", « »).
-    auto* executor = NativeFunction::create(realm, move(executor_closure), 2, "");
+    auto executor = NativeFunction::create(realm, move(executor_closure), 2, "");
 
 
     // 6. Let promise be ? Construct(C, « executor »).
     // 6. Let promise be ? Construct(C, « executor »).
     auto* promise = TRY(construct(vm, constructor.as_function(), executor));
     auto* promise = TRY(construct(vm, constructor.as_function(), executor));

+ 2 - 2
Userland/Libraries/LibJS/Runtime/PromisePrototype.cpp

@@ -122,7 +122,7 @@ JS_DEFINE_NATIVE_FUNCTION(PromisePrototype::finally)
             };
             };
 
 
             // iv. Let valueThunk be CreateBuiltinFunction(returnValue, 0, "", « »).
             // iv. Let valueThunk be CreateBuiltinFunction(returnValue, 0, "", « »).
-            auto* value_thunk = NativeFunction::create(realm, move(return_value), 0, "");
+            auto value_thunk = NativeFunction::create(realm, move(return_value), 0, "");
 
 
             // v. Return ? Invoke(promise, "then", « valueThunk »).
             // v. Return ? Invoke(promise, "then", « valueThunk »).
             return TRY(Value(promise).invoke(vm, vm.names.then, value_thunk));
             return TRY(Value(promise).invoke(vm, vm.names.then, value_thunk));
@@ -151,7 +151,7 @@ JS_DEFINE_NATIVE_FUNCTION(PromisePrototype::finally)
             };
             };
 
 
             // iv. Let thrower be CreateBuiltinFunction(throwReason, 0, "", « »).
             // iv. Let thrower be CreateBuiltinFunction(throwReason, 0, "", « »).
-            auto* thrower = NativeFunction::create(realm, move(throw_reason), 0, "");
+            auto thrower = NativeFunction::create(realm, move(throw_reason), 0, "");
 
 
             // v. Return ? Invoke(promise, "then", « thrower »).
             // v. Return ? Invoke(promise, "then", « thrower »).
             return TRY(Value(promise).invoke(vm, vm.names.then, thrower));
             return TRY(Value(promise).invoke(vm, vm.names.then, thrower));

+ 1 - 1
Userland/Libraries/LibJS/Runtime/ProxyConstructor.cpp

@@ -84,7 +84,7 @@ JS_DEFINE_NATIVE_FUNCTION(ProxyConstructor::revocable)
 
 
     // 3. Let revoker be CreateBuiltinFunction(revokerClosure, 0, "", « [[RevocableProxy]] »).
     // 3. Let revoker be CreateBuiltinFunction(revokerClosure, 0, "", « [[RevocableProxy]] »).
     // 4. Set revoker.[[RevocableProxy]] to p.
     // 4. Set revoker.[[RevocableProxy]] to p.
-    auto* revoker = NativeFunction::create(realm, move(revoker_closure), 0, "");
+    auto revoker = NativeFunction::create(realm, move(revoker_closure), 0, "");
 
 
     // 5. Let result be OrdinaryObjectCreate(%Object.prototype%).
     // 5. Let result be OrdinaryObjectCreate(%Object.prototype%).
     auto* result = Object::create(realm, realm.intrinsics().object_prototype());
     auto* result = Object::create(realm, realm.intrinsics().object_prototype());

+ 2 - 2
Userland/Libraries/LibJS/Runtime/ShadowRealm.cpp

@@ -264,14 +264,14 @@ ThrowCompletionOr<Value> shadow_realm_import_value(VM& vm, DeprecatedString spec
 
 
     // 10. Let onFulfilled be CreateBuiltinFunction(steps, 1, "", « [[ExportNameString]] », callerRealm).
     // 10. Let onFulfilled be CreateBuiltinFunction(steps, 1, "", « [[ExportNameString]] », callerRealm).
     // 11. Set onFulfilled.[[ExportNameString]] to exportNameString.
     // 11. Set onFulfilled.[[ExportNameString]] to exportNameString.
-    auto* on_fulfilled = NativeFunction::create(realm, move(steps), 1, "", &caller_realm);
+    auto on_fulfilled = NativeFunction::create(realm, move(steps), 1, "", &caller_realm);
 
 
     // 12. Let promiseCapability be ! NewPromiseCapability(%Promise%).
     // 12. Let promiseCapability be ! NewPromiseCapability(%Promise%).
     auto promise_capability = MUST(new_promise_capability(vm, realm.intrinsics().promise_constructor()));
     auto promise_capability = MUST(new_promise_capability(vm, realm.intrinsics().promise_constructor()));
 
 
     // NOTE: Even though the spec tells us to use %ThrowTypeError%, it's not observable if we actually do.
     // NOTE: Even though the spec tells us to use %ThrowTypeError%, it's not observable if we actually do.
     // Throw a nicer TypeError forwarding the import error message instead (we know the argument is an Error object).
     // Throw a nicer TypeError forwarding the import error message instead (we know the argument is an Error object).
-    auto* throw_type_error = NativeFunction::create(realm, {}, [](auto& vm) -> ThrowCompletionOr<Value> {
+    auto throw_type_error = NativeFunction::create(realm, {}, [](auto& vm) -> ThrowCompletionOr<Value> {
         return vm.template throw_completion<TypeError>(vm.argument(0).as_object().get_without_side_effects(vm.names.message).as_string().deprecated_string());
         return vm.template throw_completion<TypeError>(vm.argument(0).as_object().get_without_side_effects(vm.names.message).as_string().deprecated_string());
     });
     });
 
 

+ 2 - 2
Userland/Libraries/LibJS/Runtime/VM.cpp

@@ -1043,7 +1043,7 @@ void VM::finish_dynamic_import(ScriptOrModule referencing_script_or_module, Modu
     };
     };
 
 
     // 2. Let onFulfilled be CreateBuiltinFunction(fulfilledClosure, 0, "", « »).
     // 2. Let onFulfilled be CreateBuiltinFunction(fulfilledClosure, 0, "", « »).
-    auto* on_fulfilled = NativeFunction::create(realm, move(fulfilled_closure), 0, "");
+    auto on_fulfilled = NativeFunction::create(realm, move(fulfilled_closure), 0, "");
 
 
     // 3. Let rejectedClosure be a new Abstract Closure with parameters (error) that captures promiseCapability and performs the following steps when called:
     // 3. Let rejectedClosure be a new Abstract Closure with parameters (error) that captures promiseCapability and performs the following steps when called:
     auto rejected_closure = [&promise_capability](VM& vm) -> ThrowCompletionOr<Value> {
     auto rejected_closure = [&promise_capability](VM& vm) -> ThrowCompletionOr<Value> {
@@ -1057,7 +1057,7 @@ void VM::finish_dynamic_import(ScriptOrModule referencing_script_or_module, Modu
     };
     };
 
 
     // 4. Let onRejected be CreateBuiltinFunction(rejectedClosure, 0, "", « »).
     // 4. Let onRejected be CreateBuiltinFunction(rejectedClosure, 0, "", « »).
-    auto* on_rejected = NativeFunction::create(realm, move(rejected_closure), 0, "");
+    auto on_rejected = NativeFunction::create(realm, move(rejected_closure), 0, "");
 
 
     // 5. Perform PerformPromiseThen(innerPromise, onFulfilled, onRejected).
     // 5. Perform PerformPromiseThen(innerPromise, onFulfilled, onRejected).
     inner_promise->perform_then(on_fulfilled, on_rejected, {});
     inner_promise->perform_then(on_fulfilled, on_rejected, {});

+ 2 - 2
Userland/Libraries/LibWeb/WebIDL/Promise.cpp

@@ -108,7 +108,7 @@ JS::NonnullGCPtr<JS::Promise> react_to_promise(JS::PromiseCapability const& prom
     };
     };
 
 
     // 2. Let onFulfilled be CreateBuiltinFunction(onFulfilledSteps, « »):
     // 2. Let onFulfilled be CreateBuiltinFunction(onFulfilledSteps, « »):
-    auto* on_fulfilled = JS::NativeFunction::create(realm, move(on_fulfilled_steps), 1, "");
+    auto on_fulfilled = JS::NativeFunction::create(realm, move(on_fulfilled_steps), 1, "");
 
 
     // 3. Let onRejectedSteps be the following steps given argument R:
     // 3. Let onRejectedSteps be the following steps given argument R:
     auto on_rejected_steps = [&realm, on_rejected_callback = move(on_rejected_callback)](JS::VM& vm) -> JS::ThrowCompletionOr<JS::Value> {
     auto on_rejected_steps = [&realm, on_rejected_callback = move(on_rejected_callback)](JS::VM& vm) -> JS::ThrowCompletionOr<JS::Value> {
@@ -125,7 +125,7 @@ JS::NonnullGCPtr<JS::Promise> react_to_promise(JS::PromiseCapability const& prom
     };
     };
 
 
     // 4. Let onRejected be CreateBuiltinFunction(onRejectedSteps, « »):
     // 4. Let onRejected be CreateBuiltinFunction(onRejectedSteps, « »):
-    auto* on_rejected = JS::NativeFunction::create(realm, move(on_rejected_steps), 1, "");
+    auto on_rejected = JS::NativeFunction::create(realm, move(on_rejected_steps), 1, "");
 
 
     // 5. Let constructor be promise.[[Promise]].[[Realm]].[[Intrinsics]].[[%Promise%]].
     // 5. Let constructor be promise.[[Promise]].[[Realm]].[[Intrinsics]].[[%Promise%]].
     auto* constructor = realm.intrinsics().promise_constructor();
     auto* constructor = realm.intrinsics().promise_constructor();