ソースを参照

LibWeb: Let queue_fetch_task() take a JS::HeapFunction

Changes the signature of queue_fetch_task() from AK:Function to
JS::HeapFunction to be more clear to the user of the function that this
is what it uses internally.
Kenneth Myhra 1 年間 前
コミット
291d0e5de8

+ 6 - 6
Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp

@@ -603,7 +603,7 @@ WebIDL::ExceptionOr<void> fetch_response_handover(JS::Realm& realm, Infrastructu
         });
         });
 
 
         // 4. Let processResponseEndOfBodyTask be the following steps:
         // 4. Let processResponseEndOfBodyTask be the following steps:
-        auto process_response_end_of_body_task = [&fetch_params, &response] {
+        auto process_response_end_of_body_task = JS::create_heap_function(vm.heap(), [&fetch_params, &response] {
             // 1. Set fetchParams’s request’s done flag.
             // 1. Set fetchParams’s request’s done flag.
             fetch_params.request()->set_done(true);
             fetch_params.request()->set_done(true);
 
 
@@ -621,7 +621,7 @@ WebIDL::ExceptionOr<void> fetch_response_handover(JS::Realm& realm, Infrastructu
                 if (fetch_params.request()->initiator_type().has_value() && &client->global_object() == task_destination_global_object->ptr())
                 if (fetch_params.request()->initiator_type().has_value() && &client->global_object() == task_destination_global_object->ptr())
                     fetch_params.controller()->report_timing(client->global_object());
                     fetch_params.controller()->report_timing(client->global_object());
             }
             }
-        };
+        });
 
 
         // FIXME: Handle 'parallel queue' task destination
         // FIXME: Handle 'parallel queue' task destination
         auto task_destination = fetch_params.task_destination().get<JS::NonnullGCPtr<JS::Object>>();
         auto task_destination = fetch_params.task_destination().get<JS::NonnullGCPtr<JS::Object>>();
