Storing a #[wasm_bindgen] as window.x

#[wasm_bindgen]
pub struct Foo {
   ...
}


#[wasm_bindgen]

impl Foo {
  ...
}

let x: Foo = ... ;

now, is there a way to assign x to window.x ? Basically I want to be able (from JS land) do window.x.func_name and call the functions in impl Foo { ... }

The part I can't figure out is: how do we assign x: Foo to window.x ?

You could use Object::define_property(&js_sys::global(), &JsValue::from_str("x"), desc) I think. Where desc is a JsValue that represents the object { value: x }.

1 Like

Thanks, this is 99% what I'm looking for. The missing 1%: given:

#[wasm_bindgen]
pub struct Foo {
}

how do we go from x: Foo to a JsValue ? I think I'm missing something really obvious here.

I think you can use the dyn_into method of the JsCast trait, but I'm not sure if JsCast is automatically implemented for #[wasm_bindgen] struct.