submilli-wasm 0.1.11

A WebAssembly interpreter with a wasmtime-compatible API, built for fast compilation and startup
Documentation
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//! Type descriptors: `ValType`, `FuncType`, `MemoryType`, etc. (wasmtime-compatible).

use crate::canon::CanonicalTypeId;
use crate::engine::Engine;
use crate::value::gc_type::{ArrayType, StructType};
use crate::value::tag_type::TagType;

/// A wasm value type. (Public/boundary type — the serializable internal form is `canon::IrVal`.)
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum ValType {
    I32,
    I64,
    F32,
    F64,
    V128,
    Ref(RefType),
}

/// Mutability of a global.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub enum Mutability {
    Const,
    Var,
}

/// A reference type: nullability plus a heap type.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct RefType {
    nullable: bool,
    heap: HeapType,
}

impl RefType {
    pub fn new(is_nullable: bool, heap_type: HeapType) -> RefType {
        RefType {
            nullable: is_nullable,
            heap: heap_type,
        }
    }

    /// A *nullable* reference to `heap` — the shape of the wasmtime `*REF` constants (see
    /// [`ref_const`](super::ref_const)).
    pub(crate) const fn new_nullable(heap: HeapType) -> RefType {
        RefType {
            nullable: true,
            heap,
        }
    }

    pub fn is_nullable(&self) -> bool {
        self.nullable
    }

    pub fn heap_type(&self) -> &HeapType {
        &self.heap
    }
}

/// The heap type of a reference: the abstract hierarchies (func/extern/any + bottoms) plus
/// concrete (defined) types, each carrying an engine-interned descriptor handle (`FuncType`/
/// `StructType`/`ArrayType`). Matches `wasmtime::HeapType`. (Boundary type — the serializable
/// internal form is `canon::IrHeap`.)
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum HeapType {
    Func,
    NoFunc,
    Extern,
    NoExtern,
    Any,
    Eq,
    I31,
    Struct,
    /// A concrete struct type.
    ConcreteStruct(StructType),
    Array,
    /// A concrete array type.
    ConcreteArray(ArrayType),
    Exn,
    NoExn,
    None,
    /// A concrete function type.
    ConcreteFunc(FuncType),
}

impl HeapType {
    /// Is `self` a subtype of `other` in the **abstract** lattice (the three disjoint
    /// hierarchies + bottoms), plus concrete-type placement by hierarchy and id equality
    /// (`self == other`). Concrete-to-concrete *declared* subtyping (the supertype chain) is
    /// resolved separately against the type registry (`Engine::is_subtype`).
    pub(crate) fn matches(&self, other: &HeapType) -> bool {
        use HeapType as H;
        if self == other {
            return true;
        }
        matches!(
            (self, other),
            (H::NoFunc, H::Func | H::ConcreteFunc(_))
                | (H::ConcreteFunc(_), H::Func)
                | (H::NoExtern, H::Extern)
                | (H::NoExn, H::Exn)
                | (H::I31 | H::Struct | H::Array | H::Eq, H::Any)
                | (H::I31 | H::Struct | H::Array, H::Eq)
                | (H::ConcreteStruct(_), H::Struct | H::Eq | H::Any)
                | (H::ConcreteArray(_), H::Array | H::Eq | H::Any)
                | (
                    H::None,
                    H::Any
                        | H::Eq
                        | H::I31
                        | H::Struct
                        | H::Array
                        | H::ConcreteStruct(_)
                        | H::ConcreteArray(_)
                )
        )
    }
}

impl RefType {
    /// Reference subtyping: heap-type subtyping, and a non-nullable ref is a subtype of a
    /// nullable one (but not vice-versa).
    pub(crate) fn matches(&self, other: &RefType) -> bool {
        (!self.nullable || other.nullable) && self.heap.matches(&other.heap)
    }
}

impl ValType {
    /// Value subtyping: references by [`RefType::matches`], everything else by equality.
    pub(crate) fn matches(&self, other: &ValType) -> bool {
        match (self, other) {
            (ValType::Ref(a), ValType::Ref(b)) => a.matches(b),
            _ => self == other,
        }
    }
}

/// Whether every concrete type descriptor nested in `ty` belongs to `engine`. Canonical ids are
/// registry-local and must never cross an engine boundary, even when the reference is null.
pub(crate) fn val_type_belongs_to_engine(ty: &ValType, engine: &Engine) -> bool {
    let ValType::Ref(reference) = ty else {
        return true;
    };
    match reference.heap_type() {
        HeapType::ConcreteStruct(ty) => ty.engine().same(engine),
        HeapType::ConcreteArray(ty) => ty.engine().same(engine),
        HeapType::ConcreteFunc(ty) => ty.engine().same(engine),
        _ => true,
    }
}

/// A function signature — an engine-interned handle (identity by canonical type id; the
/// structure is materialized from the engine registry). Matches `wasmtime::FuncType`.
///
/// Refcounted (#27i): the handle holds one registration on its type's rec group; `Clone` increfs
/// and `Drop` decrefs, so the group is reclaimed once the last handle (and any `Module`/`Store`
/// holding it) is gone.
pub struct FuncType {
    engine: Engine,
    id: CanonicalTypeId,
}

impl FuncType {
    pub fn new(
        engine: &Engine,
        params: impl IntoIterator<Item = ValType>,
        results: impl IntoIterator<Item = ValType>,
    ) -> FuncType {
        let params: Vec<ValType> = params.into_iter().collect();
        let results: Vec<ValType> = results.into_iter().collect();
        assert!(
            params
                .iter()
                .chain(&results)
                .all(|ty| val_type_belongs_to_engine(ty, engine)),
            "function type contains a concrete type from a different engine"
        );
        // `intern_func_type` returns a type with one registration; this handle adopts it.
        let id = engine.intern_func_type(&params, &results);
        FuncType {
            engine: engine.clone(),
            id,
        }
    }

