diff --git a/test/addons/async-hello-world/binding.cc b/test/addons/async-hello-world/binding.cc new file mode 100644 index 0000000000..d0303f7aec --- /dev/null +++ b/test/addons/async-hello-world/binding.cc @@ -0,0 +1,65 @@ +#include +#include +#include +#include + +using namespace v8; +using namespace node; + +struct async_req { + uv_work_t req; + int input; + int output; + Persistent callback; +}; + +void DoAsync (uv_work_t *r) { + async_req *req = reinterpret_cast(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(r->data); + + Handle 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 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 callback = Local::Cast(args[1]); + req->callback = Persistent::New(callback); + + uv_queue_work(uv_default_loop(), + &req->req, + DoAsync, + (uv_after_work_cb)AfterAsync); + + return Undefined(); +} + +void init(Handle exports, Handle module) { + NODE_SET_METHOD(module, "exports", Method); +} + +NODE_MODULE(binding, init); diff --git a/test/addons/async-hello-world/binding.gyp b/test/addons/async-hello-world/binding.gyp new file mode 100644 index 0000000000..3bfb84493f --- /dev/null +++ b/test/addons/async-hello-world/binding.gyp @@ -0,0 +1,8 @@ +{ + 'targets': [ + { + 'target_name': 'binding', + 'sources': [ 'binding.cc' ] + } + ] +} diff --git a/test/addons/async-hello-world/test.js b/test/addons/async-hello-world/test.js new file mode 100644 index 0000000000..535d1ab797 --- /dev/null +++ b/test/addons/async-hello-world/test.js @@ -0,0 +1,7 @@ +var assert = require('assert'); +var binding = require('./build/Release/binding'); +binding(5, function (err, val) { + assert.equal(null, err); + assert.equal(10, val); + console.error('done :)'); +});