arora-engine 5.5.0

The Arora engine: loads and executes Arora modules (wasm and native hosts).
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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
//! Browser-host integration tests for components: the test-rust-component
//! guest (a wasm32-wasip2 component implementing the `arora:module` WIT world)
//! loaded through
//! [`BrowserComponentExecutor`](arora_engine::executor::browser::BrowserComponentExecutor), dispatched, calling back into
//! the engine and reaching the WASI 0.2 interfaces — the same calls
//! `tests/tests/component.rs` makes through the native component executor.
//!
//! Like `browser.rs`, the file only exists on `wasm32` and runs under
//! `wasm-bindgen-test` in a browser.

#![cfg(target_arch = "wasm32")]

use std::pin::Pin;
use std::rc::Rc;

use arora_engine::call::{CallBridge, CallError, Callable, CallableId};
use arora_engine::compiled::CompiledModule;
use arora_engine::engine::{Engine, EngineBuilder};
use arora_engine::executor::browser::{BrowserExecutor, SharedLoaderRc};
use arora_engine::load::load_module_from_parts;
use arora_types::module::low::Header;
use arora_types::value::Value;
use uuid::Uuid;
use wasm_bindgen_test::*;

wasm_bindgen_test_configure!(run_in_browser);

// The test-rust-component guest, built for wasm32-wasip2 as a cdylib artifact
// dependency and wired in by Cargo.
const COMPONENT: &[u8] = include_bytes!(env!(
    "CARGO_CDYLIB_FILE_TEST_RUST_COMPONENT_test_rust_component"
));

/// Function ids implemented by modules/test-rust-component/src/lib.rs.
const ECHO_FN: &str = "dd6a4f61-9f88-4376-9d50-1e92e95a73aa";
const CALL_INDIRECT_FN: &str = "1bd1cf09-e94c-4c14-b653-32bd96157c41";
const WASI_FN: &str = "0455052e-bb89-41ec-a963-2bbe82ea583b";
const TRAP_FN: &str = "42681638-6fe3-4004-92fb-bb048dc3bbab";
const ERROR_BUFFER_FN: &str = "2041d791-4440-4dc9-b54d-1004e314cf99";
const CALL_FN: &str = "473a50b2-f275-4767-989c-69aa2c2def5b";

const MODULE_ID: &str = "0cd3f0c6-95d4-4ab1-90fa-7fbcca25c2c6";

/// What the guest's `wasi` function observes on a host that runs it without
/// arguments or an environment, as `tests/tests/component.rs` expects of the
/// native component executor.
const WASI_REPORT: &str = "\
monotonic-advances=true
monotonic-resolution-positive=true
wall-after-2020=true
wall-nanoseconds-below-a-second=true
wall-resolution-positive=true
random-bytes=40
insecure-random-bytes=3
environment=0
arguments=0
initial-cwd=None
preopened-directories=0
stdout-writable=true
stdout-flushes=true
stdout-writes-zeroes=true
standard-streams-written=true
poll-ready=[0, 1]
stdout-ready=true
past-instant-waited=true";

/// The guest's header, under `executor`, declaring `functions`.
fn header_with(executor: &str, functions: &[(&str, &str)]) -> Header {
    let exports: String = functions
        .iter()
        .map(|(id, name)| {
            format!(
                "  - type: function\n    id: {id}\n    name: {name}\n    parameters: []\n    \
                 ret: {{ kind: scalar, id: 00000000-0000-0000-0000-000000000000 }}\n"
            )
        })
        .collect();
    serde_yaml::from_str(&format!(
        r#"
id: {MODULE_ID}
name: test-rust-component
author: ""
description: ~
license: ""
version: {{ major: 0, minor: 0, patch: 0 }}
executor: {{ name: {executor}, min_version: ~, max_version: ~ }}
imports: []
executable_mime: ""
exports:
{exports}"#
    ))
    .expect("parse the test header")
}

fn header() -> Header {
    header_with(
        "wasm-component",
        &[
            (ECHO_FN, "echo"),
            (CALL_INDIRECT_FN, "call-indirect"),
            (WASI_FN, "wasi"),
            (TRAP_FN, "trap"),
            (ERROR_BUFFER_FN, "error-buffer"),
            (CALL_FN, "call"),
        ],
    )
}

fn id(s: &str) -> Uuid {
    Uuid::parse_str(s).expect("valid uuid")
}

