Skip to content

Commit 9874412

Browse files
committed
Callbacks from process.fs always start with error object
1 parent fc025f8 commit 9874412

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

src/node.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -507,11 +507,12 @@ var posixModule = createInternalModule("posix", function (exports) {
507507
exports.Stats = process.Stats;
508508

509509
function callback (promise) {
510-
return function () {
511-
if (arguments[0] instanceof Error) {
510+
return function (error) {
511+
if (error) {
512512
promise.emitError.apply(promise, arguments);
513513
} else {
514-
promise.emitSuccess.apply(promise, arguments);
514+
promise.emitSuccess.apply(promise,
515+
Array.prototype.slice.call(arguments, 1));
515516
}
516517
};
517518
}

src/node_file.cc

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@ static int After(eio_req *req) {
3737
ev_unref(EV_DEFAULT_UC);
3838

3939
int argc = 0;
40-
Local<Value> argv[5]; // 5 is the maximum number of args
40+
Local<Value> argv[6]; // 6 is the maximum number of args
4141

4242
if (req->errorno != 0) {
4343
argc = 1;
4444
argv[0] = errno_exception(req->errorno);
4545
} else {
46+
// Note: the error is always given the first argument of the callback.
47+
// If there is no error then then the first argument is null.
48+
argv[0] = Local<Value>::New(Null());
49+
4650
switch (req->type) {
4751
case EIO_CLOSE:
4852
case EIO_RENAME:
@@ -54,30 +58,30 @@ static int After(eio_req *req) {
5458

5559
case EIO_OPEN:
5660
case EIO_SENDFILE:
57-
argc = 1;
58-
argv[0] = Integer::New(req->result);
61+
argc = 2;
62+
argv[1] = Integer::New(req->result);
5963
break;
6064

6165
case EIO_WRITE:
62-
argc = 1;
63-
argv[0] = Integer::New(req->result);
66+
argc = 2;
67+
argv[1] = Integer::New(req->result);
6468
break;
6569

6670
case EIO_STAT:
6771
{
6872
struct stat *s = reinterpret_cast<struct stat*>(req->ptr2);
69-
argc = 1;
70-
argv[0] = BuildStatsObject(s);
73+
argc = 2;
74+
argv[1] = BuildStatsObject(s);
7175
break;
7276
}
7377

7478
case EIO_READ:
7579
{
76-
argc = 2;
80+
argc = 3;
7781
Local<Object> obj = Local<Object>::New(*callback);
7882
Local<Value> enc_val = obj->GetHiddenValue(encoding_symbol);
79-
argv[0] = Encode(req->ptr2, req->result, ParseEncoding(enc_val));
80-
argv[1] = Integer::New(req->result);
83+
argv[1] = Encode(req->ptr2, req->result, ParseEncoding(enc_val));
84+
argv[2] = Integer::New(req->result);
8185
break;
8286
}
8387

@@ -100,8 +104,8 @@ static int After(eio_req *req) {
100104
#endif
101105
}
102106

103-
argc = 1;
104-
argv[0] = names;
107+
argc = 2;
108+
argv[1] = names;
105109
break;
106110
}
107111

0 commit comments

Comments
 (0)