boa icon indicating copy to clipboard operation
boa copied to clipboard

`TryIntoJs` trait and derive macro for it

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

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?

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

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

Nikita-str avatar Sep 28 '24 13:09 Nikita-str

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 276 commits behind head on main.

Files with missing lines Patch % Lines
core/macros/src/lib.rs 0.00% 52 Missing :warning:
core/engine/src/value/conversions/try_into_js.rs 61.90% 24 Missing :warning:
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     

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

codecov[bot] avatar Sep 28 '24 15:09 codecov[bot]

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.

HalidOdat avatar Sep 28 '24 15:09 HalidOdat