/// An engine with both browser executors, sharing one loader.
fn engine() -> (Pin<Box<Engine>>, SharedLoaderRc) {
    let executor = BrowserExecutor::new();
    let loader = executor.shared();
    let components = executor.component_executor();
    let engine = EngineBuilder::new()
        .add_executor(executor)
        .add_executor(components)
        .build();
    (engine, loader)
}

/// An engine with the guest loaded from its bytes.
fn loaded() -> Pin<Box<Engine>> {
    let (mut engine, _) = engine();
    load_module_from_parts(&mut engine, header(), COMPONENT.into()).expect("load the component");
    engine
}

fn dispatch(engine: &mut Engine, function: &str, arg: &[u8]) -> Result<Box<[u8]>, String> {
    engine
        .dispatch(&id(MODULE_ID), &id(function), arg)
        .map_err(|e| format!("{e}"))
}

#[wasm_bindgen_test]
fn a_component_dispatches_in_browser() {
    console_error_panic_hook::set_once();
    let mut engine = loaded();
    let payload = b"hello component";
    let result = dispatch(&mut engine, ECHO_FN, payload).expect("dispatch echo");
    assert_eq!(&*result, payload);
}

/// A guest's `err` result reaches the caller as the guest's error, not a trap.
#[wasm_bindgen_test]
fn a_guest_error_reaches_the_caller_in_browser() {
    console_error_panic_hook::set_once();
    let mut engine = loaded();
    let message = dispatch(&mut engine, CALL_INDIRECT_FN, b"abc").unwrap_err();
    assert!(
        message.contains("8-byte callable id"),
        "unexpected error: {message}"
    );
}

struct FortyTwo;

impl Callable for FortyTwo {
    fn call(&self, _caller: &mut dyn CallBridge) -> Result<Value, CallError> {
        Ok(Value::U64(42))
    }
}

#[wasm_bindgen_test]
fn a_component_calls_back_through_dispatch_indirect_in_browser() {
    console_error_panic_hook::set_once();
    let mut engine = loaded();
    let callable: Rc<dyn Callable> = Rc::new(FortyTwo);
    let CallableId { id: callable } = engine.arora_register_callable(callable);

    let result =
        dispatch(&mut engine, CALL_INDIRECT_FN, &callable.to_le_bytes()).expect("call back");
    let expected = arora_buffers::serde_uuid::serialize(&Value::U64(42));
    assert_eq!(&*result, &*expected);
}

/// The browser lays out every WASI argument and result as the native executor
/// does: the guest observes the same.
#[wasm_bindgen_test]
fn a_component_reaches_the_wasi_interfaces_in_browser() {
    console_error_panic_hook::set_once();
    let mut engine = loaded();
    let report = dispatch(&mut engine, WASI_FN, &[]).expect("dispatch wasi");
    assert_eq!(std::str::from_utf8(&report).expect("utf-8"), WASI_REPORT);
}

/// The asynchronous loader path (`WebAssembly.compile` +
/// `WebAssembly.instantiate`) stages a component as it stages a core module.
#[wasm_bindgen_test]
async fn a_component_prepares_asynchronously_then_dispatches() {
    console_error_panic_hook::set_once();
    let (mut engine, loader) = engine();
    loader
        .prepare(id(MODULE_ID), COMPONENT.to_vec())
        .await
        .expect("async prepare");
    load_module_from_parts(&mut engine, header(), Box::new([]))
        .expect("load the asynchronously-prepared component");
    let result = dispatch(&mut engine, ECHO_FN, b"prepared").expect("dispatch echo");
    assert_eq!(&*result, b"prepared");
}

/// A component staged by the loader is not loaded as a core module.
#[wasm_bindgen_test]
async fn a_prepared_component_is_not_loaded_as_a_core_module() {
    console_error_panic_hook::set_once();
    let (mut engine, loader) = engine();
    loader
        .prepare(id(MODULE_ID), COMPONENT.to_vec())
        .await
        .expect("async prepare");
    let error = load_module_from_parts(
        &mut engine,
        header_with("wasm", &[(ECHO_FN, "echo")]),
        Box::new([]),
    )
    .expect_err("a component staged for the core executor fails to load");
    assert!(
        format!("{error}").contains("prepared as a component"),
        "unexpected error: {error}"
    );
}

