wasm3 / wasm3-rs

Rust wrapper for Wasm3, the fastest WebAssembly interpreter
MIT License
155 stars 43 forks source link

Rework the function object api #4

Closed Veykril closed 4 years ago

Veykril commented 4 years ago

The current definition of a wasm3 function is

#[derive(Debug, Copy, Clone)]
pub struct Function<'rt, ARGS, RET> {
    raw: NNM3Function,
    rt: &'rt Runtime,
    _pd: PhantomData<(ARGS, RET)>,
}

which means a function once looked up borrows the runtime for its entire lifetime. This pretty much forces the implementation to make use of interior mutability since repeatedly looking up functions isn't really something anyone would want to do. This also means there are a lot of aliasing problems in regards of accessing memory and the stack since returning slices to them would mean that one could invalidate those by simply calling a function object. One idea that I now had is to replace function objects with tokens that have to be passed to the runtime to call them. This means instead of being able to call them like func.call(130, 14.0) one would call a func with the following syntax runtime.call(func, (130, 14.0)) or func.call(&mut runtime, 130, 14.0). This would allow us to make use of rust's linear types and compile time safety to a better degree. Another solution would be to use interior mutability via RefCells resulting in panics in case the memory is borrowed while a function is being called.

All of this only really matters if the user has access to the stack and memory though. The stack should probably be hidden from a user anyways(or be inherently unsafe to use) but I am not sure about how to handle the raw memory. So this requires a bit more thinking.

A crude implementation of the token based idea exists in #5