Skip to content

Commit

Permalink
Added filesystem access example (#55)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JayJamieson authored Jan 17, 2024
1 parent acebe6a commit bd73838
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,57 @@ config := PluginConfig{
_, err := NewPlugin(ctx, manifest, config, []HostFunction{})
```

### Enable filesystem access

WASM plugins can read/write files outside the runtime. To do this we add `AllowedPaths` mapping of "HOST:PLUGIN" to the `extism.Manifest` of our plugin.

```go
package main

import (
"context"
"fmt"
"os"

extism "github.com/extism/go-sdk"
)

func main() {
manifest := extism.Manifest{
AllowedPaths: map[string]string{
// Here we specifify a host directory data to be linked
// to the /mnt directory inside the wasm runtime
"data": "/mnt",
},
Wasm: []extism.Wasm{
extism.WasmFile{
Path: "fs_plugin.wasm",
},
},
}

ctx := context.Background()
config := extism.PluginConfig{
EnableWasi: true,
}
plugin, err := extism.NewPlugin(ctx, manifest, config, []extism.HostFunction{})

if err != nil {
fmt.Printf("Failed to initialize plugin: %v\n", err)
os.Exit(1)
}

data := []byte("Hello world, this is written from within our wasm plugin.")
exit, _, err := plugin.Call("write_file", data)
if err != nil {
fmt.Println(err)
os.Exit(int(exit))
}
}
```

> *Note*: In order for filesystem APIs to work the plugin needs to be compiled with WASI target. Source code for the plugin can be found [here](https://github.com/extism/go-pdk/blob/main/example/fs/main.go) and is written in Go, but it could be written in any of our PDK languages.
## Build example plugins

Since our [example plugins](./plugins/) are also written in Go, for compiling them we use [TinyGo](https://tinygo.org/):
Expand Down

0 comments on commit bd73838

Please sign in to comment.