-
Notifications
You must be signed in to change notification settings - Fork 1
Improve extract args #198
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
Improve extract args #198
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,50 @@ | ||
| #pragma once | ||
|
|
||
| #include <node_api.h> | ||
| #include <string> | ||
| #include <tuple> | ||
|
|
||
| template<typename T> | ||
| T napi_extract_value(napi_env env, napi_value value); | ||
|
|
||
| template<> | ||
| inline std::wstring napi_extract_value<std::wstring>(napi_env env, napi_value value) { | ||
| size_t length; | ||
| napi_get_value_string_utf16(env, value, nullptr, 0, &length); | ||
|
|
||
| std::wstring result(length + 1, L'\0'); | ||
| size_t actualLength; | ||
| napi_get_value_string_utf16(env, value, reinterpret_cast<char16_t*>(result.data()), length + 1, &actualLength); | ||
| result.resize(actualLength); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| template<> | ||
| inline int64_t napi_extract_value<int64_t>(napi_env env, napi_value value) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the function we use to extract a int64 argument |
||
| int64_t result; | ||
| napi_get_value_int64(env, value, &result); | ||
| return result; | ||
| } | ||
|
|
||
| template<size_t N> | ||
| std::array<std::wstring, N> napi_extract_args(napi_env env, napi_callback_info info) { | ||
| template<> | ||
| inline bool napi_extract_value<bool>(napi_env env, napi_value value) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the function we use to extract a boolean argument |
||
| bool result; | ||
| napi_get_value_bool(env, value, &result); | ||
| return result; | ||
| } | ||
|
|
||
| template<typename... Types, std::size_t... Is> | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These two functions are parsed in compilation to extract all arguments based on the types we pass |
||
| inline std::tuple<Types...> napi_extract_args_impl(napi_env env, napi_value* argv, std::index_sequence<Is...>) { | ||
| return std::make_tuple(napi_extract_value<Types>(env, argv[Is])...); | ||
| } | ||
|
|
||
| template<typename... Types> | ||
| inline std::tuple<Types...> napi_extract_args(napi_env env, napi_callback_info info) { | ||
| constexpr size_t N = sizeof...(Types); | ||
| size_t argc = N; | ||
| napi_value argv[N]; | ||
| napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); | ||
|
|
||
| std::array<std::wstring, N> result; | ||
|
|
||
| for (size_t i = 0; i < N; ++i) { | ||
| size_t length; | ||
| napi_get_value_string_utf16(env, argv[i], nullptr, 0, &length); | ||
| result[i].resize(length); | ||
| napi_get_value_string_utf16(env, argv[i], reinterpret_cast<char16_t*>(result[i].data()), length + 1, nullptr); | ||
| } | ||
|
|
||
| return result; | ||
| return napi_extract_args_impl<Types...>(env, argv, std::make_index_sequence<N>{}); | ||
| } | ||
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.
This is the function we use to extract a string argument.