/// One compiled component loads into several engines, each with an instance
/// of its own, and reloads in place.
#[wasm_bindgen_test]
fn a_compiled_component_loads_into_several_engines_and_reloads() {
    console_error_panic_hook::set_once();
    let compiled = CompiledModule::new(header(), COMPONENT).expect("compile the component");
    let load = || {
        let (mut engine, _) = engine();
        engine
            .load_compiled_module(&compiled)
            .expect("load the compiled component");
        engine
    };
    let first = load();
    let mut second = load();
    drop(first);

    for _ in 0..100 {
        second.reload_module(&compiled).expect("reload");
        let result = dispatch(&mut second, ECHO_FN, b"again").expect("dispatch echo");
        assert_eq!(&*result, b"again");
    }
    second.unload_module(&id(MODULE_ID)).expect("unload");
    assert!(!second.contains_module(&id(MODULE_ID)));
}

#[wasm_bindgen_test]
fn an_undeclared_function_is_not_found_in_a_component() {
    console_error_panic_hook::set_once();
    let (mut engine, _) = engine();
    load_module_from_parts(
        &mut engine,
        header_with("wasm-component", &[(ECHO_FN, "echo")]),
        COMPONENT.into(),
    )
    .expect("load");
    // The guest serves call-indirect, but this header does not declare it.
    let message = dispatch(&mut engine, CALL_INDIRECT_FN, &[]).unwrap_err();
    assert!(
        message.contains(CALL_INDIRECT_FN),
        "unexpected error: {message}"
    );
}

/// A function the header declares and the component's `functions` does not
/// return fails the load, as a core module missing an export does.
#[wasm_bindgen_test]
fn a_component_missing_a_declared_function_fails_to_load() {
    console_error_panic_hook::set_once();
    let (mut engine, _) = engine();
    let missing = "00000000-0000-0000-0000-000000000009";
    let error = load_module_from_parts(
        &mut engine,
        header_with("wasm-component", &[(ECHO_FN, "echo"), (missing, "missing")]),
        COMPONENT.into(),
    )
    .expect_err("the load fails");
    assert!(
        format!("{error}").contains(&format!("failed to find function export {missing}")),
        "unexpected error: {error}"
    );
}

/// A component of the `arora:module` world in WebAssembly text: the core
/// module `guest` (named `$guest`), its instance `$main`, and its `functions`
/// and `dispatch` lifted as `wit-component` lifts them, followed by `more` —
/// imports, modules and their instances.
fn world_component(guest: &str, more: &str) -> String {
    format!(
        r#"(component
            (type $types (instance
                (type (record (field "hi" u64) (field "lo" u64)))
                (export "id" (type (eq 0)))))
            (import "arora:module/types@0.2.0" (instance $types (type $types)))
            (alias export $types "id" (type $id))
            {guest}
            (core instance $main (instantiate $guest))
            (alias core export $main "memory" (core memory $memory))
            (alias core export $main "cabi_realloc" (core func $realloc))
            (type $ids (list $id))
            (type $functions (func (result $ids)))
            (func $functions (type $functions)
                (canon lift (core func $main "functions") (memory $memory)))
            (export "functions" (func $functions))
            (type $bytes (list u8))
            (type $result (result $bytes (error string)))
            (type $dispatch (func (param "method" $id) (param "arg" $bytes) (result $result)))
            (func $dispatch (type $dispatch)
                (canon lift (core func $main "dispatch") (memory $memory) (realloc $realloc)))
            (export "dispatch" (func $dispatch))
            {more})"#
    )
}

/// A guest module exporting what the world's lifts call, and importing
/// `imports`.
fn guest(imports: &str) -> String {
    format!(
        r#"(core module $guest
            {imports}
            (memory (export "memory") 1)
            (func (export "cabi_realloc") (param i32 i32 i32 i32) (result i32) i32.const 8)
            (func (export "functions") (result i32) i32.const 0)
            (func (export "dispatch") (param i64 i64 i32 i32) (result i32) i32.const 0))"#
    )
}

/// The error loading `component` (WebAssembly text) under the guest's header.
fn load_error(component: &str) -> String {
    let (mut engine, _) = engine();
    let bytes = wat::parse_str(component).expect("assemble the component");
    let error =
        load_module_from_parts(&mut engine, header(), bytes.into()).expect_err("the load fails");
    format!("{error}")
}

