Skip to content

StdLib Disposable WeakRef

Roger Johansson edited this page Jan 14, 2026 · 1 revision

DisposableStack, FinalizationRegistry, and WeakRef

Resource management and weak reference utilities.


DisposableStack

Manages a stack of disposable resources for the using declaration.

Implementation Status: 100% Complete (marked FLAKY)

using stack = new DisposableStack();
stack.use(resource);  // Auto-disposed when block exits

Constructor

Method Status Description
new DisposableStack() Implemented Creates empty stack

Prototype Methods

Method Status Description
use(value) Implemented Adds disposable, returns value
adopt(value, onDispose) Implemented Adds value with custom dispose
defer(onDispose) Implemented Adds cleanup callback
move() Implemented Transfers resources to new stack
dispose() Implemented Disposes all resources
[Symbol.dispose]() Implemented Alias for dispose()
disposed (getter) Implemented Is stack disposed

Example

{
    using stack = new DisposableStack();
    const file = stack.use(openFile("data.txt"));
    stack.defer(() => console.log("cleanup"));
    // file disposed, "cleanup" logged when block exits
}

AsyncDisposableStack

Async version of DisposableStack for await using.

Implementation Status: 100% Complete (marked FLAKY)

await using stack = new AsyncDisposableStack();
stack.use(asyncResource);

Constructor

Method Status Description
new AsyncDisposableStack() Implemented Creates empty stack

Prototype Methods

Method Status Description
use(value) Implemented Adds async disposable
adopt(value, onDispose) Implemented Adds with custom async dispose
defer(onDispose) Implemented Adds async cleanup callback
move() Implemented Transfers to new stack
disposeAsync() Implemented Async dispose all resources
[Symbol.asyncDispose]() Implemented Alias for disposeAsync()
disposed (getter) Implemented Is stack disposed

FinalizationRegistry

Allows cleanup callbacks when objects are garbage collected.

Implementation Status: 0% (Stubs Only)

Constructor

Method Status Description
new FinalizationRegistry(callback) Stub Creates stub instance

Prototype Methods

Method Status Description
register(target, heldValue, token) Not Implemented Throws NotImplementedException
unregister(token) Not Implemented Throws NotImplementedException
cleanupSome(callback) Missing Not defined

Note: FinalizationRegistry requires integration with the .NET garbage collector which is not implemented.


WeakRef

Holds a weak reference to an object.

Implementation Status: Simplified (Not True Weak)

Constructor

Method Status Description
new WeakRef(target) Implemented Creates "weak" reference

Prototype Methods

Method Status Description
deref() Implemented Returns target or undefined

Important Limitation

The current implementation stores the target as a regular property, not a true weak reference:

// Current implementation (simplified)
instance.SetProperty("_target", target);  // Strong reference!

This means:

  • Objects held by WeakRef will never be garbage collected
  • deref() will never return undefined due to GC
  • WeakRef behaves like a regular object wrapper

For true weak reference semantics, use WeakMap/WeakSet which use ConditionalWeakTable.


Disposal Protocol

Symbol.dispose

Sync disposal uses [Symbol.dispose]:

const resource = {
    [Symbol.dispose]() {
        console.log("disposed");
    }
};

{
    using r = resource;
}  // "disposed" logged

Symbol.asyncDispose

Async disposal uses [Symbol.asyncDispose]:

const asyncResource = {
    async [Symbol.asyncDispose]() {
        await cleanup();
        console.log("async disposed");
    }
};

{
    await using r = asyncResource;
}  // "async disposed" logged

Files

File Purpose
StdLib/DisposableStack/DisposableStackConstructor.cs Constructor
StdLib/DisposableStack/DisposableStackPrototype.cs Prototype methods
StdLib/AsyncDisposableStack/AsyncDisposableStackConstructor.cs Async constructor
StdLib/AsyncDisposableStack/AsyncDisposableStackPrototype.cs Async methods
StdLib/FinalizationRegistry/FinalizationRegistryConstructor.cs Stub
StdLib/FinalizationRegistry/FinalizationRegistryPrototype.cs Stubs
StdLib/WeakRef/WeakRefConstructor.cs Constructor
StdLib/WeakRef/WeakRefPrototype.cs deref()
JsTypes/JsDisposableStackBase.cs Shared base class

See Also

Clone this wiki locally