Skip to content
← Back

src/ir/wasmgc/wasmgc_imports.ghul

1
namespace IR.WasmGC is
2
use Wasm.MODULE
3
use Wasm.FUNCTION_IMPORT
4
5
use Collections.MAP
6
7
/// The functions a module imports. Each takes a function index ahead
8
/// of every defined function, so every import is declared before the
9
/// defined functions are numbered, and none can be declared after.
10
class WASMGC_IMPORTS is
11
_module: MODULE
12
_indices: MAP[(module_name: string, name: string), int]
13
_operations: MAP[string, int]
14
_is_frozen: bool
15
16
/// The number of functions imported.
17
count: int => _indices.count
18
19
/// An import table writing its imports into `module`.
20
init(module: MODULE) is
21
_module = module
22
_indices = MAP[(module_name: string, name: string), int]()
23
_operations = MAP[string, int]()
24
si
25
26
/// Declares a function import, or finds one already declared,
27
/// and answers its function index.
28
///
29
/// - module_name: the module the host provides it under
30
/// - name: its name in that module
31
/// - type_index: the index of its signature in the type section
32
function(module_name: string, name: string, type_index: int) -> int is
33
let key = (module_name, name)
34
let index mut = 0
35
36
if _indices.try_get_value(key, index ref) then
37
return index
38
fi
39
40
assert !_is_frozen else "import {module_name}.{name} declared after functions were numbered"
41
42
index = _module.add_import(FUNCTION_IMPORT(module_name, name, type_index))
43
44
_indices[key] = index
45
46
return index
47
si
48
49
/// Declares the host import that provides an intrinsic operation,
50
/// and answers its function index.
51
///
52
/// - operation: the intrinsic operation, `host.` and the name
53
/// - type_index: the index of its signature in the type section
54
host(operation: string, type_index: int) -> int is
55
let index = function(HOST_MODULE, operation.substring(HOST_PREFIX.length), type_index)
56
57
_operations[operation] = index
58
59
return index
60
si
61
62
/// The function index of the host import providing `operation`,
63
/// or null where none was declared.
64
index_of(operation: string) -> int? is
65
let index mut = 0
66
67
if _operations.try_get_value(operation, index ref) then
68
return index
69
fi
70
71
return null
72
si
73
74
/// Refuses any further declaration: the defined functions are
75
/// being numbered after the imports.
76
freeze() is
77
_is_frozen = true
78
si
79
80
/// The module host imports are provided under.
81
HOST_MODULE: string static => "ghul:host"
82
83
/// What an intrinsic operation the host provides starts with.
84
HOST_PREFIX: string static => "host."
85
86
/// The host import that hashes an object's identity, which a WasmGC
87
/// reference has no address to hash.
88
IDENTITY_HASH: string static => "host.identity_hash"
89
90
/// The host import that throws an exception: the host constructs it,
91
/// so it carries the stack it was thrown from, which an exception
92
/// thrown by the module itself does not.
93
THROW: string static => "host.throw"
94
95
/// Whether `operation` is one the host provides.
96
is_host(operation: string?) -> bool static =>
97
operation? /\ operation.starts_with(HOST_PREFIX)
98
si
99
si