Skip to content

Commit a29fe00

Browse files
addaleaxbengl
authored andcommitted
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: nodejs#41757 Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent 508ac84 commit a29fe00

File tree

3 files changed

+75
-29
lines changed

3 files changed

+75
-29
lines changed

src/node_native_module_env.cc

+48-27
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,6 @@ using v8::SideEffectType;
2222
using v8::String;
2323
using v8::Value;
2424

25-
// TODO(joyeecheung): make these more general and put them into util.h
26-
Local<Set> ToJsSet(Local<Context> context, const std::set<std::string>& in) {
27-
Isolate* isolate = context->GetIsolate();
28-
Local<Set> out = Set::New(isolate);
29-
for (auto const& x : in) {
30-
out->Add(context, OneByteString(isolate, x.c_str(), x.size()))
31-
.ToLocalChecked();
32-
}
33-
return out;
34-
}
35-
3625
bool NativeModuleEnv::Add(const char* id, const UnionBytes& source) {
3726
return NativeModuleLoader::GetInstance()->Add(id, source);
3827
}
@@ -67,16 +56,26 @@ void NativeModuleEnv::GetModuleCategories(
6756
cannot_be_required.insert("trace_events");
6857
}
6958

70-
result
59+
Local<Value> cannot_be_required_js;
60+
Local<Value> can_be_required_js;
61+
62+
if (!ToV8Value(context, cannot_be_required).ToLocal(&cannot_be_required_js))
63+
return;
64+
if (result
7165
->Set(context,
7266
OneByteString(isolate, "cannotBeRequired"),
73-
ToJsSet(context, cannot_be_required))
74-
.FromJust();
75-
result
67+
cannot_be_required_js)
68+
.IsNothing())
69+
return;
70+
if (!ToV8Value(context, can_be_required).ToLocal(&can_be_required_js))
71+
return;
72+
if (result
7673
->Set(context,
7774
OneByteString(isolate, "canBeRequired"),
78-
ToJsSet(context, can_be_required))
79-
.FromJust();
75+
can_be_required_js)
76+
.IsNothing()) {
77+
return;
78+
}
8079
info.GetReturnValue().Set(result);
8180
}
8281

@@ -85,23 +84,45 @@ void NativeModuleEnv::GetCacheUsage(const FunctionCallbackInfo<Value>& args) {
8584
Isolate* isolate = env->isolate();
8685
Local<Context> context = env->context();
8786
Local<Object> result = Object::New(isolate);
88-
result
87+
88+
Local<Value> native_modules_with_cache_js;
89+
Local<Value> native_modules_without_cache_js;
90+
Local<Value> native_modules_in_snapshot_js;
91+
if (!ToV8Value(context, env->native_modules_with_cache)
92+
.ToLocal(&native_modules_with_cache_js)) {
93+
return;
94+
}
95+
if (result
8996
->Set(env->context(),
9097
OneByteString(isolate, "compiledWithCache"),
91-
ToJsSet(context, env->native_modules_with_cache))
92-
.FromJust();
93-
result
98+
native_modules_with_cache_js)
99+
.IsNothing()) {
100+
return;
101+
}
102+
103+
if (!ToV8Value(context, env->native_modules_without_cache)
104+
.ToLocal(&native_modules_without_cache_js)) {
105+
return;
106+
}
107+
if (result
94108
->Set(env->context(),
95109
OneByteString(isolate, "compiledWithoutCache"),
96-
ToJsSet(context, env->native_modules_without_cache))
97-
.FromJust();
110+
native_modules_without_cache_js)
111+
.IsNothing()) {
112+
return;
113+
}
98114

99-
result
115+
if (!ToV8Value(context, env->native_modules_in_snapshot)
116+
.ToLocal(&native_modules_without_cache_js)) {
117+
return;
118+
}
119+
if (result
100120
->Set(env->context(),
101121
OneByteString(isolate, "compiledInSnapshot"),
102-
ToV8Value(env->context(), env->native_modules_in_snapshot)
103-
.ToLocalChecked())
104-
.FromJust();
122+
native_modules_without_cache_js)
123+
.IsNothing()) {
124+
return;
125+
}
105126

106127
args.GetReturnValue().Set(result);
107128
}

src/util-inl.h

+20-1
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ inline char* UncheckedCalloc(size_t n) { return UncheckedCalloc<char>(n); }
404404
void ThrowErrStringTooLong(v8::Isolate* isolate);
405405

406406
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
407-
const std::string& str,
407+
std::string_view str,
408408
v8::Isolate* isolate) {
409409
if (isolate == nullptr) isolate = context->GetIsolate();
410410
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,
436436
return handle_scope.Escape(v8::Array::New(isolate, arr.out(), arr.length()));
437437
}
438438

439+
template <typename T>
440+
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
441+
const std::set<T>& set,
442+
v8::Isolate* isolate) {
443+
if (isolate == nullptr) isolate = context->GetIsolate();
444+
v8::Local<v8::Set> set_js = v8::Set::New(isolate);
445+
v8::HandleScope handle_scope(isolate);
446+
447+
for (const T& entry : set) {
448+
v8::Local<v8::Value> value;
449+
if (!ToV8Value(context, entry, isolate).ToLocal(&value))
450+
return {};
451+
if (set_js->Add(context, value).IsEmpty())
452+
return {};
453+
}
454+
455+
return set_js;
456+
}
457+
439458
template <typename T, typename U>
440459
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
441460
const std::unordered_map<T, U>& map,

src/util.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636
#include <limits>
3737
#include <memory>
3838
#include <string>
39+
#include <string_view>
3940
#include <type_traits>
41+
#include <set>
4042
#include <unordered_map>
4143
#include <utility>
4244
#include <vector>
@@ -644,7 +646,7 @@ using DeleteFnPtr = typename FunctionDeleter<T, function>::Pointer;
644646
std::vector<std::string> SplitString(const std::string& in, char delim);
645647

646648
inline v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
647-
const std::string& str,
649+
std::string_view str,
648650
v8::Isolate* isolate = nullptr);
649651
template <typename T, typename test_for_number =
650652
typename std::enable_if<std::numeric_limits<T>::is_specialized, bool>::type>
@@ -655,6 +657,10 @@ template <typename T>
655657
inline v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
656658
const std::vector<T>& vec,
657659
v8::Isolate* isolate = nullptr);
660+
template <typename T>
661+
inline v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
662+
const std::set<T>& set,
663+
v8::Isolate* isolate = nullptr);
658664
template <typename T, typename U>
659665
inline v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
660666
const std::unordered_map<T, U>& map,

0 commit comments

Comments
 (0)