Skip to content

feat: multi-value support (and tuples too i suppose)#3016

Closed
JairusSW wants to merge 0 commit intoAssemblyScript:mainfrom
JairusSW:main
Closed

feat: multi-value support (and tuples too i suppose)#3016
JairusSW wants to merge 0 commit intoAssemblyScript:mainfrom
JairusSW:main

Conversation

@JairusSW
Copy link
Copy Markdown
Contributor

@JairusSW JairusSW commented Apr 22, 2026

Fixes #1633

This PR adds basic support for the Multi Value Proposal. Some of the major changes are:

  • Adds TupleTypeNode to the AST
  • Parser now parses TupleTypeNode
  • Supports --enable multi-value to cli and features
  • Instead of a single returnType: Type, we now generally accept returnTypes: Type[] across the board
  • Added an explicit resolveReturnTypes() method that essentially walks all types within the tuple and delegates it to resolveType()

I need to add some more tests and probably should wait until binaryen is updated before taking this off draft

I did notice that running wat2wasm on generated .wat will not compile because binaryen uses a tuple type that is not in the WAT spec. I'm assuming that the output of --textFile shouldn't necessarily need to be valid WAT?

I did utilize Claude to make an outline plan for implementation which I referenced. I had it trace other features and document what files need to be modified. Really helped with such a large codebase

I intentionally tried to modify as little as possible in this PR while still making it somewhat complete. If you have time while it's still in draft phase, I'd appreciate some basic feedback to make sure I'm going in the right direction here rather than correcting something later on which just requires more time for everyone.

Here's a sort of roadmap for this PR:

  • Allow functions to return correct multi-values through binaryen
  • Allow tuples to be indexed properly
    • Index with compile time constant
    • Index at runtime with bounds checking
    • Support unchecked()??
  • Allow tuples to be assigned to locals
    • Function locals
    • Global locals
  • Allow tuples to be ingested as parameters
  • Allow tuples to be destructured properly

Let me know if there's anything i missed here

  • I've read the contributing guidelines
  • I've added my name and email to the NOTICE file

@JairusSW JairusSW changed the title Multi-value support feat: multi-value support Apr 22, 2026
@JairusSW JairusSW changed the title feat: multi-value support feat: multi-value support (and tuples too i suppose) Apr 22, 2026
@MaxGraey
Copy link
Copy Markdown
Member

MaxGraey commented Apr 22, 2026

The PR is quite large and tries to do everything at once. I would break it down into several PRs with thorough test coverage.

  1. Support parsing and compiles (without multi-values) for tuples with a bunch of test cases and failure testes
function pair(a: i32, b: i32): [i32, i32];
function nested(a: i32, b: i32): [i32, [u32, string]];
function arg(pair: [a: i32, b: f64]): void;
function argReadonly(pair: readonly [a: i32, b: f64]): void;
function srgWithoutNames(pair: [i32, f64]): void; // legacy style

codegen should simply fallback to array literals (if all types the same) or to statically generated for specific arity and signature class Tuple which is mimic to Array (ArrayLike) and at least contains "length" property and has read access by index.

Actually, since multi-value is now widely available, it might be worth enabling it by default, so we wouldn't have to worry about the tuple fallback. So, at this first stage, we can hide all this under the compiler's experimental flag.

  1. Support multi-values infra for backend (Binaryen). Usually it relates to module.ts and compiler.ts. With own tests.
  2. Finally, put everything together, enable multi-value support, and start by supporting very simple cases that don’t require complex AA & def-use analysis.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Multi-value support?

2 participants