Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
skip to main content
research-article
Open access

Continuing WebAssembly with Effect Handlers

Published: 16 October 2023 Publication History

Abstract

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.

References

[1]
Heejin Ahn. 2022. Exception Handling Proposal for WebAssembly. https://webassembly.github.io/exception-handling/ Accessed 2022-10-27
[2]
Joe Armstrong, Robert Virding, Claes Wikström, and Mike Williams. 1996. Concurrent Programming in Erlang, Second Edition. Prentice Hall International, Hertfordshire, UK.
[3]
Alan Bateman and Ron Pressler. 2021. JEP 428: Structured Concurrency (Incubator). https://openjdk.org/jeps/428 Accessed 2023-04-14
[4]
Nick Benton and Andrew Kennedy. 2001. Exceptional Syntax Journal of Functional Programming. J. Funct. Program., 11, 4 (2001), 395–410.
[5]
Gavin M. Bierman, Claudio V. Russo, Geoffrey Mainland, Erik Meijer, and Mads Torgersen. 2012. Pause ’n’ Play: Formalizing Asynchronous C#. In ECOOP (Lecture Notes in Computer Science, Vol. 7313). Springer, 233–257.
[6]
Carl Bruggeman, Oscar Waddell, and R. Kent Dybvig. 1996. Representing Control in the Presence of One-Shot Continuations. In PLDI. ACM, 99–107.
[7]
Alex Crichton. 2021. Wasmtime Fiber API. https://docs.wasmtime.dev/api/wasmtime_fiber/index.html Accessed 2023-04-14
[8]
Kavon Farvardin and John H. Reppy. 2020. From folklore to fact: comparing implementations of stacks and continuations. In PLDI. ACM, 75–90.
[9]
Matthew Flatt and R. Kent Dybvig. 2020. Compiler and Runtime Support for Continuation Marks. In Proceedings of the 41st ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI 2020). Association for Computing Machinery, New York, NY, USA. 45–58. isbn:9781450376136 https://doi.org/10.1145/3385412.3385981
[10]
Dan R. Ghica, Sam Lindley, Marcos Maroñas Bravo, and Maciej Piróg. 2022. High-level effect handlers in C++. Proc. ACM Program. Lang., 6, OOPSLA2 (2022), 1639–1667.
[11]
Carl A. Gunter, Didier Rémy, and Jon G. Riecke. 1995. A Generalization of Exceptions and Control in ML-like Languages. In Proceedings of the Seventh International Conference on Functional Programming Languages and Computer Architecture (FPCA ’95). ACM, 12–23. isbn:0897917197 https://doi.org/10.1145/224164.224173
[12]
Andreas Haas, Andreas Rossberg, Derek Schuff, Ben Titzer, Michael Holman, Dan Gohman, Luke Wagner, Alon Zakai, and J.F. Bastien. 2017. Bringing the Web up to Speed with WebAssembly. SIGPLAN Notices, 52, 6 (2017), June, 185–200.
[13]
Daniel Hillerström and Sam Lindley. 2018. Shallow Effect Handlers. In APLAS (Lecture Notes in Computer Science, Vol. 11275). Springer, 415–435.
[14]
Daniel Hillerström. 2021. Foundations for Programming and Implementing Effect Handlers. Ph. D. Dissertation. School of Informatics, The University of Edinburgh, Scotland, UK.
[15]
Ohad Kammar, Sam Lindley, and Nicolas Oury. 2013. Handlers in action. In ICFP. ACM, 145–158.
[16]
Oleg Kiselyov. 2012. An argument against call/cc. https://okmij.org/ftp/continuations/against-callcc.html Accessed 2023-04-12
[17]
KJP. 2019. Benchmarking 1 million C# tasks vs Go goroutines: Is there any difference? https://karl-pickett.medium.com/benchmarking-a-toy-c-task-vs-a-go-goroutine-is-there-any-difference-248f73f7f7b7
[18]
Daan Leijen and KC Sivaramakrishnan. 2021. libmprompt. https://github.com/koka-lang/libmprompt Accessed 2023-04-14
[19]
Daan Leijen, Benjamin Zorn, and Leonardo de Moura. 2019. Mimalloc: Free List Sharding in Action. In APLAS (Lecture Notes in Computer Science, Vol. 11893). Springer, 244–265.
[20]
Bob Nystrom. 2015. What Color is Your Function? https://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/
[21]
Luna Phipps-Costin, Andreas Rossberg, Arjun Guha, Daan Leijen, Daniel Hillerström, KC Sivaramakrishnan, Matija Pretnar, and Sam Lindley. 2023. WasmFX OOPSLA23 Artifact. Sep, https://doi.org/10.5281/zenodo.8332962
[22]
Donald Pinckney, Arjun Guha, and Yuriy Brun. 2020. Wasm/k: delimited continuations for WebAssembly. In DLS. ACM, 16–28.
[23]
Gordon D. Plotkin and Matija Pretnar. 2009. Handlers of Algebraic Effects. In ESOP (Lecture Notes in Computer Science, Vol. 5502). Springer, 80–94.
[24]
Gordon D. Plotkin and Matija Pretnar. 2013. Handling Algebraic Effects. Log. Methods Comput. Sci., 9, 4 (2013).
[25]
Ron Pressler. 2018. Project Loom: Fibers and Continuations for the Java Virtual Machine. https://cr.openjdk.org/ rpressler/loom/Loom-Proposal.html Accessed 2023-04-14
[26]
Andreas Rossberg. 2019. WebAssembly (Release 1.0). https://webassembly.github.io/spec/ Accessed 2020-01-01
[27]
Andreas Rossberg. 2022. Function Reference Types Proposal for WebAssembly. https://webassembly.github.io/function-references/ Accessed 2022-10-27
[28]
Andreas Rossberg. 2023. WebAssembly (Release 2.0). https://webassembly.github.io/spec/ Accessed 2023-20-02
[29]
Dorai Sitaram. 1993. Handling Control. In PLDI. ACM, 147–155.
[30]
K. C. Sivaramakrishnan, Stephen Dolan, Leo White, Tom Kelly, Sadiq Jaffer, and Anil Madhavapeddy. 2021. Retrofitting effect handlers onto OCaml. In PLDI. ACM, 206–221.
[31]
Michael Sperber, Kent R. Dybvig, Matthew Flatt, Anton van Stratten, Robby Bruce Findler, and Jacob Matthews. 2009. Revised^6 Report on the Algorithmic Language Scheme. J. Funct. Program., 19, S1 (2009), 1–301.
[32]
Guy L. Steele Jr. 1990. Common LISP: The Language (2nd Ed.). Digital Press.
[33]
Conrad Watt, Xiaojia Rao, Jean Pichon-Pharabod, Martin Rao, and Philippa Gardner. 2021. Two Mechanisations of WebAssembly 1.0. In Formal Methods: 24th International Symposium. Springer-Verlag, Berlin, Germany. 61–79.
[34]
Ningning Xie, Youyou Cong, Kazuki Ikemori, and Daan Leijen. 2022. First-Class Names for Effect Handlers. Proc. ACM Program. Lang., 6, OOPSLA2 (2022), Article 126, oct, 30 pages. https://doi.org/10.1145/3563289
[35]
Ningning Xie and Daan Leijen. 2021. Generalized Evidence Passing for Effect Handlers (or, Efficient Compilation of Effect Handlers to C). Proc. ACM Prog. Lang. (ICFP’21), 5, ICFP (2021), Aug., 71. https://www.microsoft.com/en-us/research/publication/generalized-evidence-passing-for-effect-handlers-or-efficient-compilation-of-effect-handlers-to-c/
[36]
Jeremy Yallop. 2023. A collaborative bibliography of work related to the theory and practice of computational effects. https://github.com/yallop/effects-bibliography Accessed 2023-04-14
[37]
Alon Zakai. 2019. Pause and Resume WebAssembly with Binaryen’s Asyncify. https://kripken.github.io/blog/wasm/2019/07/16/asyncify.html Accessed 2022-10-27

