Skip to content

Commit bd42ba0

Browse files
committed
async-wrap: set flags using functions
Setting flags using cryptic numeric object fields is confusing. Instead use much simpler .enable()/.disable() calls on the async_wrap object. PR-URL: #1614 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent 4b2c786 commit bd42ba0

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

src/async-wrap.cc

+21-23
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,28 @@ using v8::kExternalUint32Array;
2323

2424
namespace node {
2525

26+
static void EnableHooksJS(const FunctionCallbackInfo<Value>& args) {
27+
Environment* env = Environment::GetCurrent(args);
28+
env->async_hooks()->set_enable_callbacks(1);
29+
}
30+
31+
32+
static void DisableHooksJS(const FunctionCallbackInfo<Value>& args) {
33+
Environment* env = Environment::GetCurrent(args);
34+
env->async_hooks()->set_enable_callbacks(0);
35+
}
36+
37+
2638
static void SetupHooks(const FunctionCallbackInfo<Value>& args) {
27-
Environment* env = Environment::GetCurrent(args.GetIsolate());
39+
Environment* env = Environment::GetCurrent(args);
2840

29-
CHECK(args[0]->IsObject());
41+
CHECK(args[0]->IsFunction());
3042
CHECK(args[1]->IsFunction());
3143
CHECK(args[2]->IsFunction());
32-
CHECK(args[3]->IsFunction());
33-
34-
// Attach Fields enum from Environment::AsyncHooks.
35-
// Flags attached to this object are:
36-
// - kCallInitHook (0): Tells the AsyncWrap constructor whether it should
37-
// make a call to the init JS callback. This is disabled by default, so
38-
// even after setting the callbacks the flag will have to be set to
39-
// non-zero to have those callbacks called. This only affects the init
40-
// callback. If the init callback was called, then the pre/post callbacks
41-
// will automatically be called.
42-
Local<Object> async_hooks_obj = args[0].As<Object>();
43-
Environment::AsyncHooks* async_hooks = env->async_hooks();
44-
async_hooks_obj->SetIndexedPropertiesToExternalArrayData(
45-
async_hooks->fields(),
46-
kExternalUint32Array,
47-
async_hooks->fields_count());
48-
49-
env->set_async_hooks_init_function(args[1].As<Function>());
50-
env->set_async_hooks_pre_function(args[2].As<Function>());
51-
env->set_async_hooks_post_function(args[3].As<Function>());
44+
45+
env->set_async_hooks_init_function(args[0].As<Function>());
46+
env->set_async_hooks_pre_function(args[1].As<Function>());
47+
env->set_async_hooks_post_function(args[2].As<Function>());
5248

5349
env->set_using_asyncwrap(true);
5450
}
@@ -61,7 +57,9 @@ static void Initialize(Handle<Object> target,
6157
Isolate* isolate = env->isolate();
6258
HandleScope scope(isolate);
6359

64-
NODE_SET_METHOD(target, "setupHooks", SetupHooks);
60+
env->SetMethod(target, "setupHooks", SetupHooks);
61+
env->SetMethod(target, "disable", DisableHooksJS);
62+
env->SetMethod(target, "enable", EnableHooksJS);
6563

6664
Local<Object> async_providers = Object::New(isolate);
6765
#define V(PROVIDER) \

0 commit comments

Comments
 (0)