Truly independent web browser
LibJS: Await disposers asynchronously in disposeAsync()
Andreas Kling55441137414b
diff --git a/Libraries/LibJS/Rust/src/runtime/async_disposable_stack_prototype.rs b/Libraries/LibJS/Rust/src/runtime/async_disposable_stack_prototype.rsindex fb809cd81c3..f1d718f98bd 100644--- a/Libraries/LibJS/Rust/src/runtime/async_disposable_stack_prototype.rs+++ b/Libraries/LibJS/Rust/src/runtime/async_disposable_stack_prototype.rs@@ -13,10 +13,11 @@ use crate::layout::cell::Gc;use crate::layout::object::Object;use crate::layout::value::Value;use crate::runtime::abstract_operations::{- add_disposable_resource, call_function_object, dispose_resources, ordinary_create_from_constructor_of,+ add_disposable_resource, call_function_object, ordinary_create_from_constructor_of,};use crate::runtime::async_disposable_stack::{AsyncDisposableStack, AsyncDisposableState};-use crate::runtime::completion::{Completion, Must, ThrowCompletionOr};+use crate::runtime::completion::{Completion, CompletionType, Must, ThrowCompletionOr};+use crate::runtime::disposable_resource_stack::{AsyncDisposeResourcesStep, DisposableResourceStack};use crate::runtime::environment::InitializeBindingHint;use crate::runtime::error::ErrorKind;use crate::runtime::error_types::ErrorType;@@ -25,7 +26,8 @@ use crate::runtime::intrinsics::Intrinsics;use crate::runtime::native_function::{NativeFunction, raw_native};use crate::runtime::object::{MayInterfereWithIndexedPropertyAccess, ORDINARY_OBJECT_METHODS, define_object_class};use crate::runtime::primitive_string::PrimitiveString;-use crate::runtime::promise_capability::{new_promise_capability, try_or_reject};+use crate::runtime::promise::{Promise, promise_resolve};+use crate::runtime::promise_capability::{PromiseCapability, new_promise_capability};use crate::runtime::property_attributes::{Attribute, PropertyAttributes};use crate::runtime::property_key::PropertyKey;use crate::runtime::prototype_object::typed_this_object;@@ -195,6 +197,7 @@ impl AsyncDisposableStackPrototype {}// 12.4.3.3 AsyncDisposableStack.prototype.disposeAsync(), https://tc39.es/proposal-explicit-resource-management/#sec-asyncdisposablestack.prototype.disposeAsync+ #[allow(clippy::unnecessary_wraps, reason = "native functions can throw")]fn dispose_async(vm: &Vm) -> ThrowCompletionOr<Value> {let realm = current_realm(vm);@@ -235,19 +238,14 @@ impl AsyncDisposableStackPrototype {// 6. Let result be DisposeResources(asyncDisposableStack.[[DisposeCapability]], NormalCompletion(undefined)).// 7. IfAbruptRejectPromise(result, promiseCapability).- let result = try_or_reject!(- vm,- promise_capability,- dispose_resources(- vm,- async_disposable_stack.dispose_capability(),- Completion::normal(Value::UNDEFINED),- )- .into_throw_completion_or()- );-// 8. Perform ! Call(promiseCapability.[[Resolve]], undefined, « result »).- call_function_object(vm, promise_capability.resolve(), Value::UNDEFINED, &[result]).must();+ // NB: DisposeResources may Await, which suspends this function until the awaited promise settles. We perform it+ // one step at a time, and continue with the next step from a reaction to each awaited promise, instead of+ // blocking until the promise settles. The resources move to a DisposableResourceStack, which keeps the+ // state of DisposeResources between the steps.+ let stack = DisposableResourceStack::create(vm);+ stack.take_dispose_capability(async_disposable_stack.dispose_capability());+ continue_dispose_resources(vm, stack, promise_capability, Completion::normal(Value::UNDEFINED));// 9. Return promiseCapability.[[Promise]].Ok(Value::from_object(promise_capability.promise()))@@ -337,3 +335,89 @@ impl AsyncDisposableStackPrototype {Ok(value)}}++/// Performs the next step of the DisposeResources of AsyncDisposableStack.prototype.disposeAsync(), given the completion+/// of the Await that the previous step performed, and settles the promise of disposeAsync() once DisposeResources is done.+fn continue_dispose_resources(+ vm: &Vm,+ stack: Gc<DisposableResourceStack>,+ promise_capability: Gc<PromiseCapability>,+ await_completion: Completion,+) {+ let value = match stack.async_dispose_resources_step(vm, Completion::normal(Value::UNDEFINED), await_completion) {+ Ok(AsyncDisposeResourcesStep::Await(value)) => value,+ Ok(AsyncDisposeResourcesStep::Done) => {+ // 8. Perform ! Call(promiseCapability.[[Resolve]], undefined, « result »).+ call_function_object(vm, promise_capability.resolve(), Value::UNDEFINED, &[Value::UNDEFINED]).must();+ return;+ }+ Err(throw) => {+ // 7. IfAbruptRejectPromise(result, promiseCapability).+ call_function_object(vm, promise_capability.reject(), Value::UNDEFINED, &[throw.value()]).must();+ return;+ }+ };++ // Await ( value ), https://tc39.es/ecma262/#await+ let realm = current_realm(vm);++ // 2. Let promise be ? PromiseResolve(%Promise%, value).+ let promise = match promise_resolve(vm, realm.intrinsics().promise_constructor(vm).upcast(), value) {+ Ok(promise) => promise,+ Err(throw) => {+ // NB: The Await throws, so the next step continues with its throw completion.+ continue_dispose_resources(vm, stack, promise_capability, throw.into());+ return;+ }+ };++ // 3. Let fulfilledClosure be a new Abstract Closure with parameters (v) that captures asyncContext and performs the+ // following steps when called:+ // 4. Let onFulfilled be CreateBuiltinFunction(fulfilledClosure, 1, "", « »).+ let on_fulfilled = NativeFunction::create_anonymous(+ vm,+ (stack, promise_capability),+ |vm, &(stack, promise_capability): &(Gc<DisposableResourceStack>, Gc<PromiseCapability>)| {+ // d. Resume the suspended evaluation of asyncContext using NormalCompletion(v) as the result of the+ // operation that suspended it.+ continue_dispose_resources(vm, stack, promise_capability, Completion::normal(vm.argument(0)));++ // f. Return undefined.+ Ok(Value::UNDEFINED)+ },+ 1,+ );++ // 5. Let rejectedClosure be a new Abstract Closure with parameters (reason) that captures asyncContext and performs+ // the following steps when called:+ // 6. Let onRejected be CreateBuiltinFunction(rejectedClosure, 1, "", « »).+ let on_rejected = NativeFunction::create_anonymous(+ vm,+ (stack, promise_capability),+ |vm, &(stack, promise_capability): &(Gc<DisposableResourceStack>, Gc<PromiseCapability>)| {+ // d. Resume the suspended evaluation of asyncContext using ThrowCompletion(reason) as the result of the+ // operation that suspended it.+ continue_dispose_resources(+ vm,+ stack,+ promise_capability,+ Completion::new(CompletionType::Throw, vm.argument(0)),+ );++ // f. Return undefined.+ Ok(Value::UNDEFINED)+ },+ 1,+ );++ // 7. Perform PerformPromiseThen(promise, onFulfilled, onRejected).+ promise+ .downcast::<Promise>()+ .expect("PromiseResolve of %Promise% returns a Promise")+ .perform_then(+ vm,+ Value::from_object(on_fulfilled),+ Value::from_object(on_rejected),+ None,+ );+}diff --git a/Libraries/LibJS/Rust/src/runtime/disposable_resource_stack.rs b/Libraries/LibJS/Rust/src/runtime/disposable_resource_stack.rsindex 9af13add7d3..720cdeed251 100644--- a/Libraries/LibJS/Rust/src/runtime/disposable_resource_stack.rs+++ b/Libraries/LibJS/Rust/src/runtime/disposable_resource_stack.rs@@ -86,6 +86,11 @@ impl DisposableResourceStack {stack}+ /// Takes the resources of the given dispose capability, which is left with an empty stack.+ pub fn take_dispose_capability(&self, dispose_capability: &GcRefCell<DisposeCapability>) {+ self.dispose_capability.swap(dispose_capability);+ }+pub fn add_disposable_resource(&self, vm: &Vm, value: Value, hint: InitializeBindingHint) -> ThrowCompletionOr<()> {add_disposable_resource(vm, &self.dispose_capability, value, hint, None)}diff --git a/Tests/LibJS/Runtime/regress/promise-job-queue-reentrancy.js b/Tests/LibJS/Runtime/regress/promise-job-queue-reentrancy.jsindex 03dff5a112e..b311b72b0dd 100644--- a/Tests/LibJS/Runtime/regress/promise-job-queue-reentrancy.js+++ b/Tests/LibJS/Runtime/regress/promise-job-queue-reentrancy.js@@ -13,7 +13,7 @@ test("nested promise job checkpoints wait for the current reaction to finish", (expect(order).toEqual(["reaction start", "reaction end", "queued reaction", "chained reaction"]);});-test("native awaits resume disposal before draining later promise jobs", () => {+test("disposeAsync() resumes disposal from promise jobs in order", () => {for (const reject of [false, true]) {const order = [];Promise.resolve().then(() => {@@ -39,10 +39,10 @@ test("native awaits resume disposal before draining later promise jobs", () => {runQueuedPromiseJobs();expect(order).toEqual(["first dispose",+ "dispose returned","tick 1","second dispose","tick 2",- "dispose returned","tick 3",reject ? "rejected" : "fulfilled",]);diff --git a/Tests/LibWeb/Text/expected/async-disposable-stack-dispose-async-in-microtask.txt b/Tests/LibWeb/Text/expected/async-disposable-stack-dispose-async-in-microtask.txtnew file mode 100644index 00000000000..526df84fba4--- /dev/null+++ b/Tests/LibWeb/Text/expected/async-disposable-stack-dispose-async-in-microtask.txt@@ -0,0 +1,6 @@+async disposer started+disposeAsync() returned+async disposer finished+sync disposer+disposed: true+SuppressedError: error=first suppressed=seconddiff --git a/Tests/LibWeb/Text/input/async-disposable-stack-dispose-async-in-microtask.html b/Tests/LibWeb/Text/input/async-disposable-stack-dispose-async-in-microtask.htmlnew file mode 100644index 00000000000..fd845e15385--- /dev/null+++ b/Tests/LibWeb/Text/input/async-disposable-stack-dispose-async-in-microtask.html@@ -0,0 +1,39 @@+<!DOCTYPE html>+<script src="include.js"></script>+<script>+ asyncTest(async done => {+ // NB: This runs disposeAsync() from a microtask, where awaiting a disposer cannot wait for a nested microtask+ // checkpoint.+ await null;++ const events = [];+ const stack = new AsyncDisposableStack();+ stack.defer(() => events.push("sync disposer"));+ stack.defer(async () => {+ events.push("async disposer started");+ await null;+ events.push("async disposer finished");+ });+ const promise = stack.disposeAsync();+ events.push("disposeAsync() returned");+ await promise;+ events.push(`disposed: ${stack.disposed}`);++ const throwing_stack = new AsyncDisposableStack();+ throwing_stack.defer(async () => {+ throw new Error("first");+ });+ throwing_stack.defer(async () => {+ throw new Error("second");+ });+ try {+ await throwing_stack.disposeAsync();+ } catch (error) {+ events.push(`${error.name}: error=${error.error.message} suppressed=${error.suppressed.message}`);+ }++ for (const event of events)+ println(event);+ done();+ });+</script>