Created
June 4, 2018 22:53
-
-
Save gabrielschulhof/4d01d88a75df33981511304162c4a063 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <assert.h> | |
#include <node_api.h> | |
#define NAPI_CALL(call) \ | |
do { \ | |
napi_status status = call; \ | |
assert(status == napi_ok); \ | |
} while (0) | |
struct { | |
napi_ref ref; | |
napi_async_work work; | |
} work_info = { NULL, NULL }; | |
static void workerThread(napi_env env, void* data) { | |
fprintf(stderr, "workerThread\n"); | |
} | |
static void workComplete(napi_env env, napi_status status, void* data) { | |
napi_value cb, js_status; | |
NAPI_CALL(napi_get_reference_value(env, work_info.ref, &cb)); | |
NAPI_CALL(napi_delete_async_work(env, work_info.work)); | |
NAPI_CALL(napi_delete_reference(env, work_info.ref)); | |
work_info.work = NULL; | |
work_info.ref = NULL; | |
NAPI_CALL(napi_create_uint32(env, (uint32_t)status, &js_status)); | |
NAPI_CALL(napi_call_function(env, cb, cb, 1, &js_status, NULL)); | |
} | |
static napi_value DoWork(napi_env env, napi_callback_info info) { | |
fprintf(stderr, "DoWork: Entering\n"); | |
size_t argc = 1; | |
napi_value cb, name; | |
NAPI_CALL(napi_get_cb_info(env, info, &argc, &cb, NULL, NULL)); | |
NAPI_CALL(napi_create_reference(env, cb, 1, &work_info.ref)); | |
NAPI_CALL(napi_create_string_utf8(env, "Test", NAPI_AUTO_LENGTH, &name)); | |
NAPI_CALL(napi_create_async_work(env, NULL, name, workerThread, | |
workComplete, &work_info, &work_info.work)); | |
NAPI_CALL(napi_queue_async_work(env, work_info.work)); | |
fprintf(stderr, "DoWork: Exiting\n"); | |
return NULL; | |
} | |
NAPI_MODULE_INIT() { | |
NAPI_CALL(napi_create_function(env, "doWork", NAPI_AUTO_LENGTH, DoWork, NULL, | |
&exports)); | |
return exports; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
'targets': [ | |
{ | |
'target_name': 'binding', | |
'sources': [ 'binding.c' ] | |
} | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "20966-test-async-work", | |
"version": "1.0.0", | |
"description": "", | |
"main": "test.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1", | |
"install": "node-gyp rebuild" | |
}, | |
"author": "", | |
"license": "ISC", | |
"gypfile": true | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const doWork = require('./build/Debug/binding'); | |
let x = 0; | |
function workDone(status) { | |
console.log(`JS: workDone: ${x++}: ${status}`); | |
setImmediate(() => doWork(workDone)); | |
} | |
doWork(workDone); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment