NewInstance instantiates a WebAssembly `module` with the `imports` provided. This function will attempt to create a new wasm instance given the provided imports. This can fail if the wrong number of imports are specified, the imports aren't of the right type, or for other resource-related issues.
(store Storelike, module *Module, imports []AsExtern)
| 22 | // This will also run the `start` function of the instance, returning an error |
| 23 | // if it traps. |
| 24 | func NewInstance(store Storelike, module *Module, imports []AsExtern) (*Instance, error) { |
| 25 | importsRaw := make([]C.wasmtime_extern_t, len(imports)) |
| 26 | for i, imp := range imports { |
| 27 | importsRaw[i] = imp.AsExtern() |
| 28 | } |
| 29 | var val C.wasmtime_instance_t |
| 30 | err := enterWasm(store, func(trap **C.wasm_trap_t) *C.wasmtime_error_t { |
| 31 | var imports *C.wasmtime_extern_t |
| 32 | if len(importsRaw) > 0 { |
| 33 | imports = (*C.wasmtime_extern_t)(unsafe.Pointer(&importsRaw[0])) |
| 34 | } |
| 35 | return C.wasmtime_instance_new( |
| 36 | store.Context(), |
| 37 | module.ptr(), |
| 38 | imports, |
| 39 | C.size_t(len(importsRaw)), |
| 40 | &val, |
| 41 | trap, |
| 42 | ) |
| 43 | }) |
| 44 | runtime.KeepAlive(store) |
| 45 | runtime.KeepAlive(module) |
| 46 | runtime.KeepAlive(imports) |
| 47 | runtime.KeepAlive(importsRaw) |
| 48 | if err != nil { |
| 49 | return nil, err |
| 50 | } |
| 51 | return mkInstance(val), nil |
| 52 | } |
| 53 | |
| 54 | func mkInstance(val C.wasmtime_instance_t) *Instance { |
| 55 | return &Instance{val} |
searching dependent graphs…