forked from jurevans/node-cpp-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodulename.cpp
More file actions
71 lines (59 loc) · 1.81 KB
/
modulename.cpp
File metadata and controls
71 lines (59 loc) · 1.81 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <node.h>
#include <cmath>
using namespace v8;
struct Baton {
uv_work_t request;
Persistent<Function> callback;
int32_t result;
};
Handle<Value> Fn1(const Arguments& args) {
HandleScope scope;
int answer = 133.7f / M_PI;
return scope.Close(Integer::New(answer));
}
Handle<Value> Fn2(const Arguments& args) {
HandleScope scope;
int answer = 133.7f / M_PI;
Local<Value> argv[] = {
Local<Value>::New(Null()),
Local<Value>::New(Integer::New(answer))
};
Local<Function> callback = Local<Function>::Cast(args[0]);
callback->Call(Context::GetCurrent()->Global(), 2, argv);
return Undefined();
}
void Fn3Work(uv_work_t* req) {
Baton* baton = static_cast<Baton*>(req->data);
baton->result = 133.7f / M_PI;
}
void Fn3After(uv_work_t* req) {
HandleScope scope;
Baton* baton = static_cast<Baton*>(req->data);
Local<Value> argv[] = {
Local<Value>::New(Null()),
Local<Value>::New(Integer::New(baton->result))
};
TryCatch try_catch;
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
if (try_catch.HasCaught()) {
node::FatalException(try_catch);
}
baton->callback.Dispose();
delete baton;
}
Handle<Value> Fn3(const Arguments& args) {
HandleScope scope;
Baton* baton = new Baton();
baton->request.data = baton;
Local<Function> callback = Local<Function>::Cast(args[0]);
baton->callback = Persistent<Function>::New(callback);
uv_queue_work(uv_default_loop(), &baton->request, Fn3Work,
(uv_after_work_cb)Fn3After);
return Undefined();
}
void RegisterModule(Handle<Object> target) {
NODE_SET_METHOD(target, "fn1", Fn1);
NODE_SET_METHOD(target, "fn2", Fn2);
NODE_SET_METHOD(target, "fn3", Fn3);
}
NODE_MODULE(modulename, RegisterModule);