Skip to content

Commit 8ac2016

Browse files
aduh95danielleadams
authored andcommitted
lib: add primordials.SafeStringIterator
PR-URL: #36526 Reviewed-By: Rich Trott <[email protected]>
1 parent 56af125 commit 8ac2016

File tree

5 files changed

+18
-5
lines changed

5 files changed

+18
-5
lines changed

‎lib/internal/per_context/primordials.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ primordials.SafeWeakSet = makeSafe(
242242
// Refs: https://tc39.es/ecma262/#sec-%typedarray%-intrinsic-object
243243
[
244244
{name: 'TypedArray',original: Reflect.getPrototypeOf(Uint8Array)},
245+
{name: 'StringIterator',original: {
246+
prototype: Reflect.getPrototypeOf(String.prototype[Symbol.iterator]()),
247+
}},
245248
].forEach(({ name, original })=>{
246249
primordials[name]=original;
247250
// The static %TypedArray% methods require a valid `this`, but can't be bound,
@@ -250,5 +253,10 @@ primordials.SafeWeakSet = makeSafe(
250253
copyPrototype(original.prototype,primordials,`${name}Prototype`);
251254
});
252255

256+
primordials.SafeStringIterator=createSafeIterator(
257+
primordials.StringPrototypeSymbolIterator,
258+
primordials.StringIteratorPrototypeNext
259+
);
260+
253261
Object.setPrototypeOf(primordials,null);
254262
Object.freeze(primordials);

‎lib/internal/repl/utils.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const{
99
MathMin,
1010
RegExpPrototypeTest,
1111
SafeSet,
12+
SafeStringIterator,
1213
StringPrototypeEndsWith,
1314
StringPrototypeIndexOf,
1415
StringPrototypeLastIndexOf,
@@ -425,7 +426,7 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active){
425426
getStringWidth(inspected)>maxColumns){
426427
maxColumns-=4+(repl.useColors ? 0 : 3);
427428
letres='';
428-
for(constcharofinspected){
429+
for(constcharofnewSafeStringIterator(inspected)){
429430
maxColumns-=getStringWidth(char);
430431
if(maxColumns<0)
431432
break;

‎lib/internal/source_map/prepare_stack_trace.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const{
99
StringPrototypeSlice,
1010
StringPrototypeSplit,
1111
StringPrototypeStartsWith,
12+
SafeStringIterator,
1213
}=primordials;
1314

1415
letdebug=require('internal/util/debuglog').debuglog('source_map',(fn)=>{
@@ -144,7 +145,8 @@ function getErrorSource(
144145
// Display ^ in appropriate position, regardless of whether tabs or
145146
// spaces are used:
146147
letprefix='';
147-
for(constcharacterofStringPrototypeSlice(line,0,originalColumn+1)){
148+
for(constcharacterofnewSafeStringIterator(
149+
StringPrototypeSlice(line,0,originalColumn+1))){
148150
prefix+=(character==='\t') ? '\t' :
149151
StringPrototypeRepeat(' ',getStringWidth(character));
150152
}

‎lib/internal/util/inspect.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const{
4242
ReflectApply,
4343
RegExp,
4444
RegExpPrototypeToString,
45+
SafeStringIterator,
4546
Set,
4647
SetPrototypeGetSize,
4748
SetPrototypeValues,
@@ -2005,7 +2006,7 @@ if (internalBinding('config').hasIntl){
20052006
if(removeControlChars)
20062007
str=stripVTControlCharacters(str);
20072008
str=str.normalize('NFC');
2008-
for(constcharofstr){
2009+
for(constcharofnewSafeStringIterator(str)){
20092010
constcode=char.codePointAt(0);
20102011
if(isFullWidthCodePoint(code)){
20112012
width+=2;

‎lib/readline.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const{
5959
StringPrototypeTrim,
6060
Symbol,
6161
SymbolAsyncIterator,
62+
SafeStringIterator,
6263
}=primordials;
6364

6465
const{
@@ -752,7 +753,7 @@ Interface.prototype._getDisplayPos = function(str){
752753
constcol=this.columns;
753754
letrows=0;
754755
str=stripVTControlCharacters(str);
755-
for(constcharofstr){
756+
for(constcharofnewSafeStringIterator(str)){
756757
if(char==='\n'){
757758
// Rows must be incremented by 1 even if offset = 0 or col = +Infinity.
758759
rows+=MathCeil(offset/col)||1;
@@ -1168,7 +1169,7 @@ function emitKeypressEvents(stream, iface ={}){
11681169
iface.isCompletionEnabled=false;
11691170

11701171
letlength=0;
1171-
for(constcharacterofstring){
1172+
for(constcharacterofnewSafeStringIterator(string)){
11721173
length+=character.length;
11731174
if(length===string.length){
11741175
iface.isCompletionEnabled=true;

0 commit comments

Comments
(0)