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
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
11 changes: 11 additions & 0 deletions lib/internal/per_context/primordials.js
Expand Up @@ -75,6 +75,17 @@ function copyPrototype(src, dest, prefix) {
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(
Reflect.getPrototypeOf(createIterator(new unsafe())).next
);
safe.prototype[Symbol.iterator] = function*() {
const iterator = createIterator(this);
let entry;
while (!(entry = next(iterator)).done) yield entry.value;
};
}
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