-
-
Notifications
You must be signed in to change notification settings - Fork 400
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
TryFromJs
from JsMap
for HashMap
& BtreeMap
#3998
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! I have a small suggestion to simplify things.
let next = iter.next(context)?; | ||
let Some(iter_obj) = next.as_object() else { | ||
return unexp_obj_err(); | ||
}; | ||
|
||
let done = iter_obj.get(js_str!("done"), context)?; | ||
let Some(done) = done.as_boolean() else { | ||
return unexp_obj_err(); | ||
}; | ||
if done { | ||
break; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here I think you can use IteratorResult
to do all the checks for you. We'll probably want to make next
return that instead of the raw JsValue
, but constructing it manually should be fine for now.
fn for_each_elem_in_js_map<F>(js_map: &JsMap, mut f: F, context: &mut Context) -> JsResult<()> | ||
where | ||
F: FnMut(JsValue, JsValue, &mut Context) -> JsResult<()>, | ||
{ | ||
let unexp_obj_err = || { | ||
JsResult::Err( | ||
JsNativeError::typ() | ||
.with_message("MapIterator return unexpected object") | ||
.into(), | ||
) | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On second thought, this looks like something that should replace JsMap::for_each
, right? I think it would be a lot more ergonomic instead of our current API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
~Yes, but I actually not sure how to do it via JsMp::for_each
because you should to create an JsFunction
and how I can see it can be done via NativeFunctionPointer
but you can't capture in such function outer map
Rust object (HashMap
/BTreeMap
). And if you use closure that will capture the map
, then it seems like you can't create JsFunction
.
Actually we don't want to create extra js objects here (like JsFunction
), and therefor maybe it makes sense to create an function in JsMap
that will do for_each
but for Rust fn (for F: FnMut
), and maybe make it public only in the crate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I was more talking about substituting JsMap::for_each
with this new function, since it seems that passing JsFunction
directly is a lot more painful overall.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Is riust_for_each
acceptable name?
Just for sureness, is this equivalent to the next:
let map = map
.downcast_ref::<OrderedMap<JsValue>>()
.expect("checked that `this` was a map");
map.iter().try_for_each(|(k, v)|{
let args = [v.clone(), k.clone(), this.clone()];
callback.call(this_arg, &args, context).map(|_|())
})?;
Ok(JsValue::undefined())
If they are not the same, then seems like I incorrectly impl the fn rust_for each
If they the same, maybe simplify it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really sure, but I think we have pretty good coverage. You can try replacing it to see if there are any regressions on the test262 suite.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, they are not the same because JsMap
can be changed in for_each
and it will panic because of mut and non-mut borrowing at the same time
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3998 +/- ##
==========================================
+ Coverage 47.24% 52.83% +5.58%
==========================================
Files 476 480 +4
Lines 46892 46709 -183
==========================================
+ Hits 22154 24677 +2523
+ Misses 24738 22032 -2706 ☔ View full report in Codecov by Sentry. |
Seems like
TryFromJs
didn't convert correctly fromJsMap
intoHashMap
/BtreeMap
This PR fix it
Example
Next code
before this changes will return:
and after changes returns:
Notes
I don't find more fast way(than the one used in
for_each_elem_in_js_map
) to get(key, value)
's fromJsMap
.Maybe there is a way?
Or maybe I should write it (for example by function like next but that just return
Option<(JsValue, JsValue)>
)?