lib: add primordials.SafeStringIterator

PR-URL: https://github.com/nodejs/node/pull/36526
Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
Antoine du Hamel 2020-12-04 14:48:52 +01:00 committed by Node.js GitHub Bot
parent 40fc395d7d
commit bd6f2303f4
5 changed files with 18 additions and 5 deletions

View File

@ -242,6 +242,9 @@ primordials.SafeWeakSet = makeSafe(
// Refs: https://tc39.es/ecma262/#sec-%typedarray%-intrinsic-object
[
{ name: 'TypedArray', original: Reflect.getPrototypeOf(Uint8Array) },
{ name: 'StringIterator', original: {
prototype: Reflect.getPrototypeOf(String.prototype[Symbol.iterator]()),
} },
].forEach(({ name, original }) => {
primordials[name] = original;
// The static %TypedArray% methods require a valid `this`, but can't be bound,
@ -250,5 +253,10 @@ primordials.SafeWeakSet = makeSafe(
copyPrototype(original.prototype, primordials, `${name}Prototype`);
});
primordials.SafeStringIterator = createSafeIterator(
primordials.StringPrototypeSymbolIterator,
primordials.StringIteratorPrototypeNext
);
Object.setPrototypeOf(primordials, null);
Object.freeze(primordials);

View File

@ -9,6 +9,7 @@ const {
MathMin,
RegExpPrototypeTest,
SafeSet,
SafeStringIterator,
StringPrototypeEndsWith,
StringPrototypeIndexOf,
StringPrototypeLastIndexOf,
@ -425,7 +426,7 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
getStringWidth(inspected) > maxColumns) {
maxColumns -= 4 + (repl.useColors ? 0 : 3);
let res = '';
for (const char of inspected) {
for (const char of new SafeStringIterator(inspected)) {
maxColumns -= getStringWidth(char);
if (maxColumns < 0)
break;

View File

@ -9,6 +9,7 @@ const {
StringPrototypeSlice,
StringPrototypeSplit,
StringPrototypeStartsWith,
SafeStringIterator,
} = primordials;
let debug = require('internal/util/debuglog').debuglog('source_map', (fn) => {
@ -144,7 +145,8 @@ function getErrorSource(
// Display ^ in appropriate position, regardless of whether tabs or
// spaces are used:
let prefix = '';
for (const character of StringPrototypeSlice(line, 0, originalColumn + 1)) {
for (const character of new SafeStringIterator(
StringPrototypeSlice(line, 0, originalColumn + 1))) {
prefix += (character === '\t') ? '\t' :
StringPrototypeRepeat(' ', getStringWidth(character));
}

View File

@ -42,6 +42,7 @@ const {
ReflectApply,
RegExp,
RegExpPrototypeToString,
SafeStringIterator,
Set,
SetPrototypeGetSize,
SetPrototypeValues,
@ -2005,7 +2006,7 @@ if (internalBinding('config').hasIntl) {
if (removeControlChars)
str = stripVTControlCharacters(str);
str = str.normalize('NFC');
for (const char of str) {
for (const char of new SafeStringIterator(str)) {
const code = char.codePointAt(0);
if (isFullWidthCodePoint(code)) {
width += 2;

View File

@ -59,6 +59,7 @@ const {
StringPrototypeTrim,
Symbol,
SymbolAsyncIterator,
SafeStringIterator,
} = primordials;
const {
@ -760,7 +761,7 @@ Interface.prototype._getDisplayPos = function(str) {
const col = this.columns;
let rows = 0;
str = stripVTControlCharacters(str);
for (const char of str) {
for (const char of new SafeStringIterator(str)) {
if (char === '\n') {
// Rows must be incremented by 1 even if offset = 0 or col = +Infinity.
rows += MathCeil(offset / col) || 1;
@ -1181,7 +1182,7 @@ function emitKeypressEvents(stream, iface = {}) {
iface.isCompletionEnabled = false;
let length = 0;
for (const character of string) {
for (const character of new SafeStringIterator(string)) {
length += character.length;
if (length === string.length) {
iface.isCompletionEnabled = true;