Skip to content

Commit

Permalink
chore: wip imports
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
  • Loading branch information
explodingcamera committed Jan 22, 2024
1 parent 0df1a2c commit 5d7f3bc
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 19 deletions.
13 changes: 11 additions & 2 deletions crates/tinywasm/src/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,17 @@ macro_rules! impl_from_wasm_value_tuple {
fn from_wasm_value_tuple(values: Vec<WasmValue>) -> Result<Self> {
#[allow(unused_variables, unused_mut)]
let mut iter = values.into_iter();

log::error!("from_wasm_value_tuple: {:?}", iter);

Ok((
$(
$T::try_from(
iter.next()
.ok_or(Error::Other("Not enough values in WasmValue vector".to_string()))?
)
.map_err(|_| Error::Other("Could not convert WasmValue to expected type".to_string()))?,
.map_err(|e| Error::Other(format!("FromWasmValueTuple: Could not convert WasmValue to expected type: {:?}", e,
)))?,
)*
))
}
Expand All @@ -188,7 +192,12 @@ macro_rules! impl_from_wasm_value_tuple_single {
#[allow(unused_variables, unused_mut)]
let mut iter = values.into_iter();
$T::try_from(iter.next().ok_or(Error::Other("Not enough values in WasmValue vector".to_string()))?)
.map_err(|_| Error::Other("Could not convert WasmValue to expected type".to_string()))
.map_err(|e| {
Error::Other(format!(
"FromWasmValueTupleSingle: Could not convert WasmValue to expected type: {:?}",
e
))
})
}
}
};
Expand Down
5 changes: 3 additions & 2 deletions crates/tinywasm/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,13 @@ impl Extern {
pub fn typed_func<P, R>(func: impl Fn(FuncContext<'_>, P) -> Result<R> + 'static + Send + Sync) -> Self
where
P: FromWasmValueTuple + ValTypesFromTuple,
R: IntoWasmValueTuple + ValTypesFromTuple,
R: IntoWasmValueTuple + ValTypesFromTuple + Debug,
{
let inner_func = move |ctx: FuncContext<'_>, args: &[WasmValue]| -> Result<Vec<WasmValue>> {
log::error!("args: {:?}", args);
let args = P::from_wasm_value_tuple(args.to_vec())?;
let result = func(ctx, args)?;
log::error!("result: {:?}", result);
Ok(result.into_wasm_value_tuple())
};

Expand Down Expand Up @@ -250,7 +252,6 @@ impl Imports {
import: &Import,
) -> Option<ResolvedExtern<ExternVal, Extern>> {
let name = ExternName::from(import);
log::error!("provided externs: {:?}", self.values.keys());
if let Some(v) = self.values.get(&name) {
return Some(ResolvedExtern::Extern(v.clone()));
}
Expand Down
2 changes: 1 addition & 1 deletion crates/tinywasm/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
///
/// See <https://webassembly.github.io/spec/core/exec/runtime.html#module-instances>
#[derive(Debug, Clone)]
pub struct ModuleInstance(Arc<ModuleInstanceInner>);
pub struct ModuleInstance(pub(crate) Arc<ModuleInstanceInner>);

#[allow(dead_code)]
#[derive(Debug)]
Expand Down
13 changes: 9 additions & 4 deletions crates/tinywasm/src/runtime/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
CallFrame, Error, FuncContext, LabelArgs, ModuleInstance, Result, Store, Trap,
};
use alloc::{string::ToString, vec::Vec};
use log::info;
use tinywasm_types::{ElementKind, Instruction};

mod macros;
Expand Down Expand Up @@ -127,25 +128,29 @@ fn exec_one(
}

Call(v) => {
debug!("start call");
log::info!("start call");
// prepare the call frame
let func_idx = module.resolve_func_addr(*v);
let func_inst = store.get_func(func_idx as usize)?;
let func = match &func_inst.func {
crate::Function::Wasm(ref f) => f,
crate::Function::Host(host_func) => {
let func = host_func.func.clone();
log::info!("Getting params: {:?}", host_func.ty.params);
let params = stack.values.pop_params(&host_func.ty.params)?;
log::error!("Calling host function, params: {:?}", params);
let res = (func)(FuncContext { store, module }, &params)?;
stack.values.extend_from_typed(&res);
return Ok(ExecResult::Ok);
}
};

let func_ty = module.func_ty(func.ty_addr);
log::info!("wasm call, ty: {:?}", func.ty_addr);
log::info!("tys: {:?}", module.0.types);
let func_ty = func_inst.func.ty(module);

debug!("params: {:?}", func_ty.params);
debug!("stack: {:?}", stack.values);
info!("params: {:?}", func_ty.params);
info!("stack: {:?}", stack.values);
let params = stack.values.pop_n_rev(func_ty.params.len())?;
let call_frame = CallFrame::new_raw(func_idx as usize, &params, func.locals.to_vec());

Expand Down
18 changes: 13 additions & 5 deletions crates/tinywasm/src/runtime/stack/value_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ impl ValueStack {

#[inline]
pub(crate) fn extend_from_typed(&mut self, values: &[WasmValue]) {
if values.is_empty() {
return;
}

self.top += values.len();
self.stack.extend(values.iter().map(|v| RawWasmValue::from(*v)));
}
Expand Down Expand Up @@ -82,12 +86,16 @@ impl ValueStack {

#[inline]
pub(crate) fn pop_params(&mut self, types: &[ValType]) -> Result<Vec<WasmValue>> {
let n = types.len();
if self.top < n {
return Err(Error::StackUnderflow);
log::info!("pop_params: types={:?}", types);
log::info!("stack={:?}", self.stack);

let mut res = Vec::with_capacity(types.len());
for ty in types.iter() {
let v = self.pop()?;
let v = v.attach_type(*ty);
res.push(v.into());
}
self.top -= n;
let res = self.stack.drain(self.top..).rev().map(|v| v.attach_type(types[n - 1])).collect();

Ok(res)
}

Expand Down
2 changes: 1 addition & 1 deletion crates/tinywasm/tests/generated/mvp.csv

Large diffs are not rendered by default.

20 changes: 16 additions & 4 deletions crates/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ impl TryFrom<WasmValue> for i32 {
fn try_from(value: WasmValue) -> Result<Self, Self::Error> {
match value {
WasmValue::I32(i) => Ok(i),
_ => Err(()),
_ => {
log::error!("i32: try_from failed: {:?}", value);
Err(())
}
}
}
}
Expand All @@ -182,7 +185,10 @@ impl TryFrom<WasmValue> for i64 {
fn try_from(value: WasmValue) -> Result<Self, Self::Error> {
match value {
WasmValue::I64(i) => Ok(i),
_ => Err(()),
_ => {
log::error!("i64: try_from failed: {:?}", value);
Err(())
}
}
}
}
Expand All @@ -193,7 +199,10 @@ impl TryFrom<WasmValue> for f32 {
fn try_from(value: WasmValue) -> Result<Self, Self::Error> {
match value {
WasmValue::F32(i) => Ok(i),
_ => Err(()),
_ => {
log::error!("f32: try_from failed: {:?}", value);
Err(())
}
}
}
}
Expand All @@ -204,7 +213,10 @@ impl TryFrom<WasmValue> for f64 {
fn try_from(value: WasmValue) -> Result<Self, Self::Error> {
match value {
WasmValue::F64(i) => Ok(i),
_ => Err(()),
_ => {
log::error!("f64: try_from failed: {:?}", value);
Err(())
}
}
}
}
Expand Down

0 comments on commit 5d7f3bc

Please sign in to comment.