Skip to content

Commit

Permalink
Merge pull request #4 from allenluce/support-ten
Browse files Browse the repository at this point in the history
fix: Make work on Node 0.10
  • Loading branch information
allenluce authored Oct 11, 2016
2 parents dbef875 + ec1b4b9 commit 885b1ad
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ node_js:
- iojs-v1
- '0.12'
- '0.11.15'
- '0.10'
before_install:
- npm i -g npm@^2.0.0
before_script:
Expand Down
42 changes: 26 additions & 16 deletions autovivify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,32 @@ class AutoVivify : public Nan::ObjectWrap {
AutoVivify *self = Nan::ObjectWrap::Unwrap<AutoVivify>(info.This()); \
v8::Local<v8::Object> object = Nan::New(self->backing_obj)

#define LENGTH(object) \
#define LENGTH(object) \
object->Get(Nan::New("length").ToLocalChecked())->ToObject()->Uint32Value()

NAN_PROPERTY_SETTER(AutoVivify::PropSetter) {
v8::String::Utf8Value prop(property);

UNWRAPOBJECT;


#if (NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION)
if (property->IsSymbol()) {
Nan::ThrowError("Symbol properties are not supported.");
return;
}
#endif

#if (NODE_MODULE_VERSION > NODE_0_12_MODULE_VERSION)
if (property->IsName() && !property->IsString()) {
Nan::ThrowError("Symbol properties are not supported.");
return;
}
#endif

if (string(*prop) == "constructor") {
return;
}

UNWRAPOBJECT;

if (!Nan::Set(object, property, value).FromMaybe(false))
Nan::ThrowError("Error setting property.");
}
Expand All @@ -70,7 +81,7 @@ void AutoVivify::Vivify(v8::Local<v8::Object> &object,
}

v8::Local<v8::Function> cons = Nan::New(constructor());
v8::Local<v8::Value> newobj = cons->NewInstance();
v8::Local<v8::Value> newobj = Nan::NewInstance(cons).ToLocalChecked();
if (!Nan::Set(object, property_or_index, newobj).FromMaybe(false))
Nan::ThrowError("Error autovivifying property.");
else
Expand All @@ -81,11 +92,11 @@ NAN_PROPERTY_GETTER(AutoVivify::PropGetter) {
v8::String::Utf8Value data(info.Data());
v8::String::Utf8Value src(property);

if (property->IsSymbol() ||
string(*src) == "prototype" ||
string(*src) == "valueOf" ||
string(*src) == "toString" ||
string(*src) == "nodeType")
if (
#if (NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION)
property->IsSymbol() ||
#endif
string(*src) == "prototype")
return;

if (string(*src) == "inspect") {
Expand All @@ -97,21 +108,23 @@ NAN_PROPERTY_GETTER(AutoVivify::PropGetter) {
}

UNWRAPOBJECT;

if (string(*src) == "length")
info.GetReturnValue().Set(LENGTH(object));
else
Vivify(object, property, &info);
}

NAN_PROPERTY_QUERY(AutoVivify::PropQuery) {

#if (NODE_MODULE_VERSION > NODE_0_12_MODULE_VERSION)
if (property->IsName() && !property->IsString())
return;
#endif
v8::String::Utf8Value src(property);
if (string(*src) == "prototype") {
info.GetReturnValue().Set(Nan::New<v8::Integer>(v8::None));
}

UNWRAPOBJECT;

v8::Local<v8::Value> val = Nan::Get(object, property).ToLocalChecked();
if (!val->IsUndefined())
info.GetReturnValue().Set(Nan::New<v8::Integer>(v8::None));
Expand Down Expand Up @@ -145,22 +158,19 @@ bool AutoVivify::EnsureArray(v8::Local<v8::Object> &object, AutoVivify *self) {

NAN_INDEX_GETTER(AutoVivify::IndexGetter) {
UNWRAPOBJECT;

if (EnsureArray(object, self))
Vivify(object, index, &info);
}

NAN_INDEX_SETTER(AutoVivify::IndexSetter) {
UNWRAPOBJECT;

if (EnsureArray(object, self) &&
!Nan::Set(object, index, value).FromMaybe(false))
Nan::ThrowError("Error setting array value");
}

NAN_INDEX_QUERY(AutoVivify::IndexQuery) {
UNWRAPOBJECT;

if(object->IsArray() &&
!Nan::Get(object, index).ToLocalChecked()->IsUndefined())
info.GetReturnValue().Set(Nan::New<v8::Integer>(v8::None));
Expand Down

0 comments on commit 885b1ad

Please sign in to comment.