Skip to content

Commit e780bb8

Browse files
committed
store resource on AsyncHooks object
1 parent 3756df1 commit e780bb8

3 files changed

Lines changed: 29 additions & 11 deletions

File tree

src/async_wrap.cc

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@
2828

2929
#include "v8.h"
3030

31-
// TODO(mcollina): should this be moved in a more public space?
32-
// embedders will not be able to use this field
33-
#define EXECUTION_RESOURCE_FIELD 10
34-
3531
using v8::Context;
3632
using v8::DontDelete;
3733
using v8::EscapableHandleScope;
@@ -152,7 +148,7 @@ void AsyncWrap::EmitTraceEventBefore() {
152148
void AsyncWrap::EmitBefore(Environment* env, double async_id,
153149
v8::Local<v8::Object> resource) {
154150
v8::Local<v8::Context> context = env->isolate()->GetCurrentContext();
155-
context->SetEmbedderData(EXECUTION_RESOURCE_FIELD, resource);
151+
env->async_hooks()->set_execution_async_resource(resource);
156152

157153
Emit(env, async_id, AsyncHooks::kBefore,
158154
env->async_hooks_before_function());
@@ -184,7 +180,7 @@ void AsyncWrap::EmitAfter(Environment* env, double async_id) {
184180
Emit(env, async_id, AsyncHooks::kAfter,
185181
env->async_hooks_after_function());
186182

187-
context->SetEmbedderData(EXECUTION_RESOURCE_FIELD, v8::Null(isolate));
183+
env->async_hooks()->clear_execution_async_resource();
188184
}
189185

190186
class PromiseWrap : public AsyncWrap {
@@ -270,7 +266,7 @@ static void PromiseHook(PromiseHookType type, Local<Promise> promise,
270266

271267
// needed for async functions :/
272268
// the top level will not emit before and after
273-
env->context()->SetEmbedderData(EXECUTION_RESOURCE_FIELD, wrap->object());
269+
env->async_hooks()->set_execution_async_resource(wrap->object());
274270
}
275271
}
276272

@@ -401,14 +397,16 @@ static void RegisterDestroyHook(const FunctionCallbackInfo<Value>& args) {
401397

402398
static void GetExecutionAsyncResource(const FunctionCallbackInfo<Value>& args) {
403399
Isolate* isolate = args.GetIsolate();
400+
Environment* env = Environment::GetCurrent(args);
404401
v8::Local<v8::Context> context = isolate->GetCurrentContext();
405-
args.GetReturnValue().Set(context->GetEmbedderData(EXECUTION_RESOURCE_FIELD));
402+
args.GetReturnValue().Set(env->async_hooks()->get_execution_async_resource());
406403
}
407404

408405
static void SetExecutionAsyncResource(const FunctionCallbackInfo<Value>& args) {
409406
Isolate* isolate = args.GetIsolate();
407+
Environment* env = Environment::GetCurrent(args);
410408
v8::Local<v8::Context> context = isolate->GetCurrentContext();
411-
context->SetEmbedderData(EXECUTION_RESOURCE_FIELD, args[0]);
409+
env->async_hooks()->set_execution_async_resource(args[0]);
412410
}
413411

414412
void AsyncWrap::GetAsyncId(const FunctionCallbackInfo<Value>& args) {
@@ -502,7 +500,7 @@ void AsyncWrap::Initialize(Local<Object> target,
502500
env->SetMethod(target, "setExecutionAsyncResource",
503501
SetExecutionAsyncResource);
504502

505-
context->SetEmbedderData(EXECUTION_RESOURCE_FIELD, v8::Null(isolate));
503+
env->async_hooks()->clear_execution_async_resource();
506504

507505
PropertyAttribute ReadOnlyDontDelete =
508506
static_cast<PropertyAttribute>(ReadOnly | DontDelete);

src/env-inl.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ inline MultiIsolatePlatform* IsolateData::platform() const {
6767
inline AsyncHooks::AsyncHooks()
6868
: async_ids_stack_(env()->isolate(), 16 * 2),
6969
fields_(env()->isolate(), kFieldsCount),
70-
async_id_fields_(env()->isolate(), kUidFieldsCount) {
70+
async_id_fields_(env()->isolate(), kUidFieldsCount),
71+
execution_async_resource_(env()->isolate(), v8::Null(env()->isolate())) {
7172
v8::HandleScope handle_scope(env()->isolate());
7273

7374
// Always perform async_hooks checks, not just when async_hooks is enabled.
@@ -206,6 +207,18 @@ inline AsyncHooks::DefaultTriggerAsyncIdScope ::~DefaultTriggerAsyncIdScope() {
206207
old_default_trigger_async_id_;
207208
}
208209

210+
inline void AsyncHooks::set_execution_async_resource(
211+
v8::Local<v8::Value> execution_async_resource) {
212+
execution_async_resource_.Reset(env()->isolate(), execution_async_resource);
213+
}
214+
215+
inline v8::Local<v8::Value> AsyncHooks::get_execution_async_resource() {
216+
return execution_async_resource_.Get(env()->isolate());
217+
}
218+
219+
inline void AsyncHooks::clear_execution_async_resource() {
220+
execution_async_resource_.Reset();
221+
}
209222

210223
Environment* Environment::ForAsyncHooks(AsyncHooks* hooks) {
211224
return ContainerOf(&Environment::async_hooks_, hooks);

src/env.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,11 @@ class AsyncHooks : public MemoryRetainer {
684684
inline bool pop_async_id(double async_id);
685685
inline void clear_async_id_stack(); // Used in fatal exceptions.
686686

687+
inline void set_execution_async_resource(
688+
v8::Local<v8::Value> execution_async_resource_);
689+
inline v8::Local<v8::Value> get_execution_async_resource();
690+
inline void clear_execution_async_resource();
691+
687692
AsyncHooks(const AsyncHooks&) = delete;
688693
AsyncHooks& operator=(const AsyncHooks&) = delete;
689694
AsyncHooks(AsyncHooks&&) = delete;
@@ -726,6 +731,8 @@ class AsyncHooks : public MemoryRetainer {
726731
AliasedFloat64Array async_id_fields_;
727732

728733
void grow_async_ids_stack();
734+
735+
v8::Persistent<v8::Value> execution_async_resource_;
729736
};
730737

731738
class ImmediateInfo : public MemoryRetainer {

0 commit comments

Comments
 (0)