Questions tagged [wasm-bindgen]

wasm-bindgen is a rust crate for facilitating high-level interactions between WebAssembly (Wasm) modules and JavaScript.

254 questions
22
votes
1 answer

How to pass an array of objects to WebAssembly and convert it to a vector of structs with wasm-bindgen?

It's possible to pass an array of integers like this: const js = import("./webassembly_rust"); let array_nums = [1,2,3,4,5,6,7,8,9]; js.then(js => { js.test( array_nums ); }); to WebAssembly and save it in a vector like this: extern crate…
Gregor
  • 331
  • 2
  • 6
19
votes
2 answers

Is it possible to do dynamic linking in WebAssembly with Rust?

I'm making a Turing-complete DSL in Rust for the web using wasm-bindgen. I want the ability to download arbitrary WASM code from the web and then use functions from that file in my DSL. Some sort of dynamic linking with an equivalent of dlopen is…
Emma
  • 294
  • 1
  • 4
  • 11
14
votes
1 answer

Why does wasm-opt fail in wasm-pack builds when generating a function returning a string?

I'm working through the Rust WASM tutorial for Conway's game of life. One of the simplest functions in the file is called Universe.render (it's the one for rendering a string representing game state). It's causing an error when I run wasm-pack…
vaer-k
  • 10,923
  • 11
  • 42
  • 59
11
votes
3 answers

How to convert closure to js_sys::Function?

How to convert a local closure into a js_sys::Function? I want to do something like this: let canvas = document.get_element_by_id("canvas").unwrap(); let e: web_sys::HtmlElement = canvas.dyn_into().unwrap(); let f = ||…
Hossein Noroozpour
  • 1,091
  • 1
  • 13
  • 26
10
votes
2 answers

Is it possible to use wasm-bindgen with webpack 5?

I followed the Hello World Guide for wasm-bindgen (I am using wasm-bindgen = "0.2.72"). Unfortunately the npm packages mentioned in the guide are not really up to date. Because I would like to have a clean starting point, I tried to upgrade…
frankenapps
  • 5,800
  • 6
  • 28
  • 69
10
votes
2 answers

What's a better way to deal with closures in WebAssembly with Rust instead of using forget and leaking memory?

When providing callbacks to JavaScript using Closures, what's a better way to deal with avoiding freeing them? The wasm-bindgen guide suggests using .forget, but admits that that is essentially leaking memory. Normally we'd store the handle to…
user457586
10
votes
1 answer

Multithreading in WebAssembly

I will be grateful if you answer my question about WebAssembly multithreading. I want to implement code with 2 threads (the main thread and a helper one), such that there is a global variable that is used as a counter variable in the helper thread…
user12052616
9
votes
1 answer

How to import a WASM module in WASM (Rust) and pass a String parameter

I want to instantiate a Wasm module from inside a Wasm module, following this js-sys example. In the example, the add function is called which passes i32 parameters. I've created a hello world function, which takes a string as a parameter and…
Arnold Daniels
  • 16,516
  • 4
  • 53
  • 82
9
votes
1 answer

Unable to execute "trunk serve"

I was getting into Rust and looking at these simple instructions for Yew framework (a frontend framework for Rust). (Found at: https://yew.rs/docs/tutorial) I followed the instructions up until the command trunk serve --open However, something…
thatVar
  • 152
  • 2
  • 7
9
votes
3 answers

How can I make webpack embed my *.wasm for use in a web worker?

I have some rust code that compiles to web assembly using wasm-pack and wasm-bindgen. I want to call into this code from a web worklet/worker. The entire app should eventually be just one single *.js file, with everything else inlined. This is what…
9
votes
1 answer

error[E0463]: can't find crate for `core` while building rust project for wasm32-unknown-unknown

I get the following error message: error[E0463]: can't find crate for `core` | = note: the `wasm32-unknown-unknown` target may not be installed error: aborting due to previous error For more information about this error, try `rustc --explain…
Aman Verma
  • 113
  • 1
  • 7
9
votes
1 answer

Is it possible to #[wasm_bindgen] public structs and functions defined in another crate?

I have a crate lib that contains tons of structs and impls. Then I have another one called web that will make the portability of the core lib to the web. There is also api in case I want the app to be…
LeoVen
  • 632
  • 8
  • 18
9
votes
1 answer

Enable a Cargo feature by default when the target arch is WASM?

Part of my Cargo.toml for my crate: [features] wasm = ["ed25519-dalek/nightly", "rand/wasm-bindgen", "js-sys"] This works, when the crate is used in another project, and the "wasm" feature is explicitly chosen. I want to automatically enable this…
user964375
  • 2,201
  • 3
  • 26
  • 27
9
votes
2 answers

When compiling Rust to wasm (web assembly), how can I sleep for 10 milliseconds?

My rust program is managing memory for a 2d html canvas context, and I'm trying to hit ~60fps. I can calculate the delta between each frame easily, and it turns out to be roughly ~5ms. I'm unclear on how to put my Rust webassembly program to sleep…
sgrove
  • 1,149
  • 2
  • 11
  • 23
8
votes
0 answers

Rollup: bundling/embedding wasm code from an external module

Using rollup, I'm trying to bundle a typescript library that imports and calls an npm module that contains a wasm file. Only the resulting bundle contains no trace of the wasm file contents. How can I force it to bundle the webassembly too ? Here's…
Nicolas Marshall
  • 4,186
  • 9
  • 36
  • 54
1
2 3
16 17