Skip to content

Commit

Permalink
lib,src: replace toUSVString with toWellFormed()
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Mar 31, 2023
1 parent 7ce2232 commit cd7df60
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 91 deletions.
27 changes: 0 additions & 27 deletions benchmark/url/usvstring.js

This file was deleted.

4 changes: 4 additions & 0 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -1949,6 +1949,10 @@ The encoding supported by the `TextEncoder` instance. Always set to `'utf-8'`.
added:
- v16.8.0
- v14.18.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/47342
description: Implementation uses String.prototype.toWellFormed.
-->
* `string` {string}
Expand Down
3 changes: 1 addition & 2 deletions lib/internal/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const {
customInspectSymbol: kInspect,
kEnumerableProperty,
kEmptyObject,
toUSVString,
} = require('internal/util');

const {
Expand Down Expand Up @@ -55,7 +54,7 @@ class File extends Blob {
lastModified = DateNow();
}

this.#name = toUSVString(fileName);
this.#name = fileName.toWellFormed();
this.#lastModified = lastModified;
}

Expand Down
36 changes: 17 additions & 19 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ const {
const {
getConstructorOf,
removeColors,
toUSVString,
kEnumerableProperty,
SideEffectFreeRegExpPrototypeSymbolReplace,
} = require('internal/util');
Expand Down Expand Up @@ -181,7 +180,7 @@ class URLSearchParams {
}
const convertedPair = [];
for (const element of pair)
ArrayPrototypePush(convertedPair, toUSVString(element));
ArrayPrototypePush(convertedPair, element.toWellFormed());
ArrayPrototypePush(pairs, convertedPair);
}

