Skip to content

wasm: async/await and Promise, through an async runtime in the JS host - #817

Merged
ASDAlexander77 merged 4 commits into
mainfrom
wasm-async
Nov 12, 2026
Merged

ASDAlexander77 merged 4 commits into
mainfrom
wasm-async

Conversation

@ASDAlexander77

@ASDAlexander77 ASDAlexander77 commented Nov 12, 2026 •

Copy link
Copy Markdown
Owner

async/await and Promise now run on wasm32 and wasm64. Before this, the wasm test runner skipped every test that used them, because MLIR's async runtime has no wasm build.

Builds on #816 (wasm try/catch), now merged; main is merged in.

Host (docs/how/wasm/test/host.mjs)

  • The async runtime: MLIR's mlirAsyncRuntime* API is implemented in the JS host, on the one thread a wasm program has.
    • Tokens, values and groups follow the runtime's reference counts.
    • Awaiters run as soon as a token, value or group becomes ready.
    • Execute queues a task.
    • A blocking await runs queued tasks until what it waits for is ready. If the queue empties first, it throws instead of hanging.
    • run() runs the tasks still queued when main returns.
  • Function pointers: on wasm64 the function table is a table64, so a function pointer must be turned into a BigInt index before table.get. functionAt does this, and qsort now uses it too.
  • New imports:
    • aligned_alloc, for alignments up to the heap's 16.
    • std_this_thread_sleep_for, which sleep(ms) calls. It blocks through Atomics.wait in Node, and with a busy wait on a browser's main thread, where Atomics.wait is not allowed.

Compiler

MemAllocFixPass erased the malloc/realloc/free declarations even when they were still called. The async lowering frees a coroutine's frame with free itself. On wasm32 this ended in "Referencing function in another module! call void ". Now:

  • the calls go to the matching ts_ function, which the pass then renames to free;
  • with no such function, the declaration is kept.

Tests

  • run_tests.mjs no longer skips async tests. README.md and wasm_skips.txt say so, and the README has a new "Async" section.
  • wasm suites, release and debug, wasm32 and wasm64, all passing:
Suite none / rc own
tester 652 pass (was 637) 486 pass (was 472)
defaultlib none 198, rc 199 (was 194 / 195) 198 pass (was 194)
  • Native ctest: 6976/6976.

🤖 Generated with Claude Code

ASDAlexander77 and others added 3 commits November 12, 2026 00:12
wasm32/wasm64 (not Emscripten) compile with the exception-handling feature and the wasm exception
model, set before the target machine is made (its MCAsmInfo takes it from there; without it every
invoke was lowered to a call).

WasmExceptionPass turns each Itanium landing pad into a catch-all catchpad left at once
(catchswitch, catchpad [ptr null], llvm.wasm.get.exception, catchret), so the code after the pad
runs as ordinary code, as it does on Linux; `resume` becomes llvm.wasm.throw. What the personality
would decide is decided after the catch: a pad's selector is the first clause that takes the
exception (__tslang_wasm_catches: the same type, or a base class's pointer), a pad of typed clauses
only rethrows what none takes, and a matched pointer clause binds the thrown pointer. The C++
runtime's type_infos and type_info vtables get definitions, so each has an address of its own.

The JS host imports the __cpp_exception tag (i32 or i64, read from the module by
host.instantiate), throws a WebAssembly.Exception of it from __cxa_throw, and keeps begin/end catch
and rethrow as libc++abi does. It also maps Node's ERR_FS_* codes and refuses an environment name
with '='.

run_tests.mjs no longer skips tests that catch; json.ts under none is listed (5 GB of strings).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… host

The host implements MLIR's mlirAsyncRuntime* API on the program's one thread, and
run() drains the tasks still queued after main. wasm64 function pointers go through
a BigInt table index (functionAt). New imports: aligned_alloc, std_this_thread_sleep_for.
MemAllocFixPass no longer erases a malloc/realloc/free declaration that is still called
(the coroutine frame's free): its calls go to the ts_ function, else it is kept.
run_tests.mjs no longer skips async tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Base automatically changed from wasm-eh to main November 12, 2026 00:40
…nto wasm-async

# Conflicts:
#	docs/how/wasm/test/README.md
#	docs/how/wasm/test/host.mjs
#	docs/how/wasm/test/run_tests.mjs
#	docs/how/wasm/test/wasm_skips.txt
@ASDAlexander77
ASDAlexander77 merged commit c002a49 into main Nov 12, 2026
11 of 12 checks passed
@ASDAlexander77
ASDAlexander77 deleted the wasm-async branch November 12, 2026 08:42
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.

1 participant