Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c0dd4bc

Browse files
cleanup
Signed-off-by: Henry Gressmann <[email protected]>
1 parent 49a4bf2 commit c0dd4bc

File tree

5 files changed

+40
-62
lines changed

5 files changed

+40
-62
lines changed

crates/tinywasm/src/func.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ macro_rules! impl_from_wasm_value_tuple_single {
186186
fn from_wasm_value_tuple(values: Vec<WasmValue>) -> Result<Self> {
187187
#[allow(unused_variables, unused_mut)]
188188
let mut iter = values.into_iter();
189-
Ok($T::try_from(iter.next().ok_or(Error::Other("Not enough values in WasmValue vector".to_string()))?)
190-
.map_err(|_| Error::Other("Could not convert WasmValue to expected type".to_string()))?)
189+
$T::try_from(iter.next().ok_or(Error::Other("Not enough values in WasmValue vector".to_string()))?)
190+
.map_err(|_| Error::Other("Could not convert WasmValue to expected type".to_string()))
191191
}
192192
}
193193
};

crates/tinywasm/src/imports.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(dead_code)]
2+
13
use core::fmt::Debug;
24

35
use crate::{
@@ -10,10 +12,7 @@ use alloc::{
1012
sync::Arc,
1113
vec::Vec,
1214
};
13-
use tinywasm_types::{
14-
Addr, Export, ExternVal, ExternalKind, FuncAddr, GlobalAddr, GlobalType, Import, MemAddr, MemoryType,
15-
ModuleInstanceAddr, TableAddr, TableType, TypeAddr, WasmFunction, WasmValue,
16-
};
15+
use tinywasm_types::*;
1716

1817
/// The internal representation of a function
1918
#[derive(Debug, Clone)]
@@ -39,9 +38,12 @@ impl Function {
3938
#[derive(Clone)]
4039
pub struct HostFunction {
4140
pub(crate) ty: tinywasm_types::FuncType,
42-
pub(crate) func: Arc<dyn Fn(&mut crate::Store, &[WasmValue]) -> Result<Vec<WasmValue>> + 'static + Send + Sync>,
41+
pub(crate) func: HostFuncInner,
4342
}
4443

44+
pub(crate) type HostFuncInner =
45+
Arc<dyn Fn(&mut crate::Store, &[WasmValue]) -> Result<Vec<WasmValue>> + 'static + Send + Sync>;
46+
4547
impl Debug for HostFunction {
4648
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
4749
f.debug_struct("HostFunction").field("ty", &self.ty).field("func", &"...").finish()
@@ -208,7 +210,7 @@ impl Imports {
208210

209211
pub(crate) fn take(
210212
&mut self,
211-
store: &mut crate::Store,
213+
_store: &mut crate::Store,
212214
import: &Import,
213215
) -> Option<ResolvedExtern<ExternVal, Extern>> {
214216
let name = ExternName::from(import);

crates/tinywasm/src/instance.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl ModuleInstance {
126126
}
127127

128128
pub(crate) fn func_ty(&self, addr: FuncAddr) -> &FuncType {
129-
&self.0.types.get(addr as usize).expect("No func type for func, this is a bug")
129+
self.0.types.get(addr as usize).expect("No func type for func, this is a bug")
130130
}
131131

132132
// resolve a function address to the global store address
@@ -161,7 +161,7 @@ impl ModuleInstance {
161161
};
162162

163163
let func_inst = store.get_func(func_addr as usize)?;
164-
let ty = func_inst.func.ty(&self);
164+
let ty = func_inst.func.ty(self);
165165

166166
Ok(FuncHandle { addr: func_addr, module: self.clone(), name: Some(name.to_string()), ty: ty.clone() })
167167
}
@@ -203,7 +203,7 @@ impl ModuleInstance {
203203
let func_addr = self.0.func_addrs.get(func_index as usize).expect("No func addr for start func, this is a bug");
204204

205205
let func_inst = store.get_func(*func_addr as usize)?;
206-
let ty = func_inst.func.ty(&self);
206+
let ty = func_inst.func.ty(self);
207207

208208
Ok(Some(FuncHandle { module: self.clone(), addr: *func_addr, ty, name: None }))
209209
}

crates/tinywasm/src/store.rs

Lines changed: 26 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
#![allow(dead_code)] // TODO: remove this
2-
31
use core::{
42
cell::RefCell,
53
sync::atomic::{AtomicUsize, Ordering},
64
};
75

86
use alloc::{format, rc::Rc, string::ToString, vec, vec::Vec};
9-
use tinywasm_types::{
10-
Addr, Data, DataAddr, ElemAddr, Element, ElementKind, FuncAddr, Global, GlobalType, MemAddr, MemoryArch,
11-
MemoryType, ModuleInstanceAddr, TableAddr, TableType, WasmFunction,
12-
};
7+
use tinywasm_types::*;
138

149
use crate::{
1510
runtime::{self, DefaultRuntime},
@@ -49,7 +44,7 @@ impl Store {
4944
Self::default()
5045
}
5146

52-
pub(crate) fn get_module_instance(&self, addr: ModuleInstanceAddr) -> Option<&ModuleInstance> {
47+
pub(crate) fn _get_module_instance(&self, addr: ModuleInstanceAddr) -> Option<&ModuleInstance> {
5348
self.module_instances.get(addr as usize)
5449
}
5550

@@ -118,7 +113,7 @@ impl Store {
118113
let func_count = self.data.funcs.len();
119114
let mut func_addrs = Vec::with_capacity(func_count);
120115
for (i, func) in funcs.into_iter().enumerate() {
121-
self.data.funcs.push(Rc::new(FunctionInstance { func: Function::Wasm(func), owner: idx }));
116+
self.data.funcs.push(Rc::new(FunctionInstance { func: Function::Wasm(func), _owner: idx }));
122117
func_addrs.push((i + func_count) as FuncAddr);
123118
}
124119
Ok(func_addrs)
@@ -155,9 +150,13 @@ impl Store {
155150
pub(crate) fn init_globals(&mut self, globals: Vec<Global>, idx: ModuleInstanceAddr) -> Result<Vec<Addr>> {
156151
let global_count = self.data.globals.len();
157152
let mut global_addrs = Vec::with_capacity(global_count);
158-
// then add the module globals
159153
for (i, global) in globals.iter().enumerate() {
160-
global_addrs.push(self.add_global(global.ty, self.eval_const(&global.init)?, idx)?.into());
154+
self.data.globals.push(Rc::new(RefCell::new(GlobalInstance::new(
155+
global.ty,
156+
self.eval_const(&global.init)?,
157+
idx,
158+
))));
159+
global_addrs.push((i + global_count) as Addr);
161160
}
162161

163162
Ok(global_addrs)
@@ -287,27 +286,8 @@ impl Store {
287286
Ok(self.data.mems.len() as MemAddr - 1)
288287
}
289288

290-
pub(crate) fn add_elem(&mut self, elem: Element, idx: ModuleInstanceAddr) -> Result<ElemAddr> {
291-
let init = elem
292-
.items
293-
.iter()
294-
.map(|item| {
295-
item.addr()
296-
.ok_or_else(|| Error::UnsupportedFeature(format!("const expression other than ref: {:?}", item)))
297-
})
298-
.collect::<Result<Vec<_>>>()?;
299-
300-
self.data.elems.push(ElemInstance::new(elem.kind, idx, Some(init)));
301-
Ok(self.data.elems.len() as ElemAddr - 1)
302-
}
303-
304-
pub(crate) fn add_data(&mut self, data: Data, idx: ModuleInstanceAddr) -> Result<DataAddr> {
305-
self.data.datas.push(DataInstance::new(data.data.to_vec(), idx));
306-
Ok(self.data.datas.len() as DataAddr - 1)
307-
}
308-
309289
pub(crate) fn add_func(&mut self, func: Function, idx: ModuleInstanceAddr) -> Result<FuncAddr> {
310-
self.data.funcs.push(Rc::new(FunctionInstance { func, owner: idx }));
290+
self.data.funcs.push(Rc::new(FunctionInstance { func, _owner: idx }));
311291
Ok(self.data.funcs.len() as FuncAddr - 1)
312292
}
313293

@@ -362,10 +342,6 @@ impl Store {
362342
self.data.tables.get(addr).ok_or_else(|| Error::Other(format!("table {} not found", addr)))
363343
}
364344

365-
pub(crate) fn get_elem(&self, addr: usize) -> Result<&ElemInstance> {
366-
self.data.elems.get(addr).ok_or_else(|| Error::Other(format!("element {} not found", addr)))
367-
}
368-
369345
/// Get the global at the actual index in the store
370346
pub(crate) fn get_global_val(&self, addr: usize) -> Result<RawWasmValue> {
371347
self.data
@@ -390,7 +366,7 @@ impl Store {
390366
/// See <https://webassembly.github.io/spec/core/exec/runtime.html#function-instances>
391367
pub struct FunctionInstance {
392368
pub(crate) func: Function,
393-
pub(crate) owner: ModuleInstanceAddr, // index into store.module_instances, none for host functions
369+
pub(crate) _owner: ModuleInstanceAddr, // index into store.module_instances, none for host functions
394370
}
395371

396372
// TODO: check if this actually helps
@@ -415,14 +391,14 @@ impl FunctionInstance {
415391
/// See <https://webassembly.github.io/spec/core/exec/runtime.html#table-instances>
416392
#[derive(Debug)]
417393
pub(crate) struct TableInstance {
418-
pub(crate) kind: TableType,
419394
pub(crate) elements: Vec<Addr>,
420-
pub(crate) owner: ModuleInstanceAddr, // index into store.module_instances
395+
pub(crate) _kind: TableType,
396+
pub(crate) _owner: ModuleInstanceAddr, // index into store.module_instances
421397
}
422398

423399
impl TableInstance {
424400
pub(crate) fn new(kind: TableType, owner: ModuleInstanceAddr) -> Self {
425-
Self { elements: vec![0; kind.size_initial as usize], kind, owner }
401+
Self { elements: vec![0; kind.size_initial as usize], _kind: kind, _owner: owner }
426402
}
427403

428404
pub(crate) fn get(&self, addr: usize) -> Result<Addr> {
@@ -468,7 +444,7 @@ pub(crate) struct MemoryInstance {
468444
pub(crate) kind: MemoryType,
469445
pub(crate) data: Vec<u8>,
470446
pub(crate) page_count: usize,
471-
pub(crate) owner: ModuleInstanceAddr, // index into store.module_instances
447+
pub(crate) _owner: ModuleInstanceAddr, // index into store.module_instances
472448
}
473449

474450
impl MemoryInstance {
@@ -480,7 +456,7 @@ impl MemoryInstance {
480456
kind,
481457
data: vec![0; PAGE_SIZE * kind.page_count_initial as usize],
482458
page_count: kind.page_count_initial as usize,
483-
owner,
459+
_owner: owner,
484460
}
485461
}
486462

@@ -556,14 +532,14 @@ impl MemoryInstance {
556532
/// See <https://webassembly.github.io/spec/core/exec/runtime.html#global-instances>
557533
#[derive(Debug)]
558534
pub(crate) struct GlobalInstance {
559-
pub(crate) ty: GlobalType,
560535
pub(crate) value: RawWasmValue,
561-
owner: ModuleInstanceAddr, // index into store.module_instances
536+
pub(crate) _ty: GlobalType,
537+
pub(crate) _owner: ModuleInstanceAddr, // index into store.module_instances
562538
}
563539

564540
impl GlobalInstance {
565541
pub(crate) fn new(ty: GlobalType, value: RawWasmValue, owner: ModuleInstanceAddr) -> Self {
566-
Self { ty, value, owner }
542+
Self { _ty: ty, value, _owner: owner }
567543
}
568544
}
569545

@@ -572,14 +548,14 @@ impl GlobalInstance {
572548
/// See <https://webassembly.github.io/spec/core/exec/runtime.html#element-instances>
573549
#[derive(Debug)]
574550
pub(crate) struct ElemInstance {
575-
kind: ElementKind,
576-
items: Option<Vec<u32>>, // none is the element was dropped
577-
owner: ModuleInstanceAddr, // index into store.module_instances
551+
_kind: ElementKind,
552+
_items: Option<Vec<u32>>, // none is the element was dropped
553+
_owner: ModuleInstanceAddr, // index into store.module_instances
578554
}
579555

580556
impl ElemInstance {
581557
pub(crate) fn new(kind: ElementKind, owner: ModuleInstanceAddr, items: Option<Vec<u32>>) -> Self {
582-
Self { kind, owner, items }
558+
Self { _kind: kind, _owner: owner, _items: items }
583559
}
584560
}
585561

@@ -588,12 +564,12 @@ impl ElemInstance {
588564
/// See <https://webassembly.github.io/spec/core/exec/runtime.html#data-instances>
589565
#[derive(Debug)]
590566
pub(crate) struct DataInstance {
591-
pub(crate) data: Vec<u8>,
592-
owner: ModuleInstanceAddr, // index into store.module_instances
567+
pub(crate) _data: Vec<u8>,
568+
pub(crate) _owner: ModuleInstanceAddr, // index into store.module_instances
593569
}
594570

595571
impl DataInstance {
596572
pub(crate) fn new(data: Vec<u8>, owner: ModuleInstanceAddr) -> Self {
597-
Self { data, owner }
573+
Self { _data: data, _owner: owner }
598574
}
599575
}

crates/types/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extern crate alloc;
2525
mod instructions;
2626
use core::{fmt::Debug, ops::Range};
2727

28-
use alloc::{boxed::Box, sync::Arc, vec::Vec};
28+
use alloc::boxed::Box;
2929
pub use instructions::*;
3030

3131
/// A TinyWasm WebAssembly Module

0 commit comments

Comments
 (0)