bytecodealliance/wasmtime-go

Why does the wasmtime import function need to be declared in advance?

Closed this issue · 4 comments

Why does the wasmtime import function need to be declared in advance?

Sorry I'm not 100% sure what you're asking here, can you clarify with some code and ask about a particular API?

Sorry I'm not 100% sure what you're asking here, can you clarify with some code and ask about a particular API?

Like the following code, if it is without any import declarations, it will report an error, and more often, how many functions are imported is often unknown in advance.

截屏2022-09-08 13 34 17

=== RUN TestNewInstanceButNoImport
--- FAIL: TestNewInstanceButNoImport (0.00s)
panic: expected 0 imports, found 1 [recovered]
panic: expected 0 imports, found 1

goroutine 35 [running]:
testing.tRunner.func1.2({0x4137460, 0xc00011e340})
/usr/local/Cellar/go/1.17.2/libexec/src/testing/testing.go:1209 +0x24e
testing.tRunner.func1()
/usr/local/Cellar/go/1.17.2/libexec/src/testing/testing.go:1212 +0x218
panic({0x4137460, 0xc00011e340})
/usr/local/Cellar/go/1.17.2/libexec/src/runtime/panic.go:1038 +0x215
github.com/bytecodealliance/wasmtime-go.TestNewInstanceButNoImport(0x0)
/Users/wangzhilong23/JD/go/src/wasmtime-go/instance_test.go:169 +0xf4
testing.tRunner(0xc0001051e0, 0x415ff30)
/usr/local/Cellar/go/1.17.2/libexec/src/testing/testing.go:1259 +0x102
created by testing.(*T).Run
/usr/local/Cellar/go/1.17.2/libexec/src/testing/testing.go:1306 +0x35a

This is the contract of the NewInstance API where the imports must line up exactly with the imports reported by the module. You can learn the imports before instantiation through the Module type. I'd recommend using the Linker type, however, for instantiation since the NewInstance API is intended to be a low-level control.

This is the contract of the NewInstance API where the imports must line up exactly with the imports reported by the module. You can learn the imports before instantiation through the Module type. I'd recommend using the Linker type, however, for instantiation since the NewInstance API is intended to be a low-level control.

OK, I got it, thanks