src: deduplicate CHECK_EQ/CHECK_NE macros
[platform/upstream/nodejs.git] / src / node_stat_watcher.cc
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 #include "node_stat_watcher.h"
23 #include "async-wrap.h"
24 #include "async-wrap-inl.h"
25 #include "env.h"
26 #include "env-inl.h"
27 #include "util.h"
28 #include "util-inl.h"
29
30 #include <assert.h>
31 #include <string.h>
32 #include <stdlib.h>
33
34 namespace node {
35
36 using v8::Context;
37 using v8::FunctionCallbackInfo;
38 using v8::FunctionTemplate;
39 using v8::Handle;
40 using v8::HandleScope;
41 using v8::Integer;
42 using v8::Local;
43 using v8::Object;
44 using v8::String;
45 using v8::Value;
46
47
48 void StatWatcher::Initialize(Environment* env, Handle<Object> target) {
49   HandleScope scope(env->isolate());
50
51   Local<FunctionTemplate> t = FunctionTemplate::New(env->isolate(),
52                                                     StatWatcher::New);
53   t->InstanceTemplate()->SetInternalFieldCount(1);
54   t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "StatWatcher"));
55
56   NODE_SET_PROTOTYPE_METHOD(t, "start", StatWatcher::Start);
57   NODE_SET_PROTOTYPE_METHOD(t, "stop", StatWatcher::Stop);
58
59   target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "StatWatcher"),
60               t->GetFunction());
61 }
62
63
64 static void Delete(uv_handle_t* handle) {
65   delete reinterpret_cast<uv_fs_poll_t*>(handle);
66 }
67
68
69 StatWatcher::StatWatcher(Environment* env, Local<Object> wrap)
70     : AsyncWrap(env, wrap, AsyncWrap::PROVIDER_STATWATCHER),
71       watcher_(new uv_fs_poll_t) {
72   MakeWeak<StatWatcher>(this);
73   uv_fs_poll_init(env->event_loop(), watcher_);
74   watcher_->data = static_cast<void*>(this);
75 }
76
77
78 StatWatcher::~StatWatcher() {
79   Stop();
80   uv_close(reinterpret_cast<uv_handle_t*>(watcher_), Delete);
81 }
82
83
84 void StatWatcher::Callback(uv_fs_poll_t* handle,
85                            int status,
86                            const uv_stat_t* prev,
87                            const uv_stat_t* curr) {
88   StatWatcher* wrap = static_cast<StatWatcher*>(handle->data);
89   assert(wrap->watcher_ == handle);
90   Environment* env = wrap->env();
91   HandleScope handle_scope(env->isolate());
92   Context::Scope context_scope(env->context());
93   Local<Value> argv[] = {
94     BuildStatsObject(env, curr),
95     BuildStatsObject(env, prev),
96     Integer::New(env->isolate(), status)
97   };
98   wrap->MakeCallback(env->onchange_string(), ARRAY_SIZE(argv), argv);
99 }
100
101
102 void StatWatcher::New(const FunctionCallbackInfo<Value>& args) {
103   assert(args.IsConstructCall());
104   HandleScope handle_scope(args.GetIsolate());
105   Environment* env = Environment::GetCurrent(args.GetIsolate());
106   new StatWatcher(env, args.This());
107 }
108
109
110 void StatWatcher::Start(const FunctionCallbackInfo<Value>& args) {
111   assert(args.Length() == 3);
112   Environment* env = Environment::GetCurrent(args.GetIsolate());
113   HandleScope scope(env->isolate());
114
115   StatWatcher* wrap = Unwrap<StatWatcher>(args.This());
116   String::Utf8Value path(args[0]);
117   const bool persistent = args[1]->BooleanValue();
118   const uint32_t interval = args[2]->Uint32Value();
119
120   if (!persistent)
121     uv_unref(reinterpret_cast<uv_handle_t*>(wrap->watcher_));
122   uv_fs_poll_start(wrap->watcher_, Callback, *path, interval);
123   wrap->ClearWeak();
124 }
125
126
127 void StatWatcher::Stop(const FunctionCallbackInfo<Value>& args) {
128   StatWatcher* wrap = Unwrap<StatWatcher>(args.This());
129   Environment* env = wrap->env();
130   HandleScope handle_scope(env->isolate());
131   Context::Scope context_scope(env->context());
132   wrap->MakeCallback(env->onstop_string(), 0, NULL);
133   wrap->Stop();
134 }
135
136
137 void StatWatcher::Stop() {
138   if (!uv_is_active(reinterpret_cast<uv_handle_t*>(watcher_)))
139     return;
140   uv_fs_poll_stop(watcher_);
141   MakeWeak<StatWatcher>(this);
142 }
143
144
145 }  // namespace node