一句话:提出 WasmFX,用效应处理器为 WebAssembly 增加类型安全的续延与非局部控制流支持。
WebAssembly 作为可移植低层代码格式,缺乏对 async/await、生成器、轻量线程和一等续延等非局部控制流的直接支持,源语言编译器必须对程序做繁琐的整体变换。本文提出 WasmFX:通过效应处理器(effect handlers)为 Wasm 增加通用的非局部控制目标。该扩展仅添加创建、挂起和恢复续延的三条核心指令,且续延带类型,与 Wasm 的带类型栈设计一致。作者给出 WasmFX 的形式化规范并证明其可靠性,还在 Wasm 参考解释器中实现该扩展,并基于 Wasmtime 现有 fibers API 构建生产级引擎原型。初步性能结果令人鼓舞,并展望原生实现。
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.
@article{paperbot1844,
title = {Continuing WebAssembly with Effect Handlers},
author = {Luna Phipps-Costin and Andreas Rossberg and Arjun Guha and Daan Leijen and Daniel Hillerström and KC Sivaramakrishnan and Matija Pretnar and Sam Lindley},
journal = {Proceedings of the ACM on Programming Languages},
volume = {7},
number = {OOPSLA2},
year = {2023},
doi = {10.1145/3622814}
}