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。针对该原型的初步性能测试结果令人鼓舞,我们后续计划实现原生版本。