Created
June 15, 2019 23:43
-
-
Save deepal/0509dddc38543451eb5de0dd9ddb8eb7 to your computer and use it in GitHub Desktop.
AH1: Initializing async hooks (bindings between c++ and JS contexts)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| static void SetupHooks(const FunctionCallbackInfo<Value>& args) { | |
| Environment* env = Environment::GetCurrent(args); | |
| CHECK(args[0]->IsObject()); | |
| // All of init, before, after, destroy are supplied by async_hooks | |
| // internally, so this should every only be called once. At which time all | |
| // the functions should be set. Detect this by checking if init !IsEmpty(). | |
| CHECK(env->async_hooks_init_function().IsEmpty()); | |
| Local<Object> fn_obj = args[0].As<Object>(); | |
| #define SET_HOOK_FN(name) \ | |
| do { \ | |
| Local<Value> v = \ | |
| fn_obj->Get(env->context(), \ | |
| FIXED_ONE_BYTE_STRING(env->isolate(), #name)) \ | |
| .ToLocalChecked(); \ | |
| CHECK(v->IsFunction()); \ | |
| env->set_async_hooks_##name##_function(v.As<Function>()); \ | |
| } while (0) | |
| SET_HOOK_FN(init); | |
| SET_HOOK_FN(before); | |
| SET_HOOK_FN(after); | |
| SET_HOOK_FN(destroy); | |
| SET_HOOK_FN(promise_resolve); | |
| #undef SET_HOOK_FN | |
| { | |
| Local<FunctionTemplate> ctor = | |
| FunctionTemplate::New(env->isolate()); | |
| ctor->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "PromiseWrap")); | |
| Local<ObjectTemplate> promise_wrap_template = ctor->InstanceTemplate(); | |
| promise_wrap_template->SetInternalFieldCount( | |
| PromiseWrap::kInternalFieldCount); | |
| promise_wrap_template->SetAccessor( | |
| FIXED_ONE_BYTE_STRING(env->isolate(), "isChainedPromise"), | |
| PromiseWrap::getIsChainedPromise); | |
| env->set_promise_wrap_template(promise_wrap_template); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment