Browse Source

Callbacks from process.fs always start with error object

v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
987441283b
  1. 7
      src/node.js
  2. 28
      src/node_file.cc

7
src/node.js

@ -507,11 +507,12 @@ var posixModule = createInternalModule("posix", function (exports) {
exports.Stats = process.Stats; exports.Stats = process.Stats;
function callback (promise) { function callback (promise) {
return function () { return function (error) {
if (arguments[0] instanceof Error) { if (error) {
promise.emitError.apply(promise, arguments); promise.emitError.apply(promise, arguments);
} else { } else {
promise.emitSuccess.apply(promise, arguments); promise.emitSuccess.apply(promise,
Array.prototype.slice.call(arguments, 1));
} }
}; };
} }

28
src/node_file.cc

@ -37,12 +37,16 @@ static int After(eio_req *req) {
ev_unref(EV_DEFAULT_UC); ev_unref(EV_DEFAULT_UC);
int argc = 0; int argc = 0;
Local<Value> argv[5]; // 5 is the maximum number of args Local<Value> argv[6]; // 6 is the maximum number of args
if (req->errorno != 0) { if (req->errorno != 0) {
argc = 1; argc = 1;
argv[0] = errno_exception(req->errorno); argv[0] = errno_exception(req->errorno);
} else { } 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<Value>::New(Null());
switch (req->type) { switch (req->type) {
case EIO_CLOSE: case EIO_CLOSE:
case EIO_RENAME: case EIO_RENAME:
@ -54,30 +58,30 @@ static int After(eio_req *req) {
case EIO_OPEN: case EIO_OPEN:
case EIO_SENDFILE: case EIO_SENDFILE:
argc = 1; argc = 2;
argv[0] = Integer::New(req->result); argv[1] = Integer::New(req->result);
break; break;
case EIO_WRITE: case EIO_WRITE:
argc = 1; argc = 2;
argv[0] = Integer::New(req->result); argv[1] = Integer::New(req->result);
break; break;
case EIO_STAT: case EIO_STAT:
{ {
struct stat *s = reinterpret_cast<struct stat*>(req->ptr2); struct stat *s = reinterpret_cast<struct stat*>(req->ptr2);
argc = 1; argc = 2;
argv[0] = BuildStatsObject(s); argv[1] = BuildStatsObject(s);
break; break;
} }
case EIO_READ: case EIO_READ:
{ {
argc = 2; argc = 3;
Local<Object> obj = Local<Object>::New(*callback); Local<Object> obj = Local<Object>::New(*callback);
Local<Value> enc_val = obj->GetHiddenValue(encoding_symbol); Local<Value> enc_val = obj->GetHiddenValue(encoding_symbol);
argv[0] = Encode(req->ptr2, req->result, ParseEncoding(enc_val)); argv[1] = Encode(req->ptr2, req->result, ParseEncoding(enc_val));
argv[1] = Integer::New(req->result); argv[2] = Integer::New(req->result);
break; break;
} }
@ -100,8 +104,8 @@ static int After(eio_req *req) {
#endif #endif
} }
argc = 1; argc = 2;
argv[0] = names; argv[1] = names;
break; break;
} }

Loading…
Cancel
Save