napi-rs/napi/src/js_values/object.rs

222 lines
6 KiB
Rust
Raw Normal View History

use std::ffi::CString;
use std::ptr;
use super::Value;
use crate::error::check_status;
use crate::{sys, Error, JsString, NapiValue, Property, Result, Status};
#[derive(Debug)]
pub struct JsObject(pub(crate) Value);
impl JsObject {
pub fn set_property<V>(&mut self, key: JsString, value: V) -> Result<()>
where
V: NapiValue,
{
2020-08-26 01:07:27 +09:00
check_status(unsafe {
sys::napi_set_property(self.0.env, self.0.value, key.0.value, value.raw_value())
})
}
pub fn get_property<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_value(), &mut raw_value)
})?;
T::from_raw(self.0.env, raw_value)
}
pub fn set_named_property<T>(&mut self, name: &str, value: T) -> Result<()>
where
T: NapiValue,
{
let key = CString::new(name)?;
2020-08-26 01:07:27 +09:00
check_status(unsafe {
sys::napi_set_named_property(self.0.env, self.0.value, key.as_ptr(), value.raw_value())
2020-08-26 01:07:27 +09:00
})
}
pub fn get_named_property<T>(&self, name: &str) -> Result<T>
where
T: NapiValue,
{
let key = CString::new(name)?;
let mut raw_value = ptr::null_mut();
2020-08-26 01:07:27 +09:00
check_status(unsafe {
sys::napi_get_named_property(self.0.env, self.0.value, key.as_ptr(), &mut raw_value)
2020-08-26 01:07:27 +09:00
})?;
T::from_raw(self.0.env, raw_value)
}
pub fn has_named_property<S>(&self, name: S) -> Result<bool>
where
S: AsRef<str>,
{
let mut result = false;
let key = CString::new(name.as_ref())?;
check_status(unsafe {
sys::napi_has_named_property(self.0.env, self.0.value, key.as_ptr(), &mut result)
})?;
Ok(result)
}
pub fn delete_property<S>(&mut self, name: S) -> Result<bool>
where
S: NapiValue,
{
let mut result = false;
check_status(unsafe {
sys::napi_delete_property(self.0.env, self.0.value, name.raw_value(), &mut result)
})?;
Ok(result)
}
pub fn delete_named_property(&mut self, name: &str) -> Result<bool> {
let mut result = false;
let key_str = CString::new(name)?;
let mut js_key = ptr::null_mut();
check_status(unsafe {
sys::napi_create_string_utf8(self.0.env, key_str.as_ptr(), name.len() as _, &mut js_key)
})?;
check_status(unsafe {
sys::napi_delete_property(self.0.env, self.0.value, js_key, &mut result)
})?;
Ok(result)
}
pub fn has_own_property(&self, key: &str) -> Result<bool> {
let mut result = false;
let string = CString::new(key)?;
let mut js_key = ptr::null_mut();
check_status(unsafe {
sys::napi_create_string_utf8(self.0.env, string.as_ptr(), key.len() as _, &mut js_key)
})?;
check_status(unsafe {
sys::napi_has_own_property(self.0.env, self.0.value, js_key, &mut result)
})?;
Ok(result)
}
pub fn has_own_property_js<K>(&self, key: K) -> Result<bool>
where
K: NapiValue,
{
let mut result = false;
check_status(unsafe {
sys::napi_has_own_property(self.0.env, self.0.value, key.raw_value(), &mut result)
})?;
Ok(result)
}
pub fn has_property(&self, name: &str) -> Result<bool> {
let string = CString::new(name)?;
let mut js_key = ptr::null_mut();
let mut result = false;
check_status(unsafe {
sys::napi_create_string_utf8(self.0.env, string.as_ptr(), name.len() as _, &mut js_key)
})?;
check_status(unsafe { sys::napi_has_property(self.0.env, self.0.value, js_key, &mut result) })?;
Ok(result)
}
pub fn has_property_js<K>(&self, name: K) -> Result<bool>
where
K: NapiValue,
{
let mut result = false;
check_status(unsafe {
sys::napi_has_property(self.0.env, self.0.value, name.raw_value(), &mut result)
})?;
Ok(result)
}
pub fn get_property_names<T>(&self) -> Result<T>
where
T: NapiValue,
{
let mut raw_value = ptr::null_mut();
let status = unsafe { sys::napi_get_property_names(self.0.env, self.0.value, &mut raw_value) };
check_status(status)?;
T::from_raw(self.0.env, raw_value)
}
pub fn get_prototype<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) })?;
T::from_raw(self.0.env, result)
}
pub fn set_element<T>(&mut self, index: u32, value: T) -> Result<()>
where
T: NapiValue,
{
check_status(unsafe {
sys::napi_set_element(self.0.env, self.0.value, index, value.raw_value())
})
}
pub fn has_element(&self, index: u32) -> Result<bool> {
let mut result = false;
check_status(unsafe { sys::napi_has_element(self.0.env, self.0.value, index, &mut result) })?;
Ok(result)
}
pub fn delete_element(&mut self, index: u32) -> Result<bool> {
let mut result = false;
check_status(unsafe {
sys::napi_delete_element(self.0.env, self.0.value, index, &mut result)
})?;
Ok(result)
}
pub fn get_element<T>(&self, index: u32) -> Result<T>
where
T: NapiValue,
{
let mut raw_value = ptr::null_mut();
2020-08-26 01:07:27 +09:00
check_status(unsafe {
sys::napi_get_element(self.0.env, self.0.value, index, &mut raw_value)
})?;
T::from_raw(self.0.env, raw_value)
}
pub fn define_properties(&mut self, properties: &[Property]) -> Result<()> {
check_status(unsafe {
sys::napi_define_properties(
self.0.env,
self.0.value,
properties.len() as _,
properties
.iter()
.map(|property| property.raw())
.collect::<Vec<sys::napi_property_descriptor>>()
.as_ptr(),
)
})
}
pub fn get_array_length(&self) -> Result<u32> {
if self.is_array()? != true {
return Err(Error::new(
Status::ArrayExpected,
"Object is not array".to_owned(),
));
}
2020-08-26 01:07:27 +09:00
self.get_array_length_unchecked()
}
#[inline]
pub fn get_array_length_unchecked(&self) -> Result<u32> {
let mut length: u32 = 0;
2020-08-26 01:07:27 +09:00
check_status(unsafe { sys::napi_get_array_length(self.0.env, self.raw_value(), &mut length) })?;
Ok(length)
}
}