Skip to content

Commit 50b4c96

Browse files
committed
fix: wrap std::vector<unsigned char> as uint8array in js
1 parent d1cf49a commit 50b4c96

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/utilities.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ std::optional<std::string> maybeNonemptyString(Napi::Value x, const std::string&
8888
throw std::invalid_argument{"maybeNonemptyString with invalid type, called from " + identifier};
8989
}
9090

91-
// Converts to a std::span<const unsigned char> that views directly into the Uint8Array data of `x`. Throws if x is
92-
// not a Uint8Array. The view must not be used beyond the lifetime of `x`.
91+
// Converts to a std::span<const unsigned char> that views directly into the Uint8Array data of `x`.
92+
// Throws if x is not a Uint8Array. The view must not be used beyond the lifetime of `x`.
9393
std::span<const unsigned char> toCppBufferView(Napi::Value x, const std::string& identifier) {
9494
if (x.IsNull() || x.IsUndefined())
9595
throw std::invalid_argument(
@@ -106,7 +106,8 @@ std::vector<unsigned char> toCppBuffer(Napi::Value x, const std::string& identif
106106
return session::to_vector(toCppBufferView(x, std::move(identifier)));
107107
}
108108

109-
std::optional<std::vector<unsigned char>> maybeNonemptyBuffer(Napi::Value x, const std::string& identifier) {
109+
std::optional<std::vector<unsigned char>> maybeNonemptyBuffer(
110+
Napi::Value x, const std::string& identifier) {
110111
if (x.IsNull() || x.IsUndefined())
111112
return std::nullopt;
112113

src/utilities.hpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ std::optional<std::string> maybeNonemptyString(Napi::Value x, const std::string&
6868

6969
// If the object is null/undef/empty returns nullopt, otherwise if a Uint8Array returns a
7070
// std::vector<unsigned char> of the value. Throws if something else.
71-
std::optional<std::vector<unsigned char>> maybeNonemptyBuffer(Napi::Value x, const std::string& identifier);
71+
std::optional<std::vector<unsigned char>> maybeNonemptyBuffer(
72+
Napi::Value x, const std::string& identifier);
7273

7374
// Implementation struct of toJs(); we add specializations of this for any C++ types we want to be
7475
// able to convert into JS types.
@@ -121,14 +122,24 @@ struct toJs_impl<T, std::enable_if_t<std::is_convertible_v<T, std::string_view>>
121122
};
122123

123124
template <typename T>
124-
struct toJs_impl<T, std::enable_if_t<
125-
std::is_convertible_v<T, std::span<const unsigned char>> &&
126-
!std::is_same_v<std::remove_cv_t<T>, std::vector<unsigned char>>>> {
125+
struct toJs_impl<
126+
T,
127+
std::enable_if_t<
128+
std::is_convertible_v<T, std::span<const unsigned char>> &&
129+
!std::is_same_v<std::remove_cv_t<T>, std::vector<unsigned char>>>> {
127130
auto operator()(const Napi::Env& env, std::span<const unsigned char> b) const {
128131
return Napi::Buffer<uint8_t>::Copy(env, b.data(), b.size());
129132
}
130133
};
131134

135+
// this wrap std::vector<unsigned char> to Uint8array in the js world
136+
template <>
137+
struct toJs_impl<std::vector<unsigned char>> {
138+
auto operator()(const Napi::Env& env, std::vector<unsigned char> b) const {
139+
return Napi::Buffer<uint8_t>::Copy(env, b.data(), b.size());
140+
}
141+
};
142+
132143
template <typename T>
133144
struct toJs_impl<T, std::enable_if_t<std::is_base_of_v<Napi::Value, T>>> {
134145
auto operator()(const Napi::Env& env, const T& val) { return val; }

0 commit comments

Comments
 (0)