I'm trying to export a struct from Rust to WebAssembly, but I'm getting the following error:
Uncaught (in promise) TypeError: wasm.Test is not a constructor
Rust:
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);
}
#[wasm_bindgen]
pub struct Test {
pub x: i32,
}
#[wasm_bindgen]
impl Test {
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
Self {
x: 0,
}
}
}
JS:
import init from './wasm.js'
async function run() {
const wasm = await init().catch(console.error);
console.log(wasm);
let test = new wasm.Test();
console.log(test);
}
run();
What would be the correct way to export a struct?