/// A component importing an interface the hosts do not provide is refused,
/// as the native executor's linker refuses it.
#[wasm_bindgen_test]
fn a_component_importing_what_the_browser_does_not_provide_is_refused() {
    console_error_panic_hook::set_once();
    let error = load_error(&world_component(
        &guest(""),
        r#"(import "wasi:sockets/tcp@0.2.0" (instance))"#,
    ));
    assert!(
        error.contains("imports wasi:sockets/tcp@0.2.0, which is not an interface"),
        "unexpected error: {error}"
    );
}

/// A guest module importing a function the browser does not implement fails
/// to load, naming it, whatever the component wires to it.
#[wasm_bindgen_test]
fn a_guest_module_importing_what_the_browser_does_not_provide_fails_to_load() {
    console_error_panic_hook::set_once();
    let error = load_error(&world_component(
        &guest(
            r#"(import "wasi:sockets/network@0.2.0" "[resource-drop]network" (func (param i32)))"#,
        ),
        "",
    ));
    assert!(
        error.contains("'[resource-drop]network' from 'wasi:sockets/network@0.2.0'"),
        "unexpected error: {error}"
    );
}

/// The filesystem has no directory, so no descriptor exists: a call on one
/// traps, as natively a call on an unknown handle does.
#[wasm_bindgen_test]
fn a_call_on_a_descriptor_traps_in_browser() {
    console_error_panic_hook::set_once();
    let function = "00000000-0000-0000-0000-000000000003";
    let (mut engine, _) = engine();
    let guest = r#"(core module $guest
        (import "wasi:filesystem/types@0.2.0" "[method]descriptor.stat" (func $stat (param i32 i32)))
        (memory (export "memory") 1)
        (data (i32.const 1024) "\00\00\00\00\00\00\00\00\03\00\00\00\00\00\00\00")
        (func (export "cabi_realloc") (param i32 i32 i32 i32) (result i32) i32.const 4096)
        (func (export "functions") (result i32)
            (i32.store (i32.const 2048) (i32.const 1024))
            (i32.store (i32.const 2052) (i32.const 1))
            (i32.const 2048))
        (func (export "dispatch") (param i64 i64 i32 i32) (result i32)
            (call $stat (i32.const 0) (i32.const 3072))
            (i32.const 2048)))"#;
    let bytes = wat::parse_str(world_component(guest, "")).expect("assemble the component");
    load_module_from_parts(
        &mut engine,
        header_with("wasm-component", &[(function, "f")]),
        bytes.into(),
    )
    .expect("load");
    let error = dispatch(&mut engine, function, &[]).unwrap_err();
    assert!(error.contains("no descriptor exists"), "{error}");
}

/// A component that does not export the world's `functions` is refused,
/// as the native executor refuses it.
#[wasm_bindgen_test]
fn a_component_without_the_world_s_exports_is_refused() {
    console_error_panic_hook::set_once();
    let component =
        world_component(&guest(""), "").replace(r#"(export "functions" (func $functions))"#, "");
    let error = load_error(&component);
    assert!(
        error.contains("where the world exports functions and dispatch"),
        "unexpected error: {error}"
    );
}

#[wasm_bindgen_test]
fn a_component_of_two_guest_modules_is_refused() {
    console_error_panic_hook::set_once();
    let error = load_error(
        r#"(component
            (core module (func (export "a")))
            (core module (func (export "b"))))"#,
    );
    assert!(
        error.contains("2 guest module(s)"),
        "unexpected error: {error}"
    );
}

#[wasm_bindgen_test]
fn a_component_nesting_a_component_is_refused() {
    console_error_panic_hook::set_once();
    let error = load_error("(component (component))");
    assert!(
        error.contains("nests a component"),
        "unexpected error: {error}"
    );
}

