Skip to content

Commit c9962a6

Browse files
committed
src: simplify URLPatternInit::ToJsObject by reducing boilerplate
1 parent 00f904f commit c9962a6

File tree

3 files changed

+25
-81
lines changed

3 files changed

+25
-81
lines changed

src/memory_tracker-inl.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,6 @@ void MemoryTracker::TrackField(const char* edge_name,
211211
TrackFieldWithSize(edge_name, value.size() * sizeof(T), "std::basic_string");
212212
}
213213

214-
template <typename T>
215-
void MemoryTracker::TrackField(const char* edge_name,
216-
const std::basic_string_view<T>& value,
217-
const char* node_name) {
218-
TrackFieldWithSize(
219-
edge_name, value.size() * sizeof(T), "std::basic_string_view");
220-
}
221-
222214
template <typename T>
223215
void MemoryTracker::TrackField(const char* edge_name,
224216
const v8::Eternal<T>& value,

src/memory_tracker.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,6 @@ class MemoryTracker {
199199
inline void TrackField(const char* edge_name,
200200
const std::basic_string<T>& value,
201201
const char* node_name = nullptr);
202-
template <typename T>
203-
inline void TrackField(const char* edge_name,
204-
const std::basic_string_view<T>& value,
205-
const char* node_name = nullptr);
206202
template <typename T,
207203
typename test_for_number = typename std::
208204
enable_if<std::numeric_limits<T>::is_specialized, bool>::type,

src/node_url_pattern.cc

Lines changed: 25 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,13 @@ URLPattern::URLPattern(Environment* env,
123123
}
124124

125125
void URLPattern::MemoryInfo(MemoryTracker* tracker) const {
126-
tracker->TrackField("protocol", url_pattern_.get_protocol());
127-
tracker->TrackField("username", url_pattern_.get_username());
128-
tracker->TrackField("password", url_pattern_.get_password());
129-
tracker->TrackField("hostname", url_pattern_.get_hostname());
130-
tracker->TrackField("pathname", url_pattern_.get_pathname());
131-
tracker->TrackField("search", url_pattern_.get_search());
132-
tracker->TrackField("hash", url_pattern_.get_hash());
126+
tracker->TrackFieldWithSize("protocol", url_pattern_.get_protocol().size());
127+
tracker->TrackFieldWithSize("username", url_pattern_.get_username().size());
128+
tracker->TrackFieldWithSize("password", url_pattern_.get_password().size());
129+
tracker->TrackFieldWithSize("hostname", url_pattern_.get_hostname().size());
130+
tracker->TrackFieldWithSize("pathname", url_pattern_.get_pathname().size());
131+
tracker->TrackFieldWithSize("search", url_pattern_.get_search().size());
132+
tracker->TrackFieldWithSize("hash", url_pattern_.get_hash().size());
133133
}
134134

135135
void URLPattern::New(const FunctionCallbackInfo<Value>& args) {
@@ -242,68 +242,24 @@ MaybeLocal<Value> URLPattern::URLPatternInit::ToJsObject(
242242
auto isolate = env->isolate();
243243
auto context = env->context();
244244
auto result = Object::New(isolate);
245-
if (init.protocol) {
246-
Local<Value> protocol;
247-
if (!ToV8Value(context, *init.protocol).ToLocal(&protocol) ||
248-
result->Set(context, env->protocol_string(), protocol).IsNothing()) {
249-
return {};
250-
}
251-
}
252-
if (init.username) {
253-
Local<Value> username;
254-
if (!ToV8Value(context, *init.username).ToLocal(&username) ||
255-
result->Set(context, env->username_string(), username).IsNothing()) {
256-
return {};
257-
}
258-
}
259-
if (init.password) {
260-
Local<Value> password;
261-
if (!ToV8Value(context, *init.password).ToLocal(&password) ||
262-
result->Set(context, env->password_string(), password).IsNothing()) {
263-
return {};
264-
}
265-
}
266-
if (init.hostname) {
267-
Local<Value> hostname;
268-
if (!ToV8Value(context, *init.hostname).ToLocal(&hostname) ||
269-
result->Set(context, env->hostname_string(), hostname).IsNothing()) {
270-
return {};
271-
}
272-
}
273-
if (init.port) {
274-
Local<Value> port;
275-
if (!ToV8Value(context, *init.port).ToLocal(&port) ||
276-
result->Set(context, env->port_string(), port).IsNothing()) {
277-
return {};
278-
}
279-
}
280-
if (init.pathname) {
281-
Local<Value> pathname;
282-
if (!ToV8Value(context, *init.pathname).ToLocal(&pathname) ||
283-
result->Set(context, env->pathname_string(), pathname).IsNothing()) {
284-
return {};
285-
}
286-
}
287-
if (init.search) {
288-
Local<Value> search;
289-
if (!ToV8Value(context, *init.search).ToLocal(&search) ||
290-
result->Set(context, env->search_string(), search).IsNothing()) {
291-
return {};
292-
}
293-
}
294-
if (init.hash) {
295-
Local<Value> hash;
296-
if (!ToV8Value(context, *init.hash).ToLocal(&hash) ||
297-
result->Set(context, env->hash_string(), hash).IsNothing()) {
298-
return {};
299-
}
300-
}
301-
if (init.base_url) {
302-
Local<Value> base_url;
303-
if (!ToV8Value(context, *init.base_url).ToLocal(&base_url) ||
304-
result->Set(context, env->base_url_string(), base_url).IsNothing()) {
305-
return {};
306-
}
245+
246+
const auto trySet = [&](auto name, const std::optional<std::string>& val) {
247+
if (!val.has_value()) return true;
248+
Local<Value> temp;
249+
return ToV8Value(context, *val).ToLocal(&temp) &&
250+
result->Set(context, name, temp).IsJust();
251+
};
252+
253+
if (!trySet(env->protocol_string(), init.protocol) ||
254+
!trySet(env->username_string(), init.username) ||
255+
!trySet(env->password_string(), init.password) ||
256+
!trySet(env->hostname_string(), init.hostname) ||
257+
!trySet(env->port_string(), init.port) ||
258+
!trySet(env->pathname_string(), init.pathname) ||
259+
!trySet(env->search_string(), init.search) ||
260+
!trySet(env->hash_string(), init.hash) ||
261+
!trySet(env->base_url_string(), init.base_url)) {
262+
return {};
307263
}
308264
return result;
309265
}

0 commit comments

Comments
 (0)