Skip to content

Commit

Permalink
n-api: change async resource name to napi_value
Browse files Browse the repository at this point in the history
PR-URL: nodejs/node#14697
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
jasongin authored and addaleax committed Sep 17, 2017
1 parent 74679dd commit c613f23
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 16 deletions.
2 changes: 1 addition & 1 deletion doc/api/n-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 11 additions & 4 deletions src/async-wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,16 @@ async_context EmitAsyncInit(Isolate* isolate,
Local<Object> resource,
const char* name,
async_id trigger_async_id) {
Local<String> type =
String::NewFromUtf8(isolate, name, v8::NewStringType::kInternalized)
.ToLocalChecked();
return EmitAsyncInit(isolate, resource, type, trigger_async_id);
}

async_context EmitAsyncInit(Isolate* isolate,
Local<Object> resource,
v8::Local<v8::String> name,
async_id trigger_async_id) {
Environment* env = Environment::GetCurrent(isolate);

// Initialize async context struct
Expand All @@ -655,10 +665,7 @@ async_context EmitAsyncInit(Isolate* isolate,
};

// Run init hooks
Local<String> 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;
Expand Down
15 changes: 15 additions & 0 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<v8::Object> resource,
v8::Local<v8::String> name,
async_id trigger_async_id = -1);

/* Emit the destroy() callback. */
NODE_EXTERN void EmitAsyncDestroy(v8::Isolate* isolate,
async_context asyncContext);
Expand Down Expand Up @@ -647,6 +652,16 @@ class AsyncResource {
trigger_async_id);
}

AsyncResource(v8::Isolate* isolate,
v8::Local<v8::Object> resource,
v8::Local<v8::String> 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_);
}
Expand Down
17 changes: 10 additions & 7 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3250,7 +3250,7 @@ class Work : public node::AsyncResource {
private:
explicit Work(napi_env env,
v8::Local<v8::Object> async_resource,
const char* async_resource_name,
v8::Local<v8::String> async_resource_name,
napi_async_execute_callback execute,
napi_async_complete_callback complete = nullptr,
void* data = nullptr)
Expand All @@ -3270,7 +3270,7 @@ class Work : public node::AsyncResource {
public:
static Work* New(napi_env env,
v8::Local<v8::Object> async_resource,
const char* async_resource_name,
v8::Local<v8::String> async_resource_name,
napi_async_execute_callback execute,
napi_async_complete_callback complete,
void* data) {
Expand Down Expand Up @@ -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,
Expand All @@ -3349,17 +3349,20 @@ napi_status napi_create_async_work(napi_env env,
CHECK_ARG(env, execute);
CHECK_ARG(env, result);

v8::Local<v8::Context> context = env->isolate->GetCurrentContext();

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>();
CHECK_TO_OBJECT(env, context, resource, async_resource);
} else {
resource = v8::Object::New(env->isolate);
}

v8::Local<v8::String> 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<napi_async_work>(work);
Expand Down
2 changes: 1 addition & 1 deletion src/node_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 11 additions & 3 deletions test/addons-napi/test_async/test_async.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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));
Expand Down Expand Up @@ -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));
Expand All @@ -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,
Expand Down

0 comments on commit c613f23

Please sign in to comment.