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

fix: make sure we instantiate non-main modules #93

Merged
merged 11 commits into from
Jan 30, 2025
Next Next commit
add test case that fails
mhmd-azeez committed Jan 15, 2025
commit 9525604ce5552932924082d36b95ce660cc1f657
42 changes: 36 additions & 6 deletions extism_test.go
Original file line number Diff line number Diff line change
@@ -5,6 +5,13 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"strings"
"sync"
"testing"
"time"

observe "github.com/dylibso/observe-sdk/go"
"github.com/dylibso/observe-sdk/go/adapter/stdout"
"github.com/stretchr/testify/assert"
@@ -13,12 +20,6 @@ import (
"github.com/tetratelabs/wazero/experimental"
"github.com/tetratelabs/wazero/experimental/logging"
"github.com/tetratelabs/wazero/sys"
"log"
"os"
"strings"
"sync"
"testing"
"time"
)

func TestWasmUrl(t *testing.T) {
@@ -1038,6 +1039,35 @@ func TestEnableExperimentalFeature(t *testing.T) {
}
}

func TestModuleLinking(t *testing.T) {
manifest := Manifest{
Wasm: []Wasm{
WasmFile{
Path: "wasm/lib.wasm",
Name: "lib",
},
WasmFile{
Path: "wasm/main.wasm",
Name: "main",
},
},
}

if plugin, ok := pluginInstance(t, manifest); ok {
defer plugin.Close(context.Background())

exit, output, err := plugin.Call("run_test", []byte("benjamin"))

if assertCall(t, err, exit) {
expected := "Hello, BENJAMIN"

actual := string(output)

assert.Equal(t, expected, actual)
}
}
}

func BenchmarkInitialize(b *testing.B) {
ctx := context.Background()
cache := wazero.NewCompilationCache()
2 changes: 2 additions & 0 deletions plugins/lib/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build:
tinygo build -target wasi -o ../../wasm/lib.wasm main.go
5 changes: 5 additions & 0 deletions plugins/lib/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/extism/extism-sdk-plugins-lib

go 1.22

require github.com/extism/go-pdk v1.0.2
2 changes: 2 additions & 0 deletions plugins/lib/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/extism/go-pdk v1.0.2 h1:UB7oTW3tw2zoMlsUdBEDAAbhQg9OudzgNeyCwQYZ730=
github.com/extism/go-pdk v1.0.2/go.mod h1:Gz+LIU/YCKnKXhgge8yo5Yu1F/lbv7KtKFkiCSzW/P4=
18 changes: 18 additions & 0 deletions plugins/lib/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"strings"

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

//go:export capitalize
func Capitalize(ptr uint64) uint64 {
mem := pdk.FindMemory(ptr)
bytes := mem.ReadBytes()
capitalized := strings.ToUpper(string(bytes))
out := pdk.AllocateString(capitalized)
return out.Offset()
}

func main() {}
2 changes: 2 additions & 0 deletions plugins/main/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build:
tinygo build -target wasi -o ../../wasm/main.wasm main.go
5 changes: 5 additions & 0 deletions plugins/main/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/extism/extism-sdk-plugins-main

go 1.22

require github.com/extism/go-pdk v1.0.2
2 changes: 2 additions & 0 deletions plugins/main/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/extism/go-pdk v1.0.2 h1:UB7oTW3tw2zoMlsUdBEDAAbhQg9OudzgNeyCwQYZ730=
github.com/extism/go-pdk v1.0.2/go.mod h1:Gz+LIU/YCKnKXhgge8yo5Yu1F/lbv7KtKFkiCSzW/P4=
24 changes: 24 additions & 0 deletions plugins/main/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

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

//go:wasm-module lib
//export capitalize
func Capitalize(offset uint64) uint64

//go:export run_test
func run_test() int32 {
name := pdk.InputString()

ptr := pdk.AllocateString(name)
capitalizedPtr := Capitalize(ptr.Offset())
capitalizedMem := pdk.FindMemory(capitalizedPtr)
capitalized := string(capitalizedMem.ReadBytes())

pdk.OutputString("Hello, " + capitalized)
return 0
}

func main() {}
Binary file added wasm/lib.wasm
Binary file not shown.
Binary file added wasm/main.wasm
Binary file not shown.