linera-witty 0.15.21

Generation of WIT compatible host code from Rust code
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
// Copyright (c) Zefchain Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

//! Helper code for testing using different runtimes.

use std::{
    any::Any,
    fmt::Debug,
    marker::PhantomData,
    ops::Add,
    sync::{
        atomic::{AtomicU32, Ordering},
        Arc,
    },
};

use frunk::{hlist, hlist_pat, HList};
#[cfg(with_wasmer)]
use linera_witty::wasmer;
#[cfg(with_wasmtime)]
use linera_witty::wasmtime;
use linera_witty::{
    ExportTo, InstanceWithMemory, Layout, MockExportedFunction, MockInstance, RuntimeError,
    WitLoad, WitStore,
};

/// Trait representing a type that can create instances for tests.
pub trait TestInstanceFactory {
    /// The type used to build a guest Wasm instance, which supports registration of exported host
    /// functions.
    type Builder;

    /// The type representing a guest Wasm instance.
    type Instance: InstanceWithMemory;

    /// The type received by host functions to use for reentrant calls.
    type Caller<'caller>;

    /// Loads a test module with the provided `module_name` from the named `group`
    fn load_test_module<ExportedFunctions>(
        &mut self,
        group: &str,
        module_name: &str,
    ) -> Self::Instance
    where
        ExportedFunctions: ExportTo<Self::Builder>;
}

/// A factory of [`wasmtime::Entrypoint`] instances.
#[cfg(with_wasmtime)]
#[derive(Default)]
pub struct WasmtimeInstanceFactory<UserData>(PhantomData<UserData>);

#[cfg(with_wasmtime)]
impl<UserData> TestInstanceFactory for WasmtimeInstanceFactory<UserData>
where
    UserData: Default + 'static,
{
    type Builder = ::wasmtime::Linker<UserData>;
    type Instance = wasmtime::EntrypointInstance<UserData>;
    type Caller<'caller> = ::wasmtime::Caller<'caller, UserData>;

    fn load_test_module<ExportedFunctions>(&mut self, group: &str, module: &str) -> Self::Instance
    where
        ExportedFunctions: ExportTo<Self::Builder>,
    {
        let engine = ::wasmtime::Engine::default();
        let module = ::wasmtime::Module::from_file(
            &engine,
            format!("../target/wasm32-unknown-unknown/debug/{group}-{module}.wasm"),
        )
        .expect("Failed to load module");

        let mut linker = wasmtime::Linker::new(&engine);

        ExportedFunctions::export_to(&mut linker)
            .expect("Failed to export functions to Wasmtime linker");

        let mut store = ::wasmtime::Store::new(&engine, UserData::default());
        let instance = linker
            .instantiate(&mut store, &module)
            .expect("Failed to instantiate module");

        wasmtime::EntrypointInstance::new(instance, store)
    }
}

/// A factory of [`wasmer::EntrypointInstance`]s.
#[cfg(with_wasmer)]
#[derive(Default)]
pub struct WasmerInstanceFactory<UserData>(PhantomData<UserData>);

#[cfg(with_wasmer)]
impl<UserData> TestInstanceFactory for WasmerInstanceFactory<UserData>
where
    UserData: Default + Send + 'static,
{
    type Builder = wasmer::InstanceBuilder<UserData>;
    type Instance = wasmer::EntrypointInstance<UserData>;
    type Caller<'caller> = ::wasmer::FunctionEnvMut<'caller, wasmer::Environment<UserData>>;

    fn load_test_module<ExportedFunctions>(&mut self, group: &str, module: &str) -> Self::Instance
    where
        ExportedFunctions: ExportTo<Self::Builder>,
    {
        let engine = ::wasmer::sys::EngineBuilder::new(::wasmer::Singlepass::default())
            .engine()
            .into();
        let module = ::wasmer::Module::from_file(
            &engine,
            format!("../target/wasm32-unknown-unknown/debug/{group}-{module}.wasm"),
        )
        .expect("Failed to load module");

        let mut builder = wasmer::InstanceBuilder::new(engine, UserData::default());

        ExportedFunctions::export_to(&mut builder)
            .expect("Failed to export functions to Wasmer instance builder");

        builder
            .instantiate(&module)
            .expect("Failed to instantiate module")
    }
}

