-
Notifications
You must be signed in to change notification settings - Fork 10
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
Conversation
Thanks @JayJamieson for the PR
However, when you build Reactor modules, we typically have an If we put all of the code inside 1: The fact that we are using the |
README.md
Outdated
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() {} | ||
|
||
``` |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
} | ||
``` | ||
|
||
> *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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this 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.
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.