2020-09-02 18:05:53 +09:00
|
|
|
use std::convert::From;
|
|
|
|
use std::ffi::CString;
|
2020-06-21 20:10:06 +09:00
|
|
|
use std::ptr;
|
|
|
|
|
2021-05-14 00:36:39 +09:00
|
|
|
use crate::{check_status, sys, Callback, Env, NapiRaw, Result};
|
2020-06-21 20:10:06 +09:00
|
|
|
|
2020-11-25 18:42:14 +09:00
|
|
|
#[derive(Clone, Copy)]
|
2020-09-02 18:05:53 +09:00
|
|
|
pub struct Property<'env> {
|
2020-11-25 18:42:14 +09:00
|
|
|
pub name: &'env str,
|
2020-09-02 18:05:53 +09:00
|
|
|
pub(crate) raw_descriptor: sys::napi_property_descriptor,
|
2020-06-21 20:10:06 +09:00
|
|
|
}
|
|
|
|
|
2020-09-02 18:05:53 +09:00
|
|
|
#[repr(u32)]
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub enum PropertyAttributes {
|
2020-11-10 12:09:25 +09:00
|
|
|
Default = sys::napi_property_attributes::napi_default as _,
|
|
|
|
Writable = sys::napi_property_attributes::napi_writable as _,
|
|
|
|
Enumerable = sys::napi_property_attributes::napi_enumerable as _,
|
|
|
|
Configurable = sys::napi_property_attributes::napi_configurable as _,
|
|
|
|
Static = sys::napi_property_attributes::napi_static as _,
|
2020-09-02 18:05:53 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<PropertyAttributes> for sys::napi_property_attributes {
|
|
|
|
fn from(value: PropertyAttributes) -> Self {
|
|
|
|
match value {
|
|
|
|
PropertyAttributes::Default => sys::napi_property_attributes::napi_default,
|
|
|
|
PropertyAttributes::Writable => sys::napi_property_attributes::napi_writable,
|
|
|
|
PropertyAttributes::Enumerable => sys::napi_property_attributes::napi_enumerable,
|
|
|
|
PropertyAttributes::Configurable => sys::napi_property_attributes::napi_configurable,
|
|
|
|
PropertyAttributes::Static => sys::napi_property_attributes::napi_static,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'env> Property<'env> {
|
2020-11-25 18:42:14 +09:00
|
|
|
#[inline]
|
2020-09-03 01:35:47 +09:00
|
|
|
pub fn new(env: &'env Env, name: &'env str) -> Result<Self> {
|
|
|
|
let string_value = CString::new(name)?;
|
|
|
|
let mut result = ptr::null_mut();
|
2020-11-25 18:42:14 +09:00
|
|
|
check_status!(unsafe {
|
2020-11-19 01:08:00 +09:00
|
|
|
sys::napi_create_string_utf8(env.0, string_value.as_ptr(), name.len(), &mut result)
|
2020-09-03 01:35:47 +09:00
|
|
|
})?;
|
|
|
|
Ok(Property {
|
2020-09-02 18:05:53 +09:00
|
|
|
name,
|
2020-06-21 20:10:06 +09:00
|
|
|
raw_descriptor: sys::napi_property_descriptor {
|
|
|
|
utf8name: ptr::null_mut(),
|
2020-09-03 01:35:47 +09:00
|
|
|
name: result,
|
2020-06-21 20:10:06 +09:00
|
|
|
method: None,
|
|
|
|
getter: None,
|
|
|
|
setter: None,
|
|
|
|
value: ptr::null_mut(),
|
|
|
|
attributes: sys::napi_property_attributes::napi_default,
|
|
|
|
data: ptr::null_mut(),
|
|
|
|
},
|
2020-09-03 01:35:47 +09:00
|
|
|
})
|
2020-06-21 20:10:06 +09:00
|
|
|
}
|
|
|
|
|
2020-11-25 18:42:14 +09:00
|
|
|
#[inline]
|
2021-05-14 00:36:39 +09:00
|
|
|
pub fn with_value<T: NapiRaw>(mut self, value: T) -> Self {
|
2020-11-20 01:07:13 +09:00
|
|
|
self.raw_descriptor.value = unsafe { T::raw(&value) };
|
2020-06-21 20:10:06 +09:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-11-25 18:42:14 +09:00
|
|
|
#[inline]
|
2020-06-21 20:10:06 +09:00
|
|
|
pub fn with_method(mut self, callback: Callback) -> Self {
|
|
|
|
self.raw_descriptor.method = Some(callback);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-11-25 18:42:14 +09:00
|
|
|
#[inline]
|
2020-06-21 20:10:06 +09:00
|
|
|
pub fn with_getter(mut self, callback: Callback) -> Self {
|
|
|
|
self.raw_descriptor.getter = Some(callback);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-11-25 18:42:14 +09:00
|
|
|
#[inline]
|
2020-09-02 18:05:53 +09:00
|
|
|
pub fn with_setter(mut self, callback: Callback) -> Self {
|
|
|
|
self.raw_descriptor.setter = Some(callback);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-11-25 18:42:14 +09:00
|
|
|
#[inline]
|
2020-10-31 23:22:23 +09:00
|
|
|
pub fn with_property_attributes(mut self, attributes: PropertyAttributes) -> Self {
|
2020-09-02 18:05:53 +09:00
|
|
|
self.raw_descriptor.attributes = attributes.into();
|
2020-10-31 23:22:23 +09:00
|
|
|
self
|
2020-09-02 18:05:53 +09:00
|
|
|
}
|
|
|
|
|
2020-11-25 18:42:14 +09:00
|
|
|
#[inline]
|
2020-09-03 01:35:47 +09:00
|
|
|
pub(crate) fn raw(&self) -> sys::napi_property_descriptor {
|
|
|
|
self.raw_descriptor
|
2020-06-21 20:10:06 +09:00
|
|
|
}
|
|
|
|
}
|