-
-
Notifications
You must be signed in to change notification settings - Fork 35.7k
url: refactor URLPattern::{Exec,Test} #63667
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,8 +60,10 @@ using v8::FunctionCallbackInfo; | |
| using v8::FunctionTemplate; | ||
| using v8::Global; | ||
| using v8::Isolate; | ||
| using v8::Just; | ||
| using v8::Local; | ||
| using v8::LocalVector; | ||
| using v8::Maybe; | ||
| using v8::MaybeLocal; | ||
| using v8::Name; | ||
| using v8::NewStringType; | ||
|
|
@@ -556,25 +558,31 @@ MaybeLocal<Value> URLPattern::Exec(Environment* env, | |
| } | ||
| return Null(env->isolate()); | ||
| } | ||
| THROW_ERR_OPERATION_FAILED(env, "Failed to exec URLPattern"); | ||
| return {}; | ||
| } | ||
|
|
||
| bool URLPattern::Test(Environment* env, | ||
| const ada::url_pattern_input& input, | ||
| std::optional<std::string_view>& baseURL) { | ||
| Maybe<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; | ||
| return Just(*result); | ||
| } | ||
| THROW_ERR_OPERATION_FAILED(env, "Failed to test URLPattern"); | ||
| return false; | ||
| return {}; | ||
| } | ||
|
|
||
| // V8 Methods | ||
|
|
||
| void URLPattern::Exec(const FunctionCallbackInfo<Value>& args) { | ||
| namespace { | ||
| template <typename R, | ||
| typename M, | ||
| bool (M::*to_r)(R*) const, | ||
| M (URLPattern::*func)(Environment*, | ||
| const ada::url_pattern_input&, | ||
| std::optional<std::string_view>&)> | ||
| void ExecOrTest(const FunctionCallbackInfo<Value>& args) { | ||
| URLPattern* url_pattern; | ||
| ASSIGN_OR_RETURN_UNWRAP(&url_pattern, args.This()); | ||
| auto env = Environment::GetCurrent(args); | ||
| Environment* env = Environment::GetCurrent(args); | ||
|
|
||
| ada::url_pattern_input input; | ||
| std::optional<std::string> baseURL{}; | ||
|
|
@@ -586,7 +594,8 @@ void URLPattern::Exec(const FunctionCallbackInfo<Value>& args) { | |
| 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>()); | ||
| auto maybeInput = | ||
| URLPattern::URLPatternInit::FromJsObject(env, args[0].As<Object>()); | ||
| if (!maybeInput.has_value()) return; | ||
| input = std::move(*maybeInput); | ||
| } else { | ||
|
|
@@ -607,55 +616,26 @@ void URLPattern::Exec(const FunctionCallbackInfo<Value>& args) { | |
| } | ||
| } | ||
|
|
||
| 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; | ||
| R result{}; | ||
| if (((url_pattern->*func)(env, input, baseURL_opt).*to_r)(&result)) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is readable at all.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It uses relatively uncommon but still pretty normal C++ operators – but other than that, is there a specific issue here? |
||
| args.GetReturnValue().Set(result); | ||
| } | ||
| args.GetReturnValue().Set(result); | ||
| } | ||
| } // namespace | ||
|
|
||
| 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 || args[0]->IsNullOrUndefined()) { | ||
| 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; | ||
| } | ||
| // V8 Methods | ||
|
|
||
| if (args.Length() > 1 && !args[1]->IsUndefined()) { | ||
| if (args[1]->IsNull()) { | ||
| baseURL = std::string("null"); | ||
| } else if (args[1]->IsString()) { | ||
| Utf8Value base_url_value(env->isolate(), args[1].As<String>()); | ||
| baseURL = base_url_value.ToStringView(); | ||
| } else { | ||
| THROW_ERR_INVALID_ARG_TYPE(env, "baseURL must be a string"); | ||
| return; | ||
| } | ||
| } | ||
| void URLPattern::Exec(const FunctionCallbackInfo<Value>& args) { | ||
| ExecOrTest<Local<Value>, | ||
| MaybeLocal<Value>, | ||
| &MaybeLocal<Value>::ToLocal, | ||
| &URLPattern::Exec>(args); | ||
| } | ||
|
|
||
| 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)); | ||
| void URLPattern::Test(const FunctionCallbackInfo<Value>& args) { | ||
| ExecOrTest<bool, Maybe<bool>, &Maybe<bool>::To, &URLPattern::Test>(args); | ||
| } | ||
|
|
||
| #define URL_PATTERN_COMPONENT_GETTERS(uppercase_name, lowercase_name) \ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is a good solution. Yes you're reducing code duplication but at what cost? reducing readability.