From 99451e7f069ed5fd4689e5ee3715d1d70524dc51 Mon Sep 17 00:00:00 2001 From: LiviaMedeiros Date: Tue, 17 Nov 2026 04:05:29 +0800 Subject: [PATCH 1/3] lib: add ERM classes to primordials Totes three classes: - `DisposableStack` - `AsyncDisposableStack` - `SuppressedError` Signed-off-by: LiviaMedeiros --- lib/eslint.config_partial.mjs | 10 ------- lib/internal/freeze_intrinsics.js | 35 +++++++++++++++++-------- lib/internal/per_context/primordials.js | 3 +++ typings/primordials.d.ts | 18 +++++++++++++ 4 files changed, 45 insertions(+), 21 deletions(-) diff --git a/lib/eslint.config_partial.mjs b/lib/eslint.config_partial.mjs index fcd4dd4aa78c..bbb0655bdcfc 100644 --- a/lib/eslint.config_partial.mjs +++ b/lib/eslint.config_partial.mjs @@ -412,16 +412,6 @@ export default [ name: 'SubtleCrypto', message: "Use `const { SubtleCrypto } = require('internal/crypto/webcrypto');` instead of the global.", }, - // DisposableStack and AsyncDisposableStack are not available in primordials because they can be - // disabled with --no-js-explicit-resource-management CLI flag. - { - name: 'DisposableStack', - message: 'Use `const { DisposableStack } = globalThis;` instead of the global.', - }, - { - name: 'AsyncDisposableStack', - message: 'Use `const { AsyncDisposableStack } = globalThis;` instead of the global.', - }, ], 'no-restricted-modules': [ 'error', diff --git a/lib/internal/freeze_intrinsics.js b/lib/internal/freeze_intrinsics.js index 5dd425cb0934..3f1e397ec192 100644 --- a/lib/internal/freeze_intrinsics.js +++ b/lib/internal/freeze_intrinsics.js @@ -31,6 +31,8 @@ const { ArrayPrototype, ArrayPrototypeForEach, ArrayPrototypePush, + AsyncDisposableStack, + AsyncDisposableStackPrototype, AsyncFunctionPrototype, AsyncGeneratorFunctionPrototype, AsyncGeneratorFunctionPrototypePrototype, @@ -48,6 +50,8 @@ const { DataViewPrototype, Date, DatePrototype, + DisposableStack, + DisposableStackPrototype, Error, ErrorPrototype, EvalError, @@ -106,6 +110,8 @@ const { String, StringIteratorPrototype, StringPrototype, + SuppressedError, + SuppressedErrorPrototype, Symbol, SymbolPrototype, SyntaxError, @@ -171,6 +177,7 @@ module.exports = function() { EvalErrorPrototype, RangeErrorPrototype, ReferenceErrorPrototype, + SuppressedErrorPrototype, SyntaxErrorPrototype, TypeErrorPrototype, URIErrorPrototype, @@ -225,12 +232,14 @@ module.exports = function() { IteratorPrototype, // 27.1.3 WrapForValidIteratorPrototype, // 27.1.3.2.2.1 AsyncIteratorPrototype, // 27.1.4 - PromisePrototype, // 27.2 - GeneratorFunctionPrototype, // 27.3 - AsyncGeneratorFunctionPrototype, // 27.4 - // 27.5 GeneratorPrototype + DisposableStackPrototype, // 27.3.2.1 + AsyncDisposableStackPrototype, // 27.4.2.1 + PromisePrototype, // 27.5.4.4 + GeneratorFunctionPrototype, // 27.6.2.1 + AsyncGeneratorFunctionPrototype, // 27.7.3.3 + // 27.8 GeneratorPrototype GeneratorFunctionPrototypePrototype, - // 27.6 AsyncGeneratorPrototype + // 27.9 AsyncGeneratorPrototype AsyncGeneratorFunctionPrototypePrototype, AsyncFunctionPrototype, // 27.7 @@ -270,6 +279,7 @@ module.exports = function() { EvalError, RangeError, ReferenceError, + SuppressedError, SyntaxError, TypeError, URIError, @@ -329,14 +339,17 @@ module.exports = function() { Iterator, // 27.1.3 WrapForValidIteratorPrototype, // 27.1.3.2.2.1 ArrayIteratorPrototype, // 27.1.4 - Promise, // 27.2 - // 27.3 GeneratorFunction - // 27.5 Generator + // 27.2 SymbolDispose + DisposableStack, // 27.3 + AsyncDisposableStack, // 27.4 + Promise, // 27.5 + // 27.6 GeneratorFunction + // 27.8 Generator GeneratorFunctionPrototype, - // 27.4 AsyncGeneratorFunction - // 27.6 AsyncGenerator + // 27.7 AsyncGeneratorFunction + // 27.9 AsyncGenerator AsyncGeneratorFunctionPrototype, - // 27.7 AsyncFunction + // 27.10 AsyncFunction AsyncFunctionPrototype, // 28 Reflection diff --git a/lib/internal/per_context/primordials.js b/lib/internal/per_context/primordials.js index 2e610f4ab508..582c91126b3f 100644 --- a/lib/internal/per_context/primordials.js +++ b/lib/internal/per_context/primordials.js @@ -187,12 +187,14 @@ function copyPrototype(src, dest, prefix) { 'AggregateError', 'Array', 'ArrayBuffer', + 'AsyncDisposableStack', 'BigInt', 'BigInt64Array', 'BigUint64Array', 'Boolean', 'DataView', 'Date', + 'DisposableStack', 'Error', 'EvalError', 'FinalizationRegistry', @@ -212,6 +214,7 @@ function copyPrototype(src, dest, prefix) { 'RegExp', 'Set', 'String', + 'SuppressedError', 'Symbol', 'SyntaxError', 'TypeError', diff --git a/typings/primordials.d.ts b/typings/primordials.d.ts index 7a424c729502..63a75bbee042 100644 --- a/typings/primordials.d.ts +++ b/typings/primordials.d.ts @@ -168,6 +168,14 @@ declare namespace primordials { export const ArrayBufferPrototypeSlice: UncurryThis export const ArrayBufferPrototypeTransfer: UncurryThis export const ArrayBufferPrototypeGetByteLength: UncurryGetter; + export import AsyncDisposableStack = globalThis.AsyncDisposableStack, + export const AsyncDisposableStackPrototype: typeof AsyncDisposableStack.prototype + export const AsyncDisposableStackAdopt: UncurryThis + export const AsyncDisposableStackDefer: UncurryThis + export const AsyncDisposableStackDispose: UncurryThis + export const AsyncDisposableStackMove: UncurryThis + export const AsyncDisposableStackUse: UncurryThis + export const AsyncDisposableStackSymbolAsyncDispose: UncurryMethod; export import BigInt = globalThis.BigInt; export const BigIntPrototype: typeof BigInt.prototype export const BigIntAsUintN: typeof BigInt.asUintN @@ -259,6 +267,14 @@ declare namespace primordials { export const DatePrototypeToLocaleDateString: UncurryThis export const DatePrototypeToLocaleTimeString: UncurryThis export const DatePrototypeSymbolToPrimitive: UncurryMethod; + export import DisposableStack = globalThis.DisposableStack, + export const DisposableStackPrototype: typeof DisposableStack.prototype + export const DisposableStackAdopt: UncurryThis + export const DisposableStackDefer: UncurryThis + export const DisposableStackDispose: UncurryThis + export const DisposableStackMove: UncurryThis + export const DisposableStackUse: UncurryThis + export const DisposableStackSymbolDispose: UncurryMethod; export import Error = globalThis.Error; export const ErrorPrototype: typeof Error.prototype // @ts-ignore @@ -444,6 +460,8 @@ declare namespace primordials { export const StringPrototypeToWellFormed: UncurryThis export const StringPrototypeValueOf: UncurryThis export const StringPrototypeReplaceAll: UncurryThis + export import SuppressedError = globalThis.SuppressedError; + export const SuppressedErrorPrototype: typeof SuppressedError.prototype export import Symbol = globalThis.Symbol; export const SymbolPrototype: typeof Symbol.prototype export const SymbolFor: typeof Symbol.for From 98a8eb13e40524ad19f02a4e918ee88f6d98cfe2 Mon Sep 17 00:00:00 2001 From: LiviaMedeiros Date: Tue, 17 Nov 2026 05:53:13 +0800 Subject: [PATCH 2/3] Revert "diagnostics_channel: replace using with try-finally" This reverts commit b1251217f2d28070fac7497fd600e805faa607dc. --- lib/diagnostics_channel.js | 86 +++++++++++--------------------------- 1 file changed, 24 insertions(+), 62 deletions(-) diff --git a/lib/diagnostics_channel.js b/lib/diagnostics_channel.js index bb0c7d66050c..dd600d804fd9 100644 --- a/lib/diagnostics_channel.js +++ b/lib/diagnostics_channel.js @@ -20,7 +20,6 @@ const { } = primordials; const { - aggregateTwoErrors, codes: { ERR_INVALID_ARG_TYPE, }, @@ -94,25 +93,12 @@ function maybeMarkInactive(channel) { } } -function disposeStoreScopes(stack, error, hasError = false) { - for (let i = stack.length - 1; i >= 0; i--) { - try { - stack[i][SymbolDispose](); - } catch (err) { - error = hasError ? aggregateTwoErrors(err, error) : err; - hasError = true; - } - } - - if (hasError) throw error; -} - class RunStoresScope { #stack; constructor(activeChannel, data) { - // TODO: use native DisposableStack once supported in core - const stack = []; + // eslint-disable-next-line no-restricted-globals + using stack = new DisposableStack(); // Enter stores using withScope if (activeChannel._stores) { @@ -132,11 +118,7 @@ class RunStoresScope { } } - try { - ArrayPrototypePush(stack, store.withScope(newContext)); - } catch (err) { - disposeStoreScopes(stack, err, true); - } + stack.use(store.withScope(newContext)); } } @@ -144,17 +126,11 @@ class RunStoresScope { activeChannel.publish(data); // Transfer ownership of the stack - // TODO: Restore stack.move() when DisposableStack is restored in core to avoid broken behavior. - this.#stack = stack; + this.#stack = stack.move(); } [SymbolDispose]() { - if (this.#stack !== undefined) { - const stack = this.#stack; - this.#stack = undefined; - - disposeStoreScopes(stack); - } + this.#stack[SymbolDispose](); } } @@ -230,12 +206,9 @@ class ActiveChannel { } runStores(data, fn, thisArg, ...args) { - const scope = this.withStoreScope(data); - try { - return ReflectApply(fn, thisArg, args); - } finally { - scope[SymbolDispose](); - } + // eslint-disable-next-line no-unused-vars + using scope = this.withStoreScope(data); + return ReflectApply(fn, thisArg, args); } } @@ -431,12 +404,9 @@ class BoundedChannel { run(context, fn, thisArg, ...args) { context ??= {}; - const scope = this.withScope(context); - try { - return ReflectApply(fn, thisArg, args); - } finally { - scope[SymbolDispose](); - } + // eslint-disable-next-line no-unused-vars + using scope = this.withScope(context); + return ReflectApply(fn, thisArg, args); } } @@ -562,8 +532,9 @@ class TracingChannel { } const { error } = this; - const scope = this.#callWindow.withScope(context); + // eslint-disable-next-line no-unused-vars + using scope = this.#callWindow.withScope(context); try { const result = ReflectApply(fn, thisArg, args); context.result = result; @@ -572,8 +543,6 @@ class TracingChannel { context.error = err; error.publish(context); throw err; - } finally { - scope[SymbolDispose](); } } @@ -597,9 +566,9 @@ class TracingChannel { context.error = err; error.publish(context); // Use continuation window for asyncStart/asyncEnd - const scope = continuationWindow.withScope(context); + // eslint-disable-next-line no-unused-vars + using scope = continuationWindow.withScope(context); // TODO: Is there a way to have asyncEnd _after_ the continuation? - scope[SymbolDispose](); } function onRejectWithRethrow(err) { @@ -610,14 +579,14 @@ class TracingChannel { function onResolve(result) { context.result = result; // Use continuation window for asyncStart/asyncEnd - const scope = continuationWindow.withScope(context); + // eslint-disable-next-line no-unused-vars + using scope = continuationWindow.withScope(context); // TODO: Is there a way to have asyncEnd _after_ the continuation? - scope[SymbolDispose](); return result; } - const scope = this.#callWindow.withScope(context); - + // eslint-disable-next-line no-unused-vars + using scope = this.#callWindow.withScope(context); try { const result = ReflectApply(fn, thisArg, args); // If the return value is not a thenable, return it directly with a warning. @@ -642,8 +611,6 @@ class TracingChannel { context.error = err; error.publish(context); throw err; - } finally { - scope[SymbolDispose](); } } @@ -668,28 +635,23 @@ class TracingChannel { } // Use continuation window for asyncStart/asyncEnd around callback - const scope = continuationWindow.withScope(context); - try { - return ReflectApply(callback, this, arguments); - } finally { - scope[SymbolDispose](); - } + // eslint-disable-next-line no-unused-vars + using scope = continuationWindow.withScope(context); + return ReflectApply(callback, this, arguments); } const callback = ArrayPrototypeAt(args, position); validateFunction(callback, 'callback'); ArrayPrototypeSplice(args, position, 1, wrappedCallback); - const scope = this.#callWindow.withScope(context); - + // eslint-disable-next-line no-unused-vars + using scope = this.#callWindow.withScope(context); try { return ReflectApply(fn, thisArg, args); } catch (err) { context.error = err; error.publish(context); throw err; - } finally { - scope[SymbolDispose](); } } } From 63cd7e39985df2498a69f3016e25b9f8b7a3ac95 Mon Sep 17 00:00:00 2001 From: LiviaMedeiros Date: Tue, 17 Nov 2026 06:33:45 +0800 Subject: [PATCH 3/3] diagnostics_channel: use `DisposableStack` primordial Signed-off-by: LiviaMedeiros --- lib/diagnostics_channel.js | 8 +++++--- lib/eslint.config_partial.mjs | 2 ++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/diagnostics_channel.js b/lib/diagnostics_channel.js index dd600d804fd9..61d310d383a9 100644 --- a/lib/diagnostics_channel.js +++ b/lib/diagnostics_channel.js @@ -7,6 +7,9 @@ const { ArrayPrototypePushApply, ArrayPrototypeSlice, ArrayPrototypeSplice, + DisposableStack, + DisposableStackPrototypeMove, + DisposableStackPrototypeUse, ObjectDefineProperty, ObjectGetPrototypeOf, ObjectSetPrototypeOf, @@ -97,7 +100,6 @@ class RunStoresScope { #stack; constructor(activeChannel, data) { - // eslint-disable-next-line no-restricted-globals using stack = new DisposableStack(); // Enter stores using withScope @@ -118,7 +120,7 @@ class RunStoresScope { } } - stack.use(store.withScope(newContext)); + DisposableStackPrototypeUse(stack, store.withScope(newContext)); } } @@ -126,7 +128,7 @@ class RunStoresScope { activeChannel.publish(data); // Transfer ownership of the stack - this.#stack = stack.move(); + this.#stack = DisposableStackPrototypeMove(stack); } [SymbolDispose]() { diff --git a/lib/eslint.config_partial.mjs b/lib/eslint.config_partial.mjs index bbb0655bdcfc..950e0a17099d 100644 --- a/lib/eslint.config_partial.mjs +++ b/lib/eslint.config_partial.mjs @@ -437,6 +437,7 @@ export default [ { name: 'AggregateError' }, { name: 'Array' }, { name: 'ArrayBuffer' }, + { name: 'AsyncDisposableStack' }, { name: 'Atomics' }, { name: 'BigInt' }, { name: 'BigInt64Array' }, @@ -444,6 +445,7 @@ export default [ { name: 'Boolean' }, { name: 'DataView' }, { name: 'Date' }, + { name: 'DisposableStack' }, { name: 'decodeURI' }, { name: 'decodeURIComponent' }, { name: 'encodeURI' },