Sign in Join
Wiki copytemporary

WebAssembly/Reference types: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11The 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]].
22
33# Free Pascal support
44
55Partial support for reference types was merged into the main FPC branch on Jun 26 2023.
66
77## externref
88
99There is a new type in the System unit, called *WasmExternRef*. It represents the *externref* WebAssembly type.
1010
1111## funcref
1212
1313Procedural types can now be declared as *WasmFuncRef*. For example:
1414
15-`type`\
16-`  TMyWasmFuncRef = function(a: longint; b: int64): longint; WasmFuncRef;`
15+``` pascal
16+type
17+ TMyWasmFuncRef = function(a: longint; b: int64): longint; WasmFuncRef;
18+```
1719
1820These types represent the *funcref* WebAssembly type.
1921
2022## Restrictions
2123
2224The WebAssembly reference types don't have an in-memory representation. Therefore, the following is not allowed:
2325
2426- Taking their address
2527- Taking their size, using sizeof() or bitsizeof()
2628- Using them as a field in a record, object or class
2729- Passing them as *var*, *constref* or *out* parameters
2830- Typecasting them to pointer, or integer, or any other type that is not a WebAssembly reference type
2931- Comparing them by value (they can only be compared to *nil*)
3032
3133## Allowed operations
3234
3335The WebAssembly reference types can be used in the following ways:
3436
3537- as procedure and function parameters, passed by value
3638- as function results
3739- as local variables
3840- as [[WebAssembly/Globals|WebAssembly globals]]
3941- they can be assigned the value *nil*
4042- they can be compared to *nil*, or used as a parameter to assigned()
4143- they can be copied (a := b)
4244
4345## Future features (Not yet implemented)
4446
4547The following seem to be possible to implement in the future, but is not yet implemented:
4648
4749- putting reference types inside tables, which can be modeled in Pascal as dynamic arrays, declared as [[WebAssembly/Globals|WebAssembly globals]]
4850- calling funcrefs
4951- converting a normal Pascal function to a funcref
5052- 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.
5153
5254An additional WebAssembly proposal might allow equality comparison between reference types (currently, only comparison to *nil* is allowed).
5355
5456# See also
5557
5658- [The official specification](https://github.com/WebAssembly/reference-types/blob/master/proposals/reference-types/Overview.md)
5759- [Proposal for adding WebAssembly Reference Types to Clang](https://discourse.llvm.org/t/rfc-webassembly-reference-types-in-clang/66939)
5860
5961---
6062*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).*
6163Category: WebAssembly

Back to history