Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any example how to load .wasm in python #216

Open
am2222 opened this issue Mar 24, 2024 · 9 comments
Open

Any example how to load .wasm in python #216

am2222 opened this issue Mar 24, 2024 · 9 comments

Comments

@am2222
Copy link

am2222 commented Mar 24, 2024

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?

@womeier
Copy link
Contributor

womeier commented Mar 24, 2024

Hello,
did you check the examples folder?

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

@am2222
Copy link
Author

am2222 commented Mar 25, 2024

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

Thanks!

@alexcrichton
Copy link
Member

alexcrichton commented Mar 25, 2024

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!

@am2222
Copy link
Author

am2222 commented Mar 26, 2024

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

@am2222
Copy link
Author

am2222 commented Mar 27, 2024

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

@alexcrichton
Copy link
Member

Yes that should be possible through Module.imports

@am2222
Copy link
Author

am2222 commented Mar 28, 2024

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.

@alexcrichton
Copy link
Member

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.

@am2222
Copy link
Author

am2222 commented Mar 28, 2024

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants