wasm-bindgen icon indicating copy to clipboard operation
wasm-bindgen copied to clipboard

Convert `JsValue` to `js_sys::Object`

Open coderaiser opened this issue 3 years ago • 1 comments

Summary

Hi!

I want to implement something like deep equal on Rust to speed up comparing AST nodes in 🐊Putout code transformer.

Right now I have such code:

let value_to = js_sys::Reflect::get(current_to, &key)?;

value_to is JsValue, but I need to get keys of an object and using Object::keys:

let keys_from: Array = Object::keys(&value_to);

But that's not work because Object::keys expects to receive js_sys::Object.

How can I convert JsValue to js_sys::Object and vice versa?

Additional Details

I tried to use serde_wasm_bindgen, but I have an error:

image

Object::try_from doesn't works here:

image

coderaiser avatar Jun 08 '22 13:06 coderaiser

You can do this using dyn_into. Instead of Object::try_from(&value_to).unwrap(), use value_to.dyn_into().unwrap(). (You'll also need to import JsCast to use it).

Liamolucko avatar Jun 10 '22 23:06 Liamolucko