From bdd6199cc2cbc18cb8b684af73000ac9c46ea06a Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sat, 21 Nov 2026 11:04:30 +0000 Subject: [PATCH] ObjectDictionary: the value of a string index signature, entries by run-time keys (the compiler maps { [key: string]: T } to it), and objectDictionaryAssign for a spread of one Co-Authored-By: Claude Opus 5.5 --- src/generics/lib.generics.ts | 87 ++++++++++++++++++++++++++++++++++++ tests/object_dictionary.ts | 40 +++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 tests/object_dictionary.ts diff --git a/src/generics/lib.generics.ts b/src/generics/lib.generics.ts index 089df96..c5c6bb5 100644 --- a/src/generics/lib.generics.ts +++ b/src/generics/lib.generics.ts @@ -3847,6 +3847,93 @@ class Set { } } +// The value of a string index signature, `{ [key: string]: T }`, and of an object literal with a key known +// only at run time (`{ [m]: v }`): its entries, by key, in the order they were added. The compiler reads +// `d[k]`, `d["name"]` and `d.name` as `get` (T | undefined: undefined for a key it does not hold) and +// writes them with `set`; outside this class they are always entries, never these members (@dictionary). +// Under -mm=own it owns its values: a value written in moves in, one read borrows, one overwritten or +// deleted is dropped. The keys are its own copies. +@dictionary +class ObjectDictionary { + private index: Map; + private _keys: string[]; + private _values: T[]; + + constructor() { + this.index = new Map(); + this._keys = []; + this._values = []; + } + + get size() { + return this._keys.length; + } + + get(key: string): T | undefined { + const i = this.index.get(key); + if (i === undefined) { + return undefined; + } + + return this._values[i]; + } + + // writing undefined deletes the entry + set(key: string, value: T | undefined) { + if (value === undefined) { + this.delete(key); + return; + } + + const i = this.index.get(key); + if (i === undefined) { + // a copy of the key: the caller's stays the caller's + this.index.set("" + key, this._values.length); + this._keys.push("" + key); + this._values.push(value); + return; + } + + this._values[i] = value; + } + + [key: string]: T | undefined; + + has(key: string): boolean { + return this.index.has(key); + } + + delete(key: string): boolean { + const i = this.index.get(key); + if (i === undefined) { + return false; + } + + this.index.delete(key); + this._keys.splice(i, 1); + this._values.splice(i, 1); + for (let j = i; j < this._keys.length; j++) { + this.index.set(this._keys[j], j); + } + + return true; + } + + keys(): string[] { + return this._keys.slice(0); + } +} + +// `{ ...source }` where the object made is a dictionary: source's entries copied into target, in order (the +// compiler calls it for a spread of a dictionary). A function of its own, not a member: it is made only for +// the types it is used with, so under -mm=own a dictionary of objects, which can't be copied, fails only +// where one is spread. +function objectDictionaryAssign(target: ObjectDictionary, source: ObjectDictionary) { + for (const k in source) { + target[k] = source[k]; + } +} + namespace __Iterator { // example how to restrict to iterators only diff --git a/tests/object_dictionary.ts b/tests/object_dictionary.ts new file mode 100644 index 0000000..305f750 --- /dev/null +++ b/tests/object_dictionary.ts @@ -0,0 +1,40 @@ +// not own: until a released compiler has the fix for a parameter moved on paths that exclude each other (set) +// ObjectDictionary: the value of a string index signature, `{ [key: string]: T }`. Its entries by keys +// known at run time: written, overwritten, deleted by writing undefined, and an absent key reads undefined. +class P { + constructor(public n: string) {} +} + +const d = new ObjectDictionary(); +for (let i = 0; i < 20; i++) { + d["k" + (i % 5)] = i; +} + +assert(d["k0"] == 15); +assert(d["k4"] == 19); +assert(d["none"] === undefined); + +d["k1"] = undefined; +assert(d["k1"] === undefined); +assert(d["k2"] == 17); + +// objects: under -mm=own the dictionary owns them, and a read borrows (so it is read in a function: a global +// can't hold a borrow) +function objects() { + const o = new ObjectDictionary

(); + for (let i = 0; i < 100; i++) { + const key = "p" + (i % 10); + o[key] = new P("v" + i); + if (i % 3 == 0) { + o[key] = undefined; + } + } + + const p = o["p8"]; + assert(p !== undefined && p.n == "v98"); + assert(o["p9"] === undefined); +} + +objects(); + +console.log("ALL DONE");