wirm 3.0.0

A lightweight WebAssembly Transformation Library for the Component Model
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
use crate::ir::component::visitor::driver::VisitEvent;
use crate::ir::component::visitor::events_structural::get_structural_events;
use crate::ir::component::visitor::events_topological::get_topological_events;
use crate::ir::component::visitor::VisitCtx;
use crate::{Component, Module};
use serde_json::Value;
use std::fs;
use std::path::Path;
use std::process::Command;
use wasmparser::{ComponentTypeDeclaration, InstanceTypeDeclaration};

const WASM_TOOLS_TEST_COMP_INPUTS: &str = "./tests/wasm-tools/component-model";

#[test]
fn test_equivalent_visit_events_wast_components() {
    let path_str = WASM_TOOLS_TEST_COMP_INPUTS.to_string();
    tests_from_wast(Path::new(&path_str), test_event_generation);
}

#[test]
fn test_equivalent_visit_events_wast_components_async() {
    let path_str = format!("{WASM_TOOLS_TEST_COMP_INPUTS}/async");
    tests_from_wast(Path::new(&path_str), test_event_generation);
}

#[test]
fn test_equivalent_visit_events_wast_components_error_context() {
    let path_str = format!("{WASM_TOOLS_TEST_COMP_INPUTS}/error-context");
    tests_from_wast(Path::new(&path_str), test_event_generation);
}

#[test]
fn test_equivalent_visit_events_wast_components_gc() {
    let path_str = format!("{WASM_TOOLS_TEST_COMP_INPUTS}/gc");
    tests_from_wast(Path::new(&path_str), test_event_generation);
}

#[test]
fn test_equivalent_visit_events_wast_components_shared() {
    let path_str = format!("{WASM_TOOLS_TEST_COMP_INPUTS}/shared-everything-threads");
    tests_from_wast(Path::new(&path_str), test_event_generation);
}

#[test]
fn test_equivalent_visit_events_wast_components_values() {
    let path_str = format!("{WASM_TOOLS_TEST_COMP_INPUTS}/values");
    tests_from_wast(Path::new(&path_str), test_event_generation);
}

fn get_events<'ir>(
    comp: &'ir Component<'ir>,
    get_evts: fn(&'ir Component<'ir>, &mut VisitCtx<'ir>, &mut Vec<VisitEvent<'ir>>),
) -> Vec<VisitEvent<'ir>> {
    let mut ctx = VisitCtx::new(comp);
    let mut events = Vec::new();
    get_evts(comp, &mut ctx, &mut events);

    events
}

fn check_event_validity(evts0: &Vec<VisitEvent>, evts1: &Vec<VisitEvent>) {
    check_validity_of(evts0);

    // Now we know that the events of evts0 is valid, if they are equal to evts1, then we know
    // that evts1 is valid!
    check_equality(evts0, evts1);
}

