Any example how to load `.wasm` in python
Hi,
I am working on a wasm package and I am using emscripten to compile a code to .wasm I was wondering if there is an clear example how to load a .wasm using wasmtime-py?
Hello,
did you check the examples folder?
e.g. hello.py has an example on how to use Module.from_file
@womeier thanks! I checked those examples and they load wat files. Does the api support .wasm too? I will try that!
Thanks!
Yes *.wasm is supported. All the examples use *.wat to avoid checking binaries into the repo. If you're up for it a PR to improve documentation would be appreciated here!
@alexcrichton yeah sure I will add a PR to mention it in the documentations! Thanks!
@alexcrichton I have another question. So I am trying the following code
from wasmtime import Store, Module, Instance, Func, FuncType
# Almost all operations in wasmtime require a contextual "store" argument to be
# shared amongst objects
store = Store()
# Here we can compile a `Module` which is then ready for instantiation
# afterwards
module = Module.from_file(store.engine, 'lib-wasm-py/lib.wasm')
####
# Our module needs one import, so we'll create that here.
def say_hello():
print("Hello from Python!")
hello = Func(store, FuncType([], []), say_hello)
# And with all that we can instantiate our module and call the export!
instance = Instance(store, module, [hello])
and in this example an function called hello is defined and it is passed to theimports of the instance. How can I find out how to define this function? is there any way to get the list of the imports from .wasm file directly?
Yes that should be possible through Module.imports
Thanks @alexcrichton ,
I tried to pass the module.imports to the Instance object but it seems their types are different.
I think something that confuses me is that I am not sure where does imports: Sequence[AsExtern] comes from when we define an Instance.
Ah yes Module.imports only gives you the type of the imports. It's up to you as an embedder to create those imports and pass them in.
If you're confused by this I might recommend using Linker instead as it's a bit more understandable about how modules are instantiated.
Thanks @alexcrichton !
I thought it is the imports are easy to define but it seems emscripten generates them for js and so I could easily use them in js but in python I have to define them. I am checking Linker docs now to get a grasp about their usage!
@am2222 Any progress here? I'm confused too, when I coding a wasm in RUST, it worked in JavaScript easily with generated binding, But how to get the bindings in python. I have no idea about this.
I believe this was answered and there are examples, so I'm going to close this.