Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TryFromJs from JsMap for HashMap & BtreeMap #3998

Merged
merged 6 commits into from
Nov 3, 2024

Conversation

Nikita-str
Copy link
Contributor

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)>)?

Copy link
Member

@jedel1043 jedel1043 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I have a small suggestion to simplify things.

Copy link

codecov bot commented Sep 23, 2024

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 ⚠️
...e/src/value/conversions/try_from_js/collections.rs 87.50% 2 Missing ⚠️
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     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jedel1043 jedel1043 added enhancement New feature or request API labels Nov 2, 2024
@jedel1043 jedel1043 added this to the next-release milestone Nov 2, 2024
Copy link
Member

@jedel1043 jedel1043 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thank you!

@jedel1043 jedel1043 requested a review from a team November 2, 2024 13:49
Copy link
Member

@nekevss nekevss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking great! Just had a small thought on the function name that I thought might be worth a little bikeshedding.

core/engine/src/object/builtins/jsmap.rs Outdated Show resolved Hide resolved
Copy link
Member

@nekevss nekevss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thanks for your work on this! 😄

@nekevss nekevss added this pull request to the merge queue Nov 3, 2024
Merged via the queue into boa-dev:main with commit b60b103 Nov 3, 2024
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants