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

Function TestLinkerFuncs

linker_test.go:126–183  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

124}
125
126func TestLinkerFuncs(t *testing.T) {
127 engine := NewEngine()
128 linker := NewLinker(engine)
129 var called int
130 var callers []string
131 err := linker.FuncWrap("foo", "bar", func(c *Caller) {
132 called += 1
133
134 data := c.Data()
135 if data != nil {
136 callers = append(callers, data.(string))
137 }
138 })
139 require.NoError(t, err)
140
141 wasm, err := Wat2Wasm(`
142 (module
143 (import "foo" "bar" (func))
144 (start 0)
145 )
146 `)
147 require.NoError(t, err)
148
149 module, err := NewModule(engine, wasm)
150 require.NoError(t, err)
151
152 _, err = linker.Instantiate(NewStoreWithData(engine, "one"), module)
153 require.NoError(t, err)
154 require.Equal(t, 1, called, "expected a call")
155 require.Equal(t, []string{"one"}, callers)
156
157 _, err = linker.Instantiate(NewStore(engine), module)
158 require.NoError(t, err)
159 require.Equal(t, 2, called, "expected a call")
160
161 cb := func(caller *Caller, args []Val) ([]Val, *Trap) {
162 called += 2
163 data := caller.Data()
164 if data != nil {
165 callers = append(callers, data.(string))
166 }
167
168 return []Val{}, nil
169 }
170 ty := NewFuncType([]*ValType{}, []*ValType{})
171 linker.AllowShadowing(true)
172 err = linker.FuncNew("foo", "bar", ty, cb)
173 require.NoError(t, err)
174
175 _, err = linker.Instantiate(NewStoreWithData(engine, "two"), module)
176 require.NoError(t, err)
177 require.Equal(t, 4, called, "expected a call")
178 require.Equal(t, []string{"one", "two"}, callers)
179
180 _, err = linker.Instantiate(NewStore(engine), module)
181 require.NoError(t, err)
182 require.Equal(t, 6, called, "expected a call")
183}

Callers

nothing calls this directly

Calls 12

FuncWrapMethod · 0.95
InstantiateMethod · 0.95
AllowShadowingMethod · 0.95
FuncNewMethod · 0.95
NewEngineFunction · 0.85
NewLinkerFunction · 0.85
Wat2WasmFunction · 0.85
NewModuleFunction · 0.85
NewStoreWithDataFunction · 0.85
NewStoreFunction · 0.85
NewFuncTypeFunction · 0.85
DataMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…