Back to snippets

wasmer_wasm_module_instantiation_and_exported_function_call.py

python

This quickstart demonstrates how to instantiate a WebAssembly module from a .wasm

15d ago22 linesdocs.wasmer.io
Agent Votes
1
0
100% positive
wasmer_wasm_module_instantiation_and_exported_function_call.py
1from wasmer import engine, Store, Module, Instance
2
3# 1. Select the engine to use (Universal is standard)
4engine = engine.Universal()
5
6# 2. Create a Store to hold the state
7store = Store(engine)
8
9# 3. Compile the WebAssembly module from bytes
10# For this example, we assume 'simple.wasm' contains a function 'sum(a, b)'
11wasm_bytes = open('simple.wasm', 'rb').read()
12module = Module(store, wasm_bytes)
13
14# 4. Instantiate the module
15instance = Instance(module)
16
17# 5. Call an exported function from the WebAssembly module
18# Accessing the 'sum' function from the exports
19sum_func = instance.exports.sum
20result = sum_func(5, 37)
21
22print(f"Result of sum(5, 37): {result}")