mirror of
https://github.com/denoland/deno.git
synced 2024-11-24 15:19:26 -05:00
Revert "feat: async context" (#24856)
Reverts denoland/deno#24402 deno_web can't depend on code in runtime
This commit is contained in:
parent
3a1a1cc030
commit
71ca61e189
8 changed files with 294 additions and 208 deletions
|
@ -181,6 +181,15 @@ fn op_node_build_os() -> String {
|
||||||
env!("TARGET").split('-').nth(2).unwrap().to_string()
|
env!("TARGET").split('-').nth(2).unwrap().to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[op2(fast)]
|
||||||
|
fn op_node_is_promise_rejected(value: v8::Local<v8::Value>) -> bool {
|
||||||
|
let Ok(promise) = v8::Local::<v8::Promise>::try_from(value) else {
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
promise.state() == v8::PromiseState::Rejected
|
||||||
|
}
|
||||||
|
|
||||||
#[op2]
|
#[op2]
|
||||||
#[string]
|
#[string]
|
||||||
fn op_npm_process_state(state: &mut OpState) -> Result<String, AnyError> {
|
fn op_npm_process_state(state: &mut OpState) -> Result<String, AnyError> {
|
||||||
|
@ -341,6 +350,7 @@ deno_core::extension!(deno_node,
|
||||||
ops::os::op_cpus<P>,
|
ops::os::op_cpus<P>,
|
||||||
ops::os::op_homedir<P>,
|
ops::os::op_homedir<P>,
|
||||||
op_node_build_os,
|
op_node_build_os,
|
||||||
|
op_node_is_promise_rejected,
|
||||||
op_npm_process_state,
|
op_npm_process_state,
|
||||||
ops::require::op_require_init_paths,
|
ops::require::op_require_init_paths,
|
||||||
ops::require::op_require_node_module_paths<P>,
|
ops::require::op_require_node_module_paths<P>,
|
||||||
|
|
|
@ -5,10 +5,6 @@
|
||||||
// deno-lint-ignore-file prefer-primordials
|
// deno-lint-ignore-file prefer-primordials
|
||||||
|
|
||||||
import { core } from "ext:core/mod.js";
|
import { core } from "ext:core/mod.js";
|
||||||
import {
|
|
||||||
getAsyncContext,
|
|
||||||
setAsyncContext,
|
|
||||||
} from "ext:runtime/01_async_context.js";
|
|
||||||
|
|
||||||
import { validateFunction } from "ext:deno_node/internal/validators.mjs";
|
import { validateFunction } from "ext:deno_node/internal/validators.mjs";
|
||||||
import { _exiting } from "ext:deno_node/_process/exiting.ts";
|
import { _exiting } from "ext:deno_node/_process/exiting.ts";
|
||||||
|
@ -17,7 +13,6 @@ import { FixedQueue } from "ext:deno_node/internal/fixed_queue.ts";
|
||||||
interface Tock {
|
interface Tock {
|
||||||
callback: (...args: Array<unknown>) => void;
|
callback: (...args: Array<unknown>) => void;
|
||||||
args: Array<unknown>;
|
args: Array<unknown>;
|
||||||
snapshot: unknown;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let nextTickEnabled = false;
|
let nextTickEnabled = false;
|
||||||
|
@ -28,7 +23,7 @@ export function enableNextTick() {
|
||||||
const queue = new FixedQueue();
|
const queue = new FixedQueue();
|
||||||
|
|
||||||
export function processTicksAndRejections() {
|
export function processTicksAndRejections() {
|
||||||
let tock: Tock;
|
let tock;
|
||||||
do {
|
do {
|
||||||
// deno-lint-ignore no-cond-assign
|
// deno-lint-ignore no-cond-assign
|
||||||
while (tock = queue.shift()) {
|
while (tock = queue.shift()) {
|
||||||
|
@ -36,11 +31,9 @@ export function processTicksAndRejections() {
|
||||||
// const asyncId = tock[async_id_symbol];
|
// const asyncId = tock[async_id_symbol];
|
||||||
// emitBefore(asyncId, tock[trigger_async_id_symbol], tock);
|
// emitBefore(asyncId, tock[trigger_async_id_symbol], tock);
|
||||||
|
|
||||||
const oldContext = getAsyncContext();
|
|
||||||
try {
|
try {
|
||||||
setAsyncContext(tock.snapshot);
|
const callback = (tock as Tock).callback;
|
||||||
const callback = tock.callback;
|
if ((tock as Tock).args === undefined) {
|
||||||
if (tock.args === undefined) {
|
|
||||||
callback();
|
callback();
|
||||||
} else {
|
} else {
|
||||||
const args = (tock as Tock).args;
|
const args = (tock as Tock).args;
|
||||||
|
@ -65,7 +58,6 @@ export function processTicksAndRejections() {
|
||||||
// FIXME(bartlomieju): Deno currently doesn't support async hooks
|
// FIXME(bartlomieju): Deno currently doesn't support async hooks
|
||||||
// if (destroyHooksExist())
|
// if (destroyHooksExist())
|
||||||
// emitDestroy(asyncId);
|
// emitDestroy(asyncId);
|
||||||
setAsyncContext(oldContext);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME(bartlomieju): Deno currently doesn't support async hooks
|
// FIXME(bartlomieju): Deno currently doesn't support async hooks
|
||||||
|
@ -151,7 +143,6 @@ export function nextTick<T extends Array<unknown>>(
|
||||||
// FIXME(bartlomieju): Deno currently doesn't support async hooks
|
// FIXME(bartlomieju): Deno currently doesn't support async hooks
|
||||||
// [async_id_symbol]: asyncId,
|
// [async_id_symbol]: asyncId,
|
||||||
// [trigger_async_id_symbol]: triggerAsyncId,
|
// [trigger_async_id_symbol]: triggerAsyncId,
|
||||||
snapshot: getAsyncContext(),
|
|
||||||
callback,
|
callback,
|
||||||
args: args_,
|
args: args_,
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,34 +1,191 @@
|
||||||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
||||||
// Copyright Joyent and Node contributors. All rights reserved. MIT license.
|
// Copyright Joyent and Node contributors. All rights reserved. MIT license.
|
||||||
|
|
||||||
|
// This implementation is inspired by "workerd" AsyncLocalStorage implementation:
|
||||||
|
// https://github.com/cloudflare/workerd/blob/77fd0ed6ddba184414f0216508fc62b06e716cab/src/workerd/api/node/async-hooks.c++#L9
|
||||||
|
|
||||||
// TODO(petamoriken): enable prefer-primordials for node polyfills
|
// TODO(petamoriken): enable prefer-primordials for node polyfills
|
||||||
// deno-lint-ignore-file prefer-primordials
|
// deno-lint-ignore-file prefer-primordials
|
||||||
|
|
||||||
import { primordials } from "ext:core/mod.js";
|
import { core } from "ext:core/mod.js";
|
||||||
import {
|
import { op_node_is_promise_rejected } from "ext:core/ops";
|
||||||
AsyncVariable,
|
|
||||||
getAsyncContext,
|
|
||||||
setAsyncContext,
|
|
||||||
} from "ext:runtime/01_async_context.js";
|
|
||||||
import { validateFunction } from "ext:deno_node/internal/validators.mjs";
|
import { validateFunction } from "ext:deno_node/internal/validators.mjs";
|
||||||
import { newAsyncId } from "ext:deno_node/internal/async_hooks.ts";
|
import { newAsyncId } from "ext:deno_node/internal/async_hooks.ts";
|
||||||
|
|
||||||
const {
|
function assert(cond: boolean) {
|
||||||
ObjectDefineProperties,
|
if (!cond) throw new Error("Assertion failed");
|
||||||
ReflectApply,
|
}
|
||||||
FunctionPrototypeBind,
|
const asyncContextStack: AsyncContextFrame[] = [];
|
||||||
ArrayPrototypeUnshift,
|
|
||||||
ObjectFreeze,
|
function pushAsyncFrame(frame: AsyncContextFrame) {
|
||||||
} = primordials;
|
asyncContextStack.push(frame);
|
||||||
|
}
|
||||||
|
|
||||||
|
function popAsyncFrame() {
|
||||||
|
if (asyncContextStack.length > 0) {
|
||||||
|
asyncContextStack.pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let rootAsyncFrame: AsyncContextFrame | undefined = undefined;
|
||||||
|
let promiseHooksSet = false;
|
||||||
|
|
||||||
|
const asyncContext = Symbol("asyncContext");
|
||||||
|
|
||||||
|
function setPromiseHooks() {
|
||||||
|
if (promiseHooksSet) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
promiseHooksSet = true;
|
||||||
|
|
||||||
|
const init = (promise: Promise<unknown>) => {
|
||||||
|
const currentFrame = AsyncContextFrame.current();
|
||||||
|
if (!currentFrame.isRoot()) {
|
||||||
|
if (typeof promise[asyncContext] !== "undefined") {
|
||||||
|
throw new Error("Promise already has async context");
|
||||||
|
}
|
||||||
|
AsyncContextFrame.attachContext(promise);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const before = (promise: Promise<unknown>) => {
|
||||||
|
const maybeFrame = promise[asyncContext];
|
||||||
|
if (maybeFrame) {
|
||||||
|
pushAsyncFrame(maybeFrame);
|
||||||
|
} else {
|
||||||
|
pushAsyncFrame(AsyncContextFrame.getRootAsyncContext());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const after = (promise: Promise<unknown>) => {
|
||||||
|
popAsyncFrame();
|
||||||
|
if (!op_node_is_promise_rejected(promise)) {
|
||||||
|
// @ts-ignore promise async context
|
||||||
|
promise[asyncContext] = undefined;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const resolve = (promise: Promise<unknown>) => {
|
||||||
|
const currentFrame = AsyncContextFrame.current();
|
||||||
|
if (
|
||||||
|
!currentFrame.isRoot() && op_node_is_promise_rejected(promise) &&
|
||||||
|
typeof promise[asyncContext] === "undefined"
|
||||||
|
) {
|
||||||
|
AsyncContextFrame.attachContext(promise);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
core.setPromiseHooks(init, before, after, resolve);
|
||||||
|
}
|
||||||
|
|
||||||
|
class AsyncContextFrame {
|
||||||
|
storage: StorageEntry[];
|
||||||
|
constructor(
|
||||||
|
maybeParent?: AsyncContextFrame | null,
|
||||||
|
maybeStorageEntry?: StorageEntry | null,
|
||||||
|
isRoot = false,
|
||||||
|
) {
|
||||||
|
this.storage = [];
|
||||||
|
|
||||||
|
setPromiseHooks();
|
||||||
|
|
||||||
|
const propagate = (parent: AsyncContextFrame) => {
|
||||||
|
parent.storage = parent.storage.filter((entry) => !entry.key.isDead());
|
||||||
|
parent.storage.forEach((entry) => this.storage.push(entry.clone()));
|
||||||
|
|
||||||
|
if (maybeStorageEntry) {
|
||||||
|
const existingEntry = this.storage.find((entry) =>
|
||||||
|
entry.key === maybeStorageEntry.key
|
||||||
|
);
|
||||||
|
if (existingEntry) {
|
||||||
|
existingEntry.value = maybeStorageEntry.value;
|
||||||
|
} else {
|
||||||
|
this.storage.push(maybeStorageEntry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!isRoot) {
|
||||||
|
if (maybeParent) {
|
||||||
|
propagate(maybeParent);
|
||||||
|
} else {
|
||||||
|
propagate(AsyncContextFrame.current());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static tryGetContext(promise: Promise<unknown>) {
|
||||||
|
// @ts-ignore promise async context
|
||||||
|
return promise[asyncContext];
|
||||||
|
}
|
||||||
|
|
||||||
|
static attachContext(promise: Promise<unknown>) {
|
||||||
|
// @ts-ignore promise async context
|
||||||
|
promise[asyncContext] = AsyncContextFrame.current();
|
||||||
|
}
|
||||||
|
|
||||||
|
static getRootAsyncContext() {
|
||||||
|
if (typeof rootAsyncFrame !== "undefined") {
|
||||||
|
return rootAsyncFrame;
|
||||||
|
}
|
||||||
|
|
||||||
|
rootAsyncFrame = new AsyncContextFrame(null, null, true);
|
||||||
|
return rootAsyncFrame;
|
||||||
|
}
|
||||||
|
|
||||||
|
static current() {
|
||||||
|
if (asyncContextStack.length === 0) {
|
||||||
|
return AsyncContextFrame.getRootAsyncContext();
|
||||||
|
}
|
||||||
|
|
||||||
|
return asyncContextStack[asyncContextStack.length - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
static create(
|
||||||
|
maybeParent?: AsyncContextFrame | null,
|
||||||
|
maybeStorageEntry?: StorageEntry | null,
|
||||||
|
) {
|
||||||
|
return new AsyncContextFrame(maybeParent, maybeStorageEntry);
|
||||||
|
}
|
||||||
|
|
||||||
|
static wrap(
|
||||||
|
fn: () => unknown,
|
||||||
|
maybeFrame: AsyncContextFrame | undefined,
|
||||||
|
// deno-lint-ignore no-explicit-any
|
||||||
|
thisArg: any,
|
||||||
|
) {
|
||||||
|
// deno-lint-ignore no-explicit-any
|
||||||
|
return (...args: any) => {
|
||||||
|
const frame = maybeFrame || AsyncContextFrame.current();
|
||||||
|
Scope.enter(frame);
|
||||||
|
try {
|
||||||
|
return fn.apply(thisArg, args);
|
||||||
|
} finally {
|
||||||
|
Scope.exit();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
get(key: StorageKey) {
|
||||||
|
assert(!key.isDead());
|
||||||
|
this.storage = this.storage.filter((entry) => !entry.key.isDead());
|
||||||
|
const entry = this.storage.find((entry) => entry.key === key);
|
||||||
|
if (entry) {
|
||||||
|
return entry.value;
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
isRoot() {
|
||||||
|
return AsyncContextFrame.getRootAsyncContext() == this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export class AsyncResource {
|
export class AsyncResource {
|
||||||
|
frame: AsyncContextFrame;
|
||||||
type: string;
|
type: string;
|
||||||
#snapshot: unknown;
|
|
||||||
#asyncId: number;
|
#asyncId: number;
|
||||||
|
|
||||||
constructor(type: string) {
|
constructor(type: string) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.#snapshot = getAsyncContext();
|
this.frame = AsyncContextFrame.current();
|
||||||
this.#asyncId = newAsyncId();
|
this.#asyncId = newAsyncId();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,38 +198,35 @@ export class AsyncResource {
|
||||||
thisArg: unknown,
|
thisArg: unknown,
|
||||||
...args: unknown[]
|
...args: unknown[]
|
||||||
) {
|
) {
|
||||||
const previousContext = getAsyncContext();
|
Scope.enter(this.frame);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
setAsyncContext(this.#snapshot);
|
return fn.apply(thisArg, args);
|
||||||
return ReflectApply(fn, thisArg, args);
|
|
||||||
} finally {
|
} finally {
|
||||||
setAsyncContext(previousContext);
|
Scope.exit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
emitDestroy() {}
|
emitDestroy() {}
|
||||||
|
|
||||||
bind(fn: (...args: unknown[]) => unknown, thisArg) {
|
bind(fn: (...args: unknown[]) => unknown, thisArg = this) {
|
||||||
validateFunction(fn, "fn");
|
validateFunction(fn, "fn");
|
||||||
let bound;
|
const frame = AsyncContextFrame.current();
|
||||||
if (thisArg === undefined) {
|
const bound = AsyncContextFrame.wrap(fn, frame, thisArg);
|
||||||
// deno-lint-ignore no-this-alias
|
|
||||||
const resource = this;
|
Object.defineProperties(bound, {
|
||||||
bound = function (...args) {
|
|
||||||
ArrayPrototypeUnshift(args, fn, this);
|
|
||||||
return ReflectApply(resource.runInAsyncScope, resource, args);
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
bound = FunctionPrototypeBind(this.runInAsyncScope, this, fn, thisArg);
|
|
||||||
}
|
|
||||||
ObjectDefineProperties(bound, {
|
|
||||||
"length": {
|
"length": {
|
||||||
__proto__: null,
|
|
||||||
configurable: true,
|
configurable: true,
|
||||||
enumerable: false,
|
enumerable: false,
|
||||||
value: fn.length,
|
value: fn.length,
|
||||||
writable: false,
|
writable: false,
|
||||||
},
|
},
|
||||||
|
"asyncResource": {
|
||||||
|
configurable: true,
|
||||||
|
enumerable: true,
|
||||||
|
value: this,
|
||||||
|
writable: true,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
return bound;
|
return bound;
|
||||||
}
|
}
|
||||||
|
@ -82,54 +236,95 @@ export class AsyncResource {
|
||||||
type?: string,
|
type?: string,
|
||||||
thisArg?: AsyncResource,
|
thisArg?: AsyncResource,
|
||||||
) {
|
) {
|
||||||
type = type || fn.name || "bound-anonymous-fn";
|
type = type || fn.name;
|
||||||
return (new AsyncResource(type)).bind(fn, thisArg);
|
return (new AsyncResource(type || "AsyncResource")).bind(fn, thisArg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class Scope {
|
||||||
|
static enter(maybeFrame?: AsyncContextFrame) {
|
||||||
|
if (maybeFrame) {
|
||||||
|
pushAsyncFrame(maybeFrame);
|
||||||
|
} else {
|
||||||
|
pushAsyncFrame(AsyncContextFrame.getRootAsyncContext());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static exit() {
|
||||||
|
popAsyncFrame();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class StorageEntry {
|
||||||
|
key: StorageKey;
|
||||||
|
value: unknown;
|
||||||
|
constructor(key: StorageKey, value: unknown) {
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
clone() {
|
||||||
|
return new StorageEntry(this.key, this.value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class StorageKey {
|
||||||
|
#dead = false;
|
||||||
|
|
||||||
|
reset() {
|
||||||
|
this.#dead = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
isDead() {
|
||||||
|
return this.#dead;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const fnReg = new FinalizationRegistry((key: StorageKey) => {
|
||||||
|
key.reset();
|
||||||
|
});
|
||||||
|
|
||||||
export class AsyncLocalStorage {
|
export class AsyncLocalStorage {
|
||||||
#variable = new AsyncVariable();
|
#key;
|
||||||
enabled = false;
|
|
||||||
|
constructor() {
|
||||||
|
this.#key = new StorageKey();
|
||||||
|
fnReg.register(this, this.#key);
|
||||||
|
}
|
||||||
|
|
||||||
// deno-lint-ignore no-explicit-any
|
// deno-lint-ignore no-explicit-any
|
||||||
run(store: any, callback: any, ...args: any[]): any {
|
run(store: any, callback: any, ...args: any[]): any {
|
||||||
this.enabled = true;
|
const frame = AsyncContextFrame.create(
|
||||||
const previous = this.#variable.enter(store);
|
null,
|
||||||
|
new StorageEntry(this.#key, store),
|
||||||
|
);
|
||||||
|
Scope.enter(frame);
|
||||||
|
let res;
|
||||||
try {
|
try {
|
||||||
return ReflectApply(callback, null, args);
|
res = callback(...args);
|
||||||
} finally {
|
} finally {
|
||||||
setAsyncContext(previous);
|
Scope.exit();
|
||||||
}
|
}
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
// deno-lint-ignore no-explicit-any
|
// deno-lint-ignore no-explicit-any
|
||||||
exit(callback: (...args: unknown[]) => any, ...args: any[]): any {
|
exit(callback: (...args: unknown[]) => any, ...args: any[]): any {
|
||||||
if (!this.enabled) {
|
return this.run(undefined, callback, args);
|
||||||
return ReflectApply(callback, null, args);
|
|
||||||
}
|
|
||||||
this.enabled = false;
|
|
||||||
try {
|
|
||||||
return ReflectApply(callback, null, args);
|
|
||||||
} finally {
|
|
||||||
this.enabled = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// deno-lint-ignore no-explicit-any
|
// deno-lint-ignore no-explicit-any
|
||||||
getStore(): any {
|
getStore(): any {
|
||||||
if (!this.enabled) {
|
const currentFrame = AsyncContextFrame.current();
|
||||||
return undefined;
|
return currentFrame.get(this.#key);
|
||||||
}
|
|
||||||
return this.#variable.get();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
enterWith(store: unknown) {
|
enterWith(store: unknown) {
|
||||||
this.enabled = true;
|
const frame = AsyncContextFrame.create(
|
||||||
this.#variable.enter(store);
|
null,
|
||||||
}
|
new StorageEntry(this.#key, store),
|
||||||
|
);
|
||||||
disable() {
|
Scope.enter(frame);
|
||||||
this.enabled = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bind(fn: (...args: unknown[]) => unknown) {
|
static bind(fn: (...args: unknown[]) => unknown) {
|
||||||
|
@ -140,24 +335,14 @@ export class AsyncLocalStorage {
|
||||||
return AsyncLocalStorage.bind((
|
return AsyncLocalStorage.bind((
|
||||||
cb: (...args: unknown[]) => unknown,
|
cb: (...args: unknown[]) => unknown,
|
||||||
...args: unknown[]
|
...args: unknown[]
|
||||||
) => ReflectApply(cb, null, args));
|
) => cb(...args));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function executionAsyncId() {
|
export function executionAsyncId() {
|
||||||
return 0;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function triggerAsyncId() {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function executionAsyncResource() {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
export const asyncWrapProviders = ObjectFreeze({ __proto__: null });
|
|
||||||
|
|
||||||
class AsyncHook {
|
class AsyncHook {
|
||||||
enable() {
|
enable() {
|
||||||
}
|
}
|
||||||
|
@ -170,12 +355,12 @@ export function createHook() {
|
||||||
return new AsyncHook();
|
return new AsyncHook();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Placing all exports down here because the exported classes won't export
|
||||||
|
// otherwise.
|
||||||
export default {
|
export default {
|
||||||
AsyncLocalStorage,
|
// Embedder API
|
||||||
createHook,
|
|
||||||
executionAsyncId,
|
|
||||||
triggerAsyncId,
|
|
||||||
executionAsyncResource,
|
|
||||||
asyncWrapProviders,
|
|
||||||
AsyncResource,
|
AsyncResource,
|
||||||
|
executionAsyncId,
|
||||||
|
createHook,
|
||||||
|
AsyncLocalStorage,
|
||||||
};
|
};
|
||||||
|
|
|
@ -2,10 +2,6 @@
|
||||||
|
|
||||||
import { core, primordials } from "ext:core/mod.js";
|
import { core, primordials } from "ext:core/mod.js";
|
||||||
import { op_defer, op_now } from "ext:core/ops";
|
import { op_defer, op_now } from "ext:core/ops";
|
||||||
import {
|
|
||||||
getAsyncContext,
|
|
||||||
setAsyncContext,
|
|
||||||
} from "ext:runtime/01_async_context.js";
|
|
||||||
const {
|
const {
|
||||||
Uint8Array,
|
Uint8Array,
|
||||||
Uint32Array,
|
Uint32Array,
|
||||||
|
@ -37,16 +33,14 @@ function checkThis(thisArg) {
|
||||||
* Call a callback function immediately.
|
* Call a callback function immediately.
|
||||||
*/
|
*/
|
||||||
function setImmediate(callback, ...args) {
|
function setImmediate(callback, ...args) {
|
||||||
const asyncContext = getAsyncContext();
|
if (args.length > 0) {
|
||||||
return core.queueImmediate(() => {
|
const unboundCallback = callback;
|
||||||
const oldContext = getAsyncContext();
|
callback = () => ReflectApply(unboundCallback, globalThis, args);
|
||||||
try {
|
}
|
||||||
setAsyncContext(asyncContext);
|
|
||||||
return ReflectApply(callback, globalThis, args);
|
return core.queueImmediate(
|
||||||
} finally {
|
callback,
|
||||||
setAsyncContext(oldContext);
|
);
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -59,17 +53,10 @@ function setTimeout(callback, timeout = 0, ...args) {
|
||||||
const unboundCallback = webidl.converters.DOMString(callback);
|
const unboundCallback = webidl.converters.DOMString(callback);
|
||||||
callback = () => indirectEval(unboundCallback);
|
callback = () => indirectEval(unboundCallback);
|
||||||
}
|
}
|
||||||
const unboundCallback = callback;
|
if (args.length > 0) {
|
||||||
const asyncContext = getAsyncContext();
|
const unboundCallback = callback;
|
||||||
callback = () => {
|
callback = () => ReflectApply(unboundCallback, globalThis, args);
|
||||||
const oldContext = getAsyncContext();
|
}
|
||||||
try {
|
|
||||||
setAsyncContext(asyncContext);
|
|
||||||
ReflectApply(unboundCallback, globalThis, args);
|
|
||||||
} finally {
|
|
||||||
setAsyncContext(oldContext);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
timeout = webidl.converters.long(timeout);
|
timeout = webidl.converters.long(timeout);
|
||||||
return core.queueUserTimer(
|
return core.queueUserTimer(
|
||||||
core.getTimerDepth() + 1,
|
core.getTimerDepth() + 1,
|
||||||
|
@ -88,17 +75,10 @@ function setInterval(callback, timeout = 0, ...args) {
|
||||||
const unboundCallback = webidl.converters.DOMString(callback);
|
const unboundCallback = webidl.converters.DOMString(callback);
|
||||||
callback = () => indirectEval(unboundCallback);
|
callback = () => indirectEval(unboundCallback);
|
||||||
}
|
}
|
||||||
const unboundCallback = callback;
|
if (args.length > 0) {
|
||||||
const asyncContext = getAsyncContext();
|
const unboundCallback = callback;
|
||||||
callback = () => {
|
callback = () => ReflectApply(unboundCallback, globalThis, args);
|
||||||
const oldContext = getAsyncContext(asyncContext);
|
}
|
||||||
try {
|
|
||||||
setAsyncContext(asyncContext);
|
|
||||||
ReflectApply(unboundCallback, globalThis, args);
|
|
||||||
} finally {
|
|
||||||
setAsyncContext(oldContext);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
timeout = webidl.converters.long(timeout);
|
timeout = webidl.converters.long(timeout);
|
||||||
return core.queueUserTimer(
|
return core.queueUserTimer(
|
||||||
core.getTimerDepth() + 1,
|
core.getTimerDepth() + 1,
|
||||||
|
|
|
@ -1,45 +0,0 @@
|
||||||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
||||||
|
|
||||||
import { primordials } from "ext:core/mod.js";
|
|
||||||
import { op_get_extras_binding_object } from "ext:core/ops";
|
|
||||||
|
|
||||||
const {
|
|
||||||
SafeWeakMap,
|
|
||||||
} = primordials;
|
|
||||||
|
|
||||||
const {
|
|
||||||
getContinuationPreservedEmbedderData,
|
|
||||||
setContinuationPreservedEmbedderData,
|
|
||||||
} = op_get_extras_binding_object();
|
|
||||||
|
|
||||||
let counter = 0;
|
|
||||||
|
|
||||||
export const getAsyncContext = getContinuationPreservedEmbedderData;
|
|
||||||
export const setAsyncContext = setContinuationPreservedEmbedderData;
|
|
||||||
|
|
||||||
export class AsyncVariable {
|
|
||||||
#id = counter++;
|
|
||||||
#data = new SafeWeakMap();
|
|
||||||
|
|
||||||
enter(value) {
|
|
||||||
const previousContextMapping = getAsyncContext();
|
|
||||||
const entry = { id: this.#id };
|
|
||||||
const asyncContextMapping = {
|
|
||||||
__proto__: null,
|
|
||||||
...previousContextMapping,
|
|
||||||
[this.#id]: entry,
|
|
||||||
};
|
|
||||||
this.#data.set(entry, value);
|
|
||||||
setAsyncContext(asyncContextMapping);
|
|
||||||
return previousContextMapping;
|
|
||||||
}
|
|
||||||
|
|
||||||
get() {
|
|
||||||
const current = getAsyncContext();
|
|
||||||
const entry = current?.[this.#id];
|
|
||||||
if (entry) {
|
|
||||||
return this.#data.get(entry);
|
|
||||||
}
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -2,14 +2,13 @@
|
||||||
|
|
||||||
use deno_core::error::AnyError;
|
use deno_core::error::AnyError;
|
||||||
use deno_core::op2;
|
use deno_core::op2;
|
||||||
use deno_core::v8;
|
|
||||||
use deno_core::ModuleSpecifier;
|
use deno_core::ModuleSpecifier;
|
||||||
use deno_core::OpState;
|
use deno_core::OpState;
|
||||||
use deno_permissions::PermissionsContainer;
|
use deno_permissions::PermissionsContainer;
|
||||||
|
|
||||||
deno_core::extension!(
|
deno_core::extension!(
|
||||||
deno_runtime,
|
deno_runtime,
|
||||||
ops = [op_main_module, op_ppid, op_get_extras_binding_object],
|
ops = [op_main_module, op_ppid],
|
||||||
options = { main_module: ModuleSpecifier },
|
options = { main_module: ModuleSpecifier },
|
||||||
state = |state, options| {
|
state = |state, options| {
|
||||||
state.put::<ModuleSpecifier>(options.main_module);
|
state.put::<ModuleSpecifier>(options.main_module);
|
||||||
|
@ -95,11 +94,3 @@ pub fn op_ppid() -> i64 {
|
||||||
parent_id().into()
|
parent_id().into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[op2]
|
|
||||||
pub fn op_get_extras_binding_object<'a>(
|
|
||||||
scope: &mut v8::HandleScope<'a>,
|
|
||||||
) -> v8::Local<'a, v8::Value> {
|
|
||||||
let context = scope.get_current_context();
|
|
||||||
context.get_extras_binding_object(scope).into()
|
|
||||||
}
|
|
||||||
|
|
|
@ -38,7 +38,6 @@ extension!(runtime,
|
||||||
dir "js",
|
dir "js",
|
||||||
"01_errors.js",
|
"01_errors.js",
|
||||||
"01_version.ts",
|
"01_version.ts",
|
||||||
"01_async_context.js",
|
|
||||||
"06_util.js",
|
"06_util.js",
|
||||||
"10_permissions.js",
|
"10_permissions.js",
|
||||||
"11_workers.js",
|
"11_workers.js",
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
||||||
import { AsyncLocalStorage, AsyncResource } from "node:async_hooks";
|
import { AsyncLocalStorage, AsyncResource } from "node:async_hooks";
|
||||||
import process from "node:process";
|
|
||||||
import { setImmediate } from "node:timers";
|
|
||||||
import { assert, assertEquals } from "@std/assert";
|
import { assert, assertEquals } from "@std/assert";
|
||||||
|
|
||||||
Deno.test(async function foo() {
|
Deno.test(async function foo() {
|
||||||
|
@ -94,7 +92,7 @@ Deno.test(async function enterWith() {
|
||||||
});
|
});
|
||||||
|
|
||||||
assertEquals(await deferred.promise, { x: 2 });
|
assertEquals(await deferred.promise, { x: 2 });
|
||||||
assertEquals(await deferred1.promise, null);
|
assertEquals(await deferred1.promise, { x: 1 });
|
||||||
});
|
});
|
||||||
|
|
||||||
Deno.test(async function snapshot() {
|
Deno.test(async function snapshot() {
|
||||||
|
@ -137,26 +135,3 @@ Deno.test(function emitDestroyStub() {
|
||||||
const resource = new AsyncResource("foo");
|
const resource = new AsyncResource("foo");
|
||||||
assert(typeof resource.emitDestroy === "function");
|
assert(typeof resource.emitDestroy === "function");
|
||||||
});
|
});
|
||||||
|
|
||||||
Deno.test(async function worksWithAsyncAPIs() {
|
|
||||||
const store = new AsyncLocalStorage();
|
|
||||||
const test = () => assertEquals(store.getStore(), "data");
|
|
||||||
await store.run("data", async () => {
|
|
||||||
test();
|
|
||||||
queueMicrotask(() => test());
|
|
||||||
process.nextTick(() => test());
|
|
||||||
setImmediate(() => test());
|
|
||||||
setTimeout(() => test(), 0);
|
|
||||||
const intervalId = setInterval(() => {
|
|
||||||
test();
|
|
||||||
clearInterval(intervalId);
|
|
||||||
}, 0);
|
|
||||||
|
|
||||||
store.run("data2", () => {
|
|
||||||
assertEquals(store.getStore(), "data2");
|
|
||||||
});
|
|
||||||
|
|
||||||
await new Promise((r) => setTimeout(r, 50));
|
|
||||||
test();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
Loading…
Reference in a new issue