Skip to content

Commit 4b9d8bb

Browse files
author
Allen Luce
committed
Support Node 12, drop old nodes, refactor
Looks like several things I thought would be called aren't. So clean all that up. Also fix some security issues that Github was complaining about.
1 parent 3e099d1 commit 4b9d8bb

File tree

5 files changed

+3822
-74
lines changed

5 files changed

+3822
-74
lines changed

.travis.yml

+5-9
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,12 @@ cache:
55
directories:
66
- node_modules
77
node_js:
8+
- '12'
9+
- '11'
10+
- '10'
11+
- '9'
12+
- '8'
813
- '7'
9-
- '6'
10-
- '5'
11-
- '4'
12-
- iojs-v3
13-
- iojs-v2
14-
- iojs-v1
15-
- '0.12'
16-
- '0.11.15'
17-
- '0.10'
1814
before_install:
1915
- npm i -g npm@^2.0.0
2016
before_script:

autovivify.cc

+20-55
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
#include <nan.h>
22
using namespace std;
33

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-
104
class AutoVivify : public Nan::ObjectWrap {
115
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);
137

148
private:
159
Nan::Persistent<v8::Object> backing_obj;
@@ -26,9 +20,7 @@ class AutoVivify : public Nan::ObjectWrap {
2620
static NAN_INDEX_DELETER(IndexDeleter);
2721
static NAN_INDEX_ENUMERATOR(IndexEnumerator);
2822
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>*);
3224
static bool EnsureArray(v8::Local<v8::Object>&, AutoVivify *);
3325
static inline Nan::Persistent<v8::Function> & constructor() {
3426
static Nan::Persistent<v8::Function> my_constructor;
@@ -40,29 +32,14 @@ class AutoVivify : public Nan::ObjectWrap {
4032
AutoVivify *self = Nan::ObjectWrap::Unwrap<AutoVivify>(info.This()); \
4133
v8::Local<v8::Object> object = Nan::New(self->backing_obj)
4234

43-
#define LENGTH(object) \
44-
object->Get(Nan::New("length").ToLocalChecked())->ToObject()->Uint32Value()
45-
4635
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())) {
5837
Nan::ThrowError("Symbol properties are not supported.");
5938
return;
6039
}
61-
#endif
62-
63-
if (string(*prop) == "constructor") {
64-
return;
65-
}
40+
41+
Nan::Utf8String uv(property);
42+
string prop(*uv, uv.length());
6643

6744
UNWRAPOBJECT;
6845

@@ -89,38 +66,31 @@ void AutoVivify::Vivify(v8::Local<v8::Object> &object,
8966
}
9067

9168
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());
9473

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") {
10075
return;
101-
102-
if (string(*src) == "inspect") {
76+
}
77+
78+
if (src == "inspect") {
10379
v8::Local<v8::FunctionTemplate> tmpl = Nan::New<v8::FunctionTemplate>(inspect);
10480
v8::Local<v8::Function> fn = Nan::GetFunction(tmpl).ToLocalChecked();
10581
fn->SetName(Nan::New("inspect").ToLocalChecked());
10682
info.GetReturnValue().Set(fn);
10783
return;
10884
}
109-
85+
11086
UNWRAPOBJECT;
111-
if (string(*src) == "length")
112-
info.GetReturnValue().Set(LENGTH(object));
113-
else
114-
Vivify(object, property, &info);
87+
Vivify(object, property, &info);
11588
}
11689

11790
NAN_PROPERTY_QUERY(AutoVivify::PropQuery) {
118-
119-
#if (NODE_MODULE_VERSION > NODE_0_12_MODULE_VERSION)
12091
if (property->IsName() && !property->IsString())
12192
return;
122-
#endif
123-
v8::String::Utf8Value src(property);
93+
Nan::Utf8String src(property);
12494
if (string(*src) == "prototype") {
12595
info.GetReturnValue().Set(Nan::New<v8::Integer>(v8::None));
12696
}
@@ -138,16 +108,12 @@ NAN_PROPERTY_DELETER(AutoVivify::PropDeleter) {
138108
NAN_PROPERTY_ENUMERATOR(AutoVivify::PropEnumerator) {
139109
UNWRAPOBJECT;
140110
if (object->IsObject() && !object->IsArray()) {
141-
info.GetReturnValue().Set(object->GetPropertyNames());
111+
info.GetReturnValue().Set(Nan::GetPropertyNames(object).ToLocalChecked());
142112
}
143113
}
144114

145115
bool AutoVivify::EnsureArray(v8::Local<v8::Object> &object, AutoVivify *self) {
146116
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;
151117
// Replace with array
152118
v8::Local<v8::Array> new_array = Nan::New<v8::Array>();
153119
self->backing_obj.Reset(new_array);
@@ -187,8 +153,7 @@ NAN_INDEX_ENUMERATOR(AutoVivify::IndexEnumerator) {
187153
return;
188154

189155
v8::Local<v8::Array> indexes = Nan::New<v8::Array>();
190-
191-
int length = LENGTH(object);
156+
int length = object.As<v8::Array>()->Length();
192157
for (int i = 0; i < length; ++i) {
193158
v8::Local<v8::Value> val = Nan::Get(object, i).ToLocalChecked();
194159
if (!val->IsUndefined())
@@ -215,7 +180,7 @@ NAN_METHOD(AutoVivify::inspect) {
215180
info.GetReturnValue().Set(object);
216181
}
217182

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) {
219184
v8::Local<v8::FunctionTemplate> tmpl = Nan::New<v8::FunctionTemplate>(New);
220185
tmpl->SetClassName(Nan::New("AutoVivify").ToLocalChecked());
221186

0 commit comments

Comments
 (0)