Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 26 additions & 62 deletions lib/diagnostics_channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const {
ArrayPrototypePushApply,
ArrayPrototypeSlice,
ArrayPrototypeSplice,
DisposableStack,
DisposableStackPrototypeMove,
DisposableStackPrototypeUse,
ObjectDefineProperty,
ObjectGetPrototypeOf,
ObjectSetPrototypeOf,
Expand All @@ -20,7 +23,6 @@ const {
} = primordials;

const {
aggregateTwoErrors,
codes: {
ERR_INVALID_ARG_TYPE,
},
Expand Down Expand Up @@ -94,25 +96,11 @@ 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 = [];
using stack = new DisposableStack();

// Enter stores using withScope
if (activeChannel._stores) {
Expand All @@ -132,29 +120,19 @@ class RunStoresScope {
}
}

try {
ArrayPrototypePush(stack, store.withScope(newContext));
} catch (err) {
disposeStoreScopes(stack, err, true);
}
DisposableStackPrototypeUse(stack, store.withScope(newContext));
}
}

// Publish data
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 = DisposableStackPrototypeMove(stack);
}

[SymbolDispose]() {
if (this.#stack !== undefined) {
const stack = this.#stack;
this.#stack = undefined;

disposeStoreScopes(stack);
}
this.#stack[SymbolDispose]();
}
}

Expand Down Expand Up @@ -230,12 +208,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);
}
}

Expand Down Expand Up @@ -431,12 +406,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);
}
}

Expand Down Expand Up @@ -562,8 +534,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;
Expand All @@ -572,8 +545,6 @@ class TracingChannel {
context.error = err;
error.publish(context);
throw err;
} finally {
scope[SymbolDispose]();
}
}

Expand All @@ -597,9 +568,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) {
Expand All @@ -610,14 +581,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.
Expand All @@ -642,8 +613,6 @@ class TracingChannel {
context.error = err;
error.publish(context);
throw err;
} finally {
scope[SymbolDispose]();
}
}

