Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src,stream: use SetAccessorProperty instead of SetAccessor #17665

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 40 additions & 34 deletions src/stream_base-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace node {

using v8::AccessorSignature;
using v8::Signature;
using v8::External;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
Expand All @@ -34,31 +34,42 @@ void StreamBase::AddMethods(Environment* env,
enum PropertyAttribute attributes =
static_cast<PropertyAttribute>(
v8::ReadOnly | v8::DontDelete | v8::DontEnum);
Local<AccessorSignature> signature =
AccessorSignature::New(env->isolate(), t);
t->PrototypeTemplate()->SetAccessor(env->fd_string(),
GetFD<Base>,
nullptr,
env->as_external(),
v8::DEFAULT,
attributes,
signature);

t->PrototypeTemplate()->SetAccessor(env->external_stream_string(),
GetExternal<Base>,
nullptr,
env->as_external(),
v8::DEFAULT,
attributes,
signature);

t->PrototypeTemplate()->SetAccessor(env->bytes_read_string(),
GetBytesRead<Base>,
nullptr,
env->as_external(),
v8::DEFAULT,
attributes,
signature);

Local<Signature> signature =
Signature::New(env->isolate(), t);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the person landing this commit: these two lines could be merged into a single line, which might help with readability.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 This used to be AccessorSignature, and was over the limit. Fixed.


Local<FunctionTemplate> get_fd_templ = FunctionTemplate::New(
env->isolate(),
GetFD<Base>,
Local<Value>(),
signature);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the person landing this commit: for consistency the indentation should look something like:

  Local<FunctionTemplate> get_fd_templ =
      FunctionTemplate::New(env->isolate(),
                            GetFD<Base>,
                            Local<Value>(),
                            signature);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Fixed, was struggling a bit with how to indent it before.


Local<FunctionTemplate> get_external_templ = FunctionTemplate::New(
env->isolate(),
GetExternal<Base>,
Local<Value>(),
signature);

Local<FunctionTemplate> get_bytes_read = FunctionTemplate::New(
env->isolate(),
GetBytesRead<Base>,
Local<Value>(),
signature);

t->PrototypeTemplate()->SetAccessorProperty(env->fd_string(),
get_fd_templ,
Local<FunctionTemplate>(),
attributes);

t->PrototypeTemplate()->SetAccessorProperty(env->external_stream_string(),
get_external_templ,
Local<FunctionTemplate>(),
attributes);

t->PrototypeTemplate()->SetAccessorProperty(env->bytes_read_string(),
get_bytes_read,
Local<FunctionTemplate>(),
attributes);

env->SetProtoMethod(t, "readStart", JSMethod<Base, &StreamBase::ReadStart>);
env->SetProtoMethod(t, "readStop", JSMethod<Base, &StreamBase::ReadStop>);
Expand All @@ -85,8 +96,7 @@ void StreamBase::AddMethods(Environment* env,


template <class Base>
void StreamBase::GetFD(Local<String> key,
const PropertyCallbackInfo<Value>& args) {
void StreamBase::GetFD(const FunctionCallbackInfo<Value>& args) {
// Mimic implementation of StreamBase::GetFD() and UDPWrap::GetFD().
Base* handle;
ASSIGN_OR_RETURN_UNWRAP(&handle,
Expand All @@ -100,10 +110,8 @@ void StreamBase::GetFD(Local<String> key,
args.GetReturnValue().Set(wrap->GetFD());
}


template <class Base>
void StreamBase::GetBytesRead(Local<String> key,
const PropertyCallbackInfo<Value>& args) {
void StreamBase::GetBytesRead(const FunctionCallbackInfo<Value>& args) {
// The handle instance hasn't been set. So no bytes could have been read.
Base* handle;
ASSIGN_OR_RETURN_UNWRAP(&handle,
Expand All @@ -115,10 +123,8 @@ void StreamBase::GetBytesRead(Local<String> key,
args.GetReturnValue().Set(static_cast<double>(wrap->bytes_read_));
}


template <class Base>
void StreamBase::GetExternal(Local<String> key,
const PropertyCallbackInfo<Value>& args) {
void StreamBase::GetExternal(const FunctionCallbackInfo<Value>& args) {
Base* handle;
ASSIGN_OR_RETURN_UNWRAP(&handle, args.This());

Expand Down
9 changes: 3 additions & 6 deletions src/stream_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,16 +265,13 @@ class StreamBase : public StreamResource {
int WriteString(const v8::FunctionCallbackInfo<v8::Value>& args);

template <class Base>
static void GetFD(v8::Local<v8::String> key,
const v8::PropertyCallbackInfo<v8::Value>& args);
static void GetFD(const v8::FunctionCallbackInfo<v8::Value>& args);

template <class Base>
static void GetExternal(v8::Local<v8::String> key,
const v8::PropertyCallbackInfo<v8::Value>& args);
static void GetExternal(const v8::FunctionCallbackInfo<v8::Value>& args);

template <class Base>
static void GetBytesRead(v8::Local<v8::String> key,
const v8::PropertyCallbackInfo<v8::Value>& args);
static void GetBytesRead(const v8::FunctionCallbackInfo<v8::Value>& args);

template <class Base,
int (StreamBase::*Method)(
Expand Down
20 changes: 18 additions & 2 deletions test/parallel/test-stream-base-prototype-accessors.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ const assert = require('assert');
// Or anything that calls StreamBase::AddMethods when setting up its prototype
const TTY = process.binding('tty_wrap').TTY;

// Should throw instead of raise assertions
{
const msg = /TypeError: Method \w+ called on incompatible receiver/;
// Should throw instead of raise assertions
const msg = /TypeError: Illegal invocation/;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even better, change all uses of msg below to TypeError, because the exact error message may differ depending on the JavaScript engine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Fixed.

assert.throws(() => {
TTY.prototype.bytesRead;
}, msg);
Expand All @@ -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')),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don’t need the parentheses after typeof here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Removed.

'object'
);

assert.strictEqual(
typeof (Object.getOwnPropertyDescriptor(TTY.prototype, 'fd')),
'object'
);

assert.strictEqual(
typeof (Object.getOwnPropertyDescriptor(TTY.prototype, '_externalStream')),
'object'
);
Copy link
Member

@TimothyGu TimothyGu Dec 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar tests should exist for Object.getOwnPropertyDescriptor(crypto.SecureContext.prototype, '_external') and Object.getOwnPropertyDescriptor(crypto.Connection.prototype, '_external') where crypto = process.binding('crypto'), and Object.getOwnPropertyDescriptor(process.binding('udp_wrap').UDP.prototype, 'fd').

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Added.

}