Skip to content
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

src: add ability to overload fast api functions #48993

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -1054,9 +1054,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
47 changes: 47 additions & 0 deletions src/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,53 @@ void SetFastMethodNoSideEffect(Isolate* isolate,
that->Set(name_string, t);
}

void SetFastMethod(Isolate* isolate,
Local<Template> that,
const std::string_view name,
v8::FunctionCallback slow_callback,
const v8::MemorySpan<const v8::CFunction>& methods) {
Local<v8::FunctionTemplate> t = FunctionTemplate::NewWithCFunctionOverloads(
isolate,
slow_callback,
Local<Value>(),
Local<v8::Signature>(),
0,
v8::ConstructorBehavior::kThrow,
v8::SideEffectType::kHasSideEffect,
methods);

// kInternalized strings are created in the old space.
const v8::NewStringType type = v8::NewStringType::kInternalized;
Local<v8::String> name_string =
v8::String::NewFromUtf8(isolate, name.data(), type, name.size())
.ToLocalChecked();
that->Set(name_string, t);
}

void SetFastMethodNoSideEffect(
Isolate* isolate,
Local<Template> that,
const std::string_view name,
v8::FunctionCallback slow_callback,
const v8::MemorySpan<const v8::CFunction>& methods) {
Local<v8::FunctionTemplate> t = FunctionTemplate::NewWithCFunctionOverloads(
isolate,
slow_callback,
Local<Value>(),
Local<v8::Signature>(),
0,
v8::ConstructorBehavior::kThrow,
v8::SideEffectType::kHasNoSideEffect,
methods);

// kInternalized strings are created in the old space.
const v8::NewStringType type = v8::NewStringType::kInternalized;
Local<v8::String> name_string =
v8::String::NewFromUtf8(isolate, name.data(), type, name.size())
.ToLocalChecked();
that->Set(name_string, t);
}

void SetMethodNoSideEffect(Local<v8::Context> context,
Local<v8::Object> that,
const std::string_view name,
Expand Down
12 changes: 11 additions & 1 deletion src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,11 @@ void SetFastMethod(v8::Local<v8::Context> context,
const std::string_view name,
v8::FunctionCallback slow_callback,
const v8::CFunction* c_function);
void SetFastMethod(v8::Isolate* isolate,
v8::Local<v8::Template> that,
const std::string_view name,
v8::FunctionCallback slow_callback,
const v8::MemorySpan<const v8::CFunction>& methods);
void SetFastMethodNoSideEffect(v8::Isolate* isolate,
v8::Local<v8::Template> that,
const std::string_view name,
Expand All @@ -900,7 +905,12 @@ void SetFastMethodNoSideEffect(v8::Local<v8::Context> context,
const std::string_view name,
v8::FunctionCallback slow_callback,
const v8::CFunction* c_function);

void SetFastMethodNoSideEffect(
v8::Isolate* isolate,
v8::Local<v8::Template> that,
const std::string_view name,
v8::FunctionCallback slow_callback,
const v8::MemorySpan<const v8::CFunction>& methods);
void SetProtoMethod(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> that,
const std::string_view name,
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