Merge pull request #389 from napi-rs/uncheck
feat(napi): uncheck version of Object methods
This commit is contained in:
commit
a17999024b
4 changed files with 58 additions and 12 deletions
|
@ -114,7 +114,7 @@ impl<'x, 'de, 'env> serde::de::Deserializer<'x> for &'de mut De<'env> {
|
||||||
)),
|
)),
|
||||||
ValueType::Object => {
|
ValueType::Object => {
|
||||||
let js_object = unsafe { JsObject::from_raw_unchecked(self.0.env, self.0.value) };
|
let js_object = unsafe { JsObject::from_raw_unchecked(self.0.env, self.0.value) };
|
||||||
let properties = js_object.get_property_names::<JsObject>()?;
|
let properties = js_object.get_property_names()?;
|
||||||
let property_len = properties.get_array_length_unchecked()?;
|
let property_len = properties.get_array_length_unchecked()?;
|
||||||
if property_len != 1 {
|
if property_len != 1 {
|
||||||
Err(Error::new(
|
Err(Error::new(
|
||||||
|
@ -314,7 +314,7 @@ pub(crate) struct JsObjectAccess<'env> {
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
impl<'env> JsObjectAccess<'env> {
|
impl<'env> JsObjectAccess<'env> {
|
||||||
fn new(value: &'env JsObject) -> Result<Self> {
|
fn new(value: &'env JsObject) -> Result<Self> {
|
||||||
let properties = value.get_property_names::<JsObject>()?;
|
let properties = value.get_property_names()?;
|
||||||
let property_len = properties.get_array_length_unchecked()?;
|
let property_len = properties.get_array_length_unchecked()?;
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
value,
|
value,
|
||||||
|
|
|
@ -53,6 +53,7 @@ impl JsFunction {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// https://nodejs.org/api/n-api.html#n_api_napi_new_instance
|
/// 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.
|
/// 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)]
|
#[allow(clippy::new_ret_no_self)]
|
||||||
#[inline]
|
#[inline]
|
||||||
|
|
|
@ -257,6 +257,19 @@ macro_rules! impl_object_methods {
|
||||||
unsafe { T::from_raw(self.0.env, raw_value) }
|
unsafe { T::from_raw(self.0.env, raw_value) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn get_property_unchecked<K, T>(&self, key: &K) -> Result<T>
|
||||||
|
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]
|
#[inline]
|
||||||
pub fn set_named_property<T>(&mut self, name: &str, value: T) -> Result<()>
|
pub fn set_named_property<T>(&mut self, name: &str, value: T) -> Result<()>
|
||||||
where
|
where
|
||||||
|
@ -301,6 +314,19 @@ macro_rules! impl_object_methods {
|
||||||
unsafe { T::from_raw(self.0.env, raw_value) }
|
unsafe { T::from_raw(self.0.env, raw_value) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn get_named_property_unchecked<T>(&self, name: &str) -> Result<T>
|
||||||
|
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]
|
#[inline]
|
||||||
pub fn has_named_property(&self, name: &str) -> Result<bool> {
|
pub fn has_named_property(&self, name: &str) -> Result<bool> {
|
||||||
let mut result = false;
|
let mut result = false;
|
||||||
|
@ -390,15 +416,12 @@ macro_rules! impl_object_methods {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn get_property_names<T>(&self) -> Result<T>
|
pub fn get_property_names(&self) -> Result<JsObject> {
|
||||||
where
|
|
||||||
T: NapiValue,
|
|
||||||
{
|
|
||||||
let mut raw_value = ptr::null_mut();
|
let mut raw_value = ptr::null_mut();
|
||||||
check_status!(unsafe {
|
check_status!(unsafe {
|
||||||
sys::napi_get_property_names(self.0.env, self.0.value, &mut raw_value)
|
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
|
/// 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) }
|
unsafe { T::from_raw(self.0.env, result) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn get_prototype_unchecked<T>(&self) -> Result<T>
|
||||||
|
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]
|
#[inline]
|
||||||
pub fn set_element<T>(&mut self, index: u32, value: T) -> Result<()>
|
pub fn set_element<T>(&mut self, index: u32, value: T) -> Result<()>
|
||||||
where
|
where
|
||||||
|
@ -476,6 +509,18 @@ macro_rules! impl_object_methods {
|
||||||
unsafe { T::from_raw(self.0.env, raw_value) }
|
unsafe { T::from_raw(self.0.env, raw_value) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn get_element_unchecked<T>(&self, index: u32) -> Result<T>
|
||||||
|
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.
|
/// This method allows the efficient definition of multiple properties on a given object.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn define_properties(&mut self, properties: &[Property]) -> Result<()> {
|
pub fn define_properties(&mut self, properties: &[Property]) -> Result<()> {
|
||||||
|
|
|
@ -91,7 +91,7 @@ fn test_get_property(ctx: CallContext) -> Result<JsUnknown> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[js_function(1)]
|
#[js_function(1)]
|
||||||
fn test_get_property_names(ctx: CallContext) -> Result<JsUnknown> {
|
fn test_get_property_names(ctx: CallContext) -> Result<JsObject> {
|
||||||
let obj = ctx.get::<JsObject>(0)?;
|
let obj = ctx.get::<JsObject>(0)?;
|
||||||
obj.get_property_names()
|
obj.get_property_names()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue