github.com/moontrade/wavm-go@v0.3.2-0.20220316110326-d229dd66ad65/func.go (about) 1 package wavm 2 3 /* 4 #include <stdlib.h> 5 #include "wavm-c.h" 6 7 wasm_trap_t* wasm_func_call_no_copy(wasm_store_t*, 8 const wasm_func_t*, 9 const wasm_val_t args[], 10 wasm_val_t results[]); 11 12 wasm_trap_t* wasm_func_call_no_trap(wasm_store_t*, 13 const wasm_func_t*, 14 const wasm_val_t args[], 15 wasm_val_t results[]); 16 17 wasm_trap_t* wasm_func_call_no_copy_no_trap(wasm_store_t*, 18 const wasm_func_t*, 19 const wasm_val_t args[], 20 wasm_val_t results[]); 21 22 */ 23 import "C" 24 import "unsafe" 25 26 type ( 27 Func C.wasm_func_t 28 FuncCallback C.wasm_func_callback_t 29 ) 30 31 var ( 32 WASMFuncCall = C.wasm_func_call 33 WASMFuncCallNoCopy = C.wasm_func_call_no_copy 34 WASMFuncCallNoTrap = C.wasm_func_call_no_trap 35 WASMFuncCallNoCopyNoTrap = C.wasm_func_call_no_copy_no_trap 36 ) 37 38 func NewFunc( 39 compartment *Compartment, 40 funcType *FuncType, 41 callback FuncCallback, 42 debugName string) *Func { 43 var name *C.char 44 if debugName == "" { 45 name = EMPTY 46 } else { 47 name = C.CString(debugName) 48 defer C.free(unsafe.Pointer(name)) 49 } 50 return (*Func)(C.wasm_func_new( 51 (*C.wasm_compartment_t)(compartment), 52 (*C.wasm_functype_t)(funcType), 53 (C.wasm_func_callback_t)(callback), 54 name, 55 )) 56 } 57 58 func (c *Compartment) NewFunc( 59 funcType *FuncType, 60 callback FuncCallback, 61 debugName string, 62 ) *Func { 63 return NewFunc(c, funcType, callback, debugName) 64 } 65 66 //func WASMFuncNewWithEnv( 67 // compartment *Compartment, 68 // funcType *FuncType, 69 // callback C.wasm_func_callback_with_env_t, 70 // env *C.void, 71 // finalizer func(env *C.void), 72 // debugName string, 73 //) *Func { 74 // name := C.CString(debugName) 75 // defer C.free(unsafe.Pointer(name)) 76 // return (*Func)(C.wasm_func_new((*C.wasm_compartment_t)(compartment), (*C.wasm_functype_t)(funcType), callback, name)) 77 //} 78 79 func (f *Func) Close() error { 80 f.Delete() 81 return nil 82 } 83 84 func (f *Func) Delete() { 85 C.wasm_func_delete((*C.wasm_func_t)(f)) 86 } 87 88 func (f *Func) Type() *FuncType { 89 return (*FuncType)(C.wasm_func_type((*C.wasm_func_t)(f))) 90 } 91 92 func (f *Func) ParamArity() int { 93 return int(C.wasm_func_param_arity((*C.wasm_func_t)(f))) 94 } 95 96 func (f *Func) ResultArity() int { 97 return int(C.wasm_func_result_arity((*C.wasm_func_t)(f))) 98 } 99 100 func (f *Func) Call(store *Store, args *Val, results *Val) *Trap { 101 return (*Trap)(C.wasm_func_call( 102 (*C.wasm_store_t)(store), 103 (*C.wasm_func_t)(f), 104 (*C.wasm_val_t)(args), 105 (*C.wasm_val_t)(results), 106 )) 107 } 108 109 func (f *Func) CallNoTrap(store *Store, args *Val, results *Val) *Trap { 110 return (*Trap)(C.wasm_func_call_no_trap( 111 (*C.wasm_store_t)(store), 112 (*C.wasm_func_t)(f), 113 (*C.wasm_val_t)(args), 114 (*C.wasm_val_t)(results), 115 )) 116 } 117 118 func (f *Func) CallNoCopy(store *Store, args *Val, results *Val) *Trap { 119 return (*Trap)(C.wasm_func_call_no_copy( 120 (*C.wasm_store_t)(store), 121 (*C.wasm_func_t)(f), 122 (*C.wasm_val_t)(args), 123 (*C.wasm_val_t)(results), 124 )) 125 } 126 127 func (f *Func) CallUnsafe(store *Store, args *Val, results *Val) *Trap { 128 return (*Trap)(C.wasm_func_call_no_copy_no_trap( 129 (*C.wasm_store_t)(store), 130 (*C.wasm_func_t)(f), 131 (*C.wasm_val_t)(args), 132 (*C.wasm_val_t)(results), 133 )) 134 }