Skip to content

Commit

Permalink
store resource on AsyncHooks object
Browse files Browse the repository at this point in the history
  • Loading branch information
Qard committed Dec 19, 2019
1 parent 3756df1 commit e780bb8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
18 changes: 8 additions & 10 deletions src/async_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@

#include "v8.h"

// TODO(mcollina): should this be moved in a more public space?
// embedders will not be able to use this field
#define EXECUTION_RESOURCE_FIELD 10

using v8::Context;
using v8::DontDelete;
using v8::EscapableHandleScope;
Expand Down Expand Up @@ -152,7 +148,7 @@ void AsyncWrap::EmitTraceEventBefore() {
void AsyncWrap::EmitBefore(Environment* env, double async_id,
v8::Local<v8::Object> resource) {
v8::Local<v8::Context> context = env->isolate()->GetCurrentContext();
context->SetEmbedderData(EXECUTION_RESOURCE_FIELD, resource);
env->async_hooks()->set_execution_async_resource(resource);

Emit(env, async_id, AsyncHooks::kBefore,
env->async_hooks_before_function());
Expand Down Expand Up @@ -184,7 +180,7 @@ void AsyncWrap::EmitAfter(Environment* env, double async_id) {
Emit(env, async_id, AsyncHooks::kAfter,
env->async_hooks_after_function());

context->SetEmbedderData(EXECUTION_RESOURCE_FIELD, v8::Null(isolate));
env->async_hooks()->clear_execution_async_resource();
}

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

// needed for async functions :/
// the top level will not emit before and after
env->context()->SetEmbedderData(EXECUTION_RESOURCE_FIELD, wrap->object());
env->async_hooks()->set_execution_async_resource(wrap->object());
}
}

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

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

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

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

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

PropertyAttribute ReadOnlyDontDelete =
static_cast<PropertyAttribute>(ReadOnly | DontDelete);
Expand Down
15 changes: 14 additions & 1 deletion src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ inline MultiIsolatePlatform* IsolateData::platform() const {
inline AsyncHooks::AsyncHooks()
: async_ids_stack_(env()->isolate(), 16 * 2),
fields_(env()->isolate(), kFieldsCount),
async_id_fields_(env()->isolate(), kUidFieldsCount) {
async_id_fields_(env()->isolate(), kUidFieldsCount),
execution_async_resource_(env()->isolate(), v8::Null(env()->isolate())) {
v8::HandleScope handle_scope(env()->isolate());

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

inline void AsyncHooks::set_execution_async_resource(
v8::Local<v8::Value> execution_async_resource) {
execution_async_resource_.Reset(env()->isolate(), execution_async_resource);
}

inline v8::Local<v8::Value> AsyncHooks::get_execution_async_resource() {
return execution_async_resource_.Get(env()->isolate());
}

inline void AsyncHooks::clear_execution_async_resource() {
execution_async_resource_.Reset();
}

Environment* Environment::ForAsyncHooks(AsyncHooks* hooks) {
return ContainerOf(&Environment::async_hooks_, hooks);
Expand Down
7 changes: 7 additions & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,11 @@ class AsyncHooks : public MemoryRetainer {
inline bool pop_async_id(double async_id);
inline void clear_async_id_stack(); // Used in fatal exceptions.

inline void set_execution_async_resource(
v8::Local<v8::Value> execution_async_resource_);
inline v8::Local<v8::Value> get_execution_async_resource();
inline void clear_execution_async_resource();

AsyncHooks(const AsyncHooks&) = delete;
AsyncHooks& operator=(const AsyncHooks&) = delete;
AsyncHooks(AsyncHooks&&) = delete;
Expand Down Expand Up @@ -726,6 +731,8 @@ class AsyncHooks : public MemoryRetainer {
AliasedFloat64Array async_id_fields_;

void grow_async_ids_stack();

v8::Persistent<v8::Value> execution_async_resource_;
};

class ImmediateInfo : public MemoryRetainer {
Expand Down

0 comments on commit e780bb8

Please sign in to comment.