/// A reactor guest: its `_initialize` adds one to a count its one function
/// returns. Its component holds `wit-component`'s initializer
/// ([`INITIALIZER`]), which calls `_initialize`.
const REACTOR: &str = r#"(core module $guest
    (memory (export "memory") 1)
    (global $count (mut i32) (i32.const 0))
    (global $heap (mut i32) (i32.const 4096))
    (func (export "cabi_realloc") (param i32 i32 i32 i32) (result i32)
        (local $at i32)
        (local.set $at (global.get $heap))
        (global.set $heap (i32.add (global.get $heap) (local.get 3)))
        (local.get $at))
    (func (export "_initialize")
        (global.set $count (i32.add (global.get $count) (i32.const 1))))
    ;; The id of the one function: { hi: 0, lo: 3 }.
    (data (i32.const 1024) "\00\00\00\00\00\00\00\00\03\00\00\00\00\00\00\00")
    (func (export "functions") (result i32)
        (i32.store (i32.const 2048) (i32.const 1024))
        (i32.store (i32.const 2052) (i32.const 1))
        (i32.const 2048))
    (func (export "dispatch") (param i64 i64 i32 i32) (result i32)
        (i32.store8 (i32.const 3072) (global.get $count))
        (i32.store8 (i32.const 2048) (i32.const 0))
        (i32.store (i32.const 2052) (i32.const 3072))
        (i32.store (i32.const 2056) (i32.const 1))
        (i32.const 2048)))"#;

/// `wit-component`'s initializer — a module whose start function is the
/// import it is given — instantiated as `wit-component` instantiates it,
/// given the guest instance's `_initialize`.
const INITIALIZER: &str = r#"(core module $initializer
    (import "" "" (func $initialize))
    (start $initialize))
(alias core export $main "_initialize" (core func $initialize))
(core instance $initialize-args (export "" (func $initialize)))
(core instance (instantiate $initializer (with "" (instance $initialize-args))))"#;

/// A component that defines an initializer and never instantiates it, so that
/// natively nothing runs the guest's `_initialize`, is refused rather than
/// initialized.
#[wasm_bindgen_test]
fn a_component_with_an_initializer_it_never_instantiates_is_refused() {
    console_error_panic_hook::set_once();
    let unused = r#"(core module (import "" "" (func $initialize)) (start $initialize))"#;
    let error = load_error(&world_component(REACTOR, unused));
    assert!(error.contains("initializer"), "unexpected error: {error}");
}

/// A guest writing `len` zeroes to standard output with the function `write`
/// imports, under the function id whose low half is `len`: it serves `lens`.
fn zeroes_writer(write: &str, [a, b]: [u64; 2]) -> String {
    format!(
        r#"(core module $guest
            (import "wasi:cli/stdout@0.2.0" "get-stdout" (func $stdout (result i32)))
            (import "wasi:io/streams@0.2.0" "[method]output-stream.{write}"
                (func $write (param i32 i64 i32)))
            (memory (export "memory") 1)
            (func (export "cabi_realloc") (param i32 i32 i32 i32) (result i32) i32.const 4096)
            (func (export "functions") (result i32)
                (i64.store (i32.const 1024) (i64.const 0))
                (i64.store (i32.const 1032) (i64.const {a}))
                (i64.store (i32.const 1040) (i64.const 0))
                (i64.store (i32.const 1048) (i64.const {b}))
                (i32.store (i32.const 2048) (i32.const 1024))
                (i32.store (i32.const 2052) (i32.const 2))
                (i32.const 2048))
            (func (export "dispatch") (param i64 i64 i32 i32) (result i32)
                (call $write (call $stdout) (local.get 1) (i32.const 3072))
                (i32.store8 (i32.const 2048) (i32.const 0))
                (i32.store (i32.const 2052) (i32.const 0))
                (i32.store (i32.const 2056) (i32.const 0))
                (i32.const 2048)))"#
    )
}

/// An engine with `component` loaded under a header declaring the functions
/// whose ids' low halves are `functions`.
fn loaded_with(component: &str, functions: [u64; 2]) -> (Pin<Box<Engine>>, Vec<String>) {
    let (mut engine, _) = engine();
    let ids: Vec<String> = functions
        .iter()
        .map(|lo| Uuid::from_u64_pair(0, *lo).to_string())
        .collect();
    let declared: Vec<(&str, &str)> = ids.iter().map(|id| (id.as_str(), "f")).collect();
    let bytes = wat::parse_str(component).expect("assemble the component");
    load_module_from_parts(
        &mut engine,
        header_with("wasm-component", &declared),
        bytes.into(),
    )
    .expect("load");
    (engine, ids)
}

