From 25231b6ae1c95f66102062e08293f4b3658d363a Mon Sep 17 00:00:00 2001 From: LongYinan Date: Mon, 15 Nov 2021 16:47:55 +0800 Subject: [PATCH] fix(napi): correct the CString usage --- crates/backend/src/codegen/enum.rs | 2 +- crates/napi/src/async_work.rs | 3 ++- crates/napi/src/env.rs | 31 ++++++++++++++---------------- crates/napi/src/js_values/mod.rs | 2 +- crates/napi/src/version.rs | 7 +++---- 5 files changed, 21 insertions(+), 24 deletions(-) diff --git a/crates/backend/src/codegen/enum.rs b/crates/backend/src/codegen/enum.rs index 6e1916d5..0ab5b5f9 100644 --- a/crates/backend/src/codegen/enum.rs +++ b/crates/backend/src/codegen/enum.rs @@ -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 `{}`", diff --git a/crates/napi/src/async_work.rs b/crates/napi/src/async_work.rs index 231a7f21..1ead56b4 100644 --- a/crates/napi/src/async_work.rs +++ b/crates/napi/src/async_work.rs @@ -59,11 +59,12 @@ pub fn run( 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:: as unsafe extern "C" fn(env: sys::napi_env, data: *mut c_void)), Some( complete:: diff --git a/crates/napi/src/env.rs b/crates/napi/src/env.rs index f76c8764..e57943e1 100644 --- a/crates/napi/src/env.rs +++ b/crates/napi/src/env.rs @@ -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::>(); - + 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(), diff --git a/crates/napi/src/js_values/mod.rs b/crates/napi/src/js_values/mod.rs index b0e386cf..87892a1a 100644 --- a/crates/napi/src/js_values/mod.rs +++ b/crates/napi/src/js_values/mod.rs @@ -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, diff --git a/crates/napi/src/version.rs b/crates/napi/src/version.rs index 72ecc51f..7a24ebca 100644 --- a/crates/napi/src/version.rs +++ b/crates/napi/src/version.rs @@ -19,10 +19,9 @@ impl TryFrom 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()))? }, }) }