Files
extism/python/example.py
zach dc3d54e260 feat: Add C API for host functions + support for C++, Python, Go, Node, OCaml (#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:
f9eb5ed839/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 #20
2023-01-10 12:04:40 -08:00

57 lines
1.4 KiB
Python

import sys
import json
import hashlib
sys.path.append(".")
from extism import Context, Function, host_fn, ValType
if len(sys.argv) > 1:
data = sys.argv[1].encode()
else:
data = b"some data from python!"
@host_fn
def hello_world(plugin, input, output, context, a_string):
mem = plugin.memory_at_offset(input[0])
print("Hello from Python!")
print(a_string)
print(input)
print(plugin.memory(mem)[:])
output[0] = input[0]
# a Context provides a scope for plugins to be managed within. creating multiple contexts
# is expected and groups plugins based on source/tenant/lifetime etc.
with Context() as context:
wasm = open("../wasm/code-functions.wasm", "rb").read()
hash = hashlib.sha256(wasm).hexdigest()
config = {"wasm": [{"data": wasm, "hash": hash}], "memory": {"max": 5}}
functions = [
Function(
"hello_world",
[ValType.I64],
[ValType.I64],
hello_world,
context,
"Hello again!",
)
]
plugin = context.plugin(config, wasi=True, functions=functions)
# Call `count_vowels`
j = json.loads(plugin.call("count_vowels", data))
print("Number of vowels:", j["count"])
# Compare against Python implementation
def count_vowels(data):
count = 0
for c in data:
if c in b"AaEeIiOoUu":
count += 1
return count
assert j["count"] == count_vowels(data)