github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/modules/wasm/host/host_test.go (about)

     1  package host_test
     2  
     3  import (
     4  	"os"
     5  	"reflect"
     6  	"sort"
     7  	"testing"
     8  
     9  	"github.com/google/uuid"
    10  	. "github.com/onsi/gomega"
    11  	"golang.org/x/exp/maps"
    12  
    13  	"github.com/machinefi/w3bstream/pkg/models"
    14  	"github.com/machinefi/w3bstream/pkg/modules/wasm/abi/proxy"
    15  	"github.com/machinefi/w3bstream/pkg/modules/wasm/abi/types"
    16  	"github.com/machinefi/w3bstream/pkg/modules/wasm/host"
    17  	"github.com/machinefi/w3bstream/pkg/modules/wasm/runtime/wasmtime"
    18  )
    19  
    20  func TestHostFunctions(t *testing.T) {
    21  	content, err := os.ReadFile("../testdata/log.wasm")
    22  	NewWithT(t).Expect(err).To(BeNil())
    23  
    24  	vmid := uuid.NewString()
    25  	vm := wasmtime.NewWasmtimeVM(vmid)
    26  	NewWithT(t).Expect(vm.ID()).To(Equal(vmid))
    27  
    28  	mod, err := vm.NewModule(content)
    29  	NewWithT(t).Expect(err).To(BeNil())
    30  
    31  	instance := wasmtime.NewWasmtimeInstance(vm.(*wasmtime.VM), mod.(*wasmtime.Module))
    32  	NewWithT(t).Expect(instance.ID()).To(Equal(vmid))
    33  
    34  	prj := &models.Project{}
    35  	prj.ProjectID = 1
    36  
    37  	imports := types.NewDefaultImports()
    38  
    39  	instance.SetUserdata(&proxy.ABIContext{
    40  		Imports:  imports,
    41  		Instance: instance,
    42  	})
    43  
    44  	mapping, err := host.HostFunctions(instance)
    45  	NewWithT(t).Expect(err).To(BeNil())
    46  
    47  	keys := maps.Keys(mapping)
    48  	sort.Slice(keys, func(i, j int) bool {
    49  		return keys[i] < keys[j]
    50  	})
    51  
    52  	for _, key := range keys {
    53  		t.Logf("%s: %s", key, reflect.TypeOf(mapping[key]))
    54  	}
    55  
    56  	NewWithT(t).Expect(instance.Start()).To(BeNil())
    57  	defer instance.Stop()
    58  
    59  	f, err := instance.GetExportsFunc("malloc")
    60  	NewWithT(t).Expect(err).To(BeNil())
    61  	t.Log(f.Params(), f.Results())
    62  
    63  	f, err = instance.GetExportsFunc("alloc")
    64  	NewWithT(t).Expect(err).To(BeNil())
    65  	t.Log(f.Params(), f.Results())
    66  }