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
use crate::{FuncId, WasmInterfaceTypes, WitIdsToIndices, WitIndicesToIds};
use anyhow::Result;
use id_arena::{Arena, Id};
use walrus::IndicesToIds;

#[derive(Debug, Default)]
pub struct Implements {
    arena: Arena<Implement>,
}

#[derive(Debug)]
pub struct Implement {
    id: ImplementId,
    pub adapter_func: FuncId,
    pub core_func: walrus::FunctionId,
}

pub type ImplementId = Id<Implement>;

impl WasmInterfaceTypes {
    pub(crate) fn parse_implements(
        &mut self,
        implements: wit_parser::Implements,
        ids: &IndicesToIds,
        wids: &mut WitIndicesToIds,
    ) -> Result<()> {
        for implement in implements {
            let implement = implement?;
            self.implements.add(
                wids.func(implement.adapter_func)?,
                ids.get_func(implement.core_func)?,
            );
        }

        Ok(())
    }

    pub(crate) fn encode_implements(
        &self,
        writer: &mut wit_writer::Writer,
        wids: &WitIdsToIndices,
        ids: &walrus::IdsToIndices,
    ) {
        let mut w = writer.implements(self.implements.arena.len() as u32);
        for implement in self.implements.iter() {
            w.add(
                ids.get_func_index(implement.core_func),
                wids.func(implement.adapter_func),
            );
        }
    }
}

impl Implements {
    /// Gets a reference to an implement given its id
    pub fn get(&self, id: ImplementId) -> &Implement {
        &self.arena[id]
    }

    /// Gets a reference to an implement given its id
    pub fn get_mut(&mut self, id: ImplementId) -> &mut Implement {
        &mut self.arena[id]
    }

    // /// Removes an implement from this module.
    // ///
    // /// It is up to you to ensure that any potential references to the deleted
    // /// implement are also removed, eg `get_global` expressions.
    // pub fn delete(&mut self, id: ImplementId) {
    //     self.arena.delete(id);
    // }

    /// Get a shared reference to this section's implements.
    pub fn iter(&self) -> impl Iterator<Item = &Implement> {
        self.arena.iter().map(|(_, f)| f)
    }

    /// Get mutable references to this section's implements.
    pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Implement> {
        self.arena.iter_mut().map(|(_, f)| f)
    }

    /// Adds a new implement to this section
    pub fn add(&mut self, adapter_func: FuncId, core_func: walrus::FunctionId) -> ImplementId {
        self.arena.alloc_with_id(|id| Implement {
            id,
            core_func,
            adapter_func,
        })
    }
}

impl Implement {
    /// Returns the identifier for this `Implement`
    pub fn id(&self) -> ImplementId {
        self.id
    }
}