Skip to content

Add multi-value support#330

Merged
tolauwae merged 10 commits intomainfrom
feat/multi-value
Apr 22, 2026
Merged

Add multi-value support#330
tolauwae merged 10 commits intomainfrom
feat/multi-value

Conversation

@abelstuker
Copy link
Copy Markdown
Contributor

Closes #297.

This extends the implementation to support WebAssembly's multi-value proposal, allowing functions and control structures to return multiple values. This change aligns with the accepted proposal https://github.com/WebAssembly/multi-value/blob/master/proposals/multi-value/Overview.md.

Functions can now return multiple values:

;; Function returning two values
(func $add-and-subtract (param i32 i32) (result i32 i32)
  (local.get 0) 
  (local.get 1)
  (i32.add)
  (local.get 0) 
  (local.get 1)
  (i32.sub)
)

;; The function call returns two values
(i32.const 10)
(i32.const 5)
(call $add-and-subtract)
;; Stack now has: 15, 5

Control structures support multi-value types:

;; Block with multi-value result
(block (result i32 i32)
  (i32.const 42)
  (i32.const 99)
)

;; If/else with multi-value result
(if (result i32 i32)
  (then
    (i32.const 100)
    (i32.const 200)
  )
  (else
    (i32.const 300)
    (i32.const 400)
  )
)

The host can now invoke functions with multiple return values:

// Invoke function and capture multiple results
StackValue outputs[8];
uint32_t out_count = 0;
bool ok = this->invoke(m, fidx, 0, nullptr, 8, outputs, &out_count);

// Access individual results
int32_t result1 = outputs[0].value.uint32;
int32_t result2 = outputs[1].value.uint32;

@abelstuker abelstuker marked this pull request as ready for review April 7, 2026 00:53
@tolauwae
Copy link
Copy Markdown
Member

@carllocos Multivalues should work now in WebAssembly.

Important caveat: support for it in other languages is still missing. Both Rust and AssemblyScript do not support it as far as I can tell. This does present a problem for having multivalue primitives.

@tolauwae tolauwae self-requested a review April 17, 2026 15:03
@tolauwae
Copy link
Copy Markdown
Member

@carllocos Multivalues should work now in WebAssembly.

Important caveat: support for it in other languages is still missing. Both Rust and AssemblyScript do not support it as far as I can tell. This does present a problem for having multivalue primitives.

I am partially wrong. Wasmtime docs still show how it works with tuple returns in Rust (https://docs.wasmtime.dev/examples-multi-value.html) but Rust itself does not output should functions anymore since a change in LLVM19 back in 2024.

Comment thread src/Interpreter/instructions.cpp
Comment thread src/WARDuino/WARDuino.cpp Outdated
Copy link
Copy Markdown
Member

@tolauwae tolauwae left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work 👍

@tolauwae tolauwae enabled auto-merge (squash) April 22, 2026 11:33
@tolauwae tolauwae merged commit 7955ec6 into main Apr 22, 2026
15 checks passed
@tolauwae tolauwae deleted the feat/multi-value branch April 22, 2026 11:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Condition value should be consumed before pushing onto the call stack 📑 Multi-value Extension

2 participants