Skip to content

Commit

Permalink
url: overload canParse V8 fast api method
Browse files Browse the repository at this point in the history
PR-URL: nodejs#48993
Reviewed-By: Joyee Cheung <[email protected]>
  • Loading branch information
anonrig authored and rluvaton committed Aug 15, 2023
1 parent 82a7896 commit f3072ef
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 16 deletions.
4 changes: 3 additions & 1 deletion lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
19 changes: 7 additions & 12 deletions src/node_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,15 @@ bool BindingData::FastCanParse(Local<Value> receiver,
return ada::can_parse(std::string_view(input.data, input.length));
}

CFunction BindingData::fast_can_parse_(CFunction::Make(FastCanParse));

bool BindingData::FastCanParseWithBase(Local<Value> receiver,
const FastOneByteString& input,
const FastOneByteString& base) {
auto base_view = std::string_view(base.data, base.length);
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<Value>& args) {
CHECK_GT(args.Length(), 4);
Expand Down Expand Up @@ -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<Object> target,
Expand All @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions src/node_url.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion typings/internalBinding/url.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit f3072ef

Please sign in to comment.