0Pricing
WebAssembly (WASM) for High Performance Apps · Lezione

Condivisione della memoria e degli array tipizzati tra i confini JS/WASM

Padroneggi lo scambio efficiente di dati tra JavaScript e WebAssembly usando memoria lineare, viste su array tipizzati e ABI basate su puntatori.

Condivisione della memoria e degli array tipizzati tra i confini JS/WASM è una lezione WebAssembly (WASM) for High Performance Apps gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento WebAssembly (WASM) for High Performance Apps, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso WebAssembly (WASM) for High Performance Apps include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

The Boundary Problem

WASM functions only accept and return numbers. To pass strings, arrays, or structs you must work directly with WebAssembly.Memory — a single growable ArrayBuffer.

Linear Memory as a View

You read and write guest memory from JS by creating typed array views over the module memory buffer.

const mem = wasm.exports.memory;
const u8 = new Uint8Array(mem.buffer);
u8[0] = 65; // write a byte at offset 0

Allocating Inside WASM

The guest owns its heap, so allocate there and get back a pointer.

const ptr = wasm.exports.malloc(len);
const view = new Uint8Array(wasm.exports.memory.buffer, ptr, len);
view.set(bytes);
wasm.exports.process(ptr, len);
wasm.exports.free(ptr);

Passing Strings In

Encode a JS string to UTF-8 bytes, copy into guest memory, pass pointer + length.

const enc = new TextEncoder();
const bytes = enc.encode("hello");
const ptr = wasm.exports.malloc(bytes.length);
new Uint8Array(wasm.exports.memory.buffer, ptr, bytes.length).set(bytes);
wasm.exports.handle(ptr, bytes.length);

Reading Strings Out

Given a pointer + length returned by the guest, slice the bytes and decode.

const u8 = new Uint8Array(wasm.exports.memory.buffer, ptr, len);
const str = new TextDecoder().decode(u8);

The Detached Buffer Trap

When WASM memory grows, the old ArrayBuffer is detached and your views become invalid. Always recreate views after a call that may allocate.

Numeric Arrays

For Float64Array or Int32Array data, use the matching view and remember offsets are in bytes, so multiply the index by the element size.

const f64 = new Float64Array(wasm.exports.memory.buffer, ptr, count);

Zero-Copy with SharedArrayBuffer

If both JS and WASM use a SharedArrayBuffer-backed memory, multiple threads can read the same data without copying — powerful but requires atomics for safety.

Binding Generators

Tools like wasm-bindgen (Rust) or Emscripten embind hide this pointer arithmetic, generating glue that marshals strings and objects automatically.

Endianness & Alignment

WASM memory is little-endian. Respect type alignment (e.g. 8-byte boundaries for f64) when laying out structs manually to avoid corrupt reads.

Ownership Rules

Decide who frees what. A common convention: the side that allocates also frees. Leaking guest allocations grows linear memory until memory.grow fails.

Quick Check

Why can a typed array view become invalid after calling a WASM function?

Recap

You learned to exchange data across the boundary via linear memory, pointer + length ABIs, and typed-array views. Watch the detached-buffer trap after growth, respect alignment, define clear ownership, and lean on binding generators for ergonomics.

Domande Frequenti

La lezione «Condivisione della memoria e degli array tipizzati tra i confini JS/WASM» è gratuita?

Sì — il testo completo di «Condivisione della memoria e degli array tipizzati tra i confini JS/WASM» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso WebAssembly (WASM) for High Performance Apps, passa a CoddyKit PRO. Il corso WebAssembly (WASM) for High Performance Apps include 4 lezioni in totale.

Cosa imparerò in «Condivisione della memoria e degli array tipizzati tra i confini JS/WASM»?

Padroneggi lo scambio efficiente di dati tra JavaScript e WebAssembly usando memoria lineare, viste su array tipizzati e ABI basate su puntatori. Eserciti WebAssembly (WASM) for High Performance Apps con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare WebAssembly (WASM) for High Performance Apps?

Non è richiesta alcuna esperienza precedente. WebAssembly (WASM) for High Performance Apps su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Condivisione della memoria e degli array tipizzati tra i confini JS/WASM»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione WebAssembly (WASM) for High Performance Apps?

Sì. Ogni lezione WebAssembly (WASM) for High Performance Apps include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Operazioni asincrone con WASM
  2. Callback JavaScript personalizzate
  3. Gestione degli errori e delle eccezioni
  4. Condivisione della memoria e degli array tipizzati tra i confini JS/WASM
← Torna a WebAssembly (WASM) for High Performance Apps