fix(napi): correct the CString usage
This commit is contained in:
parent
eaa96f7eb2
commit
25231b6ae1
5 changed files with 21 additions and 24 deletions
|
@ -109,7 +109,7 @@ impl NapiEnum {
|
|||
|
||||
define_properties.push(quote! {
|
||||
{
|
||||
let name = CString::new(#name_lit).unwrap();
|
||||
let name = CString::new(#name_lit)?;
|
||||
napi::bindgen_prelude::check_status!(
|
||||
napi::bindgen_prelude::sys::napi_set_named_property(env, obj_ptr, name.as_ptr(), i32::to_napi_value(env, #val_lit)?),
|
||||
"Failed to defined enum `{}`",
|
||||
|
|
|
@ -59,11 +59,12 @@ pub fn run<T: Task>(
|
|||
napi_async_work: ptr::null_mut(),
|
||||
status: task_status.clone(),
|
||||
}));
|
||||
let async_work_name = CString::new("napi_rs_async_work")?;
|
||||
check_status!(unsafe {
|
||||
sys::napi_create_async_work(
|
||||
env,
|
||||
raw_resource,
|
||||
CString::new("napi_rs_async_work")?.as_ptr() as *mut _,
|
||||
async_work_name.as_ptr() as *mut _,
|
||||
Some(execute::<T> as unsafe extern "C" fn(env: sys::napi_env, data: *mut c_void)),
|
||||
Some(
|
||||
complete::<T>
|
||||
|
|
|
@ -629,42 +629,39 @@ impl Env {
|
|||
|
||||
/// This API throws a JavaScript Error with the text provided.
|
||||
pub fn throw_error(&self, msg: &str, code: Option<&str>) -> Result<()> {
|
||||
let code = code.and_then(|s| CString::new(s).ok());
|
||||
let msg = CString::new(msg)?;
|
||||
check_status!(unsafe {
|
||||
sys::napi_throw_error(
|
||||
self.0,
|
||||
match code {
|
||||
Some(s) => CString::new(s)?.as_ptr(),
|
||||
None => ptr::null_mut(),
|
||||
},
|
||||
CString::new(msg)?.as_ptr(),
|
||||
code.map(|s| s.as_ptr()).unwrap_or(ptr::null_mut()),
|
||||
msg.as_ptr(),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/// This API throws a JavaScript RangeError with the text provided.
|
||||
pub fn throw_range_error(&self, msg: &str, code: Option<&str>) -> Result<()> {
|
||||
let code = code.and_then(|s| CString::new(s).ok());
|
||||
let msg = CString::new(msg)?;
|
||||
check_status!(unsafe {
|
||||
sys::napi_throw_range_error(
|
||||
self.0,
|
||||
match code {
|
||||
Some(s) => CString::new(s)?.as_ptr(),
|
||||
None => ptr::null_mut(),
|
||||
},
|
||||
CString::new(msg)?.as_ptr(),
|
||||
code.map(|s| s.as_ptr()).unwrap_or(ptr::null_mut()),
|
||||
msg.as_ptr(),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/// This API throws a JavaScript TypeError with the text provided.
|
||||
pub fn throw_type_error(&self, msg: &str, code: Option<&str>) -> Result<()> {
|
||||
let code = code.and_then(|s| CString::new(s).ok());
|
||||
let msg = CString::new(msg)?;
|
||||
check_status!(unsafe {
|
||||
sys::napi_throw_type_error(
|
||||
self.0,
|
||||
match code {
|
||||
Some(s) => CString::new(s)?.as_ptr(),
|
||||
None => ptr::null_mut(),
|
||||
},
|
||||
CString::new(msg)?.as_ptr(),
|
||||
code.map(|s| s.as_ptr()).unwrap_or(ptr::null_mut()),
|
||||
msg.as_ptr(),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
@ -714,11 +711,11 @@ impl Env {
|
|||
.iter()
|
||||
.map(|prop| prop.raw())
|
||||
.collect::<Vec<sys::napi_property_descriptor>>();
|
||||
|
||||
let c_name = CString::new(name)?;
|
||||
check_status!(unsafe {
|
||||
sys::napi_define_class(
|
||||
self.0,
|
||||
name.as_ptr() as *const c_char,
|
||||
c_name.as_ptr() as *const c_char,
|
||||
name.len(),
|
||||
Some(constructor_cb),
|
||||
ptr::null_mut(),
|
||||
|
|
|
@ -306,7 +306,7 @@ macro_rules! impl_object_methods {
|
|||
pub fn create_named_method(&mut self, name: &str, function: Callback) -> Result<()> {
|
||||
let mut js_function = ptr::null_mut();
|
||||
let len = name.len();
|
||||
let name = CString::new(name.as_bytes())?;
|
||||
let name = CString::new(name)?;
|
||||
check_status!(unsafe {
|
||||
sys::napi_create_function(
|
||||
self.0.env,
|
||||
|
|
|
@ -19,10 +19,9 @@ impl TryFrom<sys::napi_node_version> for NodeVersion {
|
|||
minor: value.minor,
|
||||
patch: value.patch,
|
||||
release: unsafe {
|
||||
CStr::from_ptr(value.release).to_str().map_err(|_| Error {
|
||||
status: Status::StringExpected,
|
||||
reason: "Invalid release name".to_owned(),
|
||||
})?
|
||||
CStr::from_ptr(value.release)
|
||||
.to_str()
|
||||
.map_err(|_| Error::new(Status::StringExpected, "Invalid release name".to_owned()))?
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue