WebAssembly (Wasm) is a low-level portable code format offering near native performance. It is intended as a compilation target for a wide variety of source languages. However, Wasm provides no direct support for non-local control flow features such as async/await, generators/iterators, lightweight threads, first-class continuations, etc. This means that compilers for source languages with such features must ceremoniously transform whole source programs in order to target Wasm. We present WasmFX, an extension to Wasm which provides a universal target for non-local control features via effect handlers, enabling compilers to translate such features directly into Wasm. Our extension is minimal and only adds three main instructions for creating, suspending, and resuming continuations. Moreover, our primitive instructions are type-safe providing typed continuations which are well-aligned with the design principles of Wasm whose stacks are typed. We present a formal specification of WasmFX and show that the extension is sound. We have implemented WasmFX as an extension to the Wasm reference interpreter and also built a prototype WasmFX extension for Wasmtime, a production-grade Wasm engine, piggybacking on Wasmtime's existing fibers API. The preliminary performance results for our prototype are encouraging, and we outline future plans to realise a native implementation
翻译:WebAssembly(Wasm)是一种低层次的可移植代码格式,能够提供接近原生的性能。它旨在作为多种源语言的编译目标。然而,Wasm 并不直接支持非本地控制流特性,例如 async/await、生成器/迭代器、轻量级线程、一等延续等。这意味着,对于含有这些特性的源语言,编译器必须对整个源程序进行复杂的转换才能以 Wasm 为目标。我们提出了 WasmFX,这是对 Wasm 的一个扩展,通过效果处理器为非本地控制特性提供了通用目标,使编译器能够将这些特性直接翻译为 Wasm。我们的扩展极其精简,仅添加了三条主要指令,用于创建、挂起和恢复延续。此外,我们的原始指令是类型安全的,提供了与 Wasm 设计原则(其栈具有类型)高度一致的类型化延续。我们给出了 WasmFX 的形式化规范,并证明了该扩展是可靠的。我们已将 WasmFX 实现为 Wasm 参考解释器的扩展,并基于 Wasmtime(一个生产级的 Wasm 引擎)构建了 WasmFX 的原型扩展,利用了 Wasmtime 现有的纤程 API。该原型的初步性能结果令人鼓舞,我们概述了未来实现原生版本的计划。