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: make safe primordials safe to iterate #36391

Closed
wants to merge 11 commits into from
3 changes: 1 addition & 2 deletions lib/inspector.js
Expand Up @@ -123,8 +123,7 @@ class Session extends EventEmitter {
return;
this[connectionSymbol].disconnect();
this[connectionSymbol] = null;
const remainingCallbacks = this[messageCallbacksSymbol].values();
for (const callback of remainingCallbacks) {
for (const [, callback] of this[messageCallbacksSymbol]) {
process.nextTick(callback, new ERR_INSPECTOR_CLOSED());
}
this[messageCallbacksSymbol].clear();
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/modules/cjs/loader.js
Expand Up @@ -1269,7 +1269,7 @@ Module._preloadModules = function(requests) {
};

Module.syncBuiltinESMExports = function syncBuiltinESMExports() {
for (const mod of NativeModule.map.values()) {
for (const [, mod] of NativeModule.map) {
if (mod.canBeRequiredByUsers) {
mod.syncExports();
}
Expand Down
23 changes: 23 additions & 0 deletions lib/internal/per_context/primordials.js
Expand Up @@ -72,9 +72,32 @@ function copyPrototype(src, dest, prefix) {
}
}

const createSafeIterator = (factory, next) => {
class SafeIterator {
constructor(iterable) {
this._iterator = factory(iterable);
}
next() {
return next(this._iterator);
}
}
Object.setPrototypeOf(SafeIterator.prototype, null);
Object.freeze(SafeIterator.prototype);
Object.freeze(SafeIterator);
return SafeIterator;
};

function makeSafe(unsafe, safe) {
copyProps(unsafe.prototype, safe.prototype);
copyProps(unsafe, safe);
if (Symbol.iterator in unsafe.prototype) {
const createIterator = uncurryThis(unsafe.prototype[Symbol.iterator]);
const next = uncurryThis(createIterator(new unsafe()).next);
const SafeIterator = createSafeIterator(createIterator, next);
safe.prototype[Symbol.iterator] = function() {
return new SafeIterator(this);
};
}
Object.setPrototypeOf(safe.prototype, null);
Object.freeze(safe.prototype);
Object.freeze(safe);
Expand Down
11 changes: 1 addition & 10 deletions lib/internal/source_map/source_map_cache.js
Expand Up @@ -7,17 +7,12 @@ const {
ObjectKeys,
ObjectGetOwnPropertyDescriptor,
ObjectPrototypeHasOwnProperty,
Map,
MapPrototypeEntries,
RegExpPrototypeTest,
SafeMap,
StringPrototypeMatch,
StringPrototypeSplit,
uncurryThis,
} = primordials;

const MapIteratorNext = uncurryThis(MapPrototypeEntries(new Map()).next);

function ObjectGetValueSafe(obj, key) {
const desc = ObjectGetOwnPropertyDescriptor(obj, key);
return ObjectPrototypeHasOwnProperty(desc, 'value') ? desc.value : undefined;
Expand Down Expand Up @@ -195,11 +190,7 @@ function rekeySourceMap(cjsModuleInstance, newInstance) {
function sourceMapCacheToObject() {
const obj = ObjectCreate(null);

const it = MapPrototypeEntries(esmSourceMapCache);
let entry;
while (!(entry = MapIteratorNext(it)).done) {
const k = entry.value[0];
const v = entry.value[1];
for (const [k, v] of esmSourceMapCache) {
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
obj[k] = v;
}

Expand Down