Scala.js to Wasm—Exploring the Wasm backend for Scala.js with Sébastien Doeraene

By Chrome for Developers

Share:

Key Concepts

  • ScalaJS: A Scala dialect compiling to JavaScript and now WebAssembly, offering a sound type system and blending object-oriented and functional programming.
  • Scala Ecosystem: Composed of the Scala Center, LAMP Laboratory, Wooga, and Vertus Lab, collaborating on language maintenance and development.
  • WebAssembly (Wasm) & WasmGC: The addition of WebAssembly Garbage Collection (WasmGC) enabled ScalaJS compilation to Wasm, facilitating interoperability with JavaScript.
  • Future Proposals: Stack switching and the “shared everything” proposal aim to unlock multi-threading and new language features in ScalaJS.
  • Interoperability: Core to ScalaJS design, with tools like a Scala-to-TypeScript compiler for seamless integration.

ScalaJS: History, Current State, and Future Directions

The episode features Sebastian Duran, the original author of ScalaJS, discussing the history, current state, and future of this Scala dialect. Scala was initially released around 2001, targeting the JVM. Around 2013, Duran initiated ScalaJS, culminating in its first non-experimental release in 2015 (version 0.6) and a stable version 1.0 in 2020. Scala Native, compiling to native code via LLVM, achieved a stable release (version 0.5) in 2024. Duran emphasizes that ScalaJS and Scala Native are distinct dialects, not simply Scala compilers.

The motivation behind adding WebAssembly as a backend was the introduction of WebAssembly Garbage Collection (WasmGC). Prior attempts were hindered by the lack of a garbage collector, making safe interaction with JavaScript difficult. WasmGC allows for safe interaction between managed Scala code and JavaScript, enabling reuse of existing JavaScript libraries. The goal isn’t to replace JavaScript, but to provide an alternative with potential performance benefits and a stronger type system – unlike TypeScript, which prioritizes compatibility and can allow runtime errors despite type checking. Approximately one-third of Scala libraries are currently available for JavaScript (according to ScalaDex).

Technical Implementation & Interoperability

ScalaJS compiles Scala classes to WebAssembly structs with a vtable for virtual dispatch, while JavaScript classes are compiled to actual JavaScript objects. Handling primitives (integers, booleans, doubles) when crossing the WebAssembly boundary requires conversions via JavaScript functions. Duran is involved in a proposal for JavaScript primitive built-ins in WebAssembly to optimize this process.

Current WebAssembly limitations include a lack of support for multi-module builds and issues with @JSExport annotations (used to expose Scala methods to JavaScript). The “custom descriptors” proposal aims to address the @JSExport issue by allowing descriptors to define JavaScript prototypes for WebAssembly structs. ScalaJS benefits from front-end frameworks like Tyrion (purely functional, Elm-inspired) and Laminer (functional reactive programming, efficient DOM manipulation). A Scala-to-TypeScript compiler facilitates integration with existing TypeScript front-end teams, generating type definitions for ScalaJS code.

Future Development & Personal Perspectives

Significant progress has been made with the successful compilation of ScholarJS, a full Scala dialect, to WebAssembly. Key proposals under consideration include text switching, leveraging stack switching capabilities, and the shared everything proposal, which could enable multi-threaded programming in ScalaJS by allowing threads to share Garbage Collected (GC) memory. Duran anticipates these proposals will unlock new language features.

Duran expressed a personal preference for brace-based syntax in Scala 2 over the indentation-based syntax introduced in Scala 3, finding the latter cognitively demanding. Despite acknowledging its popularity, he continues to format his code manually, while most Scala developers utilize ScalaFmt, a code formatter ensuring consistent styling across Scala, ScalaJS, and Scala Native.

Community & Resources

For updates, Duran recommends following Scala-lang (or ScalaLang) on social media for general Scala announcements and Scala_JS (or Scala-JS) for ScalaJS-specific updates, particularly regarding WebAssembly integration. The Scala Discord server also features a dedicated ScalaJS channel where Duran actively participates.

Conclusion

ScalaJS represents a compelling alternative within the JavaScript ecosystem, offering a sound type system and a blend of programming paradigms. The addition of WebAssembly as a backend, enabled by WasmGC, opens new possibilities for performance and interoperability. Ongoing development, including proposals for stack switching and shared memory, promises to further enhance ScalaJS’s capabilities and unlock new language features. While not aiming for market share dominance, ScalaJS caters to developers seeking a functional or immutable-first approach to front-end development.

Chat with this Video

AI-Powered

Hi! I can answer questions about this video "Scala.js to Wasm—Exploring the Wasm backend for Scala.js with Sébastien Doeraene". What would you like to know?

Chat is based on the transcript of this video and may not be 100% accurate.

Related Videos

Ready to summarize another video?

Summarize YouTube Video