Expand All @@ -668,28 +637,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]();
}
}
}
Expand Down
12 changes: 2 additions & 10 deletions lib/eslint.config_partial.mjs
Comment thread
Renegade334 marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -447,13 +437,15 @@ export default [
{ name: 'AggregateError' },
{ name: 'Array' },
{ name: 'ArrayBuffer' },
{ name: 'AsyncDisposableStack' },
{ name: 'Atomics' },
{ name: 'BigInt' },
{ name: 'BigInt64Array' },
{ name: 'BigUint64Array' },
{ name: 'Boolean' },
{ name: 'DataView' },
{ name: 'Date' },
{ name: 'DisposableStack' },
{ name: 'decodeURI' },
{ name: 'decodeURIComponent' },
{ name: 'encodeURI' },
Expand Down
35 changes: 24 additions & 11 deletions lib/internal/freeze_intrinsics.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const {
ArrayPrototype,
ArrayPrototypeForEach,
ArrayPrototypePush,
AsyncDisposableStack,
AsyncDisposableStackPrototype,
AsyncFunctionPrototype,
AsyncGeneratorFunctionPrototype,
AsyncGeneratorFunctionPrototypePrototype,
Expand All @@ -48,6 +50,8 @@ const {
DataViewPrototype,
Date,
DatePrototype,
DisposableStack,
DisposableStackPrototype,
Error,
ErrorPrototype,
EvalError,
Expand Down Expand Up @@ -106,6 +110,8 @@ const {
String,
StringIteratorPrototype,
StringPrototype,
SuppressedError,
SuppressedErrorPrototype,
Symbol,
SymbolPrototype,
SyntaxError,
Expand Down Expand Up @@ -171,6 +177,7 @@ module.exports = function() {
EvalErrorPrototype,
RangeErrorPrototype,
ReferenceErrorPrototype,
SuppressedErrorPrototype,
SyntaxErrorPrototype,
TypeErrorPrototype,
URIErrorPrototype,
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -270,6 +279,7 @@ module.exports = function() {
EvalError,
RangeError,
ReferenceError,
SuppressedError,
SyntaxError,
TypeError,
URIError,
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/internal/per_context/primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,14 @@ function copyPrototype(src, dest, prefix) {
'AggregateError',
'Array',
'ArrayBuffer',
'AsyncDisposableStack',
'BigInt',
'BigInt64Array',
'BigUint64Array',
'Boolean',
'DataView',
'Date',
'DisposableStack',
Comment thread
LiviaMedeiros marked this conversation as resolved.
Resolved
'Error',
'EvalError',
'FinalizationRegistry',
Expand All @@ -212,6 +214,7 @@ function copyPrototype(src, dest, prefix) {
'RegExp',
'Set',
'String',
'SuppressedError',
'Symbol',
'SyntaxError',
'TypeError',
Expand Down
18 changes: 18 additions & 0 deletions typings/primordials.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@ declare namespace primordials {
export const ArrayBufferPrototypeSlice: UncurryThis<typeof ArrayBuffer.prototype.slice>
export const ArrayBufferPrototypeTransfer: UncurryThis<typeof ArrayBuffer.prototype.transfer>
export const ArrayBufferPrototypeGetByteLength: UncurryGetter<typeof ArrayBuffer.prototype , "byteLength">;
export import AsyncDisposableStack = globalThis.AsyncDisposableStack,
export const AsyncDisposableStackPrototype: typeof AsyncDisposableStack.prototype
export const AsyncDisposableStackAdopt: UncurryThis<typeof AsyncDisposableStack.prototype.adopt>
export const AsyncDisposableStackDefer: UncurryThis<typeof AsyncDisposableStack.prototype.defer>
export const AsyncDisposableStackDispose: UncurryThis<typeof AsyncDisposableStack.prototype.dispose>
export const AsyncDisposableStackMove: UncurryThis<typeof AsyncDisposableStack.prototype.move>
export const AsyncDisposableStackUse: UncurryThis<typeof AsyncDisposableStack.prototype.use>
export const AsyncDisposableStackSymbolAsyncDispose: UncurryMethod<typeof AsyncDisposableStack.prototype, typeof Symbol.asyncDispose>;
export import BigInt = globalThis.BigInt;
export const BigIntPrototype: typeof BigInt.prototype
export const BigIntAsUintN: typeof BigInt.asUintN
Expand Down Expand Up @@ -259,6 +267,14 @@ declare namespace primordials {
export const DatePrototypeToLocaleDateString: UncurryThis<typeof Date.prototype.toLocaleDateString>
export const DatePrototypeToLocaleTimeString: UncurryThis<typeof Date.prototype.toLocaleTimeString>
export const DatePrototypeSymbolToPrimitive: UncurryMethod<typeof Date.prototype, typeof Symbol.toPrimitive>;
export import DisposableStack = globalThis.DisposableStack,
export const DisposableStackPrototype: typeof DisposableStack.prototype
export const DisposableStackAdopt: UncurryThis<typeof DisposableStack.prototype.adopt>
export const DisposableStackDefer: UncurryThis<typeof DisposableStack.prototype.defer>
export const DisposableStackDispose: UncurryThis<typeof DisposableStack.prototype.dispose>
export const DisposableStackMove: UncurryThis<typeof DisposableStack.prototype.move>
export const DisposableStackUse: UncurryThis<typeof DisposableStack.prototype.use>
export const DisposableStackSymbolDispose: UncurryMethod<typeof DisposableStack.prototype, typeof Symbol.dispose>;
export import Error = globalThis.Error;
export const ErrorPrototype: typeof Error.prototype
// @ts-ignore
Expand Down Expand Up @@ -444,6 +460,8 @@ declare namespace primordials {
export const StringPrototypeToWellFormed: UncurryThis<typeof String.prototype.toWellFormed>
export const StringPrototypeValueOf: UncurryThis<typeof String.prototype.valueOf>
export const StringPrototypeReplaceAll: UncurryThis<typeof String.prototype.replaceAll>
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
Expand Down
Loading