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,test: track URL.canParse fast API calls #54356

Merged
merged 1 commit into from
Aug 15, 2024
Merged
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
3 changes: 3 additions & 0 deletions src/node_url.cc
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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);
}
Expand Down
19 changes: 0 additions & 19 deletions test/parallel/test-url-canParse-whatwg.js

This file was deleted.

45 changes: 27 additions & 18 deletions test/parallel/test-whatwg-url-canparse.js
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions typings/internalBinding/url.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading