Questions tagged [wasm-bindgen]

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

254 questions
4
votes
1 answer

How to call an arbitrary previously defined javascript function Rust using wasm-bindgen?

In my javascript, before calling wasm, I define a function jalert that I later want to call from Rust using wasm. I couldn't find in the documentation for wasm-bindgen how to call an arbitrary function that I previously defined in javascript as…
4
votes
0 answers

Create React App import Wasm to Web Worker

I'm working with wasm-bindgen and a react app (created with create-react-app) and I'd like to import a wasm module into a web worker. I'm using this example, just to start out. To work with wasm on the "main-thread" I set up react-app-rewired,…
Mattia
  • 179
  • 15
4
votes
1 answer

Rust/Webassembly/wasm-bindgen - getting values from `js_sys' Promise

I'm playing with wasm-bindgen ( https://github.com/rustwasm/wasm-bindgen ), just out of curiosity. While playing with the Navigator (web_sys crate) I stumbled upon this…
Luca Sepe
  • 2,435
  • 1
  • 20
  • 26
4
votes
1 answer

Passing strings between Rust and JavaScript when building with wasm-pack

I'm building a Chrome extension and I opted to use some WebAssembly functionality. I'm using wasm-pack to build the source because it provides a --target web that reduces the complexity of plugging in the Wasm functions. Passing integer values…
mar-tina
  • 45
  • 6
4
votes
1 answer

Rust wasm: returning two `Uint8Array`s

When I write a function that returns one js_sys::Uint8Array (in Rust): #[wasm_bindgen] pub extern "C" fn gen_pubKey(seed: &[u8]) -> Uint8Array { let (privKey, pubKey) = ed25519::keypair(&seed); unsafe { Uint8Array::view(&pubKey) } } then…
Nicolas Marshall
  • 4,186
  • 9
  • 36
  • 54
4
votes
2 answers

Wasm-bindgen: access wasm instance's memory buffer (from JS)

According to this github comment, I can re-create an Uint8ClampedArray or Uint8Array returned from Rust/wasm by accessing the memory of the wasm instance directly: const textureRaw = new Uint8ClampedArray(memory.buffer, texture.offset(),…
Nicolas Marshall
  • 4,186
  • 9
  • 36
  • 54
4
votes
1 answer

wasm-pack build report error: could not compile `net2`

i use try to use rustwasm refered https://rustwasm.github.io/book/game-of-life/hello-world.html,all worked fine. but when i add reqwest = "0.9.22" to Cargo.toml,then execute wasm-pack build command in my project,the compiler reports such an…
王奕然
  • 3,891
  • 6
  • 41
  • 62
4
votes
0 answers

Resetting Wasm memory in node.js

I have a Wasm module (compiled with Rust/wasm-pack) that I am running in node.js. The module works under normal conditions, but given certain input data it can run out of memory. I would like to continue to process subsequent data sets with the same…
AaronP
  • 71
  • 3
4
votes
1 answer

Is there any way to annotate the TypeScript type of wasm-bindgen's JsValue to something more meaningful?

I have a Rust / Wasm function that I am generating TypeScript bindings for using wasm_bindgen. The output type for JsValue is any. Is there any way to manually annotate the type to something more meaningful without editing the auto-generated…
GiantCowFilms
  • 1,033
  • 1
  • 15
  • 25
4
votes
2 answers

How to create a function returning a borrowed value?

I have the following function as part of a Rust WASM application to convert a Boxed closure into the Rust-representation for a JavaScript function. use js_sys::Function; type Callback = Rc>>>; fn…
Zak
  • 1,910
  • 3
  • 16
  • 31
4
votes
1 answer

Is there a performance gain to use web-sys instead of plain old JavaScript?

I used wasm-bindgen to write a very basic JS/Wasm webapp in which a grayscale transformation (written in Rust) is applied to an image uploaded by the user of the webapp through an and displayed in a . Doing so, I had to…
4
votes
1 answer

FromWasmAbi is not implemented for fn(SomeStruct) , while SomeStruct is #[wasm_bindgen]

I have a 3 structs and two types defined type OnMoveEvent = fn(Board) -> (); type OnGameOverEvent = fn(Player) -> (); #[wasm_bindgen] pub struct Game { on_move: OnMoveEvent, on_game_over: OnGameOverEvent, board:…
dark_ruby
  • 7,646
  • 7
  • 32
  • 57
3
votes
0 answers

How to load a npm package to wasm_bindgen

I tried searching internet, couldn't find details how can I load a npm package to yew. I am trying to load @polkadot/extension-dapp ( code docs) #[wasm_bindgen(module = "/node_modules/@polkadot/extension-dapp/index.js")] extern "C" { …
Amiya Behera
  • 2,210
  • 19
  • 32
3
votes
0 answers

Rust - Sharing state in wasm extern functions

I'm creating a simple app with rust and wasm-bindgen. I want to be able to call the initialize function from javascript, call functions to control the state from the rust program, and then get a value from the state with a function. I cannot pass…
3
votes
0 answers

Chrome: How can I dyn_cast() JavaScript objects that are part of a window created by window.open()

I am working on a Rust WASM library that works with the DOM. To simplify matters, let's assume that this library receives a Node as argument, determines its root node and appends a newly created div to that root node (Link to repo with complete MRE…
Elias Holzmann
  • 3,216
  • 2
  • 17
  • 33