diff options
author | Anna Henningsen <anna@addaleax.net> | 2017-08-08 20:18:16 +0200 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2017-09-14 17:38:45 +0200 |
commit | 8c8c90b714756433b3333146a2893572b473dfd6 (patch) | |
tree | bb10f495f0bf76e50f2f144b8e657097463f0e3a /src | |
parent | a564c1eb20a6742a50dca283312d9881a8f1fca6 (diff) | |
download | android-node-v8-8c8c90b714756433b3333146a2893572b473dfd6.tar.gz android-node-v8-8c8c90b714756433b3333146a2893572b473dfd6.tar.bz2 android-node-v8-8c8c90b714756433b3333146a2893572b473dfd6.zip |
n-api: use AsyncResource for Work tracking
Enable combining N-API async work with async-hooks.
PR-URL: https://github.com/nodejs/node/pull/14697
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jason Ginchereau <jasongin@microsoft.com>
Reviewed-By: Michael Dawson <mhdawson@ibm.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/node_api.cc | 32 | ||||
-rw-r--r-- | src/node_api.h | 2 |
2 files changed, 29 insertions, 5 deletions
diff --git a/src/node_api.cc b/src/node_api.cc index 8c55e64b7c..f49dd6da68 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -3246,13 +3246,18 @@ static napi_status ConvertUVErrorCode(int code) { } // Wrapper around uv_work_t which calls user-provided callbacks. -class Work { +class Work : public node::AsyncResource { private: explicit Work(napi_env env, - napi_async_execute_callback execute = nullptr, + v8::Local<v8::Object> async_resource, + const char* async_resource_name, + napi_async_execute_callback execute, napi_async_complete_callback complete = nullptr, void* data = nullptr) - : _env(env), + : AsyncResource(env->isolate, + async_resource, + async_resource_name), + _env(env), _data(data), _execute(execute), _complete(complete) { @@ -3264,10 +3269,13 @@ class Work { public: static Work* New(napi_env env, + v8::Local<v8::Object> async_resource, + const char* async_resource_name, napi_async_execute_callback execute, napi_async_complete_callback complete, void* data) { - return new Work(env, execute, complete, data); + return new Work(env, async_resource, async_resource_name, + execute, complete, data); } static void Delete(Work* work) { @@ -3288,6 +3296,7 @@ class Work { // Establish a handle scope here so that every callback doesn't have to. // Also it is needed for the exception-handling below. v8::HandleScope scope(env->isolate); + CallbackScope callback_scope(work); work->_complete(env, ConvertUVErrorCode(status), work->_data); @@ -3330,6 +3339,8 @@ class Work { } while (0) napi_status napi_create_async_work(napi_env env, + napi_value async_resource, + const char* async_resource_name, napi_async_execute_callback execute, napi_async_complete_callback complete, void* data, @@ -3338,7 +3349,18 @@ napi_status napi_create_async_work(napi_env env, CHECK_ARG(env, execute); CHECK_ARG(env, result); - uvimpl::Work* work = uvimpl::Work::New(env, execute, complete, data); + v8::Local<v8::Object> resource; + if (async_resource != nullptr) { + auto value = v8impl::V8LocalValueFromJsValue(async_resource); + RETURN_STATUS_IF_FALSE(env, value->IsObject(), napi_invalid_arg); + resource = value.As<v8::Object>(); + } else { + resource = v8::Object::New(env->isolate); + } + + uvimpl::Work* work = + uvimpl::Work::New(env, resource, async_resource_name, + execute, complete, data); *result = reinterpret_cast<napi_async_work>(work); diff --git a/src/node_api.h b/src/node_api.h index 807595777d..227f26a2ff 100644 --- a/src/node_api.h +++ b/src/node_api.h @@ -524,6 +524,8 @@ NAPI_EXTERN napi_status napi_get_dataview_info(napi_env env, // Methods to manage simple async operations NAPI_EXTERN napi_status napi_create_async_work(napi_env env, + napi_value async_resource, + const char* async_resource_name, napi_async_execute_callback execute, napi_async_complete_callback complete, void* data, |