orca_wasm/ir/module/
module_exports.rs1use crate::ir::id::{ExportsID, FunctionID};
4use wasmparser::ExternalKind;
5
6#[derive(Debug, Clone)]
7pub struct Export {
9 pub name: String,
11 pub kind: ExternalKind,
13 pub index: u32,
15 pub(crate) deleted: bool,
17}
18
19impl<'a> From<wasmparser::Export<'a>> for Export {
20 fn from(export: wasmparser::Export<'a>) -> Self {
21 Export {
22 name: export.name.to_string(),
23 kind: export.kind,
24 index: export.index,
25 deleted: false,
26 }
27 }
28}
29
30#[derive(Clone, Debug, Default)]
31pub struct ModuleExports {
33 exports: Vec<Export>,
34}
35
36impl ModuleExports {
37 pub fn new(exports: Vec<Export>) -> Self {
39 ModuleExports { exports }
40 }
41
42 pub fn iter(&self) -> std::slice::Iter<'_, Export> {
43 self.exports.iter()
44 }
45
46 pub fn is_empty(&self) -> bool {
48 self.exports.is_empty()
49 }
50
51 pub fn add_export_func(&mut self, name: String, exp_id: u32) {
53 let export = Export {
54 name,
55 kind: ExternalKind::Func,
56 index: exp_id,
57 deleted: false,
58 };
59 self.exports.push(export);
60 }
61
62 pub fn get_by_name(&self, name: String) -> Option<Export> {
64 for exp in self.exports.iter() {
65 if exp.name == name {
66 return Some(exp.clone());
67 }
68 }
69 None
70 }
71
72 pub fn get_export_id_by_name(&self, name: String) -> Option<ExportsID> {
74 for (idx, exp) in self.exports.iter().enumerate() {
75 if exp.name == name {
76 return Some(ExportsID(idx as u32));
77 }
78 }
79 None
80 }
81
82 pub fn get_by_id(&self, id: ExportsID) -> Option<Export> {
84 if *id < self.exports.len() as u32 {
85 Some(self.exports[*id as usize].clone())
86 } else {
87 None
88 }
89 }
90
91 pub fn get_func_by_id(&self, id: FunctionID) -> Option<ExportsID> {
93 for (export_id, exp) in self.exports.iter().enumerate() {
94 if matches!(exp.kind, ExternalKind::Func) && exp.index == *id {
95 return Some(ExportsID(export_id as u32));
96 }
97 }
98 None
99 }
100
101 pub fn get_func_by_name(&self, name: String) -> Option<FunctionID> {
103 for exp in self.exports.iter() {
104 if matches!(exp.kind, ExternalKind::Func) && exp.name == name {
105 return Some(FunctionID(exp.index));
106 }
107 }
108 None
109 }
110
111 pub fn delete(&mut self, id: ExportsID) {
113 self.exports[*id as usize].deleted = true;
115 }
116}