From f3072ef47770328246b587ab26b502fed8a1d884 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Tue, 1 Aug 2023 18:06:08 -0400 Subject: [PATCH] url: overload `canParse` V8 fast api method PR-URL: https://github.com/nodejs/node/pull/48993 Reviewed-By: Joyee Cheung --- lib/internal/url.js | 4 +++- src/node_url.cc | 19 +++++++------------ src/node_url.h | 3 +-- typings/internalBinding/url.d.ts | 1 - 4 files changed, 11 insertions(+), 16 deletions(-) diff --git a/lib/internal/url.js b/lib/internal/url.js index 7e44959ca2caad..f777e753b68de3 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -1046,9 +1046,11 @@ class URL { url = `${url}`; if (base !== undefined) { - return bindingUrl.canParseWithBase(url, `${base}`); + return bindingUrl.canParse(url, `${base}`); } + // It is important to differentiate the canParse call statements + // since they resolve into different v8 fast api overloads. return bindingUrl.canParse(url); } } diff --git a/src/node_url.cc b/src/node_url.cc index 35a7855035a2f1..f055acd51c323c 100644 --- a/src/node_url.cc +++ b/src/node_url.cc @@ -170,8 +170,6 @@ bool BindingData::FastCanParse(Local receiver, return ada::can_parse(std::string_view(input.data, input.length)); } -CFunction BindingData::fast_can_parse_(CFunction::Make(FastCanParse)); - bool BindingData::FastCanParseWithBase(Local receiver, const FastOneByteString& input, const FastOneByteString& base) { @@ -179,8 +177,8 @@ bool BindingData::FastCanParseWithBase(Local receiver, return ada::can_parse(std::string_view(input.data, input.length), &base_view); } -CFunction BindingData::fast_can_parse_with_base_( - CFunction::Make(FastCanParseWithBase)); +CFunction BindingData::fast_can_parse_methods_[] = { + CFunction::Make(FastCanParse), CFunction::Make(FastCanParseWithBase)}; void BindingData::Format(const FunctionCallbackInfo& args) { CHECK_GT(args.Length(), 4); @@ -361,12 +359,7 @@ void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data, SetMethod(isolate, target, "parse", Parse); SetMethod(isolate, target, "update", Update); SetFastMethodNoSideEffect( - isolate, target, "canParse", CanParse, &fast_can_parse_); - SetFastMethodNoSideEffect(isolate, - target, - "canParseWithBase", - CanParse, - &fast_can_parse_with_base_); + isolate, target, "canParse", CanParse, {fast_can_parse_methods_, 2}); } void BindingData::CreatePerContextProperties(Local target, @@ -387,9 +380,11 @@ void BindingData::RegisterExternalReferences( registry->Register(Update); registry->Register(CanParse); registry->Register(FastCanParse); - registry->Register(fast_can_parse_.GetTypeInfo()); registry->Register(FastCanParseWithBase); - registry->Register(fast_can_parse_with_base_.GetTypeInfo()); + + for (const CFunction& method : fast_can_parse_methods_) { + registry->Register(method.GetTypeInfo()); + } } std::string FromFilePath(const std::string_view file_path) { diff --git a/src/node_url.h b/src/node_url.h index fc0a302778ae73..c485caa2eb0343 100644 --- a/src/node_url.h +++ b/src/node_url.h @@ -75,8 +75,7 @@ class BindingData : public SnapshotableObject { void UpdateComponents(const ada::url_components& components, const ada::scheme::type type); - static v8::CFunction fast_can_parse_; - static v8::CFunction fast_can_parse_with_base_; + static v8::CFunction fast_can_parse_methods_[]; }; std::string FromFilePath(const std::string_view file_path); diff --git a/typings/internalBinding/url.d.ts b/typings/internalBinding/url.d.ts index 90c8d5e869b479..4c8ae2ab1508f0 100644 --- a/typings/internalBinding/url.d.ts +++ b/typings/internalBinding/url.d.ts @@ -6,7 +6,6 @@ declare function InternalBinding(binding: 'url'): { domainToASCII(input: string): string; domainToUnicode(input: string): string; canParse(input: string): boolean; - canParseWithBase(input: string, base: string): boolean; format(input: string, fragment?: boolean, unicode?: boolean, search?: boolean, auth?: boolean): string; parse(input: string, base?: string): string | false; update(input: string, actionType: typeof urlUpdateActions, value: string): string | false;