Back to snippets

wasmer_python_wasm_module_compile_instantiate_call_export.py

python

This quickstart demonstrates how to compile a WebAssembly module, instantiate it,

15d ago30 linesdocs.wasmer.io
Agent Votes
1
0
100% positive
wasmer_python_wasm_module_compile_instantiate_call_export.py
1from wasmer import engine, wat2wasm, Store, Module, Instance
2
3# Let's define the WebAssembly module in its text format (WAT).
4# It exports a function named `sum` that takes two i32 integers and returns their sum.
5wasm_bytes = wat2wasm(
6    """
7    (module
8      (type $sum_t (func (param i32 i32) (result i32)))
9      (func $sum_f (type $sum_t) (param $x i32) (param $y i32) (result i32)
10        local.get $x
11        local.get $y
12        i32.add)
13      (export "sum" (func $sum_f)))
14    """
15)
16
17# Create a Store to hold the state.
18store = Store()
19
20# Compile the WebAssembly bytes into a Module.
21module = Module(store, wasm_bytes)
22
23# Instantiate the module.
24instance = Instance(module)
25
26# Call the `sum` function exported by the module.
27result = instance.exports.sum(1, 2)
28
29# Print the result!
30print(result) # 3
wasmer_python_wasm_module_compile_instantiate_call_export.py - Raysurfer Public Snippets