Questions tagged [wasm-bindgen]

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

254 questions
5
votes
1 answer

Invalid or unexpected token 'import' - jest/babel/webpack

I have a rust library I am trying to cross-compile and pack into a npm script. I have been following the process of using the wasm pack. The process builds the es6 js, I wrote some crude tests, then decided to try to use jest as a test runner as I…
nshcore
  • 73
  • 1
  • 6
5
votes
0 answers

How to get rid of lifetime in wrapper struct for wasm_bindgen

currently I have some code similar to the following: // Lifetime of child is enforced to be valid as far as Parent is valid struct Child<'a> { some_data : i32, phantom_lifetime: PhantomData<&'a Parent> } struct Parent…
milck
  • 592
  • 3
  • 12
5
votes
1 answer

Rust wasm_bindgen object turns null in JS side when the object is added to a vector

I have two structs exported to Javascript. I can create instances and use them in JavaScript without any error but when I push the instances into a vector in Rust side, I have an error "Uncaught Error: null pointer passed to rust" Since ownership is…
None
  • 330
  • 2
  • 16
5
votes
0 answers

How do I access a wasm modules memory when its loaded by webpack

I'm trying to follow How to return a string (or similar) from Rust in WebAssembly? Which allows accessing String data inside a wasm module. I have used wasm-pack build to build a RUST module and I can access the methods exported but not the memory…
teknopaul
  • 6,505
  • 2
  • 30
  • 24
5
votes
0 answers

How can I load WASM modules in webview with Rust?

I'm trying to host a React app in webview which uses a compiled Rust -> WebAssembly module. I get this error: Unhandled promise rejection: TypeError: URL is not valid or contains user credentials. The WASM module gets called in one of the…
Andrew Zah
  • 53
  • 1
  • 3
5
votes
2 answers

How to return a Rust closure to JavaScript via WebAssembly?

The comments on closure.rs are pretty great, however I can't make it work for returning a closure from a WebAssembly library. I have a function like this: #[wasm_bindgen] pub fn start_game( start_time: f64, screen_width: f32, …
davidkomer
  • 3,020
  • 2
  • 23
  • 58
5
votes
2 answers

How can I work around not being able to export functions with lifetimes when using wasm-bindgen?

I'm trying to write a simple game that runs in the browser, and I'm having a hard time modeling a game loop given the combination of restrictions imposed by the browser, rust, and wasm-bindgen. A typical game loop in the browser follows this…
w.brian
  • 16,296
  • 14
  • 69
  • 118
4
votes
1 answer

Rust Wasm constructor - Uncaught TypeError: Cannot read properties of undefined

I'm following the simple example as in here to export a rust struct to a React TypeScript application : #[wasm_bindgen] pub struct Foo { contents: u32, } #[wasm_bindgen] impl Foo { #[wasm_bindgen(constructor)] pub fn new() -> Foo { …
mastro
  • 619
  • 1
  • 8
  • 17
4
votes
3 answers

Load wasm in VSCode Web Extension

I'm trying to load a wasm module with a VSCode web extension. I started from the "lsp-web-extension-sample". Within the server directory, I created a simple rust lib with wasm-bindgen, it successfully compiles with wasm-pack. When I call the init…
Cohars
  • 3,822
  • 1
  • 29
  • 50
4
votes
0 answers

Unittests in Rust with WebAssembly

I'm writing a frontend in WASM by using the seed framework. I want to test one of my modules with unittests, but I can't make them to test WASM code. Here's my test suite: #[cfg(test)] mod tests { use super::*; #[test] fn…
Djent
  • 2,877
  • 10
  • 41
  • 66
4
votes
0 answers

How to exec js code on rust with wasm-bindgen correctly

I'm working with rust, WASM, and yew as frontend framework. I'm building a wrapper around materialize-css, to use it on yew as dependency of reusable components. To use some materialize-css components it's necesary initialize it. For example, to use…
al3x
  • 589
  • 1
  • 4
  • 16
4
votes
3 answers

How do I make an HTTP request within a wasm_bindgen function in Rust?

I am developing a NODE JS package using Rust and wasm-pack to compile, and I need to make HTTP requests in my code. I tried to use reqwest library, so everything works fine in the test but I get an error in packing. #![allow(non_snake_case)] use…
Fomalhaut
  • 8,590
  • 8
  • 51
  • 95
4
votes
0 answers

The trait `IntoWasmAbi` is not implemented for `HashMap

Trying to compile following rust code to wasm to make it compatible running with existing js. Trying to return hashmaped value from function. lib.rs use wasm_bindgen::prelude::*; use std::collections::HashMap; #[wasm_bindgen] pub fn…
Anil Arya
  • 3,100
  • 7
  • 43
  • 69
4
votes
1 answer

How to call Rust from JS and back?

I'm trying to adapt the game of life tutorial to call user-defined JS (instead of alert) from Rust: index.js: import * as wasm from "testing-wasm"; export const jsfunc = () => { console.log("jsfunc called"); }; // Call Rust from JS. This…
User
  • 31,811
  • 40
  • 131
  • 232
4
votes
1 answer

no prebuilt wasm-opt binaries

I'm trying to build some webassembly rust applications on my raspberry pi (3b). Everything is going well, although I'm getting an error when running wasm-pack ~/rust/wasm-game-of-life $ wasm-pack build [INFO]: Checking for the Wasm target... [INFO]:…
JHBonarius
  • 10,824
  • 3
  • 22
  • 41
1 2
3
16 17