mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 13:48:44 +02:00
src,test: track URL.canParse
fast API calls
Also regroup two small test files for naming consistency and simplify the tests. PR-URL: https://github.com/nodejs/node/pull/54356 Reviewed-By: Daniel Lemire <daniel@lemire.me> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
48d63c467d
commit
05495909d7
4 changed files with 30 additions and 36 deletions
|
@ -1,6 +1,7 @@
|
|||
#include "node_url.h"
|
||||
#include "ada.h"
|
||||
#include "base_object-inl.h"
|
||||
#include "node_debug.h"
|
||||
#include "node_errors.h"
|
||||
#include "node_external_reference.h"
|
||||
#include "node_i18n.h"
|
||||
|
@ -173,12 +174,14 @@ void BindingData::CanParse(const FunctionCallbackInfo<Value>& args) {
|
|||
|
||||
bool BindingData::FastCanParse(Local<Value> receiver,
|
||||
const FastOneByteString& input) {
|
||||
TRACK_V8_FAST_API_CALL("url.canParse");
|
||||
return ada::can_parse(std::string_view(input.data, input.length));
|
||||
}
|
||||
|
||||
bool BindingData::FastCanParseWithBase(Local<Value> receiver,
|
||||
const FastOneByteString& input,
|
||||
const FastOneByteString& base) {
|
||||
TRACK_V8_FAST_API_CALL("url.canParse.withBase");
|
||||
auto base_view = std::string_view(base.data, base.length);
|
||||
return ada::can_parse(std::string_view(input.data, input.length), &base_view);
|
||||
}
|
||||
|
|
|
@ -1,19 +0,0 @@
|
|||
'use strict';
|
||||
|
||||
require('../common');
|
||||
const assert = require('assert');
|
||||
|
||||
// One argument is required
|
||||
assert.throws(() => {
|
||||
URL.canParse();
|
||||
}, {
|
||||
code: 'ERR_MISSING_ARGS',
|
||||
name: 'TypeError',
|
||||
});
|
||||
|
||||
{
|
||||
// This test is to ensure that the v8 fast api works.
|
||||
for (let i = 0; i < 1e5; i++) {
|
||||
assert(URL.canParse('https://www.example.com/path/?query=param#hash'));
|
||||
}
|
||||
}
|
|
@ -1,29 +1,38 @@
|
|||
// Flags: --expose-internals
|
||||
// Flags: --expose-internals --no-warnings --allow-natives-syntax
|
||||
'use strict';
|
||||
|
||||
require('../common');
|
||||
const common = require('../common');
|
||||
|
||||
const { URL } = require('url');
|
||||
const assert = require('assert');
|
||||
|
||||
let internalBinding;
|
||||
try {
|
||||
internalBinding = require('internal/test/binding').internalBinding;
|
||||
} catch (e) {
|
||||
console.log('using `test/parallel/test-whatwg-url-canparse` requires `--expose-internals`');
|
||||
throw e;
|
||||
}
|
||||
const { internalBinding } = require('internal/test/binding');
|
||||
|
||||
const { canParse } = internalBinding('url');
|
||||
// One argument is required
|
||||
assert.throws(() => {
|
||||
URL.canParse();
|
||||
}, {
|
||||
code: 'ERR_MISSING_ARGS',
|
||||
name: 'TypeError',
|
||||
});
|
||||
|
||||
// It should not throw when called without a base string
|
||||
assert.strictEqual(URL.canParse('https://example.org'), true);
|
||||
assert.strictEqual(canParse('https://example.org'), true);
|
||||
|
||||
// This for-loop is used to test V8 Fast API optimizations
|
||||
for (let i = 0; i < 100000; i++) {
|
||||
// This example is used because only parsing the first parameter
|
||||
// results in an invalid URL. They have to be used together to
|
||||
// produce truthy value.
|
||||
assert.strictEqual(URL.canParse('/', 'http://n'), true);
|
||||
if (common.isDebug) {
|
||||
const { getV8FastApiCallCount } = internalBinding('debug');
|
||||
|
||||
function testFastPaths() {
|
||||
// `canParse` binding has two overloads.
|
||||
assert.strictEqual(URL.canParse('https://www.example.com/path/?query=param#hash'), true);
|
||||
assert.strictEqual(URL.canParse('/', 'http://n'), true);
|
||||
}
|
||||
|
||||
eval('%PrepareFunctionForOptimization(URL.canParse)');
|
||||
testFastPaths();
|
||||
eval('%OptimizeFunctionOnNextCall(URL.canParse)');
|
||||
testFastPaths();
|
||||
|
||||
assert.strictEqual(getV8FastApiCallCount('url.canParse'), 1);
|
||||
assert.strictEqual(getV8FastApiCallCount('url.canParse.withBase'), 1);
|
||||
}
|
||||
|
|
1
typings/internalBinding/url.d.ts
vendored
1
typings/internalBinding/url.d.ts
vendored
|
@ -6,6 +6,7 @@ export interface URLBinding {
|
|||
domainToASCII(input: string): string;
|
||||
domainToUnicode(input: string): string;
|
||||
canParse(input: string): boolean;
|
||||
canParse(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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue