String manipulation from wasm is weirdly difficult
HTML-код
- Опубликовано: 29 июн 2024
- Stream from June 3, 2024 at / sphaerophoria
00:00 Intro
02:50 Hello world text output
30:00 WAT text filter
01:00:00 Zig text filter
01:41:00 Trying different memory management schemes
Love that you upload these.
I fell asleep during stream yesterday so now I can catch up
The table at 2:18:00 with "direct" and "indirect" specifies that any struct which contains is not a singleton struct (ie contains more than one scalar field), like your String which contains two (ptr + len), is passed/returned indirectly, meaning the function fn(len: usize) -> String is converted to fn(out: *String, len: usize) -> void.
I think I was thrown by the syntax of
(type (;0;) (func (param i32 i32)))
(func (;0;) (type 0) (param i32 i32)
* It turns out that the (param i32 i32) in the function definition is redundant, it has to perfectly match the type
* It turns out that (param i32 i32) is the same as (param i32) (param i32)
So this function is (func (param $len i32) (param $out i32)) where $out is a pointer to our structure that's been allocated somewhere in wasm memory
So between those two, the function actually makes a lot of sense. The return struct is an out pointer, like the table and you are saying. Now we still have the problem of "how am i supposed to know", but that's the same when working with any calling convention.
I guess when we were trying to figure out how to test the stack pointer, this would be a good example as to how. An output parameter needs to be in wasm memory, so if we were to do
const x = allocateU8Array(10) with no optimizations, allocateU8Array would have to return the value as an out param, the out param would need an address in wasm memory, so we'd have to allocate some wasm memory to place x into. It's all coming together :)
It is actually very easy.
I briefly played with coding directly in WASM a year or so ago because I missed coding 8-bit and 32-bit assembly from back in the day. Interesting to see other people try it. I didn't do anything useful with it.
What is your main os?
he uses nixos atm