Skip to content

Commit

Permalink
src,test: track URL.canParse fast API calls
Browse files Browse the repository at this point in the history
Also regroup two small test files for naming consistency and simplify
the tests.

PR-URL: #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>
  • Loading branch information
targos authored and RafaelGSS committed Aug 19, 2024
1 parent 6fd3ea2 commit d03f090
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 37 deletions.
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

0 comments on commit d03f090

Please sign in to comment.