-
Notifications
You must be signed in to change notification settings - Fork 285
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 neon::types::JsMap #1080
base: main
Are you sure you want to change the base?
Add neon::types::JsMap #1080
Conversation
80343e9
to
0d8969e
Compare
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.
Thanks so much for this contribution @touilleMan! It'll be really great to have this type in here.
In terms of the design, I would like to have more ergonomic wrappers (i.e., that take Rust types). Unfortunately, this makes the design space a lot more complicated.
What is the minimum set of features you need for your use case? Maybe we could initially start with a smaller version of Map
that doesn't expose everything and we could iterate in future PRs.
@@ -51,6 +51,9 @@ mod napi1 { | |||
|
|||
fn close_handle_scope(env: Env, scope: HandleScope) -> Status; | |||
|
|||
fn instanceof(env: Env, object: Value, constructor: Value, result: *mut bool) |
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.
This is awesome! Thanks for adding it. I think we should add it as a method on the Value
trait.
trait Value {
fn instance_of<V: Value>(&self, cx: &mut Cx, ctor: &V) -> bool {
/* ... */
}
}
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.
Would you mind opening a separate PR to implement this? It's a great feature and we can get it merged pretty quick.
I'm happy to pick it up if you prefer.
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, I've also added a dedicated test
test/napi/src/lib.rs
Outdated
@@ -233,6 +234,24 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> { | |||
cx.export_function("return_js_array_with_string", return_js_array_with_string)?; | |||
cx.export_function("read_js_array", read_js_array)?; | |||
|
|||
cx.export_function("return_js_map", return_js_map)?; |
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.
Can we use the #[neon::export]
macro for all of the new tests?
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 ! that's much cleaner this way \o/
crates/neon/src/sys/tag.rs
Outdated
@@ -137,3 +139,37 @@ pub unsafe fn check_object_type_tag(env: Env, object: Local, tag: &super::TypeTa | |||
pub unsafe fn is_bigint(env: Env, val: Local) -> bool { | |||
is_type(env, val, napi::ValueType::BigInt) | |||
} | |||
|
|||
pub unsafe fn is_map(env: Env, val: Local) -> bool { |
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.
In my opinion, there isn't a great reason to put this in sys
. A lot of the sys
patterns like sys::tag
go back to when Neon supported a C++ backend (prior to Node-API).
The current conventions are to put this code alongside the safe implementation. This also makes it easier to use other safe wrappers instead of falling back to unsafe. Less unsafe code is needed. Unfortunately, the ValueInternal
trait uses pointers, so some unsafe is needed, but only for getting a Context
and Handle
.
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.
I opened #1081 to allow implementing ValueInternal::is_typeof
without any unsafe.
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.
So you mean I should entirely remove sys::tag::is_map
and instead put it content directly inside impl private::ValueInternal for JsMap
's is_typeof
?
EDIT: I've removed sys::tag::is_map
and implemented in JsMap::is_typeof
it's very satisfying how simpler it makes the code 😄
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.
Yep, exactly!
.construct_with(cx) | ||
.apply::<JsObject, _>(cx)?; | ||
|
||
Ok(map.downcast_or_throw(cx)?) |
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.
FYI, downcasting is necessary. Since it's possible to override global.Map
, we don't know that it's actually the built-in. If someone were to override it, the constructor might return a different type.
global.Map = function Map() {
return new Array();
};
new Map() instanceof Map
// false
Unfortunately, I do think we can clean it up a bit by implementing with the safe API wrappers and implementing |
@kjvalencik Thanks for you precise and quick feedback, it's a very motivating to have such nice interactions when submitting a PR 🎉 I've pushed a new comment that should have addressed all your comments (don't hesitate to tell me if you think more changes are needed ^^) (Once the PR is ready for merging, I will squash the commits together to keep things clean)
I'm doing simple conversion between Rust
What do you have in mind ? Having |
Also regarding let xx = cx.string("console.log('Map.groupBy:', Map.groupBy)");
crate::reflect::eval(cx, xx)?; And this prints What should be done then ? I would say doing |
close #1072
This PR is not done yet, but I'd like some feedback on the implementation:
instanceof
which wasn't currently defined in the NAPI1. I think it would be good to also expose this to the end users (maybecx.instanceof()
?), typically when I first discovered Neon doesn't provide Map bindings I tried to implement it myself in my codebase... only to findinstanceof
wasn't available as well ! 😄sys::tag::is_map
is very low level and clumsy, I feel like their should be a cleaner way to implemented it ^^Map.groupBy
is currently broken (this is the added test that is not passing), the issue comes from afailed to downcast any to object
error when trying to downcastMap.groupBy
into aJsFunction
. I guess this is related to the fact groupBy is a static method, but I don't know how to deal with it (maybe entirely bypassingJsFunction
and using lower level code instead ?).undefined
should we check the actual returned value isundefined
then return an empty tuple to stick with the Rust way, or should we just avoid any guesswork and return aJsValue
no matter what ?Map[Symbol.species]
andMap.prototype[Symbol.iterator]()
(FYI I've put TODO about those points in the code)