Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds host function interfaces #246

Merged
merged 3 commits into from
Feb 15, 2022
Merged

Conversation

codefromthecrypt
Copy link
Contributor

@codefromthecrypt codefromthecrypt commented Feb 15, 2022

This adds two interfaces with only one implementation each, used to
decouple end-user code from internals.

  • wasm.HostFunctionCallContext - allows access to go Context and Memory
  • wasm.Memory - read/write standard types and arbitrary data

This currently ports existing code to use the interfaces. It is true
that over time we can use structs instead of interfaces internally to
save a pointer reference in WASI call sites. However, I'm choosing not
to do this right now, as it is more important to see the interfaces
work. We should defer optimizing towards structs until after we
repackage implementations under the "internal" directory.

Fixes #204

This adds two interfaces with only one implementation each, used to
decouple end-user code from internals.

* api.HostFunctionCallContext - allows access to go Context and Memory
* api.Memory - read/write standard types and arbitrary data

This currently ports existing code to use the interfaces. It is true
that over time we can use structs instead of interfaces internally to
save a pointer reference in WASI call sites. However, I'm choosing not
to do this right now, as it is more important to see the interfaces
work. We should defer optimizing towards structs until after we
repackage implementations under the "internal" directory.

Fixes #204

Signed-off-by: Adrian Cole <adrian@tetrate.io>
// There are two result parameters: these are offsets in the wasm.HostFunctionCallContext Memory Buffer to write
// corresponding sizes in uint32 little-endian encoding.
// There are two result parameters: these are offsets in the api.HostFunctionCallContext Memory to write
// corresponding sizes in uint32 little-endian encoding. If either are invalid due to memory constraints, this
Copy link
Contributor Author

Choose a reason for hiding this comment

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

fyi @r8d8, this is the sort of comment I'm adding about errno choices. you can make one similarly, and if we need to change later we can update

If either are invalid due to memory constraints, this returns ErrnoFault.

Copy link
Member

@mathetake mathetake left a comment

Choose a reason for hiding this comment

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

One thing I would add is naming -- maybe putting on top level of repo and use as wazero.HostFunctionCallContext, etc.. together with other future user APIs

examples/simple_test.go Outdated Show resolved Hide resolved

// hasLen returns true if Len is sufficient for sizeInBytes at the given offset.
func (m *MemoryInstance) hasLen(offset uint32, sizeInBytes uint32) bool {
return uint64(offset+sizeInBytes) <= uint64(m.Len()) // uint64 prevents overflow on add
Copy link
Member

Choose a reason for hiding this comment

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

nice for overflow prevention

api/host.go Outdated
@@ -0,0 +1,69 @@
// Package api only includes constants and interfaces. This ensures no package cycles in implementation.
Copy link
Member

Choose a reason for hiding this comment

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

oh I see nvm the comment ^^

Copy link
Contributor Author

Choose a reason for hiding this comment

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

incidentally I think we can temporarily put this in the "wasm" package

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed to wasm package for now as I think even with top-level entry points in the wazero package we'll still be able to keep packages for things like constants if we choose to. I'll show what I mean post-merge in #238

codefromthecrypt and others added 2 commits February 15, 2022 18:50
Co-authored-by: Takeshi Yoneda <takeshi@tetrate.io>
Signed-off-by: Adrian Cole <adrian@tetrate.io>
@codefromthecrypt codefromthecrypt changed the title Adds api package and host function interfaces Adds host function interfaces Feb 15, 2022
@codefromthecrypt codefromthecrypt merged commit 03de8c4 into main Feb 15, 2022
@codefromthecrypt codefromthecrypt deleted the hostfunction-interface branch February 15, 2022 11:01
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.

Make wasm.HostFunctionCallContext an interface
2 participants