Skip to content

Commit 872e670

Browse files
KhafraDevtargos
authored andcommitted
src: add v8 fast api for url canParse
PR-URL: #47552 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Mohammed Keyvanzadeh <[email protected]>
1 parent 94a5abb commit 872e670

File tree

4 files changed

+36
-3
lines changed

4 files changed

+36
-3
lines changed

src/node_external_reference.h

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ using CFunctionCallbackWithInt64 = void (*)(v8::Local<v8::Object> receiver,
1919
int64_t);
2020
using CFunctionCallbackWithBool = void (*)(v8::Local<v8::Object> receiver,
2121
bool);
22+
using CFunctionCallbackWithStrings =
23+
bool (*)(v8::Local<v8::Value>, const v8::FastOneByteString& input);
2224

2325
// This class manages the external references from the V8 heap
2426
// to the C++ addresses in Node.js.
@@ -32,6 +34,7 @@ class ExternalReferenceRegistry {
3234
V(CFunctionCallbackReturnDouble) \
3335
V(CFunctionCallbackWithInt64) \
3436
V(CFunctionCallbackWithBool) \
37+
V(CFunctionCallbackWithStrings) \
3538
V(const v8::CFunctionInfo*) \
3639
V(v8::FunctionCallback) \
3740
V(v8::AccessorGetterCallback) \

src/node_url.cc

+19-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "node_external_reference.h"
66
#include "node_i18n.h"
77
#include "util-inl.h"
8+
#include "v8-fast-api-calls.h"
89
#include "v8.h"
910

1011
#include <cstdint>
@@ -14,7 +15,9 @@
1415
namespace node {
1516
namespace url {
1617

18+
using v8::CFunction;
1719
using v8::Context;
20+
using v8::FastOneByteString;
1821
using v8::FunctionCallbackInfo;
1922
using v8::HandleScope;
2023
using v8::Isolate;
@@ -113,7 +116,6 @@ void BindingData::DomainToUnicode(const FunctionCallbackInfo<Value>& args) {
113116
.ToLocalChecked());
114117
}
115118

116-
// TODO(@anonrig): Add V8 Fast API for CanParse method
117119
void BindingData::CanParse(const FunctionCallbackInfo<Value>& args) {
118120
CHECK_GE(args.Length(), 1);
119121
CHECK(args[0]->IsString()); // input
@@ -140,6 +142,17 @@ void BindingData::CanParse(const FunctionCallbackInfo<Value>& args) {
140142
args.GetReturnValue().Set(out.has_value());
141143
}
142144

145+
bool BindingData::FastCanParse(Local<Value> receiver,
146+
const FastOneByteString& input) {
147+
std::string_view input_view(input.data, input.length);
148+
149+
auto output = ada::parse<ada::url_aggregator>(input_view);
150+
151+
return output.has_value();
152+
}
153+
154+
CFunction BindingData::fast_can_parse_(CFunction::Make(FastCanParse));
155+
143156
void BindingData::Format(const FunctionCallbackInfo<Value>& args) {
144157
CHECK_GT(args.Length(), 4);
145158
CHECK(args[0]->IsString()); // url href
@@ -320,20 +333,23 @@ void BindingData::Initialize(Local<Object> target,
320333

321334
SetMethodNoSideEffect(context, target, "domainToASCII", DomainToASCII);
322335
SetMethodNoSideEffect(context, target, "domainToUnicode", DomainToUnicode);
323-
SetMethodNoSideEffect(context, target, "canParse", CanParse);
324336
SetMethodNoSideEffect(context, target, "format", Format);
325337
SetMethod(context, target, "parse", Parse);
326338
SetMethod(context, target, "update", Update);
339+
SetFastMethodNoSideEffect(
340+
context, target, "canParse", CanParse, &fast_can_parse_);
327341
}
328342

329343
void BindingData::RegisterExternalReferences(
330344
ExternalReferenceRegistry* registry) {
331345
registry->Register(DomainToASCII);
332346
registry->Register(DomainToUnicode);
333-
registry->Register(CanParse);
334347
registry->Register(Format);
335348
registry->Register(Parse);
336349
registry->Register(Update);
350+
registry->Register(CanParse);
351+
registry->Register(FastCanParse);
352+
registry->Register(fast_can_parse_.GetTypeInfo());
337353
}
338354

339355
std::string FromFilePath(const std::string_view file_path) {

src/node_url.h

+7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "node.h"
1010
#include "node_snapshotable.h"
1111
#include "util.h"
12+
#include "v8-fast-api-calls.h"
13+
#include "v8.h"
1214

1315
#include <string>
1416

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

4951
static void CanParse(const v8::FunctionCallbackInfo<v8::Value>& args);
52+
static bool FastCanParse(v8::Local<v8::Value> receiver,
53+
const v8::FastOneByteString& input);
54+
5055
static void Format(const v8::FunctionCallbackInfo<v8::Value>& args);
5156
static void Parse(const v8::FunctionCallbackInfo<v8::Value>& args);
5257
static void Update(const v8::FunctionCallbackInfo<v8::Value>& args);
@@ -63,6 +68,8 @@ class BindingData : public SnapshotableObject {
6368

6469
void UpdateComponents(const ada::url_components& components,
6570
const ada::scheme::type type);
71+
72+
static v8::CFunction fast_can_parse_;
6673
};
6774

6875
std::string FromFilePath(const std::string_view file_path);

test/parallel/test-url-canParse-whatwg.js

+7
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,10 @@ assert.throws(() => {
1010
code: 'ERR_MISSING_ARGS',
1111
name: 'TypeError',
1212
});
13+
14+
{
15+
// This test is to ensure that the v8 fast api works.
16+
for (let i = 0; i < 1e5; i++) {
17+
assert(URL.canParse('https://www.example.com/path/?query=param#hash'));
18+
}
19+
}

0 commit comments

Comments
 (0)