WebAssembly/design

How webassembly handle struct?

Closed this issue · 8 comments

I try to read webassembly document and only see it mention type support only i32,i64,f32,f64

But I can't find any place mention a composite type such as struct

It it not possible natively in webassembly? How C compiler workaround for the struct larger than 64 bit? How can I pack 2 floats and 2 ints together in the stack?

#937 would have provided an easy solution for this but is currently closed and I'm not sure if anyone on the WebAssembly team is still thinking about it.

For compositions of up to 8 bytes, you can pack them in a 64-bit integer.

For larger structures, you'll need to transfer them through linear memory or a set of mutable globals.

@rossberg-chromium Thank you very much. I think if it doesn't limit the size and order of those returned values then we could workaround with fake struct by the compiled language. That should be enough

In the meantime, structs could be implemented using pointers, right?

nidin commented

Just allocate your struct in linear memory and work with pointers.

@nidin Thank you. Is that the way of C struct compiled to wasm normally did?

@rossberg any update on structure support?

@krisnye, there are no plans for "struct" support for wasm programs that use linear memory; the compiler is supposed to lower structs and struct operations to primitive operations. There are some plans for struct types in the context of the GC support proposal, not using linear memory, see eg https://github.com/WebAssembly/gc. There's highly experimental support for at least a subset of that in Firefox Nightly, see https://github.com/lars-t-hansen/moz-gc-experiments.