Skip to content

Commit 1ff5945

Browse files
chore: cleanup
Signed-off-by: Henry <mail@henrygressmann.de>
1 parent e25773f commit 1ff5945

File tree

14 files changed

+215
-253
lines changed

14 files changed

+215
-253
lines changed

crates/tinywasm/src/func.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,11 @@ impl FuncHandle {
5656
};
5757

5858
// 6. Let f be the dummy frame
59-
let callframe = CallFrame::new(wasm_func, func_inst.owner, params, 0);
59+
let callframe = CallFrame::new_with_params(wasm_func.locals, self.addr, func_inst.owner, params, 0);
6060

6161
// 7. Push the frame f to the call stack
62-
// & 8. Push the values to the stack (Not needed since the call frame owns the values)
62+
// & 8. Push the values to the stack
6363
store.stack.clear();
64-
6564
// 9. Invoke the function instance
6665
InterpreterRuntime::exec(store, callframe)?;
6766

crates/tinywasm/src/imports.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ impl FuncContext<'_> {
6868

6969
/// Get a reference to the module instance
7070
pub fn module(&self) -> crate::ModuleInstance {
71-
self.store.get_module_instance_raw(self.module_addr)
71+
self.store.get_module_instance(self.module_addr).unwrap_or_else(|| {
72+
unreachable!("invalid module instance address in host function context: {}", self.module_addr)
73+
})
7274
}
7375

7476
/// Get a reference to an exported memory

crates/tinywasm/src/instance.rs

Lines changed: 62 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,68 @@ pub(crate) struct ModuleInstanceInner {
3535
pub(crate) exports: ArcSlice<Export>,
3636
}
3737

