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

Function WrapFunc

func.go:164–183  ·  view source on GitHub ↗

WrapFunc wraps a native Go function, `f`, as a wasm `Func`. This function differs from `NewFunc` in that it will determine the type signature of the wasm function given the input value `f`. The `f` value provided must be a Go function. It may take any number of the following types as arguments: `i

(
	store Storelike,
	f interface{},
)

Source from the content-addressed store, hash-verified

162//
163// If the function `f` panics then the panic will be propagated to the caller.
164func WrapFunc(
165 store Storelike,
166 f interface{},
167) *Func {
168 val := reflect.ValueOf(f)
169 wasmTy := inferFuncType(val)
170 idx := insertFuncWrap(getDataInStore(store), val)
171
172 ret := C.wasmtime_func_t{}
173 C.go_func_new(
174 store.Context(),
175 wasmTy.ptr(),
176 C.size_t(idx),
177 1, // this is `WrapFunc`, not `NewFunc`
178 &ret,
179 )
180 runtime.KeepAlive(store)
181 runtime.KeepAlive(wasmTy)
182 return mkFunc(ret)
183}
184
185func inferFuncType(val reflect.Value) *FuncType {
186 // Make sure the `interface{}` passed in was indeed a function

Callers 15

TestInstanceBadFunction · 0.85
TestTableFunction · 0.85
TestRefTypesWrapFunction · 0.85
TestLinkerFunction · 0.85
TestLinkerShadowingFunction · 0.85
TestInterruptWasmFunction · 0.85
TestStoreDataFunction · 0.85
TestFuncWrapSimpleFunction · 0.85
TestFuncWrapSimple1ArgFunction · 0.85
TestFuncWrapCallerArgFunction · 0.85
TestFuncWrapRet1Function · 0.85

Calls 6

inferFuncTypeFunction · 0.85
insertFuncWrapFunction · 0.85
getDataInStoreFunction · 0.85
mkFuncFunction · 0.85
ContextMethod · 0.65
ptrMethod · 0.45

Tested by 15

TestInstanceBadFunction · 0.68
TestTableFunction · 0.68
TestRefTypesWrapFunction · 0.68
TestLinkerFunction · 0.68
TestLinkerShadowingFunction · 0.68
TestInterruptWasmFunction · 0.68
TestStoreDataFunction · 0.68
TestFuncWrapSimpleFunction · 0.68
TestFuncWrapSimple1ArgFunction · 0.68
TestFuncWrapCallerArgFunction · 0.68
TestFuncWrapRet1Function · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…