github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/core/vm/wasm_test.go (about)

     1  package vm
     2  
     3  /*
     4  func TestNewWasm(t *testing.T) {
     5  		var wasm *Wasm
     6  
     7  		code, _ := ioutil.ReadFile("release.wasm")
     8  
     9  		imports := []WasmImport{
    10  			WasmImport{
    11  				namespace: "env",
    12  				functions: []WasmImportFunc{
    13  					WasmImportFunc{
    14  						name: "log",
    15  						inputTypes: []w.ValueKind{
    16  							w.I32,
    17  						},
    18  						outputTypes: []w.ValueKind{},
    19  						nativeFunc: func(args []w.Value) ([]w.Value, error) {
    20  							data, e := wasm.GetMemory("memory")
    21  							if e != nil {
    22  								return nil, e
    23  							}
    24  							fmt.Println(string(data[args[0].I32():]))
    25  							return []w.Value{}, nil
    26  						},
    27  					},
    28  					WasmImportFunc{
    29  						name: "abort",
    30  						inputTypes: []w.ValueKind{
    31  							w.I32,
    32  							w.I32,
    33  							w.I32,
    34  							w.I32,
    35  						},
    36  						outputTypes: []w.ValueKind{},
    37  						nativeFunc: func(args []w.Value) ([]w.Value, error) {
    38  							return []w.Value{}, nil
    39  						},
    40  					},
    41  				},
    42  			},
    43  		}
    44  
    45  		wasm, e := NewWasm(code, imports)
    46  		NewWithT(t).Expect(e).To(BeNil())
    47  
    48  		sum, e := wasm.ExecuteFunction("add", 1, 2)
    49  		NewWithT(t).Expect(e).To(BeNil())
    50  
    51  		v, ok := sum.(int32)
    52  		NewWithT(t).Expect(ok).To(BeTrue())
    53  		NewWithT(t).Expect(v).To(Equal(int32(3)))
    54  
    55  		_, e = wasm.ExecuteFunction("hello")
    56  		NewWithT(t).Expect(e).To(BeNil())
    57  }
    58  */