0Pricing
WebAssembly (WASM) for High Performance Apps · บทเรียน

การแลกเปลี่ยนข้อมูลพื้นฐาน ชนิดข้อมูลดั้งเดิม

ทำความเข้าใจวิธีส่งชนิดข้อมูลอย่างง่าย เช่น จำนวนเต็มและจำนวนทศนิยม ระหว่างโค้ด JavaScript กับ WASM

การแลกเปลี่ยนข้อมูลพื้นฐาน ชนิดข้อมูลดั้งเดิม เป็นบทเรียน WebAssembly (WASM) for High Performance Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน WebAssembly (WASM) for High Performance Apps และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส WebAssembly (WASM) for High Performance Apps มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Exchanging Basic Data Types

Welcome! In this lesson, we'll learn how to pass simple data types, known as primitives, between your JavaScript code and your WebAssembly (WASM) module compiled from C/C++.

Primitives include numbers like integers (int) and floating-point numbers (float, double). This is fundamental for enabling communication and interaction between your web application and high-performance WASM logic.

JS to WASM: Integer Input

Passing an integer from JavaScript to a C/C++ WASM function is quite direct. You define your C/C++ function with integer parameters, and Emscripten handles the necessary type mapping.

For example, a C function to add two integers would look like this:

int add_integers(int a, int b) {
  // Function body
  return a + b;
}

Example: WASM Integer Function

Let's look at a complete C program that defines a function to add two integers and exposes it to JavaScript using EMSCRIPTEN_KEEPALIVE.

Try running this code and see how it compiles:

#include <stdio.h>
#include <emscripten/emscripten.h>

#ifdef __cplusplus
extern "C" {
#endif

EMSCRIPTEN_KEEPALIVE
int add_integers(int a, int b) {
    printf("WASM: Adding %d and %d\n", a, b);
    return a + b;
}

#ifdef __cplusplus
}
#endif

int main() {
    printf("WASM module for integers loaded.\n");
    return 0;
}

Calling Integer Function from JS

After compiling the C code above, Emscripten generates a JavaScript glue code file (e.g., your_module.js) that provides access to your C functions.

You can then call the exported C function from your JavaScript code like this:

// Assuming 'Module' is the Emscripten-generated object
Module.onRuntimeInitialized = () => {
  // The C function is prefixed with an underscore by default
  const result = Module._add_integers(10, 25);
  console.log(`Sum from WASM: ${result}`); // Output: Sum from WASM: 35
};

WASM to JS: Integer Output

Returning an integer from a WASM function back to JavaScript is just as straightforward. C/C++ functions naturally return primitive types, and Emscripten ensures these return values are directly accessible in JavaScript.

Consider a C function that simply returns a fixed integer:

int get_magic_number() {
  return 42;
}

Example: WASM Returns Integer

Here's a C example that returns a specific integer. Notice the use of EMSCRIPTEN_KEEPALIVE to make it available to JavaScript.

Run this to see the simple C structure:

#include <stdio.h>
#include <emscripten/emscripten.h>

#ifdef __cplusplus
extern "C" {
#endif

EMSCRIPTEN_KEEPALIVE
int get_magic_number() {
    printf("WASM: Returning magic number.\n");
    return 42;
}

#ifdef __cplusplus
}
#endif

int main() {
    printf("WASM module for returning integers loaded.\n");
    return 0;
}

Exchanging Floating-Point Numbers

Passing floating-point numbers (float and double) between JavaScript and WASM works very similarly to integers. You define your C/C++ functions with float or double parameters and return types.

JavaScript's Number type is generally a 64-bit float (double-precision), which Emscripten automatically converts when passing to C/C++ float (32-bit) or double (64-bit) types.

Example: WASM Float Operations

Let's create a C function that takes two floats and returns their product, and another for doubles. This demonstrates both input and output of floating-point numbers.

Run this code to see how floats are handled:

#include <stdio.h>
#include <emscripten/emscripten.h>

#ifdef __cplusplus
extern "C" {
#endif

EMSCRIPTEN_KEEPALIVE
float multiply_floats(float a, float b) {
    printf("WASM: Multiplying floats %.2f * %.2f\n", a, b);
    return a * b;
}

EMSCRIPTEN_KEEPALIVE
double divide_doubles(double x, double y) {
    printf("WASM: Dividing doubles %.2f / %.2f\n", x, y);
    if (y == 0.0) return 0.0; // Avoid division by zero
    return x / y;
}

#ifdef __cplusplus
}
#endif

int main() {
    printf("WASM module for floats/doubles loaded.\n");
    return 0;
}

Type Matching is Key

While Emscripten handles much of the type conversion automatically, it's good practice to be aware of the underlying types:

  • JavaScript Number: This is typically a 64-bit floating-point number.
  • C/C++ int: Often 32-bit integer.
  • C/C++ float: 32-bit floating-point number.
  • C/C++ double: 64-bit floating-point number.

When passing a JS Number to a C float, there might be a slight loss of precision, as the 64-bit JS number is truncated to a 32-bit float.

Quick Check: Data Flow

You've learned how simple data types are exchanged. Let's test your understanding.

Recap: Primitive Exchange

Great job! You've mastered the basics of primitive data exchange between JavaScript and WebAssembly modules compiled from C/C++.

  • Simple data types like int, float, and double are straightforward to pass.
  • C/C++ functions can take them as parameters and return them as results.
  • Emscripten acts as the bridge, automatically converting between JavaScript's Number type and C/C++'s primitive types.
  • Be mindful of potential precision differences when converting between 64-bit (JS Number, C double) and 32-bit (C float, C int) types.

This fundamental understanding paves the way for exchanging more complex data structures later on!

คำถามที่พบบ่อย

บทเรียน “การแลกเปลี่ยนข้อมูลพื้นฐาน ชนิดข้อมูลดั้งเดิม” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การแลกเปลี่ยนข้อมูลพื้นฐาน ชนิดข้อมูลดั้งเดิม” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส WebAssembly (WASM) for High Performance Apps ให้อัปเกรดเป็น CoddyKit PRO คอร์ส WebAssembly (WASM) for High Performance Apps มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การแลกเปลี่ยนข้อมูลพื้นฐาน ชนิดข้อมูลดั้งเดิม”

ทำความเข้าใจวิธีส่งชนิดข้อมูลอย่างง่าย เช่น จำนวนเต็มและจำนวนทศนิยม ระหว่างโค้ด JavaScript กับ WASM คุณปฏิบัติ WebAssembly (WASM) for High Performance Apps ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน WebAssembly (WASM) for High Performance Apps หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน WebAssembly (WASM) for High Performance Apps บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การแลกเปลี่ยนข้อมูลพื้นฐาน ชนิดข้อมูลดั้งเดิม” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน WebAssembly (WASM) for High Performance Apps นี้ได้ไหม

ได้ บทเรียน WebAssembly (WASM) for High Performance Apps ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. คอมไพล์ C/C++ เป็น WASM ด้วย Emscripten
  2. การโหลดและเรียกใช้ WASM ใน JavaScript
  3. การแลกเปลี่ยนข้อมูลพื้นฐาน ชนิดข้อมูลดั้งเดิม
  4. เรียกใช้ฟังก์ชัน JavaScript จาก C/C++
← กลับไปที่ WebAssembly (WASM) for High Performance Apps