-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
n-api: implement async helper methods #12250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"targets": [ | ||
{ | ||
"target_name": "test_async", | ||
"sources": [ "test_async.cc" ] | ||
} | ||
] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict'; | ||
const common = require('../../common'); | ||
const assert = require('assert'); | ||
const test_async = require(`./build/${common.buildType}/test_async`); | ||
|
||
test_async(5, common.mustCall(function(err, val) { | ||
assert.strictEqual(err, null); | ||
assert.strictEqual(val, 10); | ||
process.nextTick(common.mustCall(function() {})); | ||
})); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
#include <node_api.h> | ||
#include "../common.h" | ||
|
||
#if defined _WIN32 | ||
#include <windows.h> | ||
#else | ||
#include <unistd.h> | ||
#endif | ||
|
||
typedef struct { | ||
int32_t _input; | ||
int32_t _output; | ||
napi_ref _callback; | ||
napi_async_work _request; | ||
} carrier; | ||
|
||
carrier the_carrier; | ||
|
||
struct AutoHandleScope { | ||
explicit AutoHandleScope(napi_env env) | ||
: _env(env), | ||
_scope(nullptr) { | ||
napi_open_handle_scope(_env, &_scope); | ||
} | ||
~AutoHandleScope() { | ||
napi_close_handle_scope(_env, _scope); | ||
} | ||
private: | ||
AutoHandleScope() { } | ||
|
||
napi_env _env; | ||
napi_handle_scope _scope; | ||
}; | ||
|
||
void Execute(napi_env env, void* data) { | ||
#if defined _WIN32 | ||
Sleep(1000); | ||
#else | ||
sleep(1); | ||
#endif | ||
carrier* c = static_cast<carrier*>(data); | ||
|
||
if (c != &the_carrier) { | ||
napi_throw_type_error(env, "Wrong data parameter to Execute."); | ||
return; | ||
} | ||
|
||
c->_output = c->_input * 2; | ||
} | ||
|
||
void Complete(napi_env env, napi_status status, void* data) { | ||
AutoHandleScope scope(env); | ||
carrier* c = static_cast<carrier*>(data); | ||
|
||
if (c != &the_carrier) { | ||
napi_throw_type_error(env, "Wrong data parameter to Complete."); | ||
return; | ||
} | ||
|
||
if (status != napi_ok) { | ||
napi_throw_type_error(env, "Execute callback failed."); | ||
return; | ||
} | ||
|
||
napi_value argv[2]; | ||
|
||
NAPI_CALL_RETURN_VOID(env, napi_get_null(env, &argv[0])); | ||
NAPI_CALL_RETURN_VOID(env, napi_create_number(env, c->_output, &argv[1])); | ||
napi_value callback; | ||
NAPI_CALL_RETURN_VOID(env, | ||
napi_get_reference_value(env, c->_callback, &callback)); | ||
napi_value global; | ||
NAPI_CALL_RETURN_VOID(env, napi_get_global(env, &global)); | ||
|
||
napi_value result; | ||
NAPI_CALL_RETURN_VOID(env, | ||
napi_call_function(env, global, callback, 2, argv, &result)); | ||
|
||
NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, c->_callback)); | ||
NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, c->_request)); | ||
} | ||
|
||
napi_value Test(napi_env env, napi_callback_info info) { | ||
size_t argc = 2; | ||
napi_value argv[2]; | ||
napi_value _this; | ||
void* data; | ||
NAPI_CALL(env, | ||
napi_get_cb_info(env, info, &argc, argv, &_this, &data)); | ||
NAPI_ASSERT(env, argc >= 2, "Not enough arguments, expected 2."); | ||
|
||
napi_valuetype t; | ||
NAPI_CALL(env, napi_typeof(env, argv[0], &t)); | ||
NAPI_ASSERT(env, t == napi_number, | ||
"Wrong first argument, integer expected."); | ||
NAPI_CALL(env, napi_typeof(env, argv[1], &t)); | ||
NAPI_ASSERT(env, t == napi_function, | ||
"Wrong second argument, function expected."); | ||
|
||
the_carrier._output = 0; | ||
|
||
NAPI_CALL(env, | ||
napi_get_value_int32(env, argv[0], &the_carrier._input)); | ||
NAPI_CALL(env, | ||
napi_create_reference(env, argv[1], 1, &the_carrier._callback)); | ||
NAPI_CALL(env, napi_create_async_work( | ||
env, Execute, Complete, &the_carrier, &the_carrier._request)); | ||
NAPI_CALL(env, | ||
napi_queue_async_work(env, the_carrier._request)); | ||
|
||
return nullptr; | ||
} | ||
|
||
void Init(napi_env env, napi_value exports, napi_value module, void* priv) { | ||
napi_value test; | ||
NAPI_CALL_RETURN_VOID(env, | ||
napi_create_function(env, "Test", Test, nullptr, &test)); | ||
NAPI_CALL_RETURN_VOID(env, | ||
napi_set_named_property(env, module, "exports", test)); | ||
} | ||
|
||
NAPI_MODULE(addon, Init) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was this moved?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I put the
napi_callback
typedef nearnapi_async_execute_callback
and other callback typedefs. Butnapi_async_complete_callback
takes anapi_status
so all the callbacks should be located down below the enum (at least). However,napi_property_descriptor
hasnapi_callback
members so it also had to move down in the file.