Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Change name parameter to napi_value
  • Loading branch information
jasongin committed Sep 9, 2017
commit 8c09a5ec410a55c26ae1c90c939c335795e626d7
9 changes: 5 additions & 4 deletions doc/api/n-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -3315,15 +3315,16 @@ added: REPLACEME
```C
napi_status napi_async_init(napi_env env,
napi_value async_resource,
const char* async_resource_name,
napi_value async_resource_name,
napi_async_context* result)
```

- `[in] env`: The environment that the API is invoked under.
- `[in] async_resource`: An optional object associated with the async work
that will be passed to possible async_hooks [`init` hooks][].
- `[in] async_resource_name`: An identifier for the kind of resource that is
being provided for diagnostic information exposed by the `async_hooks` API.
that will be passed to possible `async_hooks` [`init` hooks][].
- `[in] async_resource_name`: Required identifier for the kind of resource
that is being provided for diagnostic information exposed by the
`async_hooks` API.
- `[out] result`: The initialized async context.

Returns `napi_ok` if the API succeeded.
Expand Down
15 changes: 11 additions & 4 deletions src/async-wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,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 @@ -760,10 +770,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
5 changes: 5 additions & 0 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,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
21 changes: 13 additions & 8 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2757,26 +2757,31 @@ napi_status napi_instanceof(napi_env env,

napi_status napi_async_init(napi_env env,
napi_value async_resource,
const char* async_resource_name,
napi_value async_resource_name,
napi_async_context* result) {
CHECK_ENV(env);
CHECK_ARG(env, async_resource);
CHECK_ARG(env, async_resource_name);
CHECK_ARG(env, result);

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

v8::Local<v8::Object> v8resource;
CHECK_TO_OBJECT(env, context, v8resource, async_resource);
v8::Local<v8::Object> v8_resource;
if (async_resource != nullptr) {
CHECK_TO_OBJECT(env, context, v8_resource, async_resource);
} else {
v8_resource = v8::Object::New(isolate);
}

v8::Local<v8::String> v8_resource_name;
CHECK_TO_STRING(env, context, v8_resource_name, async_resource_name);

// TODO(jasongin): Consider avoiding allocation here by using
// a tagged pointer with 2×31 bit fields instead.
node::async_context* node_async_context = new node::async_context();
node::async_context* async_context = new node::async_context();

*node_async_context =
node::EmitAsyncInit(isolate, v8resource, async_resource_name);
*result = reinterpret_cast<napi_async_context>(node_async_context);
*async_context = node::EmitAsyncInit(isolate, v8_resource, v8_resource_name);
*result = reinterpret_cast<napi_async_context>(async_context);

return napi_clear_last_error(env);
}
Expand Down
3 changes: 1 addition & 2 deletions src/node_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -527,11 +527,10 @@ NAPI_EXTERN napi_status napi_queue_async_work(napi_env env,
NAPI_EXTERN napi_status napi_cancel_async_work(napi_env env,
napi_async_work work);


// Methods for custom handling of async operations
NAPI_EXTERN napi_status napi_async_init(napi_env env,
napi_value async_resource,
const char* async_resource_name,
napi_value async_resource_name,
napi_async_context* result);

NAPI_EXTERN napi_status napi_async_destroy(napi_env env,
Expand Down
5 changes: 4 additions & 1 deletion test/addons-napi/test_make_callback/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ napi_value MakeCallback(napi_env env, napi_callback_info info) {

NAPI_CALL(env, napi_typeof(env, func, &func_type));

napi_value resource_name;
NAPI_CALL(env, napi_create_string_utf8(env, "test", -1, &resource_name));

napi_async_context context;
NAPI_CALL(env, napi_async_init(env, func, "test", &context));
NAPI_CALL(env, napi_async_init(env, func, resource_name, &context));

napi_value result;
if (func_type == napi_function) {
Expand Down