-
Notifications
You must be signed in to change notification settings - Fork 51
Allow to define functions using "untyped callbacks" #207
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Allow to define functions using "untyped callbacks" #207
Conversation
6d4681d
to
56dabdb
Compare
It looks like the PR needs a small rebase with the other recent merges. |
…pecify a callback of any type without the need to use a specific delegate type. The untyped callback will receive arguments and can set results as a span of ValueBox. For example, this allows to define trapping functions for a module's import without having to know the function time at compile-time, similar to Wasmtime's define_unknown_imports_as_traps.
…Kind but a default value, as otherwise they all would be initialized with ValueKind.Int32.
a94956c
to
0a675de
Compare
Thanks! I rebased the PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great 👍! Just two questions and a minor formatting fix.
Co-authored-by: Peter Huene <peter@huene.dev>
…g overloads of Linker.DefineFunction, to reduce allocations for the strings.
Ok all pending PRs are merged, so should be able to fully rebase this now. Sorry for the churn! |
Many thanks @kpreisser and @martindevans for all this work. When we merge Martin's updated to remove the alloc for |
Currently, in order to define function callbacks in the
Store
orLinker
, you have to use aDelegate
type that specifies the signature of the function imported by WASM. This means that the function signature must be known at compile-time.However, if you want to define callbacks at runtime without knowing the signature at compile-time, this will be difficult or even impossible.
With this PR, we allow to define functions using "untyped callbacks", which allows defining a function of any type without the need to use a specific delegate type, by using the following
UntypedCallbackDelegate
:The untyped callback will receive arguments and can set results via a span of
ValueBox
. When defining the function, you will need to provide aIReadOnlyList<ValueKind>
for the function parameters and results.This can be thought of the callback equivalent to the
Function.Invoke()
method that allows to specify parameters asReadOnlySpan<ValueBox>
.For this to work, I added new methods to
ValueBox
that allow to "unbox" the boxed value, as this was previously not possible. Note that I used the nomenclature of .NET, meaning I named the methodsAsInt32
,AsSingle
,AsDouble
etc., but we could also useAsFloat32
,AsFloat64
etc.For example, this allows to define trapping functions for a module's imports without having to know the function signature at compile-time, similar to Wasmtime's
Linker::define_unknown_imports_as_traps
(bytecodealliance/wasmtime#4312, bytecodealliance/wasmtime#5557), like this:What do you think?
Thanks!