2020-09-02 18:05:53 +09:00
|
|
|
use std::convert::From;
|
2023-03-21 12:22:07 +09:00
|
|
|
#[cfg(feature = "napi5")]
|
|
|
|
use std::ffi::c_void;
|
2020-09-02 18:05:53 +09:00
|
|
|
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;
|
|
|
|
|
2023-03-21 12:22:07 +09:00
|
|
|
#[cfg(feature = "napi5")]
|
|
|
|
use crate::{
|
|
|
|
bindgen_runtime::{FromNapiValue, This, ToNapiValue},
|
|
|
|
Env,
|
|
|
|
};
|
2022-02-06 17:25:32 +09:00
|
|
|
use crate::{sys, Callback, NapiRaw, Result};
|
2020-06-21 20:10:06 +09:00
|
|
|
|
2023-03-21 12:22:07 +09:00
|
|
|
#[cfg(feature = "napi5")]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct PropertyClosures {
|
2023-04-03 12:12:07 +09:00
|
|
|
pub setter_closure: *mut c_void,
|
|
|
|
pub getter_closure: *mut c_void,
|
2023-03-21 12:22:07 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "napi5")]
|
|
|
|
impl Default for PropertyClosures {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
setter_closure: ptr::null_mut(),
|
|
|
|
getter_closure: ptr::null_mut(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2023-03-21 12:22:07 +09:00
|
|
|
#[cfg(feature = "napi5")]
|
|
|
|
pub(crate) closures: PropertyClosures,
|
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(),
|
2023-03-21 12:22:07 +09:00
|
|
|
#[cfg(feature = "napi5")]
|
|
|
|
closures: PropertyClosures::default(),
|
2022-02-06 17:25:32 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-03-21 12:22:07 +09:00
|
|
|
#[cfg(feature = "napi5")]
|
|
|
|
pub fn with_getter_closure<R, F>(mut self, callback: F) -> Self
|
|
|
|
where
|
|
|
|
F: 'static + Fn(Env, This) -> Result<R>,
|
|
|
|
R: ToNapiValue,
|
|
|
|
{
|
|
|
|
let boxed_callback = Box::new(callback);
|
|
|
|
let closure_data_ptr: *mut F = Box::into_raw(boxed_callback);
|
|
|
|
self.closures.getter_closure = closure_data_ptr.cast();
|
|
|
|
|
|
|
|
let fun = crate::trampoline_getter::<R, F>;
|
|
|
|
self.getter = Some(fun);
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-03-21 12:22:07 +09:00
|
|
|
#[cfg(feature = "napi5")]
|
|
|
|
pub fn with_setter_closure<F, V>(mut self, callback: F) -> Self
|
|
|
|
where
|
|
|
|
F: 'static + Fn(crate::Env, This, V) -> Result<()>,
|
|
|
|
V: FromNapiValue,
|
|
|
|
{
|
|
|
|
let boxed_callback = Box::new(callback);
|
|
|
|
let closure_data_ptr: *mut F = Box::into_raw(boxed_callback);
|
|
|
|
self.closures.setter_closure = closure_data_ptr.cast();
|
|
|
|
|
|
|
|
let fun = crate::trampoline_setter::<V, F>;
|
|
|
|
self.setter = Some(fun);
|
|
|
|
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 {
|
2023-03-21 12:22:07 +09:00
|
|
|
#[cfg(feature = "napi5")]
|
|
|
|
let closures = Box::into_raw(Box::new(self.closures));
|
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(),
|
2023-03-21 12:22:07 +09:00
|
|
|
#[cfg(not(feature = "napi5"))]
|
2021-09-23 02:29:09 +09:00
|
|
|
data: ptr::null_mut(),
|
2023-03-21 12:22:07 +09:00
|
|
|
#[cfg(feature = "napi5")]
|
|
|
|
data: closures.cast(),
|
2021-09-23 02:29:09 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|