Skip to content

Instantly share code, notes, and snippets.

@deepal
Created June 15, 2019 23:46
Show Gist options
  • Select an option

  • Save deepal/62826f6cecd1071426b040aa2093adee to your computer and use it in GitHub Desktop.

Select an option

Save deepal/62826f6cecd1071426b040aa2093adee to your computer and use it in GitHub Desktop.
Triggering JS init() hooks from c++ async_hooks_init_function
void AsyncWrap::EmitAsyncInit(Environment* env,
Local<Object> object,
Local<String> type,
double async_id,
double trigger_async_id) {
CHECK(!object.IsEmpty());
CHECK(!type.IsEmpty());
AsyncHooks* async_hooks = env->async_hooks();
// Nothing to execute, so can continue normally.
if (async_hooks->fields()[AsyncHooks::kInit] == 0) {
return;
}
HandleScope scope(env->isolate());
Local<Function> init_fn = env->async_hooks_init_function();
Local<Value> argv[] = {
Number::New(env->isolate(), async_id),
type,
Number::New(env->isolate(), trigger_async_id),
object,
};
TryCatchScope try_catch(env, TryCatchScope::CatchMode::kFatal);
USE(init_fn->Call(env->context(), object, arraysize(argv), argv));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment