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

feat: Add C API for host functions + support for C++, Python, Go, Node, OCaml #195

Merged
merged 50 commits into from Jan 10, 2023

Conversation

zshipko
Copy link
Contributor

@zshipko zshipko commented Jan 3, 2023

  • New types:

    • ExtismValType - Enum of WebAssembly types
    • ExtismValUnion - A union of the possible WebAssembly types
    • ExtismVal - A struct with ExtismValType and ExtismValUnion
    • ExtismFunction - The host function wrapper type
    • ExtismFunctionType - The type of the host function callback
    • ExtismCurrentPlugin - Provides access to the currently running plugin from inside a host function
  • New functions:

    • extism_function_new - Create a new ExtismFunction
    • extism_function_free - Free an ExtismFunction
    • extism_current_plugin_memory, extism_current_plugin_memory_alloc, extism_current_plugin_memory_free, extism_current_plugin_memory_length - Manage plugin memory from inside a host functions
  • Updated functions

    • extism_plugin_new and extsim_plugin_update - now accept two extra parameters for ExtismFunction* array and length of that array

Notes

  • Host functions take a user-data argument, which is owned by the resulting ExtismFunction and will be cleaned up when extism_function_free is called (if a cleanup function was passed in with the user data)
  • Host functions in every SDK require working with ExtismVal arguments directly, this is pretty low-level for what is kind of a high-level feature. We could work on adding some types to the SDKs that make working with pointers to plugin data more accessible, maybe something similar to how the Rust PDK handes input/output data.
  • In each language the host functions more-or-less share a signature: (CurrentPlugin plugin, Val inputs[], Val outputs[], userData)
    • C, C++, OCaml and Go take a single userData argument but Python and Node take a "rest" argument which allows passing any number of user-data values
  • Go requires the host function to be exported:

    extism/go/main.go

    Lines 13 to 26 in f9eb5ed

    #include <extism.h>
    EXTISM_GO_FUNCTION(hello_world);
    */
    import "C"
    //export hello_world
    func hello_world(plugin *C.ExtismCurrentPlugin, inputs *C.ExtismVal, nInputs C.ExtismSize, outputs *C.ExtismVal, nOutputs C.ExtismSize, userData unsafe.Pointer) {
    fmt.Println("Hello from Go!")
    s := *(*interface{})(userData)
    fmt.Println(s.(string))
    inputSlice := unsafe.Slice(inputs, nInputs)
    outputSlice := unsafe.Slice(outputs, nOutputs)
    outputSlice[0] = inputSlice[0]
    }
  • Zig and Ruby should be relatively simple to add host functions to next but I haven't really looked into Elixir, .NET or Java yet.
  • Also closes reminder: consider inlining -l/-L paths vs. pkg-config in go sdk #20

@zshipko
Copy link
Contributor Author

zshipko commented Jan 3, 2023

Just pushed the change to get rid of plugin_new_with_functions/plugin_update_with_functions and add the parameters to extism_plugin_new/extism_plugin_update and updated all the SDKs

@nilslice
Copy link
Member

nilslice commented Jan 3, 2023

This is awesome, @zshipko! Will be testing these out over the course of the week. For those following along, this is definitely a candidate for an upcoming v0.2.0 release, and if anyone can help test or add more of the bindings to other SDKs that would be excellent.

cc/ other SDK contributors for awareness:
@usdogu @thomasdarimont @mhmd-azeez @alistairjevans

@zshipko zshipko force-pushed the sdk-host-functions branch 2 times, most recently from b72bf8d to 16eafa3 Compare January 6, 2023 22:09
@mhmd-azeez
Copy link
Collaborator

@nilslice I will try to add bindings to the .NET sdk after the PR is merged and the new binaries are released

@nilslice
Copy link
Member

nilslice commented Jan 8, 2023

@mhmd-azeez that would be awesome. thanks!

@zshipko zshipko merged commit dc3d54e into main Jan 10, 2023
@zshipko zshipko deleted the sdk-host-functions branch January 10, 2023 20:04
Zwiterrion pushed a commit to MAIF/extism that referenced this pull request Feb 2, 2023
…e, OCaml (extism#195)

- New types:
  - `ExtismValType` - Enum of WebAssembly types
  - `ExtismValUnion` - A union of the possible WebAssembly types
  - `ExtismVal` - A struct with `ExtismValType` and `ExtismValUnion`
  - `ExtismFunction` - The host function wrapper type
  - `ExtismFunctionType` - The type of the host function callback
- `ExtismCurrentPlugin` - Provides access to the currently running
plugin from inside a host function

- New functions:
  - `extism_function_new` - Create a new `ExtismFunction`
  - `extism_function_free` - Free an `ExtismFunction`
- `extism_current_plugin_memory`, `extism_current_plugin_memory_alloc`,
`extism_current_plugin_memory_free`,
`extism_current_plugin_memory_length` - Manage plugin memory from inside
a host functions

- Updated functions
- `extism_plugin_new` and `extsim_plugin_update` - now accept two extra
parameters for `ExtismFunction*` array and length of that array

## Notes

- Host functions take a user-data argument, which is owned by the
resulting `ExtismFunction` and will be cleaned up when
`extism_function_free` is called (if a cleanup function was passed in
with the user data)
- Host functions in every SDK require working with `ExtismVal` arguments
directly, this is pretty low-level for what is kind of a high-level
feature. We could work on adding some types to the SDKs that make
working with pointers to plugin data more accessible, maybe something
similar to how the Rust PDK handes input/output data.
- In each language the host functions more-or-less share a signature:
`(CurrentPlugin plugin, Val inputs[], Val outputs[], userData)`
- C, C++, OCaml and Go take a single userData argument but Python and
Node take a "rest" argument which allows passing any number of user-data
values
- Go requires the host function to be exported:
https://github.com/extism/extism/blob/f9eb5ed8395218f08627622517e801491efb2a53/go/main.go#L13-L26
- Zig and Ruby should be relatively simple to add host functions to next
but I haven't really looked into Elixir, .NET or Java yet.
- Also closes extism#20
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.

reminder: consider inlining -l/-L paths vs. pkg-config in go sdk
3 participants