-
-
Notifications
You must be signed in to change notification settings - Fork 528
Expand file tree
/
Copy pathindexedinterceptors.cpp
More file actions
116 lines (97 loc) · 3.67 KB
/
indexedinterceptors.cpp
File metadata and controls
116 lines (97 loc) · 3.67 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
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
/*********************************************************************
* NAN - Native Abstractions for Node.js
*
* Copyright (c) 2015 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 IndexedInterceptor : public ObjectWrap {
char buf[256];
public:
IndexedInterceptor() { std::strncpy(this->buf, "foo", sizeof (this->buf)); }
static void Init(v8::Local<v8::Object> target);
static v8::Local<v8::Value> NewInstance ();
static NAN_METHOD(New);
static NAN_INDEX_GETTER(PropertyGetter);
static NAN_INDEX_SETTER(PropertySetter);
static NAN_INDEX_ENUMERATOR(PropertyEnumerator);
static NAN_INDEX_DELETER(PropertyDeleter);
static NAN_INDEX_QUERY(PropertyQuery);
};
static Persistent<v8::FunctionTemplate> indexedinterceptors_constructor;
NAN_METHOD(CreateNew) {
info.GetReturnValue().Set(IndexedInterceptor::NewInstance());
}
void IndexedInterceptor::Init(v8::Local<v8::Object> target) {
v8::Local<v8::FunctionTemplate> tpl =
Nan::New<v8::FunctionTemplate>(IndexedInterceptor::New);
indexedinterceptors_constructor.Reset(tpl);
tpl->SetClassName(Nan::New("IndexedInterceptor").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);
v8::Local<v8::ObjectTemplate> inst = tpl->InstanceTemplate();
SetIndexedPropertyHandler(
inst
, IndexedInterceptor::PropertyGetter
, IndexedInterceptor::PropertySetter
, IndexedInterceptor::PropertyQuery
, IndexedInterceptor::PropertyDeleter
, IndexedInterceptor::PropertyEnumerator);
v8::Local<v8::Function> createnew =
Nan::New<v8::FunctionTemplate>(CreateNew)->GetFunction();
Set(target, Nan::New("create").ToLocalChecked(), createnew);
}
v8::Local<v8::Value> IndexedInterceptor::NewInstance () {
EscapableHandleScope scope;
v8::Local<v8::FunctionTemplate> constructorHandle =
Nan::New(indexedinterceptors_constructor);
v8::Local<v8::Object> instance =
constructorHandle->GetFunction()->NewInstance(0, NULL);
return scope.Escape(instance);
}
NAN_METHOD(IndexedInterceptor::New) {
IndexedInterceptor* interceptor = new IndexedInterceptor();
interceptor->Wrap(info.This());
info.GetReturnValue().Set(info.This());
}
NAN_INDEX_GETTER(IndexedInterceptor::PropertyGetter) {
IndexedInterceptor* interceptor =
ObjectWrap::Unwrap<IndexedInterceptor>(info.This());
if (index == 0) {
info.GetReturnValue().Set(Nan::New(interceptor->buf).ToLocalChecked());
} else {
info.GetReturnValue().Set(Nan::New("bar").ToLocalChecked());
}
}
NAN_INDEX_SETTER(IndexedInterceptor::PropertySetter) {
IndexedInterceptor* interceptor =
ObjectWrap::Unwrap<IndexedInterceptor>(info.This());
if (index == 0) {
std::strncpy(
interceptor->buf
, *v8::String::Utf8Value(value)
, sizeof (interceptor->buf));
info.GetReturnValue().Set(info.This());
} else {
info.GetReturnValue().Set(info.This());
}
}
NAN_INDEX_ENUMERATOR(IndexedInterceptor::PropertyEnumerator) {
v8::Local<v8::Array> arr = Nan::New<v8::Array>();
Set(arr, 0, Nan::New("whee").ToLocalChecked());
info.GetReturnValue().Set(arr);
}
NAN_INDEX_DELETER(IndexedInterceptor::PropertyDeleter) {
IndexedInterceptor* interceptor =
ObjectWrap::Unwrap<IndexedInterceptor>(info.This());
std::strncpy(interceptor->buf, "goober", sizeof (interceptor->buf));
info.GetReturnValue().Set(True());
}
NAN_INDEX_QUERY(IndexedInterceptor::PropertyQuery) {
if (index == 1) {
info.GetReturnValue().Set(Nan::New<v8::Integer>(v8::DontEnum));
}
}
NAN_MODULE(indexedinterceptors, IndexedInterceptor::Init)