Skip to content

Commit

Permalink
src: merge ToJsSet into ToV8Value
Browse files Browse the repository at this point in the history
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: #41757
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
  • Loading branch information
addaleax authored and bengl committed Feb 21, 2022
1 parent 1b7359d commit f5f790b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 29 deletions.
75 changes: 48 additions & 27 deletions src/node_native_module_env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,6 @@ using v8::SideEffectType;
using v8::String;
using v8::Value;

// TODO(joyeecheung): make these more general and put them into util.h
Local<Set> ToJsSet(Local<Context> context, const std::set<std::string>& in) {
Isolate* isolate = context->GetIsolate();
Local<Set> out = Set::New(isolate);
for (auto const& x : in) {
out->Add(context, OneByteString(isolate, x.c_str(), x.size()))
.ToLocalChecked();
}
return out;
}

bool NativeModuleEnv::Add(const char* id, const UnionBytes& source) {
return NativeModuleLoader::GetInstance()->Add(id, source);
}
Expand Down Expand Up @@ -67,16 +56,26 @@ void NativeModuleEnv::GetModuleCategories(
cannot_be_required.insert("trace_events");
}

result
Local<Value> cannot_be_required_js;
Local<Value> can_be_required_js;

if (!ToV8Value(context, cannot_be_required).ToLocal(&cannot_be_required_js))
return;
if (result
->Set(context,
OneByteString(isolate, "cannotBeRequired"),
ToJsSet(context, cannot_be_required))
.FromJust();
result
cannot_be_required_js)
.IsNothing())
return;
if (!ToV8Value(context, can_be_required).ToLocal(&can_be_required_js))
return;
if (result
->Set(context,
OneByteString(isolate, "canBeRequired"),
ToJsSet(context, can_be_required))
.FromJust();
can_be_required_js)
.IsNothing()) {
return;
}
info.GetReturnValue().Set(result);
}

Expand All @@ -85,23 +84,45 @@ void NativeModuleEnv::GetCacheUsage(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = env->isolate();
Local<Context> context = env->context();
Local<Object> result = Object::New(isolate);
result

Local<Value> native_modules_with_cache_js;
Local<Value> native_modules_without_cache_js;
Local<Value> native_modules_in_snapshot_js;
if (!ToV8Value(context, env->native_modules_with_cache)
.ToLocal(&native_modules_with_cache_js)) {
return;
}
if (result
->Set(env->context(),
OneByteString(isolate, "compiledWithCache"),
ToJsSet(context, env->native_modules_with_cache))
.FromJust();
result
native_modules_with_cache_js)
.IsNothing()) {
return;
}

if (!ToV8Value(context, env->native_modules_without_cache)
.ToLocal(&native_modules_without_cache_js)) {
return;
}
if (result
->Set(env->context(),
OneByteString(isolate, "compiledWithoutCache"),
ToJsSet(context, env->native_modules_without_cache))
.FromJust();
native_modules_without_cache_js)
.IsNothing()) {
return;
}

result
if (!ToV8Value(context, env->native_modules_in_snapshot)
.ToLocal(&native_modules_without_cache_js)) {
return;
}
if (result
->Set(env->context(),
OneByteString(isolate, "compiledInSnapshot"),
ToV8Value(env->context(), env->native_modules_in_snapshot)
.ToLocalChecked())
.FromJust();
native_modules_without_cache_js)
.IsNothing()) {
return;
}

args.GetReturnValue().Set(result);
}
Expand Down
21 changes: 20 additions & 1 deletion src/util-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
#include <limits>
#include <memory>
#include <string>
#include <string_view>
#include <type_traits>
#include <set>
#include <unordered_map>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -644,7 +646,7 @@ using DeleteFnPtr = typename FunctionDeleter<T, function>::Pointer;
std::vector<std::string> SplitString(const std::string& in, char delim);

inline v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
const std::string& str,
std::string_view str,
v8::Isolate* isolate = nullptr);
template <typename T, typename test_for_number =
typename std::enable_if<std::numeric_limits<T>::is_specialized, bool>::type>
Expand All @@ -655,6 +657,10 @@ template <typename T>
inline v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
const std::vector<T>& vec,
v8::Isolate* isolate = nullptr);
template <typename T>
inline v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
const std::set<T>& set,
v8::Isolate* isolate = nullptr);
template <typename T, typename U>
inline v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
const std::unordered_map<T, U>& map,
Expand Down

0 comments on commit f5f790b

Please sign in to comment.