Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified dist/addon.node
Binary file not shown.
54 changes: 42 additions & 12 deletions include/napi_helpers/napi_extract_args.h
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) {
Copy link
Contributor Author

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.

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) {
Copy link
Contributor Author

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 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) {
Copy link
Contributor Author

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 boolean argument

bool result;
napi_get_value_bool(env, value, &result);
return result;
}

template<typename... Types, std::size_t... Is>
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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>{});
}
2 changes: 1 addition & 1 deletion native-src/virtual_drive/convert_to_placeholder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "Placeholders.h"

napi_value convert_to_placeholder_impl(napi_env env, napi_callback_info info) {
auto [path, serverIdentity] = napi_extract_args<2>(env, info);
auto [path, serverIdentity] = napi_extract_args<std::wstring, std::wstring>(env, info);

std::wstring wPath(path.begin(), path.end());
std::wstring wServerIdentity(serverIdentity.begin(), serverIdentity.end());
Expand Down
2 changes: 1 addition & 1 deletion native-src/virtual_drive/dehydrate_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "stdafx.h"

napi_value dehydrate_file(napi_env env, napi_callback_info info) {
auto [rawPath] = napi_extract_args<1>(env, info);
auto [rawPath] = napi_extract_args<std::wstring>(env, info);
const wchar_t* path = rawPath.c_str();

DWORD attrib = GetFileAttributesW(path);
Expand Down
2 changes: 1 addition & 1 deletion native-src/virtual_drive/disconnect_sync_root.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "SyncRoot.h"

napi_value disconnect_sync_root(napi_env env, napi_callback_info info) {
auto [syncRootPath] = napi_extract_args<1>(env, info);
auto [syncRootPath] = napi_extract_args<std::wstring>(env, info);

HRESULT result = SyncRoot::DisconnectSyncRoot(syncRootPath.c_str());

Expand Down
2 changes: 1 addition & 1 deletion native-src/virtual_drive/get_file_identity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "Placeholders.h"

napi_value get_file_identity_impl(napi_env env, napi_callback_info info) {
auto [path] = napi_extract_args<1>(env, info);
auto [path] = napi_extract_args<std::wstring>(env, info);

std::string fileIdentity = Placeholders::GetFileIdentity(path);
fileIdentity.erase(std::remove(fileIdentity.begin(), fileIdentity.end(), '\0'), fileIdentity.end());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "Placeholders.h"

napi_value get_placeholder_state_wrapper(napi_env env, napi_callback_info info) {
auto [path] = napi_extract_args<1>(env, info);
auto [path] = napi_extract_args<std::wstring>(env, info);

FileState state = Placeholders::GetPlaceholderInfo(path);

Expand Down
2 changes: 1 addition & 1 deletion native-src/virtual_drive/hydrate_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void complete_work(napi_env env, napi_status status, void* data) {
}

napi_value hydrate_file_impl(napi_env env, napi_callback_info info) {
auto [path] = napi_extract_args<1>(env, info);
auto [path] = napi_extract_args<std::wstring>(env, info);

// Create promise
napi_deferred deferred;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#include "register_sync_root.h"

napi_value register_sync_root_wrapper(napi_env env, napi_callback_info info) {
auto [syncRootPath, providerName, providerVersion, providerId, logoPath] = napi_extract_args<5>(env, info);
auto [syncRootPath, providerName, providerVersion, providerId, logoPath] =
napi_extract_args<std::wstring, std::wstring, std::wstring, std::wstring, std::wstring>(env, info);

register_sync_root(syncRootPath.c_str(), providerName.c_str(), providerVersion.c_str(), providerId.c_str(), logoPath.c_str());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "stdafx.h"

napi_value unregister_sync_root_wrapper(napi_env env, napi_callback_info info) {
auto [providerId] = napi_extract_args<1>(env, info);
auto [providerId] = napi_extract_args<std::wstring>(env, info);

winrt::StorageProviderSyncRootManager::Unregister(providerId);

Expand Down
Loading