Skip to content

Commit 329e162

Browse files
committed
Change to using SetAccessorProperty instead of SetAccessor in StreamBase
Fixes: nodejs#17636 Refs: nodejs#16482 Refs: nodejs#16860
1 parent 04ae486 commit 329e162

File tree

3 files changed

+61
-42
lines changed

3 files changed

+61
-42
lines changed

src/stream_base-inl.h

+40-34
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace node {
1313

14-
using v8::AccessorSignature;
14+
using v8::Signature;
1515
using v8::External;
1616
using v8::FunctionCallbackInfo;
1717
using v8::FunctionTemplate;
@@ -34,31 +34,42 @@ void StreamBase::AddMethods(Environment* env,
3434
enum PropertyAttribute attributes =
3535
static_cast<PropertyAttribute>(
3636
v8::ReadOnly | v8::DontDelete | v8::DontEnum);
37-
Local<AccessorSignature> signature =
38-
AccessorSignature::New(env->isolate(), t);
39-
t->PrototypeTemplate()->SetAccessor(env->fd_string(),
40-
GetFD<Base>,
41-
nullptr,
42-
env->as_external(),
43-
v8::DEFAULT,
44-
attributes,
45-
signature);
46-
47-
t->PrototypeTemplate()->SetAccessor(env->external_stream_string(),
48-
GetExternal<Base>,
49-
nullptr,
50-
env->as_external(),
51-
v8::DEFAULT,
52-
attributes,
53-
signature);
54-
55-
t->PrototypeTemplate()->SetAccessor(env->bytes_read_string(),
56-
GetBytesRead<Base>,
57-
nullptr,
58-
env->as_external(),
59-
v8::DEFAULT,
60-
attributes,
61-
signature);
37+
38+
Local<Signature> signature =
39+
Signature::New(env->isolate(), t);
40+
41+
Local<FunctionTemplate> get_fd_templ = FunctionTemplate::New(
42+
env->isolate(),
43+
GetFD<Base>,
44+
Local<Value>(),
45+
signature);
46+
47+
Local<FunctionTemplate> get_external_templ = FunctionTemplate::New(
48+
env->isolate(),
49+
GetExternal<Base>,
50+
Local<Value>(),
51+
signature);
52+
53+
Local<FunctionTemplate> get_bytes_read = FunctionTemplate::New(
54+
env->isolate(),
55+
GetBytesRead<Base>,
56+
Local<Value>(),
57+
signature);
58+
59+
t->PrototypeTemplate()->SetAccessorProperty(env->fd_string(),
60+
get_fd_templ,
61+
Local<FunctionTemplate>(),
62+
attributes);
63+
64+
t->PrototypeTemplate()->SetAccessorProperty(env->external_stream_string(),
65+
get_external_templ,
66+
Local<FunctionTemplate>(),
67+
attributes);
68+
69+
t->PrototypeTemplate()->SetAccessorProperty(env->bytes_read_string(),
70+
get_bytes_read,
71+
Local<FunctionTemplate>(),
72+
attributes);
6273

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

8697

8798
template <class Base>
88-
void StreamBase::GetFD(Local<String> key,
89-
const PropertyCallbackInfo<Value>& args) {
99+
void StreamBase::GetFD(const FunctionCallbackInfo<Value>& args) {
90100
// Mimic implementation of StreamBase::GetFD() and UDPWrap::GetFD().
91101
Base* handle;
92102
ASSIGN_OR_RETURN_UNWRAP(&handle,
@@ -100,10 +110,8 @@ void StreamBase::GetFD(Local<String> key,
100110
args.GetReturnValue().Set(wrap->GetFD());
101111
}
102112

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

118-
119126
template <class Base>
120-
void StreamBase::GetExternal(Local<String> key,
121-
const PropertyCallbackInfo<Value>& args) {
127+
void StreamBase::GetExternal(const FunctionCallbackInfo<Value>& args) {
122128
Base* handle;
123129
ASSIGN_OR_RETURN_UNWRAP(&handle, args.This());
124130

src/stream_base.h

+3-6
Original file line numberDiff line numberDiff line change
@@ -265,16 +265,13 @@ class StreamBase : public StreamResource {
265265
int WriteString(const v8::FunctionCallbackInfo<v8::Value>& args);
266266

267267
template <class Base>
268-
static void GetFD(v8::Local<v8::String> key,
269-
const v8::PropertyCallbackInfo<v8::Value>& args);
268+
static void GetFD(const v8::FunctionCallbackInfo<v8::Value>& args);
270269

271270
template <class Base>
272-
static void GetExternal(v8::Local<v8::String> key,
273-
const v8::PropertyCallbackInfo<v8::Value>& args);
271+
static void GetExternal(const v8::FunctionCallbackInfo<v8::Value>& args);
274272

275273
template <class Base>
276-
static void GetBytesRead(v8::Local<v8::String> key,
277-
const v8::PropertyCallbackInfo<v8::Value>& args);
274+
static void GetBytesRead(const v8::FunctionCallbackInfo<v8::Value>& args);
278275

279276
template <class Base,
280277
int (StreamBase::*Method)(

test/parallel/test-stream-base-prototype-accessors.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ const assert = require('assert');
1010
// Or anything that calls StreamBase::AddMethods when setting up its prototype
1111
const TTY = process.binding('tty_wrap').TTY;
1212

13-
// Should throw instead of raise assertions
1413
{
15-
const msg = /TypeError: Method \w+ called on incompatible receiver/;
14+
// Should throw instead of raise assertions
15+
const msg = /TypeError: Illegal invocation/;
1616
assert.throws(() => {
1717
TTY.prototype.bytesRead;
1818
}, msg);
@@ -24,4 +24,20 @@ const TTY = process.binding('tty_wrap').TTY;
2424
assert.throws(() => {
2525
TTY.prototype._externalStream;
2626
}, msg);
27+
28+
// Should not throw for Object.getOwnPropertyDescriptor
29+
assert.strictEqual(
30+
typeof (Object.getOwnPropertyDescriptor(TTY.prototype, 'bytesRead')),
31+
'object'
32+
);
33+
34+
assert.strictEqual(
35+
typeof (Object.getOwnPropertyDescriptor(TTY.prototype, 'fd')),
36+
'object'
37+
);
38+
39+
assert.strictEqual(
40+
typeof (Object.getOwnPropertyDescriptor(TTY.prototype, '_externalStream')),
41+
'object'
42+
);
2743
}

0 commit comments

Comments
 (0)