src: define urlpattern components using a macro

PR-URL: https://github.com/nodejs/node/pull/57452
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
JonasBa 2025-03-13 21:16:13 -04:00 committed by Node.js GitHub Bot
parent 26c4851821
commit d5ac3e3803
2 changed files with 52 additions and 187 deletions

View File

@ -462,48 +462,17 @@ URLPattern::URLPatternOptions::FromJsObject(Environment* env,
// by returning std::nullopt.
return std::nullopt;
}
return options;
}
MaybeLocal<Value> URLPattern::Hash() const {
auto context = env()->context();
return ToV8Value(context, url_pattern_.get_hash());
}
MaybeLocal<Value> URLPattern::Hostname() const {
auto context = env()->context();
return ToV8Value(context, url_pattern_.get_hostname());
}
MaybeLocal<Value> URLPattern::Password() const {
auto context = env()->context();
return ToV8Value(context, url_pattern_.get_password());
}
MaybeLocal<Value> URLPattern::Pathname() const {
auto context = env()->context();
return ToV8Value(context, url_pattern_.get_pathname());
}
MaybeLocal<Value> URLPattern::Port() const {
auto context = env()->context();
return ToV8Value(context, url_pattern_.get_port());
}
MaybeLocal<Value> URLPattern::Protocol() const {
auto context = env()->context();
return ToV8Value(context, url_pattern_.get_protocol());
}
MaybeLocal<Value> URLPattern::Search() const {
auto context = env()->context();
return ToV8Value(context, url_pattern_.get_search());
}
MaybeLocal<Value> URLPattern::Username() const {
auto context = env()->context();
return ToV8Value(context, url_pattern_.get_username());
}
#define URL_PATTERN_COMPONENT_GETTERS(uppercase_name, lowercase_name) \
MaybeLocal<Value> URLPattern::uppercase_name() const { \
auto context = env()->context(); \
return ToV8Value(context, url_pattern_.get_##lowercase_name()); \
}
URL_PATTERN_COMPONENTS(URL_PATTERN_COMPONENT_GETTERS)
#undef URL_PATTERN_COMPONENT_GETTERS
bool URLPattern::HasRegExpGroups() const {
return url_pattern_.has_regexp_groups();
@ -616,77 +585,17 @@ void URLPattern::Test(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(url_pattern->Test(env, input, baseURL_opt));
}
void URLPattern::Protocol(const FunctionCallbackInfo<Value>& info) {
URLPattern* url_pattern;
ASSIGN_OR_RETURN_UNWRAP(&url_pattern, info.This());
Local<Value> result;
if (url_pattern->Protocol().ToLocal(&result)) {
info.GetReturnValue().Set(result);
#define URL_PATTERN_COMPONENT_GETTERS(uppercase_name, lowercase_name) \
void URLPattern::uppercase_name(const FunctionCallbackInfo<Value>& info) { \
URLPattern* url_pattern; \
ASSIGN_OR_RETURN_UNWRAP(&url_pattern, info.This()); \
Local<Value> result; \
if (url_pattern->uppercase_name().ToLocal(&result)) { \
info.GetReturnValue().Set(result); \
} \
}
}
void URLPattern::Username(const FunctionCallbackInfo<Value>& info) {
URLPattern* url_pattern;
ASSIGN_OR_RETURN_UNWRAP(&url_pattern, info.This());
Local<Value> result;
if (url_pattern->Username().ToLocal(&result)) {
info.GetReturnValue().Set(result);
}
}
void URLPattern::Password(const FunctionCallbackInfo<Value>& info) {
URLPattern* url_pattern;
ASSIGN_OR_RETURN_UNWRAP(&url_pattern, info.This());
Local<Value> result;
if (url_pattern->Password().ToLocal(&result)) {
info.GetReturnValue().Set(result);
}
}
void URLPattern::Hostname(const FunctionCallbackInfo<Value>& info) {
URLPattern* url_pattern;
ASSIGN_OR_RETURN_UNWRAP(&url_pattern, info.This());
Local<Value> result;
if (url_pattern->Hostname().ToLocal(&result)) {
info.GetReturnValue().Set(result);
}
}
void URLPattern::Port(const FunctionCallbackInfo<Value>& info) {
URLPattern* url_pattern;
ASSIGN_OR_RETURN_UNWRAP(&url_pattern, info.This());
Local<Value> result;
if (url_pattern->Port().ToLocal(&result)) {
info.GetReturnValue().Set(result);
}
}
void URLPattern::Pathname(const FunctionCallbackInfo<Value>& info) {
URLPattern* url_pattern;
ASSIGN_OR_RETURN_UNWRAP(&url_pattern, info.This());
Local<Value> result;
if (url_pattern->Pathname().ToLocal(&result)) {
info.GetReturnValue().Set(result);
}
}
void URLPattern::Search(const FunctionCallbackInfo<Value>& info) {
URLPattern* url_pattern;
ASSIGN_OR_RETURN_UNWRAP(&url_pattern, info.This());
Local<Value> result;
if (url_pattern->Search().ToLocal(&result)) {
info.GetReturnValue().Set(result);
}
}
void URLPattern::Hash(const FunctionCallbackInfo<Value>& info) {
URLPattern* url_pattern;
ASSIGN_OR_RETURN_UNWRAP(&url_pattern, info.This());
Local<Value> result;
if (url_pattern->Hash().ToLocal(&result)) {
info.GetReturnValue().Set(result);
}
}
URL_PATTERN_COMPONENTS(URL_PATTERN_COMPONENT_GETTERS)
#undef URL_PATTERN_COMPONENT_GETTERS
void URLPattern::HasRegexpGroups(const FunctionCallbackInfo<Value>& info) {
URLPattern* url_pattern;
@ -696,14 +605,10 @@ void URLPattern::HasRegexpGroups(const FunctionCallbackInfo<Value>& info) {
static void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(URLPattern::New);
registry->Register(URLPattern::Protocol);
registry->Register(URLPattern::Username);
registry->Register(URLPattern::Password);
registry->Register(URLPattern::Hostname);
registry->Register(URLPattern::Port);
registry->Register(URLPattern::Pathname);
registry->Register(URLPattern::Search);
registry->Register(URLPattern::Hash);
#define URL_PATTERN_COMPONENT_GETTERS(uppercase_name, _) \
registry->Register(URLPattern::uppercase_name);
URL_PATTERN_COMPONENTS(URL_PATTERN_COMPONENT_GETTERS)
#undef URL_PATTERN_COMPONENT_GETTERS
registry->Register(URLPattern::HasRegexpGroups);
registry->Register(URLPattern::Exec);
registry->Register(URLPattern::Test);
@ -726,61 +631,18 @@ static void Initialize(Local<Object> target,
auto signature = Signature::New(isolate, ctor_tmpl);
instance_template->SetInternalFieldCount(URLPattern::kInternalFieldCount);
prototype_template->SetAccessorProperty(
env->protocol_string(),
FunctionTemplate::New(
isolate, URLPattern::Protocol, Local<Value>(), signature),
Local<FunctionTemplate>(),
attributes);
prototype_template->SetAccessorProperty(
env->username_string(),
FunctionTemplate::New(
isolate, URLPattern::Username, Local<Value>(), signature),
Local<FunctionTemplate>(),
attributes);
prototype_template->SetAccessorProperty(
env->password_string(),
FunctionTemplate::New(
isolate, URLPattern::Password, Local<Value>(), signature),
Local<FunctionTemplate>(),
attributes);
prototype_template->SetAccessorProperty(
env->hostname_string(),
FunctionTemplate::New(
isolate, URLPattern::Hostname, Local<Value>(), signature),
Local<FunctionTemplate>(),
attributes);
prototype_template->SetAccessorProperty(
env->port_string(),
FunctionTemplate::New(
isolate, URLPattern::Port, Local<Value>(), signature),
Local<FunctionTemplate>(),
attributes);
prototype_template->SetAccessorProperty(
env->pathname_string(),
FunctionTemplate::New(
isolate, URLPattern::Pathname, Local<Value>(), signature),
Local<FunctionTemplate>(),
attributes);
prototype_template->SetAccessorProperty(
env->search_string(),
FunctionTemplate::New(
isolate, URLPattern::Search, Local<Value>(), signature),
Local<FunctionTemplate>(),
attributes);
prototype_template->SetAccessorProperty(
env->hash_string(),
FunctionTemplate::New(
isolate, URLPattern::Hash, Local<Value>(), signature),
Local<FunctionTemplate>(),
#define ENV_GETTER(lowercase_name) env->lowercase_name##_string()
#define URL_PATTERN_COMPONENT_GETTERS(uppercase_name, lowercase_name) \
prototype_template->SetAccessorProperty( \
ENV_GETTER(lowercase_name), \
FunctionTemplate::New( \
isolate, URLPattern::uppercase_name, Local<Value>(), signature), \
Local<FunctionTemplate>(), \
attributes);
URL_PATTERN_COMPONENTS(URL_PATTERN_COMPONENT_GETTERS)
#undef URL_PATTERN_COMPONENT_GETTERS
#undef ENV_GETTER
prototype_template->SetAccessorProperty(
env->has_regexp_groups_string(),

View File

@ -14,6 +14,16 @@
namespace node::url_pattern {
#define URL_PATTERN_COMPONENTS(V) \
V(Protocol, protocol) \
V(Username, username) \
V(Password, password) \
V(Hostname, hostname) \
V(Port, port) \
V(Pathname, pathname) \
V(Search, search) \
V(Hash, hash)
// By default, ada::url_pattern doesn't ship with any regex library.
// Ada has a std::regex implementation, but it is considered unsafe and does
// not have a fully compliant ecmascript syntax support. Therefore, Ada
@ -42,15 +52,12 @@ class URLPattern : public BaseObject {
// - Functions
static void Exec(const v8::FunctionCallbackInfo<v8::Value>& info);
static void Test(const v8::FunctionCallbackInfo<v8::Value>& info);
// - Getters
static void Hash(const v8::FunctionCallbackInfo<v8::Value>& info);
static void Hostname(const v8::FunctionCallbackInfo<v8::Value>& info);
static void Password(const v8::FunctionCallbackInfo<v8::Value>& info);
static void Pathname(const v8::FunctionCallbackInfo<v8::Value>& info);
static void Port(const v8::FunctionCallbackInfo<v8::Value>& info);
static void Protocol(const v8::FunctionCallbackInfo<v8::Value>& info);
static void Search(const v8::FunctionCallbackInfo<v8::Value>& info);
static void Username(const v8::FunctionCallbackInfo<v8::Value>& info);
// - Component Getters
#define URL_PATTERN_COMPONENT_GETTERS(name, _) \
static void name(const v8::FunctionCallbackInfo<v8::Value>& info);
URL_PATTERN_COMPONENTS(URL_PATTERN_COMPONENT_GETTERS)
#undef URL_PATTERN_COMPONENT_GETTERS
// - Has Regexp Groups
static void HasRegexpGroups(const v8::FunctionCallbackInfo<v8::Value>& info);
void MemoryInfo(MemoryTracker* tracker) const override;
@ -86,14 +93,10 @@ class URLPattern : public BaseObject {
private:
ada::url_pattern<URLPatternRegexProvider> url_pattern_;
// Getter methods
v8::MaybeLocal<v8::Value> Hash() const;
v8::MaybeLocal<v8::Value> Hostname() const;
v8::MaybeLocal<v8::Value> Password() const;
v8::MaybeLocal<v8::Value> Pathname() const;
v8::MaybeLocal<v8::Value> Port() const;
v8::MaybeLocal<v8::Value> Protocol() const;
v8::MaybeLocal<v8::Value> Search() const;
v8::MaybeLocal<v8::Value> Username() const;
#define URL_PATTERN_COMPONENT_GETTERS(name, _) \
v8::MaybeLocal<v8::Value> name() const;
URL_PATTERN_COMPONENTS(URL_PATTERN_COMPONENT_GETTERS)
#undef URL_PATTERN_COMPONENT_GETTERS
bool HasRegExpGroups() const;
// Public API
v8::MaybeLocal<v8::Value> Exec(