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

Added filesystem access example #55

Merged
merged 2 commits into from
Jan 17, 2024
Merged

Conversation

JayJamieson
Copy link
Contributor

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
Copy link
Collaborator

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

README.md Outdated
Comment on lines 318 to 365
Next we need to setup the runtime to make filesystem APIs available, we do this by calling `__wasm_call_ctors()` exported method in a WASM `_initialize` hook.

```go
package main

import (
"os"

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

//export __wasm_call_ctors
func __wasm_call_ctors()

//export _initialize
func _initialize() {
__wasm_call_ctors()
}

//export write_file
func writeFile() int32 {
input := pdk.Input()

err := pluginMain("/mnt/wasm.txt", input)

if err != nil {
pdk.Log(pdk.LogTrace, err.Error())
return 1
}

return 0
}

func pluginMain(filename string, data []byte) error {
pdk.Log(pdk.LogInfo, "Writing following data to disk: "+string(data))

// Write to the file, will be created if it doesn't exist
err := os.WriteFile(filename, data, 0644)
if err != nil {
return err
}

return nil
}

func main() {}

```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a little confusing to have PDK code in this repo. This SDK can run lots of languages and documenting how to enable WASI is more the concern of the PDK readme. So maybe this could move to the go-pdk if it's not already documented there?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should maybe just have a note that the plug-in needs to be built to a WASI target or something.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm I did consider if this should be in the sdk repo. I can move this to the PDF repo easy enough.

I think a link from the PDK back to the SDK repo section covering file access will make discoverability good, what do you think?

README.md Outdated Show resolved Hide resolved
README.md Show resolved Hide resolved
}
```

> *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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor

@bhelx bhelx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Moving PDK specific discussion to that repo.

@bhelx bhelx merged commit bd73838 into extism:main Jan 17, 2024
3 checks passed
@JayJamieson JayJamieson deleted the feat/add-examples branch January 17, 2024 21:06
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.

3 participants