Cited By

View all
  • (2024)Effect Handlers for C via CoroutinesProceedings of the ACM on Programming Languages10.1145/36897988:OOPSLA2(2462-2489)Online publication date: 8-Oct-2024
  • (2024)Lexical Effect Handlers, DirectlyProceedings of the ACM on Programming Languages10.1145/36897708:OOPSLA2(1670-1698)Online publication date: 8-Oct-2024
  • (2024)Parallel Algebraic Effect HandlersProceedings of the ACM on Programming Languages10.1145/36746518:ICFP(756-788)Online publication date: 15-Aug-2024
  • Show More Cited By

Recommendations

Comments

Information & Contributors

Information

Published In

cover image Proceedings of the ACM on Programming Languages
Proceedings of the ACM on Programming Languages  Volume 7, Issue OOPSLA2
October 2023
2250 pages
EISSN:2475-1421
DOI:10.1145/3554312
Issue’s Table of Contents
This work is licensed under a Creative Commons Attribution 4.0 International License.

Publisher

Association for Computing Machinery

New York, NY, United States

Publication History

Published: 16 October 2023
Published in PACMPL Volume 7, Issue OOPSLA2

Permissions

Request permissions for this article.

Check for updates

Badges

Author Tags

  1. WebAssembly
  2. effect handlers
  3. stack switching

Qualifiers

  • Research-article

Funding Sources

  • NSF
  • UKRI

Contributors

Other Metrics

Bibliometrics & Citations

Bibliometrics

Article Metrics

  • Downloads (Last 12 months)506
  • Downloads (Last 6 weeks)127
Reflects downloads up to 15 Oct 2024

Other Metrics

Citations

Cited By

View all
  • (2024)Effect Handlers for C via CoroutinesProceedings of the ACM on Programming Languages10.1145/36897988:OOPSLA2(2462-2489)Online publication date: 8-Oct-2024
  • (2024)Lexical Effect Handlers, DirectlyProceedings of the ACM on Programming Languages10.1145/36897708:OOPSLA2(1670-1698)Online publication date: 8-Oct-2024
  • (2024)Parallel Algebraic Effect HandlersProceedings of the ACM on Programming Languages10.1145/36746518:ICFP(756-788)Online publication date: 15-Aug-2024
  • (2024)RichWasm: Bringing Safe, Fine-Grained, Shared-Memory Interoperability Down to WebAssemblyProceedings of the ACM on Programming Languages10.1145/36564448:PLDI(1656-1679)Online publication date: 20-Jun-2024
  • (2024)Soundly Handling LinearityProceedings of the ACM on Programming Languages10.1145/36328968:POPL(1600-1628)Online publication date: 5-Jan-2024
  • (2024)WARDuino: An embedded WebAssembly virtual machineJournal of Computer Languages10.1016/j.cola.2024.10126879(101268)Online publication date: Jun-2024

View Options

View options

PDF

View or Download as a PDF file.

PDF

eReader

View online with eReader.

eReader

Get Access

Login options

Full Access

Media

Figures

Other

Tables

Share

Share

Share this Publication link

Share on social media