fix(napi): CString issues

This commit is contained in:
LongYinan 2020-11-19 00:08:00 +08:00
parent 63a3e059e7
commit 2d47969d08
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
5 changed files with 9 additions and 16 deletions

View file

@ -204,7 +204,7 @@ fn get_execute_js_code(
Err(e) => { Err(e) => {
let message = format!("{}", e); let message = format!("{}", e);
unsafe { unsafe {
napi::sys::napi_throw_error(raw_env, ptr::null(), CString::from_vec_unchecked(message.into()).as_ptr() as *const c_char); napi::sys::napi_throw_error(raw_env, ptr::null(), CString::from_vec_unchecked(message.into()).into_raw());
} }
ptr::null_mut() ptr::null_mut()
} }

View file

@ -368,11 +368,7 @@ impl Env {
pub fn throw_error(&self, msg: &str) -> Result<()> { pub fn throw_error(&self, msg: &str) -> Result<()> {
check_status(unsafe { check_status(unsafe {
sys::napi_throw_error( sys::napi_throw_error(self.0, ptr::null(), CString::new(msg)?.into_raw())
self.0,
ptr::null(),
CString::from_vec_unchecked(msg.into()).as_ptr() as *const _,
)
}) })
} }

View file

@ -257,7 +257,7 @@ macro_rules! impl_object_methods {
where where
T: NapiValue, T: NapiValue,
{ {
let key = CString::new(name.to_owned())?; let key = CString::new(name)?;
check_status(unsafe { check_status(unsafe {
sys::napi_set_named_property(self.0.env, self.0.value, key.as_ptr(), value.raw()) sys::napi_set_named_property(self.0.env, self.0.value, key.as_ptr(), value.raw())
}) })
@ -273,12 +273,9 @@ macro_rules! impl_object_methods {
})?; })?;
T::from_raw(self.0.env, raw_value) T::from_raw(self.0.env, raw_value)
} }
pub fn has_named_property<S>(&self, name: S) -> Result<bool> pub fn has_named_property(&self, name: &str) -> Result<bool> {
where
S: AsRef<str>,
{
let mut result = false; let mut result = false;
let key = CString::new(name.as_ref())?; let key = CString::new(name)?;
check_status(unsafe { check_status(unsafe {
sys::napi_has_named_property(self.0.env, self.0.value, key.as_ptr(), &mut result) sys::napi_has_named_property(self.0.env, self.0.value, key.as_ptr(), &mut result)
})?; })?;
@ -299,7 +296,7 @@ macro_rules! impl_object_methods {
let key_str = CString::new(name)?; let key_str = CString::new(name)?;
let mut js_key = ptr::null_mut(); let mut js_key = ptr::null_mut();
check_status(unsafe { check_status(unsafe {
sys::napi_create_string_utf8(self.0.env, key_str.as_ptr(), name.len() as _, &mut js_key) sys::napi_create_string_utf8(self.0.env, key_str.as_ptr(), name.len(), &mut js_key)
})?; })?;
check_status(unsafe { check_status(unsafe {
sys::napi_delete_property(self.0.env, self.0.value, js_key, &mut result) sys::napi_delete_property(self.0.env, self.0.value, js_key, &mut result)
@ -311,7 +308,7 @@ macro_rules! impl_object_methods {
let string = CString::new(key)?; let string = CString::new(key)?;
let mut js_key = ptr::null_mut(); let mut js_key = ptr::null_mut();
check_status(unsafe { check_status(unsafe {
sys::napi_create_string_utf8(self.0.env, string.as_ptr(), key.len() as _, &mut js_key) sys::napi_create_string_utf8(self.0.env, string.as_ptr(), key.len(), &mut js_key)
})?; })?;
check_status(unsafe { check_status(unsafe {
sys::napi_has_own_property(self.0.env, self.0.value, js_key, &mut result) sys::napi_has_own_property(self.0.env, self.0.value, js_key, &mut result)

View file

@ -37,7 +37,7 @@ impl<'env> Property<'env> {
let string_value = CString::new(name)?; let string_value = CString::new(name)?;
let mut result = ptr::null_mut(); let mut result = ptr::null_mut();
check_status(unsafe { check_status(unsafe {
sys::napi_create_string_utf8(env.0, string_value.as_ptr(), name.len() as _, &mut result) sys::napi_create_string_utf8(env.0, string_value.as_ptr(), name.len(), &mut result)
})?; })?;
Ok(Property { Ok(Property {
name, name,

View file

@ -188,7 +188,7 @@ macro_rules! register_module {
raw_env, raw_env,
ptr::null(), ptr::null(),
CString::from_vec_unchecked(format!("Error initializing module: {}", e).into()) CString::from_vec_unchecked(format!("Error initializing module: {}", e).into())
.as_ptr() as *const _, .into_raw() as *const _,
) )
}; };
ptr::null_mut() ptr::null_mut()