Pascal WebAssembly + pas2js

09 Apr 2022

WebAssembly (abbreviated Wasm) is "a binary instruction format for a stack-based virtual machine. Wasm is designed as a portable compilation target for programming languages, enabling deployment on the web for client and server applications." Currently, all major web browsers support Wasm. Standalone and embeddable Wasm runtimes also exist that allow running Wasm programs outside of the web browser.

So far, Wasm seems be have been of more interest among the AOT compile-to-native programming languages. Free Pascal, which works across a large number of processor architectures, operating systems, and embedded platforms, also support Wasm compilation targets; see this wiki page on setting it up. Additionally, Free Pascal also comes with a Pascal-to-Javascript transpiler known as pas2js.

I've put up a reworked Free Pascal Wasm demo of Conway's Game of Life. The game play was implemented by Dmitry Boyarintsev, in Pascal, and compiled to Wasm. The user interface is implemented by me, in Pascal, and transpiled to Javascript. For good measure, I've included a simple web server, also implemented in Pascal, that serves the Wasm, Javascript and other static content.

So, to build all Pascal programs in the demo from source, three Free Pascal compilation targets are used:

  • the native compiler, for the web server,
  • pas2js, for the user interface,
  • the Wasm compiler, for the game play.

Here's a screenshot of the user interface running on my desktop web browser in mobile-responsive mode:

pas2js Conway Game of Life

Tags: Pascal, WebAssembly