-
Notifications
You must be signed in to change notification settings - Fork 504
/
accessors.cpp
167 lines (146 loc) · 5.12 KB
/
accessors.cpp
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*********************************************************************
* NAN - Native Abstractions for Node.js
*
* Copyright (c) 2018 NAN contributors
*
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
********************************************************************/
#include <nan.h>
#include <cstring>
using namespace Nan; // NOLINT(build/namespaces)
class SetterGetter : public ObjectWrap {
public:
static NAN_MODULE_INIT(Init);
static v8::Local<v8::Value> NewInstance ();
static NAN_METHOD(New);
static NAN_METHOD(Log);
static NAN_GETTER(GetProp1);
static NAN_GETTER(GetProp2);
static NAN_SETTER(SetProp2);
SetterGetter();
char log[1024];
char prop1[256];
char prop2[256];
};
static Persistent<v8::FunctionTemplate> settergetter_constructor;
NAN_METHOD(CreateNew) {
info.GetReturnValue().Set(SetterGetter::NewInstance());
}
SetterGetter::SetterGetter() {
log[0] = '\0';
strncpy(prop1, "this is property 1", sizeof (prop1) - 1);
prop1[sizeof (prop1) - 1] = '\0';
prop2[0] = '\0';
}
NAN_MODULE_INIT(SetterGetter::Init) {
v8::Local<v8::FunctionTemplate> tpl =
Nan::New<v8::FunctionTemplate>(SetterGetter::New);
settergetter_constructor.Reset(tpl);
tpl->SetClassName(Nan::New<v8::String>("SetterGetter").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);
SetPrototypeMethod(tpl, "log", SetterGetter::Log);
v8::Local<v8::ObjectTemplate> itpl = tpl->InstanceTemplate();
SetAccessor(
itpl
, Nan::New("prop1").ToLocalChecked()
, SetterGetter::GetProp1);
SetAccessor(
itpl
, Nan::New<v8::String>("prop2").ToLocalChecked()
, SetterGetter::GetProp2
, SetterGetter::SetProp2
);
v8::Local<v8::Function> createnew =
Nan::GetFunction(
Nan::New<v8::FunctionTemplate>(CreateNew)).ToLocalChecked();
Set(target, Nan::New<v8::String>("create").ToLocalChecked(), createnew);
}
v8::Local<v8::Value> SetterGetter::NewInstance () {
EscapableHandleScope scope;
v8::Local<v8::FunctionTemplate> constructorHandle =
Nan::New(settergetter_constructor);
v8::Local<v8::Object> instance =
Nan::NewInstance(
Nan::GetFunction(constructorHandle).ToLocalChecked()).ToLocalChecked();
return scope.Escape(instance);
}
NAN_METHOD(SetterGetter::New) {
SetterGetter* settergetter = new SetterGetter();
assert(strlen(settergetter->log) < sizeof (settergetter->log));
strncat(
settergetter->log
, "New()\n"
, sizeof (settergetter->log) - 1 - strlen(settergetter->log));
settergetter->Wrap(info.This());
info.GetReturnValue().Set(info.This());
}
NAN_GETTER(SetterGetter::GetProp1) {
SetterGetter* settergetter =
ObjectWrap::Unwrap<SetterGetter>(info.Holder());
assert(strlen(settergetter->log) < sizeof (settergetter->log));
strncat(
settergetter->log
, "Prop1:GETTER("
, sizeof (settergetter->log) - 1 - strlen(settergetter->log));
assert(strlen(settergetter->log) < sizeof (settergetter->log));
strncat(
settergetter->log
, settergetter->prop1
, sizeof (settergetter->log) - 1 - strlen(settergetter->log));
assert(strlen(settergetter->log) < sizeof (settergetter->log));
strncat(
settergetter->log
, ")\n"
, sizeof (settergetter->log) - 1 - strlen(settergetter->log));
info.GetReturnValue().Set(Nan::New(settergetter->prop1).ToLocalChecked());
}
NAN_GETTER(SetterGetter::GetProp2) {
SetterGetter* settergetter =
ObjectWrap::Unwrap<SetterGetter>(info.Holder());
assert(strlen(settergetter->log) < sizeof (settergetter->log));
strncat(
settergetter->log
, "Prop2:GETTER("
, sizeof (settergetter->log) - 1 - strlen(settergetter->log));
assert(strlen(settergetter->log) < sizeof (settergetter->log));
strncat(
settergetter->log
, settergetter->prop2
, sizeof (settergetter->log) - 1 - strlen(settergetter->log));
assert(strlen(settergetter->log) < sizeof (settergetter->log));
strncat(
settergetter->log
, ")\n"
, sizeof (settergetter->log) - 1 - strlen(settergetter->log));
info.GetReturnValue().Set(Nan::New(settergetter->prop2).ToLocalChecked());
}
NAN_SETTER(SetterGetter::SetProp2) {
SetterGetter* settergetter =
ObjectWrap::Unwrap<SetterGetter>(info.Holder());
strncpy(
settergetter->prop2
, *Utf8String(value)
, sizeof (settergetter->prop2));
settergetter->prop2[sizeof (settergetter->prop2) - 1] = '\0';
assert(strlen(settergetter->log) < sizeof (settergetter->log));
strncat(
settergetter->log
, "Prop2:SETTER("
, sizeof (settergetter->log) - 1 - strlen(settergetter->log));
assert(strlen(settergetter->log) < sizeof (settergetter->log));
strncat(
settergetter->log
, settergetter->prop2
, sizeof (settergetter->log) - 1 - strlen(settergetter->log));
assert(strlen(settergetter->log) < sizeof (settergetter->log));
strncat(
settergetter->log
, ")\n"
, sizeof (settergetter->log) - 1 - strlen(settergetter->log));
}
NAN_METHOD(SetterGetter::Log) {
SetterGetter* settergetter =
ObjectWrap::Unwrap<SetterGetter>(info.Holder());
info.GetReturnValue().Set(Nan::New(settergetter->log).ToLocalChecked());
}
NODE_MODULE(accessors, SetterGetter::Init)