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;
|
|
|
|
|
2022-08-06 22:54:58 +09:00
|
|
|
use bitflags::bitflags;
|
|
|
|
|
2022-02-06 17:25:32 +09:00
|
|
|
use crate::{sys, Callback, NapiRaw, Result};
|
2020-06-21 20:10:06 +09:00
|
|
|
|
2022-02-06 17:25:32 +09:00
|
|
|
#[derive(Clone)]
|
2021-09-23 02:29:09 +09:00
|
|
|
pub struct Property {
|
|
|
|
pub name: CString,
|
|
|
|
getter: sys::napi_callback,
|
|
|
|
setter: sys::napi_callback,
|
|
|
|
method: sys::napi_callback,
|
|
|
|
attrs: PropertyAttributes,
|
2022-02-06 17:25:32 +09:00
|
|
|
value: sys::napi_value,
|
2021-09-23 02:29:09 +09:00
|
|
|
pub(crate) is_ctor: bool,
|
2020-06-21 20:10:06 +09:00
|
|
|
}
|
|
|
|
|
2022-02-06 17:25:32 +09:00
|
|
|
impl Default for Property {
|
|
|
|
fn default() -> Self {
|
|
|
|
Property {
|
|
|
|
name: Default::default(),
|
|
|
|
getter: Default::default(),
|
|
|
|
setter: Default::default(),
|
|
|
|
method: Default::default(),
|
|
|
|
attrs: Default::default(),
|
|
|
|
value: ptr::null_mut(),
|
|
|
|
is_ctor: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-06 22:54:58 +09:00
|
|
|
bitflags! {
|
2023-03-16 12:23:03 +09:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
2022-08-06 22:54:58 +09:00
|
|
|
pub struct PropertyAttributes: i32 {
|
|
|
|
const Default = sys::PropertyAttributes::default;
|
|
|
|
const Writable = sys::PropertyAttributes::writable;
|
|
|
|
const Enumerable = sys::PropertyAttributes::enumerable;
|
|
|
|
const Configurable = sys::PropertyAttributes::configurable;
|
|
|
|
const Static = sys::PropertyAttributes::static_;
|
|
|
|
}
|
2020-09-02 18:05:53 +09:00
|
|
|
}
|
|
|
|
|
2021-09-23 02:29:09 +09:00
|
|
|
impl Default for PropertyAttributes {
|
|
|
|
fn default() -> Self {
|
2022-08-06 22:54:58 +09:00
|
|
|
PropertyAttributes::Configurable | PropertyAttributes::Enumerable | PropertyAttributes::Writable
|
2021-09-23 02:29:09 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-02 18:05:53 +09:00
|
|
|
impl From<PropertyAttributes> for sys::napi_property_attributes {
|
|
|
|
fn from(value: PropertyAttributes) -> Self {
|
2022-08-06 22:54:58 +09:00
|
|
|
value.bits()
|
2020-09-02 18:05:53 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-23 02:29:09 +09:00
|
|
|
impl Property {
|
|
|
|
pub fn new(name: &str) -> Result<Self> {
|
2020-09-03 01:35:47 +09:00
|
|
|
Ok(Property {
|
2021-09-23 02:29:09 +09:00
|
|
|
name: CString::new(name)?,
|
|
|
|
..Default::default()
|
2020-09-03 01:35:47 +09:00
|
|
|
})
|
2020-06-21 20:10:06 +09:00
|
|
|
}
|
|
|
|
|
2021-09-23 02:29:09 +09:00
|
|
|
pub fn with_name(mut self, name: &str) -> Self {
|
|
|
|
self.name = CString::new(name).unwrap();
|
2020-06-21 20:10:06 +09:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_method(mut self, callback: Callback) -> Self {
|
2021-09-23 02:29:09 +09:00
|
|
|
self.method = Some(callback);
|
2020-06-21 20:10:06 +09:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_getter(mut self, callback: Callback) -> Self {
|
2021-09-23 02:29:09 +09:00
|
|
|
self.getter = Some(callback);
|
2020-06-21 20:10:06 +09:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-09-02 18:05:53 +09:00
|
|
|
pub fn with_setter(mut self, callback: Callback) -> Self {
|
2021-09-23 02:29:09 +09:00
|
|
|
self.setter = Some(callback);
|
2020-09-02 18:05:53 +09:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-10-31 23:22:23 +09:00
|
|
|
pub fn with_property_attributes(mut self, attributes: PropertyAttributes) -> Self {
|
2021-09-23 02:29:09 +09:00
|
|
|
self.attrs = attributes;
|
2020-10-31 23:22:23 +09:00
|
|
|
self
|
2020-09-02 18:05:53 +09:00
|
|
|
}
|
|
|
|
|
2022-02-06 17:25:32 +09:00
|
|
|
pub fn with_value<T: NapiRaw>(mut self, value: &T) -> Self {
|
|
|
|
self.value = unsafe { T::raw(value) };
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-09-03 01:35:47 +09:00
|
|
|
pub(crate) fn raw(&self) -> sys::napi_property_descriptor {
|
2021-09-23 02:29:09 +09:00
|
|
|
sys::napi_property_descriptor {
|
|
|
|
utf8name: self.name.as_ptr(),
|
|
|
|
name: ptr::null_mut(),
|
|
|
|
method: self.method,
|
|
|
|
getter: self.getter,
|
|
|
|
setter: self.setter,
|
2022-02-06 17:25:32 +09:00
|
|
|
value: self.value,
|
2021-09-23 02:29:09 +09:00
|
|
|
attributes: self.attrs.into(),
|
|
|
|
data: ptr::null_mut(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_ctor(mut self, callback: Callback) -> Self {
|
|
|
|
self.method = Some(callback);
|
|
|
|
self.is_ctor = true;
|
|
|
|
self
|
2020-06-21 20:10:06 +09:00
|
|
|
}
|
|
|
|
}
|