How to build a simple

How do I, manually, by hand, build the JSValue of { alpha: false } ? I see the str, f64, ..., but I don't seed the map / dictionary / object.

I want to build an object/dictionary, where there is one kv pair, where k = symbol alpha, and value = boolean false.


I got an idea, build it with js_sys::Map Map in js_sys - Rust and then turn it.into::<Object>(). js_sys has richer APIs for building js data types.

1 Like

rust-tutorials has a hard understand chapter on making wasm bindings in the "triangle-from-scratch" tutorial but at the start of the "WebGL with Bare Wasm" chapter there is a link to a simple example of a thing that calls javascript from wasm. With short blocks showing how to pass things around. Even a few lines with raw string literals getting passed through unsafe pointers..
Anyway I suggest looking at this simple tutorial and read the index.html file and the src/ files.

I have and did use wasm-bindgen when working with for example "egui", but do not understand how it does what it does. This kettle11 example is easy to understand, maybe you can just skip the bindgen thing. One less thing to learn.

You could try using from_serde:


fn alpha_false()->JsValue {
    struct Map { alpha: bool }
    JsValue::from_serde(&Map{ alpha: false }).unwrap()

Internally, this uses __wbindgen_json_parse(...), which I couldn't find any documentation of; it's likely private.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.