Skip to content

src: define urlpattern components via macro #57452

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

Closed
wants to merge 2 commits into from
Closed
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
202 changes: 32 additions & 170 deletions src/node_url_pattern.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());
return options;
}

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();
Expand Down Expand Up @@ -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);
}
}

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);
#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); \
} \
}
}
URL_PATTERN_COMPONENTS(URL_PATTERN_COMPONENT_GETTERS)
#undef URL_PATTERN_COMPONENT_GETTERS

void URLPattern::HasRegexpGroups(const FunctionCallbackInfo<Value>& info) {
URLPattern* url_pattern;
Expand All @@ -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);
Expand All @@ -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(),
Expand Down
37 changes: 20 additions & 17 deletions src/node_url_pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
Loading