From 89b353de5750a2ee3cdc2e567c45ca2d6eb8dfe0 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sun, 6 Dec 2020 12:04:41 +0100 Subject: [PATCH] lib: add primordials.SafeArrayIterator PR-URL: https://github.com/nodejs/node/pull/36532 Backport-PR-URL: https://github.com/nodejs/node/pull/39446 Reviewed-By: Rich Trott --- lib/internal/modules/cjs/loader.js | 2 +- lib/internal/modules/esm/module_job.js | 9 +++++---- lib/internal/per_context/primordials.js | 7 +++++++ lib/internal/util/debuglog.js | 5 +++-- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 4ff756a0ab5c3f..7313c7ac847cd6 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -438,7 +438,7 @@ function trySelf(parentPath, request) { const EXPORTS_PATTERN = /^((?:@[^/\\%]+\/)?[^./\\%][^/\\%]*)(\/.*)?$/; function resolveExports(nmPath, request) { // The implementation's behavior is meant to mirror resolution in ESM. - const [, name, expansion = ''] = + const { 1: name, 2: expansion = '' } = StringPrototypeMatch(request, EXPORTS_PATTERN) || []; if (!name) return; diff --git a/lib/internal/modules/esm/module_job.js b/lib/internal/modules/esm/module_job.js index 9121cb388fccc2..0e1a7eb7b6a779 100644 --- a/lib/internal/modules/esm/module_job.js +++ b/lib/internal/modules/esm/module_job.js @@ -12,6 +12,7 @@ const { PromisePrototypeCatch, ReflectApply, RegExpPrototypeTest, + SafeArrayIterator, SafeSet, StringPrototypeIncludes, StringPrototypeMatch, @@ -76,9 +77,9 @@ class ModuleJob { }); if (promises !== undefined) - await PromiseAll(promises); + await PromiseAll(new SafeArrayIterator(promises)); - return PromiseAll(dependencyJobs); + return PromiseAll(new SafeArrayIterator(dependencyJobs)); }; // Promise for the list of all dependencyJobs. this.linked = link(); @@ -106,8 +107,8 @@ class ModuleJob { } jobsInGraph.add(moduleJob); const dependencyJobs = await moduleJob.linked; - return PromiseAll( - ArrayPrototypeMap(dependencyJobs, addJobsToDependencyGraph)); + return PromiseAll(new SafeArrayIterator( + ArrayPrototypeMap(dependencyJobs, addJobsToDependencyGraph))); }; await addJobsToDependencyGraph(this); diff --git a/lib/internal/per_context/primordials.js b/lib/internal/per_context/primordials.js index a4ea444ef7d832..135afd4d60ce56 100644 --- a/lib/internal/per_context/primordials.js +++ b/lib/internal/per_context/primordials.js @@ -263,6 +263,9 @@ primordials.SafeWeakSet = makeSafe( // Refs: https://tc39.es/ecma262/#sec-%typedarray%-intrinsic-object [ { name: 'TypedArray', original: Reflect.getPrototypeOf(Uint8Array) }, + { name: 'ArrayIterator', original: { + prototype: Reflect.getPrototypeOf(Array.prototype[Symbol.iterator]()), + } }, { name: 'StringIterator', original: { prototype: Reflect.getPrototypeOf(String.prototype[Symbol.iterator]()), } }, @@ -274,6 +277,10 @@ primordials.SafeWeakSet = makeSafe( copyPrototype(original.prototype, primordials, `${name}Prototype`); }); +primordials.SafeArrayIterator = createSafeIterator( + primordials.ArrayPrototypeSymbolIterator, + primordials.ArrayIteratorPrototypeNext +); primordials.SafeStringIterator = createSafeIterator( primordials.StringPrototypeSymbolIterator, primordials.StringIteratorPrototypeNext diff --git a/lib/internal/util/debuglog.js b/lib/internal/util/debuglog.js index a3b8a84772054e..46d3ed5613246e 100644 --- a/lib/internal/util/debuglog.js +++ b/lib/internal/util/debuglog.js @@ -6,6 +6,7 @@ const { ObjectDefineProperty, RegExp, RegExpPrototypeTest, + SafeArrayIterator, StringPrototypeToUpperCase } = primordials; @@ -78,7 +79,7 @@ function debuglog(set, cb) { debug = debuglogImpl(enabled, set); if (typeof cb === 'function') cb(debug); - debug(...args); + debug(...new SafeArrayIterator(args)); }; let enabled; let test = () => { @@ -86,7 +87,7 @@ function debuglog(set, cb) { test = () => enabled; return enabled; }; - const logger = (...args) => debug(...args); + const logger = (...args) => debug(...new SafeArrayIterator(args)); ObjectDefineProperty(logger, 'enabled', { get() { return test();