0Pricing
WebAssembly (WASM) for High Performance Apps · 课时

跨 JS/WASM 边界共享内存与类型化数组

通过线性内存、类型化数组视图和基于指针的 ABI,掌握 JavaScript 与 WebAssembly 之间的高效数据交换。

跨 JS/WASM 边界共享内存与类型化数组 是 CoddyKit 上的免费 WebAssembly (WASM) for High Performance Apps 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 WebAssembly (WASM) for High Performance Apps 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 WebAssembly (WASM) for High Performance Apps 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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.

常见问题解答

「跨 JS/WASM 边界共享内存与类型化数组」课时是免费的吗?

是的 — 「跨 JS/WASM 边界共享内存与类型化数组」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 WebAssembly (WASM) for High Performance Apps 课程的其余内容,请升级到 CoddyKit PRO。 WebAssembly (WASM) for High Performance Apps 课程共包含 4 节课。

「跨 JS/WASM 边界共享内存与类型化数组」这节课中我会学到什么?

通过线性内存、类型化数组视图和基于指针的 ABI,掌握 JavaScript 与 WebAssembly 之间的高效数据交换。 你通过在浏览器中直接运行的动手代码来练习 WebAssembly (WASM) for High Performance Apps,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 WebAssembly (WASM) for High Performance Apps 需要有经验吗?

无需任何先前经验。CoddyKit 上的 WebAssembly (WASM) for High Performance Apps 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「跨 JS/WASM 边界共享内存与类型化数组」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 WebAssembly (WASM) for High Performance Apps 课中编写并运行代码吗?

能。每节 WebAssembly (WASM) for High Performance Apps 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 使用 WASM 执行异步操作
  2. 自定义 JavaScript 回调
  3. 错误处理与异常
  4. 跨 JS/WASM 边界共享内存与类型化数组
← 返回 WebAssembly (WASM) for High Performance Apps