[WASM] Set up wasm-bindgen for easy Rust/JavaScript Interoperability
Interoperability between JavaScript and Rust is limited to numerics and accessing memory directly. Since this can be exhausting and overwhelming to do manually the Rust/Wasm team has created the wasm-bindgen project to facilitate high-level interactions between Rust and JavaScript.
WebAssembly programs operate on a limited set of value types. Due to this, the functions bridging between JavaScript and Rust only allow for primitive numeric types, like integer or float.
So if you pass a string between Javascript and WASM, it convert to a number.
This is not what we wanted to achieve here. Fortunately, we can work around this by directly reading or writing to WebAssembly's memory using JavaScript. Each WASM module has a linear memory, which is initialized during instantiation.
While sometimes accessing memory directly can be useful, in most cases, it's quite cumbersome. That's why the generic bindgen-style framework, wasm-bindgen, was created. The framework makes it possible to write idiomatic Rust function signatures that map to idiomatic JavaScript functions automatically.
To get started, we add wasm-bindgen as a dependency to our Cargo configuration.
# Cargo.toml [package]
name = "utils"
version = "0.1.0"
authors = ["zhentian-wan <answer881215@gmail.com>"]
edition = "" [dependencies]
wasm-bindgen="0.2" [lib]
crate-type = ["cdylib"]
lib.rs:
#![feature(use_extern_macros)] extern crate wasm_bindgen; use wasm_bindgen::prelude::*; #[wasm_bindgen(module = "../domUtils")]
extern {
fn appendStringToBody(s: &str);
} #[wasm_bindgen]
pub extern fn run() {
appendStringToBody("Hello World");
}
We compile our Rust library using wasm-pack build
wasm-pack build
This way, we can pass types, like strings, into our Rust code without manually having to take care of the conversion.
domUtils.js:
export const appendStringtoBody = (value) => {
const text = document.createTextNode(value);
document.body.appendChild(text);
}
When using wasm-bindgen, we then can declare that the module should be imported.
Run:
wasm-pack build npx webpack-dev-server
[WASM] Set up wasm-bindgen for easy Rust/JavaScript Interoperability的更多相关文章
- 在kubernetes上运行WASM负载
在kubernetes上运行WASM负载 WASM一般用在前端业务中,但目前有扩展到后端服务的趋势.本文使用Krustlet 将WASM服务部署到kubernetes. 简介 Krustlet 是一个 ...
- TVM编译机器学习到 WASM 和 WebGPU
TVM编译机器学习到 WASM 和 WebGPU TLDR TVM 深度学习编译器对 WASM 和 WebGPU 的支持.实验表明,TVM 的 WebGPU 后端在将模型部署到 Web 时可以接近原生 ...
- 试试将.NET7编译为WASM并在Docker上运行
之前有听到说Docker支持Wasmtime了,刚好.NET7也支持WASM,就带大家来了解一下这个东西,顺便试试它怎么样. 因为WASM(WebAssembly) 一开始是一个给浏览器的技术,比起J ...
- [WASM] Call a JavaScript Function from WebAssembly
Using WASM Fiddle, we show how to write a simple number logger function that calls a consoleLog func ...
- https://www.jianshu.com/writer#/notebooks/164311/notes/88906048/preview
什么是 webassembly 在 2019 年 12 月之前,如果你要编写一个web页面,那一定离不开 html.css.js 这三个好兄弟.在 2019 年 12 月之后 W3C 宣布 webas ...
- JavaScript与WebAssembly进行比较
本文由云+社区发表 作者:QQ音乐前端团队 在识别和描述核心元素的过程中,我们分享了构建SessionStack时使用的一些经验法则,这是一个轻量级但健壮且高性能的JavaScript应用程序,以帮助 ...
- webassembly
为什么需要 WebAssembly 自从 JavaScript 诞生起到现在已经变成最流行的编程语言,这背后正是 Web 的发展所推动的.Web 应用变得更多更复杂,但这也渐渐暴露出了 JavaScr ...
- ewasm项目初探
为了改进EVM1.0,以太坊的新一代虚拟机项目ewasm (github.com/ewasm)将支持WebAssembly(wasm),wasm在性能,扩展性,开发工具,社区都更有优势.除以太坊外,一 ...
- 【webAssembly系列】webAssembly初探究竟
一.前言 自从JavaScript诞生开始,到现在开始变成流行的编程语言,背后的是web发展所推动的.web应用的变得更多更复杂,但是渐渐暴露出JavaScript的问题: (1)语法太灵活导致开发大 ...
随机推荐
- 五分钟入门 Dingo API
基于 https://laravel-china.org/doc... 文档更简洁的描述Dingo,直戳重点,注重实践 Django-Book 概述 Dingo API帮助您轻松快速地构建自己的API ...
- gnu make规则记录
1. $(shell CMD) 名称: 执行 shell 命令函数 功能: 在新的 shell 中执行 CMD 命令 返回值: CMD 在 shell 中执行的结果 例如:PLATFORM=$(she ...
- idea xml 一键生成 javabean
操作步骤 1.复制的xml文件到工程的一个文件下 2.选中文件tools -> XML ACTIONS -> Generate schema from instance Document ...
- 开始 python programming第三版案例分析
最近研究python,打算将python programming第三版案例分析下 但是全书1600多页 比较费时 而且 介绍太多 感觉没有必要! python programming 堪称经典之作 第 ...
- SQLserver查看某个视图的创建语句
1.已经创建好的视图,查看视图创建时的源码 sp_helptext '视图名' 如下: sp_helptext 'V_user' 方式2: sa账户登录SQLserver,选择视图---右键---- ...
- mysql 处理数据库中的重复行
1.对于主键和唯一索引,可以用IGNORE关键字,遇到重复记录会直接忽略插入记录,返回0.insert ignore into table_name ('id','name') values (1,' ...
- NYOJ 7 街区最短路径问题
街区最短路径问题 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 一个街区有很多住户,街区的街道只能为东西.南北两种方向. 住户只可以沿着街道行走. 各个街道之间的间 ...
- .netCore例子
.netCore例子 文章:https://github.com/dotnet-architecture/eShopOnContainers
- Git升级迁移
1 原有机器进行升级和备份: https://about.gitlab.com/update/#centos-6 1. Make a backup (Optional) If you would li ...
- 有关php启动时候报错信息
1 : An another FPM instance seems to already listen on /tmp/php-cgi.sock有关/tem/php-cgi.sock问题查看进程可以或 ...