Difference between revisions of "WebAssembly"

From Lazarus wiki
Jump to navigationJump to search
(→‎WebAssembly: Add brief explanation of WebAssembly)
(→‎Free Pascal and WebAssembly: - more WASI packages/units now compile successfully)
(11 intermediate revisions by 3 users not shown)
Line 1: Line 1:
== WebAssembly ==
+
= WebAssembly =
  
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. For more information on WebAssembly, see the [https://webassembly.org WebAssembly] website.
+
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. See the [https://webassembly.org WebAssembly] website for more information.
  
==Assemblers==
+
= Free Pascal and WebAssembly =
  
There are different assemblers available, from Wabt and emscripten.org.  
+
FPC supports two Wasm compilation targets: WASI and embedded. See [[WebAssembly/Compiler]] on how to build and install FPC for Wasm.
The expected format is a slightly different between those two:
 
  
===wat2wasm (Wabt)===
+
[https://wasi.dev WASI] - the WebAssembly System Interface - defines an API for operating system-like features, including files and filesystems, network sockets, clocks and random numbers. These features, when implemented in web browsers as well as standalone Wasm runtimes on desktops, servers, and serverless cloud computing units, are available to Pascal programs and libraries compiled by FPC to Wasm for the WASI target.  
Example:
 
<source lang="lisp">
 
(module
 
  (func $add (param $lhs i32) (param $rhs i32) (result i32)
 
    local.get $lhs
 
    local.get $rhs
 
    i32.add
 
  )
 
  (export "add" (func $add))
 
)
 
</source>
 
According to the official site "Wabt" is using it's own format of the Wasm.
 
It's slightly different from the official documentation. The most current version of Wabt matches the specs, as well as supports the old syntax.
 
  
Online studio, that's using the older version of wabt syntax. https://webassembly.studio/
+
The FPC WASI RTL consists of these units:
 +
* fcl-base
 +
* fcl-css
 +
* fcl-db
 +
* fcl-hash
 +
* fcl-js
 +
* fcl-json
 +
* fcl-mustache
 +
* fcl-passrc
 +
* fcl-registry
 +
* fcl-sdo
 +
* fcl-sound
 +
* fcl-stl
 +
* fcl-xml
 +
* hash
 +
* libtar
 +
* regexpr
 +
* rtl
 +
* rtl-extra
 +
* rtl-objpas
 +
* rtl-unicode
 +
* symbolic
 +
* tplylib
 +
* webidl
  
For example. instead of  
+
The FPC Wasm embedded RTL, in line with FPC's support for [[TARGET_Embedded|embedded targets]], consists of these units:
local.get
+
* rtl
it's using
+
* rtl-extra
get_local
+
* tplylib
  
===wasm-as (emscripten)===
+
With respect to the embedded target, there are presently (2022) early efforts to create Wasm-related standards for cross-device/platform/architecture embedded applications.  
The assembler is recommended for the use in a compiler by the WebAssembly.org
 
<source lang="lisp">
 
(module
 
  (func $add (param $lhs i32) (param $rhs i32) (result i32)
 
    (
 
    local.get $lhs
 
    local.get $rhs
 
    i32.add
 
    )
 
  )
 
  (export "add" (func $add))
 
)
 
</source>
 
  
==Use on the Wiki==
+
Overall, FPC's Wasm support adds to FPC's already [https://www.freepascal.org/ extensive list of compilation targets], potentially allowing Pascal programs to run in even more environments than they already do.
WebAssembly is using s-expressions as its textual format (for either Wabt or Emscript) .
+
 
it's handy to use syntax highlighter for the code and use "lisp" language to set colors.
+
= Demos =
<pre>
+
 
  <source lang="lisp">
+
== Running in Web Browsers ==
    ;; web assembly goes here
+
In each demo, the driver program is transpiled from Pascal to Javascript using [[pas2js]], and the worker program/library is compiled from Pascal to Wasm using FPC WASI target.
  </source>
+
 
</pre>
+
* [https://www.freepascal.org/~michael/pas2js-demos/wasienv/terminal/ Simulated terminal input and output]
==See Also==
+
* [https://www.freepascal.org/~michael/pas2js-demos/wasienv/canvas/ Drawing on HTML canvas]
 +
* [https://github.com/PierceNg/wasm-demo/ Conway's Game of Life]
 +
 
 +
== With Standalone Runtime ==
 +
 
 +
Free Pascal's source tree contains [https://gitlab.com/freepascal.org/fpc/source/-/tree/main/packages/wasmtime/examples examples] embedding the [https://github.com/bytecodealliance/wasmtime wasmtime] standalone Wasm runtime in Pascal host programs.
 +
 
 +
=See Also=
 +
* [[WebAssembly/Compiler]]
 
* [[WebAssembly/Roadmap]]
 
* [[WebAssembly/Roadmap]]
* [[WebAssembly/Compiler]] - getting the compiler
 
 
* [[WebAssembly/JS]]
 
* [[WebAssembly/JS]]
* [[WebAssembly/Internals]]
+
* [[WebAssembly/Files]]
* [[WebAssembly/Instructions]] - Instructions of the WASM code
+
* [[WebAssembly/DOM]]
* https://webassembly.org/ - the official site
+
* [[WebAssembly/Threads]]
* https://webassembly.github.io/spec/core/text/index.html - text format (S-Expression) specs
+
 
* https://webassembly.github.io/spec/core/binary/index.html - binary format specs
+
There is an external pet project to create a pascal interpreter, not related to Free Pascal:
* https://developer.mozilla.org/en-US/docs/WebAssembly/Understanding_the_text_format
+
* [https://faizilham.github.io/making-budget-pascal-compiler Making a budget Pascal compiler to WebAssembly]
  
* https://rsms.me/wasm-intro - introduction to webassembly
 
* https://blog.scottlogic.com/2018/04/26/webassembly-by-hand.html
 
 
[[Category:WebAssembly]]
 
[[Category:WebAssembly]]

Revision as of 03:06, 22 June 2022

WebAssembly

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. See the WebAssembly website for more information.

Free Pascal and WebAssembly

FPC supports two Wasm compilation targets: WASI and embedded. See WebAssembly/Compiler on how to build and install FPC for Wasm.

WASI - the WebAssembly System Interface - defines an API for operating system-like features, including files and filesystems, network sockets, clocks and random numbers. These features, when implemented in web browsers as well as standalone Wasm runtimes on desktops, servers, and serverless cloud computing units, are available to Pascal programs and libraries compiled by FPC to Wasm for the WASI target.

The FPC WASI RTL consists of these units:

  • fcl-base
  • fcl-css
  • fcl-db
  • fcl-hash
  • fcl-js
  • fcl-json
  • fcl-mustache
  • fcl-passrc
  • fcl-registry
  • fcl-sdo
  • fcl-sound
  • fcl-stl
  • fcl-xml
  • hash
  • libtar
  • regexpr
  • rtl
  • rtl-extra
  • rtl-objpas
  • rtl-unicode
  • symbolic
  • tplylib
  • webidl

The FPC Wasm embedded RTL, in line with FPC's support for embedded targets, consists of these units:

  • rtl
  • rtl-extra
  • tplylib

With respect to the embedded target, there are presently (2022) early efforts to create Wasm-related standards for cross-device/platform/architecture embedded applications.

Overall, FPC's Wasm support adds to FPC's already extensive list of compilation targets, potentially allowing Pascal programs to run in even more environments than they already do.

Demos

Running in Web Browsers

In each demo, the driver program is transpiled from Pascal to Javascript using pas2js, and the worker program/library is compiled from Pascal to Wasm using FPC WASI target.

With Standalone Runtime

Free Pascal's source tree contains examples embedding the wasmtime standalone Wasm runtime in Pascal host programs.

See Also

There is an external pet project to create a pascal interpreter, not related to Free Pascal: