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

TryIntoJs trait and derive macro for it #3999

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

Nikita-str
Copy link
Contributor

This Pull Request closes #3874

Notes

  • I don't sure about conversion of HashMap into JsObject.
    Should it be converted into JsMap or into ordinary object { k1: v1, ..., kN: vN }?
    Or maybe I need to add a macro attribute to allow choose a way of conversion of Map types?

Copy link
Member

@HalidOdat HalidOdat left a comment

Choose a reason for hiding this comment

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

Why do we need to have a separate derive and implementation for it we can have an automatic impl for it, that uses TryFromJs. Like the standard library does it: https://doc.rust-lang.org/beta/src/core/convert/mod.rs.html#788-815. Implementing TryFromJs should automaticaly implement TryIntoJs.

Am I missing something?

@Nikita-str
Copy link
Contributor Author

Nikita-str commented Sep 28, 2024

@HalidOdat

TryFromJs converts JsValue into Rust types, so there will no such type that converts a Rust type via TryFromJs into JsValue.

In std mod we can have an object of any type as value in try_from.
In the definition of the trait we specifically have value: &JsValue.

So

  • TryFromJs can make only JsValue --> Rust (value always JsValue)
  • TryIntoJs can make only Rust--> JsValue (result always JsValue)

There is try_js_into fn that do the same as TryFromJs: converts JsValue into Rust type. (self is JsValue)

Copy link
Member

@HalidOdat HalidOdat left a comment

Choose a reason for hiding this comment

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

Ahh, I see, yes that makes sense!

The PR looks good to me! :)

@HalidOdat HalidOdat requested a review from a team September 28, 2024 14:55
@HalidOdat HalidOdat added enhancement New feature or request API labels Sep 28, 2024
@HalidOdat HalidOdat added this to the next-release milestone Sep 28, 2024
Copy link

codecov bot commented Sep 28, 2024

Codecov Report

Attention: Patch coverage is 33.91304% with 76 lines in your changes missing coverage. Please review.

Project coverage is 52.80%. Comparing base (6ddc2b4) to head (4be1d56).
Report is 273 commits behind head on main.

Files with missing lines Patch % Lines
core/macros/src/lib.rs 0.00% 52 Missing ⚠️
core/engine/src/value/conversions/try_into_js.rs 61.90% 24 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3999      +/-   ##
==========================================
+ Coverage   47.24%   52.80%   +5.55%     
==========================================
  Files         476      481       +5     
  Lines       46892    46791     -101     
==========================================
+ Hits        22154    24706    +2552     
+ Misses      24738    22085    -2653     

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

@HalidOdat
Copy link
Member

I don't sure about conversion of HashMap into JsObject. Should it be converted into JsMap or into ordinary object { k1: v1, ..., kN: vN }? [..]

I should probably be a JsMap by default, it's also more space efficient, since the properties are stored in an hashmap https://github.com/boa-dev/boa/blob/main/core/engine/src/builtins/map/ordered_map.rs#L36-L42, rather that in the shape and object storage.

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.

Add a TryIntoJs trait and derive macro to transform Rust values into JsValue
2 participants