forked from itsjohannawren/node-uvrun2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuvrun2.cc
46 lines (40 loc) · 1.18 KB
/
uvrun2.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <node.h>
#include <v8.h>
#include <uv.h>
#include <stdio.h>
namespace uvrun2 {
using namespace v8;
void RunOnce (const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = Isolate::GetCurrent ();
int result;
uv_run_mode mode;
// No arguments?
if (args.Length () == 0) {
// No args so use the blocking mode
mode = UV_RUN_ONCE;
// Has some arguments, but is the first one boolean?
} else if (args [0]->IsBoolean ()) {
// Sure enough, is it true?
if (args[0]->BooleanValue(isolate)) {
// Yup! Non-blocking it is
mode = UV_RUN_NOWAIT;
} else {
// Nope, blocking mode
mode = UV_RUN_ONCE;
}
// Has some arguments, but the first one is not a boolean
} else {
// Throw our hands up
isolate->ThrowException (Exception::TypeError (String::NewFromUtf8 (isolate, "Argument must be a boolean value")));
return;
}
// Run the main loop for one event, or possibly none
result = uv_run (uv_default_loop (), mode);
// Return the uv_run() return value
args.GetReturnValue ().Set (Number::New (isolate, result));
}
void init (Local<Object> target) {
NODE_SET_METHOD (target, "runOnce", RunOnce);
}
NODE_MODULE (uvrun2, init);
}