@@ -77,8 +77,13 @@ void StreamBase::AddMethods(Environment* env,
77
77
template <class Base >
78
78
void StreamBase::GetFD (Local<String> key,
79
79
const PropertyCallbackInfo<Value>& args) {
80
- StreamBase* wrap = Unwrap<Base>(args.Holder ());
80
+ Base* handle = Unwrap<Base>(args.Holder ());
81
81
82
+ // Mimic implementation of StreamBase::GetFD() and UDPWrap::GetFD().
83
+ if (handle == nullptr )
84
+ return args.GetReturnValue ().Set (-1 );
85
+
86
+ StreamBase* wrap = static_cast <StreamBase*>(handle);
82
87
if (!wrap->IsAlive ())
83
88
return args.GetReturnValue ().Set (UV_EINVAL);
84
89
@@ -89,8 +94,13 @@ void StreamBase::GetFD(Local<String> key,
89
94
template <class Base >
90
95
void StreamBase::GetBytesRead (Local<String> key,
91
96
const PropertyCallbackInfo<Value>& args) {
92
- StreamBase* wrap = Unwrap<Base>(args.Holder ());
97
+ Base* handle = Unwrap<Base>(args.Holder ());
98
+
99
+ // The handle instance hasn't been set. So no bytes could have been read.
100
+ if (handle == nullptr )
101
+ return args.GetReturnValue ().Set (0 );
93
102
103
+ StreamBase* wrap = static_cast <StreamBase*>(handle);
94
104
// uint64_t -> double. 53bits is enough for all real cases.
95
105
args.GetReturnValue ().Set (static_cast <double >(wrap->bytes_read_ ));
96
106
}
@@ -99,8 +109,12 @@ void StreamBase::GetBytesRead(Local<String> key,
99
109
template <class Base >
100
110
void StreamBase::GetExternal (Local<String> key,
101
111
const PropertyCallbackInfo<Value>& args) {
102
- StreamBase* wrap = Unwrap<Base>(args.Holder ());
112
+ Base* handle = Unwrap<Base>(args.Holder ());
103
113
114
+ if (handle == nullptr )
115
+ return args.GetReturnValue ().SetUndefined ();
116
+
117
+ StreamBase* wrap = static_cast <StreamBase*>(handle);
104
118
Local<External> ext = External::New (args.GetIsolate (), wrap);
105
119
args.GetReturnValue ().Set (ext);
106
120
}
@@ -109,8 +123,12 @@ void StreamBase::GetExternal(Local<String> key,
109
123
template <class Base ,
110
124
int (StreamBase::*Method)(const FunctionCallbackInfo<Value>& args)>
111
125
void StreamBase::JSMethod(const FunctionCallbackInfo<Value>& args) {
112
- StreamBase* wrap = Unwrap<Base>(args.Holder ());
126
+ Base* handle = Unwrap<Base>(args.Holder ());
127
+
128
+ if (handle == nullptr )
129
+ return args.GetReturnValue ().SetUndefined ();
113
130
131
+ StreamBase* wrap = static_cast <StreamBase*>(handle);
114
132
if (!wrap->IsAlive ())
115
133
return args.GetReturnValue ().Set (UV_EINVAL);
116
134
0 commit comments