/// A guest with one page of memory and an allocator that grows it, asking for
/// as many random bytes as the low half of the function id it is called by.
fn random_reader([a, b]: [u64; 2]) -> String {
    format!(
        r#"(core module $guest
            (import "wasi:random/random@0.2.0" "get-random-bytes"
                (func $random (param i64 i32)))
            (memory (export "memory") 1)
            (func (export "cabi_realloc") (param i32 i32 i32 i32) (result i32)
                (i32.mul
                    (memory.grow (i32.add (i32.shr_u (local.get 3) (i32.const 16)) (i32.const 1)))
                    (i32.const 65536)))
            (func (export "functions") (result i32)
                (i64.store (i32.const 1024) (i64.const 0))
                (i64.store (i32.const 1032) (i64.const {a}))
                (i64.store (i32.const 1040) (i64.const 0))
                (i64.store (i32.const 1048) (i64.const {b}))
                (i32.store (i32.const 2048) (i32.const 1024))
                (i32.store (i32.const 2052) (i32.const 2))
                (i32.const 2048))
            (func (export "dispatch") (param i64 i64 i32 i32) (result i32)
                (call $random (local.get 1) (i32.const 3072))
                (i32.store8 (i32.const 2048) (i32.const 0))
                (i32.store (i32.const 2052) (i32.const 0))
                (i32.store (i32.const 2056) (i32.const 0))
                (i32.const 2048)))"#
    )
}

/// Random bytes more than the guest's memory holds are given it, its allocator
/// growing that memory, as natively; past what wasmtime returns at once, the
/// call traps, as natively.
#[wasm_bindgen_test]
fn random_bytes_the_guest_grows_its_memory_for_are_given_in_browser() {
    console_error_panic_hook::set_once();
    let lens = [1 << 20, (64 << 20) + 1];
    let (mut engine, ids) = loaded_with(&world_component(&random_reader(lens), ""), lens);
    dispatch(&mut engine, &ids[0], &[]).expect("many times the guest's memory");
    let error = dispatch(&mut engine, &ids[1], &[]).unwrap_err();
    assert!(error.contains("more than the 67108864"), "{error}");
}

/// Writing more zeroes than a stream writes at once traps, as natively, where
/// wasmtime writes none of them, rather than writing some.
#[wasm_bindgen_test]
fn writing_more_zeroes_than_a_stream_takes_at_once_traps_in_browser() {
    console_error_panic_hook::set_once();
    let lens = [1 << 16, 100_000];
    let component = world_component(&zeroes_writer("write-zeroes", lens), "");
    let (mut engine, ids) = loaded_with(&component, lens);
    dispatch(&mut engine, &ids[0], &[]).expect("as many as check-write permits");
    let error = dispatch(&mut engine, &ids[1], &[]).unwrap_err();
    assert!(error.contains("more than the 65536"), "{error}");

    let lens = [4096, 4097];
    let component = world_component(&zeroes_writer("blocking-write-zeroes-and-flush", lens), "");
    let (mut engine, ids) = loaded_with(&component, lens);
    dispatch(&mut engine, &ids[0], &[]).expect("as many as a blocking write takes");
    let error = dispatch(&mut engine, &ids[1], &[]).unwrap_err();
    assert!(error.contains("more than the 4096"), "{error}");
}

/// A reactor component's `_initialize` runs once, when it loads, before its
/// first call — not again before each call.
#[wasm_bindgen_test]
fn a_reactor_component_is_initialized_once_when_it_loads_in_browser() {
    console_error_panic_hook::set_once();
    let count = "00000000-0000-0000-0000-000000000003";
    let (mut engine, _) = engine();
    let bytes =
        wat::parse_str(world_component(REACTOR, INITIALIZER)).expect("assemble the component");
    load_module_from_parts(
        &mut engine,
        header_with("wasm-component", &[(count, "count")]),
        bytes.into(),
    )
    .expect("load");
    for _ in 0..2 {
        assert_eq!(&*dispatch(&mut engine, count, &[]).expect("dispatch"), &[1]);
    }
}

/// Calls the guest's `echo` through the engine, from inside a call into the
/// same guest: the echoed argument, a structure under the function's id,
/// reads back as the return value.
struct EchoThroughTheEngine;

impl Callable for EchoThroughTheEngine {
    fn call(&self, caller: &mut dyn CallBridge) -> Result<Value, CallError> {
        let echoed = caller.arora_call(arora_types::call::Call {
            module_id: Some(id(MODULE_ID)),
            id: id(ECHO_FN),
            args: vec![arora_types::value::StructureField {
                id: id(ECHO_FN),
                value: Box::new(Value::U64(42)),
            }],
        })?;
        Ok(echoed.ret)
    }
}

