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 7 commits
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
79 changes: 45 additions & 34 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ using v8::Persistent;
using v8::PropertyAttribute;
using v8::PropertyCallbackInfo;
using v8::ReadOnly;
using v8::Signature;
using v8::String;
using v8::Value;

Expand Down Expand Up @@ -544,14 +545,18 @@ void SecureContext::Initialize(Environment* env, Local<Object> target) {
t->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kTicketKeyIVIndex"),
Integer::NewFromUnsigned(env->isolate(), kTicketKeyIVIndex));

t->PrototypeTemplate()->SetAccessor(
Local<FunctionTemplate> ctx_getter_templ =
FunctionTemplate::New(env->isolate(),
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.

We indent by four spaces in line continuations :)

Here and also in other C++ files.

CtxGetter,
Local<Value>(),
Copy link
Member

Choose a reason for hiding this comment

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

We usually use env->as_external() for the parameter right here, so that we can get access to the Environment object in the callback itself. Again, here and below.

Copy link
Contributor Author

@jure jure Dec 14, 2017

Choose a reason for hiding this comment

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

You mean instead of Local<Value>()?

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.

Yes.The same also needs to be done for all other such instances, otherwise I'm fairly sure some tests will fail because of this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tests completed fine with just Local<Value>(), so that's a bit worrying, but it's perhaps out of the scope of this PR to add tests for that. I've now switched to env->as_external wherever it was previously used.

Signature::New(env->isolate(), t));


t->PrototypeTemplate()->SetAccessorProperty(
FIXED_ONE_BYTE_STRING(env->isolate(), "_external"),
CtxGetter,
nullptr,
env->as_external(),
DEFAULT,
static_cast<PropertyAttribute>(ReadOnly | DontDelete),
AccessorSignature::New(env->isolate(), t));
ctx_getter_templ,
Local<FunctionTemplate>(),
static_cast<PropertyAttribute>(ReadOnly | DontDelete));

