1
1
#include < nan.h>
2
2
using namespace std ;
3
3
4
- #if NODE_MODULE_VERSION < IOJS_3_0_MODULE_VERSION
5
- typedef v8::Handle <v8::Object> ADDON_REGISTER_FUNCTION_ARGS2_TYPE;
6
- #else
7
- typedef v8::Local<v8::Value> ADDON_REGISTER_FUNCTION_ARGS2_TYPE;
8
- #endif
9
-
10
4
class AutoVivify : public Nan ::ObjectWrap {
11
5
public:
12
- static void Init (Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE exports, ADDON_REGISTER_FUNCTION_ARGS2_TYPE module);
6
+ static void Init (Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE exports, v8::Local<v8::Value> module);
13
7
14
8
private:
15
9
Nan::Persistent<v8::Object> backing_obj;
@@ -26,9 +20,7 @@ class AutoVivify : public Nan::ObjectWrap {
26
20
static NAN_INDEX_DELETER (IndexDeleter);
27
21
static NAN_INDEX_ENUMERATOR (IndexEnumerator);
28
22
template <typename T>
29
- static void Vivify (v8::Local<v8::Object> &,
30
- T,
31
- const Nan::PropertyCallbackInfo<v8::Value>*);
23
+ static void Vivify (v8::Local<v8::Object> &, T, const Nan::PropertyCallbackInfo<v8::Value>*);
32
24
static bool EnsureArray (v8::Local<v8::Object>&, AutoVivify *);
33
25
static inline Nan::Persistent<v8::Function> & constructor () {
34
26
static Nan::Persistent<v8::Function> my_constructor;
@@ -40,29 +32,14 @@ class AutoVivify : public Nan::ObjectWrap {
40
32
AutoVivify *self = Nan::ObjectWrap::Unwrap<AutoVivify>(info.This()); \
41
33
v8::Local<v8::Object> object = Nan::New(self->backing_obj)
42
34
43
- #define LENGTH (object ) \
44
- object->Get (Nan::New(" length" ).ToLocalChecked())->ToObject()->Uint32Value()
45
-
46
35
NAN_PROPERTY_SETTER (AutoVivify::PropSetter) {
47
- v8::String::Utf8Value prop (property);
48
-
49
- #if (NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION)
50
- if (property->IsSymbol ()) {
51
- Nan::ThrowError (" Symbol properties are not supported." );
52
- return ;
53
- }
54
- #endif
55
-
56
- #if (NODE_MODULE_VERSION > NODE_0_12_MODULE_VERSION)
57
- if (property->IsName () && !property->IsString ()) {
36
+ if (property->IsSymbol () || (property->IsName () && !property->IsString ())) {
58
37
Nan::ThrowError (" Symbol properties are not supported." );
59
38
return ;
60
39
}
61
- #endif
62
-
63
- if (string (*prop) == " constructor" ) {
64
- return ;
65
- }
40
+
41
+ Nan::Utf8String uv (property);
42
+ string prop (*uv, uv.length ());
66
43
67
44
UNWRAPOBJECT;
68
45
@@ -89,38 +66,31 @@ void AutoVivify::Vivify(v8::Local<v8::Object> &object,
89
66
}
90
67
91
68
NAN_PROPERTY_GETTER (AutoVivify::PropGetter) {
92
- v8::String::Utf8Value data (info.Data ());
93
- v8::String::Utf8Value src (property);
69
+ if (property->IsSymbol ()) return ;
70
+
71
+ Nan::Utf8String uv (property);
72
+ string src (*uv, uv.length ());
94
73
95
- if (
96
- #if (NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION)
97
- property->IsSymbol () ||
98
- #endif
99
- string (*src) == " prototype" )
74
+ if (src == " prototype" ) {
100
75
return ;
101
-
102
- if (string (*src) == " inspect" ) {
76
+ }
77
+
78
+ if (src == " inspect" ) {
103
79
v8::Local<v8::FunctionTemplate> tmpl = Nan::New<v8::FunctionTemplate>(inspect);
104
80
v8::Local<v8::Function> fn = Nan::GetFunction (tmpl).ToLocalChecked ();
105
81
fn->SetName (Nan::New (" inspect" ).ToLocalChecked ());
106
82
info.GetReturnValue ().Set (fn);
107
83
return ;
108
84
}
109
-
85
+
110
86
UNWRAPOBJECT;
111
- if (string (*src) == " length" )
112
- info.GetReturnValue ().Set (LENGTH (object));
113
- else
114
- Vivify (object, property, &info);
87
+ Vivify (object, property, &info);
115
88
}
116
89
117
90
NAN_PROPERTY_QUERY (AutoVivify::PropQuery) {
118
-
119
- #if (NODE_MODULE_VERSION > NODE_0_12_MODULE_VERSION)
120
91
if (property->IsName () && !property->IsString ())
121
92
return ;
122
- #endif
123
- v8::String::Utf8Value src (property);
93
+ Nan::Utf8String src (property);
124
94
if (string (*src) == " prototype" ) {
125
95
info.GetReturnValue ().Set (Nan::New<v8::Integer>(v8::None));
126
96
}
@@ -138,16 +108,12 @@ NAN_PROPERTY_DELETER(AutoVivify::PropDeleter) {
138
108
NAN_PROPERTY_ENUMERATOR (AutoVivify::PropEnumerator) {
139
109
UNWRAPOBJECT;
140
110
if (object->IsObject () && !object->IsArray ()) {
141
- info.GetReturnValue ().Set (object-> GetPropertyNames ());
111
+ info.GetReturnValue ().Set (Nan:: GetPropertyNames(object). ToLocalChecked ());
142
112
}
143
113
}
144
114
145
115
bool AutoVivify::EnsureArray (v8::Local<v8::Object> &object, AutoVivify *self) {
146
116
if (object->IsObject () && !object->IsArray ()) {
147
- // Is it empty?
148
- v8::Local<v8::Array> props = object->GetPropertyNames ();
149
- if (LENGTH (props) != 0 ) // No, bail.
150
- return false ;
151
117
// Replace with array
152
118
v8::Local<v8::Array> new_array = Nan::New<v8::Array>();
153
119
self->backing_obj .Reset (new_array);
@@ -187,8 +153,7 @@ NAN_INDEX_ENUMERATOR(AutoVivify::IndexEnumerator) {
187
153
return ;
188
154
189
155
v8::Local<v8::Array> indexes = Nan::New<v8::Array>();
190
-
191
- int length = LENGTH (object);
156
+ int length = object.As <v8::Array>()->Length ();
192
157
for (int i = 0 ; i < length; ++i) {
193
158
v8::Local<v8::Value> val = Nan::Get (object, i).ToLocalChecked ();
194
159
if (!val->IsUndefined ())
@@ -215,7 +180,7 @@ NAN_METHOD(AutoVivify::inspect) {
215
180
info.GetReturnValue ().Set (object);
216
181
}
217
182
218
- void AutoVivify::Init (Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE exports, ADDON_REGISTER_FUNCTION_ARGS2_TYPE module) {
183
+ void AutoVivify::Init (Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE exports, v8::Local<v8::Value> module) {
219
184
v8::Local<v8::FunctionTemplate> tmpl = Nan::New<v8::FunctionTemplate>(New);
220
185
tmpl->SetClassName (Nan::New (" AutoVivify" ).ToLocalChecked ());
221
186
0 commit comments