Integrate C++ AsyncHooks Embedder API with native abstraction

  • Version: v8.x
  • Platform: all
  • Subsystem: n-api, nan, async_hooks

The AsyncHooks Embedder API has now been merged, we need to integrate this into N-API and NAN such that userland add-ons can inform async_hooks about the context.

I'm not very familiar with either APIs, but NAN is the API I know the best, so I will explain it from that perspective.

AsyncHooks allows userland to get notified about all asynchronous event and understand what caused the asynchronous job to be tasked. This requires 4 events to be emitted:

  • init: emitted with the asynchronous job is created (called a resource). EmitAsyncInit emits this.
  • before, after: emitted with the asynchronous job calls back, this can happen multiple times. MakeCallback now emits these when two additional parameters are passed (async_id and trigger_id).
  • destroy: emitted when the resource can't call back anymore. EmitAsyncDestroy emits this.

there is also a high-level API, a C++ class called AsyncResource but I suspect this isn't useful for NAN or N-API.

In terms of NAN I think there is almost a 1 to 1 mapping between Nan::Callback and the AsyncHooks API. I believe the following changes should be made:

This is very similar to the AsyncResource class. It is not clear what the resource should be as Callback::Callback does not take such a parameter.

I believe @mhdawson said during a diagnostics meeting that if NAN required changes then likely N-API would need changes too.

/cc @mhdawson @addaleax @trevnorris @nodejs/diagnostics @nodejs/n-api @nodejs/addon-api @nodejs/nan (@kkoopa)