extism / python-sdk

Extism Python Host SDK - easily run WebAssembly modules / plugins from Python applications
https://extism.org
BSD 3-Clause "New" or "Revised" License
19 stars 0 forks source link

feat(host_fn): infer types from annotations #11

Closed chrisdickinson closed 1 year ago

chrisdickinson commented 1 year ago

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.)


Example of inferred decorator + global host func registry:

import typings
import extism
@extism.host_fn()
def incr_json(input: typings.Annotated[dict, extism.Json]) -> str
    return input.get("hello", "world")

with extism.Plugin(some_wasm_file) as plugin:
    plugin.call("incr_json", json.dumps({"hello", "earth"}))
chrisdickinson commented 1 year ago

@zshipko thanks – and great idea! I've added an extism.Codec class for use as a type annotation in the latest commit.