WebAssembly / wasm-c-api

Wasm C API prototype
Apache License 2.0
551 stars 77 forks source link

Avoid union to represent wasm::val #66

Open rossberg opened 5 years ago

rossberg commented 5 years ago

There are two problems with using a union to represent different Wasm values (especially in arrays):

  1. it wastes space,
  2. binary backwards-compatibility breaks the moment we need to add a wider value type (e.g. SIMD).

The latter could be circumvented by requiring an indirection for passing types wider than i64 or pointers, though that raises additional memory management questions.

If that's not an option that the latter might be a showstopper. For example, it might prevent Apple from officially supporting the API, because they have strict backwards-compatibility policies.

To address these issues, function arguments and results should not be passed as a homogeneous array-of-union, but as a heterogeneous list/tuple/struct.

Possible alternatives considered:

binji commented 5 years ago

Perhaps we can leave enough space for a v128 type, and rely on a pointer to allocated memory for anything larger that comes along. That would waste a lot of space, so we could also leave it pointer-sized, and use allocation for SIMD types.

rossberg commented 5 years ago

@binji, good point about using an indirection, I'll edit the issue to mention it. Maybe that is indeed good enough, though it still would be nicer to have a "naturally compact" representation.