forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathnode_url_pattern.cc
More file actions
715 lines (645 loc) · 25 KB
/
node_url_pattern.cc
File metadata and controls
715 lines (645 loc) · 25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
#include "node_url_pattern.h"
#include "base_object-inl.h"
#include "debug_utils-inl.h"
#include "env-inl.h"
#include "node.h"
#include "node_errors.h"
#include "node_mem-inl.h"
#include "path.h"
#include "util-inl.h"
namespace node {
using node::url_pattern::URLPatternRegexProvider;
template <>
struct MemoryRetainerTraits<ada::url_pattern<URLPatternRegexProvider>> {
using Type = ada::url_pattern<URLPatternRegexProvider>;
static void MemoryInfo(MemoryTracker* tracker, const Type& value) {
tracker->TraitTrackInline(value.protocol_component, "protocol_component");
tracker->TraitTrackInline(value.username_component, "username_component");
tracker->TraitTrackInline(value.password_component, "password_component");
tracker->TraitTrackInline(value.hostname_component, "hostname_component");
tracker->TraitTrackInline(value.port_component, "port_component");
tracker->TraitTrackInline(value.pathname_component, "pathname_component");
tracker->TraitTrackInline(value.search_component, "search_component");
tracker->TraitTrackInline(value.hash_component, "hash_component");
}
static const char* MemoryInfoName(const Type& value) {
return "ada::url_pattern";
}
static size_t SelfSize(const Type& value) { return sizeof(value); }
};
template <>
struct MemoryRetainerTraits<
ada::url_pattern_component<URLPatternRegexProvider>> {
using Type = ada::url_pattern_component<URLPatternRegexProvider>;
static void MemoryInfo(MemoryTracker* tracker, const Type& value) {
tracker->TrackField("pattern", value.pattern);
tracker->TrackField("group_name_list", value.group_name_list);
}
static const char* MemoryInfoName(const Type& value) {
return "ada::url_pattern_component";
}
static size_t SelfSize(const Type& value) { return sizeof(value); }
};
} // namespace node
namespace node::url_pattern {
using v8::Array;
using v8::Context;
using v8::DictionaryTemplate;
using v8::DontDelete;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::Global;
using v8::Isolate;
using v8::Local;
using v8::LocalVector;
using v8::MaybeLocal;
using v8::Name;
using v8::NewStringType;
using v8::Object;
using v8::PropertyAttribute;
using v8::ReadOnly;
using v8::RegExp;
using v8::Signature;
using v8::String;
using v8::Value;
std::optional<URLPatternRegexProvider::regex_type>
URLPatternRegexProvider::create_instance(std::string_view pattern,
bool ignore_case) {
auto isolate = Isolate::GetCurrent();
auto env = Environment::GetCurrent(isolate);
int flags = RegExp::Flags::kUnicodeSets | RegExp::Flags::kDotAll;
if (ignore_case) {
flags |= static_cast<int>(RegExp::Flags::kIgnoreCase);
}
Local<String> local_pattern;
if (!String::NewFromUtf8(
isolate, pattern.data(), NewStringType::kNormal, pattern.size())
.ToLocal(&local_pattern)) {
return std::nullopt;
}
Local<RegExp> regexp;
if (!RegExp::New(
env->context(), local_pattern, static_cast<RegExp::Flags>(flags))
.ToLocal(®exp)) {
return std::nullopt;
}
return Global<RegExp>(isolate, regexp);
}
bool URLPatternRegexProvider::regex_match(std::string_view input,
const regex_type& pattern) {
auto isolate = Isolate::GetCurrent();
auto env = Environment::GetCurrent(isolate);
Local<String> local_input;
if (!String::NewFromUtf8(
isolate, input.data(), NewStringType::kNormal, input.size())
.ToLocal(&local_input)) {
return false;
}
Local<Object> result_object;
if (!pattern.Get(isolate)
->Exec(env->context(), local_input)
.ToLocal(&result_object)) {
return false;
}
// RegExp::Exec returns null if there is no match.
return !result_object->IsNull();
}
std::optional<std::vector<std::optional<std::string>>>
URLPatternRegexProvider::regex_search(std::string_view input,
const regex_type& global_pattern) {
auto isolate = Isolate::GetCurrent();
auto env = Environment::GetCurrent(isolate);
Local<String> local_input;
if (!String::NewFromUtf8(
isolate, input.data(), NewStringType::kNormal, input.size())
.ToLocal(&local_input)) {
return std::nullopt;
}
Local<Object> exec_result_object;
auto pattern = global_pattern.Get(isolate);
if (!pattern->Exec(env->context(), local_input)
.ToLocal(&exec_result_object) ||
exec_result_object->IsNull()) {
return std::nullopt;
}
DCHECK(exec_result_object->IsArray());
auto exec_result = exec_result_object.As<Array>();
size_t len = exec_result->Length();
std::vector<std::optional<std::string>> result;
result.reserve(len);
for (size_t i = 1; i < len; i++) {
Local<Value> entry;
if (!exec_result->Get(env->context(), i).ToLocal(&entry)) {
return std::nullopt;
}
// V8 checks that the regexp exec result is one of the correct types.
DCHECK_IMPLIES(!entry->IsUndefined(), entry->IsString());
if (entry->IsUndefined()) {
result.emplace_back(std::nullopt);
} else if (entry->IsString()) {
Utf8Value utf8_entry(isolate, entry.As<String>());
result.emplace_back(utf8_entry.ToString());
}
}
return result;
}
URLPattern::URLPattern(Environment* env,
Local<Object> object,
ada::url_pattern<URLPatternRegexProvider>&& url_pattern)
: BaseObject(env, object), url_pattern_(std::move(url_pattern)) {
MakeWeak();
}
void URLPattern::MemoryInfo(MemoryTracker* tracker) const {
tracker->TraitTrackInline(url_pattern_, "url_pattern");
#define URL_PATTERN_CACHED_VALUES(_, lowercase) \
tracker->TrackField(#lowercase, lowercase);
URL_PATTERN_COMPONENTS(URL_PATTERN_CACHED_VALUES)
#undef URL_PATTERN_CACHED_VALUES
}
void URLPattern::New(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
if (!args.IsConstructCall()) {
THROW_ERR_CONSTRUCT_CALL_REQUIRED(env);
return;
}
// If no arguments are passed, then we parse the empty URLPattern.
if (args.Length() == 0) {
auto init = ada::url_pattern_init{};
auto url_pattern = ada::parse_url_pattern<URLPatternRegexProvider>(init);
CHECK(url_pattern);
new URLPattern(env, args.This(), std::move(*url_pattern));
return;
}
std::optional<ada::url_pattern_init> init{};
std::optional<std::string> input{};
std::optional<std::string> base_url{};
std::optional<ada::url_pattern_options> options{};
// Following patterns are supported:
// - new URLPattern(input)
// - new URLPattern(input, baseURL)
// - new URLPattern(input, options)
// - new URLPattern(input, baseURL, options)
if (args[0]->IsString()) {
BufferValue input_buffer(env->isolate(), args[0]);
CHECK_NOT_NULL(*input_buffer);
input = input_buffer.ToString();
} else if (args[0]->IsObject()) {
init = URLPatternInit::FromJsObject(env, args[0].As<Object>());
// If init does not have a value here, the implication is that an
// error was thrown. Let's allow that to be handled now by returning
// early. If we don't, the error thrown will be swallowed.
if (!init) return;
} else {
THROW_ERR_INVALID_ARG_TYPE(env, "Input must be an object or a string");
return;
}
// The next argument can be baseURL or options.
if (args.Length() > 1) {
if (args[1]->IsString()) {
BufferValue base_url_buffer(env->isolate(), args[1]);
CHECK_NOT_NULL(*base_url_buffer);
base_url = base_url_buffer.ToString();
} else if (args[1]->IsObject()) {
CHECK(!options.has_value());
options = URLPatternOptions::FromJsObject(env, args[1].As<Object>());
if (!options) {
// If options does not have a value, we assume an error was
// thrown and scheduled on the isolate. Return early to
// propagate it.
return;
}
} else {
THROW_ERR_INVALID_ARG_TYPE(env,
"second argument must be a string or object");
return;
}
// Only remaining argument can be options.
if (args.Length() > 2) {
if (!args[2]->IsObject()) {
THROW_ERR_INVALID_ARG_TYPE(env, "options must be an object");
return;
}
CHECK(!options.has_value());
options = URLPatternOptions::FromJsObject(env, args[2].As<Object>());
if (!options) {
// If options does not have a value, we assume an error was
// thrown and scheduled on the isolate. Return early to
// propagate it.
return;
}
}
}
// Either url_pattern_init or input as a string must be provided.
CHECK_IMPLIES(init.has_value(), !input.has_value());
std::string_view base_url_view{};
if (base_url) base_url_view = {base_url->data(), base_url->size()};
ada::url_pattern_input arg0;
if (init.has_value()) {
arg0 = std::move(*init);
} else {
arg0 = std::move(*input);
}
auto url_pattern = parse_url_pattern<URLPatternRegexProvider>(
std::move(arg0),
base_url ? &base_url_view : nullptr,
options.has_value() ? &options.value() : nullptr);
if (!url_pattern) {
THROW_ERR_INVALID_URL_PATTERN(env, "Failed to construct URLPattern");
return;
}
new URLPattern(env, args.This(), std::move(*url_pattern));
}
MaybeLocal<Value> URLPattern::URLPatternInit::ToJsObject(
Environment* env, const ada::url_pattern_init& init) {
auto isolate = env->isolate();
auto context = env->context();
auto tmpl = env->urlpatterninit_template();
if (tmpl.IsEmpty()) {
static constexpr std::string_view namesVec[] = {
"protocol",
"username",
"password",
"hostname",
"port",
"pathname",
"search",
"hash",
"baseURL",
};
tmpl = DictionaryTemplate::New(isolate, namesVec);
env->set_urlpatterninit_template(tmpl);
}
MaybeLocal<Value> values[] = {
Undefined(isolate), // protocol
Undefined(isolate), // username
Undefined(isolate), // password
Undefined(isolate), // hostname
Undefined(isolate), // port
Undefined(isolate), // pathname
Undefined(isolate), // search
Undefined(isolate), // hash
Undefined(isolate), // baseURL
};
int idx = 0;
Local<Value> temp;
const auto trySet = [&](const std::optional<std::string>& val) {
if (val.has_value()) {
if (!ToV8Value(context, *val).ToLocal(&temp)) {
return false;
}
values[idx] = temp;
}
idx++;
return true;
};
if (!trySet(init.protocol) || !trySet(init.username) ||
!trySet(init.password) || !trySet(init.hostname) || !trySet(init.port) ||
!trySet(init.pathname) || !trySet(init.search) || !trySet(init.hash) ||
!trySet(init.base_url)) {
return {};
}
return NewDictionaryInstance(env->context(), tmpl, values);
}
std::optional<ada::url_pattern_init> URLPattern::URLPatternInit::FromJsObject(
Environment* env, Local<Object> obj) {
ada::url_pattern_init init{};
Local<String> components[] = {
env->protocol_string(),
env->username_string(),
env->password_string(),
env->hostname_string(),
env->port_string(),
env->pathname_string(),
env->search_string(),
env->hash_string(),
env->base_url_string(),
};
auto isolate = env->isolate();
const auto set_parameter = [&](std::string_view key, std::string_view value) {
if (key == "protocol") {
init.protocol = std::string(value);
} else if (key == "username") {
init.username = std::string(value);
} else if (key == "password") {
init.password = std::string(value);
} else if (key == "hostname") {
init.hostname = std::string(value);
} else if (key == "port") {
init.port = std::string(value);
} else if (key == "pathname") {
init.pathname = std::string(value);
} else if (key == "search") {
init.search = std::string(value);
} else if (key == "hash") {
init.hash = std::string(value);
} else if (key == "baseURL") {
init.base_url = std::string(value);
}
};
Local<Value> value;
for (const auto& component : components) {
Utf8Value key(isolate, component);
if (obj->Get(env->context(), component).ToLocal(&value)) {
if (value->IsString()) {
Utf8Value utf8_value(isolate, value);
set_parameter(key.ToStringView(), utf8_value.ToStringView());
}
} else {
// If ToLocal failed then we assume an error occurred,
// bail out early to propagate the error.
return std::nullopt;
}
}
return init;
}
MaybeLocal<Object> URLPattern::URLPatternComponentResult::ToJSObject(
Environment* env, const ada::url_pattern_component_result& result) {
auto isolate = env->isolate();
auto context = env->context();
LocalVector<Name> group_names(isolate);
LocalVector<Value> group_values(isolate);
group_names.reserve(result.groups.size());
group_values.reserve(result.groups.size());
for (const auto& [group_key, group_value] : result.groups) {
Local<Value> key;
if (!ToV8Value(context, group_key).ToLocal(&key)) {
return {};
}
group_names.push_back(key.As<Name>());
Local<Value> value;
if (group_value) {
if (!ToV8Value(env->context(), *group_value).ToLocal(&value)) {
return {};
}
} else {
value = Undefined(isolate);
}
group_values.push_back(value);
}
auto parsed_group = Object::New(isolate,
Object::New(isolate),
group_names.data(),
group_values.data(),
group_names.size());
Local<Value> input;
if (!ToV8Value(env->context(), result.input).ToLocal(&input)) {
return {};
}
auto tmpl = env->urlpatterncomponentresult_template();
if (tmpl.IsEmpty()) {
static constexpr std::string_view namesVec[] = {
"input",
"groups",
};
tmpl = DictionaryTemplate::New(isolate, namesVec);
env->set_urlpatterncomponentresult_template(tmpl);
}
MaybeLocal<Value> values[] = {input, parsed_group};
return NewDictionaryInstance(env->context(), tmpl, values);
}
MaybeLocal<Value> URLPattern::URLPatternResult::ToJSValue(
Environment* env, const ada::url_pattern_result& result) {
auto isolate = env->isolate();
auto tmpl = env->urlpatternresult_template();
if (tmpl.IsEmpty()) {
static constexpr std::string_view namesVec[] = {
"inputs",
"protocol",
"username",
"password",
"hostname",
"port",
"pathname",
"search",
"hash",
};
tmpl = DictionaryTemplate::New(isolate, namesVec);
env->set_urlpatternresult_template(tmpl);
}
size_t index = 0;
MaybeLocal<Value> vals[] = {
Array::New(env->context(),
result.inputs.size(),
[&index, &inputs = result.inputs, env]() {
auto& input = inputs[index++];
if (std::holds_alternative<std::string_view>(input)) {
auto input_str = std::get<std::string_view>(input);
return ToV8Value(env->context(), input_str);
} else {
DCHECK(
std::holds_alternative<ada::url_pattern_init>(input));
auto init = std::get<ada::url_pattern_init>(input);
return URLPatternInit::ToJsObject(env, init);
}
}),
URLPatternComponentResult::ToJSObject(env, result.protocol),
URLPatternComponentResult::ToJSObject(env, result.username),
URLPatternComponentResult::ToJSObject(env, result.password),
URLPatternComponentResult::ToJSObject(env, result.hostname),
URLPatternComponentResult::ToJSObject(env, result.port),
URLPatternComponentResult::ToJSObject(env, result.pathname),
URLPatternComponentResult::ToJSObject(env, result.search),
URLPatternComponentResult::ToJSObject(env, result.hash)};
return NewDictionaryInstanceNullProto(env->context(), tmpl, vals);
}
std::optional<ada::url_pattern_options>
URLPattern::URLPatternOptions::FromJsObject(Environment* env,
Local<Object> obj) {
ada::url_pattern_options options{};
Local<Value> ignore_case;
if (obj->Get(env->context(), env->ignore_case_string())
.ToLocal(&ignore_case)) {
if (!ignore_case->IsBoolean()) {
THROW_ERR_INVALID_ARG_TYPE(env, "options.ignoreCase must be a boolean");
return std::nullopt;
}
options.ignore_case = ignore_case->IsTrue();
} else {
// If ToLocal returns false, the assumption is that getting the
// ignore_case_string threw an error, let's propagate that now
// by returning std::nullopt.
return std::nullopt;
}
return options;
}
// Perform value lookup and cache the result in a v8::Global.
#define URL_PATTERN_COMPONENT_GETTERS(uppercase_name, lowercase_name) \
MaybeLocal<Value> URLPattern::uppercase_name() { \
auto isolate = env()->isolate(); \
if (lowercase_name.IsEmpty()) { \
Local<Value> value; \
if (ToV8Value(env()->context(), url_pattern_.get_##lowercase_name()) \
.ToLocal(&value)) { \
lowercase_name.Reset(isolate, value); \
return value; \
} \
return {}; \
} \
return lowercase_name.Get(isolate); \
}
URL_PATTERN_COMPONENTS(URL_PATTERN_COMPONENT_GETTERS)
#undef URL_PATTERN_COMPONENT_GETTERS
bool URLPattern::HasRegExpGroups() const {
return url_pattern_.has_regexp_groups();
}
// Instance methods
MaybeLocal<Value> URLPattern::Exec(Environment* env,
const ada::url_pattern_input& input,
std::optional<std::string_view>& baseURL) {
if (auto result = url_pattern_.exec(input, baseURL ? &*baseURL : nullptr)) {
if (result->has_value()) {
return URLPatternResult::ToJSValue(env, result->value());
}
return Null(env->isolate());
}
return {};
}
bool URLPattern::Test(Environment* env,
const ada::url_pattern_input& input,
std::optional<std::string_view>& baseURL) {
if (auto result = url_pattern_.test(input, baseURL ? &*baseURL : nullptr)) {
return *result;
}
THROW_ERR_OPERATION_FAILED(env, "Failed to test URLPattern");
return false;
}
// V8 Methods
void URLPattern::Exec(const FunctionCallbackInfo<Value>& args) {
URLPattern* url_pattern;
ASSIGN_OR_RETURN_UNWRAP(&url_pattern, args.This());
auto env = Environment::GetCurrent(args);
ada::url_pattern_input input;
std::optional<std::string> baseURL{};
std::string input_base;
if (args.Length() == 0) {
input = ada::url_pattern_init{};
} else if (args[0]->IsString()) {
Utf8Value input_value(env->isolate(), args[0].As<String>());
input_base = input_value.ToString();
input = std::string_view(input_base);
} else if (args[0]->IsObject()) {
auto maybeInput = URLPatternInit::FromJsObject(env, args[0].As<Object>());
if (!maybeInput.has_value()) return;
input = std::move(*maybeInput);
} else {
THROW_ERR_INVALID_ARG_TYPE(
env, "URLPattern input needs to be a string or an object");
return;
}
if (args.Length() > 1) {
if (!args[1]->IsString()) {
THROW_ERR_INVALID_ARG_TYPE(env, "baseURL must be a string");
return;
}
Utf8Value base_url_value(env->isolate(), args[1].As<String>());
baseURL = base_url_value.ToStringView();
}
Local<Value> result;
std::optional<std::string_view> baseURL_opt =
baseURL ? std::optional<std::string_view>(*baseURL) : std::nullopt;
if (!url_pattern->Exec(env, input, baseURL_opt).ToLocal(&result)) {
THROW_ERR_OPERATION_FAILED(env, "Failed to exec URLPattern");
return;
}
args.GetReturnValue().Set(result);
}
void URLPattern::Test(const FunctionCallbackInfo<Value>& args) {
URLPattern* url_pattern;
ASSIGN_OR_RETURN_UNWRAP(&url_pattern, args.This());
auto env = Environment::GetCurrent(args);
ada::url_pattern_input input;
std::optional<std::string> baseURL{};
std::string input_base;
if (args.Length() == 0) {
input = ada::url_pattern_init{};
} else if (args[0]->IsString()) {
Utf8Value input_value(env->isolate(), args[0].As<String>());
input_base = input_value.ToString();
input = std::string_view(input_base);
} else if (args[0]->IsObject()) {
auto maybeInput = URLPatternInit::FromJsObject(env, args[0].As<Object>());
if (!maybeInput.has_value()) return;
input = std::move(*maybeInput);
} else {
THROW_ERR_INVALID_ARG_TYPE(
env, "URLPattern input needs to be a string or an object");
return;
}
if (args.Length() > 1) {
if (!args[1]->IsString()) {
THROW_ERR_INVALID_ARG_TYPE(env, "baseURL must be a string");
return;
}
Utf8Value base_url_value(env->isolate(), args[1].As<String>());
baseURL = base_url_value.ToStringView();
}
std::optional<std::string_view> baseURL_opt =
baseURL ? std::optional<std::string_view>(*baseURL) : std::nullopt;
args.GetReturnValue().Set(url_pattern->Test(env, input, baseURL_opt));
}
#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;
ASSIGN_OR_RETURN_UNWRAP(&url_pattern, info.This());
info.GetReturnValue().Set(url_pattern->HasRegExpGroups());
}
static void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(URLPattern::New);
#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);
}
static void Initialize(Local<Object> target,
Local<Value> unused,
Local<Context> context,
void* priv) {
Environment* env = Environment::GetCurrent(context);
Isolate* isolate = env->isolate();
auto attributes = static_cast<PropertyAttribute>(ReadOnly | DontDelete);
auto ctor_tmpl = NewFunctionTemplate(isolate, URLPattern::New);
auto instance_template = ctor_tmpl->InstanceTemplate();
auto prototype_template = ctor_tmpl->PrototypeTemplate();
ctor_tmpl->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "URLPattern"));
// The signature is used to prevent the property accessors from being
// called on the wrong receiver object (`this`)
auto signature = Signature::New(isolate, ctor_tmpl);
instance_template->SetInternalFieldCount(URLPattern::kInternalFieldCount);
#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(),
FunctionTemplate::New(
isolate, URLPattern::HasRegexpGroups, Local<Value>(), signature),
Local<FunctionTemplate>(),
attributes);
SetProtoMethodNoSideEffect(isolate, ctor_tmpl, "exec", URLPattern::Exec);
SetProtoMethodNoSideEffect(isolate, ctor_tmpl, "test", URLPattern::Test);
SetConstructorFunction(context, target, "URLPattern", ctor_tmpl);
}
} // namespace node::url_pattern
NODE_BINDING_CONTEXT_AWARE_INTERNAL(url_pattern, node::url_pattern::Initialize)
NODE_BINDING_EXTERNAL_REFERENCE(url_pattern,
node::url_pattern::RegisterExternalReferences)