forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeakMap.ts
More file actions
48 lines (41 loc) · 1.49 KB
/
WeakMap.ts
File metadata and controls
48 lines (41 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
WeakMap = class WeakMap<K extends object, V> {
public static [Symbol.species] = WeakMap;
public [Symbol.toStringTag] = "WeakMap";
private items = new LuaTable<K, V>();
constructor(entries?: Iterable<readonly [K, V]> | Array<readonly [K, V]>) {
setmetatable(this.items, { __mode: "k" });
if (entries === undefined) return;
const iterable = entries as Iterable<[K, V]>;
if (iterable[Symbol.iterator]) {
// Iterate manually because WeakMap is compiled with ES5 which doesn't support Iterables in for...of
const iterator = iterable[Symbol.iterator]();
while (true) {
const result = iterator.next();
if (result.done) {
break;
}
const value: [K, V] = result.value; // Ensures index is offset when tuple is accessed
this.items.set(value[0], value[1]);
}
} else {
for (const kvp of entries as Array<[K, V]>) {
this.items.set(kvp[0], kvp[1]);
}
}
}
public delete(key: K): boolean {
const contains = this.has(key);
this.items.set(key, undefined);
return contains;
}
public get(key: K): V | undefined {
return this.items.get(key);
}
public has(key: K): boolean {
return this.items.get(key) !== undefined;
}
public set(key: K, value: V): this {
this.items.set(key, value);
return this;
}
};