Skip to content

Commit 0b16af1

Browse files
targosRafaelGSS
authored andcommitted
src,test: track URL.canParse fast API calls
Also regroup two small test files for naming consistency and simplify the tests. PR-URL: #54356 Reviewed-By: Daniel Lemire <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 7766c1d commit 0b16af1

File tree

4 files changed

+31
-37
lines changed

4 files changed

+31
-37
lines changed

src/node_url.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "node_url.h"
22
#include "ada.h"
33
#include "base_object-inl.h"
4+
#include "node_debug.h"
45
#include "node_errors.h"
56
#include "node_external_reference.h"
67
#include "node_i18n.h"
@@ -173,12 +174,14 @@ void BindingData::CanParse(const FunctionCallbackInfo<Value>& args) {
173174

174175
bool BindingData::FastCanParse(Local<Value> receiver,
175176
const FastOneByteString& input) {
177+
TRACK_V8_FAST_API_CALL("url.canParse");
176178
return ada::can_parse(std::string_view(input.data, input.length));
177179
}
178180

179181
bool BindingData::FastCanParseWithBase(Local<Value> receiver,
180182
const FastOneByteString& input,
181183
const FastOneByteString& base) {
184+
TRACK_V8_FAST_API_CALL("url.canParse.withBase");
182185
auto base_view = std::string_view(base.data, base.length);
183186
return ada::can_parse(std::string_view(input.data, input.length), &base_view);
184187
}

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

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,38 @@
1-
// Flags: --expose-internals
1+
// Flags: --expose-internals --no-warnings --allow-natives-syntax
22
'use strict';
33

4-
require('../common');
4+
const common = require('../common');
55

66
const { URL } = require('url');
77
const assert = require('assert');
88

9-
let internalBinding;
10-
try {
11-
internalBinding = require('internal/test/binding').internalBinding;
12-
} catch (e) {
13-
console.log('using `test/parallel/test-whatwg-url-canparse` requires `--expose-internals`');
14-
throw e;
15-
}
9+
const { internalBinding } = require('internal/test/binding');
1610

17-
const { canParse } = internalBinding('url');
11+
// One argument is required
12+
assert.throws(() => {
13+
URL.canParse();
14+
}, {
15+
code: 'ERR_MISSING_ARGS',
16+
name: 'TypeError',
17+
});
1818

1919
// It should not throw when called without a base string
2020
assert.strictEqual(URL.canParse('https://example.org'), true);
21-
assert.strictEqual(canParse('https://example.org'), true);
22-
23-
// This for-loop is used to test V8 Fast API optimizations
24-
for (let i = 0; i < 100000; i++) {
25-
// This example is used because only parsing the first parameter
26-
// results in an invalid URL. They have to be used together to
27-
// produce truthy value.
28-
assert.strictEqual(URL.canParse('/', 'http://n'), true);
21+
22+
if (common.isDebug) {
23+
const { getV8FastApiCallCount } = internalBinding('debug');
24+
25+
function testFastPaths() {
26+
// `canParse` binding has two overloads.
27+
assert.strictEqual(URL.canParse('https://www.example.com/path/?query=param#hash'), true);
28+
assert.strictEqual(URL.canParse('/', 'http://n'), true);
29+
}
30+
31+
eval('%PrepareFunctionForOptimization(URL.canParse)');
32+
testFastPaths();
33+
eval('%OptimizeFunctionOnNextCall(URL.canParse)');
34+
testFastPaths();
35+
36+
assert.strictEqual(getV8FastApiCallCount('url.canParse'), 1);
37+
assert.strictEqual(getV8FastApiCallCount('url.canParse.withBase'), 1);
2938
}

typings/internalBinding/url.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export interface URLBinding {
66
domainToASCII(input: string): string;
77
domainToUnicode(input: string): string;
88
canParse(input: string): boolean;
9+
canParse(input: string, base: string): boolean;
910
format(input: string, fragment?: boolean, unicode?: boolean, search?: boolean, auth?: boolean): string;
1011
parse(input: string, base?: string): string | false;
1112
update(input: string, actionType: typeof urlUpdateActions, value: string): string | false;

0 commit comments

Comments
 (0)