extism / go-sdk

Extism Go SDK - easily run WebAssembly modules in your Go applications
https://pkg.go.dev/github.com/extism/go-sdk
BSD 3-Clause "New" or "Revised" License
85 stars 10 forks source link

Added filesystem access example #55

Closed JayJamieson closed 10 months ago

JayJamieson commented 10 months ago

Adds a filesystem access example to README.md.

I'm a little unsure what __wasm_call_ctors() and _initialize() do and may need a better worded explanation as part of the example. My assumption is that it initializes some needed runtime APIs needed for filesystem access.

Let me know of any adjustments needed.

mhmd-azeez commented 10 months ago

Thanks @JayJamieson for the PR

I'm a little unsure what __wasm_call_ctors() and _initialize()

__wasm_call_ctors is a function generated by TinyGo where it initializes the Go runtime inside it. It's a standard convention for Command modules. Extism will call __wasm_call_ctors before calling _start.

However, when you build Reactor modules, we typically have an _initialize function that gets called only once during plugin initialization (not before every call). So what we are doing here is basically importing __wasm_call_ctors[1] and then calling it in _initialize to signal to Extism that this is a Reactor module. This is basically a workaround because TinyGo doesn't explicitly support Reactor modules. For more information about Command vs Reactor modules, please read this blog post.

If we put all of the code inside main, then it becomes a Command module and we shouldn't need to do any of that. Maybe this would be a better choice for the README, but maybe @bhelx and @nilslice can also chime in

1: The fact that we are using the export directive is a bit confusing, but if you don't provide an implementation for the function, then TinyGo interprets it as an import