Skip to content

Commit

Permalink
feat: async context (#24402)
Browse files Browse the repository at this point in the history
We are switching to ContinuationPreservedEmbedderData. This allows
adding async context tracking to the various async operations that deno
provides.

Fixes: #7010
Fixes: #22886
Fixes: #24368
  • Loading branch information
devsnek authored Aug 2, 2024
1 parent b82a2f1 commit 3a1a1cc
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 294 deletions.
10 changes: 0 additions & 10 deletions ext/node/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,6 @@ fn op_node_build_os() -> 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]
#[string]
fn op_npm_process_state(state: &mut OpState) -> Result<String, AnyError> {
Expand Down Expand Up @@ -350,7 +341,6 @@ deno_core::extension!(deno_node,
ops::os::op_cpus<P>,
ops::os::op_homedir<P>,
op_node_build_os,
op_node_is_promise_rejected,
op_npm_process_state,
ops::require::op_require_init_paths,
ops::require::op_require_node_module_paths<P>,
Expand Down
15 changes: 12 additions & 3 deletions ext/node/polyfills/_next_tick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
// deno-lint-ignore-file prefer-primordials

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 { _exiting } from "ext:deno_node/_process/exiting.ts";
Expand All @@ -13,6 +17,7 @@ import { FixedQueue } from "ext:deno_node/internal/fixed_queue.ts";
interface Tock {
callback: (...args: Array<unknown>) => void;
args: Array<unknown>;
snapshot: unknown;
}

let nextTickEnabled = false;
Expand All @@ -23,17 +28,19 @@ export function enableNextTick() {
const queue = new FixedQueue();

export function processTicksAndRejections() {
let tock;
let tock: Tock;
do {
// deno-lint-ignore no-cond-assign
while (tock = queue.shift()) {
// FIXME(bartlomieju): Deno currently doesn't support async hooks
// const asyncId = tock[async_id_symbol];
// emitBefore(asyncId, tock[trigger_async_id_symbol], tock);

const oldContext = getAsyncContext();
try {
const callback = (tock as Tock).callback;
if ((tock as Tock).args === undefined) {
setAsyncContext(tock.snapshot);
const callback = tock.callback;
if (tock.args === undefined) {
callback();
} else {
const args = (tock as Tock).args;
Expand All @@ -58,6 +65,7 @@ export function processTicksAndRejections() {
// FIXME(bartlomieju): Deno currently doesn't support async hooks
// if (destroyHooksExist())
// emitDestroy(asyncId);
setAsyncContext(oldContext);
}

// FIXME(bartlomieju): Deno currently doesn't support async hooks
Expand Down Expand Up @@ -143,6 +151,7 @@ export function nextTick<T extends Array<unknown>>(
// FIXME(bartlomieju): Deno currently doesn't support async hooks
// [async_id_symbol]: asyncId,
// [trigger_async_id_symbol]: triggerAsyncId,
snapshot: getAsyncContext(),
callback,
args: args_,
};
Expand Down
Loading

0 comments on commit 3a1a1cc

Please # to comment.