<p>I am building a compiler targeting WebAssembly for a language that makes heavy use of first-class coroutines. WASM doesn't have native coroutine support (yet), so I have to emulate it. Given the native tools that WebAssembly <em>does</em> provide, what is the most suitable approach to implementing them?</p>
<p>These are primarily within-language coroutines, but calls out to platform functionality will occur within them occasionally. Coroutines are currently stackless but ubiquitous. I am primarily interested in browser-based execution, but ideally it would work on <a href="https://webassembly.org/roadmap/" rel="noreferrer">other WASM environments as well</a>.</p>

<implementation><compilers><coroutines><webassembly>
How do I implement coroutines on WebAssembly?
CC BY-SA 4.0
2023-05-28T22:39:31.010Z
Is this in particular about Simultaneous Co-routines aka threads, or Asymmetric Co-routines like [Lua](https://www.lua.org/pil/9.1.html)?
ATaco
CC BY-SA 4.0
2023-05-28T22:46:03.960Z
@ATaco Asymmetric.
Michael Homer
CC BY-SA 4.0
2023-05-29T07:40:58.033Z
Cooperative stackless coroutines are State Machines (Automata) in disguise, aren't they? Thus you'll need to transform them into explicit SMs in a usual way (like Async in C# or Rust or pretty much everywhere), and add a shim to switch from one machine to another on top of that. At least that's how I understand your question...
Alex Chichigin
CC BY-SA 4.0
2023-05-29T08:49:25.477Z
@AlexChichigin State machines could form the basis of an answer. Any means of implementing these in WebAssembly is in scope.
Michael Homer
CC BY-SA 4.0
2023-05-30T15:00:22.700Z
@MichaelHomer pretty much the only way you **can't** implement a state machine in Wasm is via mutual tail-recursive functions. Not until tail calls proposal stabilizes. Every other approach from any other language should work. Modulo manual implementation of OOP and alike. A [Wikipedia article](https://en.wikipedia.org/wiki/Automata-based_programming) has a number of examples.
Alex Chichigin
CC BY-SA 4.0
2023-05-30T20:36:54.520Z
@AlexChichigin You can post an answer below.
Michael Homer
CC BY-SA 4.0
2023-05-31T08:24:32.253Z
@MichaelHomer that would answer a different question: "how do I implement state machines on WebAssembly?"
Alex Chichigin
CC BY-SA 4.0
2023-05-31T09:24:34.717Z
@AlexChichigin Regardless, answers go in the answer box below, not in the comments, and definitely not in the comments of questions you don't think they answer.
Michael Homer
CC BY-SA 4.0
2023-09-22T07:40:55.823Z
It is about javascript, but I expect it will apply as well to wasm—see this paper: https://arxiv.org/abs/1802.02974
Moonchild
<h1>For Symmetrically, Not Natively, However...</h1>
<p>Web Assembly does not provide a native solution to multiple threads, meaning each solution will have to be platform specific.</p>
<p>You will need to import a function that handles messaging on the target platform for the purpose of spawning and communicating between threads, this can be handled in Javascript via <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers" rel="nofollow noreferrer">Web-Workers</a>.</p>
<p>Exact implementation will of course depend on the language details.</p>

CC BY-SA 4.0
2023-05-28T23:13:58.187Z
2023-05-29T00:00:14.237Z
2023-05-29T00:00:14.237Z
ATaco
# How do I implement coroutines on WebAssembly?
Posted by: username_0 on 2023-05-28
I am building a compiler targeting WebAssembly for a language that makes heavy use of first-class coroutines. WASM doesn't have native coroutine support (yet), so I have to emulate it. Given the native tools that WebAssembly *does* provide, what is the most suitable approach to implementing them?

These are primarily within-language coroutines, but calls out to platform functionality will occur within them occasionally. Coroutines are currently stackless but ubiquitous. I am primarily interested in browser-based execution, but ideally it would work on [other WASM environments as well](https://webassembly.org/roadmap/).
**Tags:** implementation compilers coroutines webassembly
> **Comments:**
> **username_1**: Is this in particular about Simultaneous Co-routines aka threads, or Asymmetric Co-routines like [Lua](https://www.lua.org/pil/9.1.html)?  
> **username_0**: @ATaco Asymmetric.  
> **username_2**: Cooperative stackless coroutines are State Machines (Automata) in disguise, aren't they? Thus you'll need to transform them into explicit SMs in a usual way (like Async in C# or Rust or pretty much everywhere), and add a shim to switch from one machine to another on top of that. At least that's how I understand your question...  
> **username_0**: @AlexChichigin State machines could form the basis of an answer. Any means of implementing these in WebAssembly is in scope.  
> **username_2**: @MichaelHomer pretty much the only way you **can't** implement a state machine in Wasm is via mutual tail-recursive functions. Not until tail calls proposal stabilizes. Every other approach from any other language should work. Modulo manual implementation of OOP and alike. A [Wikipedia article](https://en.wikipedia.org/wiki/Automata-based_programming) has a number of examples.  
> **username_0**: @AlexChichigin You can post an answer below.  
> **username_2**: @MichaelHomer that would answer a different question: "how do I implement state machines on WebAssembly?"  
> **username_0**: @AlexChichigin Regardless, answers go in the answer box below, not in the comments, and definitely not in the comments of questions you don't think they answer.  
> **username_3**: It is about javascript, but I expect it will apply as well to wasm—see this paper: https://arxiv.org/abs/1802.02974  
---
## 1 answers
### Answer by username_1 on 2023-05-28. Score: 3
# For Symmetrically, Not Natively, However...

Web Assembly does not provide a native solution to multiple threads, meaning each solution will have to be platform specific.

You will need to import a function that handles messaging on the target platform for the purpose of spawning and communicating between threads, this can be handled in Javascript via [Web-Workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers).

Exact implementation will of course depend on the language details.
---
