From: Lady Date: Sun, 25 Jun 2023 00:06:14 +0000 (-0700) Subject: Export welknown symbols from value.js and use them X-Git-Tag: 0.4.0~8 X-Git-Url: https://git.ladys.computer/Pisces/commitdiff_plain/da52ccecff0e48d48ea235b4d0def93b149eb0c1 Export welknown symbols from value.js and use them This commit changes function.js to depend on value.js and not the other way around. --- diff --git a/binary.js b/binary.js index deb13db..686d3a2 100644 --- a/binary.js +++ b/binary.js @@ -17,23 +17,23 @@ import { stringFromCodeUnits, stringReplace, } from "./string.js"; +import { ITERATOR } from "./value.js"; const Buffer = ArrayBuffer; const View = DataView; const TypedArray = Object.getPrototypeOf(Uint8Array); const { prototype: arrayPrototype } = Array; const { prototype: bufferPrototype } = Buffer; -const { iterator: iteratorSymbol } = Symbol; const { prototype: rePrototype } = RegExp; const { prototype: typedArrayPrototype } = TypedArray; const { prototype: viewPrototype } = View; -const { [iteratorSymbol]: arrayIterator } = arrayPrototype; +const { [ITERATOR]: arrayIterator } = arrayPrototype; const { next: arrayIteratorNext, -} = Object.getPrototypeOf([][iteratorSymbol]()); +} = Object.getPrototypeOf([][ITERATOR]()); const argumentIterablePrototype = { - [iteratorSymbol]() { + [ITERATOR]() { return { next: bind( arrayIteratorNext, @@ -44,7 +44,7 @@ const argumentIterablePrototype = { }, }; const binaryCodeUnitIterablePrototype = { - [iteratorSymbol]() { + [ITERATOR]() { return { next: bind( arrayIteratorNext, diff --git a/function.js b/function.js index 46cab86..72b3b0d 100644 --- a/function.js +++ b/function.js @@ -1,12 +1,14 @@ // ♓🌟 Piscēs ∷ function.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 // file, You can obtain one at . +import { ITERATOR } from "./value.js"; + export const { /** * Creates a bound function from the provided function using the @@ -40,13 +42,12 @@ export const { defineProperty: defineOwnProperty, getPrototypeOf: getPrototype, } = Object; - const { iterator: iteratorSymbol } = Symbol; - const { [iteratorSymbol]: arrayIterator } = Array.prototype; + const { [ITERATOR]: arrayIterator } = Array.prototype; const { next: arrayIteratorNext, - } = getPrototype([][iteratorSymbol]()); + } = getPrototype([][ITERATOR]()); const argumentIterablePrototype = { - [iteratorSymbol]() { + [ITERATOR]() { return { next: callBind( arrayIteratorNext, diff --git a/iri.js b/iri.js index 47b1fb8..5dd9dd0 100644 --- a/iri.js +++ b/iri.js @@ -1,7 +1,7 @@ // ♓🌟 Piscēs ∷ iri.js // ==================================================================== // -// Copyright © 2020, 2022 Lady [@ Lady’s Computer]. +// Copyright © 2020, 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 @@ -20,6 +20,7 @@ import { stringStartsWith, substring, } from "./string.js"; +import { ITERATOR } from "./value.js"; const sub·delims = rawString`[!\$&'()*+,;=]`; const gen·delims = rawString`[:/?#\[\]@]`; @@ -198,24 +199,23 @@ export const { removeDotSegments, } = (() => { const TE = TextEncoder; - const { iterator: iteratorSymbol } = Symbol; const { toString: numberToString } = Number.prototype; const { encode: teEncode } = TE.prototype; - const { [iteratorSymbol]: arrayIterator } = Array.prototype; + const { [ITERATOR]: arrayIterator } = Array.prototype; const { next: arrayIteratorNext, - } = Object.getPrototypeOf([][iteratorSymbol]()); + } = Object.getPrototypeOf([][ITERATOR]()); const { next: generatorIteratorNext, } = Object.getPrototypeOf(function* () {}.prototype); - const { [iteratorSymbol]: stringIterator } = String.prototype; + const { [ITERATOR]: stringIterator } = String.prototype; const { next: stringIteratorNext, - } = Object.getPrototypeOf(""[iteratorSymbol]()); + } = Object.getPrototypeOf(""[ITERATOR]()); const iriCharacterIterablePrototype = { - [iteratorSymbol]() { + [ITERATOR]() { return { next: bind( stringIteratorNext, @@ -226,14 +226,14 @@ export const { }, }; const iriGeneratorIterablePrototype = { - [iteratorSymbol]() { + [ITERATOR]() { return { next: bind(generatorIteratorNext, this.generator(), []), }; }, }; const iriSegmentIterablePrototype = { - [iteratorSymbol]() { + [ITERATOR]() { return { next: bind( arrayIteratorNext, diff --git a/object.js b/object.js index 9986291..71b81db 100644 --- a/object.js +++ b/object.js @@ -1,14 +1,14 @@ // ♓🌟 Piscēs ∷ object.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 // file, You can obtain one at . import { bind, call } from "./function.js"; -import { toPrimitive, type } from "./value.js"; +import { ITERATOR, SPECIES, toPrimitive, type } from "./value.js"; /** * An object whose properties are lazy‐loaded from the methods on the @@ -688,15 +688,11 @@ export const { */ frozenCopy, } = (() => { - const { - iterator: iteratorSymbol, - species: speciesSymbol, - } = Symbol; const { next: generatorIteratorNext, } = getPrototype(function* () {}.prototype); const propertyDescriptorEntryIterablePrototype = { - [iteratorSymbol]() { + [ITERATOR]() { return { next: bind(generatorIteratorNext, this.generator(), []), }; @@ -714,7 +710,7 @@ export const { // // (If not provided, the constructor will be the value of // getting the `constructor` property of O.) - const species = constructor?.[speciesSymbol] ?? constructor; + const species = constructor?.[SPECIES] ?? constructor; return preventExtensions( objectCreate( species == null || !("prototype" in species) diff --git a/string.js b/string.js index eb6f6fb..c8805d9 100644 --- a/string.js +++ b/string.js @@ -15,7 +15,7 @@ import { objectCreate, setPrototype, } from "./object.js"; -import { type } from "./value.js"; +import { ITERATOR, TO_STRING_TAG, type } from "./value.js"; export const { /** @@ -310,21 +310,17 @@ export const { */ scalarValueString, } = (() => { - const { - iterator: iteratorSymbol, - toStringTag: toStringTagSymbol, - } = Symbol; - const { [iteratorSymbol]: arrayIterator } = Array.prototype; + const { [ITERATOR]: arrayIterator } = Array.prototype; const arrayIteratorPrototype = Object.getPrototypeOf( - [][iteratorSymbol](), + [][ITERATOR](), ); const { next: arrayIteratorNext } = arrayIteratorPrototype; const iteratorPrototype = Object.getPrototypeOf( arrayIteratorPrototype, ); - const { [iteratorSymbol]: stringIterator } = String.prototype; + const { [ITERATOR]: stringIterator } = String.prototype; const stringIteratorPrototype = Object.getPrototypeOf( - ""[iteratorSymbol](), + ""[ITERATOR](), ); const { next: stringIteratorNext } = stringIteratorPrototype; @@ -413,7 +409,7 @@ export const { value: stringCodeValueIteratorNext, writable: true, }, - [toStringTagSymbol]: { + [TO_STRING_TAG]: { configurable: true, enumerable: false, value: "String Code Value Iterator", @@ -422,7 +418,7 @@ export const { }, ); const scalarValueIterablePrototype = { - [iteratorSymbol]() { + [ITERATOR]() { return { next: bind( stringCodeValueIteratorNext, @@ -463,7 +459,7 @@ export const { * String::[Symbol.iterator]. */ export const characters = makeCallable( - String.prototype[Symbol.iterator], + String.prototype[ITERATOR], ); /** diff --git a/value.js b/value.js index 77cb6b4..782c857 100644 --- a/value.js +++ b/value.js @@ -1,13 +1,49 @@ // ♓🌟 Piscēs ∷ value.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 // file, You can obtain one at . -import { call } from "./function.js"; +export const { + /** The welknown `@@asyncIterator` symbol. */ + asyncIterator: ASYNC_ITERATOR, + + /** The welknown `@@hasInstance` symbol. */ + hasInstance: HAS_INSTANCE, + + /** The welknown `@@isConcatSpreadable` symbol. */ + isConcatSpreadable: IS_CONCAT_SPREADABLE, + + /** The welknown `@@iterator` symbol. */ + iterator: ITERATOR, + + /** The welknown `@@match` symbol. */ + match: MATCH, + + /** The welknown `@@matchAll` symbol. */ + matchAll: MATCH_ALL, + + /** The welknown `@@replace` symbol. */ + replace: REPLACE, + + /** The welknown `@@species` symbol. */ + species: SPECIES, + + /** The welknown `@@split` symbol. */ + split: SPLIT, + + /** The welknown `@@toPrimitive` symbol. */ + toPrimitive: TO_PRIMITIVE, + + /** The welknown `@@toStringTag` symbol. */ + toStringTag: TO_STRING_TAG, + + /** The welknown `@@unscopables` symbol. */ + unscopables: UNSCOPABLES, +} = Symbol; /** The null primitive. */ export const NULL = null; @@ -40,7 +76,7 @@ export const { */ toPrimitive, } = (() => { - const { toPrimitive: toPrimitiveSymbol } = Symbol; + const { apply: call } = Reflect; return { ordinaryToPrimitive: (O, hint) => { @@ -80,7 +116,7 @@ export const { ); } else if (type($) === "object") { // The provided value is an object. - const exoticToPrim = $[toPrimitiveSymbol] ?? undefined; + const exoticToPrim = $[TO_PRIMITIVE] ?? undefined; if (exoticToPrim !== undefined) { // The provided value has an exotic primitive conversion // method. diff --git a/value.test.js b/value.test.js index 6729b85..df86778 100644 --- a/value.test.js +++ b/value.test.js @@ -1,7 +1,7 @@ // ♓🌟 Piscēs ∷ value.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 @@ -15,27 +15,114 @@ import { it, } from "./dev-deps.js"; import { + ASYNC_ITERATOR, + HAS_INSTANCE, + IS_CONCAT_SPREADABLE, + ITERATOR, + MATCH, + MATCH_ALL, NULL, ordinaryToPrimitive, + REPLACE, sameValue, sameValueZero, + SPECIES, + SPLIT, + TO_PRIMITIVE, + TO_STRING_TAG, toPrimitive, type, UNDEFINED, + UNSCOPABLES, } from "./value.js"; +describe("ASYNC_ITERATOR", () => { + it("[[Get]] is @@asyncIterator", () => { + assertStrictEquals(ASYNC_ITERATOR, Symbol.asyncIterator); + }); +}); + +describe("HAS_INSTANCE", () => { + it("[[Get]] is @@hasInstance", () => { + assertStrictEquals(HAS_INSTANCE, Symbol.hasInstance); + }); +}); + +describe("IS_CONCAT_SPREADABLE", () => { + it("[[Get]] is @@isConcatSpreadable", () => { + assertStrictEquals( + IS_CONCAT_SPREADABLE, + Symbol.isConcatSpreadable, + ); + }); +}); + +describe("ITERATOR", () => { + it("[[Get]] is @@iterator", () => { + assertStrictEquals(ITERATOR, Symbol.iterator); + }); +}); + +describe("MATCH", () => { + it("[[Get]] is @@match", () => { + assertStrictEquals(MATCH, Symbol.match); + }); +}); + +describe("MATCH_ALL", () => { + it("[[Get]] is @@matchAll", () => { + assertStrictEquals(MATCH_ALL, Symbol.matchAll); + }); +}); + describe("NULL", () => { it("[[Get]] is null", () => { assertStrictEquals(NULL, null); }); }); +describe("REPLACE", () => { + it("[[Get]] is @@replace", () => { + assertStrictEquals(REPLACE, Symbol.replace); + }); +}); + +describe("SPECIES", () => { + it("[[Get]] is @@species", () => { + assertStrictEquals(SPECIES, Symbol.species); + }); +}); + +describe("SPLIT", () => { + it("[[Get]] is @@split", () => { + assertStrictEquals(SPLIT, Symbol.split); + }); +}); + +describe("TO_PRIMITIVE", () => { + it("[[Get]] is @@toPrimitive", () => { + assertStrictEquals(TO_PRIMITIVE, Symbol.toPrimitive); + }); +}); + +describe("TO_STRING_TAG", () => { + it("[[Get]] is @@toStringTag", () => { + assertStrictEquals(TO_STRING_TAG, Symbol.toStringTag); + }); +}); + describe("UNDEFINED", () => { it("[[Get]] is undefined", () => { assertStrictEquals(UNDEFINED, void {}); }); }); +describe("UNSCOPABLES", () => { + it("[[Get]] is @@unscopables", () => { + assertStrictEquals(UNSCOPABLES, Symbol.unscopables); + }); +}); + describe("ordinaryToPrimitive", () => { it("[[Call]] prefers `valueOf` by default", () => { const obj = {