Skip to content

Commit 5d08124

Browse files
jimpoVeykril
authored andcommitted
Upgrade to v0.5.0 release
1 parent 12462db commit 5d08124

12 files changed

+382
-492
lines changed

src/error.rs

+2
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ impl Error {
117117
pub(crate) fn from_ffi_res(ptr: ffi::M3Result) -> Result<()> {
118118
if ptr.is_null() {
119119
Ok(())
120+
} else if unsafe { ptr == ffi::m3Err_functionLookupFailed } {
121+
Err(Error::FunctionNotFound)
120122
} else {
121123
Err(Error::Wasm3(Wasm3Error(ptr)))
122124
}

src/function.rs

+47-85
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1+
use alloc::vec::Vec;
12
use core::cmp::{Eq, PartialEq};
23
use core::hash::{Hash, Hasher};
34
use core::marker::PhantomData;
45
use core::ptr::{self, NonNull};
5-
use core::slice;
66
use core::str;
77

88
use crate::error::{Error, Result};
99
use crate::runtime::Runtime;
1010
use crate::utils::cstr_to_str;
11-
use crate::wasm3_priv;
12-
use crate::{WasmArgs, WasmType};
11+
use crate::{WasmArgs, WasmType, Module};
1312

1413
/// Calling Context for a host function.
1514
pub struct CallContext<'cc> {
@@ -25,24 +24,15 @@ impl<'cc> CallContext<'cc> {
2524
}
2625
}
2726

28-
unsafe fn mallocated(&self) -> *mut ffi::M3MemoryHeader {
29-
self.runtime.as_ref().memory.mallocated
30-
}
31-
3227
/// Returns the raw memory of the runtime associated with this context.
3328
///
3429
/// # Safety
3530
///
3631
/// The returned pointer may get invalidated when wasm function objects are called due to reallocations.
3732
pub unsafe fn memory(&self) -> *const [u8] {
38-
let mallocated = self.mallocated();
39-
let len = (*mallocated).length as usize;
40-
let data = if len == 0 {
41-
ptr::NonNull::dangling().as_ptr()
42-
} else {
43-
mallocated.offset(1).cast()
44-
};
45-
ptr::slice_from_raw_parts(data, len)
33+
let mut memory_size = 0u32;
34+
let data = ffi::m3_GetMemory(self.runtime.as_ptr(), &mut memory_size as *mut u32, 0);
35+
ptr::slice_from_raw_parts(data, memory_size as usize)
4636
}
4737

4838
/// Returns the raw memory of the runtime associated with this context.
@@ -51,22 +41,18 @@ impl<'cc> CallContext<'cc> {
5141
///
5242
/// The returned pointer may get invalidated when wasm function objects are called due to reallocations.
5343
pub unsafe fn memory_mut(&self) -> *mut [u8] {
54-
let mallocated = self.mallocated();
55-
let len = (*mallocated).length as usize;
56-
let data = if len == 0 {
57-
ptr::NonNull::dangling().as_ptr()
58-
} else {
59-
mallocated.offset(1).cast()
60-
};
61-
ptr::slice_from_raw_parts_mut(data, len)
44+
let mut memory_size = 0u32;
45+
let data = ffi::m3_GetMemory(self.runtime.as_ptr(), &mut memory_size as *mut u32, 0);
46+
ptr::slice_from_raw_parts_mut(data, memory_size as usize)
6247
}
6348
}
6449

6550
// redefine of ffi::RawCall without the Option<T> around it
6651
/// Type of a raw host function for wasm3.
6752
pub type RawCall = unsafe extern "C" fn(
6853
runtime: ffi::IM3Runtime,
69-
_sp: ffi::m3stack_t,
54+
ctx: ffi::IM3ImportContext,
55+
_sp: *mut u64,
7056
_mem: *mut cty::c_void,
7157
) -> *const cty::c_void;
7258

@@ -102,7 +88,17 @@ where
10288
{
10389
/// The name of this function.
10490
pub fn name(&self) -> &str {
105-
unsafe { cstr_to_str(self.raw.as_ref().name) }
91+
unsafe { cstr_to_str(ffi::m3_GetFunctionName(self.raw.as_ptr())) }
92+
}
93+
94+
/// The module containing this function.
95+
pub fn module(&self) -> Option<Module<'rt>> {
96+
let module = unsafe { ffi::m3_GetFunctionModule(self.raw.as_ptr()) };
97+
if !module.is_null() {
98+
Some(Module::from_raw(self.rt, module))
99+
} else {
100+
None
101+
}
106102
}
107103
}
108104

