summaryrefslogtreecommitdiff
path: root/test/addons/async-hello-world/binding.cc
blob: d0303f7aeccf52ee2ae3367dbf31665fc4c3f786 (plain)
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
#include <unistd.h>
#include <node.h>
#include <v8.h>
#include <uv.h>

using namespace v8;
using namespace node;

struct async_req {
  uv_work_t req;
  int input;
  int output;
  Persistent<Function> callback;
};

void DoAsync (uv_work_t *r) {
  async_req *req = reinterpret_cast<async_req *>(r->data);
  sleep(1); // simulate CPU intensive process...
  req->output = req->input * 2;
}

void AfterAsync (uv_work_t *r) {
  HandleScope scope;
  async_req *req = reinterpret_cast<async_req *>(r->data);

  Handle<Value> argv[2] = { Null(), Integer::New(req->output) };

  TryCatch try_catch;

  req->callback->Call(Context::GetCurrent()->Global(), 2, argv);

  // cleanup
  req->callback.Dispose();
  delete req;

  if (try_catch.HasCaught()) {
    FatalException(try_catch);
  }
}

Handle<Value> Method(const Arguments& args) {
  HandleScope scope;

  async_req *req = new async_req;
  req->req.data = req;

  req->input = args[0]->IntegerValue();
  req->output = 0;

  Local<Function> callback = Local<Function>::Cast(args[1]);
  req->callback = Persistent<Function>::New(callback);

  uv_queue_work(uv_default_loop(),
                &req->req,
                DoAsync,
                (uv_after_work_cb)AfterAsync);

  return Undefined();
}

void init(Handle<Object> exports, Handle<Object> module) {
  NODE_SET_METHOD(module, "exports", Method);
}

NODE_MODULE(binding, init);