lens-vm / lens

Mozilla Public License 2.0
297 stars 6 forks source link

Pointer + Length as a single i64 return #41

Open jsimnz opened 1 year ago

jsimnz commented 1 year ago

Currently, we share memory between host + guest as pointer return values i32 (Since we're using 32-bit wasm). The "pointer" is an index/offset in the WASM linear memory that points to a buffer which contains the return data, length prefixed. pointer:i32 -> memory[pointer] -> <length:i32>:<return_data_buffer>

The downside of this method, is that we always need to serialize and deserialize the 32-bit length from whatever data we want to send/receive between guest/host.

Proposal: Function return values are i64s which contain both pointer and length together as a single return value.

Then, the return would look like someFunc() i64 where i64: <i32, i32>, composed of UPPER32 bits for pointer and LOWER32 bits for size.

This only works for 32-bit wasm (unless 64bit wasm also supported i128 - which I dont know).

Overall this would greatly simplify the host/guest memory sharing mechanic, and scope the pointer/length parsing to a single place (function return values) instead of littering size prefix serialization/deserialization all over the place, and making sure its consistent across all guest implementations.

Edit: As pointed by Fred, we would need to find an alternative method for the TypeID:i8 we also prefix.

AndrewSisley commented 1 year ago

Is worth noting that not all types have length (Nil, and the in PR EndOfStream). It seems likely that there will be more.