/// A guest that calls back into the engine can be called again before that
/// call returns, as the native component executor allows.
#[wasm_bindgen_test]
fn a_component_is_reentered_through_the_host_in_browser() {
    console_error_panic_hook::set_once();
    let mut engine = loaded();
    let callable: Rc<dyn Callable> = Rc::new(EchoThroughTheEngine);
    let CallableId { id: callable } = engine.arora_register_callable(callable);

    let result =
        dispatch(&mut engine, CALL_INDIRECT_FN, &callable.to_le_bytes()).expect("re-enter");
    let expected = arora_buffers::serde_uuid::serialize(&Value::U64(42));
    assert_eq!(&*result, &*expected);
}

/// A guest that returns an error buffer, as a core module writes its errors,
/// has it read as its error, as natively.
#[wasm_bindgen_test]
fn a_component_s_error_buffer_is_its_error_in_browser() {
    console_error_panic_hook::set_once();
    let mut engine = loaded();
    let error = engine
        .dispatch(&id(MODULE_ID), &id(ERROR_BUFFER_FN), b"no such thing")
        .unwrap_err();
    assert!(
        matches!(&error, arora_engine::module::DispatchError::Guest { message } if message == "no such thing"),
        "{error:?}"
    );
}

/// After a trap a component runs no call, saying why, until it is reloaded,
/// as natively.
#[wasm_bindgen_test]
fn a_component_that_traps_refuses_calls_until_it_is_reloaded_in_browser() {
    console_error_panic_hook::set_once();
    let compiled = CompiledModule::new(header(), COMPONENT).expect("compile the component");
    let (mut engine, _) = engine();
    engine.load_compiled_module(&compiled).expect("load");

    let trap = dispatch(&mut engine, TRAP_FN, &[]).unwrap_err();
    assert!(trap.contains("dispatch trapped"), "{trap}");
    let refused = dispatch(&mut engine, ECHO_FN, b"after").unwrap_err();
    assert!(refused.contains("trapped in an earlier call"), "{refused}");

    engine.reload_module(&compiled).expect("reload");
    assert_eq!(
        &*dispatch(&mut engine, ECHO_FN, b"after").expect("echo"),
        b"after"
    );
}

/// A guest calls a function of a module — here its own — through
/// `host.dispatch`, whose lowered form the browser implements.
#[wasm_bindgen_test]
fn a_component_calls_through_host_dispatch_in_browser() {
    console_error_panic_hook::set_once();
    let mut engine = loaded();
    let mut arg = id(MODULE_ID).as_bytes().to_vec();
    arg.extend_from_slice(id(ECHO_FN).as_bytes());
    arg.extend_from_slice(b"through the host");
    let result = dispatch(&mut engine, CALL_FN, &arg).expect("call through the host");
    assert_eq!(&*result, b"through the host");
}

/// A guest whose `err` is not UTF-8, which traps, as wasmtime's lift does.
const NOT_UTF_8: &str = r#"(core module $guest
    (memory (export "memory") 1)
    (data (i32.const 1024) "\00\00\00\00\00\00\00\00\03\00\00\00\00\00\00\00")
    (data (i32.const 3072) "\ff\fe")
    (func (export "cabi_realloc") (param i32 i32 i32 i32) (result i32) i32.const 4096)
    (func (export "functions") (result i32)
        (i32.store (i32.const 2048) (i32.const 1024))
        (i32.store (i32.const 2052) (i32.const 1))
        (i32.const 2048))
    (func (export "dispatch") (param i64 i64 i32 i32) (result i32)
        (i32.store8 (i32.const 2048) (i32.const 1))
        (i32.store (i32.const 2052) (i32.const 3072))
        (i32.store (i32.const 2056) (i32.const 2))
        (i32.const 2048)))"#;

#[wasm_bindgen_test]
fn a_component_s_error_that_is_not_utf_8_traps_in_browser() {
    console_error_panic_hook::set_once();
    let function = "00000000-0000-0000-0000-000000000003";
    let (mut engine, _) = engine();
    let bytes = wat::parse_str(world_component(NOT_UTF_8, "")).expect("assemble the component");
    load_module_from_parts(
        &mut engine,
        header_with("wasm-component", &[(function, "f")]),
        bytes.into(),
    )
    .expect("load");
    let error = dispatch(&mut engine, function, &[]).unwrap_err();
    assert!(error.contains("not UTF-8"), "{error}");
}