/
async.cpp
100 lines (71 loc) · 2.5 KB
/
async.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <cstdlib>
#include <node.h>
#include <node_buffer.h>
using namespace v8;
using namespace node;
class Async : public ObjectWrap {
static Persistent<FunctionTemplate> constructor_template;
int x, y;
public:
Async(int xx, int yy) : x(xx), y(yy) {}
static void Initialize(Handle<Object> target) {
HandleScope scope;
Local<FunctionTemplate> t = FunctionTemplate::New(New);
constructor_template = Persistent<FunctionTemplate>::New(t);
constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
constructor_template->SetClassName(String::NewSymbol("Async"));
NODE_SET_PROTOTYPE_METHOD(constructor_template, "moo", Moo);
target->Set(String::NewSymbol("Async"), constructor_template->GetFunction());
}
static Handle<Value> New(const Arguments &args) {
HandleScope scope;
Async *async = new Async(args[0]->Int32Value(), args[1]->Int32Value());
async->Wrap(args.This());
return args.This();
}
struct moo_request {
Persistent<Function> cb;
Async *async;
int result;
};
static int EIO_Moo(eio_req *req) {
moo_request *moo_req = (moo_request *)req->data;
Async *async = moo_req->async;
moo_req->result = async->x * async->y;
return 0;
}
static int EIO_MooAfter(eio_req *req) {
HandleScope scope;
ev_unref(EV_DEFAULT_UC);
moo_request *moo_req = (moo_request *)req->data;
Local<Value> argv[1];
argv[0] = Integer::New(moo_req->result);
TryCatch try_catch;
moo_req->cb->Call(Context::GetCurrent()->Global(), 1, argv);
if (try_catch.HasCaught())
FatalException(try_catch);
moo_req->cb.Dispose();
moo_req->async->Unref();
free(moo_req);
return 0;
}
static Handle<Value> Moo(const Arguments &args) {
HandleScope scope;
Local<Function> cb = Local<Function>::Cast(args[0]);
Async *async = ObjectWrap::Unwrap<Async>(args.This());
moo_request *moo_req = (moo_request *)malloc(sizeof(moo_request));
moo_req->cb = Persistent<Function>::New(cb);
moo_req->async = async;
eio_custom(EIO_Moo, EIO_PRI_DEFAULT, EIO_MooAfter, moo_req);
ev_ref(EV_DEFAULT_UC);
async->Ref();
return Undefined();
}
};
Persistent<FunctionTemplate> Async::constructor_template;
extern "C" void
init(Handle<Object> target)
{
HandleScope scope;
Async::Initialize(target);
}