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 new type Convert<> to convert values #3786

Merged
merged 6 commits into from
Apr 10, 2024

Conversation

hansl
Copy link
Contributor

@hansl hansl commented Apr 4, 2024

This can be used to transform values in to converted versions of Rust types by using the type system and TryFromJs.

Also added a JsString conversion for TryFromJs. These methods are going to be useful when #3773 goes in (but this PR nor that one depend on each others).

This can be used to transform values in to coerced versions by
using the type system and TryFromJs.
Copy link

codecov bot commented Apr 4, 2024

Codecov Report

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

Project coverage is 50.19%. Comparing base (6ddc2b4) to head (645b5e5).
Report is 126 commits behind head on main.

Files Patch % Lines
core/engine/src/value/conversions/convert.rs 0.00% 17 Missing ⚠️
core/engine/src/value/conversions/try_from_js.rs 0.00% 12 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3786      +/-   ##
==========================================
+ Coverage   47.24%   50.19%   +2.95%     
==========================================
  Files         476      457      -19     
  Lines       46892    44851    -2041     
==========================================
+ Hits        22154    22513     +359     
+ Misses      24738    22338    -2400     

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

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.

I think this shouldn't be called Coerce, since it's not really an implicit conversion. Instead, it should be called Cast<T>, which makes sense as a name for an explicit conversion.

@hansl
Copy link
Contributor Author

hansl commented Apr 4, 2024

Coerce (I was hesitating with Coerced) is the proper JavaScript term though when doing implicitly. When explicitly doing Number("123") it is called type conversion, so I'd be fine with Convert.

Cast in Rust is explicitly using ... as ... which is different than JavaScript, so I'd suggest not using that word. Notably this new type can fail when the original type is utterly impossible to cast (NaN to i8 for example), which is impossible with a (Rust) cast.

WDYT?

@jedel1043
Copy link
Member

jedel1043 commented Apr 4, 2024

Yeah, Convert is much better.

@hansl hansl requested a review from jedel1043 April 4, 2024 23:25
@hansl
Copy link
Contributor Author

hansl commented Apr 9, 2024

@jedel1043 ping for this. This is ready to review.

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! Just a small nitpick that doesn't block merging.

core/engine/src/value/conversions/coerce.rs Outdated Show resolved Hide resolved
@jedel1043 jedel1043 requested a review from a team April 9, 2024 03:01
@hansl hansl changed the title Add a new type Coerce<> to coerce values Add a new type Convert<> to convert values Apr 9, 2024
@jedel1043 jedel1043 added enhancement New feature or request API labels Apr 9, 2024
@jedel1043 jedel1043 added this to the v0.18.1 milestone Apr 9, 2024
@HalidOdat
Copy link
Member

I was thinking maybe we should add this logic to the TryFromJs instead of having a separate struct and just using the try_from_js directly:

let int = i32::try_from_js(&JsValue::from(10), context)?;

Is there any benefit to having a separate struct?

@jedel1043
Copy link
Member

The problem is that TryFromJs has "lossless" semantics, which means that it only succeeds if the output type is roughly the same as the JsValue type (no to_string, to_number, to_boolean, etc).
If we want to do the change you're proposing, we would have to change the semantics of TryFromJs, and declare that it allows "lossy" conversions.

I tend towards not doing that because I don't really like to have implicit conversions in Rust APIs, but we could discuss with the team if this is a behaviour we want from TryFromJs.

@HalidOdat HalidOdat added this pull request to the merge queue Apr 10, 2024
Merged via the queue into boa-dev:main with commit 0d7018f Apr 10, 2024
14 checks passed
@hansl hansl deleted the jsstring_from_js branch April 10, 2024 21:44
@raskad raskad modified the milestones: v0.18.1, v0.19.0 Jul 8, 2024
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.

4 participants