@@ -516,6 +516,7 @@ class fs_req_wrap {
516
516
DISALLOW_COPY_AND_ASSIGN (fs_req_wrap);
517
517
};
518
518
519
+ // Returns nullptr if the operation fails from the start.
519
520
template <typename Func, typename ... Args>
520
521
inline FSReqBase* AsyncDestCall (Environment* env,
521
522
FSReqBase* req_wrap,
@@ -530,16 +531,16 @@ inline FSReqBase* AsyncDestCall(Environment* env,
530
531
uv_fs_t * uv_req = req_wrap->req ();
531
532
uv_req->result = err;
532
533
uv_req->path = nullptr ;
533
- after (uv_req);
534
+ after (uv_req); // after may delete req_wrap if there is an error
534
535
req_wrap = nullptr ;
536
+ } else {
537
+ req_wrap->SetReturnValue (args);
535
538
}
536
539
537
- if (req_wrap != nullptr ) {
538
- args.GetReturnValue ().Set (req_wrap->persistent ());
539
- }
540
540
return req_wrap;
541
541
}
542
542
543
+ // Returns nullptr if the operation fails from the start.
543
544
template <typename Func, typename ... Args>
544
545
inline FSReqBase* AsyncCall (Environment* env,
545
546
FSReqBase* req_wrap,
@@ -618,7 +619,6 @@ void Access(const FunctionCallbackInfo<Value>& args) {
618
619
if (req_wrap != nullptr ) { // access(path, mode, req)
619
620
AsyncCall (env, req_wrap, args, " access" , UTF8, AfterNoArgs,
620
621
uv_fs_access, *path, mode);
621
- req_wrap->SetReturnValue (args);
622
622
} else { // access(path, mode, undefined, ctx)
623
623
CHECK_EQ (argc, 4 );
624
624
fs_req_wrap req_wrap;
@@ -640,7 +640,6 @@ void Close(const FunctionCallbackInfo<Value>& args) {
640
640
if (req_wrap != nullptr ) { // close(fd, req)
641
641
AsyncCall (env, req_wrap, args, " close" , UTF8, AfterNoArgs,
642
642
uv_fs_close, fd);
643
- req_wrap->SetReturnValue (args);
644
643
} else { // close(fd, undefined, ctx)
645
644
CHECK_EQ (argc, 3 );
646
645
fs_req_wrap req_wrap;
@@ -749,7 +748,6 @@ static void Stat(const FunctionCallbackInfo<Value>& args) {
749
748
if (req_wrap != nullptr ) { // stat(path, req)
750
749
AsyncCall (env, req_wrap, args, " stat" , UTF8, AfterStat,
751
750
uv_fs_stat, *path);
752
- req_wrap->SetReturnValue (args);
753
751
} else { // stat(path, undefined, ctx)
754
752
CHECK_EQ (argc, 3 );
755
753
fs_req_wrap req_wrap;
@@ -774,7 +772,6 @@ static void LStat(const FunctionCallbackInfo<Value>& args) {
774
772
if (req_wrap != nullptr ) { // lstat(path, req)
775
773
AsyncCall (env, req_wrap, args, " lstat" , UTF8, AfterStat,
776
774
uv_fs_lstat, *path);
777
- req_wrap->SetReturnValue (args);
778
775
} else { // lstat(path, undefined, ctx)
779
776
CHECK_EQ (argc, 3 );
780
777
fs_req_wrap req_wrap;
@@ -799,7 +796,6 @@ static void FStat(const FunctionCallbackInfo<Value>& args) {
799
796
if (req_wrap != nullptr ) { // fstat(fd, req)
800
797
AsyncCall (env, req_wrap, args, " fstat" , UTF8, AfterStat,
801
798
uv_fs_fstat, fd);
802
- req_wrap->SetReturnValue (args);
803
799
} else { // fstat(fd, undefined, ctx)
804
800
CHECK_EQ (argc, 3 );
805
801
fs_req_wrap req_wrap;
@@ -853,7 +849,6 @@ static void Link(const FunctionCallbackInfo<Value>& args) {
853
849
if (req_wrap != nullptr ) { // link(src, dest, req)
854
850
AsyncDestCall (env, req_wrap, args, " link" , *dest, dest.length (), UTF8,
855
851
AfterNoArgs, uv_fs_link, *src, *dest);
856
- req_wrap->SetReturnValue (args);
857
852
} else { // link(src, dest)
858
853
CHECK_EQ (argc, 4 );
859
854
fs_req_wrap req;
@@ -877,7 +872,6 @@ static void ReadLink(const FunctionCallbackInfo<Value>& args) {
877
872
if (req_wrap != nullptr ) { // readlink(path, encoding, req)
878
873
AsyncCall (env, req_wrap, args, " readlink" , encoding, AfterStringPtr,
879
874
uv_fs_readlink, *path);
880
- req_wrap->SetReturnValue (args);
881
875
} else {
882
876
CHECK_EQ (argc, 4 );
883
877
fs_req_wrap req;
@@ -918,7 +912,6 @@ static void Rename(const FunctionCallbackInfo<Value>& args) {
918
912
if (req_wrap != nullptr ) {
919
913
AsyncDestCall (env, req_wrap, args, " rename" , *new_path, new_path.length (),
920
914
UTF8, AfterNoArgs, uv_fs_rename, *old_path, *new_path);
921
- req_wrap->SetReturnValue (args);
922
915
} else {
923
916
CHECK_EQ (argc, 4 );
924
917
fs_req_wrap req;
@@ -942,7 +935,6 @@ static void FTruncate(const FunctionCallbackInfo<Value>& args) {
942
935
if (req_wrap != nullptr ) {
943
936
AsyncCall (env, req_wrap, args, " ftruncate" , UTF8, AfterNoArgs,
944
937
uv_fs_ftruncate, fd, len);
945
- req_wrap->SetReturnValue (args);
946
938
} else {
947
939
CHECK_EQ (argc, 4 );
948
940
fs_req_wrap req;
@@ -963,7 +955,6 @@ static void Fdatasync(const FunctionCallbackInfo<Value>& args) {
963
955
if (req_wrap != nullptr ) {
964
956
AsyncCall (env, req_wrap, args, " fdatasync" , UTF8, AfterNoArgs,
965
957
uv_fs_fdatasync, fd);
966
- req_wrap->SetReturnValue (args);
967
958
} else {
968
959
CHECK_EQ (argc, 3 );
969
960
fs_req_wrap req;
@@ -984,7 +975,6 @@ static void Fsync(const FunctionCallbackInfo<Value>& args) {
984
975
if (req_wrap != nullptr ) {
985
976
AsyncCall (env, req_wrap, args, " fsync" , UTF8, AfterNoArgs,
986
977
uv_fs_fsync, fd);
987
- req_wrap->SetReturnValue (args);
988
978
} else {
989
979
CHECK_EQ (argc, 3 );
990
980
fs_req_wrap req;
@@ -1005,7 +995,6 @@ static void Unlink(const FunctionCallbackInfo<Value>& args) {
1005
995
if (req_wrap != nullptr ) {
1006
996
AsyncCall (env, req_wrap, args, " unlink" , UTF8, AfterNoArgs,
1007
997
uv_fs_unlink, *path);
1008
- req_wrap->SetReturnValue (args);
1009
998
} else {
1010
999
CHECK_EQ (argc, 3 );
1011
1000
fs_req_wrap req;
@@ -1025,7 +1014,6 @@ static void RMDir(const FunctionCallbackInfo<Value>& args) {
1025
1014
if (req_wrap != nullptr ) {
1026
1015
AsyncCall (env, req_wrap, args, " rmdir" , UTF8, AfterNoArgs,
1027
1016
uv_fs_rmdir, *path);
1028
- req_wrap->SetReturnValue (args);
1029
1017
} else {
1030
1018
SYNC_CALL (rmdir, *path, *path)
1031
1019
}
@@ -1046,7 +1034,6 @@ static void MKDir(const FunctionCallbackInfo<Value>& args) {
1046
1034
if (req_wrap != nullptr ) {
1047
1035
AsyncCall (env, req_wrap, args, " mkdir" , UTF8, AfterNoArgs,
1048
1036
uv_fs_mkdir, *path, mode);
1049
- req_wrap->SetReturnValue (args);
1050
1037
} else {
1051
1038
SYNC_CALL (mkdir, *path, *path, mode)
1052
1039
}
@@ -1064,7 +1051,6 @@ static void RealPath(const FunctionCallbackInfo<Value>& args) {
1064
1051
if (req_wrap != nullptr ) {
1065
1052
AsyncCall (env, req_wrap, args, " realpath" , encoding, AfterStringPtr,
1066
1053
uv_fs_realpath, *path);
1067
- req_wrap->SetReturnValue (args);
1068
1054
} else {
1069
1055
SYNC_CALL (realpath, *path, *path);
1070
1056
const char * link_path = static_cast <const char *>(SYNC_REQ.ptr );
@@ -1096,7 +1082,6 @@ static void ReadDir(const FunctionCallbackInfo<Value>& args) {
1096
1082
if (req_wrap != nullptr ) {
1097
1083
AsyncCall (env, req_wrap, args, " scandir" , encoding, AfterScanDir,
1098
1084
uv_fs_scandir, *path, 0 /* flags*/ );
1099
- req_wrap->SetReturnValue (args);
1100
1085
} else {
1101
1086
SYNC_CALL (scandir, *path, *path, 0 /* flags*/ )
1102
1087
@@ -1167,7 +1152,6 @@ static void Open(const FunctionCallbackInfo<Value>& args) {
1167
1152
if (req_wrap != nullptr ) {
1168
1153
AsyncCall (env, req_wrap, args, " open" , UTF8, AfterInteger,
1169
1154
uv_fs_open, *path, flags, mode);
1170
- req_wrap->SetReturnValue (args);
1171
1155
} else {
1172
1156
SYNC_CALL (open, *path, *path, flags, mode)
1173
1157
args.GetReturnValue ().Set (SYNC_RESULT);
@@ -1192,7 +1176,6 @@ static void OpenFileHandle(const FunctionCallbackInfo<Value>& args) {
1192
1176
if (req_wrap != nullptr ) {
1193
1177
AsyncCall (env, req_wrap, args, " open" , UTF8, AfterOpenFileHandle,
1194
1178
uv_fs_open, *path, flags, mode);
1195
- req_wrap->SetReturnValue (args);
1196
1179
} else {
1197
1180
SYNC_CALL (open, *path, *path, flags, mode)
1198
1181
HandleScope scope (env->isolate ());
@@ -1217,7 +1200,6 @@ static void CopyFile(const FunctionCallbackInfo<Value>& args) {
1217
1200
if (req_wrap != nullptr ) {
1218
1201
AsyncCall (env, req_wrap, args, " copyfile" , UTF8, AfterNoArgs,
1219
1202
uv_fs_copyfile, *src, *dest, flags);
1220
- req_wrap->SetReturnValue (args);
1221
1203
} else {
1222
1204
SYNC_DEST_CALL (copyfile, *src, *dest, *src, *dest, flags)
1223
1205
}
@@ -1260,7 +1242,7 @@ static void WriteBuffer(const FunctionCallbackInfo<Value>& args) {
1260
1242
if (req_wrap != nullptr ) {
1261
1243
AsyncCall (env, req_wrap, args, " write" , UTF8, AfterInteger,
1262
1244
uv_fs_write, fd, &uvbuf, 1 , pos);
1263
- return req_wrap-> SetReturnValue (args) ;
1245
+ return ;
1264
1246
}
1265
1247
1266
1248
SYNC_CALL (write, nullptr , fd, &uvbuf, 1 , pos)
@@ -1297,7 +1279,7 @@ static void WriteBuffers(const FunctionCallbackInfo<Value>& args) {
1297
1279
if (req_wrap != nullptr ) {
1298
1280
AsyncCall (env, req_wrap, args, " write" , UTF8, AfterInteger,
1299
1281
uv_fs_write, fd, *iovs, iovs.length (), pos);
1300
- return req_wrap-> SetReturnValue (args) ;
1282
+ return ;
1301
1283
}
1302
1284
1303
1285
SYNC_CALL (write, nullptr , fd, *iovs, iovs.length (), pos)
@@ -1365,7 +1347,6 @@ static void WriteString(const FunctionCallbackInfo<Value>& args) {
1365
1347
if (req_wrap != nullptr ) {
1366
1348
AsyncCall (env, req_wrap, args, " write" , UTF8, AfterInteger,
1367
1349
uv_fs_write, fd, &uvbuf, 1 , pos);
1368
- req_wrap->SetReturnValue (args);
1369
1350
} else {
1370
1351
SYNC_CALL (write, nullptr , fd, &uvbuf, 1 , pos)
1371
1352
return args.GetReturnValue ().Set (SYNC_RESULT);
@@ -1420,7 +1401,6 @@ static void Read(const FunctionCallbackInfo<Value>& args) {
1420
1401
if (req_wrap != nullptr ) {
1421
1402
AsyncCall (env, req_wrap, args, " read" , UTF8, AfterInteger,
1422
1403
uv_fs_read, fd, &uvbuf, 1 , pos);
1423
- req_wrap->SetReturnValue (args);
1424
1404
} else {
1425
1405
SYNC_CALL (read, 0 , fd, &uvbuf, 1 , pos)
1426
1406
args.GetReturnValue ().Set (SYNC_RESULT);
@@ -1446,7 +1426,6 @@ static void Chmod(const FunctionCallbackInfo<Value>& args) {
1446
1426
if (req_wrap != nullptr ) {
1447
1427
AsyncCall (env, req_wrap, args, " chmod" , UTF8, AfterNoArgs,
1448
1428
uv_fs_chmod, *path, mode);
1449
- req_wrap->SetReturnValue (args);
1450
1429
} else {
1451
1430
SYNC_CALL (chmod, *path, *path, mode);
1452
1431
}
@@ -1469,7 +1448,6 @@ static void FChmod(const FunctionCallbackInfo<Value>& args) {
1469
1448
if (req_wrap != nullptr ) {
1470
1449
AsyncCall (env, req_wrap, args, " fchmod" , UTF8, AfterNoArgs,
1471
1450
uv_fs_fchmod, fd, mode);
1472
- req_wrap->SetReturnValue (args);
1473
1451
} else {
1474
1452
SYNC_CALL (fchmod, 0 , fd, mode);
1475
1453
}
@@ -1497,7 +1475,6 @@ static void Chown(const FunctionCallbackInfo<Value>& args) {
1497
1475
if (req_wrap != nullptr ) {
1498
1476
AsyncCall (env, req_wrap, args, " chown" , UTF8, AfterNoArgs,
1499
1477
uv_fs_chown, *path, uid, gid);
1500
- req_wrap->SetReturnValue (args);
1501
1478
} else {
1502
1479
SYNC_CALL (chown, *path, *path, uid, gid);
1503
1480
}
@@ -1522,7 +1499,6 @@ static void FChown(const FunctionCallbackInfo<Value>& args) {
1522
1499
if (req_wrap != nullptr ) {
1523
1500
AsyncCall (env, req_wrap, args, " fchown" , UTF8, AfterNoArgs,
1524
1501
uv_fs_fchown, fd, uid, gid);
1525
- req_wrap->SetReturnValue (args);
1526
1502
} else {
1527
1503
SYNC_CALL (fchown, 0 , fd, uid, gid);
1528
1504
}
@@ -1546,7 +1522,6 @@ static void UTimes(const FunctionCallbackInfo<Value>& args) {
1546
1522
if (req_wrap != nullptr ) {
1547
1523
AsyncCall (env, req_wrap, args, " utime" , UTF8, AfterNoArgs,
1548
1524
uv_fs_utime, *path, atime, mtime);
1549
- req_wrap->SetReturnValue (args);
1550
1525
} else {
1551
1526
SYNC_CALL (utime, *path, *path, atime, mtime);
1552
1527
}
@@ -1567,7 +1542,6 @@ static void FUTimes(const FunctionCallbackInfo<Value>& args) {
1567
1542
if (req_wrap != nullptr ) {
1568
1543
AsyncCall (env, req_wrap, args, " futime" , UTF8, AfterNoArgs,
1569
1544
uv_fs_futime, fd, atime, mtime);
1570
- req_wrap->SetReturnValue (args);
1571
1545
} else {
1572
1546
SYNC_CALL (futime, 0 , fd, atime, mtime);
1573
1547
}
@@ -1587,7 +1561,6 @@ static void Mkdtemp(const FunctionCallbackInfo<Value>& args) {
1587
1561
if (req_wrap != nullptr ) {
1588
1562
AsyncCall (env, req_wrap, args, " mkdtemp" , encoding, AfterStringPath,
1589
1563
uv_fs_mkdtemp, *tmpl);
1590
- req_wrap->SetReturnValue (args);
1591
1564
} else {
1592
1565
SYNC_CALL (mkdtemp, *tmpl, *tmpl);
1593
1566
const char * path = static_cast <const char *>(SYNC_REQ.path );
0 commit comments