Expand All @@ -202,10 +201,10 @@ class URLSearchParams {
const key = keys[i];
const desc = ReflectGetOwnPropertyDescriptor(init, key);
if (desc !== undefined && desc.enumerable) {
const typedKey = toUSVString(key);
const typedValue = toUSVString(init[key]);
const typedKey = key.toWellFormed();
const typedValue = init[key].toWellFormed();

// Two different key may result same after `toUSVString()`, we only
// Two different key may result same after `String.prototype.toWellFormed`, we only
// leave the later one. Refers to WPT.
if (visited[typedKey] !== undefined) {
this[searchParams][visited[typedKey]] = typedValue;
Expand All @@ -219,7 +218,7 @@ class URLSearchParams {
}
} else {
// USVString
init = toUSVString(init);
init = init.toWellFormed();
this[searchParams] = init ? parseParams(init) : [];
}

Expand Down Expand Up @@ -277,8 +276,8 @@ class URLSearchParams {
throw new ERR_MISSING_ARGS('name', 'value');
}

name = toUSVString(name);
value = toUSVString(value);
name = `${name}`.toWellFormed();
value = `${value}`.toWellFormed();
ArrayPrototypePush(this[searchParams], name, value);
if (this[context]) {
this[context].search = this.toString();
Expand All @@ -294,7 +293,7 @@ class URLSearchParams {
}

const list = this[searchParams];
name = toUSVString(name);
name = `${name}`.toWellFormed();
for (let i = 0; i < list.length;) {
const cur = list[i];
if (cur === name) {
Expand All @@ -317,7 +316,7 @@ class URLSearchParams {
}

const list = this[searchParams];
name = toUSVString(name);
name = `${name}`.toWellFormed();
for (let i = 0; i < list.length; i += 2) {
if (list[i] === name) {
return list[i + 1];
Expand All @@ -336,7 +335,7 @@ class URLSearchParams {

const list = this[searchParams];
const values = [];
name = toUSVString(name);
name = `${name}`.toWellFormed();
for (let i = 0; i < list.length; i += 2) {
if (list[i] === name) {
values.push(list[i + 1]);
Expand All @@ -354,7 +353,7 @@ class URLSearchParams {
}

const list = this[searchParams];
name = toUSVString(name);
name = `${name}`.toWellFormed();
for (let i = 0; i < list.length; i += 2) {
if (list[i] === name) {
return true;
Expand All @@ -372,8 +371,8 @@ class URLSearchParams {
}

const list = this[searchParams];
name = toUSVString(name);
value = toUSVString(value);
name = `${name}`.toWellFormed();
value = `${value}`.toWellFormed();

// If there are any name-value pairs whose name is `name`, in `list`, set
// the value of the first such name-value pair to `value` and remove the
Expand Down Expand Up @@ -549,7 +548,7 @@ class URL {
#searchParams;

constructor(input, base = undefined) {
// toUSVString is not needed.
// String.prototype.toWellFormed is not needed.
input = `${input}`;

if (base !== undefined) {
Expand Down Expand Up @@ -690,7 +689,7 @@ class URL {
}

set search(value) {
updateUrl(this.#context.href, updateActions.kSearch, toUSVString(value), this.#onParseComplete);
updateUrl(this.#context.href, updateActions.kSearch, `${value}`.toWellFormed(), this.#onParseComplete);
}

// readonly
Expand Down Expand Up @@ -1106,15 +1105,15 @@ function domainToASCII(domain) {
if (arguments.length < 1)
throw new ERR_MISSING_ARGS('domain');

// toUSVString is not needed.
// String.prototype.toWellFormed is not needed.
return _domainToASCII(`${domain}`);
}

function domainToUnicode(domain) {
if (arguments.length < 1)
throw new ERR_MISSING_ARGS('domain');

// toUSVString is not needed.
// String.prototype.toWellFormed is not needed.
return _domainToUnicode(`${domain}`);
}

Expand Down Expand Up @@ -1287,7 +1286,6 @@ function toPathIfFileURL(fileURLOrPath) {
}

module.exports = {
toUSVString,
fileURLToPath,
pathToFileURL,
toPathIfFileURL,
Expand Down
6 changes: 0 additions & 6 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ const {
decorated_private_symbol,
},
sleep: _sleep,
toUSVString: _toUSVString,
} = internalBinding('util');
const { isNativeError } = internalBinding('types');

Expand All @@ -69,10 +68,6 @@ const experimentalWarnings = new SafeSet();

const colorRegExp = /\u001b\[\d\d?m/g; // eslint-disable-line no-control-regex

function toUSVString(val) {
return _toUSVString(`${val}`);
}

let uvBinding;

function lazyUv() {
Expand Down Expand Up @@ -786,7 +781,6 @@ module.exports = {
sleep,
spliceOne,
setupCoverageHooks,
toUSVString,
removeColors,

// Symbol used to customize promisify conversion
Expand Down
9 changes: 8 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ const {
getSystemErrorMap,
getSystemErrorName: internalErrorName,
promisify,
toUSVString,
defineLazyProperties,
} = require('internal/util');

Expand Down Expand Up @@ -346,6 +345,14 @@ function getSystemErrorName(err) {
return internalErrorName(err);
}

/**
* @param {string} input
* @returns {string}
*/
function toUSVString(input) {
return input.toWellFormed();
}

// Keep the `exports =` so that various functions can still be monkeypatched
module.exports = {
_errnoException: errnoException,
Expand Down
36 changes: 0 additions & 36 deletions src/node_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ using v8::String;
using v8::Uint32;
using v8::Value;

// Used in ToUSVString().
constexpr char16_t kUnicodeReplacementCharacter = 0xFFFD;

// If a UTF-16 character is a low/trailing surrogate.
CHAR_TEST(16, IsUnicodeTrail, (ch & 0xFC00) == 0xDC00)

Expand Down Expand Up @@ -320,36 +317,6 @@ static void GuessHandleType(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(OneByteString(env->isolate(), type));
}

static void ToUSVString(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
CHECK_GE(args.Length(), 1);
CHECK(args[0]->IsString());

TwoByteValue value(env->isolate(), args[0]);

for (size_t i = 0; i < value.length(); i++) {
char16_t c = value[i];
if (!IsUnicodeSurrogate(c)) {
continue;
} else if (IsUnicodeSurrogateTrail(c) || i == value.length() - 1) {
value[i] = kUnicodeReplacementCharacter;
} else {
char16_t d = value[i + 1];
if (IsUnicodeTrail(d)) {
i++;
} else {
value[i] = kUnicodeReplacementCharacter;
}
}
}

args.GetReturnValue().Set(
String::NewFromTwoByte(env->isolate(),
*value,
v8::NewStringType::kNormal,
value.length()).ToLocalChecked());
}

void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(GetPromiseDetails);
registry->Register(GetProxyDetails);
Expand All @@ -366,7 +333,6 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(WeakReference::IncRef);
registry->Register(WeakReference::DecRef);
registry->Register(GuessHandleType);
registry->Register(ToUSVString);
}

void Initialize(Local<Object> target,
Expand Down Expand Up @@ -472,8 +438,6 @@ void Initialize(Local<Object> target,
SetConstructorFunction(context, target, "WeakReference", weak_ref);

SetMethod(context, target, "guessHandleType", GuessHandleType);

SetMethodNoSideEffect(context, target, "toUSVString", ToUSVString);
}

} // namespace util
Expand Down

0 comments on commit cd7df60

Please sign in to comment.