concordium_wasm/
artifact_output.rs

1use crate::{
2    artifact::*,
3    output::{OutResult, Output},
4    types::*,
5};
6use std::io::Write;
7
8impl Output for ArtifactLocal {
9    fn output(&self, out: &mut impl Write) -> OutResult<()> {
10        self.multiplicity.output(out)?;
11        self.ty.output(out)
12    }
13}
14
15impl Output for ArtifactData {
16    fn output(&self, out: &mut impl Write) -> OutResult<()> {
17        self.offset.output(out)?;
18        self.init.output(out)?;
19        Ok(())
20    }
21}
22
23impl Output for GlobalInit {
24    fn output(&self, out: &mut impl Write) -> OutResult<()> {
25        match *self {
26            GlobalInit::I32(x) => {
27                0u8.output(out)?;
28                x.output(out)
29            }
30            GlobalInit::I64(x) => {
31                1u8.output(out)?;
32                x.output(out)
33            }
34        }
35    }
36}
37
38impl Output for ArtifactNamedImport {
39    fn output(&self, out: &mut impl Write) -> OutResult<()> {
40        self.mod_name.output(out)?;
41        self.item_name.output(out)?;
42        self.ty.output(out)?;
43        Ok(())
44    }
45}
46
47impl Output for ArtifactMemory {
48    fn output(&self, out: &mut impl Write) -> OutResult<()> {
49        self.init_size.output(out)?;
50        self.max_size.output(out)?;
51        self.init.output(out)
52    }
53}
54
55impl<C: RunnableCode> Output for C {
56    fn output(&self, out: &mut impl Write) -> OutResult<()> {
57        self.type_idx().output(out)?;
58        self.return_type().output(out)?;
59        self.params().output(out)?;
60        self.num_locals().output(out)?;
61        self.locals().locals.output(out)?;
62        self.num_registers().output(out)?;
63        self.constants().output(out)?;
64        self.code().output(out)
65    }
66}
67
68impl Output for InstantiatedGlobals {
69    fn output(&self, out: &mut impl Write) -> OutResult<()> { self.inits.output(out) }
70}
71
72impl Output for ArtifactVersion {
73    fn output(&self, out: &mut impl Write) -> OutResult<()> {
74        match self {
75            // 255 is used to make sure that this type of artifact
76            // cannot be mistaken for the older, unversioned artifact
77            // whose serialization started with the number of imports
78            // The number of imports allowed on the chain was never 127 or more
79            // so using 255 ensures there'll be no confusion.
80            ArtifactVersion::V1 => out.write_all(&[255])?,
81        }
82        Ok(())
83    }
84}
85
86impl<ImportFunc: Output, CompiledCode: RunnableCode> Output for Artifact<ImportFunc, CompiledCode> {
87    fn output(&self, out: &mut impl Write) -> OutResult<()> {
88        self.version.output(out)?;
89        let imports_len = u16::try_from(self.imports.len())?;
90        imports_len.output(out)?;
91        for i in &self.imports {
92            i.output(out)?;
93        }
94        self.ty.output(out)?;
95        self.table.functions.output(out)?;
96        self.memory.output(out)?;
97        self.global.output(out)?;
98        (self.export.len() as u32).output(out)?;
99        for (name, idx) in self.export.iter() {
100            name.output(out)?;
101            idx.output(out)?;
102        }
103        self.code.output(out)
104    }
105}