Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions lib/slots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Slots[keyof Slots]['usedBy'], Record<keyof Slots, Slots[keyof Slots]['value']>>();

function _GetSlots(container: Slots[keyof Slots]['usedBy']) {
return globalSlots.get(container);
}

function GetSlots<T extends AnyTemporalType>(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<ID1 extends SlotKey>(container: unknown, id1: ID1): container is Slots[ID1]['usedBy'];
Expand Down Expand Up @@ -278,7 +294,7 @@ export function GetSlot<KeyT extends keyof Slots>(
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;
}
Expand All @@ -287,5 +303,15 @@ export function SetSlot<KeyT extends SlotKey>(
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');
}

if (id in slots) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to write this as

const existingSlot = slots[id];
if (existingSlot) throw new TypeError(...);
slots[id] = value;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed it as suggested!

throw new TypeError(`${id} already has set`);
}

slots[id] = value;
}
2 changes: 1 addition & 1 deletion test/test262.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down