target->Set(secureContextString, t->GetFunction());
env->set_secure_context_constructor_template(t);
Expand Down Expand Up @@ -1565,8 +1570,7 @@ int SecureContext::TicketCompatibilityCallback(SSL* ssl,
#endif


void SecureContext::CtxGetter(Local<String> property,
const PropertyCallbackInfo<Value>& info) {
void SecureContext::CtxGetter(const FunctionCallbackInfo<Value>& info) {
SecureContext* sc;
ASSIGN_OR_RETURN_UNWRAP(&sc, info.This());
Local<External> ext = External::New(info.GetIsolate(), sc->ctx_);
Expand Down Expand Up @@ -1636,14 +1640,17 @@ void SSLWrap<Base>::AddMethods(Environment* env, Local<FunctionTemplate> t) {
env->SetProtoMethod(t, "getALPNNegotiatedProtocol", GetALPNNegotiatedProto);
env->SetProtoMethod(t, "setALPNProtocols", SetALPNProtocols);

t->PrototypeTemplate()->SetAccessor(
Local<FunctionTemplate> ssl_getter_templ =
FunctionTemplate::New(env->isolate(),
SSLGetter,
Local<Value>(),
Signature::New(env->isolate(), t));

t->PrototypeTemplate()->SetAccessorProperty(
FIXED_ONE_BYTE_STRING(env->isolate(), "_external"),
SSLGetter,
nullptr,
env->as_external(),
DEFAULT,
static_cast<PropertyAttribute>(ReadOnly | DontDelete),
AccessorSignature::New(env->isolate(), t));
ssl_getter_templ,
Local<FunctionTemplate>(),
static_cast<PropertyAttribute>(ReadOnly | DontDelete));
}


Expand Down Expand Up @@ -2804,8 +2811,7 @@ void SSLWrap<Base>::CertCbDone(const FunctionCallbackInfo<Value>& args) {


template <class Base>
void SSLWrap<Base>::SSLGetter(Local<String> property,
const PropertyCallbackInfo<Value>& info) {
void SSLWrap<Base>::SSLGetter(const FunctionCallbackInfo<Value>& info) {
Base* base;
ASSIGN_OR_RETURN_UNWRAP(&base, info.This());
SSL* ssl = base->ssl_;
Expand Down Expand Up @@ -4779,14 +4785,17 @@ void DiffieHellman::Initialize(Environment* env, Local<Object> target) {
env->SetProtoMethod(t, "setPublicKey", SetPublicKey);
env->SetProtoMethod(t, "setPrivateKey", SetPrivateKey);

t->InstanceTemplate()->SetAccessor(
Local<FunctionTemplate> verify_error_getter_templ =
FunctionTemplate::New(env->isolate(),
DiffieHellman::VerifyErrorGetter,
Local<Value>(),
Signature::New(env->isolate(), t));

t->InstanceTemplate()->SetAccessorProperty(
env->verify_error_string(),
DiffieHellman::VerifyErrorGetter,
nullptr,
env->as_external(),
DEFAULT,
attributes,
AccessorSignature::New(env->isolate(), t));
verify_error_getter_templ,
Local<FunctionTemplate>(),
attributes);

target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "DiffieHellman"),
t->GetFunction());
Expand All @@ -4801,14 +4810,17 @@ void DiffieHellman::Initialize(Environment* env, Local<Object> target) {
env->SetProtoMethod(t2, "getPublicKey", GetPublicKey);
env->SetProtoMethod(t2, "getPrivateKey", GetPrivateKey);

t2->InstanceTemplate()->SetAccessor(
Local<FunctionTemplate> verify_error_getter_templ2 =
FunctionTemplate::New(env->isolate(),
DiffieHellman::VerifyErrorGetter,
Local<Value>(),
Signature::New(env->isolate(), t2));

t2->InstanceTemplate()->SetAccessorProperty(
env->verify_error_string(),
DiffieHellman::VerifyErrorGetter,
nullptr,
env->as_external(),
DEFAULT,
attributes,
AccessorSignature::New(env->isolate(), t2));
verify_error_getter_templ2,
Local<FunctionTemplate>(),
attributes);

target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "DiffieHellmanGroup"),
t2->GetFunction());
Expand Down Expand Up @@ -5124,8 +5136,7 @@ void DiffieHellman::SetPrivateKey(const FunctionCallbackInfo<Value>& args) {
}


void DiffieHellman::VerifyErrorGetter(Local<String> property,
const PropertyCallbackInfo<Value>& args) {
void DiffieHellman::VerifyErrorGetter(const FunctionCallbackInfo<Value>& args) {
HandleScope scope(args.GetIsolate());

DiffieHellman* diffieHellman;
Expand Down
10 changes: 3 additions & 7 deletions src/node_crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,7 @@ class SecureContext : public BaseObject {
const v8::FunctionCallbackInfo<v8::Value>& args);
static void EnableTicketKeyCallback(
const v8::FunctionCallbackInfo<v8::Value>& args);
static void CtxGetter(v8::Local<v8::String> property,
const v8::PropertyCallbackInfo<v8::Value>& info);
static void CtxGetter(const v8::FunctionCallbackInfo<v8::Value>& info);

template <bool primary>
static void GetCertificate(const v8::FunctionCallbackInfo<v8::Value>& args);
Expand Down Expand Up @@ -329,8 +328,7 @@ class SSLWrap {
void* arg);
static int TLSExtStatusCallback(SSL* s, void* arg);
static int SSLCertCallback(SSL* s, void* arg);
static void SSLGetter(v8::Local<v8::String> property,
const v8::PropertyCallbackInfo<v8::Value>& info);
static void SSLGetter(const v8::FunctionCallbackInfo<v8::Value>& info);

void DestroySSL();
void WaitForCertCb(CertCb cb, void* arg);
Expand Down Expand Up @@ -695,9 +693,7 @@ class DiffieHellman : public BaseObject {
static void ComputeSecret(const v8::FunctionCallbackInfo<v8::Value>& args);
static void SetPublicKey(const v8::FunctionCallbackInfo<v8::Value>& args);
static void SetPrivateKey(const v8::FunctionCallbackInfo<v8::Value>& args);
static void VerifyErrorGetter(
v8::Local<v8::String> property,
const v8::PropertyCallbackInfo<v8::Value>& args);
static void VerifyErrorGetter(const v8::FunctionCallbackInfo<v8::Value>& args);

DiffieHellman(Environment* env, v8::Local<v8::Object> wrap)
: BaseObject(env, wrap),
Expand Down
73 changes: 39 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,41 @@ 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);

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

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

Local<FunctionTemplate> get_bytes_read_templ =
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_templ,
Local<FunctionTemplate>(),
attributes);

env->SetProtoMethod(t, "readStart", JSMethod<Base, &StreamBase::ReadStart>);
env->SetProtoMethod(t, "readStop", JSMethod<Base, &StreamBase::ReadStop>);
Expand All @@ -85,8 +95,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 +109,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 +122,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
22 changes: 15 additions & 7 deletions src/udp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ using v8::Local;
using v8::Object;
using v8::PropertyAttribute;
using v8::PropertyCallbackInfo;
using v8::Signature;
using v8::String;
using v8::Uint32;
using v8::Undefined;
Expand Down Expand Up @@ -110,12 +111,19 @@ void UDPWrap::Initialize(Local<Object> target,

enum PropertyAttribute attributes =
static_cast<PropertyAttribute>(v8::ReadOnly | v8::DontDelete);
t->PrototypeTemplate()->SetAccessor(env->fd_string(),
UDPWrap::GetFD,
nullptr,
env->as_external(),
v8::DEFAULT,
attributes);

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

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

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

env->SetProtoMethod(t, "bind", Bind);
env->SetProtoMethod(t, "send", Send);
Expand Down Expand Up @@ -163,7 +171,7 @@ void UDPWrap::New(const FunctionCallbackInfo<Value>& args) {
}


void UDPWrap::GetFD(Local<String>, const PropertyCallbackInfo<Value>& args) {
void UDPWrap::GetFD(const FunctionCallbackInfo<Value>& args) {
int fd = UV_EBADF;
#if !defined(_WIN32)
UDPWrap* wrap = Unwrap<UDPWrap>(args.This());
Expand Down
3 changes: 1 addition & 2 deletions src/udp_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ class UDPWrap: public HandleWrap {
static void Initialize(v8::Local<v8::Object> target,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context);
static void GetFD(v8::Local<v8::String>,
const v8::PropertyCallbackInfo<v8::Value>&);
static void GetFD(const v8::FunctionCallbackInfo<v8::Value>& args);
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Bind(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Send(const v8::FunctionCallbackInfo<v8::Value>& args);
Expand Down
25 changes: 20 additions & 5 deletions test/parallel/test-stream-base-prototype-accessors.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,33 @@ 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
assert.throws(() => {
TTY.prototype.bytesRead;
}, msg);
}, TypeError);

assert.throws(() => {
TTY.prototype.fd;
}, msg);
}, TypeError);

assert.throws(() => {
TTY.prototype._externalStream;
}, msg);
}, TypeError);

// 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'
);
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.

}