/// Events are VALID if:
/// 1. every enter* is paired with an exit*
/// 2. recgroup subtypes only appear between enter_recgroup and exit_recgroup
/// 3. mod type decls only appear between enter/exit core type
/// 4. comp and inst type decls only appear between enter/exit comp type
///   - if the decl contains a comp type, the next event is enter_comp_type
///   - if the decl contains a core type, the next event is enter_core_type
fn check_validity_of(evts: &Vec<VisitEvent>) {
    let mut stack = vec![];
    let mut next_is_enter_comp_type = false;
    let mut next_is_enter_core_type = false;

    for evt in evts.iter() {
        if next_is_enter_comp_type {
            assert!(is_comp_ty_enter(evt),
                "Had a declaration with an inner component type, but the next event was not an enter component type event."
            );
            next_is_enter_comp_type = false;
        }
        if next_is_enter_core_type {
            assert!(is_core_ty_enter(evt),
                    "Had a declaration with an inner core type, but the next event was not an enter core type event."
            );
            next_is_enter_core_type = false;
        }

        if is_enter_evt(evt) {
            stack.push(evt);
        }
        // 1. every enter is paired with an exit
        if is_exit_evt(evt) {
            let enter = stack.last().unwrap();
            assert!(
                enter_exit_match(stack.last().unwrap(), evt),
                "Received mismatched enter/exit events:\n- enter: {enter:?}\n- exit: {evt:?}"
            );
            stack.pop();
        }

        // 2. recgroup subtypes only appear between enter_recgroup and exit_recgroup
        if is_subtype(evt) {
            assert!(
                is_recgroup_enter(stack.last().unwrap()),
                "Received a recgroup subtype event without a recgroup enter event!"
            );
        }

        // 3. mod type decls only appear between enter/exit core type
        if is_mod_decl(evt) {
            assert!(
                is_core_ty_enter(stack.last().unwrap()),
                "Received a module type decl without a core type enter event!"
            );
        }

        // 4. comp and inst type decls only appear between enter/exit comp type
        if is_comp_ty_decl(evt) || is_inst_ty_decl(evt) {
            assert!(
                is_comp_ty_enter(stack.last().unwrap()),
                "Received a component or instance type decl without a comp type enter event!"
            );
            // - if the decl contains a comp type, the next event is enter_comp_type
            if decl_contains_inner_comp_ty(evt) {
                next_is_enter_comp_type = true;
            } else if decl_contains_inner_core_ty(evt) {
                // - if the decl contains a core type, the next event is enter_core_type
                next_is_enter_core_type = true;
            }
        }
    }
}
fn is_enter_evt(evt: &VisitEvent) -> bool {
    matches!(
        evt,
        VisitEvent::EnterRootComp { .. }
            | VisitEvent::EnterComp { .. }
            | VisitEvent::EnterCompType { .. }
            | VisitEvent::EnterCoreType { .. }
            | VisitEvent::EnterCoreRecGroup { .. }
    )
}
fn is_exit_evt(evt: &VisitEvent) -> bool {
    matches!(
        evt,
        VisitEvent::ExitRootComp { .. }
            | VisitEvent::ExitComp { .. }
            | VisitEvent::ExitCompType { .. }
            | VisitEvent::ExitCoreType { .. }
            | VisitEvent::ExitCoreRecGroup { .. }
    )
}
fn is_subtype(evt: &VisitEvent) -> bool {
    matches!(evt, VisitEvent::CoreSubtype { .. })
}
fn is_recgroup_enter(evt: &VisitEvent) -> bool {
    matches!(evt, VisitEvent::EnterCoreRecGroup { .. })
}
fn is_mod_decl(evt: &VisitEvent) -> bool {
    matches!(evt, VisitEvent::ModuleTypeDecl { .. })
}
fn is_core_ty_enter(evt: &VisitEvent) -> bool {
    matches!(evt, VisitEvent::EnterCoreType { .. })
}
fn is_comp_ty_decl(evt: &VisitEvent) -> bool {
    matches!(evt, VisitEvent::CompTypeDecl { .. })
}
fn is_inst_ty_decl(evt: &VisitEvent) -> bool {
    matches!(evt, VisitEvent::InstTypeDecl { .. })
}
fn is_comp_ty_enter(evt: &VisitEvent) -> bool {
    matches!(evt, VisitEvent::EnterCompType { .. })
}
fn decl_contains_inner_comp_ty(evt: &VisitEvent) -> bool {
    match evt {
        VisitEvent::CompTypeDecl { decl, .. } => matches!(decl, ComponentTypeDeclaration::Type(_)),
        VisitEvent::InstTypeDecl { decl, .. } => matches!(decl, InstanceTypeDeclaration::Type(_)),
        _ => false,
    }
}
fn decl_contains_inner_core_ty(evt: &VisitEvent) -> bool {
    match evt {
        VisitEvent::CompTypeDecl { decl, .. } => {
            matches!(decl, ComponentTypeDeclaration::CoreType(_))
        }
        VisitEvent::InstTypeDecl { decl, .. } => {
            matches!(decl, InstanceTypeDeclaration::CoreType(_))
        }
        _ => false,
    }
}

