การส่งโครงสร้างข้อมูลซับซ้อน
เรียนรู้กลยุทธ์ถ่ายโอนสตริง อาร์เรย์ และออบเจ็กต์แบบกำหนดเองระหว่าง WASM กับ JavaScript อย่างมีประสิทธิภาพ
การส่งโครงสร้างข้อมูลซับซ้อน เป็นบทเรียน WebAssembly (WASM) for High Performance Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน WebAssembly (WASM) for High Performance Apps และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส WebAssembly (WASM) for High Performance Apps มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Complex Data in WASM
When building high-performance applications with WebAssembly (WASM), you often need to exchange more than just simple numbers between your WASM module and JavaScript.
- How do you pass a string?
- What about an array of numbers?
- Or even a custom object structure?
This lesson explores strategies for efficiently handling these complex data types.
WASM's Linear Memory
The key to passing complex data lies in understanding WASM's linear memory. This is a contiguous block of memory (like a large array of bytes) that is shared between your WASM module and the JavaScript environment.
WASM functions can only return numbers. So, instead of passing the entire data, we pass a pointer (a memory address, which is just a number!) to where the data resides in this shared memory.
Strings: Pointer & Length
Strings are sequences of characters. To pass a string from WASM to JavaScript, the WASM module will:
- Write the string's bytes into its linear memory.
- Return two pieces of information to JavaScript:
- The starting memory address (pointer) of the string.
- The length of the string (how many bytes it occupies).
JavaScript then uses these to read the bytes from the shared memory and decode them into a JavaScript string.
C String to JS Example
Here's a C function that returns a pointer to a string. When compiled with Emscripten, JavaScript can call this function and then read the string from the WASM memory.
EMSCRIPTEN_KEEPALIVE makes the function callable from JavaScript.
#include <emscripten/emscripten.h>
#include <string.h>
// A static string for demonstration
const char* myString = "Hello WASM!";
EMSCRIPTEN_KEEPALIVE
const char* getGreetingPtr() {
return myString;
}
EMSCRIPTEN_KEEPALIVE
int getGreetingLength() {
return strlen(myString);
}
int main() {
// Required for a complete C program
return 0;
}Arrays: Contiguous Data
Numeric arrays (like int[] or float[]) are similar to strings. They are stored as a contiguous block of values in WASM's linear memory.
To pass an array, WASM returns:
- A pointer to the first element of the array.
- The number of elements in the array.
JavaScript then reads the raw bytes from the shared memory, knowing the type and size of each element (e.g., 4 bytes for an integer).
C Array to JS Example
This C code creates an array, populates it, and provides functions to get its pointer and length. JavaScript would then use Module.HEAP32 (for 32-bit integers) to access the elements.
#include <emscripten/emscripten.h>
#include <stdlib.h> // For malloc
int* dynamicArray = NULL;
int currentArrayLength = 0;
EMSCRIPTEN_KEEPALIVE
int* createAndGetArray(int size) {
if (dynamicArray) {
free(dynamicArray);
}
dynamicArray = (int*)malloc(size * sizeof(int));
currentArrayLength = size;
for (int i = 0; i < size; ++i) {
dynamicArray[i] = i * 10 + 5;
}
return dynamicArray;
}
EMSCRIPTEN_KEEPALIVE
int getArrayLength() {
return currentArrayLength;
}
int main() {
// Required for a complete C program
return 0;
}Custom Objects (Structs)
Passing custom objects (like structs in C/C++) is more involved. These objects are collections of different data types (e.g., an integer, a float, and a string).
The strategy is to manually lay out the object's fields in WASM's linear memory. WASM then returns a pointer to the start of this memory block. JavaScript must then know the exact memory layout (offsets and types of each field) to correctly read the data.
C Struct to JS Example
This C example defines a Person struct and a function to create one. JavaScript would receive a pointer and then read values from specific offsets in the WASM memory buffer, knowing the struct's layout.
id(int) at offset 0age(int) at offset 4height(float) at offset 8
#include <emscripten/emscripten.h>
#include <stdlib.h> // For malloc
#include <string.h> // For strcpy
typedef struct {
int id;
int age;
float height; // assuming 4 bytes after age
char name[16]; // Fixed-size string buffer
} Person;
EMSCRIPTEN_KEEPALIVE
Person* createPerson(int id, int age, float height, const char* name) {
Person* p = (Person*)malloc(sizeof(Person));
p->id = id;
p->age = age;
p->height = height;
strncpy(p->name, name, sizeof(p->name) - 1);
p->name[sizeof(p->name) - 1] = '\0'; // Ensure null termination
return p;
}
int main() {
// Required for a complete C program
return 0;
}JS Allocates WASM Memory
What if JavaScript needs to pass complex data *into* WASM? For example, a large image array for WASM to process.
JavaScript can call WASM's exposed memory allocation functions (like Emscripten's _malloc) to reserve space within WASM's linear memory. Then, JavaScript writes the data into that allocated space and passes the pointer to a WASM function for processing.
Quick Check
You've learned how WASM handles complex data. Let's test your understanding!
Recap: Complex Data
Congratulations! You've learned the fundamental techniques for passing complex data between WebAssembly and JavaScript.
- WASM and JavaScript communicate via a shared linear memory.
- Complex data (strings, arrays, structs) are passed by their pointer (memory address) and often their length or size.
- JavaScript then reads the raw bytes from the shared memory, interpreting them based on the known data structure.
- For custom objects, understanding the exact memory layout is crucial.
These techniques allow for efficient, high-performance data exchange!
คำถามที่พบบ่อย
บทเรียน “การส่งโครงสร้างข้อมูลซับซ้อน” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การส่งโครงสร้างข้อมูลซับซ้อน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส WebAssembly (WASM) for High Performance Apps ให้อัปเกรดเป็น CoddyKit PRO คอร์ส WebAssembly (WASM) for High Performance Apps มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การส่งโครงสร้างข้อมูลซับซ้อน”
เรียนรู้กลยุทธ์ถ่ายโอนสตริง อาร์เรย์ และออบเจ็กต์แบบกำหนดเองระหว่าง WASM กับ JavaScript อย่างมีประสิทธิภาพ คุณปฏิบัติ WebAssembly (WASM) for High Performance Apps ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน WebAssembly (WASM) for High Performance Apps หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน WebAssembly (WASM) for High Performance Apps บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การส่งโครงสร้างข้อมูลซับซ้อน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน WebAssembly (WASM) for High Performance Apps นี้ได้ไหม
ได้ บทเรียน WebAssembly (WASM) for High Performance Apps ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การส่งโครงสร้างข้อมูลซับซ้อน
- แบบจำลองและการจัดการหน่วยความจำ WASM
- หน่วยความจำร่วมและอะตอมิก
- การขยายและจัดการหน่วยความจำเชิงเส้น