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

Add a try_from_js implementation for Vec<T> (accept any Array-like) #3755

Merged
merged 6 commits into from
Mar 23, 2024

Conversation

hansl
Copy link
Contributor

@hansl hansl commented Mar 19, 2024

This PR adds an implementation of TryFromJs for Vec<T: TryFromJs> to create Rust vectors from Array-like JavaScript objects.

This is useful when using typed arguments to native functions with arrays.

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 just have a small suggestion, but everything else looks good!

core/engine/src/value/conversions/try_from_js.rs Outdated Show resolved Hide resolved
Copy link

codecov bot commented Mar 19, 2024

Codecov Report

Attention: Patch coverage is 47.36842% with 10 lines in your changes are missing coverage. Please review.

Project coverage is 47.80%. Comparing base (6ddc2b4) to head (439009b).
Report is 96 commits behind head on main.

Files Patch % Lines
core/engine/src/value/conversions/try_from_js.rs 47.36% 10 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3755      +/-   ##
==========================================
+ Coverage   47.24%   47.80%   +0.56%     
==========================================
  Files         476      454      -22     
  Lines       46892    44594    -2298     
==========================================
- Hits        22154    21319     -835     
+ Misses      24738    23275    -1463     

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

@Razican
Copy link
Member

Razican commented Mar 19, 2024

The only concern here would be on how to implement this for TypedArrays, which should have a much faster conversion. Not sure if we would need to specialize or we could just add another branch.

@hansl
Copy link
Contributor Author

hansl commented Mar 19, 2024

The only concern here would be on how to implement this for TypedArrays

I suggest a specialized type, like impl TryFromJs for Bytes. There's no easy way to do that in Rust yet (see how serde handles Vec<u8> for example). Maybe when we have trait specialization in stable.

@hansl hansl requested a review from jedel1043 March 20, 2024 17:15
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 good to me! 😄 Had one nit, but that's not blocking merging.

@@ -250,3 +283,42 @@ impl TryFromJs for u128 {
}
}
}

#[test]
fn value_into_vec() {
Copy link
Member

Choose a reason for hiding this comment

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

nit: can you add a test or two for a failing cases as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. What could I do to unblock this? It's blocking some of my follow work that I'd like to submit.

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 good! Thank you for the contribution!

@jedel1043 jedel1043 added this pull request to the merge queue Mar 23, 2024
Merged via the queue into boa-dev:main with commit 1c6a095 Mar 23, 2024
14 checks passed
@hansl hansl deleted the tryfromjs-for-vec branch March 25, 2024 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants