diff --git a/napi/src/js_values/de.rs b/napi/src/js_values/de.rs index 2dac524b..64b07827 100644 --- a/napi/src/js_values/de.rs +++ b/napi/src/js_values/de.rs @@ -114,7 +114,7 @@ impl<'x, 'de, 'env> serde::de::Deserializer<'x> for &'de mut De<'env> { )), ValueType::Object => { let js_object = unsafe { JsObject::from_raw_unchecked(self.0.env, self.0.value) }; - let properties = js_object.get_property_names::()?; + let properties = js_object.get_property_names()?; let property_len = properties.get_array_length_unchecked()?; if property_len != 1 { Err(Error::new( @@ -314,7 +314,7 @@ pub(crate) struct JsObjectAccess<'env> { #[doc(hidden)] impl<'env> JsObjectAccess<'env> { fn new(value: &'env JsObject) -> Result { - let properties = value.get_property_names::()?; + let properties = value.get_property_names()?; let property_len = properties.get_array_length_unchecked()?; Ok(Self { value, diff --git a/napi/src/js_values/function.rs b/napi/src/js_values/function.rs index 07d5b548..22c2b1df 100644 --- a/napi/src/js_values/function.rs +++ b/napi/src/js_values/function.rs @@ -14,10 +14,10 @@ pub struct JsFunction(pub(crate) Value); /// /// #[js_function(1)] /// pub fn call_function(ctx: CallContext) -> Result { -/// let js_func = ctx.get::(0)?; -/// let js_string = ctx.env.create_string("hello".as_ref())?.into_unknown()?; -/// js_func.call(None, &[js_string])?; -/// Ok(ctx.env.get_null()?) +/// let js_func = ctx.get::(0)?; +/// let js_string = ctx.env.create_string("hello".as_ref())?.into_unknown()?; +/// js_func.call(None, &[js_string])?; +/// Ok(ctx.env.get_null()?) /// } /// ``` impl JsFunction { @@ -53,6 +53,7 @@ impl JsFunction { } /// https://nodejs.org/api/n-api.html#n_api_napi_new_instance + /// /// This method is used to instantiate a new `JavaScript` value using a given `JsFunction` that represents the constructor for the object. #[allow(clippy::new_ret_no_self)] #[inline] diff --git a/napi/src/js_values/mod.rs b/napi/src/js_values/mod.rs index ad700ba0..99b4f99b 100644 --- a/napi/src/js_values/mod.rs +++ b/napi/src/js_values/mod.rs @@ -257,6 +257,19 @@ macro_rules! impl_object_methods { unsafe { T::from_raw(self.0.env, raw_value) } } + #[inline] + pub fn get_property_unchecked(&self, key: &K) -> Result + where + K: NapiValue, + T: NapiValue, + { + let mut raw_value = ptr::null_mut(); + check_status!(unsafe { + sys::napi_get_property(self.0.env, self.0.value, key.raw(), &mut raw_value) + })?; + Ok(unsafe { T::from_raw_unchecked(self.0.env, raw_value) }) + } + #[inline] pub fn set_named_property(&mut self, name: &str, value: T) -> Result<()> where @@ -301,6 +314,19 @@ macro_rules! impl_object_methods { unsafe { T::from_raw(self.0.env, raw_value) } } + #[inline] + pub fn get_named_property_unchecked(&self, name: &str) -> Result + where + T: NapiValue, + { + let key = CString::new(name)?; + let mut raw_value = ptr::null_mut(); + check_status!(unsafe { + sys::napi_get_named_property(self.0.env, self.0.value, key.as_ptr(), &mut raw_value) + })?; + Ok(unsafe { T::from_raw_unchecked(self.0.env, raw_value) }) + } + #[inline] pub fn has_named_property(&self, name: &str) -> Result { let mut result = false; @@ -390,15 +416,12 @@ macro_rules! impl_object_methods { } #[inline] - pub fn get_property_names(&self) -> Result - where - T: NapiValue, - { + pub fn get_property_names(&self) -> Result { let mut raw_value = ptr::null_mut(); check_status!(unsafe { sys::napi_get_property_names(self.0.env, self.0.value, &mut raw_value) })?; - unsafe { T::from_raw(self.0.env, raw_value) } + Ok(unsafe { JsObject::from_raw_unchecked(self.0.env, raw_value) }) } /// https://nodejs.org/api/n-api.html#n_api_napi_get_all_property_names @@ -436,6 +459,16 @@ macro_rules! impl_object_methods { unsafe { T::from_raw(self.0.env, result) } } + #[inline] + pub fn get_prototype_unchecked(&self) -> Result + where + T: NapiValue, + { + let mut result = ptr::null_mut(); + check_status!(unsafe { sys::napi_get_prototype(self.0.env, self.0.value, &mut result) })?; + Ok(unsafe { T::from_raw_unchecked(self.0.env, result) }) + } + #[inline] pub fn set_element(&mut self, index: u32, value: T) -> Result<()> where @@ -476,6 +509,18 @@ macro_rules! impl_object_methods { unsafe { T::from_raw(self.0.env, raw_value) } } + #[inline] + pub fn get_element_unchecked(&self, index: u32) -> Result + where + T: NapiValue, + { + let mut raw_value = ptr::null_mut(); + check_status!(unsafe { + sys::napi_get_element(self.0.env, self.0.value, index, &mut raw_value) + })?; + Ok(unsafe { T::from_raw_unchecked(self.0.env, raw_value) }) + } + /// This method allows the efficient definition of multiple properties on a given object. #[inline] pub fn define_properties(&mut self, properties: &[Property]) -> Result<()> { diff --git a/test_module/src/object.rs b/test_module/src/object.rs index 647821e7..a3a9f2c0 100644 --- a/test_module/src/object.rs +++ b/test_module/src/object.rs @@ -91,7 +91,7 @@ fn test_get_property(ctx: CallContext) -> Result { } #[js_function(1)] -fn test_get_property_names(ctx: CallContext) -> Result { +fn test_get_property_names(ctx: CallContext) -> Result { let obj = ctx.get::(0)?; obj.get_property_names() }