Skip to content

Commit 37a9ca4

Browse files
authored
Add BigInt support and more nj-core support for N-API v7. (#97)
* Update to N-API v7 and add BigInt support. * 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. * Update from comment
1 parent e81c419 commit 37a9ca4

19 files changed

+292
-9
lines changed

CHANGELOG.md

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Changelog
22

3+
## Unreleased
4+
- Support for converting to/from [Rust BigInt][rust-bigint] to/from [JavaScript BigInt][js-bigint] ([#97][]).
5+
- Support for converting Rust `u64` to BigInt in JavaScript. ([#97][])
6+
- Updated to N-API v7 in `js-sys`. ([#97][])
7+
8+
[rust-bigint]: https://crates.io/crates/num-bigint
9+
[js-bigint]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt
10+
[#97]: https://github.com/infinyon/node-bindgen/pull/97
11+
312
## [3.0.0] - 2020-10-14-2000
413
- Support for Passing Buffer to Rust
514
- Support for Env cleanup
@@ -11,7 +20,7 @@
1120
- Support for Array [#26](https://github.com/infinyon/node-bindgen/pull/26)
1221

1322
## Fixed
14-
- Fixed conversion of () [#31](https://github.com/infinyon/node-bindgen/pull/31)
23+
- Fixed conversion of `()` [#31](https://github.com/infinyon/node-bindgen/pull/31)
1524

1625
# [2.0.0] - 2020-05-011
1726

@@ -21,4 +30,4 @@
2130

2231
## Fixed
2332

24-
- Proper support for boolean [#19](https://github.com/infinyon/node-bindgen/pull/19)
33+
- Proper support for boolean [#19](https://github.com/infinyon/node-bindgen/pull/19)

Cargo.lock

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/Cargo.lock

+19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ members = [
1414
"buffer",
1515
"array",
1616
"electron",
17-
"cleanup"
17+
"cleanup",
18+
"bigint",
1819
]
1920

2021
#[patch.crates-io]
21-
#flv-future-aio = { path = "../../flv-future/src/future-aio"}
22+
#flv-future-aio = { path = "../../flv-future/src/future-aio"}

examples/Makefile

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ clean:
1111
make -C class-async clean
1212
make -C param clean
1313
make -C electron clean
14+
make -C bigint clean
1415
make -C cleanup clean
1516

1617

1718
test: test-function test-cb test-async-cb test-promise test-json test-class-simple \
18-
test-class-wrapper test-class-async test-stream test-buffer test-array test-cleanup
19+
test-class-wrapper test-class-async test-stream test-buffer test-array test-bigint \
20+
test-cleanup
1921

2022
test-function:
2123
make -C function test
@@ -56,5 +58,8 @@ test-array:
5658
test-electron:
5759
make -C electron test
5860

61+
test-bigint:
62+
make -C bigint test
63+
5964
test-cleanup:
60-
make -C cleanup test
65+
make -C cleanup test

examples/bigint/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist

examples/bigint/Cargo.toml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "nj-example-bigint"
3+
version = "0.1.0"
4+
authors = ["fluvio.io"]
5+
edition = "2018"
6+
7+
8+
[lib]
9+
crate-type = ["cdylib"]
10+
11+
12+
[dependencies]
13+
node-bindgen = { path = "../.."}
14+
15+
16+
[build-dependencies]
17+
node-bindgen = { path = "../../", features = ["build"] }

examples/bigint/Makefile

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
all: build
2+
3+
build:
4+
nj-cli build
5+
6+
test: build
7+
node test.js
8+
9+
10+
clean:
11+
rm -rf dist
12+

examples/bigint/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
manual JS callback

examples/bigint/build.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
node_bindgen::build::configure();
3+
}

examples/bigint/src/lib.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use node_bindgen::derive::node_bindgen;
2+
use node_bindgen::core::bigint::BigInt;
3+
4+
// example where we multiply a big_int.
5+
#[node_bindgen]
6+
fn multiply_big_int(arg: BigInt) -> BigInt {
7+
println!("bigint arg: {}", arg);
8+
arg*2
9+
}
10+
11+
// Test that we can go across the FFI without screwing up the bits.
12+
#[node_bindgen]
13+
fn do_nothing(arg: BigInt) -> BigInt {
14+
println!("bigint arg: {}", arg);
15+
arg
16+
}
17+
18+
// Test out the signage conversion
19+
#[node_bindgen]
20+
fn go_negative(arg: BigInt) -> BigInt {
21+
println!("bigint arg: {}", arg);
22+
-1*arg
23+
}
24+
25+
// Test out that we can return a u64 which is automatically converted to a bigint.
26+
#[node_bindgen]
27+
fn return_u64(arg: u32) -> u64 {
28+
println!("bigint arg: {}", arg);
29+
arg as u64
30+
}

examples/bigint/test.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const assert = require('assert');
2+
3+
let addon = require('./dist');
4+
5+
const hugeBin = BigInt("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")
6+
7+
// Test that we can go back and forth through the FFI
8+
assert.equal(addon.doNothing(hugeBin), hugeBin);
9+
10+
// Test out some signage
11+
assert.equal(addon.goNegative(hugeBin), hugeBin*BigInt(-1));
12+
13+
// How about multiplication?
14+
assert.equal(addon.multiplyBigInt(hugeBin), hugeBin*(BigInt(2)));
15+
16+
// Basic tests from js-env
17+
assert.equal(addon.multiplyBigInt(BigInt(-5)), BigInt(-10));
18+
assert.equal(addon.multiplyBigInt(BigInt(5)), BigInt(10));
19+
assert.equal(addon.multiplyBigInt(BigInt(5)), BigInt(10));
20+
21+
// Rust's u64s turn into JS BigInts.
22+
assert.equal(addon.returnU64(5), BigInt(5));

examples/js-env/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use node_bindgen::core::val::JsEnv;
99
/// JsEnv argument does not manipulate JsCb arguments
1010
#[node_bindgen]
1111
fn multiply(env: JsEnv, arg: f64) -> Result<napi_value, NjError> {
12-
12+
1313
println!("arg: {}",arg);
1414
env.create_double(arg * 2.0)
15-
}
15+
}
1616

examples/js-env/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ const assert = require('assert');
22

33
let addon = require('./dist');
44

5-
assert.equal(addon.multiply(5),10);
5+
assert.equal(addon.multiply(5), 10);

nj-core/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ futures-lite = "1.7.0"
2121
nj-sys = { version = "1.0.0", path = "../nj-sys" }
2222
fluvio-future = { version = "0.1.0", default-features=false, features=["task","subscriber"]}
2323
pin-utils = "0.1.0"
24+
num-bigint = "0.3.0"

nj-core/src/basic.rs

+33
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,18 @@ impl JsEnv {
154154
Ok(result)
155155
}
156156

157+
pub fn create_bigint_uint64(&self, value: u64) -> Result<napi_value,NjError> {
158+
let mut result = ptr::null_mut();
159+
napi_call_result!(
160+
crate::sys::napi_create_bigint_uint64(
161+
self.0,
162+
value,
163+
&mut result
164+
)
165+
)?;
166+
Ok(result)
167+
}
168+
157169
pub fn create_object(&self) -> Result<napi_value,NjError> {
158170

159171
let mut result = ptr::null_mut();
@@ -725,6 +737,27 @@ impl JsEnv {
725737

726738
}
727739

740+
/// Detach ArrayBuffer
741+
pub fn detach_arraybuffer(&self, napi_value: napi_value) -> Result<(),NjError> {
742+
napi_call_result!(
743+
crate::sys::napi_detach_arraybuffer(self.inner(), napi_value)
744+
)?;
745+
Ok(())
746+
}
747+
748+
/// Is this ArrayBuffer Detached?
749+
pub fn is_detached_arraybuffer(&self, napi_value: napi_value) -> Result<bool,NjError> {
750+
let mut is_detached = false;
751+
napi_call_result!(
752+
crate::sys::napi_is_detached_arraybuffer(
753+
self.inner(),
754+
napi_value,
755+
&mut is_detached,
756+
)
757+
)?;
758+
Ok(is_detached)
759+
}
760+
728761
#[allow(unused_unsafe)]
729762
#[allow(clippy::missing_safety_doc)]
730763
pub unsafe fn add_env_clean_up_hook(

0 commit comments

Comments
 (0)