| Arquivo: simple.wat |
| (module (func (export "add") (param i32 i32) (result i32) local.get 0 local.get 1 i32.add) ) |
| Arquivo: index.html |
| <!DOCTYPE html> <html lang="pt-br"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>WebAssembly Example</title> </head> <body> <script> async function runWebAssembly() { // Carrega o módulo WebAssembly a partir do arquivo simple.wasm const response = await fetch('simple.wasm'); const wasmBuffer = await response.arrayBuffer(); const wasmModule = await WebAssembly.compile(wasmBuffer); const wasmInstance = await WebAssembly.instantiate(wasmModule); // Chama a função WebAssembly e obtém o resultado const result = wasmInstance.exports.add(5, 7); // Retorna o resultado (substitua isso por qualquer outra lógica desejada) return result; } // Executa a função e obtém o resultado runWebAssembly().then(result => { alert('O resultado da funcao WebAssembly eh: '+result); }); </script> </body> </html> |
| Compilando simple.wat |
| wat2wasm simple.wat -o simple.wasm |
| Arquivo simple.wasm já compilado (se sentir dificuldade de compilar) |
| simple.wasm |
| Resultado: http://localhost/webassembly/teste.html (tem que usar um servidor) |
![]() |