WebAssembly/Reference types: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: WebAssembly/Reference types
| Old | New | Line | |
|---|---|---|---|
| 1 | + | The WebAssembly reference types extension adds support for two opaque types - externref and funcref. They are managed by the host and cannot be stored in linear memory. They can be used as function parameters (they can only be passed by value), local variables, function results, and as [[WebAssembly/Globals|WebAssembly globals]]. | |
| 2 | + | ||
| 3 | + | # Free Pascal support | |
| 4 | + | ||
| 5 | + | Partial support for reference types was merged into the main FPC branch on Jun 26 2023. | |
| 6 | + | ||
| 7 | + | ## externref | |
| 8 | + | ||
| 9 | + | There is a new type in the System unit, called *WasmExternRef*. It represents the *externref* WebAssembly type. | |
| 10 | + | ||
| 11 | + | ## funcref | |
| 12 | + | ||
| 13 | + | Procedural types can now be declared as *WasmFuncRef*. For example: | |
| 14 | + | ||
| 15 | + | `type`\ | |
| 16 | + | ` TMyWasmFuncRef = function(a: longint; b: int64): longint; WasmFuncRef;` | |
| 17 | + | ||
| 18 | + | These types represent the *funcref* WebAssembly type. | |
| 19 | + | ||
| 20 | + | ## Restrictions | |
| 21 | + | ||
| 22 | + | The WebAssembly reference types don't have an in-memory representation. Therefore, the following is not allowed: | |
| 23 | + | ||
| 24 | + | - Taking their address | |
| 25 | + | - Taking their size, using sizeof() or bitsizeof() | |
| 26 | + | - Using them as a field in a record, object or class | |
| 27 | + | - Passing them as *var*, *constref* or *out* parameters | |
| 28 | + | - Typecasting them to pointer, or integer, or any other type that is not a WebAssembly reference type | |
| 29 | + | - Comparing them by value (they can only be compared to *nil*) | |
| 30 | + | ||
| 31 | + | ## Allowed operations | |
| 32 | + | ||
| 33 | + | The WebAssembly reference types can be used in the following ways: | |
| 34 | + | ||
| 35 | + | - as procedure and function parameters, passed by value | |
| 36 | + | - as function results | |
| 37 | + | - as local variables | |
| 38 | + | - as [[WebAssembly/Globals|WebAssembly globals]] | |
| 39 | + | - they can be assigned the value *nil* | |
| 40 | + | - they can be compared to *nil*, or used as a parameter to assigned() | |
| 41 | + | - they can be copied (a := b) | |
| 42 | + | ||
| 43 | + | ## Future features (Not yet implemented) | |
| 44 | + | ||
| 45 | + | The following seem to be possible to implement in the future, but is not yet implemented: | |
| 46 | + | ||
| 47 | + | - putting reference types inside tables, which can be modeled in Pascal as dynamic arrays, declared as [[WebAssembly/Globals|WebAssembly globals]] | |
| 48 | + | - calling funcrefs | |
| 49 | + | - converting a normal Pascal function to a funcref | |
| 50 | + | - with the [multivalue extension](https://github.com/WebAssembly/spec/blob/master/proposals/multi-value/Overview.md), it is possible to allow them as *out* or *var* parameters. In this case, they would need to be converted as extra function results. | |
| 51 | + | ||
| 52 | + | An additional WebAssembly proposal might allow equality comparison between reference types (currently, only comparison to *nil* is allowed). | |
| 53 | + | ||
| 54 | + | # See also | |
| 55 | + | ||
| 56 | + | - [The official specification](https://github.com/WebAssembly/reference-types/blob/master/proposals/reference-types/Overview.md) | |
| 57 | + | - [Proposal for adding WebAssembly Reference Types to Clang](https://discourse.llvm.org/t/rfc-webassembly-reference-types-in-clang/66939) | |
| 58 | + | ||
| 59 | + | --- | |
| 60 | + | *From the Free Pascal wiki, page "WebAssembly/Reference types", as it stood on 2023-06-26 after a change by Nickysn, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* | |
| 61 | + | Category: WebAssembly |
