Design: How webassembly handle struct?

Created on 26 Jun 2017  ·  8Comments  ·  Source: WebAssembly/design

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?

Most helpful comment

Multiple return values are definitely still on the radar, and I intend to
write up a proposal very soon. That said, they don't give you structs
directly, you'll still have to split them up into individual values for
everything else but function calls.

All 8 comments

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.

Multiple return values are definitely still on the radar, and I intend to
write up a proposal very soon. That said, they don't give you structs
directly, you'll still have to split them up into individual values for
everything else but function calls.

@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?

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mfateev picture mfateev  ·  5Comments

aaabbbcccddd00001111 picture aaabbbcccddd00001111  ·  3Comments

Artur-A picture Artur-A  ·  3Comments

spidoche picture spidoche  ·  4Comments

frehberg picture frehberg  ·  6Comments