fn enter_exit_match(enter: &VisitEvent, exit: &VisitEvent) -> bool {
    matches!(
        (enter, exit),
        (
            VisitEvent::EnterRootComp { .. },
            VisitEvent::ExitRootComp { .. }
        ) | (VisitEvent::EnterComp { .. }, VisitEvent::ExitComp { .. })
            | (
                VisitEvent::EnterCompType { .. },
                VisitEvent::ExitCompType { .. }
            )
            | (
                VisitEvent::EnterCoreRecGroup { .. },
                VisitEvent::ExitCoreRecGroup { .. }
            )
            | (
                VisitEvent::EnterCoreType { .. },
                VisitEvent::ExitCoreType { .. }
            )
    )
}

fn check_equality(evts0: &Vec<VisitEvent>, evts1: &Vec<VisitEvent>) {
    for (a, b) in evts0.iter().zip(evts1.iter()) {
        match (a, b) {
            (
                VisitEvent::EnterRootComp { component: a_comp },
                VisitEvent::EnterRootComp { component: b_comp },
            ) => {
                assert_eq!(a_comp.id, b_comp.id);
                // check pointing to same memory region
                assert_eq!(*a_comp as *const Component, *b_comp as *const Component);
            }
            (
                VisitEvent::ExitRootComp { component: a_comp },
                VisitEvent::ExitRootComp { component: b_comp },
            ) => {
                assert_eq!(a_comp.id, b_comp.id);
            }
            (
                VisitEvent::EnterComp {
                    idx: a_idx,
                    component: a_comp,
                },
                VisitEvent::EnterComp {
                    idx: b_idx,
                    component: b_comp,
                },
            ) => {
                assert_eq!(a_idx, b_idx);
                assert_eq!(a_comp.id, b_comp.id);
            }
            (
                VisitEvent::ExitComp {
                    idx: a_idx,
                    component: a_comp,
                },
                VisitEvent::ExitComp {
                    idx: b_idx,
                    component: b_comp,
                },
            ) => {
                assert_eq!(a_idx, b_idx);
                assert_eq!(a_comp.id, b_comp.id);
            }
            (
                VisitEvent::Module {
                    idx: a_idx,
                    module: a_mod,
                },
                VisitEvent::Module {
                    idx: b_idx,
                    module: b_mod,
                },
            ) => {
                assert_eq!(a_idx, b_idx);
                // check pointing to same memory region
                assert_eq!(*a_mod as *const Module, *b_mod as *const Module);
            }
            (
                VisitEvent::EnterCompType {
                    idx: a_idx,
                    ty: a_ty,
                },
                VisitEvent::EnterCompType {
                    idx: b_idx,
                    ty: b_ty,
                },
            ) => {
                assert_eq!(a_idx, b_idx);
                assert_eq!(a_ty, b_ty);
            }
            (
                VisitEvent::ExitCompType {
                    idx: a_idx,
                    ty: a_ty,
                },
                VisitEvent::ExitCompType {
                    idx: b_idx,
                    ty: b_ty,
                },
            ) => {
                assert_eq!(a_idx, b_idx);
                assert_eq!(a_ty, b_ty);
            }
            (
                VisitEvent::CompTypeDecl {
                    parent: a_parent,
                    idx: a_idx,
                    decl: a_decl,
                },
                VisitEvent::CompTypeDecl {
                    parent: b_parent,
                    idx: b_idx,
                    decl: b_decl,
                },
            ) => {
                assert_eq!(a_parent, b_parent);
                assert_eq!(a_idx, b_idx);
                assert_eq!(a_decl, b_decl);
            }
            (
                VisitEvent::InstTypeDecl {
                    parent: a_parent,
                    idx: a_idx,
                    decl: a_decl,
                },
                VisitEvent::InstTypeDecl {
                    parent: b_parent,
                    idx: b_idx,
                    decl: b_decl,
                },
            ) => {
                assert_eq!(a_parent, b_parent);
                assert_eq!(a_idx, b_idx);
                assert_eq!(a_decl, b_decl);
            }
            (
                VisitEvent::CompInst {
                    idx: a_idx,
                    inst: a_inst,
                },
                VisitEvent::CompInst {
                    idx: b_idx,
                    inst: b_inst,
                },
            ) => {
                assert_eq!(a_idx, b_idx);
                assert_eq!(a_inst, b_inst);
            }
            (
                VisitEvent::Canon {
                    kind: a_kind,
                    idx: a_idx,
                    canon: a_canon,
                },
                VisitEvent::Canon {
                    kind: b_kind,
                    idx: b_idx,
                    canon: b_canon,
                },
            ) => {
                assert_eq!(a_kind, b_kind);
                assert_eq!(a_idx, b_idx);
                assert_eq!(a_canon, b_canon);
            }
            (
                VisitEvent::Alias {
                    kind: a_kind,
                    idx: a_idx,
                    alias: a_alias,
                },
                VisitEvent::Alias {
                    kind: b_kind,
                    idx: b_idx,
                    alias: b_alias,
                },
            ) => {
                assert_eq!(a_kind, b_kind);
                assert_eq!(a_idx, b_idx);
                assert_eq!(a_alias, b_alias);
            }
            (
                VisitEvent::Import {
                    kind: a_kind,
                    idx: a_idx,
                    imp: a_imp,
                },
                VisitEvent::Import {
                    kind: b_kind,
                    idx: b_idx,
                    imp: b_imp,
                },
            ) => {
                assert_eq!(a_kind, b_kind);
                assert_eq!(a_idx, b_idx);
                assert_eq!(a_imp, b_imp);
            }
            (
                VisitEvent::Export {
                    kind: a_kind,
                    idx: a_idx,
                    exp: a_exp,
                },
                VisitEvent::Export {
                    kind: b_kind,
                    idx: b_idx,
                    exp: b_exp,
                },
            ) => {
                assert_eq!(a_kind, b_kind);
                assert_eq!(a_idx, b_idx);
                assert_eq!(a_exp, b_exp);
            }
            (
                VisitEvent::EnterCoreRecGroup {
                    ty: a_ty,
                    count: a_count,
                },
                VisitEvent::EnterCoreRecGroup {
                    ty: b_ty,
                    count: b_count,
                },
            ) => {
                assert_eq!(a_ty, b_ty);
                assert_eq!(a_count, b_count);
            }
            (
                VisitEvent::CoreSubtype {
                    parent_idx: a_pidx,
                    subvec_idx: a_sidx,
                    subtype: a_ty,
                },
                VisitEvent::CoreSubtype {
                    parent_idx: b_pidx,
                    subvec_idx: b_sidx,
                    subtype: b_ty,
                },
            ) => {
                assert_eq!(a_pidx, b_pidx);
                assert_eq!(a_sidx, b_sidx);
                assert_eq!(a_ty, b_ty);
            }
            (VisitEvent::ExitCoreRecGroup {}, VisitEvent::ExitCoreRecGroup {}) => {} // just variant equivalence is enough
            (
                VisitEvent::EnterCoreType {
                    idx: a_idx,
                    ty: a_ty,
                },
                VisitEvent::EnterCoreType {
                    idx: b_idx,
                    ty: b_ty,
                },
            ) => {
                assert_eq!(a_idx, b_idx);
                assert_eq!(a_ty, b_ty);
            }
            (
                VisitEvent::ModuleTypeDecl {
                    parent: a_parent,
                    idx: a_idx,
                    decl: a_decl,
                },
                VisitEvent::ModuleTypeDecl {
                    parent: b_parent,
                    idx: b_idx,
                    decl: b_decl,
                },
            ) => {
                assert_eq!(a_parent, b_parent);
                assert_eq!(a_idx, b_idx);
                assert_eq!(a_decl, b_decl);
            }
            (
                VisitEvent::ExitCoreType {
                    idx: a_idx,
                    ty: a_ty,
                },
                VisitEvent::ExitCoreType {
                    idx: b_idx,
                    ty: b_ty,
                },
            ) => {
                assert_eq!(a_idx, b_idx);
                assert_eq!(a_ty, b_ty);
            }
            (
                VisitEvent::CoreInst {
                    idx: a_idx,
                    inst: a_inst,
                },
                VisitEvent::CoreInst {
                    idx: b_idx,
                    inst: b_inst,
                },
            ) => {
                assert_eq!(a_idx, b_idx);
                assert_eq!(a_inst, b_inst);
            }
            (
                VisitEvent::CustomSection { sect: a_sect },
                VisitEvent::CustomSection { sect: b_sect },
            ) => {
                // best effort check here
                assert_eq!(a_sect.name, b_sect.name);
            }
            (VisitEvent::StartFunc { func: a_func }, VisitEvent::StartFunc { func: b_func }) => {
                assert_eq!(a_func.func_index, b_func.func_index);
                assert_eq!(a_func.arguments, b_func.arguments);
                assert_eq!(a_func.results, b_func.results);
            }
            _ => panic!("events are not the same discriminant: {a:?} != {b:?}"),
        }
    }
}

