Currently the Node-API foreground task runner lifetime is depending on the napi_env lifetime.
This is wrong because some tasks must be run after the napi_env is destroyed.
This issue causes a crash in React Native for Windows when we do direct debugging.
In this PR we are fixing this issue by implementing a new task runner:
Client code must call v8_create_task_runner function to create a new task runner and then pass it to napi_ext_env_settings when creating new napi_env. The code that internally uses v8runtime::JSITaskRunner is responsible for deleting it.
The v8_create_task_runner captures external task scheduler and functions to run tasks and to destroy the external task scheduler.
An additional change in this PR: removed NAPI_EXTERN from all implementations as it may cause issues if the function signature is not the same in the .h and .cpp files. This issue was observed while working on the new v8_create_task_runner function.
Note that the new code has prefix v8_ instead of previously used napi_ext_. The reason is that these functions are V8 JS Engine specific and will be never accepted as a part of the Node-API in Node.JS. Thus, the proposal is to use the v8_ prefix instead. Ideally, we should replace js_native_ext_api.h with a new v8_api.h where all declarations will have the v8_ prefix.
Cherry pick PR #153 to the main branch.
Original PR description:
Currently the Node-API foreground task runner lifetime is depending on the
napi_env
lifetime. This is wrong because some tasks must be run after thenapi_env
is destroyed. This issue causes a crash in React Native for Windows when we do direct debugging.In this PR we are fixing this issue by implementing a new task runner:
v8_create_task_runner
function to create a new task runner and then pass it tonapi_ext_env_settings
when creating newnapi_env
. The code that internally usesv8runtime::JSITaskRunner
is responsible for deleting it.v8_create_task_runner
captures external task scheduler and functions to run tasks and to destroy the external task scheduler.An additional change in this PR: removed
NAPI_EXTERN
from all implementations as it may cause issues if the function signature is not the same in the .h and .cpp files. This issue was observed while working on the newv8_create_task_runner
function.Note that the new code has prefix
v8_
instead of previously usednapi_ext_
. The reason is that these functions are V8 JS Engine specific and will be never accepted as a part of the Node-API in Node.JS. Thus, the proposal is to use thev8_
prefix instead. Ideally, we should replacejs_native_ext_api.h
with a newv8_api.h
where all declarations will have thev8_
prefix.Microsoft Reviewers: Open in CodeFlow