การสร้างและเรียกใช้โมดูล WASI
คอมไพล์โค้ด Rust ให้ทำงานกับ WASI และเรียกใช้โมดูล WebAssembly ในรันไทม์ WASI ต่าง ๆ
การสร้างและเรียกใช้โมดูล WASI เป็นบทเรียน WebAssembly (WASM) for High Performance Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน WebAssembly (WASM) for High Performance Apps และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส WebAssembly (WASM) for High Performance Apps มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Building WASI Modules
Welcome back! In the previous lesson, we learned what WASI is and its goals. Now, let's get practical.
This lesson focuses on the hands-on process of compiling Rust code to target WASI and then executing your WebAssembly modules in various WASI runtimes.
Rust for WASI Development
Rust is a prime language for WASI development due to its performance, safety features, and strong support for WebAssembly. To get started, you'll need:
- Rust Toolchain: Install it via
rustupif you haven't already. - WASI Target: Add the specific WASI compilation target.
rustup install stable
rustup target add wasm32-wasiSetting Up a WASI Project
We'll use Cargo, Rust's official package manager and build system, to create a new project. This sets up the basic directory structure.
cargo new my-wasi-app
cd my-wasi-appOur First WASI Program
Let's write a simple Rust program. For WASI, basic I/O operations like printing to the console are handled through the WASI API. Open src/main.rs and add this code:
fn main() {
println!("Hello from WASI!");
}WASI's Role in I/O
When you use println! in a standard Rust program, it interacts directly with the operating system. For WASI, things are a bit different.
- The WASM module is sandboxed.
println!calls are routed through the WASI API.- WASI provides a standardized interface for basic system functions like console output, file access, and environment variables.
Compiling to WASI
Now, let's compile our Rust code into a WebAssembly module specifically for the WASI target. This command tells Cargo to use the wasm32-wasi target we added earlier.
cargo build --target wasm32-wasiThe WASM Module Output
After successful compilation, you'll find your WASI WebAssembly module. It's a standalone binary file, ready for execution!
Look in the target/wasm32-wasi/debug/ directory for a file named my-wasi-app.wasm. This is your compiled WASI module.
Running with Wasmtime
To execute a WASI module, you need a WASI runtime. Wasmtime is a popular, high-performance runtime designed for this purpose.
- Install Wasmtime: Follow instructions on
wasmtime.devfor your OS. - Run your module: Execute the
.wasmfile using thewasmtimecommand.
# Install Wasmtime (example for Linux/macOS):
curl https://wasmtime.dev/install.sh -sSf | bash
# Run your compiled module:
wasmtime target/wasm32-wasi/debug/my-wasi-app.wasmWASI File System Access
WASI allows controlled access to the host's file system. Let's modify our Rust program to read from a file. Remember, you'll need to explicitly grant Wasmtime permission to access files.
use std::fs;
use std::io::{self, Read};
fn main() -> io::Result<()> {
let path = "input.txt";
let mut file = fs::File::open(path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
println!("File content: {}", contents);
Ok(())
}Running with File Access
After updating src/main.rs, compile again. Then, you'll need to create an input.txt file in your project root with some content. Finally, run it with Wasmtime, granting directory access:
# Create input.txt with content (e.g., "Hello WASI File!")
# Compile again:
cargo build --target wasm32-wasi
# Run, mapping current dir to WASI root:
wasmtime --mapdir /::. target/wasm32-wasi/debug/my-wasi-app.wasmQuick Check
Let's test your understanding of building and running WASI modules.
Recap: Building & Running WASI
In this lesson, we explored the practical steps of building and running WebAssembly modules with WASI.
- We set up our Rust environment for the
wasm32-wasitarget. - We wrote Rust code that leverages WASI for system interactions.
- We compiled our code into a portable
.wasmmodule. - We learned how to execute these modules using a WASI runtime like Wasmtime, including granting necessary permissions for host resource access.
Next, we'll delve deeper into WASI's capabilities and its implications for secure and portable applications.
คำถามที่พบบ่อย
บทเรียน “การสร้างและเรียกใช้โมดูล WASI” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การสร้างและเรียกใช้โมดูล WASI” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส WebAssembly (WASM) for High Performance Apps ให้อัปเกรดเป็น CoddyKit PRO คอร์ส WebAssembly (WASM) for High Performance Apps มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การสร้างและเรียกใช้โมดูล WASI”
คอมไพล์โค้ด Rust ให้ทำงานกับ WASI และเรียกใช้โมดูล WebAssembly ในรันไทม์ WASI ต่าง ๆ คุณปฏิบัติ WebAssembly (WASM) for High Performance Apps ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน WebAssembly (WASM) for High Performance Apps หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน WebAssembly (WASM) for High Performance Apps บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การสร้างและเรียกใช้โมดูล WASI” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน WebAssembly (WASM) for High Performance Apps นี้ได้ไหม
ได้ บทเรียน WebAssembly (WASM) for High Performance Apps ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- บทนำสู่ WASI และเป้าหมาย
- การสร้างและเรียกใช้โมดูล WASI
- ความสามารถและอนาคตของ WASI
- ทำงานกับระบบไฟล์ WASI