Skip to content

Commit

Permalink
fix: make Object.prototype.toString.call() work
Browse files Browse the repository at this point in the history
So when Node 6+ wants to do an Object.prototype.toString call, it first
sends a query for a property (the GetToStringTag property, in
particular). Autovivify didn't know how to deal with this query, so
would bomb.

This has the property getter just return if it's asked for a
symbol. This lets v8 fall through to its own stringify routines, so an
appropriate value is emitted ("[object AutoVivify]").

This also removes the explicit toString method as the existing fall-back
one seems to work just fine.

This fixes #1
  • Loading branch information
Allen Luce committed Jun 2, 2016
1 parent 3b0e35d commit 4400a99
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
12 changes: 3 additions & 9 deletions autovivify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class AutoVivify : public Nan::ObjectWrap {
Nan::Persistent<v8::Object> backing_obj;
static NAN_METHOD(New);
static NAN_METHOD(inspect);
static NAN_METHOD(ToString);
static NAN_PROPERTY_SETTER(PropSetter);
static NAN_PROPERTY_GETTER(PropGetter);
static NAN_PROPERTY_QUERY(PropQuery);
Expand Down Expand Up @@ -78,15 +77,12 @@ void AutoVivify::Vivify(v8::Local<v8::Object> &object,
info->GetReturnValue().Set(newobj);
}

NAN_METHOD(AutoVivify::ToString) {
UNWRAPOBJECT;
info.GetReturnValue().Set(object->ToString());
}

NAN_PROPERTY_GETTER(AutoVivify::PropGetter) {
v8::String::Utf8Value data(info.Data());
v8::String::Utf8Value src(property);
if (string(*src) == "prototype" ||

if (property->IsSymbol() ||
string(*src) == "prototype" ||
string(*src) == "valueOf" ||
string(*src) == "toString" ||
string(*src) == "nodeType")
Expand Down Expand Up @@ -215,8 +211,6 @@ void AutoVivify::Init(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE exports, ADDON_REGI

tmpl->InstanceTemplate()->SetInternalFieldCount(1);

Nan::SetPrototypeMethod(tmpl, "toString", ToString);

v8::Local<v8::ObjectTemplate> proto = tmpl->PrototypeTemplate();
Nan::SetNamedPropertyHandler(proto, PropGetter, PropSetter, PropQuery, PropDeleter, PropEnumerator,
Nan::New<v8::String>("prototype").ToLocalChecked());
Expand Down
9 changes: 6 additions & 3 deletions test/test-autovivify.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,11 @@ describe('Autovivify', function () {
expect(proto1).to.equal(proto2)
})

it("has a working toString() method", function () {
expect(this.obj.toString()).to.equal("[object Object]")
it('has a working toString() method', function () {
expect(this.obj.toString()).to.equal('[object AutoVivify]')
})

it('Object.prototype.toString.call() returns an appropriate representation', function () {
expect(Object.prototype.toString.call(this.obj)).to.equal('[object AutoVivify]')
})

})

0 comments on commit 4400a99

Please sign in to comment.