Skip to content

Commit

Permalink
lib: add primordials.SafeStringIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Dec 15, 2020
1 parent 45dbcbe commit d1ae5c5
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 7 deletions.
8 changes: 8 additions & 0 deletions lib/internal/per_context/primordials.js
Expand Up @@ -232,6 +232,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,
Expand All @@ -240,5 +243,10 @@ primordials.SafeWeakSet = makeSafe(
copyPrototype(original.prototype, primordials, `${name}Prototype`);
});

primordials.SafeStringIterator = createSafeIterator(
primordials.StringPrototypeSymbolIterator,
primordials.StringIteratorPrototypeNext
);

Object.setPrototypeOf(primordials, null);
Object.freeze(primordials);
3 changes: 2 additions & 1 deletion lib/internal/repl/utils.js
Expand Up @@ -9,6 +9,7 @@ const {
MathMin,
RegExpPrototypeTest,
SafeSet,
SafeStringIterator,
StringPrototypeEndsWith,
StringPrototypeIndexOf,
StringPrototypeLastIndexOf,
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion lib/internal/source_map/prepare_stack_trace.js
Expand Up @@ -9,6 +9,7 @@ const {
StringPrototypeSlice,
StringPrototypeSplit,
StringPrototypeStartsWith,
SafeStringIterator,
} = primordials;

let debug = require('internal/util/debuglog').debuglog('source_map', (fn) => {
Expand Down Expand Up @@ -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));
}
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/tls.js
Expand Up @@ -3,6 +3,7 @@
const {
ArrayIsArray,
ArrayPrototypePush,
SafeStringIterator,
StringPrototypeIndexOf,
StringPrototypeSlice,
StringPrototypeSplit,
Expand All @@ -13,7 +14,7 @@ const {
// C=US\nST=CA\nL=SF\nO=Joyent\nOU=Node.js\nCN=ca1\nemailAddress=ry@clouds.org
function parseCertString(s) {
const out = ObjectCreate(null);
for (const part of StringPrototypeSplit(s, '\n')) {
for (const part of new SafeStringIterator(StringPrototypeSplit(s, '\n'))) {
const sepIndex = StringPrototypeIndexOf(part, '=');
if (sepIndex > 0) {
const key = StringPrototypeSlice(part, 0, sepIndex);
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/util/inspect.js
Expand Up @@ -42,6 +42,7 @@ const {
ReflectApply,
RegExp,
RegExpPrototypeToString,
SafeStringIterator,
Set,
SetPrototypeGetSize,
SetPrototypeValues,
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions lib/readline.js
Expand Up @@ -59,6 +59,7 @@ const {
StringPrototypeTrim,
Symbol,
SymbolAsyncIterator,
SafeStringIterator,
} = primordials;

const {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion lib/tls.js
Expand Up @@ -32,6 +32,7 @@ const {
ObjectDefineProperty,
ObjectFreeze,
RegExpPrototypeTest,
SafeStringIterator,
StringFromCharCode,
StringPrototypeCharCodeAt,
StringPrototypeEndsWith,
Expand Down Expand Up @@ -239,7 +240,8 @@ exports.checkServerIdentity = function checkServerIdentity(hostname, cert) {
hostname = '' + hostname;

if (altNames) {
for (const name of StringPrototypeSplit(altNames, ', ')) {
for (const name of new SafeStringIterator(StringPrototypeSplit(altNames,
', '))) {
if (StringPrototypeStartsWith(name, 'DNS:')) {
ArrayPrototypePush(dnsNames, StringPrototypeSlice(name, 4));
} else if (StringPrototypeStartsWith(name, 'URI:')) {
Expand Down

0 comments on commit d1ae5c5

Please sign in to comment.