Reputation: 4444
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-pack
ed function definitions would match the rust function definitions ?
Upvotes: 2
Views: 1413
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