Wasm-bindgen 提供 WebAssembly(wasm)模块和 JavaScript 之间的高级别交互。 使用 wasm-bindgen,JavaScript 和 WebAssembly 之前不仅可以通过整形和浮点型进行通信,还可以使用字符串、JavaScript 对象和类进行。 wasm-bindgen 的高级功能包括: 导入 JavaScript 结构、函数和对象以在 WebAssembly 中调用。开发人员可以调用结构中的方法和访问属性。 基于 ECMAScript 模块,wasm-bindgen 专注于 Rust,但其目的也是最终用于 C 和 C ++ 等语言。 但 Mozilla 称 wasm-bindgen 目前还不够稳定,暂时还无法扩展到其它语言。 Rust 社区的一大目标是让 Rust 成为 Web 语言。
Rust web 前端库/框架在所有前端库/框架的位置 从评测结果来看,wasm-bindgen 性能和 vanillajs 处于同一水平,甚至有几项已经超越。 虽然说,目前 wasm-bindgen 还处于初级阶段,但其总需要通过 web-sys 和 js-sys 与 JavaScript 交互。 所以除非前端技术有什么超级大的变革,否则 wasm-bindgen 最好的性能估计也就是总体和原生 vanillajs 总体持平吧。 具体来说,wasm-bindgen、stdweb 是 wasm 模块与 JavaScript 之间的高层次交互库。 所以 wasm-bindgen、stdweb 肯定是各项评测性能都胜出的。
好消息是,有两个库可促进基于Rust的Wasm与JavaScript之间的通信:wasm-bindgen和stdweb。然而,坏消息是,这两个库互不兼容。 Wasm-bindgen比stdout更底层,它试图提供对JavaScript和Rust交互方式的完全控制。实际上,甚至有人在谈论使用wasm-bindgen重写stdweb,这将解决不兼容的问题。 因为wasm-bindgen是更轻量级的选项(该选项由Rust WebAssembly官方工作组正式开发),所以我们将重点讨论这个选项。 幸运的是,wasm-bindgen不仅仅只是一个库,它还具有为我们创建一种“胶水”JavaScript的能力。 首先,我们需要安装wasm-pack : $ cargo install wasm - pack 默认情况下,wasm-bindgen 生成 ES6模块。
我们将深入了解由wasm-bindgen生成的代码,以及它们如何共同协作来帮助我们进行开发。我们还将使用wabt来探索生成的wasm代码。 项目搭建 2.1 安装wasm-bindgen 我们可以通过cargo install --list来查看在$HOME/.cargo/bin位置安装过的Rust二进制文件。 我们可以将wasm-bindgen视为一种工具,它通过生成用于JavaScript和WebAssembly之间高效交互的「粘合代码」和绑定来帮助我们实现丝滑的交互体验。 这说明了wasm-bindgen在Rust WebAssembly开发中的重要性。 最后,让我们看看wasm-bindgen生成的其他文件。
wasm-bindgen wasm-bindgen 提供了 JS 和 Rust 类型之间的桥梁,它允许 JS 使用字符串调用 Rust API,或者使用 Rust 函数来捕获 JS 异常。 wasm-bindgen 的核心是促进 javascript 和 Rust 之间使用 wasm 进行通信。 然后,我们在 Cargo.toml 中引入 wasm-bindgen 这个包,另外我们还需要告诉编译器这个包是一个 cdylib: [package] name = "react-wasm" version = "1.0.0" edition = "2021" [lib] crate-type = ["cdylib"] [dependencies] wasm-bindgen = "0.2" 现在,你可以先尝试执行下 中添加一些常用的 npm 脚本: "build:wasm": "cargo build --target wasm32-unknown-unknown", "build:bindgen": "wasm-bindgen
wasm-bindgen:用于生成Rust与JavaScript之间的桥接代码。wasm-pack:简化了WebAssembly模块的打包和发布流程。3. Rust与WebAssembly的兼容性:掌握Rust如何通过工具链生成WebAssembly模块,以及如何利用wasm-bindgen暴露Rust逻辑到JavaScript。 使用wasm-bindgen将Rust逻辑以WebAssembly形式暴露给JavaScript。在Web前端页面中,通过JavaScript调用这些功能,实现用户与模块的交互。 依赖,它允许Rust与JavaScript进行交互:[dependencies]wasm-bindgen = "0.2"4. 我们将定义一个简单的加法函数和减法函数,并通过wasm-bindgen暴露给JavaScript。
本文将以一个“功能应用案例”为核心,从零开始,使用Rust、image库和wasm-bindgen生态,构建一个在浏览器中运行的高性能图像灰度化处理器。这不仅仅是一个“玩具”项目。 *`wasm-bindgen`会自动将其映射为一个Rust的`&[u8]`(字节切片)。*这是一个“借用”,在某些情况下是零拷贝的,效率极高。**@return:返回一个`Result`。 `JsValue`是一个“动态”类型,*`wasm-bindgen`会把它转换成一个JS`Error`对象。*这实现了健壮的、跨语言的错误处理。 wasm-bindgen生成的JS胶水代码。永远不应该手动修改它。wasm_image_processor.d.ts:TypeScript类型定义。 wasm-bindgen自动生成了类型,提供了“智能感知”体验。package.json:一个完整的package.json。
Rust / wasm数据 / 内存共享 wasm-bindgen是一个Rust库和CLI工具,它促进了wasm模块和JavaScript之间的高级交互。 wasm-bindgen 工具和库都是Rust和WebAssembly生态系统的一部分。 wasm-bindgen工具有点半填充性质,用于主机绑定等功能,以及用于增强JS和wasm编译代码(目前主要来自Rust)之间高级交互的部分功能。
crate has been compiled with wasm32-unknown-unknown, the WebAssembly can be executed within a browser. wasm-bindgen For example, to compile the hello-triangle example and generate bindings with wasm-bindgen: # Build with Generate bindings with wasm-bindgen-cli into a `generated` directory cargo install -f wasm-bindgen-cli && wasm-bindgen
cargo install trunk wasm-bindgen-cli 依赖项 crate 我们目前仅是 yew 开发环境的初始配置,所以需要的依赖项 crate 仅为 yew 和 wasm-bindgen cargo add yew wasm-bindgen 此时,frontend-yew 项目中的 Cargo.toml 文件内容如下: [package] name = "frontend-yew" version keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] wasm-bindgen
Winit 0.20 與 web 支援 現在支援 stdweb 跟 wasm-bindgen 範例在這 https://github.com/rust-windowing/winit Read more
一个「专注于前端开发技术/Rust及AI应用知识分享」的Coder ❝此篇文章所涉及到的技术有 Rust wasm-bindgen/js-sys/web-sys Web Worker WebAssembly 在 Rust 中,#[wasm_bindgen] 是一个属性宏(attribute macro),用于与 wasm-bindgen 工具一起工作。 使用wasm-bindgen: cargo install wasm-bindgen-cli cargo build --target wasm32-unknown-unknown wasm-bindgen /out wasm-bindgen 是一个用于在Rust和JavaScript之间生成互操作性代码的工具。 : https://crates.io/crates/wasm-bindgen [13] web-sys: https://rustwasm.github.io/wasm-bindgen/web-sys
, name)); } 接着在 Cargo.toml 文件中添加 wasm-bindgen 依赖,wasm-bindgen 来提供 JavaScript 和 Rust 类型之间的桥梁,允许 JavaScript Cargo.toml [package] name = "hello-wasm" version = "0.1.0" [lib] crate-type = ["cdylib"] [dependencies] wasm-bindgen
Repo 推荐阅读——面向大众的Rust和WebAssembly教程 Introduction wasm-bindgen Sharing Classes
Trunk 支持所有基于 wasm-bindgen 的框架,包括但不仅限于 Yew 和 Seed。
学习Rust有一段时间了,之前有做过一些JavaScript和wasm-bindgen在浏览器上的性能对比,最近有一点闲暇时间,就用Rust独立做了一个项目。
app.rs └── lib.rs 这里面最重要的就是 Cargo.toml 了,其中最关键的依赖如下: [lib] crate-type = ["cdylib"] [dependencies] wasm-bindgen = "0.2" yew = "0.10.0" cdylib 是用于配置构建 C interoperability(C FFI),wasm-bindgen 依赖是用于 WASM 模块和JavaScript
Rust 生态提供了完善的 wasm 相关工具链,包括 wasm-bindgen、wasm-pack、wasm-opt、wasm-gc、wasm-snip、wasm-bindgen-test 等。
一个专注于前端开发技术/Rust及AI应用知识分享的Coder ❝此篇文章所涉及到的技术有 WebAssembly Rust wasm-bindgen 线程池 Vite+React/Vue(下面的内容 如果大家写过Rust+WebAssembly的程序的话,想必肯定听说过大名鼎鼎的 - wasm-bindgen[5]。 然后,使用cargo install 安装对应的wasm-bindgen/js-sys/web-sys等包。 : https://rustwasm.github.io/wasm-bindgen/introduction.html [6] web-sys: https://rustwasm.github.io/ wasm-bindgen/examples/dom.html [7] Parallel Raytracing: https://rustwasm.github.io/wasm-bindgen/examples
本文示例环境为:mac环境(12.3.1 Monterey) + nodejs(v 18.5.0) + react (v 18.2) + rustc(v 1.62.0) + cargo(1.57.0) + wasm-bindgen 马上就来了: 参考下图,修改Cargo.toml文件,核心就是添加wasm-bindgen依赖,并告诉rust编译器,要生成一个符合C语言规范的动态链接库(C Dynamic Lib) 然后微调lib.rs