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的更多相关文章

  1. 在kubernetes上运行WASM负载

    在kubernetes上运行WASM负载 WASM一般用在前端业务中,但目前有扩展到后端服务的趋势.本文使用Krustlet 将WASM服务部署到kubernetes. 简介 Krustlet 是一个 ...

  2. TVM编译机器学习到 WASM 和 WebGPU

    TVM编译机器学习到 WASM 和 WebGPU TLDR TVM 深度学习编译器对 WASM 和 WebGPU 的支持.实验表明,TVM 的 WebGPU 后端在将模型部署到 Web 时可以接近原生 ...

  3. 试试将.NET7编译为WASM并在Docker上运行

    之前有听到说Docker支持Wasmtime了,刚好.NET7也支持WASM,就带大家来了解一下这个东西,顺便试试它怎么样. 因为WASM(WebAssembly) 一开始是一个给浏览器的技术,比起J ...

  4. [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 ...

  5. https://www.jianshu.com/writer#/notebooks/164311/notes/88906048/preview

    什么是 webassembly 在 2019 年 12 月之前,如果你要编写一个web页面,那一定离不开 html.css.js 这三个好兄弟.在 2019 年 12 月之后 W3C 宣布 webas ...

  6. JavaScript与WebAssembly进行比较

    本文由云+社区发表 作者:QQ音乐前端团队 在识别和描述核心元素的过程中,我们分享了构建SessionStack时使用的一些经验法则,这是一个轻量级但健壮且高性能的JavaScript应用程序,以帮助 ...

  7. webassembly

    为什么需要 WebAssembly 自从 JavaScript 诞生起到现在已经变成最流行的编程语言,这背后正是 Web 的发展所推动的.Web 应用变得更多更复杂,但这也渐渐暴露出了 JavaScr ...

  8. ewasm项目初探

    为了改进EVM1.0,以太坊的新一代虚拟机项目ewasm (github.com/ewasm)将支持WebAssembly(wasm),wasm在性能,扩展性,开发工具,社区都更有优势.除以太坊外,一 ...

  9. 【webAssembly系列】webAssembly初探究竟

    一.前言 自从JavaScript诞生开始,到现在开始变成流行的编程语言,背后的是web发展所推动的.web应用的变得更多更复杂,但是渐渐暴露出JavaScript的问题: (1)语法太灵活导致开发大 ...

随机推荐

  1. TB平台搭建之二

    主要想记录关于debug问题: 一般我会1.定位问题所在位置比如使能信号错误.地址读写错误.数据流pipeline错误.... 2.首先看问题的源头(对应信号)是否还正确,比如出现XX要查看她的第一级 ...

  2. if else elif 用法和区别

    1.If语句:“如果条件为真,执行子句中的代码."始终包含以下部分: if关键字: 条件(即求值为True或False的表达式): 冒号: 在下一行开始,缩进的代码块(称为if子句) 例如: ...

  3. Educational Codeforces Round 32:E. Maximum Subsequence(Meet-in-the-middle)

    题目链接:E. Maximum Subsequence 用了一个Meet-in-the-middle的技巧,还是第一次用到这个技巧,其实这个技巧和二分很像,主要是在dfs中,如果数量减小一半可以节约很 ...

  4. 算法竞赛中c++一些需要注意的错误

    1. 关于精度: 取整 除法取整: (除数为正)被除数为正时系统除法为向下取整,被除数为负时系统除法为向上取整. 向上取整(被除数非负,除数为正): 一般写法(有bug): int cal(int x ...

  5. 笛卡尔&小雷:科学发展有规律,研究科学有方法

    一直在总结自己的学习和研究方法,最近在读吴军写的<文明之光> ,感觉这篇介绍笛卡尔的内容非常有价值,特此整理.最近开始在密谋自己的理论体系,低调实施中...  笛卡尔按照感知的方式,把人的 ...

  6. 【LeetCode】Reverse Nodes in k-Group(k个一组翻转链表)

    这是LeetCode里的第25道题. 题目要求: 给出一个链表,每 k 个节点一组进行翻转,并返回翻转后的链表. k 是一个正整数,它的值小于或等于链表的长度.如果节点总数不是 k 的整数倍,那么将最 ...

  7. zoj 2830 Champion of the Swordsmanship

    Champion of the Swordsmanship Time Limit: 2 Seconds      Memory Limit: 65536 KB In Zhejiang Universi ...

  8. LCA+主席树 (求树上路径点权第k大)

      SPOJ 10628. Count on a tree (树上第k大,LCA+主席树) 10628. Count on a tree Problem code: COT You are given ...

  9. 划分树C++版百度百科模板

    此代码同hdu2665改编#include <iostream> #include <cstdio> #include<cstring> #include < ...

  10. 九度oj 题目1016:火星A+B

    题目描述:     读入两个不超过25位的火星正整数A和B,计算A+B.需要注意的是:在火星上,整数不是单一进制的,第n位的进制就是第n个素数.例如:地球上的10进制数2,在火星上记为“1,0”,因为 ...