-
-
Notifications
You must be signed in to change notification settings - Fork 456
Implement Upsert methods for weakMap: getOrInsert and getOrInsertComputed #4459
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
base: main
Are you sure you want to change the base?
Conversation
- Enforce object keys; callable callback; reentrancy-safe insert/update. - Register methods, update constructor metadata, and add tests.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4459 +/- ##
==========================================
+ Coverage 47.24% 51.58% +4.33%
==========================================
Files 476 504 +28
Lines 46892 51416 +4524
==========================================
+ Hits 22154 26522 +4368
- Misses 24738 24894 +156 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
Everything looks nice! I just have a couple of suggestions to improve the code.
})?; | ||
|
||
// 3. If CanBeHeldWeakly(key) is false, throw a TypeError exception. | ||
// In Boa this maps to: key must be an Object. |
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.
Could you change this to a TODO
comment instead? It is true that this currently maps to key must be an Object
, but hopefully in the future we'll have a proper CanBeHeldWeakly
to also accept Symbols as valid keys.
let map = object | ||
.as_ref() | ||
.and_then(JsObject::downcast_ref::<NativeWeakMap>) | ||
.ok_or_else(|| { | ||
JsNativeError::typ() | ||
.with_message("WeakMap.getOrInsertComputed: called with non-object value") | ||
})?; |
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.
You can avoid downcasting multiple times by using JsObject::downcast
instead. This will return a "typed" object that you can borrow from by using the borrow
and borrow_mut
methods, and the type system will know that you're working with a JsObject
that has a NativeWeakMap
inside.
This Pull Request closes #4435, implementing the Upsert proposal for weak map
It changes the following:
WeakMap.prototype.getOrInsert
method.WeakMap.prototype.getOrInsertComputed
method.