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{},
)
| 162 | // |
| 163 | // If the function `f` panics then the panic will be propagated to the caller. |
| 164 | func 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 | |
| 185 | func inferFuncType(val reflect.Value) *FuncType { |
| 186 | // Make sure the `interface{}` passed in was indeed a function |
searching dependent graphs…