Skip to content

Commit 157dc38

Browse files
committed
src: unify the top level resource in JS and C++
Use a shared object as the top level async resource in JS and C++ so that when there is no active async resource, the same top level async resource object is returned from `async_hooks.executionAsyncResource()`.
1 parent c544bd0 commit 157dc38

5 files changed

Lines changed: 14 additions & 5 deletions

File tree

lib/internal/async_hooks.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ const active_hooks = {
7979
tmp_fields: null,
8080
};
8181

82-
const { registerDestroyHook } = async_wrap;
82+
const {
83+
registerDestroyHook,
84+
topLevelResource,
85+
} = async_wrap;
8386
const { enqueueMicrotask } = internalBinding('task_queue');
8487
const { resource_symbol, owner_symbol } = internalBinding('symbols');
8588

@@ -137,8 +140,6 @@ function callbackTrampoline(asyncId, resource, cb, ...args) {
137140
return result;
138141
}
139142

140-
const topLevelResource = {};
141-
142143
function executionAsyncResource() {
143144
// Indicate to the native layer that this function is likely to be used,
144145
// in which case it will inform JS about the current async resource via
@@ -597,6 +598,7 @@ module.exports = {
597598
clearAsyncIdStack,
598599
hasAsyncIdStack,
599600
executionAsyncResource,
601+
topLevelResource,
600602
// Internal Embedder API
601603
newAsyncId,
602604
getOrSetAsyncId,

src/async_wrap.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,9 @@ void AsyncWrap::CreatePerContextProperties(Local<Object> target,
412412
"execution_async_resources",
413413
env->async_hooks()->js_execution_async_resources());
414414

415+
FORCE_SET_TARGET_FIELD(
416+
target, "topLevelResource", realm->async_hooks_top_level_resource());
417+
415418
target->Set(context,
416419
env->async_ids_stack_string(),
417420
env->async_hooks()->async_ids_stack().GetJSArray()).Check();

src/env_properties.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@
404404
V(async_hooks_destroy_function, v8::Function) \
405405
V(async_hooks_init_function, v8::Function) \
406406
V(async_hooks_promise_resolve_function, v8::Function) \
407+
V(async_hooks_top_level_resource, v8::Object) \
407408
V(buffer_prototype_object, v8::Object) \
408409
V(crypto_key_object_constructor, v8::Function) \
409410
V(crypto_key_object_private_constructor, v8::Function) \

src/node.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,8 @@ std::optional<StartExecutionCallbackInfo> CallbackInfoFromArray(
311311
MaybeLocal<Value> StartExecution(Environment* env, StartExecutionCallback cb) {
312312
InternalCallbackScope callback_scope(
313313
env,
314-
Object::New(env->isolate()),
315-
{ 1, 0 },
314+
env->principal_realm()->async_hooks_top_level_resource(),
315+
{1, 0},
316316
InternalCallbackScope::kSkipAsyncHooks);
317317

318318
// Only snapshot builder or embedder applications set the

src/node_realm.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ void Realm::CreateProperties() {
8181
Local<Object> process_object =
8282
node::CreateProcessObject(this).FromMaybe(Local<Object>());
8383
set_process_object(process_object);
84+
85+
set_async_hooks_top_level_resource(
86+
Object::New(isolate_, v8::Null(isolate_), nullptr, nullptr, 0));
8487
}
8588

8689
RealmSerializeInfo Realm::Serialize(SnapshotCreator* creator) {

0 commit comments

Comments
 (0)