38-
impl ModuleInstance {
39-
// drop the module instance reference and swap it with another one
40-
#[inline]
41-
pub(crate) fn swap(&mut self, other: Self) {
42-
self.0 = other.0;
38+
impl ModuleInstanceInner {
39+
pub(crate) fn func_ty(&self, addr: FuncAddr) -> &FuncType {
40+
match self.types.get(addr as usize) {
41+
Some(ty) => ty,
42+
None => unreachable!("invalid function address: {addr}"),
43+
}
4344
}
4445

45-
#[inline]
46-
pub(crate) fn swap_with(&mut self, other_addr: ModuleInstanceAddr, store: &mut Store) {
47-
if other_addr != self.id() {
48-
self.swap(store.get_module_instance_raw(other_addr))
46+
pub(crate) fn func_addrs(&self) -> &[FuncAddr] {
47+
&self.func_addrs
48+
}
49+
50+
// resolve a function address to the global store address
51+
pub(crate) fn resolve_func_addr(&self, addr: FuncAddr) -> FuncAddr {
52+
match self.func_addrs.get(addr as usize) {
53+
Some(addr) => *addr,
54+
None => unreachable!("invalid function address: {addr}"),
55+
}
56+
}
57+
58+
// resolve a table address to the global store address
59+
pub(crate) fn resolve_table_addr(&self, addr: TableAddr) -> TableAddr {
60+
match self.table_addrs.get(addr as usize) {
61+
Some(addr) => *addr,
62+
None => unreachable!("invalid table address: {addr}"),
63+
}
64+
}
65+
66+
// resolve a memory address to the global store address
67+
pub(crate) fn resolve_mem_addr(&self, addr: MemAddr) -> MemAddr {
68+
match self.mem_addrs.get(addr as usize) {
69+
Some(addr) => *addr,
70+
None => unreachable!("invalid memory address: {addr}"),
4971
}
5072
}
5173

74+
// resolve a data address to the global store address
75+
pub(crate) fn resolve_data_addr(&self, addr: DataAddr) -> DataAddr {
76+
match self.data_addrs.get(addr as usize) {
77+
Some(addr) => *addr,
78+
None => unreachable!("invalid data address: {addr}"),
79+
}
80+
}
81+
82+
// resolve a memory address to the global store address
83+
pub(crate) fn resolve_elem_addr(&self, addr: ElemAddr) -> ElemAddr {
84+
match self.elem_addrs.get(addr as usize) {
85+
Some(addr) => *addr,
86+
None => unreachable!("invalid element address: {addr}"),
87+
}
88+
}
89+
90+
// resolve a global address to the global store address
91+
pub(crate) fn resolve_global_addr(&self, addr: GlobalAddr) -> GlobalAddr {
92+
match self.global_addrs.get(addr as usize) {
93+
Some(addr) => *addr,
94+
None => unreachable!("invalid global address: {addr}"),
95+
}
96+
}
97+
}
98+
99+
impl ModuleInstance {
52100
/// Get the module instance's address
53101
#[inline]
54102
pub fn id(&self) -> ModuleInstanceAddr {
@@ -91,12 +139,12 @@ impl ModuleInstance {
91139
exports: module.0.exports.clone(),
92140
};
93141

94-
let instance = Self::new(instance);
142+
let instance = Rc::new(instance);
95143
store.add_instance(instance.clone());
96144

97145
match (elem_trapped, data_trapped) {
98146
(Some(trap), _) | (_, Some(trap)) => Err(trap.into()),
99-
_ => Ok(instance),
147+
_ => Ok(ModuleInstance(instance)),
100148
}
101149
}
102150

@@ -113,57 +161,6 @@ impl ModuleInstance {
113161
Some(ExternVal::new(exports.kind, *addr))
114162
}
115163

116-
#[inline]
117-
pub(crate) fn new(inner: ModuleInstanceInner) -> Self {
118-
Self(Rc::new(inner))
119-
}
120-
121-
#[inline]
122-
pub(crate) fn func_ty(&self, addr: FuncAddr) -> &FuncType {
123-
&self.0.types[addr as usize]
124-
}
125-
126-
#[inline]
127-
pub(crate) fn func_addrs(&self) -> &[FuncAddr] {
128-
&self.0.func_addrs
129-
}
130-
131-
// resolve a function address to the global store address
132-
#[inline]
133-
pub(crate) fn resolve_func_addr(&self, addr: FuncAddr) -> FuncAddr {
134-
self.0.func_addrs[addr as usize]
135-
}
136-
137-
// resolve a table address to the global store address
138-
#[inline]
139-
pub(crate) fn resolve_table_addr(&self, addr: TableAddr) -> TableAddr {
140-
self.0.table_addrs[addr as usize]
141-
}
142-
143-
// resolve a memory address to the global store address
144-
#[inline]
145-
pub(crate) fn resolve_mem_addr(&self, addr: MemAddr) -> MemAddr {
146-
self.0.mem_addrs[addr as usize]
147-
}
148-
149-
// resolve a data address to the global store address
150-
#[inline]
151-
pub(crate) fn resolve_data_addr(&self, addr: DataAddr) -> DataAddr {
152-
self.0.data_addrs[addr as usize]
153-
}
154-
155-
// resolve a memory address to the global store address
156-
#[inline]
157-
pub(crate) fn resolve_elem_addr(&self, addr: ElemAddr) -> ElemAddr {
158-
self.0.elem_addrs[addr as usize]
159-
}
160-
161-
// resolve a global address to the global store address
162-
#[inline]
163-
pub(crate) fn resolve_global_addr(&self, addr: GlobalAddr) -> GlobalAddr {
164-
self.0.global_addrs[addr as usize]
165-
}
166-
167164
/// Get an exported function by name
168165
pub fn exported_func_untyped(&self, store: &Store, name: &str) -> Result<FuncHandle> {
169166
if self.0.store_id != store.id() {
@@ -211,13 +208,13 @@ impl ModuleInstance {
211208

212209
/// Get a memory by address
213210
pub fn memory<'a>(&self, store: &'a Store, addr: MemAddr) -> Result<MemoryRef<'a>> {
214-
let mem = store.state.get_mem(self.resolve_mem_addr(addr));
211+
let mem = store.state.get_mem(self.0.resolve_mem_addr(addr));
215212
Ok(MemoryRef(mem))
216213
}
217214

218215
/// Get a memory by address (mutable)
219216
pub fn memory_mut<'a>(&self, store: &'a mut Store, addr: MemAddr) -> Result<MemoryRefMut<'a>> {
220-
let mem = store.state.get_mem_mut(self.resolve_mem_addr(addr));
217+
let mem = store.state.get_mem_mut(self.0.resolve_mem_addr(addr));
221218
Ok(MemoryRefMut(mem))
222219
}
223220

@@ -244,7 +241,7 @@ impl ModuleInstance {
244241
}
245242
};
246243

247-
let func_addr = self.resolve_func_addr(func_index);
244+
let func_addr = self.0.resolve_func_addr(func_index);
248245
let func_inst = store.state.get_func(func_addr);
249246
let ty = func_inst.func.ty();
250247

0 commit comments

Comments
 (0)