Skip to content
← Back

src/ir/wasmgc/wasmgc_literal_pool.ghul

1
namespace IR.WasmGC is
2
use Collections.LIST
3
use Collections.MAP
4
5
/// The distinct string literals a program uses, each placed in one run
6
/// of UTF-16 code units, low byte first, ready for a data segment.
7
class WASMGC_LITERAL_POOL is
8
_indices: MAP[string, int]
9
_entries: LIST[(text: string, offset: int)]
10
_bytes: LIST[ubyte]
11
12
init() is
13
_indices = MAP[string, int]()
14
_entries = LIST[(text: string, offset: int)]()
15
_bytes = LIST[ubyte]()
16
si
17
18
/// The literals in the order they were first added, each with the
19
/// byte offset its code units start at.
20
entries: Collections.List[(text: string, offset: int)] => _entries
21
22
/// Every literal's code units, in the order they were added.
23
bytes: ubyte[] => _bytes.to_array()
24
25
/// The position of `text` among the literals, adding it the first
26
/// time it is seen. Equal texts share one position.
27
add(text: string) -> int is
28
let index mut = 0
29
30
if _indices.try_get_value(text, index ref) then
31
return index
32
fi
33
34
index = _entries.count
35
36
_indices[text] = index
37
_entries.add((text, _bytes.count))
38
39
for c in text do
40
let unit = int(c)
41
42
_bytes.add(ubyte(unit & 0xFF))
43
_bytes.add(ubyte(unit >> 8))
44
od
45
46
return index
47
si
48
si
49
si