From 987441283b77aecea42338d9379a82e5ff53b1f1 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Tue, 2 Feb 2010 11:04:20 -0800 Subject: [PATCH] Callbacks from process.fs always start with error object --- src/node.js | 7 ++++--- src/node_file.cc | 28 ++++++++++++++++------------ 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/node.js b/src/node.js index dbd227b05b..0f890ff0f8 100644 --- a/src/node.js +++ b/src/node.js @@ -507,11 +507,12 @@ var posixModule = createInternalModule("posix", function (exports) { exports.Stats = process.Stats; function callback (promise) { - return function () { - if (arguments[0] instanceof Error) { + return function (error) { + if (error) { promise.emitError.apply(promise, arguments); } else { - promise.emitSuccess.apply(promise, arguments); + promise.emitSuccess.apply(promise, + Array.prototype.slice.call(arguments, 1)); } }; } diff --git a/src/node_file.cc b/src/node_file.cc index 1289064323..522903fd1a 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -37,12 +37,16 @@ static int After(eio_req *req) { ev_unref(EV_DEFAULT_UC); int argc = 0; - Local argv[5]; // 5 is the maximum number of args + Local argv[6]; // 6 is the maximum number of args if (req->errorno != 0) { argc = 1; argv[0] = errno_exception(req->errorno); } else { + // Note: the error is always given the first argument of the callback. + // If there is no error then then the first argument is null. + argv[0] = Local::New(Null()); + switch (req->type) { case EIO_CLOSE: case EIO_RENAME: @@ -54,30 +58,30 @@ static int After(eio_req *req) { case EIO_OPEN: case EIO_SENDFILE: - argc = 1; - argv[0] = Integer::New(req->result); + argc = 2; + argv[1] = Integer::New(req->result); break; case EIO_WRITE: - argc = 1; - argv[0] = Integer::New(req->result); + argc = 2; + argv[1] = Integer::New(req->result); break; case EIO_STAT: { struct stat *s = reinterpret_cast(req->ptr2); - argc = 1; - argv[0] = BuildStatsObject(s); + argc = 2; + argv[1] = BuildStatsObject(s); break; } case EIO_READ: { - argc = 2; + argc = 3; Local obj = Local::New(*callback); Local enc_val = obj->GetHiddenValue(encoding_symbol); - argv[0] = Encode(req->ptr2, req->result, ParseEncoding(enc_val)); - argv[1] = Integer::New(req->result); + argv[1] = Encode(req->ptr2, req->result, ParseEncoding(enc_val)); + argv[2] = Integer::New(req->result); break; } @@ -100,8 +104,8 @@ static int After(eio_req *req) { #endif } - argc = 1; - argv[0] = names; + argc = 2; + argv[1] = names; break; }