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
use crate::{
    artifact::*,
    output::{OutResult, Output},
    types::*,
};
use std::io::Write;

impl Output for ArtifactLocal {
    fn output(&self, out: &mut impl Write) -> OutResult<()> {
        self.multiplicity.output(out)?;
        self.ty.output(out)
    }
}

impl Output for ArtifactData {
    fn output(&self, out: &mut impl Write) -> OutResult<()> {
        self.offset.output(out)?;
        self.init.output(out)?;
        Ok(())
    }
}

impl Output for GlobalInit {
    fn output(&self, out: &mut impl Write) -> OutResult<()> {
        match *self {
            GlobalInit::I32(x) => {
                0u8.output(out)?;
                x.output(out)
            }
            GlobalInit::I64(x) => {
                1u8.output(out)?;
                x.output(out)
            }
        }
    }
}

impl Output for ArtifactNamedImport {
    fn output(&self, out: &mut impl Write) -> OutResult<()> {
        self.mod_name.output(out)?;
        self.item_name.output(out)?;
        self.ty.output(out)?;
        Ok(())
    }
}

impl Output for ArtifactMemory {
    fn output(&self, out: &mut impl Write) -> OutResult<()> {
        self.init_size.output(out)?;
        self.max_size.output(out)?;
        self.init.output(out)
    }
}

impl<C: RunnableCode> Output for C {
    fn output(&self, out: &mut impl Write) -> OutResult<()> {
        self.type_idx().output(out)?;
        self.return_type().output(out)?;
        self.params().output(out)?;
        self.num_locals().output(out)?;
        self.locals().locals.output(out)?;
        self.code().output(out)
    }
}

impl Output for InstantiatedGlobals {
    fn output(&self, out: &mut impl Write) -> OutResult<()> { self.inits.output(out) }
}

impl<ImportFunc: Output, CompiledCode: RunnableCode> Output for Artifact<ImportFunc, CompiledCode> {
    fn output(&self, out: &mut impl Write) -> OutResult<()> {
        self.imports.output(out)?;
        self.ty.output(out)?;
        self.table.functions.output(out)?;
        self.memory.output(out)?;
        self.global.output(out)?;
        (self.export.len() as u32).output(out)?;
        for (name, idx) in self.export.iter() {
            name.output(out)?;
            idx.output(out)?;
        }
        self.code.output(out)
    }
}