Skip to content

Commit 5d076f1

Browse files
committed
src: define urlpattern components using a macro
1 parent 5d9b63d commit 5d076f1

File tree

2 files changed

+28
-56
lines changed

2 files changed

+28
-56
lines changed

src/node_url_pattern.cc

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -462,48 +462,17 @@ URLPattern::URLPatternOptions::FromJsObject(Environment* env,
462462
// by returning std::nullopt.
463463
return std::nullopt;
464464
}
465-
return options;
466-
}
467465

468-
MaybeLocal<Value> URLPattern::Hash() const {
469-
auto context = env()->context();
470-
return ToV8Value(context, url_pattern_.get_hash());
471-
}
472-
473-
MaybeLocal<Value> URLPattern::Hostname() const {
474-
auto context = env()->context();
475-
return ToV8Value(context, url_pattern_.get_hostname());
476-
}
477-
478-
MaybeLocal<Value> URLPattern::Password() const {
479-
auto context = env()->context();
480-
return ToV8Value(context, url_pattern_.get_password());
481-
}
482-
483-
MaybeLocal<Value> URLPattern::Pathname() const {
484-
auto context = env()->context();
485-
return ToV8Value(context, url_pattern_.get_pathname());
486-
}
487-
488-
MaybeLocal<Value> URLPattern::Port() const {
489-
auto context = env()->context();
490-
return ToV8Value(context, url_pattern_.get_port());
491-
}
492-
493-
MaybeLocal<Value> URLPattern::Protocol() const {
494-
auto context = env()->context();
495-
return ToV8Value(context, url_pattern_.get_protocol());
496-
}
497-
498-
MaybeLocal<Value> URLPattern::Search() const {
499-
auto context = env()->context();
500-
return ToV8Value(context, url_pattern_.get_search());
466+
return options;
501467
}
502468

503-
MaybeLocal<Value> URLPattern::Username() const {
504-
auto context = env()->context();
505-
return ToV8Value(context, url_pattern_.get_username());
506-
}
469+
#define URL_PATTERN_COMPONENT_GETTERS(uppercase_name, lowercase_name) \
470+
MaybeLocal<Value> URLPattern::uppercase_name() const { \
471+
auto context = env()->context(); \
472+
return ToV8Value(context, url_pattern_.get_##lowercase_name()); \
473+
}
474+
URL_PATTERN_COMPONENTS(URL_PATTERN_COMPONENT_GETTERS)
475+
#undef URL_PATTERN_COMPONENT_GETTERS
507476

508477
bool URLPattern::HasRegExpGroups() const {
509478
return url_pattern_.has_regexp_groups();

src/node_url_pattern.h

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@
1414

1515
namespace node::url_pattern {
1616

17+
#define URL_PATTERN_COMPONENTS(V) \
18+
V(Protocol, protocol) \
19+
V(Username, username) \
20+
V(Password, password) \
21+
V(Hostname, hostname) \
22+
V(Port, port) \
23+
V(Pathname, pathname) \
24+
V(Search, search) \
25+
V(Hash, hash)
26+
1727
// By default, ada::url_pattern doesn't ship with any regex library.
1828
// Ada has a std::regex implementation, but it is considered unsafe and does
1929
// not have a fully compliant ecmascript syntax support. Therefore, Ada
@@ -42,15 +52,12 @@ class URLPattern : public BaseObject {
4252
// - Functions
4353
static void Exec(const v8::FunctionCallbackInfo<v8::Value>& info);
4454
static void Test(const v8::FunctionCallbackInfo<v8::Value>& info);
45-
// - Getters
46-
static void Hash(const v8::FunctionCallbackInfo<v8::Value>& info);
47-
static void Hostname(const v8::FunctionCallbackInfo<v8::Value>& info);
48-
static void Password(const v8::FunctionCallbackInfo<v8::Value>& info);
49-
static void Pathname(const v8::FunctionCallbackInfo<v8::Value>& info);
50-
static void Port(const v8::FunctionCallbackInfo<v8::Value>& info);
51-
static void Protocol(const v8::FunctionCallbackInfo<v8::Value>& info);
52-
static void Search(const v8::FunctionCallbackInfo<v8::Value>& info);
53-
static void Username(const v8::FunctionCallbackInfo<v8::Value>& info);
55+
// - Component Getters
56+
#define URL_PATTERN_COMPONENT_GETTERS(name, _) \
57+
static void name(const v8::FunctionCallbackInfo<v8::Value>& info);
58+
URL_PATTERN_COMPONENTS(URL_PATTERN_COMPONENT_GETTERS)
59+
#undef URL_PATTERN_COMPONENT_GETTERS
60+
// - Has Regexp Groups
5461
static void HasRegexpGroups(const v8::FunctionCallbackInfo<v8::Value>& info);
5562

5663
void MemoryInfo(MemoryTracker* tracker) const override;
@@ -86,14 +93,10 @@ class URLPattern : public BaseObject {
8693
private:
8794
ada::url_pattern<URLPatternRegexProvider> url_pattern_;
8895
// Getter methods
89-
v8::MaybeLocal<v8::Value> Hash() const;
90-
v8::MaybeLocal<v8::Value> Hostname() const;
91-
v8::MaybeLocal<v8::Value> Password() const;
92-
v8::MaybeLocal<v8::Value> Pathname() const;
93-
v8::MaybeLocal<v8::Value> Port() const;
94-
v8::MaybeLocal<v8::Value> Protocol() const;
95-
v8::MaybeLocal<v8::Value> Search() const;
96-
v8::MaybeLocal<v8::Value> Username() const;
96+
#define URL_PATTERN_COMPONENT_GETTERS(name, _) \
97+
v8::MaybeLocal<v8::Value> name() const;
98+
URL_PATTERN_COMPONENTS(URL_PATTERN_COMPONENT_GETTERS)
99+
#undef URL_PATTERN_COMPONENT_GETTERS
97100
bool HasRegExpGroups() const;
98101
// Public API
99102
v8::MaybeLocal<v8::Value> Exec(

0 commit comments

Comments
 (0)