@@ -111,77 +107,43 @@ where
111107
Args: WasmArgs,
112108
Ret: WasmType,
113109
{
114-
pub(crate) fn validate_sig(mut func: NNM3Function) -> Result<()> {
115-
let &ffi::M3FuncType {
116-
returnType: ret,
117-
argTypes: ref args,
118-
numArgs: num,
119-
..
120-
} = unsafe { &*func.as_mut().funcType };
121-
// argTypes is actually dynamically sized.
122-
let args = unsafe { slice::from_raw_parts(args.as_ptr(), num as usize) };
123-
match Ret::TYPE_INDEX == ret && Args::validate_types(args) {
124-
true => Ok(()),
125-
false => Err(Error::InvalidFunctionSignature),
126-
}
110+
fn validate_sig(func: NNM3Function) -> bool {
111+
let num_args = unsafe { ffi::m3_GetArgCount(func.as_ptr()) };
112+
let num_rets = unsafe { ffi::m3_GetRetCount(func.as_ptr()) };
113+
let args = (0..num_args)
114+
.map(|i| unsafe { ffi::m3_GetArgType(func.as_ptr(), i) })
115+
.collect::<Vec<_>>();
116+
let rets = (0..num_rets)
117+
.map(|i| unsafe { ffi::m3_GetRetType(func.as_ptr(), i) })
118+
.collect::<Vec<_>>();
119+
return Args::validate_types(&args) &&
120+
((rets.len() == 0 && Ret::TYPE_INDEX == ffi::M3ValueType::c_m3Type_none) ||
121+
(rets.len() == 1 && Ret::TYPE_INDEX == rets[0]));
127122
}
128123

129124
#[inline]
130125
pub(crate) fn from_raw(rt: &'rt Runtime, raw: NNM3Function) -> Result<Self> {
131-
Self::validate_sig(raw)?;
132-
let this = Function {
126+
if !Self::validate_sig(raw) {
127+
return Err(Error::InvalidFunctionSignature);
128+
}
129+
Ok(Function {
133130
raw,
134131
rt,
135132
_pd: PhantomData,
136-
};
137-
// make sure the function is compiled
138-
this.compile()
139-
}
140-
141-
#[inline]
142-
pub(crate) fn compile(self) -> Result<Self> {
143-
unsafe {
144-
if self.raw.as_ref().compiled.is_null() {
145-
Error::from_ffi_res(wasm3_priv::Compile_Function(self.raw.as_ptr()))?;
146-
}
147-
};
148-
Ok(self)
133+
})
149134
}
150135

151136
fn call_impl(&self, args: Args) -> Result<Ret> {
152-
let stack = self.rt.stack_mut();
153-
let ret = unsafe {
154-
args.push_on_stack(stack);
155-
Self::call_impl_(
156-
self.raw.as_ref().compiled,
157-
stack.cast(),
158-
self.rt.mallocated(),
159-
0,
160-
0.0,
161-
)
137+
let mut argv = args.ptrs_vec();
138+
let result = unsafe {
139+
ffi::m3_Call(self.raw.as_ptr(), argv.len() as u32, argv.as_mut_ptr())
162140
};
163-
Error::from_ffi_res(ret.cast()).map(|()| unsafe { Ret::pop_from_stack(stack.cast()) })
164-
}
165-
166-
#[inline]
167-
unsafe fn call_impl_(
168-
_pc: ffi::pc_t,
169-
_sp: ffi::m3stack_t,
170-
_mem: *mut ffi::M3MemoryHeader,
171-
_r0: ffi::m3reg_t,
172-
_fp0: f64,
173-
) -> ffi::m3ret_t {
174-
let possible_trap = ffi::m3_Yield();
175-
if !possible_trap.is_null() {
176-
possible_trap.cast()
177-
} else {
178-
(*_pc.cast::<ffi::IM3Operation>()).expect("IM3Operation was null")(
179-
_pc.add(1),
180-
_sp,
181-
_mem,
182-
_r0,
183-
_fp0,
184-
)
141+
Error::from_ffi_res(result)?;
142+
unsafe {
143+
let mut ret = core::mem::MaybeUninit::<Ret>::uninit();
144+
let result = ffi::m3_GetResultsV(self.raw.as_ptr(), ret.as_mut_ptr());
145+
Error::from_ffi_res(result)?;
146+
Ok(ret.assume_init())
185147
}
186148
}
187149
}

src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,3 @@ mod ty;
2020
pub use self::ty::{WasmArg, WasmArgs, WasmType};
2121
mod utils;
2222
pub use ffi as wasm3_sys;
23-
24-
pub(crate) mod wasm3_priv;

src/macros.rs

+18-13
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,49 @@
1616
/// ```
1717
#[macro_export]
1818
macro_rules! make_func_wrapper {
19-
( $wis:vis $wrapper_name:ident: $original:ident( $( $pname:ident: $ptype:ident ),* $( , )? ) -> TrappedResult<$rtype:ident>) => {
19+
( $wis:vis $wrapper_name:ident: $original:ident( $( $pname:ident: $ptype:ty ),* $( , )? ) -> TrappedResult<$rtype:ty>) => {
2020
$wis unsafe extern "C" fn $wrapper_name(
2121
_rt: $crate::wasm3_sys::IM3Runtime,
22-
_sp: $crate::wasm3_sys::m3stack_t,
22+
_ctx: $crate::wasm3_sys::IM3ImportContext,
23+
sp: *mut u64,
2324
_mem: *mut core::ffi::c_void,
2425
) -> *const core::ffi::c_void {
2526
use $crate::WasmType as _;
26-
let ssp = _sp;
27+
let mut _argp = sp.add(<$rtype>::SIZE_IN_SLOT_COUNT);
2728
$(
28-
let $pname = $ptype::pop_from_stack(_sp);
29-
let _sp = _sp.add($ptype::SIZE_IN_SLOT_COUNT);
29+
let $pname = <$ptype as $crate::WasmType>::pop_from_stack(_argp);
30+
_argp = _argp.add(<$ptype>::SIZE_IN_SLOT_COUNT);
3031
)*
3132
let ret = $original( $( $pname ),* );
3233
match ret {
3334
Ok(ret) => {
34-
$rtype::push_on_stack(ret, ssp);
35+
<$rtype as $crate::WasmType>::push_on_stack(ret, sp);
3536
$crate::wasm3_sys::m3Err_none as _
3637
},
3738
Err(trap) => trap.as_ptr() as _
3839
}
3940
}
4041
};
4142
// ptype is an ident because we still want to match on it later -- \/ rtype too -- \/
42-
( $wis:vis $wrapper_name:ident: $original:ident( $( $pname:ident: $ptype:ident ),* $( , )? ) $( -> $rtype:ident )?) => {
43+
( $wis:vis $wrapper_name:ident: $original:ident( $( $pname:ident: $ptype:ty ),* $( , )? ) $( -> $rtype:ty )?) => {
4344
$wis unsafe extern "C" fn $wrapper_name(
4445
_rt: $crate::wasm3_sys::IM3Runtime,
45-
_sp: $crate::wasm3_sys::m3stack_t,
46+
_ctx: $crate::wasm3_sys::IM3ImportContext,
47+
sp: *mut u64,
4648
_mem: *mut core::ffi::c_void,
4749
) -> *const core::ffi::c_void {
4850
use $crate::WasmType as _;
49-
let ssp = _sp;
51+
let mut _argp = sp;
5052
$(
51-
let $pname = $ptype::pop_from_stack(_sp);
52-
let _sp = _sp.add($ptype::SIZE_IN_SLOT_COUNT);
53+
_argp = _argp.add(<$rtype>::SIZE_IN_SLOT_COUNT);
54+
)?
55+
$(
56+
let $pname = <$ptype as $crate::WasmType>::pop_from_stack(_argp);
57+
_argp = _argp.add(<$ptype>::SIZE_IN_SLOT_COUNT);
5358
)*
54-
let ret = $original( $( $pname ),* );
59+
let _ret = $original( $( $pname ),* );
5560
$(
56-
$rtype::push_on_stack(ret, ssp);
61+
<$rtype as $crate::WasmType>::push_on_stack(_ret, sp);
5762
)?
5863
$crate::wasm3_sys::m3Err_none as _
5964
}

0 commit comments

Comments
 (0)