跳到正文
Skillpedia
通用编程18k · #10720

wasm-expert

它能做什么

这个技能指导用 Rust 和 C 等语言构建 WebAssembly 模块,包括用于服务器端的 WASI 和浏览器集成。适合希望在浏览器或服务器上运行可移植、快速代码的开发者。

适用场景

  • ✓当你想在网页中以接近原生的速度运行 Rust 或 C 代码时。
  • ✓当你需要一个能通过 WASI 在服务器上运行的可移植模块时。
  • ✓当 WebAssembly 文件过大,必须缩小以便浏览器加载时。

技能链接

https://github.com/RightNow-AI/openfang/blob/main/crates/openfang-skills/bundled/wasm-expert/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/RightNow-AI/openfang/blob/main/crates/openfang-skills/bundled/wasm-expert/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请把我的 Rust 图片缩放函数编译为 WebAssembly 以便在浏览器中运行,并让文件尽可能小。”

每周 5 个新技能,1 分钟读完。

工作原理

该技能涵盖使用 wasm-pack 或用于 WASI 的 Cargo 进行构建,只授予模块所需的访问权限,并用优化工具减小二进制文件的体积。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

WebAssembly Expert

A systems programmer and runtime specialist with deep expertise in WebAssembly compilation, WASI system interfaces, the component model, and browser integration. This skill provides guidance for compiling Rust, C, and other languages to WebAssembly, building portable server-side modules with WASI, designing composable components with WIT interfaces, and integrating Wasm modules into web applications with optimal performance.

Key Principles

  • WebAssembly provides a portable, sandboxed execution environment; leverage its security model by granting only the capabilities a module needs through explicit imports
  • Target wasm32-wasi for server-side and CLI applications that need file system, network, or clock access through the standardized WASI interface
  • Use the Component Model and WIT (WebAssembly Interface Types) for language-agnostic module composition; components communicate through typed interfaces, not raw memory
  • Optimize Wasm binary size aggressively for browser delivery; every kilobyte matters for initial load time, so strip debug info, use wasm-opt, and enable LTO
  • Understand linear memory: Wasm modules operate on a flat byte array that grows but never shrinks; design data structures and allocation patterns accordingly

Techniques

  • Compile Rust to Wasm with wasm-pack for browser targets (wasm-pack build --target web) or cargo build --target wasm32-wasi for server-side WASI modules
  • Use wasm-bindgen to expose Rust functions to JavaScript and import JS APIs into Rust; annotate public functions with #[wasm_bindgen] and use JsValue for dynamic interop
  • Define component interfaces in WIT files specifying exports (functions the component provides) and imports (functions the component requires from the host)
  • Compose multiple Wasm components using wasm-tools compose, linking one component's imports to another's exports without source-level dependencies
  • Optimize binaries with wasm-opt -Oz for size or -O3 for speed; use wasm-tools strip to remove custom sections and debug information for production builds
  • Instantiate modules in the browser with WebAssembly.instantiateStreaming(fetch("module.wasm"), importObject) for the fastest possible startup
  • Enable SIMD (Single Instruction, Multiple Data) for compute-intensive workloads by compiling with target features enabled and using explicit SIMD intrinsics or auto-vectorization

Common Patterns

  • Plugin Architecture: Host application loads untrusted Wasm plugins with restricted WASI capabilities; plugins export a known interface (defined in WIT) and cannot access resources beyond what the host provides
  • Polyglot Composition: Compile components from different languages (Rust, Go, Python) to Wasm components with WIT interfaces, then compose them into a single application using wasm-tools
  • Streaming Compilation: Use WebAssembly.compileStreaming to compile the module while it downloads; pair with instantiate for near-zero wait time after the network transfer completes
  • Memory-Mapped I/O: For large data processing in Wasm, share a linear memory region between the host and the module, passing pointers and lengths instead of copying data across the boundary

Pitfalls to Avoid

  • Do not assume all WASI APIs are available in every runtime; WASI Preview 2 is still being adopted, and different runtimes (Wasmtime, Wasmer, WasmEdge) support different subsets
  • Do not allocate memory freely without a strategy; Wasm linear memory grows in 64KB page increments and never releases pages back to the OS, so fragmentation accumulates over time
  • Do not pass complex data structures across the Wasm boundary by serializing to JSON; use shared linear memory with well-defined layouts or the component model's typed interface for efficiency
  • Do not skip testing on the target runtime; behavior differences exist between browser engines (V8, SpiderMonkey, JavaScriptCore) and server-side runtimes, especially for threading and SIMD
常见问题

什么是 WASI?

它是一组标准的系统接口,使 WebAssembly 模块能够以受控的方式使用文件、网络或时钟。

为什么要把文件做小?

更小的文件在浏览器中加载更快,该技能建议去除调试信息并优化二进制文件。

什么是线性内存?

它是 WebAssembly 模块使用的扁平内存块,只能增长而不能缩小,因此数据布局应为此做好规划。

详情

作者的原始描述

WebAssembly expert for WASI, component model, Rust/C compilation, and browser integration

repo: RightNow-AI/openfang

热度

GitHub 星标
18k
排名
#10720
许可证
Apache-2.0
更新时间
2026/7/2

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能