Skip to content

Commit 8a08275

Browse files
anonrigrichardlau
authored andcommitted
lib,src: replace toUSVString with toWellFormed()
PR-URL: #47342 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]>
1 parent 0930be6 commit 8a08275

File tree

9 files changed

+31
-128
lines changed

9 files changed

+31
-128
lines changed

‎benchmark/url/usvstring.js‎

Lines changed: 0 additions & 27 deletions
This file was deleted.

‎benchmark/util/to-usv-string.js‎

Lines changed: 0 additions & 21 deletions
This file was deleted.

‎lib/internal/file.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const{
44
DateNow,
55
NumberIsNaN,
66
ObjectDefineProperties,
7+
StringPrototypeToWellFormed,
78
SymbolToStringTag,
89
}=primordials;
910

@@ -15,7 +16,6 @@ const{
1516
customInspectSymbol: kInspect,
1617
kEnumerableProperty,
1718
kEmptyObject,
18-
toUSVString,
1919
}=require('internal/util');
2020

2121
const{
@@ -55,7 +55,7 @@ class File extends Blob{
5555
lastModified=DateNow();
5656
}
5757

58-
this.#name =toUSVString(fileName);
58+
this.#name =StringPrototypeToWellFormed(`${fileName}`);
5959
this.#lastModified =lastModified;
6060
}
6161

