From 6e3f4d6194cb34c214e61638a3bda499fafeaf6f Mon Sep 17 00:00:00 2001 From: tars0x9752 <46079709+tars0x9752@users.noreply.github.com> Date: Thu, 21 Jul 2022 22:06:56 +0900 Subject: [PATCH 1/5] Use globalThis slots --- lib/slots.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/slots.ts b/lib/slots.ts index 9032c94b..fb30a87a 100644 --- a/lib/slots.ts +++ b/lib/slots.ts @@ -150,7 +150,12 @@ interface SlotsToTypes { type SlotKey = keyof SlotsToTypes; -const slots = new WeakMap(); +const globalTemporalSlotMapKey = Symbol.for('@@Temporal__slots__private_do_not_use'); + +(globalThis as any)[globalTemporalSlotMapKey] ||= new WeakMap(); + +const slots = (globalThis as any)[globalTemporalSlotMapKey]; + export function CreateSlots(container: AnyTemporalType): void { slots.set(container, Object.create(null)); } From 24a16288cdc6e5292e1d5f1f7f8f61ad317f6f5c Mon Sep 17 00:00:00 2001 From: tars0x9752 <46079709+tars0x9752@users.noreply.github.com> Date: Sun, 9 Oct 2022 18:11:52 +0900 Subject: [PATCH 2/5] Expose GetSlots and CreateSlots instead of the slots itself to avoid dual package hazards --- lib/slots.ts | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/lib/slots.ts b/lib/slots.ts index fb30a87a..baac0dde 100644 --- a/lib/slots.ts +++ b/lib/slots.ts @@ -150,19 +150,30 @@ interface SlotsToTypes { type SlotKey = keyof SlotsToTypes; -const globalTemporalSlotMapKey = Symbol.for('@@Temporal__slots__private_do_not_use'); +const globalSlots = new WeakMap(); -(globalThis as any)[globalTemporalSlotMapKey] ||= new WeakMap(); +function _GetSlots(container: Slots[SlotKey]['usedBy']) { + return globalSlots.get(container); +} -const slots = (globalThis as any)[globalTemporalSlotMapKey]; +const GetSlotsSymbol = Symbol.for('@@Temporal__GetSlots'); -export function CreateSlots(container: AnyTemporalType): void { - slots.set(container, Object.create(null)); -} +// expose GetSlots to avoid dual package hazards +(globalThis as any)[GetSlotsSymbol] ||= _GetSlots; + +const GetSlots = (globalThis as any)[GetSlotsSymbol]; -function GetSlots(container: T) { - return slots.get(container); +function _CreateSlots(container: Slots[SlotKey]['usedBy']): void { + globalSlots.set(container, Object.create(null)); } + +const CreateSlotsSymbol = Symbol.for('@@Temporal__CreateSlots'); + +// expose CreateSlots to avoid dual package hazards +(globalThis as any)[CreateSlotsSymbol] ||= _CreateSlots; + +export const CreateSlots = (globalThis as any)[CreateSlotsSymbol]; + // TODO: is there a better way than 9 overloads to make HasSlot into a type // guard that takes a variable number of parameters? export function HasSlot(container: unknown, id1: ID1): container is Slots[ID1]['usedBy']; @@ -292,5 +303,11 @@ export function SetSlot( id: KeyT, value: Slots[KeyT]['value'] ): void { - GetSlots(container)[id] = value; + const slot = GetSlots(container); + + if (id in slot) { + throw new TypeError(`${id} already has set`); + } + + slot[id] = value; } From f1767a3ee101a389fc59f53523d778a4efc4e65a Mon Sep 17 00:00:00 2001 From: tars0x9752 <46079709+tars0x9752@users.noreply.github.com> Date: Sun, 9 Oct 2022 18:13:19 +0900 Subject: [PATCH 3/5] Update shebang in test262.sh to be more portable --- test/test262.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test262.sh b/test/test262.sh index 726aa78e..b4c45f27 100755 --- a/test/test262.sh +++ b/test/test262.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash # Note that this script is only expected to be run via `npm run test262`, not by # being manually executed. set -e From 829c58b9ea049f5c23854a7dcb71ddf3020086f8 Mon Sep 17 00:00:00 2001 From: tars0x9752 <46079709+tars0x9752@users.noreply.github.com> Date: Sun, 9 Oct 2022 19:30:11 +0900 Subject: [PATCH 4/5] Add more precise types --- lib/slots.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/slots.ts b/lib/slots.ts index baac0dde..f45d2751 100644 --- a/lib/slots.ts +++ b/lib/slots.ts @@ -150,9 +150,9 @@ interface SlotsToTypes { type SlotKey = keyof SlotsToTypes; -const globalSlots = new WeakMap(); +const globalSlots = new WeakMap>(); -function _GetSlots(container: Slots[SlotKey]['usedBy']) { +function _GetSlots(container: Slots[keyof Slots]['usedBy']) { return globalSlots.get(container); } @@ -161,9 +161,9 @@ const GetSlotsSymbol = Symbol.for('@@Temporal__GetSlots'); // expose GetSlots to avoid dual package hazards (globalThis as any)[GetSlotsSymbol] ||= _GetSlots; -const GetSlots = (globalThis as any)[GetSlotsSymbol]; +const GetSlots = (globalThis as any)[GetSlotsSymbol] as typeof _GetSlots; -function _CreateSlots(container: Slots[SlotKey]['usedBy']): void { +function _CreateSlots(container: Slots[keyof Slots]['usedBy']): void { globalSlots.set(container, Object.create(null)); } @@ -172,7 +172,7 @@ const CreateSlotsSymbol = Symbol.for('@@Temporal__CreateSlots'); // expose CreateSlots to avoid dual package hazards (globalThis as any)[CreateSlotsSymbol] ||= _CreateSlots; -export const CreateSlots = (globalThis as any)[CreateSlotsSymbol]; +export const CreateSlots = (globalThis as any)[CreateSlotsSymbol] as typeof _CreateSlots; // TODO: is there a better way than 9 overloads to make HasSlot into a type // guard that takes a variable number of parameters? @@ -294,7 +294,7 @@ export function GetSlot( container: Slots[typeof id]['usedBy'], id: KeyT ): Slots[KeyT]['value'] { - const value = GetSlots(container)[id]; + const value = GetSlots(container)?.[id]; if (value === undefined) throw new TypeError(`Missing internal slot ${id}`); return value; } @@ -303,11 +303,15 @@ export function SetSlot( id: KeyT, value: Slots[KeyT]['value'] ): void { - const slot = GetSlots(container); + const slots = GetSlots(container); - if (id in slot) { + if (slots === undefined) { + throw new TypeError('Missing slots for the given container'); + } + + if (id in slots) { throw new TypeError(`${id} already has set`); } - slot[id] = value; + slots[id] = value; } From 5c2e9b3111bff39ed955150cce1e272ba018aad6 Mon Sep 17 00:00:00 2001 From: tars0x9752 <46079709+tars0x9752@users.noreply.github.com> Date: Mon, 20 Mar 2023 19:10:02 +0900 Subject: [PATCH 5/5] Apply suggested changes about existingSlot --- lib/slots.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/slots.ts b/lib/slots.ts index f45d2751..42473ef6 100644 --- a/lib/slots.ts +++ b/lib/slots.ts @@ -305,13 +305,11 @@ export function SetSlot( ): void { const slots = GetSlots(container); - if (slots === undefined) { - throw new TypeError('Missing slots for the given container'); - } + if (slots === undefined) throw new TypeError('Missing slots for the given container'); - if (id in slots) { - throw new TypeError(`${id} already has set`); - } + const existingSlot = slots[id]; + + if (existingSlot) throw new TypeError(`${id} already has set`); slots[id] = value; }