boa icon indicating copy to clipboard operation
boa copied to clipboard

`TryFromJs` from `JsMap` for `HashMap` & `BtreeMap`

Open Nikita-str opened this issue 1 year ago • 1 comments

Seems like TryFromJs didn't convert correctly from JsMap into HashMap / BtreeMap

This PR fix it

Example
Next code

use boa_engine::{value::TryFromJs, Context, JsResult, Source};
use std::collections::HashMap;

fn main() -> JsResult<()> {
    let js_code = "new Map([['a', 2], ['b', 3]])";
    let mut context = Context::default();

    let js_value = context.eval(Source::from_bytes(js_code))?;
    println!("{}", js_value.display());

    let map = HashMap::<String, i32>::try_from_js(&js_value, &mut context)?;
    println!("{map:?}");
    Ok(())
}

before this changes will return:

Map { "a" → 2, "b" → 3 }
{}

and after changes returns:

Map { "a" → 2, "b" → 3 }
{"a": 2, "b": 3}

Notes
I don't find more fast way(than the one used in for_each_elem_in_js_map) to get (key, value)'s from JsMap. Maybe there is a way? Or maybe I should write it (for example by function like next but that just return Option<(JsValue, JsValue)>)?

Nikita-str avatar Sep 19 '24 15:09 Nikita-str

Codecov Report

Attention: Patch coverage is 75.00000% with 9 lines in your changes missing coverage. Please review.

Project coverage is 52.91%. Comparing base (6ddc2b4) to head (3a8bf86). Report is 280 commits behind head on main.

Files with missing lines Patch % Lines
core/engine/src/builtins/map/mod.rs 58.82% 7 Missing :warning:
...e/src/value/conversions/try_from_js/collections.rs 87.50% 2 Missing :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3998      +/-   ##
==========================================
+ Coverage   47.24%   52.91%   +5.66%     
==========================================
  Files         476      483       +7     
  Lines       46892    46893       +1     
==========================================
+ Hits        22154    24813    +2659     
+ Misses      24738    22080    -2658     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Sep 23 '24 03:09 codecov[bot]