fn wasm_tools() -> Command {
    Command::new("wasm-tools")
}

fn test_event_generation(filename: &str) {
    println!("\nfilename: {:?}", filename);
    let buff = wat::parse_file(filename).expect("couldn't convert the input wat to Wasm");
    let original = wasmprinter::print_bytes(&buff).expect("couldn't convert original Wasm to wat");
    println!("original: {:?}", original);

    let comp = Component::parse(&buff, false, false).expect("Unable to parse");
    let evts_struct = get_events(&comp, get_structural_events);
    let evts_topo = get_events(&comp, get_topological_events);
    check_event_validity(&evts_struct, &evts_topo);
}

pub fn tests_from_wast(path: &Path, run_test: fn(&str)) {
    let path = path.to_str().unwrap().replace("\\", "/");
    for entry in fs::read_dir(path).unwrap() {
        let file = entry.unwrap();
        match file.path().extension() {
            None => continue,
            Some(ext) => {
                if ext.to_str() != Some("wast") {
                    continue;
                }
            }
        }
        let mut cmd = wasm_tools();
        let td = tempfile::TempDir::new().unwrap();
        cmd.arg("json-from-wast")
            .arg(file.path())
            .arg("--pretty")
            .arg("--wasm-dir")
            .arg(td.path())
            .arg("-o")
            .arg(td.path().join(format!(
                "{:?}.json",
                Path::new(&file.path())
                    .file_stem()
                    .unwrap()
                    .to_str()
                    .unwrap()
            )));
        let output = cmd.output().unwrap();
        let stdout = String::from_utf8_lossy(&output.stdout);
        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            panic!("failed to run {cmd:?}\nstdout: {stdout}\nstderr: {stderr}");
        }
        // For every file that is not invalid in the output, do round-trip
        for entry in fs::read_dir(td.path()).unwrap() {
            let file_json = entry.unwrap();
            match file_json.path().extension() {
                None => continue,
                Some(ext) => {
                    if ext.to_str() != Some("json") {
                        continue;
                    }
                }
            }
            let json: Value = serde_json::from_str(
                &fs::read_to_string(file_json.path()).expect("Unable to open file"),
            )
            .unwrap();
            if let Value::Object(map) = json {
                if let Value::Array(vals) = map.get_key_value("commands").unwrap().1 {
                    for value in vals {
                        if let Value::Object(testcase) = value {
                            // If assert is not in the string, that means it is a valid test case
                            if let Value::String(ty) = testcase.get_key_value("type").unwrap().1 {
                                if !ty.contains("assert") && testcase.contains_key("filename") {
                                    if let Value::String(test_file) =
                                        testcase.get_key_value("filename").unwrap().1
                                    {
                                        run_test(
                                            Path::new(td.path()).join(test_file).to_str().unwrap(),
                                        );
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}