feat(host_fn): infer types from annotations#11
Merged
chrisdickinson merged 2 commits intomainfrom Oct 16, 2023
Merged
Conversation
Infer host function Wasm types from type annotations if given. When inferred, parameters and return values are automatically marshaled and unmarshaled and "current plugin" access is hidden. Whenever a host function is declared, add it to a thread-local registry of host functions. Default `Plugin` creation will use the global list of plugins. In the weeds note: because this means we have to hang on to functions at specific addresses, we always end up using user_data now: in particular we store the offset of the target host function in the user_data to determine which implementation to call. I added this because the FFI call would fail if the target python host function wasn't available at module-level; this would turn into a stack overflow (it appeared that the runtime tried to re-invoke a host function infinitely if it couldn't find it.)
43e4068 to
ee9abf5
Compare
zshipko
approved these changes
Oct 13, 2023
Contributor
zshipko
left a comment
There was a problem hiding this comment.
This is really nice!
The one thing I'm unclear about is if it's possible to a user to write their own encoding format (like Json or Pickle)? If not, maybe we could provide an abstract class users could implement - which also could be a big enough chunk of work for a separate PR. But for now it seems fine as-is since they could always use a string/bytes argument and decode it themselves.
example.py
Outdated
| @host_fn | ||
| def hello_world(plugin, input_, output, a_string): | ||
| @host_fn(user_data=b"Hello again!") | ||
| def hello_world(inp: str, *a_string) -> str: |
Author
|
@zshipko thanks – and great idea! I've added an |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Infer host function Wasm types from type annotations if given. When inferred, parameters and return values are automatically marshaled and unmarshaled and "current plugin" access is hidden. Whenever a host function is declared, add it to a thread-local registry of host functions. Default
Plugincreation will use the global list of plugins.In the weeds note: because this means we have to hang on to functions at specific addresses, we always end up using user_data now: in particular we store the offset of the target host function in the user_data to determine which implementation to call. I added this because the FFI call would fail if the target python host function wasn't available at module-level; this would turn into a stack overflow (it appeared that the runtime tried to re-invoke a host function infinitely if it couldn't find it.)
Example of inferred decorator + global host func registry: