wasm-bindgen is a rust crate for facilitating high-level interactions between WebAssembly (Wasm) modules and JavaScript.
Questions tagged [wasm-bindgen]
254 questions
7
votes
0 answers
Why is passing DOM objects as `exnternref`s *slower* than passing through the JS value table?
I made a benchmark to measure how fast it is to call DOM APIs by passing DOM objects as exnternrefs to a Wasm function. This is the function to measure (written in Rust and compiled by rustc 1.55.0):
#[wasm_bindgen]
pub fn append_and_remove(elem:…

YAMAMOTO Yuji
- 1,364
- 9
- 17
7
votes
1 answer
Rust wasm-bindgen struct with string
I'm trying to export the following struct:
#[wasm_bindgen]
#[derive(Eq, PartialEq, Debug, Clone)]
pub enum TokenType {
KeywordLiteral,
NumberLiteral,
Operator,
Separator,
StringLiteral,
}
#[wasm_bindgen]
#[derive(Eq, PartialEq,…

Elias
- 3,592
- 2
- 19
- 42
7
votes
1 answer
Why do the values of Rust WASM pointer and JS pointer differ?
Let's say I have the following definitions in the Rust code:
#[wasm_bindgen]
pub struct RustType {
foo: usize
}
#[wasm_bindgen]
impl RustType {
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
Self { foo: 100 }
…

OlegTheCat
- 4,443
- 16
- 24
7
votes
1 answer
Can C++ and Rust programs compiled to wasm interoperate somehow?
Let's say I have one program written in Rust and another in C++. Given that they both are compiled to Wasm, can I somehow call a function in one program from the other?

Nurbol Alpysbayev
- 19,522
- 3
- 54
- 89
6
votes
0 answers
Using a Yew Callback as a wasm_bindgen Closure
This question is written for Yew v0.19
Asynchronous foreign JavaScript functions can be used in Rust through Closures, as the function to pass-in:
#[wasm_bindgen]
extern "C" {
fn setInterval(closure: &Closure, time: u32) ->…

Athan Clark
- 3,886
- 2
- 21
- 39
6
votes
0 answers
How to import JavaScript functions with wasm-bindgen with --target no-modules?
I am trying to work out how to call a JavaScript function using Rust and wasm-bindgen. Due to lack of browser support, I cannot use wasm-bindgen with ES6 modules together with a Web Worker.
As far as I can tell, declaring that a JavaScript function…

Skeletonxf
- 341
- 2
- 7
5
votes
1 answer
Why does js_sys Promise::new require FnMut?
js_sys exposes JavaScript Promise via a function pub fn new(cb: &mut dyn FnMut(Function, Function)) -> Promise;. Per my reading of the MDN documentation, there's nothing suggesting that the executor function will be called more than once, yet js_sys…

Huckle
- 1,810
- 3
- 26
- 40
5
votes
1 answer
Only 1/4th of max memory available when rust wasm compiled with +atomics flag webassembly
So, I've been running out of memory with wasm/rust with +atomic flag and wanted to check how much memory is practically available. Here is my crude minimal working example that logs the memory of a vector before it panics:
index.js
import init from…

ste_kwr
- 820
- 1
- 5
- 21
5
votes
1 answer
How to call an async JavaScript Import Function from WebAssembly (Rust) in a node.js environment?
Let's consider an example import object that looks like this:
const importObject = {
exampleAsyncImportFunction: async () => fs.readFile("./someExampleFile.txt", "utf-8") // returns Promise
};
I want to use it in my rust-written…

Konrad Koschel
- 193
- 9
5
votes
4 answers
Rust - Wasm - Iterating over input file
I'm trying to get access to an Iterator over the contents of a file uploaded via an input field.
I can pass the JS file into Wasm just fine via web-sys, but I cannot for the life of me figure out how to access anything other then length and name of…

Erik Schulze
- 119
- 9
5
votes
1 answer
How can I call a JavaScript function that is a module with wasm-bindgen?
I'm trying to use the Web3 JavaScript library from Rust and I'm stuck. The standard usage of the library starts with:
// In Node.js use: const Web3 = require('web3');
let web3 = new Web3(Web3.givenProvider || "ws://localhost:8545");
The module…

Echo Nolan
- 1,111
- 1
- 11
- 23
5
votes
1 answer
How can a Vec be returned as a typed array with wasm-bindgen?
I have a Vec I would like to return and convert to a typed array with wasm-bindgen, ie, to turn a Vec into a Uint32Array. From my research it appears that wasm-bindgen cannot handle automatically converting these by itself right now (like it…

curiousdannii
- 1,658
- 1
- 25
- 40
5
votes
1 answer
Why are string arguments blank when calling an async Rust function compiled to Wasm from JavaScript?
I'm using wasm_bindgen built with wasm-pack. I have a Rust function I expose to JS:
#[wasm_bindgen]
pub async fn validate_registration_token(backend_api_domain: String, token: String) -> Result {
…

BenjaminC
- 169
- 7
5
votes
1 answer
How to actually get the text of the response body in WASM-Bindgen and Rust
Maybe this is a simple question, but I have yet to find the answer anywhere. I've searched the documentation, but the single example I have found on the entire internet for web-sys fetch still doesn't address this (the official docs, no less).
How…

gajbooks
- 65
- 4
5
votes
1 answer
How to use Vec as return type and make it readable in Javascript with wasm_bindgen
I want to compile the following code.
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub struct Dummy {}
#[wasm_bindgen]
pub fn test() -> Vec {
vec![]
}
However, the compiler doesn't allow me to do that.
error[E0277]: the trait bound…

buckle2000
- 139
- 8