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

+8
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

+2-1
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
let res = '';
428-
for (const char of inspected) {
429+
for (const char of new SafeStringIterator(inspected)) {
429430
maxColumns -= getStringWidth(char);
430431
if (maxColumns < 0)
431432
break;

lib/internal/source_map/prepare_stack_trace.js

+3-1
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
let debug = 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
let prefix = '';
147-
for (const character of StringPrototypeSlice(line, 0, originalColumn + 1)) {
148+
for (const character of new SafeStringIterator(
149+
StringPrototypeSlice(line, 0, originalColumn + 1))) {
148150
prefix += (character === '\t') ? '\t' :
149151
StringPrototypeRepeat(' ', getStringWidth(character));
150152
}

lib/internal/util/inspect.js

+2-1
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 (const char of str) {
2009+
for (const char of new SafeStringIterator(str)) {
20092010
const code = char.codePointAt(0);
20102011
if (isFullWidthCodePoint(code)) {
20112012
width += 2;

lib/readline.js

+3-2
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
const col = this.columns;
753754
let rows = 0;
754755
str = stripVTControlCharacters(str);
755-
for (const char of str) {
756+
for (const char of new SafeStringIterator(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
let length = 0;
1171-
for (const character of string) {
1172+
for (const character of new SafeStringIterator(string)) {
11721173
length += character.length;
11731174
if (length === string.length) {
11741175
iface.isCompletionEnabled = true;

0 commit comments

Comments
 (0)