Skip to content

Add BigInt support and more nj-core support for N-API v7. #97

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

Merged
merged 3 commits into from
Oct 26, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## Unreleased
- Support for converting to/from [Rust BigInt][rust-bigint] to/from [JavaScript BigInt][js-bigint] ([#97][]).
- Support for converting Rust `u64` to BigInt in JavaScript. ([#97][])
- Updated to N-API v7 in `js-sys`. ([#97][])

[rust-bigint]: https://crates.io/crates/num-bigint
[js-bigint]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt
[#97]: https://github.com/infinyon/node-bindgen/pull/97

## [3.0.0] - 2020-10-14-2000
- Support for Passing Buffer to Rust
- Support for Env cleanup
@@ -11,7 +20,7 @@
- Support for Array [#26](https://github.com/infinyon/node-bindgen/pull/26)

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

# [2.0.0] - 2020-05-011

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

## Fixed

- Proper support for boolean [#19](https://github.com/infinyon/node-bindgen/pull/19)
- Proper support for boolean [#19](https://github.com/infinyon/node-bindgen/pull/19)
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions examples/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -14,8 +14,9 @@ members = [
"buffer",
"array",
"electron",
"cleanup"
"cleanup",
"bigint",
]

#[patch.crates-io]
#flv-future-aio = { path = "../../flv-future/src/future-aio"}
#flv-future-aio = { path = "../../flv-future/src/future-aio"}
9 changes: 7 additions & 2 deletions examples/Makefile
Original file line number Diff line number Diff line change
@@ -11,11 +11,13 @@ clean:
make -C class-async clean
make -C param clean
make -C electron clean
make -C bigint clean
make -C cleanup clean


test: test-function test-cb test-async-cb test-promise test-json test-class-simple \
test-class-wrapper test-class-async test-stream test-buffer test-array test-cleanup
test-class-wrapper test-class-async test-stream test-buffer test-array test-bigint \
test-cleanup

test-function:
make -C function test
@@ -56,5 +58,8 @@ test-array:
test-electron:
make -C electron test

test-bigint:
make -C bigint test

test-cleanup:
make -C cleanup test
make -C cleanup test
1 change: 1 addition & 0 deletions examples/bigint/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
17 changes: 17 additions & 0 deletions examples/bigint/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "nj-example-bigint"
version = "0.1.0"
authors = ["fluvio.io"]
edition = "2018"


[lib]
crate-type = ["cdylib"]


[dependencies]
node-bindgen = { path = "../.."}


[build-dependencies]
node-bindgen = { path = "../../", features = ["build"] }
12 changes: 12 additions & 0 deletions examples/bigint/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
all: build

build:
nj-cli build

test: build
node test.js


clean:
rm -rf dist

1 change: 1 addition & 0 deletions examples/bigint/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
manual JS callback
3 changes: 3 additions & 0 deletions examples/bigint/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
node_bindgen::build::configure();
}
30 changes: 30 additions & 0 deletions examples/bigint/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use node_bindgen::derive::node_bindgen;
use node_bindgen::core::bigint::BigInt;

// example where we multiply a big_int.
#[node_bindgen]
fn multiply_big_int(arg: BigInt) -> BigInt {
println!("bigint arg: {}", arg);
arg*2
}

// Test that we can go across the FFI without screwing up the bits.
#[node_bindgen]
fn do_nothing(arg: BigInt) -> BigInt {
println!("bigint arg: {}", arg);
arg
}

// Test out the signage conversion
#[node_bindgen]
fn go_negative(arg: BigInt) -> BigInt {
println!("bigint arg: {}", arg);
-1*arg
}

// Test out that we can return a u64 which is automatically converted to a bigint.
#[node_bindgen]
fn return_u64(arg: u32) -> u64 {
println!("bigint arg: {}", arg);
arg as u64
}
22 changes: 22 additions & 0 deletions examples/bigint/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const assert = require('assert');

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

const hugeBin = BigInt("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")

// Test that we can go back and forth through the FFI
assert.equal(addon.doNothing(hugeBin), hugeBin);

// Test out some signage
assert.equal(addon.goNegative(hugeBin), hugeBin*BigInt(-1));

// How about multiplication?
assert.equal(addon.multiplyBigInt(hugeBin), hugeBin*(BigInt(2)));

// Basic tests from js-env
assert.equal(addon.multiplyBigInt(BigInt(-5)), BigInt(-10));
assert.equal(addon.multiplyBigInt(BigInt(5)), BigInt(10));
assert.equal(addon.multiplyBigInt(BigInt(5)), BigInt(10));

// Rust's u64s turn into JS BigInts.
assert.equal(addon.returnU64(5), BigInt(5));
4 changes: 2 additions & 2 deletions examples/js-env/src/lib.rs
Original file line number Diff line number Diff line change
@@ -9,8 +9,8 @@ use node_bindgen::core::val::JsEnv;
/// JsEnv argument does not manipulate JsCb arguments
#[node_bindgen]
fn multiply(env: JsEnv, arg: f64) -> Result<napi_value, NjError> {

println!("arg: {}",arg);
env.create_double(arg * 2.0)
}
}

2 changes: 1 addition & 1 deletion examples/js-env/test.js
Original file line number Diff line number Diff line change
@@ -2,4 +2,4 @@ const assert = require('assert');

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

assert.equal(addon.multiply(5),10);
assert.equal(addon.multiply(5), 10);
1 change: 1 addition & 0 deletions nj-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -21,3 +21,4 @@ futures-lite = "1.7.0"
nj-sys = { version = "1.0.0", path = "../nj-sys" }
fluvio-future = { version = "0.1.0", default-features=false, features=["task","subscriber"]}
pin-utils = "0.1.0"
num-bigint = "0.3.0"
33 changes: 33 additions & 0 deletions nj-core/src/basic.rs
Original file line number Diff line number Diff line change
@@ -154,6 +154,18 @@ impl JsEnv {
Ok(result)
}

pub fn create_bigint_uint64(&self, value: u64) -> Result<napi_value,NjError> {
let mut result = ptr::null_mut();
napi_call_result!(
crate::sys::napi_create_bigint_uint64(
self.0,
value,
&mut result
)
)?;
Ok(result)
}

pub fn create_object(&self) -> Result<napi_value,NjError> {

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

}

/// Detach ArrayBuffer
pub fn detach_arraybuffer(&self, napi_value: napi_value) -> Result<(),NjError> {
napi_call_result!(
crate::sys::napi_detach_arraybuffer(self.inner(), napi_value)
)?;
Ok(())
}

/// Is this ArrayBuffer Detached?
pub fn is_detached_arraybuffer(&self, napi_value: napi_value) -> Result<bool,NjError> {
let mut is_detached = false;
napi_call_result!(
crate::sys::napi_is_detached_arraybuffer(
self.inner(),
napi_value,
&mut is_detached,
)
)?;
Ok(is_detached)
}

#[allow(unused_unsafe)]
#[allow(clippy::missing_safety_doc)]
pub unsafe fn add_env_clean_up_hook(
Loading