diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 0a74d89ea0..b827367bc4 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -3265,7 +3265,7 @@ changes: NAPI_EXTERN napi_status napi_create_async_work(napi_env env, napi_value async_resource, - const char* async_resource_name, + napi_value async_resource_name, napi_async_execute_callback execute, napi_async_complete_callback complete, void* data, diff --git a/src/async-wrap.cc b/src/async-wrap.cc index e08b2adc35..b678f1500b 100644 --- a/src/async-wrap.cc +++ b/src/async-wrap.cc @@ -643,6 +643,16 @@ async_context EmitAsyncInit(Isolate* isolate, Local resource, const char* name, async_id trigger_async_id) { + Local type = + String::NewFromUtf8(isolate, name, v8::NewStringType::kInternalized) + .ToLocalChecked(); + return EmitAsyncInit(isolate, resource, type, trigger_async_id); +} + +async_context EmitAsyncInit(Isolate* isolate, + Local resource, + v8::Local name, + async_id trigger_async_id) { Environment* env = Environment::GetCurrent(isolate); // Initialize async context struct @@ -655,10 +665,7 @@ async_context EmitAsyncInit(Isolate* isolate, }; // Run init hooks - Local type = - String::NewFromUtf8(isolate, name, v8::NewStringType::kInternalized) - .ToLocalChecked(); - AsyncWrap::EmitAsyncInit(env, resource, type, context.async_id, + AsyncWrap::EmitAsyncInit(env, resource, name, context.async_id, context.trigger_async_id); return context; diff --git a/src/node.h b/src/node.h index 6558a9ee6d..8d93fea7dc 100644 --- a/src/node.h +++ b/src/node.h @@ -566,6 +566,11 @@ NODE_EXTERN async_context EmitAsyncInit(v8::Isolate* isolate, const char* name, async_id trigger_async_id = -1); +NODE_EXTERN async_context EmitAsyncInit(v8::Isolate* isolate, + v8::Local resource, + v8::Local name, + async_id trigger_async_id = -1); + /* Emit the destroy() callback. */ NODE_EXTERN void EmitAsyncDestroy(v8::Isolate* isolate, async_context asyncContext); @@ -647,6 +652,16 @@ class AsyncResource { trigger_async_id); } + AsyncResource(v8::Isolate* isolate, + v8::Local resource, + v8::Local name, + async_id trigger_async_id = -1) + : isolate_(isolate), + resource_(isolate, resource) { + async_context_ = EmitAsyncInit(isolate, resource, name, + trigger_async_id); + } + ~AsyncResource() { EmitAsyncDestroy(isolate_, async_context_); } diff --git a/src/node_api.cc b/src/node_api.cc index f49dd6da68..8f84de7879 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -3250,7 +3250,7 @@ class Work : public node::AsyncResource { private: explicit Work(napi_env env, v8::Local async_resource, - const char* async_resource_name, + v8::Local async_resource_name, napi_async_execute_callback execute, napi_async_complete_callback complete = nullptr, void* data = nullptr) @@ -3270,7 +3270,7 @@ class Work : public node::AsyncResource { public: static Work* New(napi_env env, v8::Local async_resource, - const char* async_resource_name, + v8::Local async_resource_name, napi_async_execute_callback execute, napi_async_complete_callback complete, void* data) { @@ -3340,7 +3340,7 @@ class Work : public node::AsyncResource { napi_status napi_create_async_work(napi_env env, napi_value async_resource, - const char* async_resource_name, + napi_value async_resource_name, napi_async_execute_callback execute, napi_async_complete_callback complete, void* data, @@ -3349,17 +3349,20 @@ napi_status napi_create_async_work(napi_env env, CHECK_ARG(env, execute); CHECK_ARG(env, result); + v8::Local context = env->isolate->GetCurrentContext(); + v8::Local resource; if (async_resource != nullptr) { - auto value = v8impl::V8LocalValueFromJsValue(async_resource); - RETURN_STATUS_IF_FALSE(env, value->IsObject(), napi_invalid_arg); - resource = value.As(); + CHECK_TO_OBJECT(env, context, resource, async_resource); } else { resource = v8::Object::New(env->isolate); } + v8::Local resource_name; + CHECK_TO_STRING(env, context, resource_name, async_resource_name); + uvimpl::Work* work = - uvimpl::Work::New(env, resource, async_resource_name, + uvimpl::Work::New(env, resource, resource_name, execute, complete, data); *result = reinterpret_cast(work); diff --git a/src/node_api.h b/src/node_api.h index 227f26a2ff..b127fd7fe8 100644 --- a/src/node_api.h +++ b/src/node_api.h @@ -525,7 +525,7 @@ NAPI_EXTERN napi_status napi_get_dataview_info(napi_env env, NAPI_EXTERN napi_status napi_create_async_work(napi_env env, napi_value async_resource, - const char* async_resource_name, + napi_value async_resource_name, napi_async_execute_callback execute, napi_async_complete_callback complete, void* data, diff --git a/test/addons-napi/test_async/test_async.cc b/test/addons-napi/test_async/test_async.cc index 4ffc1d2fc9..574785c6d1 100644 --- a/test/addons-napi/test_async/test_async.cc +++ b/test/addons-napi/test_async/test_async.cc @@ -71,6 +71,7 @@ napi_value Test(napi_env env, napi_callback_info info) { size_t argc = 3; napi_value argv[3]; napi_value _this; + napi_value resource_name; void* data; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &_this, &data)); @@ -93,7 +94,10 @@ napi_value Test(napi_env env, napi_callback_info info) { napi_get_value_int32(env, argv[0], &the_carrier._input)); NAPI_CALL(env, napi_create_reference(env, argv[2], 1, &the_carrier._callback)); - NAPI_CALL(env, napi_create_async_work(env, argv[1], "TestResource", + + NAPI_CALL(env, + napi_create_string_utf8(env, "TestResource", -1, &resource_name)); + NAPI_CALL(env, napi_create_async_work(env, argv[1], resource_name, Execute, Complete, &the_carrier, &the_carrier._request)); NAPI_CALL(env, napi_queue_async_work(env, the_carrier._request)); @@ -138,12 +142,16 @@ napi_value TestCancel(napi_env env, napi_callback_info info) { size_t argc = 1; napi_value argv[1]; napi_value _this; + napi_value resource_name; void* data; + NAPI_CALL(env, + napi_create_string_utf8(env, "TestResource", -1, &resource_name)); + // make sure the work we are going to cancel will not be // able to start by using all the threads in the pool for (int i = 1; i < MAX_CANCEL_THREADS; i++) { - NAPI_CALL(env, napi_create_async_work(env, nullptr, "TestCancelBusy", + NAPI_CALL(env, napi_create_async_work(env, nullptr, resource_name, CancelExecute, BusyCancelComplete, &async_carrier[i], &async_carrier[i]._request)); NAPI_CALL(env, napi_queue_async_work(env, async_carrier[i]._request)); @@ -155,7 +163,7 @@ napi_value TestCancel(napi_env env, napi_callback_info info) { // workers above. NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &_this, &data)); - NAPI_CALL(env, napi_create_async_work(env, nullptr, "TestCancelled", + NAPI_CALL(env, napi_create_async_work(env, nullptr, resource_name, CancelExecute, CancelComplete, &async_carrier[0], &async_carrier[0]._request)); NAPI_CALL(env,