As N-API calls are made, handles to objects in the heap for the underlying VM may be returned as napi_values. These handles must hold the objects ‘live’ until they are no longer required by the native code, otherwise the objects could be collected before the native code was finished using them.

    As object handles are returned they are associated with a ‘scope’. The lifespan for the default scope is tied to the lifespan of the native method call. The result is that, by default, handles remain valid and the objects associated with these handles will be held live for the lifespan of the native method call.

    In many cases, however, it is necessary that the handles remain valid for either a shorter or longer lifespan than that of the native method. The sections which follow describe the N-API functions that can be used to change the handle lifespan from the default.