Questions tagged [wasm-bindgen]

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

254 questions
3
votes
1 answer

How to use exported struct in JS file?

I have the following definitions in the Rust code: #[wasm_bindgen] pub struct Point { x: i32, y: i32, } #[wasm_bindgen] impl Point { #[wasm_bindgen(constructor)] pub fn new(x: i32, y: i32) -> Point { Point { x, y } …
3
votes
2 answers

Rust wasm in next js

I am trying to use a rust-generated wasm module inside of my NextJs project. I am importing the wasm in the following way: import init, { tokenize } from "@/wasm/lazyjson"; const Test = dynamic({ loader: async () => { await init(); …
Elias
  • 3,592
  • 2
  • 19
  • 42
3
votes
0 answers

Using tagged unions from JavaScript with wasm-bindgen

I have a JavaScript object type where the kind field determines what other fields are present. Examples: { kind: 0, name: "foo" } { kind: 1, name: "foo", value: 69 } { kind: 2 } I put it into an extern block to use it in…
Dull Bananas
  • 892
  • 7
  • 29
3
votes
1 answer

How can I convert JsString into &str in Rust WASM?

How can I convert an instance of type js_sys::JsString into &str within Rust WebAssembly code? Background: I'd like to convert the code found in this SO answer into Rust. I struggle to pass the output of js_sys::encode_uri_component as the value…
blerontin
  • 2,892
  • 5
  • 34
  • 60
3
votes
1 answer

`*arg0` does not live long enough - wasm_bindgen

When running the following code: Cargo.toml [lib] crate-type = ["cdylib"] [dependencies] serde = { version = "1.0", features = ["derive"] } wasm-bindgen = {version = "0.2.67", features = ["serde-serialize"] } wasm-bindgen-futures =…
Eidolon
  • 309
  • 3
  • 12
3
votes
1 answer

How to Pass a String from Js to Wasm generated through Rust using Wasm-Bindgen Web_sys Wasm-pack

the code is basic i am calling a function from js with a string input which is declared in the lib.rs but the string is not passing onto the function in wasm i am receiving an empty string and the reverse is also not working i am not able to pass a…
Alexei Kitaev
  • 159
  • 1
  • 4
3
votes
1 answer

Rust Petgraph WASM Project, trying to use struct with Graph property, but requires 2 parameters

I am very new to Rust, so this might be a very rookie question. I am trying to create a web app with rust -> wasm. Tried to follow the tutorial https://rustwasm.github.io/docs/book/introduction.html and tried to use the crate package "petgraph" I…
bbofrk
  • 33
  • 3
3
votes
2 answers

How to access JS object properties in Wasm (Rust)?

I'm using wasm bindgen and I have following function : #[wasm_bindgen] pub fn obj(o: &JsValue){ console::log_1(o); } and in js I call this function obj({name: "john"}); and it works fine, but when i try to console::log_1(o.name); it gives error…
3
votes
2 answers

How can I link a Rust Wasm application with libpq via wasm-pack?

I'm trying to create a NodeJS application with a Wasm database layer. I use Rust, Diesel as a database driver and wasm-pack as a WebAssembly compiler. When I try to build my service with wasm-pack, it fails trying to link the libpq library. The…
Lodin
  • 2,028
  • 2
  • 18
  • 29
3
votes
1 answer

Implementation of Send is missing when calling FutureExt::boxed()

I'm experimenting with futures with WASM, using wasm-bindgen-futures and the rust-webpack-template; the full working code and problem code is available. My experiment is to call an async fn run(), wrapped by a fn run_js() -> js_sys::Promise, from…
Silly Freak
  • 4,061
  • 1
  • 36
  • 58
3
votes
0 answers

How to handle Rust's errors from Result as a return value instead of throwing an exception in JavaScript?

I'd like to write JavaScript like: var [greeting, err] = wasm.hello("world"); It appears that if I use the standard Result type, then I need to use a try/catch: try { greeting = wasm.hello() } catch(error) { console.error(error); } Rust code…
Ultrasaurus
  • 3,031
  • 2
  • 33
  • 52
3
votes
2 answers

Rust WebAssembly Custom Elements Memory Deallocation Error

My first Rust-produced WASM is producing the following error, I have no idea how to go about it debugging. wasm-000650c2-23:340 Uncaught RuntimeError: memory access out of bounds at dlmalloc::dlmalloc::Dlmalloc::free::h36961b6fbcc40c05…
Lee Goddard
  • 10,680
  • 4
  • 46
  • 63
3
votes
3 answers

How to dump wasm?

I found out that you can view wasm modules in developer mode in chrome, and I also found a chromium flag here, however I want to use a different browser to dump the wasm file. How do I do that?
John v B
  • 41
  • 1
  • 3
2
votes
1 answer

use blockon in rust wasm

i want to use block_on in rust when compiling to wasm. The problem is i am using egui and want to compile it to web. Its function are sync and i need to block async functions. I tried with async_futures, but it seems to start the future, but not…
justauser
  • 23
  • 3
2
votes
1 answer

How to get a value out of a wasm_bindgen_futures::spawn_local

I know that wasm doesn't have blocking so how would I do the equivalent. Or is there a different tool to use? I'm new to this so it could also be that my approach is completely off. I'm using wasm-pack with a web target to compile. fn…
Diomedes
  • 31
  • 1