MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime-go / NewInstance

Function NewInstance

instance.go:24–52  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

22// This will also run the `start` function of the instance, returning an error
23// if it traps.
24func 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
54func mkInstance(val C.wasmtime_instance_t) *Instance {
55 return &Instance{val}

Callers 15

TestInstanceFunction · 0.85
TestInstanceBadFunction · 0.85
TestInstanceGetFuncFunction · 0.85
refTypesInstanceFunction · 0.85
TestLinkerFunction · 0.85
TestTrapFramesFunction · 0.85
TestTrapModuleNameFunction · 0.85
TestInterruptWasmFunction · 0.85
TestStoreDataFunction · 0.85
TestCallFuncFromCallerFunction · 0.85

Calls 5

enterWasmFunction · 0.85
mkInstanceFunction · 0.85
AsExternMethod · 0.65
ContextMethod · 0.65
ptrMethod · 0.45

Tested by 15

TestInstanceFunction · 0.68
TestInstanceBadFunction · 0.68
TestInstanceGetFuncFunction · 0.68
refTypesInstanceFunction · 0.68
TestLinkerFunction · 0.68
TestTrapFramesFunction · 0.68
TestTrapModuleNameFunction · 0.68
TestInterruptWasmFunction · 0.68
TestStoreDataFunction · 0.68
TestCallFuncFromCallerFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…