Skip to content

runtime: expose DisposableStack, AsyncDisposableStack, and SuppressedError #2875

Closed

Description

@andrewtdiz

Summary

Node exposes the explicit-resource-management globals DisposableStack, AsyncDisposableStack, and SuppressedError. Perry has lowering support for using / await using declarations and well-known dispose symbols, but the standard stack constructors and SuppressedError error type are not exposed.

Expected Node behavior

Local Node v25.9.0 probe:

for (const name of ["DisposableStack", "AsyncDisposableStack", "SuppressedError"]) {
  console.log(name, typeof globalThis[name]);
}

const stack = new DisposableStack();
let disposed = 0;
console.log(typeof stack.defer(() => disposed++)); // "undefined"
stack.dispose();
console.log(disposed, stack.disposed); // 1 true

Output:

DisposableStack function
AsyncDisposableStack function
SuppressedError function
defer ret type undefined
x 1 disposed true

Current Perry behavior in source

crates/perry-hir/src/lower_decl/block.rs desugars using / await using declarations into try/finally blocks that call [Symbol.dispose]() / [Symbol.asyncDispose]()-style methods. The same comment notes that SuppressedError chaining is not implemented yet:

// SuppressedError chaining when a body throw is followed by a dispose throw is not yet
// implemented — the dispose throw shadows the original.

Source search found no DisposableStack, AsyncDisposableStack, or SuppressedError entries in crates/perry-runtime/src/object/global_this.rs, no runtime implementations, and no constructor lowering paths. That means direct API usage such as new DisposableStack() or new SuppressedError(...) is missing even though the syntax-level using path has partial support.

Suggested tests

Add parity coverage for:

expect(typeof DisposableStack).toBe("function");
expect(typeof AsyncDisposableStack).toBe("function");
expect(typeof SuppressedError).toBe("function");

const calls = [];
const stack = new DisposableStack();
stack.defer(() => calls.push("defer"));
stack.use({ [Symbol.dispose]: () => calls.push("use") });
stack.dispose();
expect(calls).toEqual(["use", "defer"]);
expect(stack.disposed).toBe(true);
expect(() => stack.defer(() => {})).toThrow(ReferenceError);

Async coverage should verify AsyncDisposableStack#disposeAsync() awaits async disposers. Error-path tests should verify SuppressedError shape when both body/dispose failures occur.

Scope / non-goals

This is scoped to the standard global constructors and basic stack behavior. Existing using statement desugaring and missing well-known Symbol constants are related but separate (#2852 for remaining symbols).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions