forked from nodejs/node-addon-examples
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmyobject.cc
More file actions
40 lines (30 loc) · 1.12 KB
/
Copy pathmyobject.cc
File metadata and controls
40 lines (30 loc) · 1.12 KB
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
#include "myobject.h"
#include <napi.h>
#include <uv.h>
using namespace Napi;
Napi::Object MyObject::Init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
Napi::Function func = DefineClass(
env, "MyObject", {InstanceMethod("plusOne", &MyObject::PlusOne)});
Napi::FunctionReference* constructor = new Napi::FunctionReference();
*constructor = Napi::Persistent(func);
env.SetInstanceData(constructor);
exports.Set("MyObject", func);
return exports;
}
MyObject::MyObject(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<MyObject>(info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
this->counter_ = info[0].As<Napi::Number>().DoubleValue();
};
Napi::Object MyObject::NewInstance(Napi::Env env, Napi::Value arg) {
Napi::EscapableHandleScope scope(env);
Napi::Object obj = env.GetInstanceData<Napi::FunctionReference>()->New({arg});
return scope.Escape(napi_value(obj)).ToObject();
}
Napi::Value MyObject::PlusOne(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
this->counter_ = this->counter_ + 1;
return Napi::Number::New(env, this->counter_);
}