diff options
author | Anna Henningsen <anna@addaleax.net> | 2022-02-14 02:44:30 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-14 02:44:30 +0300 |
commit | 34be1af5e17195dd9ce127e1daa7ab2bafd2df84 (patch) | |
tree | 7947b63af699a5ba9ef7ae841c54c7fb1d3add2d /src/util-inl.h | |
parent | 60b8e795993cd6fcb7dc70d696a6ebe7aa0ef3d4 (diff) |
src: merge ToJsSet into ToV8Value
This addresses a `TODO` comment, and makes use of the opportunity
to also clean up our `MaybeLocal` handling in this area and
start accepting `std::string_view` where we accept `std::string`.
PR-URL: https://github.com/nodejs/node/pull/41757
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Diffstat (limited to 'src/util-inl.h')
-rw-r--r-- | src/util-inl.h | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/src/util-inl.h b/src/util-inl.h index b860c7008a3..32789361852 100644 --- a/src/util-inl.h +++ b/src/util-inl.h @@ -404,7 +404,7 @@ inline char* UncheckedCalloc(size_t n) { return UncheckedCalloc<char>(n); } void ThrowErrStringTooLong(v8::Isolate* isolate); v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context, - const std::string& str, + std::string_view str, v8::Isolate* isolate) { if (isolate == nullptr) isolate = context->GetIsolate(); if (UNLIKELY(str.size() >= static_cast<size_t>(v8::String::kMaxLength))) { @@ -436,6 +436,25 @@ v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context, return handle_scope.Escape(v8::Array::New(isolate, arr.out(), arr.length())); } +template <typename T> +v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context, + const std::set<T>& set, + v8::Isolate* isolate) { + if (isolate == nullptr) isolate = context->GetIsolate(); + v8::Local<v8::Set> set_js = v8::Set::New(isolate); + v8::HandleScope handle_scope(isolate); + + for (const T& entry : set) { + v8::Local<v8::Value> value; + if (!ToV8Value(context, entry, isolate).ToLocal(&value)) + return {}; + if (set_js->Add(context, value).IsEmpty()) + return {}; + } + + return set_js; +} + template <typename T, typename U> v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context, const std::unordered_map<T, U>& map, |