Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: add primordials.SafeArrayIterator #36532

Merged
merged 1 commit into from
Dec 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
ObjectGetOwnPropertyDescriptor,
ObjectGetOwnPropertyDescriptors,
ReflectApply,
SafeArrayIterator,
SafeMap,
String,
Symbol,
Expand Down Expand Up @@ -653,6 +654,7 @@ function defineEventHandler(emitter, name) {
const EventEmitterMixin = (Superclass) => {
class MixedEventEmitter extends Superclass {
constructor(...args) {
args = new SafeArrayIterator(args);
super(...args);
FunctionPrototypeCall(EventEmitter, this);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,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;
Expand Down
9 changes: 5 additions & 4 deletions lib/internal/modules/esm/module_job.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const {
PromiseResolve,
PromisePrototypeCatch,
ReflectApply,
SafeArrayIterator,
SafeSet,
StringPrototypeIncludes,
StringPrototypeMatch,
Expand Down Expand Up @@ -60,9 +61,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();
Expand Down Expand Up @@ -90,8 +91,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);

Expand Down
7 changes: 7 additions & 0 deletions lib/internal/per_context/primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,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]()),
} },
Expand All @@ -253,6 +256,10 @@ primordials.SafeWeakSet = makeSafe(
copyPrototype(original.prototype, primordials, `${name}Prototype`);
});

primordials.SafeArrayIterator = createSafeIterator(
primordials.ArrayPrototypeSymbolIterator,
primordials.ArrayIteratorPrototypeNext
);
primordials.SafeStringIterator = createSafeIterator(
primordials.StringPrototypeSymbolIterator,
primordials.StringIteratorPrototypeNext
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,8 @@ ObjectDefineProperties(URL.prototype, {
if (ctx.host === null && ctx.path.length > 1 && ctx.path[0] === '') {
ret += '/.';
}
for (const segment of ctx.path) {
ret += '/' + segment;
if (ctx.path.length) {
ret += '/' + ArrayPrototypeJoin(ctx.path, '/');
}
}
if (options.search && ctx.query !== null)
Expand Down
5 changes: 3 additions & 2 deletions lib/internal/util/debuglog.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
ObjectDefineProperty,
RegExp,
RegExpPrototypeTest,
SafeArrayIterator,
StringPrototypeToUpperCase
} = primordials;

Expand Down Expand Up @@ -78,15 +79,15 @@ function debuglog(set, cb) {
debug = debuglogImpl(enabled, set);
if (typeof cb === 'function')
cb(debug);
debug(...args);
debug(...new SafeArrayIterator(args));
};
let enabled;
let test = () => {
init();
test = () => enabled;
return enabled;
};
const logger = (...args) => debug(...args);
const logger = (...args) => debug(...new SafeArrayIterator(args));
ObjectDefineProperty(logger, 'enabled', {
get() {
return test();
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-require-delete-array-iterator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

const common = require('../common');


const ArrayIteratorPrototype =
Object.getPrototypeOf(Array.prototype[Symbol.iterator]());

delete Array.prototype[Symbol.iterator];
delete ArrayIteratorPrototype.next;

require('../common/fixtures');
import('../fixtures/es-modules/test-esm-ok.mjs').then(common.mustCall());