-
Notifications
You must be signed in to change notification settings - Fork 471
/
Copy pathasync_context.cc
36 lines (30 loc) · 1.2 KB
/
async_context.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "napi.h"
using namespace Napi;
namespace {
static void MakeCallback(const CallbackInfo& info) {
Function callback = info[0].As<Function>();
Object resource = info[1].As<Object>();
AsyncContext context(info.Env(), "async_context_test", resource);
callback.MakeCallback(
Object::New(info.Env()), std::initializer_list<napi_value>{}, context);
}
static void MakeCallbackNoResource(const CallbackInfo& info) {
Function callback = info[0].As<Function>();
AsyncContext context(info.Env(), "async_context_no_res_test");
callback.MakeCallback(
Object::New(info.Env()), std::initializer_list<napi_value>{}, context);
}
static Boolean AssertAsyncContextReturnCorrectEnv(const CallbackInfo& info) {
AsyncContext context(info.Env(), "empty_context_test");
return Boolean::New(info.Env(), context.Env() == info.Env());
}
} // end anonymous namespace
Object InitAsyncContext(Env env) {
Object exports = Object::New(env);
exports["makeCallback"] = Function::New(env, MakeCallback);
exports["makeCallbackNoResource"] =
Function::New(env, MakeCallbackNoResource);
exports["asyncCxtReturnCorrectEnv"] =
Function::New(env, AssertAsyncContextReturnCorrectEnv);
return exports;
}