Skip to content
Prev Previous commit
Next Next commit
fixup! lib: make safe primordials safe to iterate
  • Loading branch information
aduh95 committed Dec 6, 2020
commit 0e6ecc07e028630d30e34fd1cc9469b19bac9670
27 changes: 19 additions & 8 deletions lib/internal/per_context/primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,30 @@ 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 function () {
return new SafeIterator(this);
};
};

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;
};
const next = uncurryThis(createIterator(new unsafe()).next);
safe.prototype[Symbol.iterator] = createSafeIterator(createIterator, next);
}
Object.setPrototypeOf(safe.prototype, null);
Object.freeze(safe.prototype);
Expand Down