diff --git a/extism.go b/extism.go index def51fa..cf0383f 100644 --- a/extism.go +++ b/extism.go @@ -451,6 +451,10 @@ func (p *Plugin) Call(name string, data []byte) (uint32, []byte, error) { // Call a function by name with the given input and context, returning the output func (p *Plugin) CallWithContext(ctx context.Context, name string, data []byte) (uint32, []byte, error) { + if p.mainModule.IsClosed() { + return 0, nil, fmt.Errorf("module is closed") + } + ctx = context.WithValue(ctx, PluginCtxKey("extism"), p.extism) if p.Timeout > 0 { var cancel context.CancelFunc @@ -497,11 +501,6 @@ func (p *Plugin) CallWithContext(ctx context.Context, name string, data []byte) exitCode := exitErr.ExitCode() if exitCode == 0 { - // It's possible for the function to return 0 as an error code, even - // if the module is closed. - if p.mainModule.IsClosed() { - return 0, nil, fmt.Errorf("module is closed") - } err = nil } diff --git a/extism_test.go b/extism_test.go index 5c8f109..5d51a9a 100644 --- a/extism_test.go +++ b/extism_test.go @@ -781,6 +781,23 @@ func TestCountVowels(t *testing.T) { } } +func TestCountVowelsStdGo(t *testing.T) { + manifest := manifest("std_command.wasm") + + if plugin, ok := pluginInstance(t, manifest); ok { + defer plugin.Close(context.Background()) + + exit, output, err := plugin.Call("_start", []byte("ben")) + + result := string(output) + if assertCall(t, err, exit) { + expected := "hello ben" + + assert.Equal(t, expected, result) + } + } +} + func TestMultipleCallsOutput(t *testing.T) { manifest := manifest("count_vowels.wasm") diff --git a/plugins/std_command/build.sh b/plugins/std_command/build.sh new file mode 100644 index 0000000..4b96425 --- /dev/null +++ b/plugins/std_command/build.sh @@ -0,0 +1,2 @@ +# standard go compiler, command module +GOOS=wasip1 GOARCH=wasm go build -tags std -o ../../wasm/std_command.wasm . \ No newline at end of file diff --git a/plugins/std_command/go.mod b/plugins/std_command/go.mod new file mode 100644 index 0000000..f06a8d2 --- /dev/null +++ b/plugins/std_command/go.mod @@ -0,0 +1,5 @@ +module github.com/extism/extism-sdk-plugins-count-vowels + +go 1.23.6 + +require github.com/extism/go-pdk v1.1.1 diff --git a/plugins/std_command/go.sum b/plugins/std_command/go.sum new file mode 100644 index 0000000..e697f1c --- /dev/null +++ b/plugins/std_command/go.sum @@ -0,0 +1,2 @@ +github.com/extism/go-pdk v1.1.1 h1:c8twD4RXAgh5Q3bgQ4srBLnVJ1OJK1ZlKhD/0P/pbxA= +github.com/extism/go-pdk v1.1.1/go.mod h1:Gz+LIU/YCKnKXhgge8yo5Yu1F/lbv7KtKFkiCSzW/P4= diff --git a/plugins/std_command/main.go b/plugins/std_command/main.go new file mode 100644 index 0000000..10da1c9 --- /dev/null +++ b/plugins/std_command/main.go @@ -0,0 +1,14 @@ +//go:build std +// +build std + +package main + +import ( + "github.com/extism/go-pdk" +) + +func main() { + input := pdk.InputString() + + pdk.OutputString("hello " + input) +} diff --git a/wasm/count_vowels_std.wasm b/wasm/count_vowels_std.wasm new file mode 100644 index 0000000..147341a Binary files /dev/null and b/wasm/count_vowels_std.wasm differ diff --git a/wasm/fs_std.wasm b/wasm/fs_std.wasm new file mode 100644 index 0000000..f9f4e81 Binary files /dev/null and b/wasm/fs_std.wasm differ diff --git a/wasm/std_command.wasm b/wasm/std_command.wasm new file mode 100644 index 0000000..6a185ad Binary files /dev/null and b/wasm/std_command.wasm differ