diff --git a/lib/slots.ts b/lib/slots.ts index 9032c94b..42473ef6 100644 --- a/lib/slots.ts +++ b/lib/slots.ts @@ -150,14 +150,30 @@ interface SlotsToTypes { type SlotKey = keyof SlotsToTypes; -const slots = new WeakMap(); -export function CreateSlots(container: AnyTemporalType): void { - slots.set(container, Object.create(null)); +const globalSlots = new WeakMap>(); + +function _GetSlots(container: Slots[keyof Slots]['usedBy']) { + return globalSlots.get(container); } -function GetSlots(container: T) { - return slots.get(container); +const GetSlotsSymbol = Symbol.for('@@Temporal__GetSlots'); + +// expose GetSlots to avoid dual package hazards +(globalThis as any)[GetSlotsSymbol] ||= _GetSlots; + +const GetSlots = (globalThis as any)[GetSlotsSymbol] as typeof _GetSlots; + +function _CreateSlots(container: Slots[keyof Slots]['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] 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? export function HasSlot(container: unknown, id1: ID1): container is Slots[ID1]['usedBy']; @@ -278,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; } @@ -287,5 +303,13 @@ export function SetSlot( id: KeyT, value: Slots[KeyT]['value'] ): void { - GetSlots(container)[id] = value; + const slots = GetSlots(container); + + if (slots === undefined) throw new TypeError('Missing slots for the given container'); + + const existingSlot = slots[id]; + + if (existingSlot) throw new TypeError(`${id} already has set`); + + slots[id] = value; } 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