github.com/moontrade/wavm-go@v0.3.2-0.20220316110326-d229dd66ad65/instance.go (about) 1 package wavm 2 3 // #include <stdlib.h> 4 // #include "wavm-c.h" 5 import "C" 6 import ( 7 "math" 8 "unsafe" 9 ) 10 11 type Instance C.wasm_instance_t 12 13 func NewInstance( 14 store *Store, 15 module *Module, 16 imports []*Extern, 17 outTrap **Trap, 18 debugName string, 19 ) *Instance { 20 var name *C.char 21 if debugName == "" { 22 name = EMPTY 23 } else { 24 name = C.CString(debugName) 25 defer C.free(unsafe.Pointer(name)) 26 } 27 return (*Instance)(C.wasm_instance_new( 28 (*C.wasm_store_t)(store), 29 (*C.wasm_module_t)(module), 30 (**C.wasm_extern_t)(unsafe.Pointer(&imports[0])), 31 (**C.wasm_trap_t)(unsafe.Pointer(outTrap)), 32 name, 33 )) 34 } 35 36 func NewInstanceWithQuota( 37 store *Store, 38 module *Module, 39 imports []*Extern, 40 outTrap **Trap, 41 maxTableElems int32, 42 maxMemoryPages int32, 43 callStartFunction bool, 44 debugName string, 45 ) *Instance { 46 if maxTableElems < 1 { 47 maxTableElems = math.MaxUint16 48 } 49 if maxMemoryPages < 1 { 50 maxMemoryPages = 1 51 } 52 var name *C.char 53 if debugName == "" { 54 name = EMPTY 55 } else { 56 name = C.CString(debugName) 57 defer C.free(unsafe.Pointer(name)) 58 } 59 callStart := C.int32_t(0) 60 if callStartFunction { 61 callStart = C.int32_t(1) 62 } 63 return (*Instance)(C.wasm_instance_new_with_quota( 64 (*C.wasm_store_t)(store), 65 (*C.wasm_module_t)(module), 66 (**C.wasm_extern_t)(unsafe.Pointer(&imports[0])), 67 (**C.wasm_trap_t)(unsafe.Pointer(outTrap)), 68 C.int32_t(maxTableElems), C.int32_t(maxMemoryPages), 69 callStart, 70 name, 71 )) 72 } 73 74 func (s *Store) NewInstance( 75 module *Module, 76 imports []*Extern, 77 outTrap **Trap, 78 debugName string, 79 ) *Instance { 80 return NewInstance(s, module, imports, outTrap, debugName) 81 } 82 83 func (s *Store) NewInstanceWithQuota( 84 module *Module, 85 imports []*Extern, 86 outTrap **Trap, 87 maxTableElems, maxMemoryPages int32, 88 callStartFunction bool, 89 debugName string, 90 ) *Instance { 91 return NewInstanceWithQuota(s, module, imports, outTrap, maxTableElems, maxMemoryPages, callStartFunction, debugName) 92 } 93 94 func (inst *Instance) Close() error { 95 inst.Delete() 96 return nil 97 } 98 func (inst *Instance) Delete() { 99 C.wasm_instance_delete((*C.wasm_instance_t)(inst)) 100 } 101 102 func (inst *Instance) NumExports() int { 103 return int(C.wasm_instance_num_exports((*C.wasm_instance_t)(inst))) 104 } 105 106 func (inst *Instance) Export(index int) *Extern { 107 return (*Extern)(C.wasm_instance_export((*C.wasm_instance_t)(inst), C.size_t(index))) 108 } 109 110 func (inst *Instance) Exports(exports []*Extern) []*Extern { 111 count := inst.NumExports() 112 if len(exports) > 0 { 113 exports = exports[:0] 114 } 115 for i := 0; i < count; i++ { 116 exports = append(exports, inst.Export(i)) 117 } 118 return exports 119 }