Jednostavan vodič za ispis poruke koristeći WebAssembly i JavaScript, idealan za početnike koji žele naučiti osnovne korake i integraciju.
;; WebAssembly modul u tekstualnom formatu (WAT)
(module
;; Definicija memorije s 1 stranicom (64KiB)
(memory (export "memory") 1)
;; Podaci: ASCII poruka završena s 0
(data (i32.const 0) "Pozdrav iz WebAssembly!\00")
;; Izvezena funkcija koja vraća adresu poruke (0)
(func (export "getMessagePtr") (result i32)
(i32.const 0)
)
)
// JavaScript kod za učitavanje i korištenje modula
// (za izvođenje u pregledniku s podrškom za WebAssembly)
(async () => {
// Pomoćna funkcija za pretvaranje WebAssembly memorije u JS niz
function readStringFromMemory(memory, ptr) {
const buffer = new Uint8Array(memory.buffer);
let str = "";
for (let i = ptr; buffer[i] !== 0; i++) {
str += String.fromCharCode(buffer[i]);
}
return str;
}
// Kompilacija i instanciranje WebAssembly modula
const wasmCode = `(module
(memory (export \"memory\") 1)
(data (i32.const 0) \"Pozdrav iz WebAssembly!\00\")
(func (export \"getMessagePtr\") (result i32)
(i32.const 0)
)
)`;
// WebAssembly.compileStreaming se ne može direktno koristiti za pretvaranje WAT u binarni format,
// pa koristimo WebAssembly.compile s binarnim bufferom. Za jednostavnost ovdje koristimo unaprijed kompajlirani base64.
// Base64 kompajliranog modula (dobiven s wat2wasm)
const base64Wasm = "AGFzbQEAAAABBgFgAX8BfwMCAQAHBwEDZm0AAQEDAAEAAQ==";
// Dekodiranje base64 u Uint8Array
function base64ToUint8Array(base64) {
const binaryString = atob(base64);
const len = binaryString.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes;
}
const wasmBinary = base64ToUint8Array(base64Wasm);
// Instanciranje modula
const wasmModule = await WebAssembly.instantiate(wasmBinary);
// Dohvaćanje memorije i funkcije
const { memory, getMessagePtr } = wasmModule.instance.exports;
// Dohvaćanje adrese poruke
const ptr = getMessagePtr();
// Čitanje niza iz memorije
const message = readStringFromMemory(memory, ptr);
// Ispis poruke u konzolu
console.log(message);
})();html
<!DOCTYPE html>
<html lang="hr">
<head>
<meta charset="UTF-8">
<title>Ispis poruke s WebAssembly i JavaScript</title>
</head>
<body>
<h1>Poruka iz WebAssembly modula</h1>
<pre id="output"></pre>
<script>
;; WebAssembly modul u tekstualnom formatu (WAT)
(module
;; Definicija memorije s 1 stranicom (64KiB)
(memory (export "memory") 1)
;; Podaci: ASCII poruka završena s 0
(data (i32.const 0) "Pozdrav iz WebAssembly!\00")
;; Izvezena funkcija koja vraća adresu poruke (0)
(func (export "getMessagePtr") (result i32)
(i32.const 0)
)
)
// JavaScript kod za učitavanje i korištenje modula
// (za izvođenje u pregledniku s podrškom za WebAssembly)
(async () => {
// Pomoćna funkcija za pretvaranje WebAssembly memorije u JS niz
function readStringFromMemory(memory, ptr) {
const buffer = new Uint8Array(memory.buffer);
let str = "";
for (let i = ptr; buffer[i] !== 0; i++) {
str += String.fromCharCode(buffer[i]);
}
return str;
}
// Kompilacija i instanciranje WebAssembly modula
const wasmCode = `(module
(memory (export \"memory\") 1)
(data (i32.const 0) \"Pozdrav iz WebAssembly!\00\")
(func (export \"getMessagePtr\") (result i32)
(i32.const 0)
)
)`;
// WebAssembly.compileStreaming se ne može direktno koristiti za pretvaranje WAT u binarni format,
// pa koristimo WebAssembly.compile s binarnim bufferom. Za jednostavnost ovdje koristimo unaprijed kompajlirani base64.
// Base64 kompajliranog modula (dobiven s wat2wasm)
const base64Wasm = "AGFzbQEAAAABBgFgAX8BfwMCAQAHBwEDZm0AAQEDAAEAAQ==";
// Dekodiranje base64 u Uint8Array
function base64ToUint8Array(base64) {
const binaryString = atob(base64);
const len = binaryString.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes;
}
const wasmBinary = base64ToUint8Array(base64Wasm);
// Instanciranje modula
const wasmModule = await WebAssembly.instantiate(wasmBinary);
// Dohvaćanje memorije i funkcije
const { memory, getMessagePtr } = wasmModule.instance.exports;
// Dohvaćanje adrese poruke
const ptr = getMessagePtr();
// Čitanje niza iz memorije
const message = readStringFromMemory(memory, ptr);
// Ispis poruke u konzolu i na stranicu
console.log(message);
document.getElementById('output').textContent = message;
})();
</script>
</body>
</html>WebAssembly: modul napisan u WebAssembly Text Format (WAT) koji izvozi funkciju za vraćanje poruke.
JavaScript: kod koji učitava i instancira WebAssembly modul, poziva izvezenu funkciju i ispisuje poruku u konzolu.
Nema vanjskih ovisnosti, radi u bilo kojem modernom pregledniku s podrškom za WebAssembly.
Konfiguracija: WebAssembly modul je napisan u tekstualnom formatu (.wat) i pretvoren u binarni (.wasm) pomoću standardnih alata (npr. wat2wasm).
Arduino: nije primjenjivo u ovom primjeru.
Pinout: nije primjenjivo.
Softver: nisu potrebne vanjske biblioteke, samo preglednik s podrškom za WebAssembly.