-
Notifications
You must be signed in to change notification settings - Fork 471
/
Copy pathaddon.cc
44 lines (34 loc) · 1.09 KB
/
addon.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
37
38
39
40
41
42
43
44
#if (NAPI_VERSION > 5)
#include <stdio.h>
#include "napi.h"
namespace {
class TestAddon : public Napi::Addon<TestAddon> {
public:
inline TestAddon(Napi::Env env, Napi::Object exports) {
DefineAddon(
exports,
{InstanceMethod("increment", &TestAddon::Increment),
InstanceValue(
"subObject",
DefineProperties(
Napi::Object::New(env),
{InstanceMethod("decrement", &TestAddon::Decrement)}))});
}
~TestAddon() { fprintf(stderr, "TestAddon::~TestAddon\n"); }
private:
Napi::Value Increment(const Napi::CallbackInfo& info) {
return Napi::Number::New(info.Env(), ++value);
}
Napi::Value Decrement(const Napi::CallbackInfo& info) {
return Napi::Number::New(info.Env(), --value);
}
uint32_t value = 42;
};
Napi::Value CreateAddon(const Napi::CallbackInfo& info) {
return TestAddon::Init(info.Env(), Napi::Object::New(info.Env()));
}
} // end of anonymous namespace
Napi::Object InitAddon(Napi::Env env) {
return Napi::Function::New<CreateAddon>(env, "CreateAddon");
}
#endif // (NAPI_VERSION > 5)