From 329e162162366b4309e467619cff3f843fe79a79 Mon Sep 17 00:00:00 2001 From: Jure Triglav Date: Wed, 13 Dec 2017 22:35:32 +0000 Subject: [PATCH] Change to using SetAccessorProperty instead of SetAccessor in StreamBase Fixes: https://github.com/nodejs/node/issues/17636 Refs: https://github.com/nodejs/node/pull/16482 Refs: https://github.com/nodejs/node/pull/16860 --- src/stream_base-inl.h | 74 ++++++++++--------- src/stream_base.h | 9 +-- .../test-stream-base-prototype-accessors.js | 20 ++++- 3 files changed, 61 insertions(+), 42 deletions(-) diff --git a/src/stream_base-inl.h b/src/stream_base-inl.h index 29739011c6a439..f53079541f3708 100644 --- a/src/stream_base-inl.h +++ b/src/stream_base-inl.h @@ -11,7 +11,7 @@ namespace node { -using v8::AccessorSignature; +using v8::Signature; using v8::External; using v8::FunctionCallbackInfo; using v8::FunctionTemplate; @@ -34,31 +34,42 @@ void StreamBase::AddMethods(Environment* env, enum PropertyAttribute attributes = static_cast( v8::ReadOnly | v8::DontDelete | v8::DontEnum); - Local signature = - AccessorSignature::New(env->isolate(), t); - t->PrototypeTemplate()->SetAccessor(env->fd_string(), - GetFD, - nullptr, - env->as_external(), - v8::DEFAULT, - attributes, - signature); - - t->PrototypeTemplate()->SetAccessor(env->external_stream_string(), - GetExternal, - nullptr, - env->as_external(), - v8::DEFAULT, - attributes, - signature); - - t->PrototypeTemplate()->SetAccessor(env->bytes_read_string(), - GetBytesRead, - nullptr, - env->as_external(), - v8::DEFAULT, - attributes, - signature); + + Local signature = + Signature::New(env->isolate(), t); + + Local get_fd_templ = FunctionTemplate::New( + env->isolate(), + GetFD, + Local(), + signature); + + Local get_external_templ = FunctionTemplate::New( + env->isolate(), + GetExternal, + Local(), + signature); + + Local get_bytes_read = FunctionTemplate::New( + env->isolate(), + GetBytesRead, + Local(), + signature); + + t->PrototypeTemplate()->SetAccessorProperty(env->fd_string(), + get_fd_templ, + Local(), + attributes); + + t->PrototypeTemplate()->SetAccessorProperty(env->external_stream_string(), + get_external_templ, + Local(), + attributes); + + t->PrototypeTemplate()->SetAccessorProperty(env->bytes_read_string(), + get_bytes_read, + Local(), + attributes); env->SetProtoMethod(t, "readStart", JSMethod); env->SetProtoMethod(t, "readStop", JSMethod); @@ -85,8 +96,7 @@ void StreamBase::AddMethods(Environment* env, template -void StreamBase::GetFD(Local key, - const PropertyCallbackInfo& args) { +void StreamBase::GetFD(const FunctionCallbackInfo& args) { // Mimic implementation of StreamBase::GetFD() and UDPWrap::GetFD(). Base* handle; ASSIGN_OR_RETURN_UNWRAP(&handle, @@ -100,10 +110,8 @@ void StreamBase::GetFD(Local key, args.GetReturnValue().Set(wrap->GetFD()); } - template -void StreamBase::GetBytesRead(Local key, - const PropertyCallbackInfo& args) { +void StreamBase::GetBytesRead(const FunctionCallbackInfo& args) { // The handle instance hasn't been set. So no bytes could have been read. Base* handle; ASSIGN_OR_RETURN_UNWRAP(&handle, @@ -115,10 +123,8 @@ void StreamBase::GetBytesRead(Local key, args.GetReturnValue().Set(static_cast(wrap->bytes_read_)); } - template -void StreamBase::GetExternal(Local key, - const PropertyCallbackInfo& args) { +void StreamBase::GetExternal(const FunctionCallbackInfo& args) { Base* handle; ASSIGN_OR_RETURN_UNWRAP(&handle, args.This()); diff --git a/src/stream_base.h b/src/stream_base.h index 5bf5f013947347..2269aa199b9dbc 100644 --- a/src/stream_base.h +++ b/src/stream_base.h @@ -265,16 +265,13 @@ class StreamBase : public StreamResource { int WriteString(const v8::FunctionCallbackInfo& args); template - static void GetFD(v8::Local key, - const v8::PropertyCallbackInfo& args); + static void GetFD(const v8::FunctionCallbackInfo& args); template - static void GetExternal(v8::Local key, - const v8::PropertyCallbackInfo& args); + static void GetExternal(const v8::FunctionCallbackInfo& args); template - static void GetBytesRead(v8::Local key, - const v8::PropertyCallbackInfo& args); + static void GetBytesRead(const v8::FunctionCallbackInfo& args); template { TTY.prototype.bytesRead; }, msg); @@ -24,4 +24,20 @@ const TTY = process.binding('tty_wrap').TTY; assert.throws(() => { TTY.prototype._externalStream; }, msg); + + // Should not throw for Object.getOwnPropertyDescriptor + assert.strictEqual( + typeof (Object.getOwnPropertyDescriptor(TTY.prototype, 'bytesRead')), + 'object' + ); + + assert.strictEqual( + typeof (Object.getOwnPropertyDescriptor(TTY.prototype, 'fd')), + 'object' + ); + + assert.strictEqual( + typeof (Object.getOwnPropertyDescriptor(TTY.prototype, '_externalStream')), + 'object' + ); }