Nicolas Marshall
Nicolas Marshall

Reputation: 4444

Wasm-bindgen: arrays of u8 as inputs and outputs, generated javascript has different function signatures

I've written a function that takes an u8 array as input and outputs an Uint8Array

use js_sys::Uint8Array;

#[wasm_bindgen]
pub extern "C" fn ab(seed: &[u8]) -> Uint8Array {
    let array: Array = seed.into_iter().map(|x| JsValue::from(*x as u8)).collect();
    let u8a = Uint8Array::new(&array);
    u8a
}

Then built it into wasm+javascript with wasm-pack.

As output, I get the following typescript function definitions:

export function ab(a: number, b: number): number;
export function __wbindgen_malloc(a: number): number;

What would the two input numbers to ab() be ? How should these functions be used ?

Also, is there a better way to allocate to the wasm memory from Rust directly so that the wasm-packed function definitions would match the rust function definitions ?

Upvotes: 2

Views: 1413

Answers (1)

apetranzilla
apetranzilla

Reputation: 5919

wasm_bindgen generates two JS files. You're looking at the <name>_bg.js file, which exposes the "raw" bindings to the webassembly module, where the arguments are pointers. It should also have generated a <name>.js file, which has the functions that use the expected JS types.

Upvotes: 4

Related Questions