From: Lady Date: Sat, 25 Nov 2023 18:11:05 +0000 (-0500) Subject: Be safer about iterable.js property descriptors X-Git-Url: https://git.ladys.computer/Pisces/commitdiff_plain/9539760b1bf114c7cf5d44c09cb618ca02e08fdb Be safer about iterable.js property descriptors --- diff --git a/iterable.js b/iterable.js index 2eb9aa4..7149a28 100644 --- a/iterable.js +++ b/iterable.js @@ -9,11 +9,13 @@ import { bind, call, identity } from "./function.js"; import { + defineOwnDataProperty, defineOwnProperty, getPrototype, objectCreate, + setPropertyValues, } from "./object.js"; -import { ITERATOR, TO_STRING_TAG } from "./value.js"; +import { ITERATOR, TO_STRING_TAG, UNDEFINED } from "./value.js"; export const { /** @@ -228,7 +230,7 @@ export const { const nextIterator = this.#nextIterator; if (baseIterator === null) { // The base iterator has already been exhausted. - return { value: undefined, done: true }; + return { value: UNDEFINED, done: true }; } else if (nextIterator === null) { // This iterator is not currently yielding values from the // provided generator function, either because it doesn’t @@ -296,20 +298,20 @@ export const { objectCreate( iteratorPrototype, { - next: { + next: setPropertyValues(objectCreate(null), { configurable: true, enumerable: false, value: function next() { return call(iteratorNext, this, []); }, writable: true, - }, - [TO_STRING_TAG]: { + }), + [TO_STRING_TAG]: setPropertyValues(objectCreate(null), { configurable: true, enumerable: false, value: stringTag, writable: false, - }, + }), }, ); @@ -330,7 +332,7 @@ export const { stringTag = "Iterator", ) => { const prototype = makePrototype(stringTag); // intentionally cached - return ($, thisArg = undefined) => + return ($, thisArg = UNDEFINED) => new Iterator( prototype, call(makeBaseIterator, $, []), @@ -343,16 +345,20 @@ export const { arrayIteratorFunction: defineOwnProperty( bind( iteratorFunction, - undefined, + UNDEFINED, [arrayIterator, arrayIteratorNext], ), "name", - { value: "arrayIteratorFunction" }, + defineOwnDataProperty( + objectCreate(null), + "value", + "arrayIteratorFunction", + ), ), generatorIteratorFunction: defineOwnProperty( bind( iteratorFunction, - undefined, + UNDEFINED, [ function () { return this(); @@ -361,34 +367,50 @@ export const { ], ), "name", - { value: "generatorIteratorFunction" }, + defineOwnDataProperty( + objectCreate(null), + "value", + "generatorIteratorFunction", + ), ), mapIteratorFunction: defineOwnProperty( bind( iteratorFunction, - undefined, + UNDEFINED, [mapIterator, mapIteratorNext], ), "name", - { value: "mapIteratorFunction" }, + defineOwnDataProperty( + objectCreate(null), + "value", + "mapIteratorFunction", + ), ), setIteratorFunction: defineOwnProperty( bind( iteratorFunction, - undefined, + UNDEFINED, [setIterator, setIteratorNext], ), "name", - { value: "setIteratorFunction" }, + defineOwnDataProperty( + objectCreate(null), + "value", + "setIteratorFunction", + ), ), stringIteratorFunction: defineOwnProperty( bind( iteratorFunction, - undefined, + UNDEFINED, [stringIterator, stringIteratorNext], ), "name", - { value: "stringIteratorFunction" }, + defineOwnDataProperty( + objectCreate(null), + "value", + "stringIteratorFunction", + ), ), }; })();