|
1 |
| -// Flags: --expose-internals |
| 1 | +// Flags: --expose-internals --no-warnings --allow-natives-syntax |
2 | 2 | 'use strict';
|
3 | 3 |
|
4 |
| -require('../common'); |
| 4 | +const common = require('../common'); |
5 | 5 |
|
6 | 6 | const { URL } = require('url');
|
7 | 7 | const assert = require('assert');
|
8 | 8 |
|
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'); |
16 | 10 |
|
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 | +}); |
18 | 18 |
|
19 | 19 | // It should not throw when called without a base string
|
20 | 20 | 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); |
29 | 38 | }
|
0 commit comments