-
-
Notifications
You must be signed in to change notification settings - Fork 402
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
[Merged by Bors] - Initial version of a JS -> Rust conversion trait. #2276
Conversation
Test262 conformance changes
|
91bd915
to
e4db757
Compare
After some thoughts coming from the feedback of our users, this would need an extra attribute for each member, so that users can override the transformation function for a particular field. Similarly to We will also need to provide conversions for |
e4db757
to
4c09237
Compare
I have added conversion overrides with attributes in the derive. This should make it easy to implement specific conversions for types that either do not implement The example in Since this was a feature requested by @lastmjs, I would like to get some feedback and see if he likes this approach. This still requires some clean-up, since the coding style of this is not 100% idiomatic. |
What if we want to override these traits for the primitive types, like For our use case, I'm thinking that overriding specific fields isn't really that useful. It's the specific types that we are concerned about. We only need one implementation per type as far as I know, so for every conceivable struct or enum, we might want to override the implementations for certain types, especially the primitives. So thinking of a hypothetical scenario, we could swap out our |
If just a few types will be overridden, couldn't you just use an intermediary newtype, and implement |
f12e6f2
to
e942216
Compare
I'm setting this as "ready for review", since there are a few things that can be implemented, but, I don't have the needed time right now, and this is already useful as-is. Things for the future:
I think this is the minimal implementation that we can provide without making sacrifices in the long run. |
I think this is ready for review. Could we merge it as-is? In the future, we could use this trait to convert |
Does it make sense to put the macros in |
Indeed, this was started when |
@HalidOdat, @raskad, @jedel1043, @RageKnify, @nekevss, @jasonwilliams did you have the chance to run
|
I cannot reproduce it from my machine. I think it's probably a caching issue. |
Let's see if this works... |
Oh, so it isn't caching, it's the tarpaulin action itself apparently... |
So neither the cache nor the action are the cause... |
Codecov Report
@@ Coverage Diff @@
## main #2276 +/- ##
==========================================
- Coverage 51.47% 50.98% -0.50%
==========================================
Files 405 409 +4
Lines 40347 40759 +412
==========================================
+ Hits 20768 20780 +12
- Misses 19579 19979 +400
... and 22 files with indirect coverage changes Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
Huh, removing the cyclic test dep fixed it...
Was about to check if re adding the tests fixed the error but I had to go out, so feel free to uncomment the commented out code, otherwise I'll do that in a couple of hours. |
I'll try to move the tests to a new crate. |
It seems this solves the issue :) Should we merge this? |
Sounds good! bors r+ |
🕐 Waiting for PR status (GitHub check) to be set, probably by CI. Bors will automatically try to run when all required PR statuses are set. |
bors r+ |
This Pull Request closes #1975. It's still a work in progress, but tries to go in that direction. It changes the following: - Adds a new `TryFromJs` trait, that can be derived using a new `boa_derive` crate. - Adds a new `try_js_into()` function that, similarly to the standard library `TryInto` trait Things to think about: - Should the `boa_derive` crate be re-exported in `boa_engine` using a `derive` feature, similar to how it's done in `serde`? - The current implementation only converts perfectly valid values. So, if we try to convert a big integer into an `i8`, or any floating point number to an `f32`. So, you cannot derive `TryFromJs` for structures that contain an `f32` for example (you can still manually implement the trait, though, and decide in favour of a loss of precision). Should we also provide some traits for transparent loss of precision? - Currently, you cannot convert between types, so if the JS struct has an integer, you cannot cast it to a boolean, for example. Should we provide a `TryConvertJs` trait, for example to force conversions? - Currently we only have basic types and object conversions. Should add `Array` to `Vec` conversion, for example, right? Should we also add `TypedArray` conversions? What about `Map` and `Set`? Does this step over the fine grained APIs that we were creating? Note that this still requires a bunch of documentation, tests, and validation from the dev team and from the users that requested this feature. I'm particularly interested in @lastmjs's thoughts on this API. I already added an usage example in `boa_examples/src/bin/derive.rs`. Co-authored-by: jedel1043 <jedel0124@gmail.com>
Pull request successfully merged into main. Build succeeded: |
This Pull Request closes #1975. It's still a work in progress, but tries to go in that direction.
It changes the following:
TryFromJs
trait, that can be derived using a newboa_derive
crate.try_js_into()
function that, similarly to the standard libraryTryInto
traitThings to think about:
boa_derive
crate be re-exported inboa_engine
using aderive
feature, similar to how it's done inserde
?i8
, or any floating point number to anf32
. So, you cannot deriveTryFromJs
for structures that contain anf32
for example (you can still manually implement the trait, though, and decide in favour of a loss of precision). Should we also provide some traits for transparent loss of precision?TryConvertJs
trait, for example to force conversions?Array
toVec
conversion, for example, right? Should we also addTypedArray
conversions? What aboutMap
andSet
? Does this step over the fine grained APIs that we were creating?Note that this still requires a bunch of documentation, tests, and validation from the dev team and from the users that requested this feature. I'm particularly interested in @lastmjs's thoughts on this API.
I already added an usage example in
boa_examples/src/bin/derive.rs
.