Js-sys set array length

Why js_sys::Array do not have the set_length method? That's normal in javascript to modify the array's length.
I've tried this myself and it works without any problem, but I prefer to use the library instead of my own bindings. And I do not see a way to just add this method from outside js-sys.

#[wasm_bindgen]
extern "C" {
    pub type Array;

    #[wasm_bindgen(method, structural, indexing_getter)]
    pub fn get(this: &Array, index: u32) -> JsValue;

    #[wasm_bindgen(method, structural, indexing_setter)]
    pub fn set(this: &Array, index: u32, value: JsValue);

    #[wasm_bindgen(method, structural, getter)]
    pub fn length(this: &Array) -> u32;

    #[wasm_bindgen(method, structural, setter)]
    pub fn set_length(this: &Array, value: u32);
}

Also, it is possible to do this, but it's ugly.

 Reflect::set(
        &out_array,
        &JsValue::from_str("length"),
        &JsValue::from(objects.len() as u32),
    )
    .expect("set_length failed");

Please note when you cross-post.

1 Like