Skip to content

Commit

Permalink
Update rust-bindgen version. Use napi v8 (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
morenol committed Sep 22, 2023
1 parent 952a08a commit 20c330c
Show file tree
Hide file tree
Showing 8 changed files with 559 additions and 604 deletions.
739 changes: 310 additions & 429 deletions Cargo.lock

Large diffs are not rendered by default.

29 changes: 12 additions & 17 deletions nj-core/src/basic.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::ptr;
use std::ffi::CString;
use std::collections::VecDeque;
use std::convert::TryFrom;

use log::error;
use log::debug;
Expand All @@ -18,7 +17,6 @@ use crate::sys::napi_has_property;
use crate::sys::napi_ref;
use crate::sys::napi_deferred;
use crate::sys::napi_threadsafe_function_call_js;
use crate::sys::size_t;

use crate::napi_call_result;
use crate::napi_call_assert;
Expand Down Expand Up @@ -83,7 +81,7 @@ impl JsEnv {
napi_call_result!(napi_create_string_utf8(
self.0,
r_string.as_ptr() as *const ::std::os::raw::c_char,
size_t::try_from(r_string.len()).unwrap(),
r_string.len(),
&mut js_value
))?;
Ok(js_value)
Expand All @@ -96,7 +94,7 @@ impl JsEnv {
napi_call_result!(napi_create_string_utf8(
self.0,
r_string.as_ptr() as *const ::std::os::raw::c_char,
size_t::try_from(r_string.len()).unwrap(),
r_string.len(),
&mut js_value
))?;
Ok(js_value)
Expand Down Expand Up @@ -153,9 +151,7 @@ impl JsEnv {
let mut array = ptr::null_mut();

napi_call_result!(crate::sys::napi_create_array_with_length(
self.0,
size_t::try_from(len).unwrap(),
&mut array
self.0, len, &mut array
))?;
Ok(array)
}
Expand Down Expand Up @@ -259,7 +255,7 @@ impl JsEnv {
self.0,
recv,
func,
size_t::try_from(argv.len()).unwrap(),
argv.len(),
argv.as_mut_ptr(),
&mut result
))?;
Expand All @@ -278,7 +274,7 @@ impl JsEnv {

let mut this = ptr::null_mut();

let mut argc = size_t::try_from(max_count).unwrap();
let mut argc = max_count;
let mut args = vec![ptr::null_mut(); max_count];
napi_call_result!(napi_get_cb_info(
self.0,
Expand All @@ -290,7 +286,7 @@ impl JsEnv {
))?;

// truncate arg to actual received count
args.resize(usize::try_from(argc).unwrap(), ptr::null_mut());
args.resize(argc, ptr::null_mut());

Ok(JsCallback::new(JsEnv::new(self.0), this, args))
}
Expand All @@ -313,10 +309,10 @@ impl JsEnv {
napi_call_result!(crate::sys::napi_define_class(
self.0,
name.as_ptr() as *const ::std::os::raw::c_char,
u64::try_from(name.len()).unwrap(),
name.len(),
Some(constructor),
ptr::null_mut(),
u64::try_from(raw_properties.len()).unwrap(),
raw_properties.len(),
raw_properties.as_mut_ptr(),
&mut js_constructor
))?;
Expand Down Expand Up @@ -406,7 +402,7 @@ impl JsEnv {
napi_call_result!(crate::sys::napi_new_instance(
self.0,
constructor,
u64::try_from(args.len()).unwrap(),
args.len(),
args.as_mut_ptr(),
&mut result
))?;
Expand Down Expand Up @@ -621,7 +617,7 @@ impl JsEnv {
use std::slice;
use crate::sys::napi_get_buffer_info;

let mut len = 0u64;
let mut len = 0_usize;
let mut data = ptr::null_mut();

// napi_status napi_get_buffer_info(napi_env env,
Expand All @@ -636,8 +632,7 @@ impl JsEnv {
&mut len
))?;

let array: &[u8] =
unsafe { slice::from_raw_parts(data as *const u8, usize::try_from(len).unwrap()) };
let array: &[u8] = unsafe { slice::from_raw_parts(data as *const u8, len) };

Ok(array)
}
Expand Down Expand Up @@ -914,7 +909,7 @@ impl JsExports {
napi_call_result!(crate::sys::napi_define_properties(
self.env.inner(),
self.inner,
u64::try_from(raw_properties.len()).unwrap(),
raw_properties.len(),
raw_properties.as_mut_ptr()
))
}
Expand Down
8 changes: 3 additions & 5 deletions nj-core/src/bigint.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use std::ptr;
use std::convert::TryFrom;

use log::trace;

use crate::sys::size_t;
use crate::TryIntoJs;
use crate::JSValue;
use crate::sys::napi_value;
Expand All @@ -18,7 +16,7 @@ impl<'a> JSValue<'a> for BigInt {
trace!("Converting JS BigInt to Rust!");

env.assert_type(js_value, crate::sys::napi_valuetype_napi_bigint)?;
let mut word_count = 0u64;
let mut word_count = 0_usize;

// https://nodejs.org/api/n-api.html#n_api_napi_get_value_bigint_words
// Frist call is to figure out how long of a vec to make.
Expand All @@ -31,7 +29,7 @@ impl<'a> JSValue<'a> for BigInt {
))?;

// Now we actually get the sign and the vector.
let mut napi_buffer: Vec<u64> = vec![0; usize::try_from(word_count).unwrap()];
let mut napi_buffer: Vec<u64> = vec![0; word_count];
let mut sign = 0;

crate::napi_call_result!(crate::sys::napi_get_value_bigint_words(
Expand Down Expand Up @@ -109,7 +107,7 @@ impl TryIntoJs for BigInt {
crate::napi_call_result!(crate::sys::napi_create_bigint_words(
env.inner(),
sign,
size_t::try_from(word_count).unwrap(),
word_count,
words.as_ptr(),
&mut napi_buffer
))?;
Expand Down
4 changes: 1 addition & 3 deletions nj-core/src/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use std::ptr;
use std::ops::Deref;
use std::convert::TryFrom;

use log::trace;

use crate::sys::size_t;
use crate::TryIntoJs;
use crate::JSValue;
use crate::sys::{napi_value, napi_ref, napi_env};
Expand Down Expand Up @@ -61,7 +59,7 @@ impl TryIntoJs for ArrayBuffer {
crate::napi_call_result!(crate::sys::napi_create_external_arraybuffer(
js_env.inner(),
data_buffer as *mut core::ffi::c_void,
size_t::try_from(len).unwrap(),
len,
Some(Self::finalize_buffer),
data_box_ptr,
&mut napi_buffer
Expand Down
18 changes: 7 additions & 11 deletions nj-core/src/convert.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::convert::TryFrom;

use std::ptr;

use crate::sys::size_t;
use crate::sys::napi_value;
use crate::val::JsEnv;
use crate::NjError;
Expand Down Expand Up @@ -320,7 +319,7 @@ impl JSValue<'_> for String {

use crate::sys::napi_get_value_string_utf8;

let mut string_size: size_t = 0;
let mut string_size: usize = 0;

napi_call_result!(napi_get_value_string_utf8(
env.inner(),
Expand All @@ -332,9 +331,9 @@ impl JSValue<'_> for String {

string_size += 1;

let chars_vec: Vec<u8> = vec![0; usize::try_from(string_size).unwrap()];
let chars_vec: Vec<u8> = vec![0; string_size];
let mut chars: Box<[u8]> = chars_vec.into_boxed_slice();
let mut read_size: size_t = 0;
let mut read_size: usize = 0;

napi_call_result!(napi_get_value_string_utf8(
env.inner(),
Expand All @@ -344,7 +343,7 @@ impl JSValue<'_> for String {
&mut read_size
))?;

let my_chars: Vec<u8> = chars[0..usize::try_from(read_size).unwrap()].into();
let my_chars: Vec<u8> = chars[0..read_size].into();

String::from_utf8(my_chars).map_err(|err| err.into())
}
Expand All @@ -355,7 +354,7 @@ impl<'a> JSValue<'a> for &'a str {
fn convert_to_rust(env: &'a JsEnv, js_value: napi_value) -> Result<Self, NjError> {
use crate::sys::napi_get_buffer_info;

let mut len: size_t = 0;
let mut len: usize = 0;
let mut data = ptr::null_mut();

napi_call_result!(napi_get_buffer_info(
Expand All @@ -366,10 +365,7 @@ impl<'a> JSValue<'a> for &'a str {
))?;

unsafe {
let i8slice = std::slice::from_raw_parts(
data as *mut ::std::os::raw::c_char,
usize::try_from(len).unwrap(),
);
let i8slice = std::slice::from_raw_parts(data as *mut ::std::os::raw::c_char, len);
let u8slice = &*(i8slice as *const _ as *const [u8]);
std::str::from_utf8(u8slice).map_err(|err| err.into())
}
Expand Down
4 changes: 2 additions & 2 deletions nj-sys/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

install_bindgen:
cargo install bindgen --version 0.56.0
cargo install bindgen-cli --version 0.68.1

generate: install_bindgen
bindgen --verbose node.h -o src/binding.rs
Expand All @@ -12,4 +12,4 @@ generate_arm64: install_bindgen


clean:
rm -rf src/binding.rs
rm -rf src/binding.rs
2 changes: 1 addition & 1 deletion nj-sys/node.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#define NAPI_VERSION 7
#define NAPI_VERSION 8

#include "vendor/nodejs_18_17_1/node_api.h"
Loading

0 comments on commit 20c330c

Please sign in to comment.