/// A factory of [`MockInstance`]s.
#[derive(Default)]
pub struct MockInstanceFactory<UserData = ()> {
    deferred_assertions: Vec<Box<dyn Any>>,
    user_data: PhantomData<UserData>,
}

impl<UserData> TestInstanceFactory for MockInstanceFactory<UserData>
where
    UserData: Default + 'static,
{
    type Builder = MockInstance<UserData>;
    type Instance = MockInstance<UserData>;
    type Caller<'caller> = MockInstance<UserData>;

    fn load_test_module<ExportedFunctions>(&mut self, group: &str, module: &str) -> Self::Instance
    where
        ExportedFunctions: ExportTo<Self::Builder>,
    {
        let mut instance = MockInstance::default();

        match (group, module) {
            ("export", "simple-function") => self.export_simple_function(&mut instance),
            ("export", "getters") => self.export_getters(&mut instance),
            ("export", "setters") => self.export_setters(&mut instance),
            ("export", "operations") => self.export_operations(&mut instance),
            ("import", "simple-function") => self.import_simple_function(&mut instance),
            ("import", "getters") => self.import_getters(&mut instance),
            ("import", "setters") => self.import_setters(&mut instance),
            ("import", "operations") => self.import_operations(&mut instance),
            ("reentrancy", "simple-function") => self.reentrancy_simple_function(&mut instance),
            ("reentrancy", "getters") => self.reentrancy_getters(&mut instance),
            ("reentrancy", "setters") => self.reentrancy_setters(&mut instance),
            ("reentrancy", "operations") => self.reentrancy_operations(&mut instance),
            ("reentrancy", "global-state") => self.reentrancy_global_state(&mut instance),
            _ => panic!(
                "Attempt to load module \"{group}-{module}\" which has no mock configuration"
            ),
        }

        ExportedFunctions::export_to(&mut instance)
            .expect("Failed to export functions to mock instance");

        instance
    }
}

