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。我们原型的初步性能结果令人鼓舞,并概述了实现原生实现的未来计划。