From: Lady Date: Tue, 5 Sep 2023 00:35:57 +0000 (-0400) Subject: Define names for LazyLoader getters & setters X-Git-Url: https://git.ladys.computer/Pisces/commitdiff_plain/d1f332625c597e21c4be9be32e69030a0f27c6ad?ds=inline;hp=393f4d07436fec754c9863fddf98b8503afdb2c9 Define names for LazyLoader getters & setters This commit also fixes some of the `LazyLoader` tests. --- diff --git a/object.js b/object.js index a403795..55593a3 100644 --- a/object.js +++ b/object.js @@ -7,7 +7,7 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at . -import { bind, call } from "./function.js"; +import { bind, call, toFunctionName } from "./function.js"; import { ITERATOR, SPECIES, toPrimitive, type } from "./value.js"; /** @@ -39,37 +39,50 @@ export class LazyLoader extends null { */ constructor(loadMethods) { if (type(loadMethods) !== "object") { + // The provided value is not an object; throw an error. throw new TypeError( `Piscēs: Cannot construct LazyLoader: Provided argument is not an object: ${loadMethods}.`, ); } else { + // The provided value is an object; process it and build the + // result. const result = objectCreate(getPrototype(loadMethods)); const methodKeys = getOwnPropertyKeys(loadMethods); for (let index = 0; index < methodKeys.length; ++index) { + // Iterate over the property keys of the provided object and + // define getters and setters appropriately on the result. const methodKey = methodKeys[index]; const { configurable, enumerable, writable } = getOwnPropertyDescriptor(loadMethods, methodKey); defineOwnProperty(result, methodKey, { configurable: true, enumerable, - get: () => { - const value = call(loadMethods[methodKey], result, []); - defineOwnProperty(result, methodKey, { - configurable, - enumerable, - value, - writable, - }); - return value; - }, - set: writable - ? ($) => + get: defineOwnProperty( + () => { + const value = call(loadMethods[methodKey], result, []); defineOwnProperty(result, methodKey, { configurable, enumerable, - value: $, + value, writable, - }) + }); + return value; + }, + "name", + { value: toFunctionName(methodKey, "get") }, + ), + set: writable + ? defineOwnProperty( + ($) => + defineOwnProperty(result, methodKey, { + configurable, + enumerable, + value: $, + writable, + }), + "name", + { value: toFunctionName(methodKey, "set") }, + ) : void {}, }); } diff --git a/object.test.js b/object.test.js index efef4d8..68e3264 100644 --- a/object.test.js +++ b/object.test.js @@ -1,7 +1,7 @@ // ♓🌟 Piscēs ∷ object.test.js // ==================================================================== // -// Copyright © 2022 Lady [@ Lady’s Computer]. +// Copyright © 2022–2023 Lady [@ Lady’s Computer]. // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -34,7 +34,7 @@ import { } from "./object.js"; describe("LazyLoader", () => { - const symbol = Symbol(); + const symbol = Symbol("foo"); const prototype = {}; const etaoinMethod = spy(() => "success"); const shrdluMethod = spy(() => "success"); @@ -147,18 +147,18 @@ describe("LazyLoader", () => { for (const key of Reflect.ownKeys(ll)) { yield [ key, - Object.getOwnPropertyDescriptor(ll, key).get !== void {}, + Object.getOwnPropertyDescriptor(ll, key).get?.name, ]; } }(new LazyLoader(methodsObject)), ), { - etaoin: true, - shrdlu: true, - cmfwyp: true, - vbgkqj: true, - xzfiflffffi: true, - [symbol]: true, + etaoin: "get etaoin", + shrdlu: "get shrdlu", + cmfwyp: "get cmfwyp", + vbgkqj: "get vbgkqj", + xzfiflffffi: "get xzfiflffffi", + [symbol]: `get [${symbol.description}]`, }, ); }); @@ -170,23 +170,23 @@ describe("LazyLoader", () => { for (const key of Reflect.ownKeys(ll)) { yield [ key, - Object.getOwnPropertyDescriptor(ll, key).set !== void {}, + Object.getOwnPropertyDescriptor(ll, key).set?.name, ]; } }(new LazyLoader(methodsObject)), ), { - etaoin: false, - shrdlu: false, - cmfwyp: false, - vbgkqj: false, - xzfiflffffi: false, - [symbol]: true, + etaoin: undefined, + shrdlu: undefined, + cmfwyp: undefined, + vbgkqj: undefined, + xzfiflffffi: undefined, + [symbol]: `set [${symbol.description}]`, }, ); }); - describe("[[Construct]] creates a new object with correct getter behaviour", () => { + it("[[Construct]] creates a new object with correct getter behaviour", () => { const ll = new LazyLoader(methodsObject); ll.etaoin; assertEquals( @@ -256,7 +256,7 @@ describe("LazyLoader", () => { assertThrows(() => ll[symbol]); }); - describe("[[Construct]] creates a new object with correct setter behaviour", () => { + it("[[Construct]] creates a new object with correct setter behaviour", () => { const ll = new LazyLoader(methodsObject); ll[symbol] = "success"; assertEquals(