Skip to content

Implement WebAssembly reference types and table imports. - #29

Merged
peterhuene merged 6 commits into
bytecodealliance:mainfrom
peterhuene:reference-types
Jul 24, 2020
Merged

Implement WebAssembly reference types and table imports.#29
peterhuene merged 6 commits into
bytecodealliance:mainfrom
peterhuene:reference-types

Conversation

@peterhuene

Copy link
Copy Markdown
Member

This PR implements support in the .NET API for WebAssembly reference types (extern ref and func ref).

It also implements support for declaring table imports in a Host.

A few bug fixes were made, including using a GCHandle for functions so that they are rooted while still alive, rather than keeping a set of functions declared in a Host.

Closes #27.

Peter Huene added 3 commits July 22, 2020 17:50
This commit implements `externref` values for .NET bindings.

Values can now be any .NET reference type.  Parameters and return values for
host functions can also be any .NET reference type and will be imported as
`externref` values.

An example was added to show passing and returning .NET strings as `externref`
values.

This commit also fixes host function callback rooting so that it is no longer
tied to the host.  Instead, the `_with_env` functions are used for creating the
function and the env is a GCHandle that is rooting the callback. A finalizer
for the env is used to free the handle.  This makes it so that the .NET
callback delegate remains rooted while the underlying Wasmtime `Func` exists.

Some refactoring was also done in `Function.cs` to streamline the code a
little.
This commit implements support for function references in the .NET API.

A `Function` can now be created with `Function.FromCallback` and passed as a
value to WebAssembly; this will pass the value as a `funcref`.

A `Function` can also be used as a parameter for a host function that accepts a
`funcref`.  The new method `Invoke` can be used to call the function reference.

This also adds a new example project showing some simple function reference
interactions.
This commit implements table imports.

It defines a new class `Table` representing a table import, which can be
defined with `Host.DefineTable`.

This also fixes support for null funcrefs.
@peterhuene
peterhuene requested a review from yurydelendik July 23, 2020 21:32

@yurydelendik yurydelendik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really good. Most of the time I tried to figure out Interop.ToValue/.DeleteValue pair safety. I recommend to add some disposable object/utility for most of the cases, e.g. SafeValue: IDisposable.

Comment thread src/Interop.cs
Comment thread src/Table.cs Outdated
@peterhuene

Copy link
Copy Markdown
Member Author

I'll see about making the value interface a little safer.

@peterhuene

Copy link
Copy Markdown
Member Author

Unfortunately the wasm_val_t type cannot implement IDisposable directly without an ugly pointer cast hack as this cannot be used in a fixed expression and that is what needs to be passed to wasm_val_delete.

I'd also rather not wrap wasm_val_t with another type as then we can't use stackalloc to allocate an array of them easily.

I did add some comments though.

@peterhuene
peterhuene merged commit f5f89bd into bytecodealliance:main Jul 24, 2020
@peterhuene
peterhuene deleted the reference-types branch July 24, 2020 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement reference types for .NET embeddings.

2 participants