@@ -110,7 +110,7 @@ using v8::Value;
110110# define MIN (a, b ) ((a) < (b) ? (a) : (b))
111111#endif
112112
113- #define GET_OFFSET (a ) ((a)->IsNumber () ? (a)->IntegerValue () : -1)
113+ #define GET_OFFSET (a ) ((a)->IsNumber () ? (a).As<Integer>()->Value () : -1)
114114
115115// The FileHandle object wraps a file descriptor and will close it on garbage
116116// collection if necessary. If that happens, a process warning will be
@@ -1411,51 +1411,50 @@ static void WriteString(const FunctionCallbackInfo<Value>& args) {
14111411 *
14121412 * bytesRead = fs.read(fd, buffer, offset, length, position)
14131413 *
1414- * 0 fd integer . file descriptor
1414+ * 0 fd int32 . file descriptor
14151415 * 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
14201419 */
14211420static void Read (const FunctionCallbackInfo<Value>& args) {
14221421 Environment* env = Environment::GetCurrent (args);
14231422
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 );
14331425
1434- char * buf = nullptr ;
1426+ CHECK (args[0 ]->IsInt32 ());
1427+ const int fd = args[0 ].As <Int32>()->Value ();
14351428
1429+ CHECK (Buffer::HasInstance (args[1 ]));
14361430 Local<Object> buffer_obj = args[1 ].As <Object>();
1437- char * buffer_data = Buffer::Data (buffer_obj);
1431+ char * buffer_data = Buffer::Data (buffer_obj);
14381432 size_t buffer_length = Buffer::Length (buffer_obj);
14391433
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 ());
14411436 CHECK_LT (off, buffer_length);
14421437
1443- len = args[3 ]->Int32Value ();
1438+ CHECK (args[3 ]->IsInt32 ());
1439+ const size_t len = static_cast <size_t >(args[3 ].As <Int32>()->Value ());
14441440 CHECK (Buffer::IsWithinBounds (off, len, buffer_length));
14451441
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 ();
14491444
1445+ char * buf = buffer_data + off;
14501446 uv_buf_t uvbuf = uv_buf_init (const_cast <char *>(buf), len);
14511447
14521448 FSReqBase* req_wrap = GetReqWrap (env, args[5 ]);
1453- if (req_wrap != nullptr ) {
1449+ if (req_wrap != nullptr ) { // read(fd, buffer, offset, len, pos, req)
14541450 AsyncCall (env, req_wrap, args, " read" , UTF8, AfterInteger,
14551451 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);
14591458 }
14601459}
14611460
0 commit comments