Module | Web Assembly
- Module | Web Assembly
(module
;; Exported function to add two numbers
(func $add (param $a i32) (param $b i32) (result i32)
;; Add the two parameters and return the result
(i32.add
(get_local $a)
(get_local $b)
)
)
;; Export the function so it can be called from JavaScript
(export "add" (func $add))
)
Transformando de wat para wasm
wat2wasm module.wat -o module.wasm
Usando no javascript
// Load the WebAssembly module
fetch('module.wasm')
.then(response => response.arrayBuffer())
.then(bytes => WebAssembly.instantiate(bytes))
.then(results => {
// Get the exported function
const add = results.instance.exports.add;
// Call the function
console.log(add(2, 3)); // Output: 5
});