‎lib/internal/url.js‎

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const{
2626
StringPrototypeIndexOf,
2727
StringPrototypeSlice,
2828
StringPrototypeStartsWith,
29+
StringPrototypeToWellFormed,
2930
Symbol,
3031
SymbolIterator,
3132
SymbolToStringTag,
@@ -42,7 +43,6 @@ const{
4243
const{
4344
getConstructorOf,
4445
removeColors,
45-
toUSVString,
4646
kEnumerableProperty,
4747
SideEffectFreeRegExpPrototypeSymbolReplace,
4848
}=require('internal/util');
@@ -360,7 +360,11 @@ class URLSearchParams{
360360
thrownewERR_INVALID_TUPLE('Each query pair','[name, value]');
361361
}
362362
// Append (innerSequence[0], innerSequence[1]) to querys list.
363-
ArrayPrototypePush(this.#searchParams,toUSVString(pair[0]),toUSVString(pair[1]));
363+
ArrayPrototypePush(
364+
this.#searchParams,
365+
StringPrototypeToWellFormed(`${pair[0]}`),
366+
StringPrototypeToWellFormed(`${pair[1]}`),
367+
);
364368
}else{
365369
if(((typeofpair!=='object'&&typeofpair!=='function')||
366370
typeofpair[SymbolIterator]!=='function')){
@@ -371,7 +375,7 @@ class URLSearchParams{
371375

372376
for(constelementofpair){
373377
length++;
374-
ArrayPrototypePush(this.#searchParams,toUSVString(element));
378+
ArrayPrototypePush(this.#searchParams,StringPrototypeToWellFormed(`${element}`));
375379
}
376380

377381
// If innerSequence's size is not 2, then throw a TypeError.
@@ -389,8 +393,8 @@ class URLSearchParams{
389393
constkey=keys[i];
390394
constdesc=ReflectGetOwnPropertyDescriptor(init,key);
391395
if(desc!==undefined&&desc.enumerable){
392-
consttypedKey=toUSVString(key);
393-
consttypedValue=toUSVString(init[key]);
396+
consttypedKey=StringPrototypeToWellFormed(key);
397+
consttypedValue=StringPrototypeToWellFormed(`${init[key]}`);
394398

395399
// Two different keys may become the same USVString after normalization.
396400
// In that case, we retain the later one. Refer to WPT.
@@ -407,7 +411,7 @@ class URLSearchParams{
407411
}
408412
}else{
409413
// https://url.spec.whatwg.org/#dom-urlsearchparams-urlsearchparams
410-
init=toUSVString(init);
414+
init=StringPrototypeToWellFormed(`${init}`);
411415
this.#searchParams =init ? parseParams(init) : [];
412416
}
413417
}
@@ -462,8 +466,8 @@ class URLSearchParams{
462466
thrownewERR_MISSING_ARGS('name','value');
463467
}
464468

465-
name=toUSVString(name);
466-
value=toUSVString(value);
469+
name=StringPrototypeToWellFormed(`${name}`);
470+
value=StringPrototypeToWellFormed(`${value}`);
467471
ArrayPrototypePush(this.#searchParams,name,value);
468472
if(this.#context){
469473
this.#context.search=this.toString();
@@ -479,10 +483,10 @@ class URLSearchParams{
479483
}
480484

481485
constlist=this.#searchParams;
482-
name=toUSVString(name);
486+
name=StringPrototypeToWellFormed(`${name}`);
483487

484488
if(value!==undefined){
485-
value=toUSVString(value);
489+
value=StringPrototypeToWellFormed(`${value}`);
486490
for(leti=0;i<list.length;){
487491
if(list[i]===name&&list[i+1]===value){
488492
list.splice(i,2);
@@ -513,7 +517,7 @@ class URLSearchParams{
513517
}
514518

515519
constlist=this.#searchParams;
516-
name=toUSVString(name);
520+
name=StringPrototypeToWellFormed(`${name}`);
517521
for(leti=0;i<list.length;i+=2){
518522
if(list[i]===name){
519523
returnlist[i+1];
@@ -532,7 +536,7 @@ class URLSearchParams{
532536

533537
constlist=this.#searchParams;
534538
constvalues=[];
535-
name=toUSVString(name);
539+
name=StringPrototypeToWellFormed(`${name}`);
536540
for(leti=0;i<list.length;i+=2){
537541
if(list[i]===name){
538542
values.push(list[i+1]);
@@ -550,10 +554,10 @@ class URLSearchParams{
550554
}
551555

552556
constlist=this.#searchParams;
553-
name=toUSVString(name);
557+
name=StringPrototypeToWellFormed(`${name}`);
554558

555559
if(value!==undefined){
556-
value=toUSVString(value);
560+
value=StringPrototypeToWellFormed(`${value}`);
557561
}
558562

559563
for(leti=0;i<list.length;i+=2){
@@ -576,8 +580,8 @@ class URLSearchParams{
576580
}
577581

578582
constlist=this.#searchParams;
579-
name=toUSVString(name);
580-
value=toUSVString(value);
583+
name=StringPrototypeToWellFormed(`${name}`);
584+
value=StringPrototypeToWellFormed(`${value}`);
581585

582586
// If there are any name-value pairs whose name is `name`, in `list`, set
583587
// the value of the first such name-value pair to `value` and remove the
@@ -765,7 +769,7 @@ class URL{
765769
thrownewERR_MISSING_ARGS('url');
766770
}
767771

768-
// toUSVString is not needed.
772+
// StringPrototypeToWellFormed is not needed.
769773
input=`${input}`;
770774

771775
if(base!==undefined){
@@ -998,7 +1002,7 @@ class URL{
9981002
}
9991003

10001004
setsearch(value){
1001-
consthref=bindingUrl.update(this.#context.href,updateActions.kSearch,toUSVString(value));
1005+
consthref=bindingUrl.update(this.#context.href,updateActions.kSearch,StringPrototypeToWellFormed(`${value}`));
10021006
if(href){
10031007
this.#updateContext(href);
10041008
}
@@ -1289,15 +1293,15 @@ function domainToASCII(domain){
12891293
if(arguments.length<1)
12901294
thrownewERR_MISSING_ARGS('domain');
12911295

1292-
// toUSVString is not needed.
1296+
// StringPrototypeToWellFormed is not needed.
12931297
returnbindingUrl.domainToASCII(`${domain}`);
12941298
}
12951299

12961300
functiondomainToUnicode(domain){
12971301
if(arguments.length<1)
12981302
thrownewERR_MISSING_ARGS('domain');
12991303

1300-
// toUSVString is not needed.
1304+
// StringPrototypeToWellFormed is not needed.
13011305
returnbindingUrl.domainToUnicode(`${domain}`);
13021306
}
13031307

@@ -1493,7 +1497,6 @@ function getURLOrigin(url){
14931497
}
14941498

14951499
module.exports={
1496-
toUSVString,
14971500
fileURLToPath,
14981501
pathToFileURL,
14991502
toPathIfFileURL,

‎lib/internal/util.js‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ const{
6262
decorated_private_symbol,
6363
},
6464
sleep: _sleep,
65-
toUSVString: _toUSVString,
6665
}=internalBinding('util');
6766
const{ isNativeError }=internalBinding('types');
6867
const{ getOptionValue }=require('internal/options');
@@ -73,18 +72,6 @@ const experimentalWarnings = new SafeSet();
7372

7473
constcolorRegExp=/\u001b\[\d\d?m/g;// eslint-disable-line no-control-regex
7574

76-
constunpairedSurrogateRe=
77-
/(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])/;
78-
functiontoUSVString(val){
79-
conststr=`${val}`;
80-
// As of V8 5.5, `str.search()` (and `unpairedSurrogateRe[@@search]()`) are
81-
// slower than `unpairedSurrogateRe.exec()`.
82-
constmatch=RegExpPrototypeExec(unpairedSurrogateRe,str);
83-
if(!match)
84-
returnstr;
85-
return_toUSVString(str,match.index);
86-
}
87-
8875
letuvBinding;
8976

9077
functionlazyUv(){
@@ -910,7 +897,6 @@ module.exports ={
910897
sleep,
911898
spliceOne,
912899
setupCoverageHooks,
913-
toUSVString,
914900
removeColors,
915901

916902
// Symbol used to customize promisify conversion

‎lib/util.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const{
4444
ObjectValues,
4545
ReflectApply,
4646
StringPrototypePadStart,
47+
StringPrototypeToWellFormed,
4748
}=primordials;
4849

4950
const{
@@ -75,7 +76,6 @@ const{
7576
getSystemErrorMap,
7677
getSystemErrorName: internalErrorName,
7778
promisify,
78-
toUSVString,
7979
defineLazyProperties,
8080
}=require('internal/util');
8181

@@ -411,7 +411,9 @@ module.exports ={
411411
log,
412412
promisify,
413413
stripVTControlCharacters,
414-
toUSVString,
414+
toUSVString(input){
415+
returnStringPrototypeToWellFormed(`${input}`);
416+
},
415417
gettransferableAbortSignal(){
416418
returnlazyAbortController().transferableAbortSignal;
417419
},

‎src/node_util.cc‎

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ using v8::String;
3737
using v8::Uint32;
3838
using v8::Value;
3939

40-
// Used in ToUSVString().
41-
constexprchar16_tkUnicodeReplacementCharacter = 0xFFFD;
42-
4340
// If a UTF-16 character is a low/trailing surrogate.
4441
CHAR_TEST(16, IsUnicodeTrail, (ch & 0xFC00) == 0xDC00)
4542

@@ -240,40 +237,6 @@ static uint32_t FastGuessHandleType(Local<Value> receiver, const uint32_t fd){
240237

241238
CFunction fast_guess_handle_type_(CFunction::Make(FastGuessHandleType));
242239

243-
staticvoidToUSVString(const FunctionCallbackInfo<Value>& args){
244-
Environment* env = Environment::GetCurrent(args);
245-
CHECK_GE(args.Length(), 2);
246-
CHECK(args[0]->IsString());
247-
CHECK(args[1]->IsNumber());
248-
249-
TwoByteValue value(env->isolate(), args[0]);
250-
251-
int64_t start = args[1]->IntegerValue(env->context()).FromJust();
252-
CHECK_GE(start, 0);
253-
254-
for (size_t i = start; i < value.length(); i++){
255-
char16_t c = value[i];
256-
if (!IsUnicodeSurrogate(c)){
257-
continue;
258-
} elseif (IsUnicodeSurrogateTrail(c) || i == value.length() - 1){
259-
value[i] = kUnicodeReplacementCharacter;
260-
} else{
261-
char16_t d = value[i + 1];
262-
if (IsUnicodeTrail(d)){
263-
i++;
264-
} else{
265-
value[i] = kUnicodeReplacementCharacter;
266-
}
267-
}
268-
}
269-
270-
args.GetReturnValue().Set(
271-
String::NewFromTwoByte(env->isolate(),
272-
*value,
273-
v8::NewStringType::kNormal,
274-
value.length()).ToLocalChecked());
275-
}
276-
277240
voidRegisterExternalReferences(ExternalReferenceRegistry* registry){
278241
registry->Register(GetPromiseDetails);
279242
registry->Register(GetProxyDetails);
@@ -288,7 +251,6 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry){
288251
registry->Register(GuessHandleType);
289252
registry->Register(FastGuessHandleType);
290253
registry->Register(fast_guess_handle_type_.GetTypeInfo());
291-
registry->Register(ToUSVString);
292254
}
293255

294256
voidInitialize(Local<Object> target,
@@ -389,8 +351,6 @@ void Initialize(Local<Object> target,
389351
"guessHandleType",
390352
GuessHandleType,
391353
&fast_guess_handle_type_);
392-
393-
SetMethodNoSideEffect(context, target, "toUSVString", ToUSVString);
394354
}
395355

396356
} // namespace util

‎typings/internalBinding/util.d.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,4 @@ export interface UtilBinding{
4343
shouldAbortOnUncaughtToggle: [shouldAbort: 0|1];
4444
WeakReference: typeofInternalUtilBinding.WeakReference;
4545
guessHandleType(fd: number): 'TCP'|'TTY'|'UDP'|'FILE'|'PIPE'|'UNKNOWN';
46-
toUSVString(str: string,start: number): string;
4746
}

‎typings/primordials.d.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ declare namespace primordials{
420420
exportconstStringPrototypeToLocaleUpperCase: UncurryThis<typeofString.prototype.toLocaleUpperCase>
421421
exportconstStringPrototypeToLowerCase: UncurryThis<typeofString.prototype.toLowerCase>
422422
exportconstStringPrototypeToUpperCase: UncurryThis<typeofString.prototype.toUpperCase>
423+
exportconstStringPrototypeToWellFormed: UncurryThis<typeofString.prototype.toWellFormed>
423424
exportconstStringPrototypeValueOf: UncurryThis<typeofString.prototype.valueOf>
424425
exportconstStringPrototypeReplaceAll: UncurryThis<typeofString.prototype.replaceAll>
425426
exportimportSymbol=globalThis.Symbol;

0 commit comments

Comments
(0)