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: implement v8 fast api for url canParse #47552

Merged
merged 6 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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: 4 additions & 0 deletions src/node_external_reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ using CFunctionCallbackWithInt64 = void (*)(v8::Local<v8::Object> receiver,
int64_t);
using CFunctionCallbackWithBool = void (*)(v8::Local<v8::Object> receiver,
bool);
using CFunctionCallbackWithStrings =
bool (*)(v8::Local<v8::Value>,
const v8::FastOneByteString& input);

// This class manages the external references from the V8 heap
// to the C++ addresses in Node.js.
Expand All @@ -32,6 +35,7 @@ class ExternalReferenceRegistry {
V(CFunctionCallbackReturnDouble) \
V(CFunctionCallbackWithInt64) \
V(CFunctionCallbackWithBool) \
V(CFunctionCallbackWithStrings) \
V(const v8::CFunctionInfo*) \
V(v8::FunctionCallback) \
V(v8::AccessorGetterCallback) \
Expand Down
23 changes: 20 additions & 3 deletions src/node_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "node_i18n.h"
#include "util-inl.h"
#include "v8.h"
#include "v8-fast-api-calls.h"

#include <cstdint>
#include <cstdio>
Expand All @@ -14,7 +15,9 @@
namespace node {
namespace url {

using v8::CFunction;
using v8::Context;
using v8::FastOneByteString;
using v8::FunctionCallbackInfo;
using v8::HandleScope;
using v8::Isolate;
Expand Down Expand Up @@ -113,7 +116,6 @@ void BindingData::DomainToUnicode(const FunctionCallbackInfo<Value>& args) {
.ToLocalChecked());
}

// TODO(@anonrig): Add V8 Fast API for CanParse method
void BindingData::CanParse(const FunctionCallbackInfo<Value>& args) {
CHECK_GE(args.Length(), 2);
CHECK(args[0]->IsString()); // input
Expand All @@ -140,6 +142,18 @@ void BindingData::CanParse(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(out.has_value());
}

bool BindingData::FastCanParse(Local<Value> receiver,
const FastOneByteString& input) {
std::string_view input_view(input.data, input.length);

ada::result<ada::url_aggregator> output =
KhafraDev marked this conversation as resolved.
Show resolved Hide resolved
ada::parse<ada::url_aggregator>(input_view);

return output.has_value();
}

CFunction BindingData::fast_canParse_(CFunction::Make(FastCanParse));
KhafraDev marked this conversation as resolved.
Show resolved Hide resolved

void BindingData::Format(const FunctionCallbackInfo<Value>& args) {
CHECK_GT(args.Length(), 4);
CHECK(args[0]->IsString()); // url href
Expand Down Expand Up @@ -320,20 +334,23 @@ void BindingData::Initialize(Local<Object> target,

SetMethodNoSideEffect(context, target, "domainToASCII", DomainToASCII);
SetMethodNoSideEffect(context, target, "domainToUnicode", DomainToUnicode);
SetMethodNoSideEffect(context, target, "canParse", CanParse);
SetMethodNoSideEffect(context, target, "format", Format);
SetMethod(context, target, "parse", Parse);
SetMethod(context, target, "update", Update);
SetFastMethodNoSideEffect(
context, target, "canParse", CanParse, &fast_canParse_);
}

void BindingData::RegisterExternalReferences(
ExternalReferenceRegistry* registry) {
registry->Register(DomainToASCII);
registry->Register(DomainToUnicode);
registry->Register(CanParse);
registry->Register(Format);
registry->Register(Parse);
registry->Register(Update);
registry->Register(CanParse);
registry->Register(FastCanParse);
registry->Register(fast_canParse_.GetTypeInfo());
}

std::string FromFilePath(const std::string_view file_path) {
Expand Down
7 changes: 7 additions & 0 deletions src/node_url.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "node.h"
#include "node_snapshotable.h"
#include "util.h"
#include "v8.h"
#include "v8-fast-api-calls.h"

#include <string>

Expand Down Expand Up @@ -47,6 +49,9 @@ class BindingData : public SnapshotableObject {
static void DomainToUnicode(const v8::FunctionCallbackInfo<v8::Value>& args);

static void CanParse(const v8::FunctionCallbackInfo<v8::Value>& args);
static bool FastCanParse(v8::Local<v8::Value> receiver,
const v8::FastOneByteString& input);

static void Format(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Parse(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Update(const v8::FunctionCallbackInfo<v8::Value>& args);
Expand All @@ -63,6 +68,8 @@ class BindingData : public SnapshotableObject {

void UpdateComponents(const ada::url_components& components,
const ada::scheme::type type);

static v8::CFunction fast_canParse_;
};

std::string FromFilePath(const std::string_view file_path);
Expand Down