Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 23fdb7a

Browse files
RafaelGSSaddaleax
andcommitted
src: make ReqWrap weak
This commit allows throwing an exception after creating `FSReqCallback` Co-authored-by: Anna Henningsen <[email protected]>
1 parent 07d7e1b commit 23fdb7a

File tree

5 files changed

+13
-15
lines changed

5 files changed

+13
-15
lines changed

lib/fs.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@
2424

2525
'use strict';
2626

27-
// When using FSReqCallback, make sure to create the object only *after* all
28-
// parameter validation has happened, so that the objects are not kept in memory
29-
// in case they are created but never used due to an exception.
30-
3127
const {
3228
ArrayPrototypePush,
3329
BigIntPrototypeToString,

src/cares_wrap.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,7 +1429,7 @@ static void Query(const FunctionCallbackInfo<Value>& args) {
14291429

14301430
void AfterGetAddrInfo(uv_getaddrinfo_t* req, int status, struct addrinfo* res) {
14311431
auto cleanup = OnScopeLeave([&]() { uv_freeaddrinfo(res); });
1432-
std::unique_ptr<GetAddrInfoReqWrap> req_wrap {
1432+
BaseObjectPtr<GetAddrInfoReqWrap> req_wrap {
14331433
static_cast<GetAddrInfoReqWrap*>(req->data)};
14341434
Environment* env = req_wrap->env();
14351435

@@ -1502,7 +1502,7 @@ void AfterGetNameInfo(uv_getnameinfo_t* req,
15021502
int status,
15031503
const char* hostname,
15041504
const char* service) {
1505-
std::unique_ptr<GetNameInfoReqWrap> req_wrap {
1505+
BaseObjectPtr<GetNameInfoReqWrap> req_wrap {
15061506
static_cast<GetNameInfoReqWrap*>(req->data)};
15071507
Environment* env = req_wrap->env();
15081508

src/connection_wrap.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ void ConnectionWrap<WrapType, UVType>::OnConnection(uv_stream_t* handle,
7777
template <typename WrapType, typename UVType>
7878
void ConnectionWrap<WrapType, UVType>::AfterConnect(uv_connect_t* req,
7979
int status) {
80-
std::unique_ptr<ConnectWrap> req_wrap
81-
(static_cast<ConnectWrap*>(req->data));
82-
CHECK_NOT_NULL(req_wrap);
80+
BaseObjectPtr<ConnectWrap> req_wrap
81+
{ static_cast<ConnectWrap*>(req->data) };
82+
CHECK(req_wrap);
8383
WrapType* wrap = static_cast<WrapType*>(req->handle->data);
8484
CHECK_EQ(req_wrap->env(), wrap->env());
8585
Environment* env = wrap->env();

src/node_file.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,8 @@ MaybeLocal<Promise> FileHandle::ClosePromise() {
374374

375375
CloseReq* req = new CloseReq(env(), close_req_obj, promise, object());
376376
auto AfterClose = uv_fs_callback_t{[](uv_fs_t* req) {
377-
std::unique_ptr<CloseReq> close(CloseReq::from_req(req));
378-
CHECK_NOT_NULL(close);
377+
BaseObjectPtr<CloseReq> close(CloseReq::from_req(req));
378+
CHECK(close);
379379
close->file_handle()->AfterClose();
380380
if (!close->env()->can_call_into_js()) return;
381381
Isolate* isolate = close->env()->isolate();

src/req_wrap-inl.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ ReqWrap<T>::ReqWrap(Environment* env,
2020
AsyncWrap::ProviderType provider)
2121
: AsyncWrap(env, object, provider),
2222
ReqWrapBase(env) {
23+
MakeWeak();
2324
Reset();
2425
}
2526

2627
template <typename T>
2728
ReqWrap<T>::~ReqWrap() {
28-
CHECK_EQ(false, persistent().IsEmpty());
2929
}
3030

3131
template <typename T>
@@ -120,7 +120,8 @@ struct MakeLibuvRequestCallback<ReqT, void(*)(ReqT*, Args...)> {
120120
using F = void(*)(ReqT* req, Args... args);
121121

122122
static void Wrapper(ReqT* req, Args... args) {
123-
ReqWrap<ReqT>* req_wrap = ReqWrap<ReqT>::from_req(req);
123+
BaseObjectPtr<ReqWrap<ReqT>> req_wrap { ReqWrap<ReqT>::from_req(req) };
124+
req_wrap->MakeWeak();
124125
req_wrap->env()->DecreaseWaitingRequestCounter();
125126
F original_callback = reinterpret_cast<F>(req_wrap->original_callback_);
126127
original_callback(req, args...);
@@ -138,7 +139,6 @@ template <typename T>
138139
template <typename LibuvFunction, typename... Args>
139140
int ReqWrap<T>::Dispatch(LibuvFunction fn, Args... args) {
140141
Dispatched();
141-
142142
// This expands as:
143143
//
144144
// int err = fn(env()->event_loop(), req(), arg1, arg2, Wrapper, arg3, ...)
@@ -158,8 +158,10 @@ int ReqWrap<T>::Dispatch(LibuvFunction fn, Args... args) {
158158
env()->event_loop(),
159159
req(),
160160
MakeLibuvRequestCallback<T, Args>::For(this, args)...);
161-
if (err >= 0)
161+
if (err >= 0) {
162+
ClearWeak();
162163
env()->IncreaseWaitingRequestCounter();
164+
}
163165
return err;
164166
}
165167

0 commit comments

Comments
 (0)