@@ -636,9 +636,9 @@ WebIDL::ExceptionOr<void> fetch_response_handover(JS::Realm& realm, Infrastructu
     // 4. If fetchParams’s process response is non-null, then queue a fetch task to run fetchParams’s process response
     // 4. If fetchParams’s process response is non-null, then queue a fetch task to run fetchParams’s process response
     //    given response, with fetchParams’s task destination.
     //    given response, with fetchParams’s task destination.
     if (fetch_params.algorithms()->process_response()) {
     if (fetch_params.algorithms()->process_response()) {
-        Infrastructure::queue_fetch_task(fetch_params.controller(), task_destination, [&fetch_params, &response]() {
+        Infrastructure::queue_fetch_task(fetch_params.controller(), task_destination, JS::create_heap_function(vm.heap(), [&fetch_params, &response]() {
             fetch_params.algorithms()->process_response()(response);
             fetch_params.algorithms()->process_response()(response);
-        });
+        }));
     }
     }
 
 
     // 5. Let internalResponse be response, if response is a network error; otherwise response’s internal response.
     // 5. Let internalResponse be response, if response is a network error; otherwise response’s internal response.
@@ -674,9 +674,9 @@ WebIDL::ExceptionOr<void> fetch_response_handover(JS::Realm& realm, Infrastructu
         // 3. If internalResponse's body is null, then queue a fetch task to run processBody given null, with
         // 3. If internalResponse's body is null, then queue a fetch task to run processBody given null, with
         //    fetchParams’s task destination.
         //    fetchParams’s task destination.
         if (!internal_response->body()) {
         if (!internal_response->body()) {
-            Infrastructure::queue_fetch_task(fetch_params.controller(), task_destination, [process_body = move(process_body)]() {
+            Infrastructure::queue_fetch_task(fetch_params.controller(), task_destination, JS::create_heap_function(vm.heap(), [process_body = move(process_body)]() {
                 process_body({});
                 process_body({});
-            });
+            }));
         }
         }
         // 4. Otherwise, fully read internalResponse body given processBody, processBodyError, and fetchParams’s task
         // 4. Otherwise, fully read internalResponse body given processBody, processBodyError, and fetchParams’s task
         //    destination.
         //    destination.

+ 6 - 6
Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp

@@ -72,20 +72,20 @@ WebIDL::ExceptionOr<void> Body::fully_read(JS::Realm& realm, Web::Fetch::Infrast
     auto task_destination_object = task_destination.get<JS::NonnullGCPtr<JS::Object>>();
     auto task_destination_object = task_destination.get<JS::NonnullGCPtr<JS::Object>>();
 
 
     // 2. Let successSteps given a byte sequence bytes be to queue a fetch task to run processBody given bytes, with taskDestination.
     // 2. Let successSteps given a byte sequence bytes be to queue a fetch task to run processBody given bytes, with taskDestination.
-    auto success_steps = [process_body = move(process_body), task_destination_object = JS::make_handle(task_destination_object)](ByteBuffer const& bytes) mutable -> ErrorOr<void> {
+    auto success_steps = [&realm, process_body = move(process_body), task_destination_object = JS::make_handle(task_destination_object)](ByteBuffer const& bytes) mutable -> ErrorOr<void> {
         // Make a copy of the bytes, as the source of the bytes may disappear between the time the task is queued and executed.
         // Make a copy of the bytes, as the source of the bytes may disappear between the time the task is queued and executed.
         auto bytes_copy = TRY(ByteBuffer::copy(bytes));
         auto bytes_copy = TRY(ByteBuffer::copy(bytes));
-        queue_fetch_task(*task_destination_object, [process_body = move(process_body), bytes_copy = move(bytes_copy)]() {
+        queue_fetch_task(*task_destination_object, JS::create_heap_function(realm.heap(), [process_body = move(process_body), bytes_copy = move(bytes_copy)]() {
             process_body(move(bytes_copy));
             process_body(move(bytes_copy));
-        });
+        }));
         return {};
         return {};
     };
     };
 
 
     // 3. Let errorSteps optionally given an exception exception be to queue a fetch task to run processBodyError given exception, with taskDestination.
     // 3. Let errorSteps optionally given an exception exception be to queue a fetch task to run processBodyError given exception, with taskDestination.
-    auto error_steps = [process_body_error = move(process_body_error), task_destination_object = JS::make_handle(task_destination_object)](JS::GCPtr<WebIDL::DOMException> exception) mutable {
-        queue_fetch_task(*task_destination_object, [process_body_error = move(process_body_error), exception = JS::make_handle(exception)]() {
+    auto error_steps = [&realm, process_body_error = move(process_body_error), task_destination_object = JS::make_handle(task_destination_object)](JS::GCPtr<WebIDL::DOMException> exception) mutable {
+        queue_fetch_task(*task_destination_object, JS::create_heap_function(realm.heap(), [process_body_error = move(process_body_error), exception = JS::make_handle(exception)]() {
             process_body_error(*exception);
             process_body_error(*exception);
-        });
+        }));
     };
     };
 
 
     // 4. Let reader be the result of getting a reader for body’s stream. If that threw an exception, then run errorSteps with that exception and return.
     // 4. Let reader be the result of getting a reader for body’s stream. If that threw an exception, then run errorSteps with that exception and return.

+ 7 - 7
Userland/Libraries/LibWeb/Fetch/Infrastructure/Task.cpp

@@ -11,25 +11,25 @@
 namespace Web::Fetch::Infrastructure {
 namespace Web::Fetch::Infrastructure {
 
 
 // https://fetch.spec.whatwg.org/#queue-a-fetch-task
 // https://fetch.spec.whatwg.org/#queue-a-fetch-task
-int queue_fetch_task(JS::Object& task_destination, Function<void()> algorithm)
+int queue_fetch_task(JS::Object& task_destination, JS::NonnullGCPtr<JS::HeapFunction<void()>> algorithm)
 {
 {
     // FIXME: 1. If taskDestination is a parallel queue, then enqueue algorithm to taskDestination.
     // FIXME: 1. If taskDestination is a parallel queue, then enqueue algorithm to taskDestination.
 
 
     // 2. Otherwise, queue a global task on the networking task source with taskDestination and algorithm.
     // 2. Otherwise, queue a global task on the networking task source with taskDestination and algorithm.
-    auto& vm = task_destination.vm();
-    return HTML::queue_global_task(HTML::Task::Source::Networking, task_destination, JS::create_heap_function(vm.heap(), move(algorithm)));
+    return HTML::queue_global_task(HTML::Task::Source::Networking, task_destination, algorithm);
 }
 }
 
 
 // AD-HOC: This overload allows tracking the queued task within the fetch controller so that we may cancel queued tasks
 // AD-HOC: This overload allows tracking the queued task within the fetch controller so that we may cancel queued tasks
 //         when the spec indicates that we must stop an ongoing fetch.
 //         when the spec indicates that we must stop an ongoing fetch.
-int queue_fetch_task(JS::NonnullGCPtr<FetchController> fetch_controller, JS::Object& task_destination, Function<void()> algorithm)
+int queue_fetch_task(JS::NonnullGCPtr<FetchController> fetch_controller, JS::Object& task_destination, JS::NonnullGCPtr<JS::HeapFunction<void()>> algorithm)
 {
 {
     auto fetch_task_id = fetch_controller->next_fetch_task_id();
     auto fetch_task_id = fetch_controller->next_fetch_task_id();
 
 
-    int event_id = queue_fetch_task(task_destination, [fetch_controller, fetch_task_id, algorithm = move(algorithm)]() {
+    auto& heap = task_destination.heap();
+    int event_id = queue_fetch_task(task_destination, JS::create_heap_function(heap, [fetch_controller, fetch_task_id, algorithm]() {
         fetch_controller->fetch_task_complete(fetch_task_id);
         fetch_controller->fetch_task_complete(fetch_task_id);
-        algorithm();
-    });
+        algorithm->function()();
+    }));
 
 
     fetch_controller->fetch_task_queued(fetch_task_id, event_id);
     fetch_controller->fetch_task_queued(fetch_task_id, event_id);
     return event_id;
     return event_id;

+ 2 - 2
Userland/Libraries/LibWeb/Fetch/Infrastructure/Task.h

@@ -17,7 +17,7 @@ namespace Web::Fetch::Infrastructure {
 // FIXME: 'or a parallel queue'
 // FIXME: 'or a parallel queue'
 using TaskDestination = Variant<Empty, JS::NonnullGCPtr<JS::Object>>;
 using TaskDestination = Variant<Empty, JS::NonnullGCPtr<JS::Object>>;
 
 
-int queue_fetch_task(JS::Object&, Function<void()>);
-int queue_fetch_task(JS::NonnullGCPtr<FetchController>, JS::Object&, Function<void()>);
+int queue_fetch_task(JS::Object&, JS::NonnullGCPtr<JS::HeapFunction<void()>>);
+int queue_fetch_task(JS::NonnullGCPtr<FetchController>, JS::Object&, JS::NonnullGCPtr<JS::HeapFunction<void()>>);
 
 
 }
 }