    /// Wraps an already-interned canonical id, taking a fresh registration (internal — used by the
    /// registry/module materialization boundary).
    pub(crate) fn from_id(engine: &Engine, id: CanonicalTypeId) -> FuncType {
        engine.incref_type(id);
        FuncType {
            engine: engine.clone(),
            id,
        }
    }

    /// The engine-canonical type id (internal identity).
    pub(crate) fn canonical_id(&self) -> CanonicalTypeId {
        self.id
    }

    pub(crate) fn engine(&self) -> &Engine {
        &self.engine
    }

    pub fn params(&self) -> impl ExactSizeIterator<Item = ValType> {
        self.signature().0.into_iter()
    }

    pub fn results(&self) -> impl ExactSizeIterator<Item = ValType> {
        self.signature().1.into_iter()
    }

    // This handle holds a reference on its type, so the lookup cannot miss while it lives; the
    // `wasmtime` signatures above have no error to report one with.
    fn signature(&self) -> (Vec<ValType>, Vec<ValType>) {
        self.engine
            .func_sig(self.id)
            .expect("live FuncType registration must retain its signature")
    }
}

impl Clone for FuncType {
    fn clone(&self) -> Self {
        self.engine.incref_type(self.id);
        FuncType {
            engine: self.engine.clone(),
            id: self.id,
        }
    }
}

impl Drop for FuncType {
    fn drop(&mut self) {
        self.engine.decref_type(self.id);
    }
}

impl PartialEq for FuncType {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

impl Eq for FuncType {}

impl core::hash::Hash for FuncType {
    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
        self.id.hash(state);
    }
}

impl core::fmt::Debug for FuncType {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("FuncType")
            .field("id", &self.id)
            .finish_non_exhaustive()
    }
}

/// A linear memory type (limits in 64 KiB pages).
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct MemoryType {
    minimum: u64,
    maximum: Option<u64>,
    memory64: bool,
}

impl MemoryType {
    pub fn new(minimum: u32, maximum: Option<u32>) -> MemoryType {
        MemoryType {
            minimum: u64::from(minimum),
            maximum: maximum.map(u64::from),
            memory64: false,
        }
    }

    pub fn new64(minimum: u64, maximum: Option<u64>) -> MemoryType {
        MemoryType {
            minimum,
            maximum,
            memory64: true,
        }
    }

    pub fn minimum(&self) -> u64 {
        self.minimum
    }

    pub fn maximum(&self) -> Option<u64> {
        self.maximum
    }

    pub fn is_64(&self) -> bool {
        self.memory64
    }
}

/// A global type: content type plus mutability. (Boundary type; internal storage is IR.)
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct GlobalType {
    content: ValType,
    mutability: Mutability,
}

impl GlobalType {
    pub fn new(content: ValType, mutability: Mutability) -> GlobalType {
        GlobalType {
            content,
            mutability,
        }
    }

    pub fn content(&self) -> &ValType {
        &self.content
    }

    pub fn mutability(&self) -> Mutability {
        self.mutability
    }
}

/// A table type: element reference type plus limits and index width. (Boundary type; internal
/// storage is IR.)
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct TableType {
    element: RefType,
    minimum: u64,
    maximum: Option<u64>,
    table64: bool,
}

impl TableType {
    pub fn new(element: RefType, min: u32, max: Option<u32>) -> TableType {
        TableType {
            element,
            minimum: u64::from(min),
            maximum: max.map(u64::from),
            table64: false,
        }
    }

    pub fn new64(element: RefType, min: u64, max: Option<u64>) -> TableType {
        TableType {
            element,
            minimum: min,
            maximum: max,
            table64: true,
        }
    }

    pub fn element(&self) -> &RefType {
        &self.element
    }

    pub fn minimum(&self) -> u64 {
        self.minimum
    }

    pub fn maximum(&self) -> Option<u64> {
        self.maximum
    }

    pub fn is_64(&self) -> bool {
        self.table64
    }
}

/// The type of an importable/exportable external item.
#[derive(Clone, Debug)]
pub enum ExternType {
    Func(FuncType),
    Memory(MemoryType),
    Global(GlobalType),
    Table(TableType),
    Tag(TagType),
}

/// Metadata about a module import.
#[derive(Clone, Debug)]
pub struct ImportType<'module> {
    module: &'module str,
    name: &'module str,
    ty: ExternType,
}

impl<'module> ImportType<'module> {
    pub(crate) fn new(module: &'module str, name: &'module str, ty: ExternType) -> Self {
        ImportType { module, name, ty }
    }

    pub fn module(&self) -> &'module str {
        self.module
    }

    pub fn name(&self) -> &'module str {
        self.name
    }

    pub fn ty(&self) -> ExternType {
        self.ty.clone()
    }
}

/// Metadata about a module export.
#[derive(Clone, Debug)]
pub struct ExportType<'module> {
    name: &'module str,
    ty: ExternType,
}

impl<'module> ExportType<'module> {
    pub(crate) fn new(name: &'module str, ty: ExternType) -> Self {
        ExportType { name, ty }
    }

    pub fn name(&self) -> &'module str {
        self.name
    }

    pub fn ty(&self) -> ExternType {
        self.ty.clone()
    }
}