wasmtime-py icon indicating copy to clipboard operation
wasmtime-py copied to clipboard

Any example how to load `.wasm` in python

Open am2222 opened this issue 1 year ago • 9 comments

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?

am2222 avatar Mar 24 '24 14:03 am2222

Hello, did you check the examples folder?

e.g. hello.py has an example on how to use Module.from_file

womeier avatar Mar 24 '24 18:03 womeier

@womeier thanks! I checked those examples and they load wat files. Does the api support .wasm too? I will try that!

Thanks!

am2222 avatar Mar 25 '24 00:03 am2222

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 avatar Mar 25 '24 14:03 alexcrichton

@alexcrichton yeah sure I will add a PR to mention it in the documentations! Thanks!

am2222 avatar Mar 26 '24 17:03 am2222

@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?

am2222 avatar Mar 27 '24 22:03 am2222

Yes that should be possible through Module.imports

alexcrichton avatar Mar 27 '24 23:03 alexcrichton

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.

am2222 avatar Mar 28 '24 13:03 am2222

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.

alexcrichton avatar Mar 28 '24 15:03 alexcrichton

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 avatar Mar 28 '24 18:03 am2222

@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.

yeqown avatar Jun 24 '25 04:06 yeqown

I believe this was answered and there are examples, so I'm going to close this.

alexcrichton avatar Nov 05 '25 22:11 alexcrichton