diff --git a/src/node.cc b/src/node.cc index dc82055a17..55ca0d1c75 100644 --- a/src/node.cc +++ b/src/node.cc @@ -63,6 +63,7 @@ #ifdef __POSIX__ # include # include +# include #endif #include #include @@ -1888,6 +1889,11 @@ static Handle Binding(const Arguments& args) { binding_cache->Set(module, exports); #endif + } else if (!strcmp(*module_v, "timer")) { + exports = Object::New(); + Timer::Initialize(exports); + binding_cache->Set(module, exports); + } else if (!strcmp(*module_v, "natives")) { exports = Object::New(); DefineJavaScript(exports); diff --git a/src/node_timer.cc b/src/node_timer.cc new file mode 100644 index 0000000000..50902895e7 --- /dev/null +++ b/src/node_timer.cc @@ -0,0 +1,200 @@ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +#include +#include +#include + +namespace node { + +using namespace v8; + +Persistent Timer::constructor_template; + + +static Persistent timeout_symbol; +static Persistent repeat_symbol; +static Persistent callback_symbol; + + +void Timer::Initialize(Handle target) { + HandleScope scope; + + Local t = FunctionTemplate::New(Timer::New); + constructor_template = Persistent::New(t); + constructor_template->InstanceTemplate()->SetInternalFieldCount(1); + constructor_template->SetClassName(String::NewSymbol("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); + + constructor_template->InstanceTemplate()->SetAccessor(repeat_symbol, + RepeatGetter, RepeatSetter); + + target->Set(String::NewSymbol("Timer"), constructor_template->GetFunction()); +} + + +Handle Timer::RepeatGetter(Local property, + const AccessorInfo& info) { + HandleScope scope; + Timer *timer = ObjectWrap::Unwrap(info.This()); + + assert(timer); + assert(property == repeat_symbol); + + Local v = Integer::New(timer->watcher_.repeat); + + return scope.Close(v); +} + +void Timer::RepeatSetter(Local property, + Local value, + const AccessorInfo& info) { + HandleScope scope; + Timer *timer = ObjectWrap::Unwrap(info.This()); + + assert(timer); + assert(property == repeat_symbol); + + timer->watcher_.repeat = NODE_V8_UNIXTIME(value); +} + +void Timer::OnTimeout(EV_P_ ev_timer *watcher, int revents) { + Timer *timer = static_cast(watcher->data); + + assert(revents == EV_TIMEOUT); + + HandleScope scope; + + Local callback_v = timer->handle_->Get(callback_symbol); + if (!callback_v->IsFunction()) { + timer->Stop(); + return; + } + + Local callback = Local::Cast(callback_v); + + TryCatch try_catch; + + callback->Call(timer->handle_, 0, NULL); + + if (try_catch.HasCaught()) { + FatalException(try_catch); + } + + if (timer->watcher_.repeat == 0) timer->Unref(); +} + + +Timer::~Timer() { + ev_timer_stop(EV_DEFAULT_UC_ &watcher_); +} + + +Handle Timer::New(const Arguments& args) { + if (!args.IsConstructCall()) { + return FromConstructorTemplate(constructor_template, args); + } + + HandleScope scope; + + Timer *t = new Timer(); + t->Wrap(args.Holder()); + + return args.This(); +} + +Handle Timer::Start(const Arguments& args) { + HandleScope scope; + Timer *timer = ObjectWrap::Unwrap(args.Holder()); + + 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); + timer->watcher_.data = timer; + + // Update the event loop time. Need to call this because processing JS can + // take non-negligible amounts of time. + ev_now_update(EV_DEFAULT_UC); + + ev_timer_start(EV_DEFAULT_UC_ &timer->watcher_); + + if (!was_active) timer->Ref(); + + return Undefined(); +} + + +Handle Timer::Stop(const Arguments& args) { + HandleScope scope; + Timer *timer = ObjectWrap::Unwrap(args.Holder()); + timer->Stop(); + return Undefined(); +} + + +void Timer::Stop() { + if (watcher_.active) { + ev_timer_stop(EV_DEFAULT_UC_ &watcher_); + Unref(); + } +} + + +Handle Timer::Again(const Arguments& args) { + HandleScope scope; + Timer *timer = ObjectWrap::Unwrap(args.Holder()); + + int was_active = ev_is_active(&timer->watcher_); + + if (args.Length() > 0) { + ev_tstamp repeat = NODE_V8_UNIXTIME(args[0]); + if (repeat > 0) timer->watcher_.repeat = repeat; + } + + ev_timer_again(EV_DEFAULT_UC_ &timer->watcher_); + + // ev_timer_again can start or stop the watcher. + // So we need to check what happened and adjust the ref count + // appropriately. + + if (ev_is_active(&timer->watcher_)) { + if (!was_active) timer->Ref(); + } else { + if (was_active) timer->Unref(); + } + + return Undefined(); +} + + +} // namespace node diff --git a/src/node_timer.h b/src/node_timer.h new file mode 100644 index 0000000000..8e1c6a5126 --- /dev/null +++ b/src/node_timer.h @@ -0,0 +1,64 @@ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +#ifndef SRC_NODE_TIMER_H_ +#define SRC_NODE_TIMER_H_ + +#include +#include +#include +#include + +namespace node { + +class Timer : ObjectWrap { + public: + static void Initialize(v8::Handle target); + + protected: + static v8::Persistent constructor_template; + + Timer() : ObjectWrap() { + // dummy timeout values + ev_timer_init(&watcher_, OnTimeout, 0., 1.); + watcher_.data = this; + } + + ~Timer(); + + static v8::Handle New(const v8::Arguments& args); + static v8::Handle Start(const v8::Arguments& args); + static v8::Handle Stop(const v8::Arguments& args); + static v8::Handle Again(const v8::Arguments& args); + static v8::Handle RepeatGetter(v8::Local property, + const v8::AccessorInfo& info); + static void RepeatSetter(v8::Local property, + v8::Local value, + const v8::AccessorInfo& info); + + private: + static void OnTimeout(EV_P_ ev_timer *watcher, int revents); + void Stop(); + ev_timer watcher_; +}; + +} // namespace node +#endif // SRC_NODE_TIMER_H_ diff --git a/wscript b/wscript index 5955b4219a..7fd6315f95 100644 --- a/wscript +++ b/wscript @@ -838,6 +838,7 @@ def build(bld): node.source += " src/node_io_watcher.cc " node.source += " src/node_stdio.cc " node.source += " src/node_child_process.cc " + node.source += " src/node_timer.cc " node.source += bld.env["PLATFORM_FILE"] if not product_type_is_lib: