-
Notifications
You must be signed in to change notification settings - Fork 4
Implementation of napi_async_init, napi_async_destroy, napi_make_callback
#219
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
Open
paradowstack
wants to merge
5
commits into
main
Choose a base branch
from
kp/make_callback
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
aa2faf0
feat: added support to three async functions
paradowstack c54398c
chore: adopt node make_callback tests
paradowstack f54c866
chore: changeset
paradowstack a0baa54
refactor: cleanup
paradowstack 8ffa400
fix: not releasing the context + linter
paradowstack 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"react-native-node-api": minor | ||
--- | ||
|
||
Provided implementation of napi_async_init, napi_async_destroy, napi_make_callback |
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
15 changes: 15 additions & 0 deletions
15
packages/node-addon-examples/tests/make_callback/CMakeLists.txt
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,15 @@ | ||
cmake_minimum_required(VERSION 3.15) | ||
project(tests-make_callback) | ||
|
||
add_compile_definitions(NAPI_VERSION=8) | ||
|
||
add_library(addon SHARED addon.c ${CMAKE_JS_SRC}) | ||
set_target_properties(addon PROPERTIES PREFIX "" SUFFIX ".node") | ||
target_include_directories(addon PRIVATE ${CMAKE_JS_INC}) | ||
target_link_libraries(addon PRIVATE ${CMAKE_JS_LIB}) | ||
target_compile_features(addon PRIVATE cxx_std_17) | ||
|
||
if(MSVC AND CMAKE_JS_NODELIB_DEF AND CMAKE_JS_NODELIB_TARGET) | ||
# Generate node.lib | ||
execute_process(COMMAND ${CMAKE_AR} /def:${CMAKE_JS_NODELIB_DEF} /out:${CMAKE_JS_NODELIB_TARGET} ${CMAKE_STATIC_LINKER_FLAGS}) | ||
endif() |
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,68 @@ | ||
#include <assert.h> | ||
#include <node_api.h> | ||
#include <stdio.h> | ||
#include "../RuntimeNodeApiTestsCommon.h" | ||
|
||
#define MAX_ARGUMENTS 10 | ||
#define RESERVED_ARGS 3 | ||
|
||
static napi_value MakeCallback(napi_env env, napi_callback_info info) { | ||
size_t argc = MAX_ARGUMENTS; | ||
size_t n; | ||
napi_value args[MAX_ARGUMENTS]; | ||
// NOLINTNEXTLINE (readability/null_usage) | ||
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); | ||
|
||
NODE_API_ASSERT(env, argc > 0, "Wrong number of arguments"); | ||
printf("Make callback called with %zu arguments\n", argc); | ||
|
||
napi_value resource = args[0]; | ||
napi_value recv = args[1]; | ||
napi_value func = args[2]; | ||
|
||
napi_value argv[MAX_ARGUMENTS - RESERVED_ARGS]; | ||
for (n = RESERVED_ARGS; n < argc; n += 1) { | ||
argv[n - RESERVED_ARGS] = args[n]; | ||
} | ||
|
||
napi_valuetype func_type; | ||
|
||
NODE_API_CALL(env, napi_typeof(env, func, &func_type)); | ||
|
||
napi_value resource_name; | ||
NODE_API_CALL(env, | ||
napi_create_string_utf8(env, "test", NAPI_AUTO_LENGTH, &resource_name)); | ||
|
||
napi_async_context context; | ||
NODE_API_CALL(env, napi_async_init(env, resource, resource_name, &context)); | ||
|
||
napi_value result; | ||
if (func_type == napi_function) { | ||
NODE_API_CALL(env, | ||
napi_make_callback( | ||
env, context, recv, func, argc - RESERVED_ARGS, argv, &result)); | ||
} else { | ||
NODE_API_ASSERT(env, false, "Unexpected argument type"); | ||
} | ||
|
||
NODE_API_CALL(env, napi_async_destroy(env, context)); | ||
|
||
return result; | ||
} | ||
|
||
static napi_value Init(napi_env env, napi_value exports) { | ||
napi_value fn; | ||
NODE_API_CALL(env, | ||
napi_create_function( | ||
// NOLINTNEXTLINE (readability/null_usage) | ||
env, | ||
NULL, | ||
NAPI_AUTO_LENGTH, | ||
MakeCallback, | ||
NULL, | ||
&fn)); | ||
NODE_API_CALL(env, napi_set_named_property(env, exports, "makeCallback", fn)); | ||
return exports; | ||
} | ||
|
||
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.