Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: merge ToJsSet into ToV8Value #41757

Merged
merged 2 commits into from
Feb 13, 2022
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
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)) {
Comment on lines +91 to +92
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if allowed by the linter, but the . should not align with the ! IMO.

Suggested change
if (!ToV8Value(context, env->native_modules_with_cache)
.ToLocal(&native_modules_with_cache_js)) {
if (!ToV8Value(context, env->native_modules_with_cache)
.ToLocal(&native_modules_with_cache_js)) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if allowed by the linter

The c++ linter doesn't care about indentations for now.

but the . should not align with the ! IMO.

That would be 5 spaces instead of what's suggested in the style guide, i.e., 4 - https://github.com/nodejs/node/blob/HEAD/doc/contributing/cpp-style-guide.md#4-spaces-of-indentation-for-statement-continuations. Should the doc be updated with more info about this special case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we’re following the 4-spaces rule pretty universally, and I wouldn’t change these kinds of cases to be special in that regard.

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)) {
Comment on lines +103 to +104
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!ToV8Value(context, env->native_modules_without_cache)
.ToLocal(&native_modules_without_cache_js)) {
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)) {
Comment on lines +115 to +116
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!ToV8Value(context, env->native_modules_in_snapshot)
.ToLocal(&native_modules_without_cache_js)) {
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,
RaisinTen marked this conversation as resolved.
Show resolved Hide resolved
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