0Pricing
WebAssembly (WASM) for High Performance Apps · درس

مشاركة الذاكرة والمصفوفات الم类型ة عبر حد JS/WASM

أتقن تبادل البيانات بكفاءة بين JavaScript وWebAssembly باستخدام الذاكرة الخطية، وطرق عرض المصفوفات الم类型ة، وواجهات ABI المعتمدة على المؤشرات.

مشاركة الذاكرة والمصفوفات الم类型ة عبر حد JS/WASM درس مجاني في WebAssembly (WASM) for High Performance Apps على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة WebAssembly (WASM) for High Performance Apps، انتقل إلى CoddyKit PRO. تتضمن دورة WebAssembly (WASM) for High Performance Apps 4 دروس في المجموع.

ماذا ستتعلم في «مشاركة الذاكرة والمصفوفات الم类型ة عبر حد JS/WASM»؟

أتقن تبادل البيانات بكفاءة بين JavaScript وWebAssembly باستخدام الذاكرة الخطية، وطرق عرض المصفوفات الم类型ة، وواجهات ABI المعتمدة على المؤشرات. تتمرن على WebAssembly (WASM) for High Performance Apps مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ WebAssembly (WASM) for High Performance Apps؟

لا تُشترط خبرة سابقة. WebAssembly (WASM) for High Performance Apps على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «مشاركة الذاكرة والمصفوفات الم类型ة عبر حد JS/WASM»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس WebAssembly (WASM) for High Performance Apps هذا؟

نعم. كل درس في WebAssembly (WASM) for High Performance Apps يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. العمليات غير المتزامنة باستخدام WASM
  2. عمليات استدعاء JavaScript المخصّصة
  3. معالجة الأخطاء والاستثناءات
  4. مشاركة الذاكرة والمصفوفات الم类型ة عبر حد JS/WASM
← العودة إلى WebAssembly (WASM) for High Performance Apps