Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: regality/node
...
head fork: regality/node
  • 9 commits
  • 16 files changed
  • 0 commit comments
  • 1 contributor
40 lib/fs.js
View
@@ -134,7 +134,7 @@ function noop () {}
// list to make the arguments clear.
fs.close = function (fd, callback) {
- binding.close(fd, callback || noop);
+ return binding.close(fd, callback || noop);
};
fs.closeSync = function (fd) {
@@ -146,7 +146,7 @@ fs.open = function (path, flags, mode_, callback) {
var callback_ = arguments[arguments.length - 1];
var callback = (typeof(callback_) == 'function' ? callback_ : null);
- binding.open(path, stringToFlags(flags), mode, callback || noop);
+ return binding.open(path, stringToFlags(flags), mode, callback || noop);
};
fs.openSync = function (path, flags, mode) {
@@ -175,7 +175,7 @@ fs.read = function (fd, buffer, offset, length, position, callback) {
};
}
- binding.read(fd, buffer, offset, length, position, callback || noop);
+ return binding.read(fd, buffer, offset, length, position, callback || noop);
};
fs.readSync = function (fd, buffer, offset, length, position) {
@@ -222,7 +222,7 @@ fs.write = function (fd, buffer, offset, length, position, callback) {
return;
}
- binding.write(fd, buffer, offset, length, position, callback || noop);
+ return binding.write(fd, buffer, offset, length, position, callback || noop);
};
fs.writeSync = function (fd, buffer, offset, length, position) {
@@ -240,7 +240,7 @@ fs.writeSync = function (fd, buffer, offset, length, position) {
};
fs.rename = function (oldPath, newPath, callback) {
- binding.rename(oldPath, newPath, callback || noop);
+ return binding.rename(oldPath, newPath, callback || noop);
};
fs.renameSync = function (oldPath, newPath) {
@@ -248,7 +248,7 @@ fs.renameSync = function (oldPath, newPath) {
};
fs.truncate = function (fd, len, callback) {
- binding.truncate(fd, len, callback || noop);
+ return binding.truncate(fd, len, callback || noop);
};
fs.truncateSync = function (fd, len) {
@@ -256,7 +256,7 @@ fs.truncateSync = function (fd, len) {
};
fs.rmdir = function (path, callback) {
- binding.rmdir(path, callback || noop);
+ return binding.rmdir(path, callback || noop);
};
fs.rmdirSync = function (path) {
@@ -272,7 +272,7 @@ fs.fdatasyncSync = function (fd) {
};
fs.fsync = function (fd, callback) {
- binding.fsync(fd, callback || noop);
+ return binding.fsync(fd, callback || noop);
};
fs.fsyncSync = function (fd) {
@@ -288,7 +288,7 @@ fs.mkdirSync = function (path, mode) {
};
fs.sendfile = function (outFd, inFd, inOffset, length, callback) {
- binding.sendfile(outFd, inFd, inOffset, length, callback || noop);
+ return binding.sendfile(outFd, inFd, inOffset, length, callback || noop);
};
fs.sendfileSync = function (outFd, inFd, inOffset, length) {
@@ -296,7 +296,7 @@ fs.sendfileSync = function (outFd, inFd, inOffset, length) {
};
fs.readdir = function (path, callback) {
- binding.readdir(path, callback || noop);
+ return binding.readdir(path, callback || noop);
};
fs.readdirSync = function (path) {
@@ -304,15 +304,15 @@ fs.readdirSync = function (path) {
};
fs.fstat = function (fd, callback) {
- binding.fstat(fd, callback || noop);
+ return binding.fstat(fd, callback || noop);
};
fs.lstat = function (path, callback) {
- binding.lstat(path, callback || noop);
+ return binding.lstat(path, callback || noop);
};
fs.stat = function (path, callback) {
- binding.stat(path, callback || noop);
+ return binding.stat(path, callback || noop);
};
fs.fstatSync = function (fd) {
@@ -328,7 +328,7 @@ fs.statSync = function (path) {
};
fs.readlink = function (path, callback) {
- binding.readlink(path, callback || noop);
+ return binding.readlink(path, callback || noop);
};
fs.readlinkSync = function (path) {
@@ -336,7 +336,7 @@ fs.readlinkSync = function (path) {
};
fs.symlink = function (destination, path, callback) {
- binding.symlink(destination, path, callback || noop);
+ return binding.symlink(destination, path, callback || noop);
};
fs.symlinkSync = function (destination, path) {
@@ -344,7 +344,7 @@ fs.symlinkSync = function (destination, path) {
};
fs.link = function (srcpath, dstpath, callback) {
- binding.link(srcpath, dstpath, callback || noop);
+ return binding.link(srcpath, dstpath, callback || noop);
};
fs.linkSync = function (srcpath, dstpath) {
@@ -352,7 +352,7 @@ fs.linkSync = function (srcpath, dstpath) {
};
fs.unlink = function (path, callback) {
- binding.unlink(path, callback || noop);
+ return binding.unlink(path, callback || noop);
};
fs.unlinkSync = function (path) {
@@ -360,7 +360,7 @@ fs.unlinkSync = function (path) {
};
fs.chmod = function (path, mode, callback) {
- binding.chmod(path, mode, callback || noop);
+ return binding.chmod(path, mode, callback || noop);
};
fs.chmodSync = function (path, mode) {
@@ -368,7 +368,7 @@ fs.chmodSync = function (path, mode) {
};
fs.chown = function(path, uid, gid, callback) {
- binding.chown(path, uid, gid, callback || noop);
+ return binding.chown(path, uid, gid, callback || noop);
};
fs.chownSync = function(path, uid, gid) {
@@ -452,7 +452,7 @@ fs.watchFile = function (filename) {
stat = statWatchers[filename];
stat.start(filename, options.persistent, options.interval);
}
- stat.addListener("change", listener);
+ stat.callback = listener;
return stat;
};
7 src/node.cc
View
@@ -917,7 +917,7 @@ const char* ToCString(const v8::String::Utf8Value& value) {
return *value ? *value : "<str conversion failed>";
}
-static void ReportException(TryCatch &try_catch, bool show_line) {
+void ReportException(TryCatch &try_catch, bool show_line) {
Handle<Message> message = try_catch.Message();
node::Stdio::DisableRawMode(STDIN_FILENO);
@@ -1463,6 +1463,11 @@ static Handle<Value> Binding(const Arguments& args) {
Timer::Initialize(exports);
binding_cache->Set(module, exports);
+ } else if (!strcmp(*module_v, "signal_watcher")) {
+ exports = Object::New();
+ SignalWatcher::Initialize(exports);
+ binding_cache->Set(module, exports);
+
} else if (!strcmp(*module_v, "natives")) {
exports = Object::New();
// Explicitly define native sources.
4 src/node.h
View
@@ -36,7 +36,7 @@ namespace node {
do { \
v8::Local<v8::Signature> __callback##_SIG = v8::Signature::New(templ); \
v8::Local<v8::FunctionTemplate> __callback##_TEM = \
- FunctionTemplate::New(callback, v8::Handle<v8::Value>(), \
+ v8::FunctionTemplate::New(callback, v8::Handle<v8::Value>(), \
__callback##_SIG); \
templ->PrototypeTemplate()->Set(v8::String::NewSymbol(name), \
__callback##_TEM); \
@@ -87,6 +87,8 @@ v8::Local<v8::Value> ErrnoException(int errorno,
const char *msg = "",
const char *path = NULL);
+void ReportException(v8::TryCatch &try_catch, bool show_line);
+
const char *signo_string(int errorno);
struct node_module_struct {
5 src/node.js
View
@@ -110,7 +110,8 @@ var constants; // lazy loaded.
if (!signalWatchers.hasOwnProperty(type)) {
if (!constants) constants = process.binding("constants");
var b = process.binding('signal_watcher');
- var w = new b.SignalWatcher(constants[type]);
+ var w = new b.SignalWatcher();
+ w.set(constants[type]);
w.callback = function () { process.emit(type); };
signalWatchers[type] = w;
w.start();
@@ -721,7 +722,7 @@ var module = (function () {
exports.runMain = function () {
// Load the main module--the command line argument.
process.mainModule = new Module(".");
- process.mainModule.loadSync(process.argv[1]);
+ process.mainModule.load(process.argv[1]);
}
return exports;
142 src/node_events.cc
View
@@ -2,6 +2,8 @@
#include <node_events.h>
#include <node.h>
+#define RECORD 0
+
namespace node {
using namespace v8;
@@ -69,4 +71,144 @@ bool EventEmitter::Emit(Handle<String> event, int argc, Handle<Value> argv[]) {
return true;
}
+
+// EventSource
+
+EventSource* EventSource::current_source;
+
+
+Local<Value> EventSource::MakeCallback(int argc, Handle<Value> argv[]) {
+ HandleScope scope;
+
+ Local<Value> callback_v = handle_->Get(String::NewSymbol("callback"));
+ if (!callback_v->IsFunction()) return Local<Value>();
+ Local<Function> callback = Local<Function>::Cast(callback_v);
+
+ // TODO DTrace probe here.
+
+ TryCatch try_catch;
+
+ assert(current_source == NULL);
+ current_source = this;
+
+ Local<Value> ret = callback->Call(handle_, argc, argv);
+
+ current_source = NULL;
+
+ if (try_catch.HasCaught()) {
+ // TODO Look for 'uncaughtException' handlers.
+
+ // Here we print the stack trace from try_catch
+ ReportException(try_catch, true);
+ // Then we print the stored stacktrace plus our ancestors stacks.
+ PrintStack();
+ // XXX Stop whatever activity might have been going on?
+
+ exit(1);
+ }
+
+ // Call nextTick callbacks?
+
+ return scope.Close(ret);
+}
+
+
+void EventSource::PrintStack(int count) {
+ if (trace_.IsEmpty()) return;
+
+
+ // Print from for this EventSource
+ fprintf(stderr, " ---------------------------\n");
+ int length = trace_->GetFrameCount();
+ for (int i = 0; i < length; i++) {
+ Local<StackFrame> frame = trace_->GetFrame(i);
+
+ String::Utf8Value script_name(frame->GetScriptName());
+ String::Utf8Value function_name(frame->GetFunctionName());
+ int column = frame->GetColumn();
+ int line_number = frame->GetLineNumber();
+
+ // how do you cast Value to StackFrame ?
+ // print frame somehow...
+ /*
+ fprintf(stderr,
+ " at %s (%s:%d:%d)\n",
+ *function_name,
+ *script_name,
+ line_number,
+ column);
+ */
+ fprintf(stderr,
+ " at %s %d\n",
+ *script_name,
+ line_number);
+ }
+
+ // Recursively print up to kAncestorStackLimit ancestor traces...
+ if (parent_source_.IsEmpty() == false && count < kAncestorStackLimit) {
+ EventSource *parent = ObjectWrap::Unwrap<EventSource>(parent_source_);
+ parent->PrintStack(count + 1);
+ }
+}
+
+
+void EventSource::Active() {
+ Ref();
+ RecordStack();
+ // TODO DTrace probe here.
+}
+
+
+void EventSource::Inactive() {
+ DeleteParent();
+ ClearStack();
+ Unref();
+}
+
+
+void EventSource::ClearStack() {
+ if (!trace_.IsEmpty()) {
+ trace_.Dispose();
+ trace_.Clear();
+ }
+}
+
+
+void EventSource::RecordStack() {
+ HandleScope scope;
+
+ ClearStack();
+
+ // Assume inside HandleScope
+#if RECORD
+ Local<StackTrace> trace =
+ StackTrace::CurrentStackTrace(kFrameLimit, StackTrace::kOverview);
+
+ trace_ = Persistent<StackTrace>::New(trace);
+
+ // Set parent.
+ if (current_source) {
+ parent_source_ = Persistent<Object>::New(current_source->handle_);
+ parent_source_.MakeWeak(this, WeakParent);
+ }
+#endif
+}
+
+
+void EventSource::WeakParent(Persistent<Value> object, void* data) {
+ EventSource *s = static_cast<EventSource*>(data);
+ assert(s->parent_source_->StrictEquals(object));
+ s->DeleteParent();
+}
+
+
+void EventSource::DeleteParent() {
+ if (!parent_source_.IsEmpty()) {
+ parent_source_.ClearWeak();
+ parent_source_.Dispose();
+ parent_source_.Clear();
+ }
+}
+
+
} // namespace node
64 src/node_events.h
View
@@ -2,11 +2,13 @@
#ifndef SRC_EVENTS_H_
#define SRC_EVENTS_H_
+#include <node.h>
#include <node_object_wrap.h>
#include <v8.h>
namespace node {
+// Legacy interface, do no use.
class EventEmitter : public ObjectWrap {
public:
static void Initialize(v8::Local<v8::FunctionTemplate> ctemplate);
@@ -20,5 +22,67 @@ class EventEmitter : public ObjectWrap {
EventEmitter() : ObjectWrap () { }
};
+
+// "hard emitter" base class
+class EventSource : public ObjectWrap {
+ public:
+ // All subclasses must call this function to initialize their JavaScript
+ // counterparts. Subclasses are free to decorate the template with additional
+ // and such.
+ template <class T>
+ static v8::Local<v8::FunctionTemplate> BuildTemplate(const char *name) {
+ // TODO node_events_impl.h
+ v8::HandleScope scope;
+
+ v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(EventSource::JSNew<T>);
+ t->InstanceTemplate()->SetInternalFieldCount(1);
+ t->SetClassName(v8::String::NewSymbol(name));
+
+ return scope.Close(t);
+ }
+
+ // Subclasses to call this when they go from inactive to active.
+ void Active();
+
+ // And this when going from active to inactive
+ void Inactive();
+
+ EventSource() : ObjectWrap() { }
+
+ virtual ~EventSource() {
+ ClearStack();
+ DeleteParent();
+ }
+
+ // Subclasses call this when they get a event.
+ v8::Local<v8::Value> MakeCallback(int argc, v8::Handle<v8::Value> argv[]);
+
+ private:
+ template <class T>
+ static v8::Handle<v8::Value> JSNew(const v8::Arguments& args) {
+ // TODO node_events_impl.h
+ v8::HandleScope scope;
+ T *t = new T();
+ t->Wrap(args.This());
+ return args.This();
+ }
+
+ static void WeakParent(v8::Persistent<v8::Value> object, void* data);
+
+ void RecordStack();
+ void ClearStack();
+ void DeleteParent();
+
+ void PrintStack(int count = 0);
+
+ v8::Persistent<v8::Object> parent_source_;
+ v8::Persistent<v8::StackTrace> trace_;
+
+ static const int kFrameLimit = 10;
+ static const int kAncestorStackLimit = 10;
+ static EventSource* current_source;
+};
+
+
} // namespace node
#endif // SRC_EVENTS_H_
65 src/node_file.cc
View
@@ -36,12 +36,10 @@ static Persistent<String> errno_symbol;
// file-level rather than method-level to avoid excess stack usage.
static char getbuf[PATH_MAX + 1];
-static int After(eio_req *req) {
- HandleScope scope;
-
- Persistent<Function> *callback = cb_unwrap(req->data);
+int EIORequest::After(eio_req *req) {
+ EIORequest *r = static_cast<EIORequest*>(req->data);
- ev_unref(EV_DEFAULT_UC);
+ HandleScope scope;
int argc = 0;
Local<Value> argv[6]; // 6 is the maximum number of args
@@ -150,26 +148,28 @@ static int After(eio_req *req) {
}
}
- TryCatch try_catch;
- (*callback)->Call(v8::Context::GetCurrent()->Global(), argc, argv);
+ r->MakeCallback(argc, argv);
+ r->Inactive();
- if (try_catch.HasCaught()) {
- FatalException(try_catch);
- }
+ return 0;
+}
- // Dispose of the persistent handle
- cb_destroy(callback);
- return 0;
+void EIORequest::Set(eio_req *req, Handle<Value> callback) {
+ req_ = req;
+ req->data = this;
+ handle_->ToObject()->Set(String::NewSymbol("callback"), callback);
+ Active();
}
-#define ASYNC_CALL(func, callback, ...) \
- eio_req *req = eio_##func(__VA_ARGS__, EIO_PRI_DEFAULT, After, \
- cb_persist(callback)); \
- assert(req); \
- ev_ref(EV_DEFAULT_UC); \
- return Undefined();
+
+#define ASYNC_CALL(func, callback, ...) \
+ EIORequest* r = EIORequest::New(); \
+ eio_req *req = eio_##func(__VA_ARGS__, EIO_PRI_DEFAULT, \
+ EIORequest::After, r); \
+ r->Set(req, callback); \
+ return scope.Close(r->handle_);
static Handle<Value> Close(const Arguments& args) {
HandleScope scope;
@@ -775,6 +775,31 @@ static Handle<Value> Chown(const Arguments& args) {
}
}
+
+static Persistent<FunctionTemplate> eio_request_template;
+
+void EIORequest::Initialize(Handle<Object> target) {
+ HandleScope scope;
+
+ Local<FunctionTemplate> t = BuildTemplate<EIORequest>("EIORequest");
+
+ eio_request_template = Persistent<FunctionTemplate>::New(t);
+
+ //NODE_SET_PROTOTYPE_METHOD(t, "cancel", EIORequest::Cancel);
+
+ target->Set(String::NewSymbol("EIORequest"),
+ eio_request_template->GetFunction());
+}
+
+
+EIORequest* EIORequest::New() {
+ HandleScope scope;
+ Local<Object> req_obj = eio_request_template->GetFunction()->NewInstance();
+ EIORequest *r = ObjectWrap::Unwrap<EIORequest>(req_obj);
+ return r;
+}
+
+
void File::Initialize(Handle<Object> target) {
HandleScope scope;
@@ -803,6 +828,8 @@ void File::Initialize(Handle<Object> target) {
errno_symbol = NODE_PSYMBOL("errno");
encoding_symbol = NODE_PSYMBOL("node:encoding");
+
+ EIORequest::Initialize(target);
}
void InitFs(Handle<Object> target) {
30 src/node_file.h
View
@@ -5,6 +5,7 @@
#include <node.h>
#include <node_events.h>
#include <v8.h>
+#include <eio.h>
namespace node {
@@ -15,5 +16,34 @@ class File {
void InitFs(v8::Handle<v8::Object> target);
+class EIORequest : public EventSource {
+ public:
+ static void Initialize(v8::Handle<v8::Object> target);
+
+ static EIORequest* New();
+
+ EIORequest() : EventSource() { }
+ ~EIORequest() { }
+
+ void Set(eio_req *req, v8::Handle<v8::Value> callback);
+
+ static int After(eio_req *req);
+
+ void Active() {
+ EventSource::Active();
+ ev_ref(EV_DEFAULT_UC);
+ }
+
+ void Inactive() {
+ ev_unref(EV_DEFAULT_UC);
+ req_ = NULL;
+ EventSource::Inactive();
+ }
+
+ private:
+ eio_req *req_;
+};
+
+
} // namespace node
#endif // SRC_FILE_H_
108 src/node_io_watcher.cc
View
@@ -2,6 +2,7 @@
#include <node_io_watcher.h>
#include <node.h>
+#include <node_events.h>
#include <v8.h>
#include <assert.h>
@@ -10,25 +11,17 @@ namespace node {
using namespace v8;
-Persistent<FunctionTemplate> IOWatcher::constructor_template;
-Persistent<String> callback_symbol;
-
void IOWatcher::Initialize(Handle<Object> target) {
HandleScope scope;
- Local<FunctionTemplate> t = FunctionTemplate::New(IOWatcher::New);
- constructor_template = Persistent<FunctionTemplate>::New(t);
- constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
- constructor_template->SetClassName(String::NewSymbol("IOWatcher"));
-
- NODE_SET_PROTOTYPE_METHOD(constructor_template, "start", IOWatcher::Start);
- NODE_SET_PROTOTYPE_METHOD(constructor_template, "stop", IOWatcher::Stop);
- NODE_SET_PROTOTYPE_METHOD(constructor_template, "set", IOWatcher::Set);
+ Local<FunctionTemplate> t = BuildTemplate<IOWatcher>("IOWatcher");
- target->Set(String::NewSymbol("IOWatcher"), constructor_template->GetFunction());
+ NODE_SET_PROTOTYPE_METHOD(t, "set", IOWatcher::Set);
+ NODE_SET_PROTOTYPE_METHOD(t, "start", IOWatcher::Start);
+ NODE_SET_PROTOTYPE_METHOD(t, "stop", IOWatcher::Stop);
- callback_symbol = NODE_PSYMBOL("callback");
+ target->Set(String::NewSymbol("IOWatcher"), t->GetFunction());
}
@@ -37,73 +30,11 @@ void IOWatcher::Callback(EV_P_ ev_io *w, int revents) {
assert(w == &io->watcher_);
HandleScope scope;
- Local<Value> callback_v = io->handle_->Get(callback_symbol);
- if (!callback_v->IsFunction()) {
- io->Stop();
- return;
- }
-
- Local<Function> callback = Local<Function>::Cast(callback_v);
-
- TryCatch try_catch;
-
Local<Value> argv[2];
argv[0] = Local<Value>::New(revents & EV_READ ? True() : False());
argv[1] = Local<Value>::New(revents & EV_WRITE ? True() : False());
- io->Ref();
- callback->Call(io->handle_, 2, argv);
- io->Unref();
-
- if (try_catch.HasCaught()) {
- FatalException(try_catch);
- }
-}
-
-
-//
-// var io = new process.IOWatcher();
-// process.callback = function (readable, writable) { ... };
-// io.set(fd, true, false);
-// io.start();
-//
-Handle<Value> IOWatcher::New(const Arguments& args) {
- HandleScope scope;
- IOWatcher *s = new IOWatcher();
- s->Wrap(args.This());
- return args.This();
-}
-
-
-Handle<Value> IOWatcher::Start(const Arguments& args) {
- HandleScope scope;
- IOWatcher *io = ObjectWrap::Unwrap<IOWatcher>(args.Holder());
- io->Start();
- return Undefined();
-}
-
-
-Handle<Value> IOWatcher::Stop(const Arguments& args) {
- HandleScope scope;
- IOWatcher *io = ObjectWrap::Unwrap<IOWatcher>(args.Holder());
- io->Stop();
- return Undefined();
-}
-
-
-void IOWatcher::Start() {
- if (!ev_is_active(&watcher_)) {
- ev_io_start(EV_DEFAULT_UC_ &watcher_);
- Ref();
- }
-}
-
-
-void IOWatcher::Stop() {
- if (ev_is_active(&watcher_)) {
- ev_io_stop(EV_DEFAULT_UC_ &watcher_);
- Unref();
- }
+ io->MakeCallback(2, argv);
}
@@ -141,5 +72,30 @@ Handle<Value> IOWatcher::Set(const Arguments& args) {
}
+Handle<Value> IOWatcher::Start(const Arguments& args) {
+ HandleScope scope;
+ IOWatcher *io = ObjectWrap::Unwrap<IOWatcher>(args.Holder());
+
+ if (!ev_is_active(&io->watcher_)) {
+ ev_io_start(EV_DEFAULT_UC_ &io->watcher_);
+ io->Active();
+ }
+
+ return Undefined();
+}
+
+
+Handle<Value> IOWatcher::Stop(const Arguments& args) {
+ HandleScope scope;
+ IOWatcher *io = ObjectWrap::Unwrap<IOWatcher>(args.Holder());
+
+ if (ev_is_active(&io->watcher_)) {
+ ev_io_stop(EV_DEFAULT_UC_ &io->watcher_);
+ io->Inactive();
+ }
+
+ return Undefined();
+}
+
} // namespace node
17 src/node_io_watcher.h
View
@@ -2,40 +2,33 @@
#ifndef NODE_IO_H_
#define NODE_IO_H_
-#include <node_object_wrap.h>
+#include <node_events.h>
#include <ev.h>
namespace node {
-class IOWatcher : ObjectWrap {
+class IOWatcher : public EventSource {
public:
static void Initialize(v8::Handle<v8::Object> target);
- protected:
- static v8::Persistent<v8::FunctionTemplate> constructor_template;
-
- IOWatcher() : ObjectWrap() {
+ IOWatcher() : EventSource() {
ev_init(&watcher_, IOWatcher::Callback);
watcher_.data = this;
}
+ protected:
~IOWatcher() {
- ev_io_stop(EV_DEFAULT_UC_ &watcher_);
assert(!ev_is_active(&watcher_));
assert(!ev_is_pending(&watcher_));
}
- static v8::Handle<v8::Value> New(const v8::Arguments& args);
+ static v8::Handle<v8::Value> Set(const v8::Arguments& args);
static v8::Handle<v8::Value> Start(const v8::Arguments& args);
static v8::Handle<v8::Value> Stop(const v8::Arguments& args);
- static v8::Handle<v8::Value> Set(const v8::Arguments& args);
private:
static void Callback(EV_P_ ev_io *watcher, int revents);
- void Start();
- void Stop();
-
ev_io watcher_;
};
61 src/node_signal_watcher.cc
View
@@ -6,52 +6,30 @@ namespace node {
using namespace v8;
-Persistent<FunctionTemplate> SignalWatcher::constructor_template;
-static Persistent<String> callback_symbol;
void SignalWatcher::Initialize(Handle<Object> target) {
HandleScope scope;
- Local<FunctionTemplate> t = FunctionTemplate::New(SignalWatcher::New);
- constructor_template = Persistent<FunctionTemplate>::New(t);
- constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
- constructor_template->SetClassName(String::NewSymbol("SignalWatcher"));
+ Local<FunctionTemplate> t = BuildTemplate<SignalWatcher>("SignalWatcher");
- NODE_SET_PROTOTYPE_METHOD(constructor_template, "start", SignalWatcher::Start);
- NODE_SET_PROTOTYPE_METHOD(constructor_template, "stop", SignalWatcher::Stop);
+ NODE_SET_PROTOTYPE_METHOD(t, "start", Start);
+ NODE_SET_PROTOTYPE_METHOD(t, "stop", Stop);
+ NODE_SET_PROTOTYPE_METHOD(t, "set", Set);
- target->Set(String::NewSymbol("SignalWatcher"),
- constructor_template->GetFunction());
-
- callback_symbol = NODE_PSYMBOL("callback");
+ target->Set(String::NewSymbol("SignalWatcher"), t->GetFunction());
}
+
void SignalWatcher::Callback(EV_P_ ev_signal *watcher, int revents) {
SignalWatcher *w = static_cast<SignalWatcher*>(watcher->data);
-
assert(watcher == &w->watcher_);
-
- HandleScope scope;
-
- Local<Value> callback_v = w->handle_->Get(callback_symbol);
- if (!callback_v->IsFunction()) {
- w->Stop();
- return;
- }
-
- Local<Function> callback = Local<Function>::Cast(callback_v);
-
- TryCatch try_catch;
-
- callback->Call(w->handle_, 0, NULL);
-
- if (try_catch.HasCaught()) {
- FatalException(try_catch);
- }
+ w->MakeCallback(0, NULL);
}
-Handle<Value> SignalWatcher::New(const Arguments& args) {
+
+Handle<Value> SignalWatcher::Set(const Arguments& args) {
HandleScope scope;
+ SignalWatcher *w = ObjectWrap::Unwrap<SignalWatcher>(args.Holder());
if (args.Length() != 1 || !args[0]->IsInt32()) {
return ThrowException(String::New("Bad arguments"));
@@ -59,12 +37,11 @@ Handle<Value> SignalWatcher::New(const Arguments& args) {
int sig = args[0]->Int32Value();
- SignalWatcher *w = new SignalWatcher(sig);
- w->Wrap(args.Holder());
-
- return args.This();
+ ev_signal_set(&w->watcher_, sig);
+ return Undefined();
}
+
Handle<Value> SignalWatcher::Start(const Arguments& args) {
HandleScope scope;
SignalWatcher *w = ObjectWrap::Unwrap<SignalWatcher>(args.Holder());
@@ -72,14 +49,16 @@ Handle<Value> SignalWatcher::Start(const Arguments& args) {
return Undefined();
}
+
void SignalWatcher::Start () {
- if (!watcher_.active) {
+ if (!ev_is_active(&watcher_)) {
ev_signal_start(EV_DEFAULT_UC_ &watcher_);
ev_unref(EV_DEFAULT_UC);
- Ref();
+ Active();
}
}
+
Handle<Value> SignalWatcher::Stop(const Arguments& args) {
HandleScope scope;
SignalWatcher *w = ObjectWrap::Unwrap<SignalWatcher>(args.Holder());
@@ -87,14 +66,16 @@ Handle<Value> SignalWatcher::Stop(const Arguments& args) {
return Undefined();
}
+
void SignalWatcher::Stop () {
- if (watcher_.active) {
+ if (ev_is_active(&watcher_)) {
ev_ref(EV_DEFAULT_UC);
ev_signal_stop(EV_DEFAULT_UC_ &watcher_);
- Unref();
+ Inactive();
}
}
+
} // namespace node
NODE_MODULE(node_signal_watcher, node::SignalWatcher::Initialize);
12 src/node_signal_watcher.h
View
@@ -10,15 +10,12 @@
namespace node {
-class SignalWatcher : ObjectWrap {
+class SignalWatcher : public EventSource {
public:
static void Initialize(v8::Handle<v8::Object> target);
- protected:
- static v8::Persistent<v8::FunctionTemplate> constructor_template;
-
- SignalWatcher(int sig) : ObjectWrap() {
- ev_signal_init(&watcher_, SignalWatcher::Callback, sig);
+ SignalWatcher() : EventSource() {
+ ev_signal_init(&watcher_, SignalWatcher::Callback, SIGINT);
watcher_.data = this;
}
@@ -26,9 +23,10 @@ class SignalWatcher : ObjectWrap {
ev_signal_stop(EV_DEFAULT_UC_ &watcher_);
}
- static v8::Handle<v8::Value> New(const v8::Arguments& args);
+ protected:
static v8::Handle<v8::Value> Start(const v8::Arguments& args);
static v8::Handle<v8::Value> Stop(const v8::Arguments& args);
+ static v8::Handle<v8::Value> Set(const v8::Arguments& args);
private:
static void Callback(EV_P_ ev_signal *watcher, int revents);
63 src/node_stat_watcher.cc
View
@@ -9,47 +9,28 @@ namespace node {
using namespace v8;
-Persistent<FunctionTemplate> StatWatcher::constructor_template;
-
-static Persistent<String> change_symbol;
-static Persistent<String> stop_symbol;
-
void StatWatcher::Initialize(Handle<Object> target) {
HandleScope scope;
- Local<FunctionTemplate> t = FunctionTemplate::New(StatWatcher::New);
- constructor_template = Persistent<FunctionTemplate>::New(t);
- constructor_template->Inherit(EventEmitter::constructor_template);
- constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
- constructor_template->SetClassName(String::NewSymbol("StatWatcher"));
-
- change_symbol = NODE_PSYMBOL("change");
- stop_symbol = NODE_PSYMBOL("stop");
+ Local<FunctionTemplate> t = BuildTemplate<StatWatcher>("StatWatcher");
- NODE_SET_PROTOTYPE_METHOD(constructor_template, "start", StatWatcher::Start);
- NODE_SET_PROTOTYPE_METHOD(constructor_template, "stop", StatWatcher::Stop);
+ NODE_SET_PROTOTYPE_METHOD(t, "start", Start);
+ NODE_SET_PROTOTYPE_METHOD(t, "stop", Stop);
- target->Set(String::NewSymbol("StatWatcher"), constructor_template->GetFunction());
+ target->Set(String::NewSymbol("StatWatcher"), t->GetFunction());
}
void StatWatcher::Callback(EV_P_ ev_stat *watcher, int revents) {
assert(revents == EV_STAT);
- StatWatcher *handler = static_cast<StatWatcher*>(watcher->data);
- assert(watcher == &handler->watcher_);
+ StatWatcher *w = static_cast<StatWatcher*>(watcher->data);
+ assert(watcher == &w->watcher_);
+
HandleScope scope;
Handle<Value> argv[2];
argv[0] = Handle<Value>(BuildStatsObject(&watcher->attr));
argv[1] = Handle<Value>(BuildStatsObject(&watcher->prev));
- handler->Emit(change_symbol, 2, argv);
-}
-
-
-Handle<Value> StatWatcher::New(const Arguments& args) {
- HandleScope scope;
- StatWatcher *s = new StatWatcher();
- s->Wrap(args.Holder());
- return args.This();
+ w->MakeCallback(2, argv);
}
@@ -60,27 +41,24 @@ Handle<Value> StatWatcher::Start(const Arguments& args) {
return ThrowException(Exception::TypeError(String::New("Bad arguments")));
}
- StatWatcher *handler = ObjectWrap::Unwrap<StatWatcher>(args.Holder());
+ StatWatcher *w = ObjectWrap::Unwrap<StatWatcher>(args.Holder());
+
String::Utf8Value path(args[0]->ToString());
+ assert(w->path_ == NULL);
+ w->path_ = strdup(*path);
- assert(handler->path_ == NULL);
- handler->path_ = strdup(*path);
+ w->persistent_ = args[1]->IsTrue();
ev_tstamp interval = 0.;
if (args[2]->IsInt32()) {
interval = NODE_V8_UNIXTIME(args[2]);
}
- ev_stat_set(&handler->watcher_, handler->path_, interval);
- ev_stat_start(EV_DEFAULT_UC_ &handler->watcher_);
-
- handler->persistent_ = args[1]->IsTrue();
-
- if (!handler->persistent_) {
- ev_unref(EV_DEFAULT_UC);
- }
+ ev_stat_set(&w->watcher_, w->path_, interval);
+ ev_stat_start(EV_DEFAULT_UC_ &w->watcher_);
- handler->Ref();
+ w->Active();
+ if (!w->persistent_) ev_unref(EV_DEFAULT_UC);
return Undefined();
}
@@ -88,9 +66,8 @@ Handle<Value> StatWatcher::Start(const Arguments& args) {
Handle<Value> StatWatcher::Stop(const Arguments& args) {
HandleScope scope;
- StatWatcher *handler = ObjectWrap::Unwrap<StatWatcher>(args.Holder());
- handler->Emit(stop_symbol, 0, NULL);
- handler->Stop();
+ StatWatcher *w = ObjectWrap::Unwrap<StatWatcher>(args.Holder());
+ w->Stop();
return Undefined();
}
@@ -99,9 +76,9 @@ void StatWatcher::Stop () {
if (watcher_.active) {
if (!persistent_) ev_ref(EV_DEFAULT_UC);
ev_stat_stop(EV_DEFAULT_UC_ &watcher_);
+ Inactive();
free(path_);
path_ = NULL;
- Unref();
}
}
10 src/node_stat_watcher.h
View
@@ -8,14 +8,11 @@
namespace node {
-class StatWatcher : EventEmitter {
+class StatWatcher : public EventSource {
public:
static void Initialize(v8::Handle<v8::Object> target);
- protected:
- static v8::Persistent<v8::FunctionTemplate> constructor_template;
-
- StatWatcher() : EventEmitter() {
+ StatWatcher() : EventSource() {
persistent_ = false;
path_ = NULL;
ev_init(&watcher_, StatWatcher::Callback);
@@ -27,7 +24,8 @@ class StatWatcher : EventEmitter {
assert(path_ == NULL);
}
- static v8::Handle<v8::Value> New(const v8::Arguments& args);
+
+ protected:
static v8::Handle<v8::Value> Start(const v8::Arguments& args);
static v8::Handle<v8::Value> Stop(const v8::Arguments& args);
97 src/node_timer.cc
View
@@ -5,39 +5,30 @@
using namespace v8;
using namespace node;
-Persistent<FunctionTemplate> Timer::constructor_template;
-
-static Persistent<String> timeout_symbol;
static Persistent<String> repeat_symbol;
-static Persistent<String> callback_symbol;
-void
-Timer::Initialize (Handle<Object> target)
-{
+
+void Timer::Initialize (Handle<Object> target) {
HandleScope scope;
- Local<FunctionTemplate> t = FunctionTemplate::New(Timer::New);
- constructor_template = Persistent<FunctionTemplate>::New(t);
- constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
- constructor_template->SetClassName(String::NewSymbol("Timer"));
+ Local<FunctionTemplate> t = BuildTemplate<Timer>("Timer");
- timeout_symbol = NODE_PSYMBOL("timeout");
repeat_symbol = NODE_PSYMBOL("repeat");
- callback_symbol = NODE_PSYMBOL("callback");
- NODE_SET_PROTOTYPE_METHOD(constructor_template, "start", Timer::Start);
- NODE_SET_PROTOTYPE_METHOD(constructor_template, "stop", Timer::Stop);
- NODE_SET_PROTOTYPE_METHOD(constructor_template, "again", Timer::Again);
+ NODE_SET_PROTOTYPE_METHOD(t, "start", Timer::Start);
+ NODE_SET_PROTOTYPE_METHOD(t, "stop", Timer::Stop);
+ NODE_SET_PROTOTYPE_METHOD(t, "again", Timer::Again);
- constructor_template->InstanceTemplate()->SetAccessor(repeat_symbol,
- RepeatGetter, RepeatSetter);
+ t->InstanceTemplate()->SetAccessor(repeat_symbol,
+ RepeatGetter,
+ RepeatSetter);
- target->Set(String::NewSymbol("Timer"), constructor_template->GetFunction());
+ target->Set(String::NewSymbol("Timer"), t->GetFunction());
}
-Handle<Value>
-Timer::RepeatGetter (Local<String> property, const AccessorInfo& info)
-{
+
+Handle<Value> Timer::RepeatGetter(Local<String> property,
+ const AccessorInfo& info) {
HandleScope scope;
Timer *timer = ObjectWrap::Unwrap<Timer>(info.This());
@@ -49,9 +40,10 @@ Timer::RepeatGetter (Local<String> property, const AccessorInfo& info)
return scope.Close(v);
}
-void
-Timer::RepeatSetter (Local<String> property, Local<Value> value, const AccessorInfo& info)
-{
+
+void Timer::RepeatSetter(Local<String> property,
+ Local<Value> value,
+ const AccessorInfo& info) {
HandleScope scope;
Timer *timer = ObjectWrap::Unwrap<Timer>(info.This());
@@ -61,64 +53,35 @@ Timer::RepeatSetter (Local<String> property, Local<Value> value, const AccessorI
timer->watcher_.repeat = NODE_V8_UNIXTIME(value);
}
-void
-Timer::OnTimeout (EV_P_ ev_timer *watcher, int revents)
-{
+
+void Timer::Callback(EV_P_ ev_timer *watcher, int revents) {
Timer *timer = static_cast<Timer*>(watcher->data);
assert(revents == EV_TIMEOUT);
HandleScope scope;
- Local<Value> callback_v = timer->handle_->Get(callback_symbol);
- if (!callback_v->IsFunction()) {
- timer->Stop();
- return;
- }
-
- Local<Function> callback = Local<Function>::Cast(callback_v);
-
- TryCatch try_catch;
-
- callback->Call(timer->handle_, 0, NULL);
+ timer->MakeCallback(0, NULL);
- if (try_catch.HasCaught()) {
- FatalException(try_catch);
+ if (timer->watcher_.repeat == 0) {
+ timer->Inactive();
}
-
- if (timer->watcher_.repeat == 0) timer->Unref();
-}
-
-Timer::~Timer ()
-{
- ev_timer_stop(EV_DEFAULT_UC_ &watcher_);
}
-Handle<Value>
-Timer::New (const Arguments& args)
-{
- HandleScope scope;
-
- Timer *t = new Timer();
- t->Wrap(args.Holder());
-
- return args.This();
-}
-Handle<Value>
-Timer::Start (const Arguments& args)
-{
+Handle<Value> Timer::Start(const Arguments& args) {
HandleScope scope;
Timer *timer = ObjectWrap::Unwrap<Timer>(args.Holder());
- if (args.Length() != 2)
+ if (args.Length() != 2) {
return ThrowException(String::New("Bad arguments"));
+ }
bool was_active = ev_is_active(&timer->watcher_);
ev_tstamp after = NODE_V8_UNIXTIME(args[0]);
ev_tstamp repeat = NODE_V8_UNIXTIME(args[1]);
- ev_timer_init(&timer->watcher_, Timer::OnTimeout, after, repeat);
+ ev_timer_init(&timer->watcher_, Timer::Callback, after, repeat);
timer->watcher_.data = timer;
// Update the event loop time. Need to call this because processing JS can
@@ -127,7 +90,7 @@ Timer::Start (const Arguments& args)
ev_timer_start(EV_DEFAULT_UC_ &timer->watcher_);
- if (!was_active) timer->Ref();
+ if (!was_active) timer->Active();
return Undefined();
}
@@ -144,7 +107,7 @@ Handle<Value> Timer::Stop(const Arguments& args) {
void Timer::Stop () {
if (watcher_.active) {
ev_timer_stop(EV_DEFAULT_UC_ &watcher_);
- Unref();
+ Inactive();
}
}
@@ -167,9 +130,9 @@ Handle<Value> Timer::Again(const Arguments& args) {
// appropriately.
if (ev_is_active(&timer->watcher_)) {
- if (!was_active) timer->Ref();
+ if (!was_active) timer->Active();
} else {
- if (was_active) timer->Unref();
+ if (was_active) timer->Inactive();
}
return Undefined();
21 src/node_timer.h
View
@@ -2,27 +2,28 @@
#define node_timer_h
#include <node.h>
-#include <node_object_wrap.h>
+#include <node_events.h>
#include <v8.h>
#include <ev.h>
namespace node {
-class Timer : ObjectWrap {
+class Timer : public EventSource {
public:
static void Initialize (v8::Handle<v8::Object> target);
- protected:
- static v8::Persistent<v8::FunctionTemplate> constructor_template;
-
- Timer () : ObjectWrap () {
+ Timer () : EventSource () {
// dummy timeout values
- ev_timer_init(&watcher_, OnTimeout, 0., 1.);
+ ev_timer_init(&watcher_, Callback, 0., 1.);
watcher_.data = this;
}
- ~Timer();
- static v8::Handle<v8::Value> New (const v8::Arguments& args);
+ protected:
+
+ ~Timer() {
+ ev_timer_stop(EV_DEFAULT_UC_ &watcher_);
+ }
+
static v8::Handle<v8::Value> Start (const v8::Arguments& args);
static v8::Handle<v8::Value> Stop (const v8::Arguments& args);
static v8::Handle<v8::Value> Again (const v8::Arguments& args);
@@ -30,7 +31,7 @@ class Timer : ObjectWrap {
static void RepeatSetter (v8::Local<v8::String> property, v8::Local<v8::Value> value, const v8::AccessorInfo& info);
private:
- static void OnTimeout (EV_P_ ev_timer *watcher, int revents);
+ static void Callback (EV_P_ ev_timer *watcher, int revents);
void Stop ();
ev_timer watcher_;
};

No commit comments for this range

Something went wrong with that request. Please try again.