impl<UserData> MockInstanceFactory<UserData>
where
    UserData: 'static,
{
    /// Mock the exported functions from the "export-simple-function" module.
    fn export_simple_function(&mut self, instance: &mut MockInstance<UserData>) {
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/simple-function#simple",
            |_, _: HList![]| Ok(hlist![]),
            1,
        );
    }

    /// Mock the exported functions from the "export-getters" module.
    fn export_getters(&mut self, instance: &mut MockInstance<UserData>) {
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/getters#get-true",
            |_, _: HList![]| Ok(hlist![1_i32]),
            1,
        );
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/getters#get-false",
            |_, _: HList![]| Ok(hlist![0_i32]),
            1,
        );
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/getters#get-s8",
            |_, _: HList![]| Ok(hlist![-125_i8 as u8 as i32]),
            1,
        );
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/getters#get-u8",
            |_, _: HList![]| Ok(hlist![200_u8 as i32]),
            1,
        );
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/getters#get-s16",
            |_, _: HList![]| Ok(hlist![-410_i16 as u16 as i32]),
            1,
        );
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/getters#get-u16",
            |_, _: HList![]| Ok(hlist![60_000_u16 as i32]),
            1,
        );
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/getters#get-s32",
            |_, _: HList![]| Ok(hlist![-100_000_i32]),
            1,
        );
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/getters#get-u32",
            |_, _: HList![]| Ok(hlist![3_000_111_i32]),
            1,
        );
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/getters#get-s64",
            |_, _: HList![]| Ok(hlist![-5_000_000_i64]),
            1,
        );
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/getters#get-u64",
            |_, _: HList![]| Ok(hlist![10_000_000_000_i64]),
            1,
        );
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/getters#get-float32",
            |_, _: HList![]| Ok(hlist![-0.125_f32]),
            1,
        );
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/getters#get-float64",
            |_, _: HList![]| Ok(hlist![128.25_f64]),
            1,
        );
    }

    /// Mock the exported functions from the "export-setters" module.
    fn export_setters(&mut self, instance: &mut MockInstance<UserData>) {
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/setters#set-bool",
            |_, hlist_pat![parameter]: HList![i32]| {
                assert_eq!(parameter, 0);
                Ok(hlist![])
            },
            1,
        );
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/setters#set-s8",
            |_, hlist_pat![parameter]: HList![i32]| {
                assert_eq!(parameter, -100_i8 as i32);
                Ok(hlist![])
            },
            1,
        );
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/setters#set-u8",
            |_, hlist_pat![parameter]: HList![i32]| {
                assert_eq!(parameter, 201);
                Ok(hlist![])
            },
            1,
        );
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/setters#set-s16",
            |_, hlist_pat![parameter]: HList![i32]| {
                assert_eq!(parameter, -20_000_i16 as i32);
                Ok(hlist![])
            },
            1,
        );
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/setters#set-u16",
            |_, hlist_pat![parameter]: HList![i32]| {
                assert_eq!(parameter, 50_000);
                Ok(hlist![])
            },
            1,
        );
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/setters#set-s32",
            |_, hlist_pat![parameter]: HList![i32]| {
                assert_eq!(parameter, -2_000_000);
                Ok(hlist![])
            },
            1,
        );
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/setters#set-u32",
            |_, hlist_pat![parameter]: HList![i32]| {
                assert_eq!(parameter, 4_000_000_u32 as i32);
                Ok(hlist![])
            },
            1,
        );
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/setters#set-s64",
            |_, hlist_pat![parameter]: HList![i64]| {
                assert_eq!(parameter, -25_000_000_000);
                Ok(hlist![])
            },
            1,
        );
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/setters#set-u64",
            |_, hlist_pat![parameter]: HList![i64]| {
                assert_eq!(parameter, 7_000_000_000);
                Ok(hlist![])
            },
            1,
        );
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/setters#set-float32",
            |_, hlist_pat![parameter]: HList![f32]| {
                assert_eq!(parameter, 10.4);
                Ok(hlist![])
            },
            1,
        );
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/setters#set-float64",
            |_, hlist_pat![parameter]: HList![f64]| {
                assert_eq!(parameter, -0.000_08);
                Ok(hlist![])
            },
            1,
        );
    }

    /// Mock the exported functions from the "operations" module.
    fn export_operations(&mut self, instance: &mut MockInstance<UserData>) {
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/operations#and-bool",
            |_, hlist_pat![first, second]: HList![i32, i32]| {
                Ok(hlist![if first != 0 && second != 0 { 1 } else { 0 }])
            },
            2,
        );
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/operations#add-s8",
            |_, hlist_pat![first, second]: HList![i32, i32]| Ok(hlist![first + second]),
            1,
        );
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/operations#add-u8",
            |_, hlist_pat![first, second]: HList![i32, i32]| Ok(hlist![first + second]),
            1,
        );
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/operations#add-s16",
            |_, hlist_pat![first, second]: HList![i32, i32]| Ok(hlist![first + second]),
            1,
        );
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/operations#add-u16",
            |_, hlist_pat![first, second]: HList![i32, i32]| Ok(hlist![first + second]),
            1,
        );
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/operations#add-s32",
            |_, hlist_pat![first, second]: HList![i32, i32]| Ok(hlist![first + second]),
            1,
        );
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/operations#add-u32",
            |_, hlist_pat![first, second]: HList![i32, i32]| Ok(hlist![first + second]),
            1,
        );
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/operations#add-s64",
            |_, hlist_pat![first, second]: HList![i64, i64]| Ok(hlist![first + second]),
            1,
        );
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/operations#add-u64",
            |_, hlist_pat![first, second]: HList![i64, i64]| Ok(hlist![first + second]),
            1,
        );
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/operations#add-float32",
            |_, hlist_pat![first, second]: HList![f32, f32]| Ok(hlist![first + second]),
            1,
        );
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/operations#add-float64",
            |_, hlist_pat![first, second]: HList![f64, f64]| Ok(hlist![first + second]),
            1,
        );
    }

    /// Mock calling the imported function in the "import-simple-function" module.
    fn import_simple_function(&mut self, instance: &mut MockInstance<UserData>) {
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/entrypoint#entrypoint",
            |caller, _: HList![]| {
                let hlist_pat![] = caller.call_imported_function(
                    "witty-macros:test-modules/simple-function#simple",
                    hlist![],
                )?;
                Ok(hlist![])
            },
            1,
        );
    }

    /// Mock calling the imported functions in the "import-getters" module.
    fn import_getters(&mut self, instance: &mut MockInstance<UserData>) {
        fn check_getter<Value, UserData>(
            caller: &MockInstance<UserData>,
            name: &str,
            expected_value: &Value,
        ) where
            Value: Debug + PartialEq + WitLoad + 'static,
        {
            let value: Value = caller
                .call_imported_function(
                    &format!("witty-macros:test-modules/getters#{name}"),
                    hlist![],
                )
                .unwrap_or_else(|error| panic!("Failed to call getter function {name:?}: {error}"));

            assert_eq!(&value, expected_value);
        }

        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/entrypoint#entrypoint",
            |caller, _: HList![]| {
                check_getter(&caller, "get-true", &true);
                check_getter(&caller, "get-false", &false);
                check_getter(&caller, "get-s8", &-125_i8);
                check_getter(&caller, "get-u8", &200_u8);
                check_getter(&caller, "get-s16", &-410_i16);
                check_getter(&caller, "get-u16", &60_000_u16);
                check_getter(&caller, "get-s32", &-100_000_i32);
                check_getter(&caller, "get-u32", &3_000_111_u32);
                check_getter(&caller, "get-s64", &-5_000_000_i64);
                check_getter(&caller, "get-u64", &10_000_000_000_u64);
                check_getter(&caller, "get-float32", &-0.125_f32);
                check_getter(&caller, "get-float64", &128.25_f64);

                Ok(hlist![])
            },
            1,
        );
    }

    /// Mock calling the imported functions in the "import-setters" module.
    fn import_setters(&mut self, instance: &mut MockInstance<UserData>) {
        fn send_to_setter<Value, UserData>(
            caller: &MockInstance<UserData>,
            name: &str,
            value: Value,
        ) where
            Value: WitStore + 'static,
            Value::Layout: Add<HList![]>,
            <Value::Layout as Add<HList![]>>::Output:
                Layout<Flat = <<Value::Layout as Layout>::Flat as Add<HList![]>>::Output>,
            <Value::Layout as Layout>::Flat: Add<HList![]>,
        {
            let () = caller
                .call_imported_function(
                    &format!("witty-macros:test-modules/setters#{name}"),
                    hlist![value],
                )
                .unwrap_or_else(|error| panic!("Failed to call setter function {name:?}: {error}"));
        }

        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/entrypoint#entrypoint",
            |caller, _: HList![]| {
                send_to_setter(&caller, "set-bool", false);
                send_to_setter(&caller, "set-s8", -100_i8);
                send_to_setter(&caller, "set-u8", 201_u8);
                send_to_setter(&caller, "set-s16", -20_000_i16);
                send_to_setter(&caller, "set-u16", 50_000_u16);
                send_to_setter(&caller, "set-s32", -2_000_000_i32);
                send_to_setter(&caller, "set-u32", 4_000_000_u32);
                send_to_setter(&caller, "set-s64", -25_000_000_000_i64);
                send_to_setter(&caller, "set-u64", 7_000_000_000_u64);
                send_to_setter(&caller, "set-float32", 10.4_f32);
                send_to_setter(&caller, "set-float64", -0.000_08_f64);

                Ok(hlist![])
            },
            1,
        );
    }

    /// Mock calling the imported functions in the "import-operations".
    fn import_operations(&mut self, instance: &mut MockInstance<UserData>) {
        fn check_operation<Value, UserData>(
            caller: &MockInstance<UserData>,
            name: &str,
            operands: impl WitStore + 'static,
            expected_result: &Value,
        ) where
            Value: Debug + PartialEq + WitLoad + 'static,
        {
            let result: Value = caller
                .call_imported_function(
                    &format!("witty-macros:test-modules/operations#{name}"),
                    operands,
                )
                .unwrap_or_else(|error| panic!("Failed to call setter function {name:?}: {error}"));

            assert_eq!(&result, expected_result);
        }

        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/entrypoint#entrypoint",
            |caller, _: HList![]| {
                check_operation(&caller, "and-bool", (true, true), &true);
                check_operation(&caller, "and-bool", (true, false), &false);
                check_operation(&caller, "add-s8", (-100_i8, 40_i8), &-60_i8);
                check_operation(&caller, "add-u8", (201_u8, 32_u8), &233_u8);
                check_operation(&caller, "add-s16", (-20_000_i16, 30_000_i16), &10_000_i16);
                check_operation(&caller, "add-u16", (50_000_u16, 256_u16), &50_256_u16);
                check_operation(
                    &caller,
                    "add-s32",
                    (-2_000_000_i32, -1_i32),
                    &-2_000_001_i32,
                );
                check_operation(&caller, "add-u32", (4_000_000_u32, 1_u32), &4_000_001_u32);
                check_operation(
                    &caller,
                    "add-s64",
                    (-16_000_000_i64, 32_000_000_i64),
                    &16_000_000_i64,
                );
                check_operation(
                    &caller,
                    "add-u64",
                    (3_000_000_000_u64, 9_345_678_999_u64),
                    &12_345_678_999_u64,
                );
                check_operation(&caller, "add-float32", (10.5_f32, 120.25_f32), &130.75_f32);
                check_operation(
                    &caller,
                    "add-float64",
                    (-0.000_08_f64, 1.0_f64),
                    &0.999_92_f64,
                );

                Ok(hlist![])
            },
            1,
        );
    }

    /// Mock the behavior of the "reentrancy-simple-function" module.
    fn reentrancy_simple_function(&mut self, instance: &mut MockInstance<UserData>) {
        self.import_simple_function(instance);
        self.export_simple_function(instance);
    }

    /// Mock the behavior of the "reentrancy-getters" module.
    fn reentrancy_getters(&mut self, instance: &mut MockInstance<UserData>) {
        self.import_getters(instance);
        self.export_getters(instance);
    }

    /// Mock the behavior of the "reentrancy-setters" module.
    fn reentrancy_setters(&mut self, instance: &mut MockInstance<UserData>) {
        self.import_setters(instance);
        self.export_setters(instance);
    }

    /// Mock the behavior of the "reentrancy-operations" module.
    fn reentrancy_operations(&mut self, instance: &mut MockInstance<UserData>) {
        self.import_operations(instance);
        self.export_operations(instance);
    }

    /// Mock the behavior of the "reentrancy-global-state" module.
    fn reentrancy_global_state(&mut self, instance: &mut MockInstance<UserData>) {
        let global_state_for_entrypoint = Arc::new(AtomicU32::new(0));
        let global_state_for_getter = global_state_for_entrypoint.clone();

        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/global-state#entrypoint",
            move |caller, hlist_pat![value]: HList![i32]| {
                global_state_for_entrypoint.store(value as u32, Ordering::Release);

                caller
                    .call_imported_function(
                        "witty-macros:test-modules/get-host-value#get-host-value",
                        (),
                    )
                    .map(|value: u32| hlist![value as i32])
            },
            1,
        );
        self.mock_exported_function(
            instance,
            "witty-macros:test-modules/global-state#get-global-state",
            move |_, _: HList![]| {
                Ok(hlist![
                    global_state_for_getter.load(Ordering::Acquire) as i32
                ])
            },
            1,
        );
    }

    /// Mocks an exported function with the provided `name`.
    ///
    /// The `handler` is used when the exported function is called, which expected to happen
    /// `expected_calls` times.
    ///
    /// The created [`MockExportedFunction`] is automatically registered in the `instance` and
    /// added to the current list of deferred assertions, to be checked when the test finishes.
    fn mock_exported_function<Parameters, Results>(
        &mut self,
        instance: &mut MockInstance<UserData>,
        name: &str,
        handler: impl Fn(MockInstance<UserData>, Parameters) -> Result<Results, RuntimeError> + 'static,
        expected_calls: usize,
    ) where
        Parameters: 'static,
        Results: 'static,
    {
        let mock_exported_function = MockExportedFunction::new(name, handler, expected_calls);

        mock_exported_function.register(instance);

        self.deferred_assertions
            .push(Box::new(mock_exported_function));
    }
}

/// Marker type to indicate no extra functions should be exported to the Wasm instance.
#[allow(dead_code)]
pub struct WithoutExports;

impl<T> ExportTo<T> for WithoutExports {
    fn export_to(_target: &mut T) -> Result<(), RuntimeError> {
        Ok(())
    }
}