diff --git a/src/node_url_pattern.cc b/src/node_url_pattern.cc index 1e41f374f8e57b..47a5fb2ced4387 100644 --- a/src/node_url_pattern.cc +++ b/src/node_url_pattern.cc @@ -130,6 +130,8 @@ void URLPattern::MemoryInfo(MemoryTracker* tracker) const { tracker->TrackField("pathname", url_pattern_.get_pathname()); tracker->TrackField("search", url_pattern_.get_search()); tracker->TrackField("hash", url_pattern_.get_hash()); + tracker->TrackFieldWithSize( + "pattern", sizeof(url_pattern_), "ada:url_pattern"); } void URLPattern::New(const FunctionCallbackInfo& args) { @@ -242,68 +244,24 @@ MaybeLocal URLPattern::URLPatternInit::ToJsObject( auto isolate = env->isolate(); auto context = env->context(); auto result = Object::New(isolate); - if (init.protocol) { - Local protocol; - if (!ToV8Value(context, *init.protocol).ToLocal(&protocol) || - result->Set(context, env->protocol_string(), protocol).IsNothing()) { - return {}; - } - } - if (init.username) { - Local username; - if (!ToV8Value(context, *init.username).ToLocal(&username) || - result->Set(context, env->username_string(), username).IsNothing()) { - return {}; - } - } - if (init.password) { - Local password; - if (!ToV8Value(context, *init.password).ToLocal(&password) || - result->Set(context, env->password_string(), password).IsNothing()) { - return {}; - } - } - if (init.hostname) { - Local hostname; - if (!ToV8Value(context, *init.hostname).ToLocal(&hostname) || - result->Set(context, env->hostname_string(), hostname).IsNothing()) { - return {}; - } - } - if (init.port) { - Local port; - if (!ToV8Value(context, *init.port).ToLocal(&port) || - result->Set(context, env->port_string(), port).IsNothing()) { - return {}; - } - } - if (init.pathname) { - Local pathname; - if (!ToV8Value(context, *init.pathname).ToLocal(&pathname) || - result->Set(context, env->pathname_string(), pathname).IsNothing()) { - return {}; - } - } - if (init.search) { - Local search; - if (!ToV8Value(context, *init.search).ToLocal(&search) || - result->Set(context, env->search_string(), search).IsNothing()) { - return {}; - } - } - if (init.hash) { - Local hash; - if (!ToV8Value(context, *init.hash).ToLocal(&hash) || - result->Set(context, env->hash_string(), hash).IsNothing()) { - return {}; - } - } - if (init.base_url) { - Local base_url; - if (!ToV8Value(context, *init.base_url).ToLocal(&base_url) || - result->Set(context, env->base_url_string(), base_url).IsNothing()) { - return {}; - } + + const auto trySet = [&](auto name, const std::optional& val) { + if (!val.has_value()) return true; + Local temp; + return ToV8Value(context, *val).ToLocal(&temp) && + result->Set(context, name, temp).IsJust(); + }; + + if (!trySet(env->protocol_string(), init.protocol) || + !trySet(env->username_string(), init.username) || + !trySet(env->password_string(), init.password) || + !trySet(env->hostname_string(), init.hostname) || + !trySet(env->port_string(), init.port) || + !trySet(env->pathname_string(), init.pathname) || + !trySet(env->search_string(), init.search) || + !trySet(env->hash_string(), init.hash) || + !trySet(env->base_url_string(), init.base_url)) { + return {}; } return result; }