@@ -110,7 +110,7 @@ using v8::Value;
110
110
# define MIN (a, b ) ((a) < (b) ? (a) : (b))
111
111
#endif
112
112
113
- #define GET_OFFSET (a ) ((a)->IsNumber () ? (a)->IntegerValue () : -1)
113
+ #define GET_OFFSET (a ) ((a)->IsNumber () ? (a).As<Integer>()->Value () : -1)
114
114
115
115
// The FileHandle object wraps a file descriptor and will close it on garbage
116
116
// collection if necessary. If that happens, a process warning will be
@@ -1411,51 +1411,50 @@ static void WriteString(const FunctionCallbackInfo<Value>& args) {
1411
1411
*
1412
1412
* bytesRead = fs.read(fd, buffer, offset, length, position)
1413
1413
*
1414
- * 0 fd integer . file descriptor
1414
+ * 0 fd int32 . file descriptor
1415
1415
* 1 buffer instance of Buffer
1416
- * 2 offset integer. offset to start reading into inside buffer
1417
- * 3 length integer. length to read
1418
- * 4 position file position - null for current position
1419
- *
1416
+ * 2 offset int32. offset to start reading into inside buffer
1417
+ * 3 length int32. length to read
1418
+ * 4 position int64. file position - -1 for current position
1420
1419
*/
1421
1420
static void Read (const FunctionCallbackInfo<Value>& args) {
1422
1421
Environment* env = Environment::GetCurrent (args);
1423
1422
1424
- CHECK (args[0 ]->IsInt32 ());
1425
- CHECK (Buffer::HasInstance (args[1 ]));
1426
-
1427
- int fd = args[0 ]->Int32Value ();
1428
-
1429
- Local<Value> req;
1430
-
1431
- size_t len;
1432
- int64_t pos;
1423
+ const int argc = args.Length ();
1424
+ CHECK_GE (argc, 5 );
1433
1425
1434
- char * buf = nullptr ;
1426
+ CHECK (args[0 ]->IsInt32 ());
1427
+ const int fd = args[0 ].As <Int32>()->Value ();
1435
1428
1429
+ CHECK (Buffer::HasInstance (args[1 ]));
1436
1430
Local<Object> buffer_obj = args[1 ].As <Object>();
1437
- char * buffer_data = Buffer::Data (buffer_obj);
1431
+ char * buffer_data = Buffer::Data (buffer_obj);
1438
1432
size_t buffer_length = Buffer::Length (buffer_obj);
1439
1433
1440
- size_t off = args[2 ]->Int32Value ();
1434
+ CHECK (args[2 ]->IsInt32 ());
1435
+ const size_t off = static_cast <size_t >(args[2 ].As <Int32>()->Value ());
1441
1436
CHECK_LT (off, buffer_length);
1442
1437
1443
- len = args[3 ]->Int32Value ();
1438
+ CHECK (args[3 ]->IsInt32 ());
1439
+ const size_t len = static_cast <size_t >(args[3 ].As <Int32>()->Value ());
1444
1440
CHECK (Buffer::IsWithinBounds (off, len, buffer_length));
1445
1441
1446
- pos = GET_OFFSET (args[4 ]);
1447
-
1448
- buf = buffer_data + off;
1442
+ CHECK (args[4 ]->IsNumber ());
1443
+ const int64_t pos = args[4 ].As <Integer>()->Value ();
1449
1444
1445
+ char * buf = buffer_data + off;
1450
1446
uv_buf_t uvbuf = uv_buf_init (const_cast <char *>(buf), len);
1451
1447
1452
1448
FSReqBase* req_wrap = GetReqWrap (env, args[5 ]);
1453
- if (req_wrap != nullptr ) {
1449
+ if (req_wrap != nullptr ) { // read(fd, buffer, offset, len, pos, req)
1454
1450
AsyncCall (env, req_wrap, args, " read" , UTF8, AfterInteger,
1455
1451
uv_fs_read, fd, &uvbuf, 1 , pos);
1456
- } else {
1457
- SYNC_CALL (read, 0 , fd, &uvbuf, 1 , pos)
1458
- args.GetReturnValue ().Set (SYNC_RESULT);
1452
+ } else { // read(fd, buffer, offset, len, pos, undefined, ctx)
1453
+ CHECK_EQ (argc, 7 );
1454
+ fs_req_wrap req_wrap;
1455
+ const int bytesRead = SyncCall (env, args[6 ], &req_wrap, " read" ,
1456
+ uv_fs_read, fd, &uvbuf, 1 , pos);
1457
+ args.GetReturnValue ().Set (bytesRead);
1459
1458
}
1460
1459
}
1461
1460
0 commit comments