diff --git a/napi/src/async_work.rs b/napi/src/async_work.rs index 231f7bfb..913ee454 100644 --- a/napi/src/async_work.rs +++ b/napi/src/async_work.rs @@ -39,12 +39,7 @@ pub fn run(env: &Env, task: T) -> Result> { let mut raw_name = ptr::null_mut(); let s = "napi_rs_async_work"; check_status!(unsafe { - sys::napi_create_string_utf8( - env.0, - s.as_ptr() as *const c_char, - s.len() as _, - &mut raw_name, - ) + sys::napi_create_string_utf8(env.0, s.as_ptr() as *const c_char, s.len(), &mut raw_name) })?; let result = Box::leak(Box::new(AsyncWork { inner_task: task, diff --git a/napi/src/env.rs b/napi/src/env.rs index 5ab7c334..d24b4be2 100644 --- a/napi/src/env.rs +++ b/napi/src/env.rs @@ -151,12 +151,12 @@ impl Env { true => 1, false => 0, }, - len as _, + len, words.as_ptr(), &mut raw_value, ) })?; - Ok(JsBigint::from_raw_unchecked(self.0, raw_value, len as _)) + Ok(JsBigint::from_raw_unchecked(self.0, raw_value, len)) } #[inline] @@ -182,9 +182,7 @@ impl Env { #[inline] fn create_string_from_chars(&self, data_ptr: *const c_char, len: usize) -> Result { let mut raw_value = ptr::null_mut(); - check_status!(unsafe { - sys::napi_create_string_utf8(self.0, data_ptr, len as _, &mut raw_value) - })?; + check_status!(unsafe { sys::napi_create_string_utf8(self.0, data_ptr, len, &mut raw_value) })?; Ok(unsafe { JsString::from_raw_unchecked(self.0, raw_value) }) } @@ -192,7 +190,7 @@ impl Env { pub fn create_string_utf16(&self, chars: &[u16]) -> Result { let mut raw_value = ptr::null_mut(); check_status!(unsafe { - sys::napi_create_string_utf16(self.0, chars.as_ptr(), chars.len() as _, &mut raw_value) + sys::napi_create_string_utf16(self.0, chars.as_ptr(), chars.len(), &mut raw_value) })?; Ok(unsafe { JsString::from_raw_unchecked(self.0, raw_value) }) } @@ -204,7 +202,7 @@ impl Env { sys::napi_create_string_latin1( self.0, chars.as_ptr() as *const _, - chars.len() as _, + chars.len(), &mut raw_value, ) })?; @@ -251,9 +249,7 @@ impl Env { #[inline] pub fn create_array_with_length(&self, length: usize) -> Result { let mut raw_value = ptr::null_mut(); - check_status!(unsafe { - sys::napi_create_array_with_length(self.0, length as _, &mut raw_value) - })?; + check_status!(unsafe { sys::napi_create_array_with_length(self.0, length, &mut raw_value) })?; Ok(unsafe { JsObject::from_raw_unchecked(self.0, raw_value) }) } @@ -264,7 +260,7 @@ impl Env { let mut data: Vec = Vec::with_capacity(length); let mut data_ptr = data.as_mut_ptr() as *mut c_void; check_status!(unsafe { - sys::napi_create_buffer(self.0, length as _, &mut data_ptr, &mut raw_value) + sys::napi_create_buffer(self.0, length, &mut data_ptr, &mut raw_value) })?; Ok(JsBufferValue::new( @@ -287,7 +283,7 @@ impl Env { check_status!(unsafe { sys::napi_create_external_buffer( self.0, - length as _, + length, data_ptr as *mut c_void, Some(drop_buffer), &mut length as *mut usize as *mut _, @@ -322,7 +318,7 @@ impl Env { check_status!(unsafe { sys::napi_create_buffer_copy( self.0, - length as _, + length, data_ptr as *mut c_void, &mut copy_data, &mut raw_value, @@ -344,7 +340,7 @@ impl Env { let mut data: Vec = Vec::with_capacity(length as usize); let mut data_ptr = data.as_mut_ptr() as *mut c_void; check_status!(unsafe { - sys::napi_create_arraybuffer(self.0, length as _, &mut data_ptr, &mut raw_value) + sys::napi_create_arraybuffer(self.0, length, &mut data_ptr, &mut raw_value) })?; Ok(JsArrayBufferValue::new( @@ -362,7 +358,7 @@ impl Env { sys::napi_create_external_arraybuffer( self.0, data_ptr as *mut c_void, - length as _, + length, Some(drop_buffer), &mut length as *mut usize as *mut c_void, &mut raw_value, @@ -512,10 +508,10 @@ impl Env { sys::napi_define_class( self.0, name.as_ptr() as *const c_char, - name.len() as _, + name.len(), Some(constructor_cb), ptr::null_mut(), - raw_properties.len() as _, + raw_properties.len(), raw_properties.as_ptr(), &mut raw_result, ) diff --git a/napi/src/js_values/arraybuffer.rs b/napi/src/js_values/arraybuffer.rs index 6de957f5..45b961c1 100644 --- a/napi/src/js_values/arraybuffer.rs +++ b/napi/src/js_values/arraybuffer.rs @@ -116,17 +116,17 @@ impl JsArrayBuffer { pub fn into_typedarray( self, typedarray_type: TypedArrayType, - length: u64, - byte_offset: u64, + length: usize, + byte_offset: usize, ) -> Result { let mut typedarray_value = ptr::null_mut(); check_status!(unsafe { sys::napi_create_typedarray( self.0.env, typedarray_type.into(), - length as _, + length, self.0.value, - byte_offset as _, + byte_offset, &mut typedarray_value, ) })?; @@ -138,14 +138,14 @@ impl JsArrayBuffer { } #[inline] - pub fn into_dataview(self, length: u64, byte_offset: u64) -> Result { + pub fn into_dataview(self, length: usize, byte_offset: usize) -> Result { let mut dataview_value = ptr::null_mut(); check_status!(unsafe { sys::napi_create_dataview( self.0.env, - length as _, + length, self.0.value, - byte_offset as _, + byte_offset, &mut dataview_value, ) })?; diff --git a/napi/src/js_values/bigint.rs b/napi/src/js_values/bigint.rs index 77f20895..1cfffe9a 100644 --- a/napi/src/js_values/bigint.rs +++ b/napi/src/js_values/bigint.rs @@ -6,7 +6,7 @@ use crate::{check_status, sys, Result}; pub struct JsBigint { pub(crate) raw: Value, - pub word_count: u64, + pub word_count: usize, } impl JsBigint { @@ -14,7 +14,7 @@ impl JsBigint { pub(crate) fn from_raw_unchecked( env: sys::napi_env, value: sys::napi_value, - word_count: u64, + word_count: usize, ) -> Self { Self { raw: Value { @@ -128,12 +128,12 @@ impl NapiValue for JsBigint { } unsafe fn from_raw(env: sys::napi_env, value: sys::napi_value) -> Result { - let mut word_count: u64 = 0; + let mut word_count = 0usize; check_status!(sys::napi_get_value_bigint_words( env, value, ptr::null_mut(), - &mut word_count as *mut u64 as *mut _, + &mut word_count, ptr::null_mut(), ))?; Ok(JsBigint { @@ -147,12 +147,12 @@ impl NapiValue for JsBigint { } unsafe fn from_raw_unchecked(env: sys::napi_env, value: sys::napi_value) -> Self { - let mut word_count: u64 = 0; + let mut word_count = 0usize; let status = sys::napi_get_value_bigint_words( env, value, ptr::null_mut(), - &mut word_count as *mut u64 as *mut _, + &mut word_count, ptr::null_mut(), ); debug_assert!( @@ -200,7 +200,7 @@ impl JsBigint { self.raw.env, self.raw.value, &mut sign_bit, - word_count as *mut u64 as *mut _, + word_count, words.as_mut_ptr(), ) })?; diff --git a/napi/src/js_values/function.rs b/napi/src/js_values/function.rs index b395f395..07d5b548 100644 --- a/napi/src/js_values/function.rs +++ b/napi/src/js_values/function.rs @@ -43,7 +43,7 @@ impl JsFunction { self.0.env, raw_this, self.0.value, - args.len() as _, + args.len(), raw_args.as_ptr(), &mut return_value, ) @@ -70,7 +70,7 @@ impl JsFunction { sys::napi_new_instance( self.0.env, self.0.value, - length as _, + length, raw_args.as_ptr(), &mut js_instance, ) diff --git a/napi/src/js_values/mod.rs b/napi/src/js_values/mod.rs index 56c7f386..326d7dd8 100644 --- a/napi/src/js_values/mod.rs +++ b/napi/src/js_values/mod.rs @@ -369,7 +369,7 @@ macro_rules! impl_object_methods { let mut js_key = ptr::null_mut(); let mut result = false; check_status!(unsafe { - sys::napi_create_string_utf8(self.0.env, string.as_ptr(), name.len() as _, &mut js_key) + sys::napi_create_string_utf8(self.0.env, string.as_ptr(), name.len(), &mut js_key) })?; check_status!(unsafe { sys::napi_has_property(self.0.env, self.0.value, js_key, &mut result) @@ -483,7 +483,7 @@ macro_rules! impl_object_methods { sys::napi_define_properties( self.0.env, self.0.value, - properties.len() as _, + properties.len(), properties .iter() .map(|property| property.raw()) diff --git a/napi/src/js_values/string/mod.rs b/napi/src/js_values/string/mod.rs index cb2010a8..83715efa 100644 --- a/napi/src/js_values/string/mod.rs +++ b/napi/src/js_values/string/mod.rs @@ -55,7 +55,7 @@ impl JsString { self.0.env, self.0.value, buf_ptr, - len as _, + len, &mut written_char_count, ) })?; @@ -65,11 +65,7 @@ impl JsString { Ok(JsStringUtf8 { inner: self, buf: mem::ManuallyDrop::new(unsafe { - Vec::from_raw_parts( - buf_ptr as *mut _, - written_char_count as _, - written_char_count as _, - ) + Vec::from_raw_parts(buf_ptr, written_char_count, written_char_count) }), }) } @@ -81,7 +77,7 @@ impl JsString { #[inline] pub fn into_utf16(self) -> Result { - let mut written_char_count = 0; + let mut written_char_count = 0usize; let len = self.utf16_len()? + 1; let mut result = Vec::with_capacity(len); let buf_ptr = result.as_mut_ptr(); @@ -90,7 +86,7 @@ impl JsString { self.0.env, self.0.value, buf_ptr, - len as _, + len, &mut written_char_count, ) })?; @@ -99,7 +95,7 @@ impl JsString { Ok(JsStringUtf16 { inner: self, buf: mem::ManuallyDrop::new(unsafe { - Vec::from_raw_parts(buf_ptr, written_char_count as _, written_char_count as _) + Vec::from_raw_parts(buf_ptr, written_char_count, written_char_count) }), }) } @@ -111,7 +107,7 @@ impl JsString { #[inline] pub fn into_latin1(self) -> Result { - let mut written_char_count: u64 = 0; + let mut written_char_count = 0usize; let len = self.latin1_len()? + 1; let mut result = Vec::with_capacity(len); let buf_ptr = result.as_mut_ptr(); @@ -120,8 +116,8 @@ impl JsString { self.0.env, self.0.value, buf_ptr, - len as _, - &mut written_char_count as *mut u64 as *mut _, + len, + &mut written_char_count, ) })?; @@ -130,11 +126,7 @@ impl JsString { Ok(JsStringLatin1 { inner: self, buf: mem::ManuallyDrop::new(unsafe { - Vec::from_raw_parts( - buf_ptr as *mut _, - written_char_count as _, - written_char_count as _, - ) + Vec::from_raw_parts(buf_ptr as *mut _, written_char_count, written_char_count) }), }) } diff --git a/napi/src/js_values/string/utf8.rs b/napi/src/js_values/string/utf8.rs index fb8c662d..5239b383 100644 --- a/napi/src/js_values/string/utf8.rs +++ b/napi/src/js_values/string/utf8.rs @@ -1,24 +1,27 @@ use std::convert::TryFrom; +use std::ffi::CStr; use std::mem::ManuallyDrop; +use std::os::raw::c_char; use std::str; use crate::{Error, JsString, Result, Status}; pub struct JsStringUtf8 { pub(crate) inner: JsString, - pub(crate) buf: ManuallyDrop>, + pub(crate) buf: ManuallyDrop>, } impl JsStringUtf8 { #[inline] pub fn as_str(&self) -> Result<&str> { - str::from_utf8(self.buf.as_slice()) + unsafe { CStr::from_ptr(self.buf.as_ptr()) } + .to_str() .map_err(|e| Error::new(Status::InvalidArg, format!("{}", e))) } #[inline] pub fn as_slice(&self) -> &[u8] { - &self.buf + unsafe { CStr::from_ptr(self.buf.as_ptr()) }.to_bytes() } #[inline] diff --git a/napi/src/promise.rs b/napi/src/promise.rs index a7ad507c..4ab3d4f0 100644 --- a/napi/src/promise.rs +++ b/napi/src/promise.rs @@ -27,7 +27,7 @@ impl FuturePromise { sys::napi_create_string_utf8( env, s.as_ptr() as *const c_char, - s.len() as _, + s.len(), &mut async_resource_name, ) })?; @@ -55,7 +55,7 @@ impl FuturePromise { ptr::null_mut(), async_resource_name, 0, - initial_thread_count as _, + initial_thread_count, ptr::null_mut(), None, self_ref as *mut _ as *mut c_void, diff --git a/napi/src/threadsafe_function.rs b/napi/src/threadsafe_function.rs index cb94e88d..82d581ba 100644 --- a/napi/src/threadsafe_function.rs +++ b/napi/src/threadsafe_function.rs @@ -1,6 +1,7 @@ use std::convert::Into; +use std::ffi::CString; use std::marker::PhantomData; -use std::os::raw::{c_char, c_void}; +use std::os::raw::c_void; use std::ptr; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; @@ -104,16 +105,13 @@ impl ThreadsafeFunction { ) -> Result { let mut async_resource_name = ptr::null_mut(); let s = "napi_rs_threadsafe_function"; + let len = s.len(); + let s = CString::new(s)?; check_status!(unsafe { - sys::napi_create_string_utf8( - env, - s.as_ptr() as *const c_char, - s.len() as _, - &mut async_resource_name, - ) + sys::napi_create_string_utf8(env, s.as_ptr(), len, &mut async_resource_name) })?; - let initial_thread_count = 1; + let initial_thread_count = 1usize; let mut raw_tsfn = ptr::null_mut(); let context = ThreadSafeContext(Box::from(callback)); let ptr = Box::into_raw(Box::new(context)) as *mut _; @@ -123,7 +121,7 @@ impl ThreadsafeFunction { func.0.value, ptr::null_mut(), async_resource_name, - max_queue_size as _, + max_queue_size, initial_thread_count, ptr, Some(thread_finalize_cb::), @@ -280,7 +278,7 @@ unsafe extern "C" fn call_js_cb( raw_env, recv, js_callback, - args_length as _, + args_length, args.as_ptr(), ptr::null_mut(), );