Skip to content
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 BigInt support and more nj-core support for N-API v7. #97

Merged
merged 3 commits into from
Oct 26, 2020

Conversation

simlay
Copy link
Contributor

@simlay simlay commented Oct 22, 2020

This PR will resolve #28 and will also deal with some of the undone parts of #96.

In an unlisted manner this PR should include:

  • Go get the node.js src, and copy the headers from it for nj-sys. Will be resolved with Added n-api-v7 to nj-sys #99.
  • Add calls from nj-core to nj-sys for N-API v6:
    • napi_create_bigint_uint64 to convert u64s in rust to BigInts in JS.
    • napi_create_bigint_int64 Not applicable.
    • Converting node BigInts to rust BigInts using napi_get_value_bigint_words and napi_create_bigint_words.
    • napi_set_instance_data
    • napi_get_instance_data
    • napi_get_all_property_names
  • Add calls from nj-core to nj-sys for N-API v7:
    • napi_detach_arraybuffer
    • napi_is_detached_arraybuffer
  • Add examples for integration tests.
    • Usage of BigInt in new example workspace.
  • Add words to CHANGLOG.md

@@ -1,646 +1,258 @@
/* automatically generated by rust-bindgen */

pub const NAPI_VERSION: u32 = 5;
pub const NAPI_VERSION: u32 = 7;
Copy link
Contributor Author

@simlay simlay Oct 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't wanna preemptively add more scope to this task but I accidentally ended up pushing the wrong bindings earlier because I didn't run make generate in js-sys. If we're open to it, I'd love to move to using bindgen as a build dependency and generate these bindings at build time rather than having them in the source code.

The pros for doing this are that when the next person to come along and update the N-API version, they'll pop in the new headers into the vendor directory, update the version and then just simply run cargo build in the parent directory and deal with whatever bugs exist. It will also make it so you've got a consistent version of bindgen at compile time (I also had this problem).

The cons of this are that build times will be longer and the jump-to-definition feature of rust-analyzer may not work for things in nj-sys :/.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initial version of the node-bindgen used build.rs to generate binding. Main issue was build time which was long. Hopefully, current rust compiler performance has improved that that is no longer concern.

Also, there was bug in the bindgen the created inconsistent mapping between versions. I think that could be mitigated by tied to specific version of bindgen

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initial version of the node-bindgen used build.rs to generate binding. Main issue was build time which was long. Hopefully, current rust compiler performance has improved that that is no longer concern.

Ah. Yeah. That's definitely a con. I'll play around with it and see what the compile time is like.

nj-core/src/convert.rs Outdated Show resolved Hide resolved
let mut words: Vec<u64> = Vec::new();
use std::cmp::min;

// bytes can be non-multiples of 8.
Copy link
Contributor Author

@simlay simlay Oct 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was tempted to use something like byteorder for this because byte mangling like this can be error prone. Also, writing this took way longer that I wanted (and will for the next person). I'm open adding it but as we've seen with rust-bindgen, it'll add more compile time to whatever depends on this project.

* Updated to N-API v7 in nj-sys.
* Updated CHANGELOG.
* Added detach buffer calls in JsEnv.
* Added BigInt for going to-from rust and JS.
* Re-export the rust bigint exports.
@simlay simlay marked this pull request as ready for review October 24, 2020 21:43
@simlay
Copy link
Contributor Author

simlay commented Oct 25, 2020

I spent a bunch of time today in my non-computer time occasionally thinking about the need to convert Vec to/from Vec for bigint. Anyway, I finally decided to look into the implementation of the rust BigInt crate used in this PR and well depending on your system it actually does use u64's internally. In rust-num/num-bigint#152, a suggestion to expose the internal data structure BigDigitbut the maintainer has some reservations. Depending on how ambitious I am, I might look into adding aFrom for BigInt` in the bigint crate. I think that'd be the way to gain the best performance and maintainability here.

@sehz
Copy link
Collaborator

sehz commented Oct 25, 2020

Can you split these into separate PR? It would be easier to track changes.
Thanks

nj-core/src/bigint.rs Outdated Show resolved Hide resolved
@simlay simlay mentioned this pull request Oct 25, 2020
@simlay simlay changed the title Support N API v7 Add BigInt support and more nj-core support for N-API v7. Oct 25, 2020
@simlay
Copy link
Contributor Author

simlay commented Oct 25, 2020

Can you split these into separate PR? It would be easier to track changes.
Thanks

Sure. I've made #99 to pull out the nj-sys changes which is the bulk of this PR.

@sehz sehz linked an issue Oct 25, 2020 that may be closed by this pull request
51 tasks
@simlay simlay requested a review from sehz October 26, 2020 04:22
Copy link
Collaborator

@sehz sehz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

Thanks

@sehz sehz merged commit 37a9ca4 into infinyon:master Oct 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tracking Issue: complete N-API Support n-api v7
2 participants