var util = require('util');
var Process = process.binding('process_wrap').Process;
+var WriteWrap = process.binding('stream_wrap').WriteWrap;
var uv = process.binding('uv');
var spawn_sync; // Lazy-loaded process.binding('spawn_sync')
return;
}
- var req = { oncomplete: nop };
+ var req = new WriteWrap();
+ req.oncomplete = nop;
var string = JSON.stringify(message) + '\n';
var err = channel.writeUtf8String(req, string, handle);
var constants = require('constants');
var UDP = process.binding('udp_wrap').UDP;
+var SendWrap = process.binding('udp_wrap').SendWrap;
var BIND_STATE_UNBOUND = 0;
var BIND_STATE_BINDING = 1;
self.emit('error', ex);
}
else if (self._handle) {
- var req = { buffer: buffer, length: length }; // Keep reference alive.
+ var req = new SendWrap();
+ req.buffer = buffer; // Keep reference alive.
+ req.length = length;
if (callback) {
req.callback = callback;
req.oncomplete = afterSend;
var cares = process.binding('cares_wrap');
var uv = process.binding('uv');
+var GetAddrInfoReqWrap = cares.GetAddrInfoReqWrap;
+var GetNameInfoReqWrap = cares.GetNameInfoReqWrap;
+
var isIp = net.isIP;
return {};
}
- var req = {
- callback: callback,
- family: family,
- hostname: hostname,
- oncomplete: onlookup
- };
+ var req = new GetAddrInfoReqWrap();
+ req.callback = callback;
+ req.family = family;
+ req.hostname = hostname;
+ req.oncomplete = onlookup;
var err = cares.getaddrinfo(req, hostname, family, hints);
if (err) {
callback = makeAsync(callback);
- var req = {
- callback: callback,
- host: host,
- port: port,
- oncomplete: onlookupservice
- };
+ var req = new GetNameInfoReqWrap();
+ req.callback = callback;
+ req.host = host;
+ req.port = port;
+ req.oncomplete = onlookupservice;
+
var err = cares.getnameinfo(req, host, port);
if (err) throw errnoException(err, 'getnameinfo', host);
var fs = exports;
var Stream = require('stream').Stream;
var EventEmitter = require('events').EventEmitter;
+var FSReqWrap = binding.FSReqWrap;
var Readable = Stream.Readable;
var Writable = Stream.Writable;
fs.exists = function(path, callback) {
if (!nullCheck(path, cb)) return;
- binding.stat(pathModule._makeLong(path), cb);
+ var req = new FSReqWrap();
+ req.oncomplete = cb;
+ binding.stat(pathModule._makeLong(path), req);
function cb(err, stats) {
if (callback) callback(err ? false : true);
}
// list to make the arguments clear.
fs.close = function(fd, callback) {
- binding.close(fd, makeCallback(callback));
+ var req = new FSReqWrap();
+ req.oncomplete = makeCallback(callback);
+ binding.close(fd, req);
};
fs.closeSync = function(fd) {
mode = modeNum(mode, 438 /*=0666*/);
if (!nullCheck(path, callback)) return;
+
+ var req = new FSReqWrap();
+ req.oncomplete = callback;
+
binding.open(pathModule._makeLong(path),
stringToFlags(flags),
mode,
- callback);
+ req);
};
fs.openSync = function(path, flags, mode) {
callback && callback(err, bytesRead || 0, buffer);
}
- binding.read(fd, buffer, offset, length, position, wrapper);
+ var req = new FSReqWrap();
+ req.oncomplete = wrapper;
+
+ binding.read(fd, buffer, offset, length, position, req);
};
fs.readSync = function(fd, buffer, offset, length, position) {
// OR
// fs.write(fd, string[, position[, encoding]], callback);
fs.write = function(fd, buffer, offset, length, position, callback) {
+ function strWrapper(err, written) {
+ // Retain a reference to buffer so that it can't be GC'ed too soon.
+ callback(err, written || 0, buffer);
+ }
+
+ function bufWrapper(err, written) {
+ // retain reference to string in case it's external
+ callback(err, written || 0, buffer);
+ }
+
if (util.isBuffer(buffer)) {
// if no position is passed then assume null
if (util.isFunction(position)) {
position = null;
}
callback = maybeCallback(callback);
- var wrapper = function(err, written) {
- // Retain a reference to buffer so that it can't be GC'ed too soon.
- callback(err, written || 0, buffer);
- };
- return binding.writeBuffer(fd, buffer, offset, length, position, wrapper);
+ var req = new FSReqWrap();
+ req.oncomplete = strWrapper;
+ return binding.writeBuffer(fd, buffer, offset, length, position, req);
}
if (util.isString(buffer))
length = 'utf8';
}
callback = maybeCallback(position);
- position = function(err, written) {
- // retain reference to string in case it's external
- callback(err, written || 0, buffer);
- };
- return binding.writeString(fd, buffer, offset, length, position);
+ var req = new FSReqWrap();
+ req.oncomplete = bufWrapper;
+ return binding.writeString(fd, buffer, offset, length, req);
};
// usage:
callback = makeCallback(callback);
if (!nullCheck(oldPath, callback)) return;
if (!nullCheck(newPath, callback)) return;
+ var req = new FSReqWrap();
+ req.oncomplete = callback;
binding.rename(pathModule._makeLong(oldPath),
pathModule._makeLong(newPath),
- callback);
+ req);
};
fs.renameSync = function(oldPath, newPath) {
fs.truncate = function(path, len, callback) {
if (util.isNumber(path)) {
- // legacy
- return fs.ftruncate(path, len, callback);
+ var req = new FSReqWrap();
+ req.oncomplete = callback;
+ return fs.ftruncate(path, len, req);
}
if (util.isFunction(len)) {
callback = len;
} else if (util.isUndefined(len)) {
len = 0;
}
+
callback = maybeCallback(callback);
fs.open(path, 'r+', function(er, fd) {
if (er) return callback(er);
- binding.ftruncate(fd, len, function(er) {
+ var req = new FSReqWrap();
+ req.oncomplete = function ftruncateCb(er) {
fs.close(fd, function(er2) {
callback(er || er2);
});
- });
+ };
+ binding.ftruncate(fd, len, req);
});
};
} else if (util.isUndefined(len)) {
len = 0;
}
- binding.ftruncate(fd, len, makeCallback(callback));
+ var req = new FSReqWrap();
+ req.oncomplete = makeCallback(callback);
+ binding.ftruncate(fd, len, req);
};
fs.ftruncateSync = function(fd, len) {
};
fs.rmdir = function(path, callback) {
- callback = makeCallback(callback);
+ callback = maybeCallback(callback);
if (!nullCheck(path, callback)) return;
- binding.rmdir(pathModule._makeLong(path), callback);
+ var req = new FSReqWrap();
+ req.oncomplete = callback;
+ binding.rmdir(pathModule._makeLong(path), req);
};
fs.rmdirSync = function(path) {
};
fs.fdatasync = function(fd, callback) {
- binding.fdatasync(fd, makeCallback(callback));
+ var req = new FSReqWrap();
+ req.oncomplete = makeCallback(callback);
+ binding.fdatasync(fd, req);
};
fs.fdatasyncSync = function(fd) {
};
fs.fsync = function(fd, callback) {
- binding.fsync(fd, makeCallback(callback));
+ var req = new FSReqWrap();
+ req.oncomplete = makeCallback(callback);
+ binding.fsync(fd, req);
};
fs.fsyncSync = function(fd) {
if (util.isFunction(mode)) callback = mode;
callback = makeCallback(callback);
if (!nullCheck(path, callback)) return;
+ var req = new FSReqWrap();
+ req.oncomplete = callback;
binding.mkdir(pathModule._makeLong(path),
modeNum(mode, 511 /*=0777*/),
- callback);
+ req);
};
fs.mkdirSync = function(path, mode) {
fs.readdir = function(path, callback) {
callback = makeCallback(callback);
if (!nullCheck(path, callback)) return;
- binding.readdir(pathModule._makeLong(path), callback);
+ var req = new FSReqWrap();
+ req.oncomplete = callback;
+ binding.readdir(pathModule._makeLong(path), req);
};
fs.readdirSync = function(path) {
};
fs.fstat = function(fd, callback) {
- binding.fstat(fd, makeCallback(callback));
+ var req = new FSReqWrap();
+ req.oncomplete = makeCallback(callback);
+ binding.fstat(fd, req);
};
fs.lstat = function(path, callback) {
callback = makeCallback(callback);
if (!nullCheck(path, callback)) return;
- binding.lstat(pathModule._makeLong(path), callback);
+ var req = new FSReqWrap();
+ req.oncomplete = callback;
+ binding.lstat(pathModule._makeLong(path), req);
};
fs.stat = function(path, callback) {
callback = makeCallback(callback);
if (!nullCheck(path, callback)) return;
- binding.stat(pathModule._makeLong(path), callback);
+ var req = new FSReqWrap();
+ req.oncomplete = callback;
+ binding.stat(pathModule._makeLong(path), req);
};
fs.fstatSync = function(fd) {
fs.readlink = function(path, callback) {
callback = makeCallback(callback);
if (!nullCheck(path, callback)) return;
- binding.readlink(pathModule._makeLong(path), callback);
+ var req = new FSReqWrap();
+ req.oncomplete = callback;
+ binding.readlink(pathModule._makeLong(path), req);
};
fs.readlinkSync = function(path) {
if (!nullCheck(destination, callback)) return;
if (!nullCheck(path, callback)) return;
+ var req = new FSReqWrap();
+ req.oncomplete = callback;
+
binding.symlink(preprocessSymlinkDestination(destination, type, path),
pathModule._makeLong(path),
type,
- callback);
+ req);
};
fs.symlinkSync = function(destination, path, type) {
if (!nullCheck(srcpath, callback)) return;
if (!nullCheck(dstpath, callback)) return;
+ var req = new FSReqWrap();
+ req.oncomplete = callback;
+
binding.link(pathModule._makeLong(srcpath),
pathModule._makeLong(dstpath),
- callback);
+ req);
};
fs.linkSync = function(srcpath, dstpath) {
fs.unlink = function(path, callback) {
callback = makeCallback(callback);
if (!nullCheck(path, callback)) return;
- binding.unlink(pathModule._makeLong(path), callback);
+ var req = new FSReqWrap();
+ req.oncomplete = callback;
+ binding.unlink(pathModule._makeLong(path), req);
};
fs.unlinkSync = function(path) {
};
fs.fchmod = function(fd, mode, callback) {
- binding.fchmod(fd, modeNum(mode), makeCallback(callback));
+ var req = new FSReqWrap();
+ req.oncomplete = makeCallback(callback);
+ binding.fchmod(fd, modeNum(mode), req);
};
fs.fchmodSync = function(fd, mode) {
fs.chmod = function(path, mode, callback) {
callback = makeCallback(callback);
if (!nullCheck(path, callback)) return;
+ var req = new FSReqWrap();
+ req.oncomplete = callback;
binding.chmod(pathModule._makeLong(path),
modeNum(mode),
- callback);
+ req);
};
fs.chmodSync = function(path, mode) {
}
fs.fchown = function(fd, uid, gid, callback) {
- binding.fchown(fd, uid, gid, makeCallback(callback));
+ var req = new FSReqWrap();
+ req.oncomplete = makeCallback(callback);
+ binding.fchown(fd, uid, gid, req);
};
fs.fchownSync = function(fd, uid, gid) {
fs.chown = function(path, uid, gid, callback) {
callback = makeCallback(callback);
if (!nullCheck(path, callback)) return;
- binding.chown(pathModule._makeLong(path), uid, gid, callback);
+ var req = new FSReqWrap();
+ req.oncomplete = callback;
+ binding.chown(pathModule._makeLong(path), uid, gid, req);
};
fs.chownSync = function(path, uid, gid) {
fs.utimes = function(path, atime, mtime, callback) {
callback = makeCallback(callback);
if (!nullCheck(path, callback)) return;
+ var req = new FSReqWrap();
+ req.oncomplete = callback;
binding.utimes(pathModule._makeLong(path),
toUnixTimestamp(atime),
toUnixTimestamp(mtime),
- callback);
+ req);
};
fs.utimesSync = function(path, atime, mtime) {
fs.futimes = function(fd, atime, mtime, callback) {
atime = toUnixTimestamp(atime);
mtime = toUnixTimestamp(mtime);
- binding.futimes(fd, atime, mtime, makeCallback(callback));
+ var req = new FSReqWrap();
+ req.oncomplete = makeCallback(callback);
+ binding.futimes(fd, atime, mtime, req);
};
fs.futimesSync = function(fd, atime, mtime) {
var uv = process.binding('uv');
var Pipe = process.binding('pipe_wrap').Pipe;
+var TCPConnectWrap = process.binding('tcp_wrap').TCPConnectWrap;
+var PipeConnectWrap = process.binding('pipe_wrap').PipeConnectWrap;
+var ShutdownWrap = process.binding('stream_wrap').ShutdownWrap;
+var WriteWrap = process.binding('stream_wrap').WriteWrap;
+
var cluster;
var errnoException = util._errnoException;
if (!this._handle || !this._handle.shutdown)
return this.destroy();
- var req = { oncomplete: afterShutdown };
+ var req = new ShutdownWrap();
+ req.oncomplete = afterShutdown;
var err = this._handle.shutdown(req);
if (err)
return false;
}
- var req = { oncomplete: afterWrite, async: false };
+ var req = new WriteWrap();
+ req.oncomplete = afterWrite;
+ req.async = false;
var err;
if (writev) {
}
}
- var req = {
- oncomplete: afterConnect,
- port: undefined,
- address: undefined,
- localAddress: undefined,
- localPort: undefined
- };
if (addressType === 6 || addressType === 4) {
+ var req = new TCPConnectWrap();
+ req.oncomplete = afterConnect;
port = port | 0;
if (port <= 0 || port > 65535)
throw new RangeError('Port should be > 0 and < 65536');
err = self._handle.connect(req, address, port);
}
} else {
+ var req = new PipeConnectWrap();
req.address = address;
+ req.oncomplete = afterConnect;
err = self._handle.connect(req, address, afterConnect);
}
class AsyncWrap : public BaseObject {
public:
enum ProviderType {
- PROVIDER_NONE = 1 << 0,
- PROVIDER_CARES = 1 << 1,
- PROVIDER_CONNECTWRAP = 1 << 2,
- PROVIDER_CRYPTO = 1 << 3,
- PROVIDER_FSEVENTWRAP = 1 << 4,
- PROVIDER_GETADDRINFOREQWRAP = 1 << 5,
- PROVIDER_PIPEWRAP = 1 << 6,
- PROVIDER_PROCESSWRAP = 1 << 7,
- PROVIDER_REQWRAP = 1 << 8,
- PROVIDER_SHUTDOWNWRAP = 1 << 9,
- PROVIDER_SIGNALWRAP = 1 << 10,
- PROVIDER_STATWATCHER = 1 << 11,
- PROVIDER_TCPWRAP = 1 << 12,
- PROVIDER_TIMERWRAP = 1 << 13,
- PROVIDER_TLSWRAP = 1 << 14,
- PROVIDER_TTYWRAP = 1 << 15,
- PROVIDER_UDPWRAP = 1 << 16,
- PROVIDER_ZLIB = 1 << 17,
- PROVIDER_GETNAMEINFOREQWRAP = 1 << 18
+ PROVIDER_NONE,
+ PROVIDER_CARES,
+ PROVIDER_CONNECTWRAP,
+ PROVIDER_CRYPTO,
+ PROVIDER_FSEVENTWRAP,
+ PROVIDER_FSREQWRAP,
+ PROVIDER_GETADDRINFOREQWRAP,
+ PROVIDER_GETNAMEINFOREQWRAP,
+ PROVIDER_PIPEWRAP,
+ PROVIDER_PROCESSWRAP,
+ PROVIDER_QUERYWRAP,
+ PROVIDER_REQWRAP,
+ PROVIDER_SHUTDOWNWRAP,
+ PROVIDER_SIGNALWRAP,
+ PROVIDER_STATWATCHER,
+ PROVIDER_TCPWRAP,
+ PROVIDER_TIMERWRAP,
+ PROVIDER_TLSWRAP,
+ PROVIDER_TTYWRAP,
+ PROVIDER_UDPWRAP,
+ PROVIDER_WRITEWRAP,
+ PROVIDER_ZLIB
};
inline AsyncWrap(Environment* env,
using v8::EscapableHandleScope;
using v8::Function;
using v8::FunctionCallbackInfo;
+using v8::FunctionTemplate;
using v8::Handle;
using v8::HandleScope;
using v8::Integer;
using v8::String;
using v8::Value;
-typedef class ReqWrap<uv_getaddrinfo_t> GetAddrInfoReqWrap;
-typedef class ReqWrap<uv_getnameinfo_t> GetNameInfoReqWrap;
+
+class GetAddrInfoReqWrap : public ReqWrap<uv_getaddrinfo_t> {
+ public:
+ GetAddrInfoReqWrap(Environment* env, Local<Object> req_wrap_obj);
+};
+
+GetAddrInfoReqWrap::GetAddrInfoReqWrap(Environment* env,
+ Local<Object> req_wrap_obj)
+ : ReqWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_GETADDRINFOREQWRAP) {
+ Wrap<GetAddrInfoReqWrap>(req_wrap_obj, this);
+}
+
+
+static void NewGetAddrInfoReqWrap(const FunctionCallbackInfo<Value>& args) {
+ CHECK(args.IsConstructCall());
+}
+
+
+class GetNameInfoReqWrap : public ReqWrap<uv_getnameinfo_t> {
+ public:
+ GetNameInfoReqWrap(Environment* env, Local<Object> req_wrap_obj);
+};
+
+GetNameInfoReqWrap::GetNameInfoReqWrap(Environment* env,
+ Local<Object> req_wrap_obj)
+ : ReqWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_GETNAMEINFOREQWRAP) {
+ Wrap<GetNameInfoReqWrap>(req_wrap_obj, this);
+}
+
+
+static void NewGetNameInfoReqWrap(const FunctionCallbackInfo<Value>& args) {
+ CHECK(args.IsConstructCall());
+}
static int cmp_ares_tasks(const ares_task_t* a, const ares_task_t* b) {
class QueryWrap : public AsyncWrap {
public:
QueryWrap(Environment* env, Local<Object> req_wrap_obj)
- : AsyncWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_CARES) {
+ : AsyncWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_QUERYWRAP) {
if (env->in_domain())
req_wrap_obj->Set(env->domain_string(), env->domain_array()->Get(0));
}
abort();
}
- GetAddrInfoReqWrap* req_wrap =
- new GetAddrInfoReqWrap(env,
- req_wrap_obj,
- AsyncWrap::PROVIDER_GETADDRINFOREQWRAP);
+ GetAddrInfoReqWrap* req_wrap = new GetAddrInfoReqWrap(env, req_wrap_obj);
struct addrinfo hints;
memset(&hints, 0, sizeof(struct addrinfo));
CHECK(uv_ip4_addr(*ip, port, reinterpret_cast<sockaddr_in*>(&addr)) == 0 ||
uv_ip6_addr(*ip, port, reinterpret_cast<sockaddr_in6*>(&addr)) == 0);
- GetNameInfoReqWrap* req_wrap =
- new GetNameInfoReqWrap(env,
- req_wrap_obj,
- AsyncWrap::PROVIDER_GETNAMEINFOREQWRAP);
+ GetNameInfoReqWrap* req_wrap = new GetNameInfoReqWrap(env, req_wrap_obj);
int err = uv_getnameinfo(env->event_loop(),
&req_wrap->req_,
Integer::New(env->isolate(), AI_ADDRCONFIG));
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "AI_V4MAPPED"),
Integer::New(env->isolate(), AI_V4MAPPED));
+
+ Local<FunctionTemplate> aiw =
+ FunctionTemplate::New(env->isolate(), NewGetAddrInfoReqWrap);
+ aiw->InstanceTemplate()->SetInternalFieldCount(1);
+ aiw->SetClassName(
+ FIXED_ONE_BYTE_STRING(env->isolate(), "GetAddrInfoReqWrap"));
+ target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "GetAddrInfoReqWrap"),
+ aiw->GetFunction());
+
+ Local<FunctionTemplate> niw =
+ FunctionTemplate::New(env->isolate(), NewGetNameInfoReqWrap);
+ niw->InstanceTemplate()->SetInternalFieldCount(1);
+ niw->SetClassName(
+ FIXED_ONE_BYTE_STRING(env->isolate(), "GetNameInfoReqWrap"));
+ target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "GetNameInfoReqWrap"),
+ niw->GetFunction());
}
} // namespace cares_wrap
void* operator new(size_t size) { return new char[size]; }
void* operator new(size_t size, char* storage) { return storage; }
- FSReqWrap(Environment* env, const char* syscall, char* data = nullptr)
- : ReqWrap<uv_fs_t>(env, Object::New(env->isolate())),
+ FSReqWrap(Environment* env,
+ Local<Object> req,
+ const char* syscall,
+ char* data = nullptr)
+ : ReqWrap(env, req, AsyncWrap::PROVIDER_FSREQWRAP),
syscall_(syscall),
data_(data),
dest_len_(0) {
+ Wrap<FSReqWrap>(object(), this);
}
void ReleaseEarly() {
};
+static void NewFSReqWrap(const FunctionCallbackInfo<Value>& args) {
+ CHECK(args.IsConstructCall());
+}
+
+
#define ASSERT_OFFSET(a) \
if (!(a)->IsUndefined() && !(a)->IsNull() && !IsInt64((a)->NumberValue())) { \
return env->ThrowTypeError("Not an integer"); \
};
-#define ASYNC_DEST_CALL(func, callback, dest_path, ...) \
+#define ASYNC_DEST_CALL(func, req, dest_path, ...) \
Environment* env = Environment::GetCurrent(args); \
FSReqWrap* req_wrap; \
char* dest_str = (dest_path); \
int dest_len = dest_str == nullptr ? 0 : strlen(dest_str); \
char* storage = new char[sizeof(*req_wrap) + dest_len]; \
- req_wrap = new(storage) FSReqWrap(env, #func); \
+ CHECK(req->IsObject()); \
+ req_wrap = new(storage) FSReqWrap(env, req.As<Object>(), #func); \
req_wrap->dest_len(dest_len); \
if (dest_str != nullptr) { \
memcpy(const_cast<char*>(req_wrap->dest()), \
dest_str, \
dest_len + 1); \
} \
- int err = uv_fs_ ## func(env->event_loop() , \
+ int err = uv_fs_ ## func(env->event_loop(), \
&req_wrap->req_, \
__VA_ARGS__, \
After); \
- req_wrap->object()->Set(env->oncomplete_string(), callback); \
req_wrap->Dispatched(); \
if (err < 0) { \
- uv_fs_t* req = &req_wrap->req_; \
- req->result = err; \
- req->path = nullptr; \
- After(req); \
+ uv_fs_t* uv_req = &req_wrap->req_; \
+ uv_req->result = err; \
+ uv_req->path = nullptr; \
+ After(uv_req); \
} \
args.GetReturnValue().Set(req_wrap->persistent());
-#define ASYNC_CALL(func, callback, ...) \
- ASYNC_DEST_CALL(func, callback, nullptr, __VA_ARGS__) \
+#define ASYNC_CALL(func, req, ...) \
+ ASYNC_DEST_CALL(func, req, nullptr, __VA_ARGS__) \
#define SYNC_DEST_CALL(func, path, dest, ...) \
fs_req_wrap req_wrap; \
int fd = args[0]->Int32Value();
- if (args[1]->IsFunction()) {
+ if (args[1]->IsObject()) {
ASYNC_CALL(close, args[1], fd)
} else {
SYNC_CALL(close, 0, fd)
node::Utf8Value path(args[0]);
- if (args[1]->IsFunction()) {
+ if (args[1]->IsObject()) {
ASYNC_CALL(stat, args[1], *path)
} else {
SYNC_CALL(stat, *path, *path)
node::Utf8Value path(args[0]);
- if (args[1]->IsFunction()) {
+ if (args[1]->IsObject()) {
ASYNC_CALL(lstat, args[1], *path)
} else {
SYNC_CALL(lstat, *path, *path)
int fd = args[0]->Int32Value();
- if (args[1]->IsFunction()) {
+ if (args[1]->IsObject()) {
ASYNC_CALL(fstat, args[1], fd)
} else {
SYNC_CALL(fstat, 0, fd)
}
}
- if (args[3]->IsFunction()) {
+ if (args[3]->IsObject()) {
ASYNC_DEST_CALL(symlink, args[3], *path, *dest, *path, flags)
} else {
SYNC_DEST_CALL(symlink, *dest, *path, *dest, *path, flags)
node::Utf8Value orig_path(args[0]);
node::Utf8Value new_path(args[1]);
- if (args[2]->IsFunction()) {
+ if (args[2]->IsObject()) {
ASYNC_DEST_CALL(link, args[2], *new_path, *orig_path, *new_path)
} else {
SYNC_DEST_CALL(link, *orig_path, *new_path, *orig_path, *new_path)
node::Utf8Value path(args[0]);
- if (args[1]->IsFunction()) {
+ if (args[1]->IsObject()) {
ASYNC_CALL(readlink, args[1], *path)
} else {
SYNC_CALL(readlink, *path, *path)
node::Utf8Value old_path(args[0]);
node::Utf8Value new_path(args[1]);
- if (args[2]->IsFunction()) {
+ if (args[2]->IsObject()) {
ASYNC_DEST_CALL(rename, args[2], *new_path, *old_path, *new_path)
} else {
SYNC_DEST_CALL(rename, *old_path, *new_path, *old_path, *new_path)
ASSERT_TRUNCATE_LENGTH(args[1]);
int64_t len = GET_TRUNCATE_LENGTH(args[1]);
- if (args[2]->IsFunction()) {
+ if (args[2]->IsObject()) {
ASYNC_CALL(ftruncate, args[2], fd, len)
} else {
SYNC_CALL(ftruncate, 0, fd, len)
int fd = args[0]->Int32Value();
- if (args[1]->IsFunction()) {
+ if (args[1]->IsObject()) {
ASYNC_CALL(fdatasync, args[1], fd)
} else {
SYNC_CALL(fdatasync, 0, fd)
int fd = args[0]->Int32Value();
- if (args[1]->IsFunction()) {
+ if (args[1]->IsObject()) {
ASYNC_CALL(fsync, args[1], fd)
} else {
SYNC_CALL(fsync, 0, fd)
node::Utf8Value path(args[0]);
- if (args[1]->IsFunction()) {
+ if (args[1]->IsObject()) {
ASYNC_CALL(unlink, args[1], *path)
} else {
SYNC_CALL(unlink, *path, *path)
node::Utf8Value path(args[0]);
- if (args[1]->IsFunction()) {
+ if (args[1]->IsObject()) {
ASYNC_CALL(rmdir, args[1], *path)
} else {
SYNC_CALL(rmdir, *path, *path)
node::Utf8Value path(args[0]);
int mode = static_cast<int>(args[1]->Int32Value());
- if (args[2]->IsFunction()) {
+ if (args[2]->IsObject()) {
ASYNC_CALL(mkdir, args[2], *path, mode)
} else {
SYNC_CALL(mkdir, *path, *path, mode)
node::Utf8Value path(args[0]);
- if (args[1]->IsFunction()) {
+ if (args[1]->IsObject()) {
ASYNC_CALL(scandir, args[1], *path, 0 /*flags*/)
} else {
SYNC_CALL(scandir, *path, *path, 0 /*flags*/)
int flags = args[1]->Int32Value();
int mode = static_cast<int>(args[2]->Int32Value());
- if (args[3]->IsFunction()) {
+ if (args[3]->IsObject()) {
ASYNC_CALL(open, args[3], *path, flags, mode)
} else {
SYNC_CALL(open, *path, *path, flags, mode)
size_t off = args[2]->Uint32Value();
size_t len = args[3]->Uint32Value();
int64_t pos = GET_OFFSET(args[4]);
- Local<Value> cb = args[5];
+ Local<Value> req = args[5];
if (off > buffer_length)
return env->ThrowRangeError("offset out of bounds");
uv_buf_t uvbuf = uv_buf_init(const_cast<char*>(buf), len);
- if (cb->IsFunction()) {
- ASYNC_CALL(write, cb, fd, &uvbuf, 1, pos)
+ if (req->IsObject()) {
+ ASYNC_CALL(write, req, fd, &uvbuf, 1, pos)
return;
}
if (!args[0]->IsInt32())
return env->ThrowTypeError("First argument must be file descriptor");
- Local<Value> cb;
+ Local<Value> req;
Local<Value> string = args[1];
int fd = args[0]->Int32Value();
char* buf = nullptr;
must_free = true;
}
pos = GET_OFFSET(args[2]);
- cb = args[4];
+ req = args[4];
uv_buf_t uvbuf = uv_buf_init(const_cast<char*>(buf), len);
- if (!cb->IsFunction()) {
+ if (!req->IsObject()) {
SYNC_CALL(write, nullptr, fd, &uvbuf, 1, pos)
if (must_free)
delete[] buf;
return args.GetReturnValue().Set(SYNC_RESULT);
}
- FSReqWrap* req_wrap = new FSReqWrap(env, "write", must_free ? buf : nullptr);
+ FSReqWrap* req_wrap =
+ new FSReqWrap(env, req.As<Object>(), "write", must_free ? buf : nullptr);
int err = uv_fs_write(env->event_loop(),
&req_wrap->req_,
fd,
1,
pos,
After);
- req_wrap->object()->Set(env->oncomplete_string(), cb);
req_wrap->Dispatched();
if (err < 0) {
- uv_fs_t* req = &req_wrap->req_;
- req->result = err;
- req->path = nullptr;
- After(req);
+ uv_fs_t* uv_req = &req_wrap->req_;
+ uv_req->result = err;
+ uv_req->path = nullptr;
+ After(uv_req);
}
return args.GetReturnValue().Set(req_wrap->persistent());
int fd = args[0]->Int32Value();
- Local<Value> cb;
+ Local<Value> req;
size_t len;
int64_t pos;
uv_buf_t uvbuf = uv_buf_init(const_cast<char*>(buf), len);
- cb = args[5];
+ req = args[5];
- if (cb->IsFunction()) {
- ASYNC_CALL(read, cb, fd, &uvbuf, 1, pos);
+ if (req->IsObject()) {
+ ASYNC_CALL(read, req, fd, &uvbuf, 1, pos);
} else {
SYNC_CALL(read, 0, fd, &uvbuf, 1, pos)
args.GetReturnValue().Set(SYNC_RESULT);
node::Utf8Value path(args[0]);
int mode = static_cast<int>(args[1]->Int32Value());
- if (args[2]->IsFunction()) {
+ if (args[2]->IsObject()) {
ASYNC_CALL(chmod, args[2], *path, mode);
} else {
SYNC_CALL(chmod, *path, *path, mode);
int fd = args[0]->Int32Value();
int mode = static_cast<int>(args[1]->Int32Value());
- if (args[2]->IsFunction()) {
+ if (args[2]->IsObject()) {
ASYNC_CALL(fchmod, args[2], fd, mode);
} else {
SYNC_CALL(fchmod, 0, fd, mode);
uv_uid_t uid = static_cast<uv_uid_t>(args[1]->Uint32Value());
uv_gid_t gid = static_cast<uv_gid_t>(args[2]->Uint32Value());
- if (args[3]->IsFunction()) {
+ if (args[3]->IsObject()) {
ASYNC_CALL(chown, args[3], *path, uid, gid);
} else {
SYNC_CALL(chown, *path, *path, uid, gid);
uv_uid_t uid = static_cast<uv_uid_t>(args[1]->Uint32Value());
uv_gid_t gid = static_cast<uv_gid_t>(args[2]->Uint32Value());
- if (args[3]->IsFunction()) {
+ if (args[3]->IsObject()) {
ASYNC_CALL(fchown, args[3], fd, uid, gid);
} else {
SYNC_CALL(fchown, 0, fd, uid, gid);
const double atime = static_cast<double>(args[1]->NumberValue());
const double mtime = static_cast<double>(args[2]->NumberValue());
- if (args[3]->IsFunction()) {
+ if (args[3]->IsObject()) {
ASYNC_CALL(utime, args[3], *path, atime, mtime);
} else {
SYNC_CALL(utime, *path, *path, atime, mtime);
const double atime = static_cast<double>(args[1]->NumberValue());
const double mtime = static_cast<double>(args[2]->NumberValue());
- if (args[3]->IsFunction()) {
+ if (args[3]->IsObject()) {
ASYNC_CALL(futime, args[3], fd, atime, mtime);
} else {
SYNC_CALL(futime, 0, fd, atime, mtime);
env->SetMethod(target, "futimes", FUTimes);
StatWatcher::Initialize(env, target);
+
+ // Create FunctionTemplate for FSReqWrap
+ Local<FunctionTemplate> fst =
+ FunctionTemplate::New(env->isolate(), NewFSReqWrap);
+ fst->InstanceTemplate()->SetInternalFieldCount(1);
+ fst->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "FSReqWrap"));
+ target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "FSReqWrap"),
+ fst->GetFunction());
}
} // end namespace node
using v8::Undefined;
using v8::Value;
+
// TODO(bnoordhuis) share with TCPWrap?
-typedef class ReqWrap<uv_connect_t> ConnectWrap;
+class PipeConnectWrap : public ReqWrap<uv_connect_t> {
+ public:
+ PipeConnectWrap(Environment* env, Local<Object> req_wrap_obj);
+};
+
+
+PipeConnectWrap::PipeConnectWrap(Environment* env, Local<Object> req_wrap_obj)
+ : ReqWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_PIPEWRAP) {
+ Wrap<PipeConnectWrap>(req_wrap_obj, this);
+}
+
+
+static void NewPipeConnectWrap(const FunctionCallbackInfo<Value>& args) {
+ CHECK(args.IsConstructCall());
+}
uv_pipe_t* PipeWrap::UVHandle() {
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Pipe"), t->GetFunction());
env->set_pipe_constructor_template(t);
+
+ // Create FunctionTemplate for PipeConnectWrap.
+ Local<FunctionTemplate> cwt =
+ FunctionTemplate::New(env->isolate(), NewPipeConnectWrap);
+ cwt->InstanceTemplate()->SetInternalFieldCount(1);
+ cwt->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "PipeConnectWrap"));
+ target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "PipeConnectWrap"),
+ cwt->GetFunction());
}
// TODO(bnoordhuis) Maybe share this with TCPWrap?
void PipeWrap::AfterConnect(uv_connect_t* req, int status) {
- ConnectWrap* req_wrap = static_cast<ConnectWrap*>(req->data);
+ PipeConnectWrap* req_wrap = static_cast<PipeConnectWrap*>(req->data);
PipeWrap* wrap = static_cast<PipeWrap*>(req->handle->data);
CHECK_EQ(req_wrap->env(), wrap->env());
Environment* env = wrap->env();
Local<Object> req_wrap_obj = args[0].As<Object>();
node::Utf8Value name(args[1]);
- ConnectWrap* req_wrap = new ConnectWrap(env,
- req_wrap_obj,
- AsyncWrap::PROVIDER_CONNECTWRAP);
+ PipeConnectWrap* req_wrap = new PipeConnectWrap(env, req_wrap_obj);
uv_pipe_connect(&req_wrap->req_,
&wrap->handle_,
*name,
public:
ReqWrap(Environment* env,
v8::Handle<v8::Object> object,
- AsyncWrap::ProviderType provider = AsyncWrap::PROVIDER_REQWRAP)
- : AsyncWrap(env, object, AsyncWrap::PROVIDER_REQWRAP) {
+ AsyncWrap::ProviderType provider)
+ : AsyncWrap(env, object, provider) {
if (env->in_domain())
object->Set(env->domain_string(), env->domain_array()->Get(0));
using v8::Context;
using v8::EscapableHandleScope;
using v8::FunctionCallbackInfo;
+using v8::FunctionTemplate;
using v8::Handle;
using v8::HandleScope;
using v8::Integer;
using v8::Value;
+void StreamWrap::Initialize(Handle<Object> target,
+ Handle<Value> unused,
+ Handle<Context> context) {
+ Environment* env = Environment::GetCurrent(context);
+
+ Local<FunctionTemplate> sw =
+ FunctionTemplate::New(env->isolate(), ShutdownWrap::NewShutdownWrap);
+ sw->InstanceTemplate()->SetInternalFieldCount(1);
+ sw->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "ShutdownWrap"));
+ target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "ShutdownWrap"),
+ sw->GetFunction());
+
+ Local<FunctionTemplate> ww =
+ FunctionTemplate::New(env->isolate(), WriteWrap::NewWriteWrap);
+ ww->InstanceTemplate()->SetInternalFieldCount(1);
+ ww->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "WriteWrap"));
+ target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "WriteWrap"),
+ ww->GetFunction());
+}
+
+
StreamWrap::StreamWrap(Environment* env,
Local<Object> object,
uv_stream_t* stream,
object()->Set(env()->write_queue_size_string(), write_queue_size);
}
+
void StreamWrap::ReadStart(const FunctionCallbackInfo<Value>& args) {
StreamWrap* wrap = Unwrap<StreamWrap>(args.Holder());
if (!IsAlive(wrap))
CHECK(args[0]->IsObject());
Local<Object> req_wrap_obj = args[0].As<Object>();
- ShutdownWrap* req_wrap = new ShutdownWrap(env,
- req_wrap_obj,
- AsyncWrap::PROVIDER_SHUTDOWNWRAP);
+ ShutdownWrap* req_wrap = new ShutdownWrap(env, req_wrap_obj);
int err = wrap->callbacks()->DoShutdown(req_wrap, AfterShutdown);
req_wrap->Dispatched();
if (err)
}
} // namespace node
+
+NODE_MODULE_CONTEXT_AWARE_BUILTIN(stream_wrap, node::StreamWrap::Initialize)
// Forward declaration
class StreamWrap;
-typedef class ReqWrap<uv_shutdown_t> ShutdownWrap;
+class ShutdownWrap : public ReqWrap<uv_shutdown_t> {
+ public:
+ ShutdownWrap(Environment* env, v8::Local<v8::Object> req_wrap_obj)
+ : ReqWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_SHUTDOWNWRAP) {
+ Wrap<ShutdownWrap>(req_wrap_obj, this);
+ }
+
+ static void NewShutdownWrap(const v8::FunctionCallbackInfo<v8::Value>& args) {
+ CHECK(args.IsConstructCall());
+ }
+};
class WriteWrap: public ReqWrap<uv_write_t> {
public:
// TODO(trevnorris): WrapWrap inherits from ReqWrap, which I've globbed
// into the same provider. How should these be broken apart?
WriteWrap(Environment* env, v8::Local<v8::Object> obj, StreamWrap* wrap)
- : ReqWrap<uv_write_t>(env, obj),
+ : ReqWrap(env, obj, AsyncWrap::PROVIDER_WRITEWRAP),
wrap_(wrap) {
+ Wrap<WriteWrap>(obj, this);
}
void* operator new(size_t size, char* storage) { return storage; }
return wrap_;
}
+ static void NewWriteWrap(const v8::FunctionCallbackInfo<v8::Value>& args) {
+ CHECK(args.IsConstructCall());
+ }
+
private:
// People should not be using the non-placement new and delete operator on a
// WriteWrap. Ensure this never happens.
class StreamWrap : public HandleWrap {
public:
+ static void Initialize(v8::Handle<v8::Object> target,
+ v8::Handle<v8::Value> unused,
+ v8::Handle<v8::Context> context);
+
void OverrideCallbacks(StreamWrapCallbacks* callbacks, bool gc) {
StreamWrapCallbacks* old = callbacks_;
callbacks_ = callbacks;
using v8::Undefined;
using v8::Value;
-typedef class ReqWrap<uv_connect_t> ConnectWrap;
+
+class TCPConnectWrap : public ReqWrap<uv_connect_t> {
+ public:
+ TCPConnectWrap(Environment* env, Local<Object> req_wrap_obj);
+};
+
+
+TCPConnectWrap::TCPConnectWrap(Environment* env, Local<Object> req_wrap_obj)
+ : ReqWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_TCPWRAP) {
+ Wrap<TCPConnectWrap>(req_wrap_obj, this);
+}
+
+
+static void NewTCPConnectWrap(const FunctionCallbackInfo<Value>& args) {
+ CHECK(args.IsConstructCall());
+}
Local<Object> TCPWrap::Instantiate(Environment* env) {
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "TCP"), t->GetFunction());
env->set_tcp_constructor_template(t);
+
+ // Create FunctionTemplate for TCPConnectWrap.
+ Local<FunctionTemplate> cwt =
+ FunctionTemplate::New(env->isolate(), NewTCPConnectWrap);
+ cwt->InstanceTemplate()->SetInternalFieldCount(1);
+ cwt->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "TCPConnectWrap"));
+ target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "TCPConnectWrap"),
+ cwt->GetFunction());
}
void TCPWrap::AfterConnect(uv_connect_t* req, int status) {
- ConnectWrap* req_wrap = static_cast<ConnectWrap*>(req->data);
+ TCPConnectWrap* req_wrap = static_cast<TCPConnectWrap*>(req->data);
TCPWrap* wrap = static_cast<TCPWrap*>(req->handle->data);
CHECK_EQ(req_wrap->env(), wrap->env());
Environment* env = wrap->env();
int err = uv_ip4_addr(*ip_address, port, &addr);
if (err == 0) {
- ConnectWrap* req_wrap = new ConnectWrap(env,
- req_wrap_obj,
- AsyncWrap::PROVIDER_CONNECTWRAP);
+ TCPConnectWrap* req_wrap = new TCPConnectWrap(env, req_wrap_obj);
err = uv_tcp_connect(&req_wrap->req_,
&wrap->handle_,
reinterpret_cast<const sockaddr*>(&addr),
int err = uv_ip6_addr(*ip_address, port, &addr);
if (err == 0) {
- ConnectWrap* req_wrap = new ConnectWrap(env,
- req_wrap_obj,
- AsyncWrap::PROVIDER_CONNECTWRAP);
+ TCPConnectWrap* req_wrap = new TCPConnectWrap(env, req_wrap_obj);
err = uv_tcp_connect(&req_wrap->req_,
&wrap->handle_,
reinterpret_cast<const sockaddr*>(&addr),
SendWrap::SendWrap(Environment* env,
Local<Object> req_wrap_obj,
bool have_callback)
- : ReqWrap<uv_udp_send_t>(env, req_wrap_obj),
+ : ReqWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_UDPWRAP),
have_callback_(have_callback) {
+ Wrap<SendWrap>(req_wrap_obj, this);
}
}
+static void NewSendWrap(const FunctionCallbackInfo<Value>& args) {
+ CHECK(args.IsConstructCall());
+}
+
+
UDPWrap::UDPWrap(Environment* env, Handle<Object> object)
: HandleWrap(env,
object,
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "UDP"), t->GetFunction());
env->set_udp_constructor_function(t->GetFunction());
+
+ // Create FunctionTemplate for SendWrap
+ Local<FunctionTemplate> swt =
+ FunctionTemplate::New(env->isolate(), NewSendWrap);
+ swt->InstanceTemplate()->SetInternalFieldCount(1);
+ swt->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "SendWrap"));
+ target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "SendWrap"),
+ swt->GetFunction());
}
console.log('looking up nodejs.org...');
-var req = {};
-var err = process.binding('cares_wrap').getaddrinfo(req, 'nodejs.org', 4);
+var cares = process.binding('cares_wrap');
+var req = new cares.GetAddrInfoReqWrap();
+var err = cares.getaddrinfo(req, 'nodejs.org', 4);
req.oncomplete = function(err, domains) {
assert.strictEqual(err, 0);
var common = require('../common');
var assert = require('assert');
var TCP = process.binding('tcp_wrap').TCP;
+var TCPConnectWrap = process.binding('tcp_wrap').TCPConnectWrap;
+var ShutdownWrap = process.binding('stream_wrap').ShutdownWrap;
function makeConnection() {
var client = new TCP();
- var req = {};
+ var req = new TCPConnectWrap();
var err = client.connect(req, '127.0.0.1', common.PORT);
assert.equal(err, 0);
assert.equal(req, req_);
console.log('connected');
- var shutdownReq = {};
+ var shutdownReq = new ShutdownWrap();
var err = client.shutdown(shutdownReq);
assert.equal(err, 0);
var assert = require('assert');
var TCP = process.binding('tcp_wrap').TCP;
+var WriteWrap = process.binding('stream_wrap').WriteWrap;
var server = new TCP();
assert.equal(0, client.writeQueueSize);
- var req = { async: false };
+ var req = new WriteWrap();
+ req.async = false;
var err = client.writeBuffer(req, buffer);
assert.equal(err, 0);
client.pendingWrites.push(req);