Upgrade to Pro — share decks privately, control downloads, hide ads and more …

WebAssembly 入門:在 Browser 上跑 C Code 是怎麼回事@Hacker...

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →
YUKAI
May 30, 2025

WebAssembly 入門:在 Browser 上跑 C Code 是怎麼回事@HackerSir StudyGroup

WASM(WebAssembly)是一種可攜式、跨平台的二進位格式,主要由 C、C++、Rust 等語言編譯而成,能在瀏覽器或伺服器中高效、安全地執行。其應用包括 AI 推論封裝、圖形渲染、影音處理、線上遊戲、沙箱環境及虛擬機模擬等。開發工具如 Emscripten 可將 C/C++ 編譯成 .wasm,而 WABT 則提供 .wasm 與 .wat 的轉換工具。WASI(WebAssembly System Interface)允許 WASM 模組與作業系統互動,使其更適合 CLI 或伺服器環境。常見執行環境有 Wasmtime、WasmEdge、Subtle 等,也能與 Docker 結合實現輕量化部署。

YUKAI

May 30, 2025

Transcript

27 slides
  1. WASM 想解決的問題 • 用 Wasm 封裝 AI 推論應用程式碼 ◦ 將數

    GB 的 container image 轉為數 MB 的 binary file ◦ 降低 Cold start 速度 ◦ 實現 JIT 的推論回應 • Example:LIamaEdge
  2. WASM Runtime • Wasmtime:一個高效能的 WASM Runtime,通常使用於 browser 和 server •

    WASMEdge:一個輕量化的 WASM Runtime,為嵌入式裝置、 IoT 應用設計 • Subtle:由 Rust 所寫,強調安全性的 WASM Runtime • Emscripten:可以將 C 和 C++ 程式碼轉換為 WASM 的編譯器
  3. Emscripten • 基於 LLVM / Clang 的編譯器 • 早期是將 C/C++

    Code 轉成 asm.js • 由 Mozilla 開發 • 其他類似 Emscripten 的例子 ◦ Google Native Client(Nacl)
  4. Emscripten – emcc emcc [input file] -Os -sWASM=1 -sSIDE_MODULE=1 –o

    [ouput file] 將 C/C++ Code 轉成 wasm -Os:程式碼體積優化 -sWASM=1:預設轉換為 WASM -sSIDE_MODULE=1:生成為 Side Module,無 runtime
  5. wabt • The WebAssembly Binary Toolkit • wasm2wast 可以將 binary

    format 轉成 text format wasm2wast [wasm file] -o [wst file]
  6. WebAssembly API 其他類別 • WebAssembly.Memory:配置 ArrayBuffer 或 SharedArrayBuffer • WebAssembly.Table:JS

    用來控制 WASM 的 Function • WebAssembly.Global:此物件可用來被多個 module 和 JS 使用 • WebAssembly Error ◦ WebAssembly.CompileError ◦ WebAssembly.LinkError ◦ WebAssembly.RuntimeError
  7. More WASM - WASI • 協助與作業系統進行交互,如讀寫檔案、處理網路連線等 • 簡單例子:Node.js 環境處理加密、或著操作其他語言工具 •

    相關資料 ◦ Standardizing WASI: A system interface to run WebAssembly outside the web ◦ 以 Node.js 角度出發實作 WASI
  8. 資料來源 • WebVM 的 BLOG • WebAssembly:無需容器的 Docker • Building,

    Deploying and Observing WASM Apps • 完整使用 WebAssembly 使用方式

More Decks by YUKAI

Other Decks in Research