feat(napi): with_value method on Property

This commit is contained in:
LongYinan 2022-02-06 16:25:32 +08:00
parent ed12bd76bd
commit dfd213a1ee
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
9 changed files with 86 additions and 5 deletions
crates/napi/src

View file

@ -211,6 +211,38 @@ pub fn get_js_function(raw_fn: ExportRegisterCallback) -> Result<JsFunction> {
})
}
/// Get `C Callback` from defined Rust `fn`
/// ```rust
/// #[napi]
/// fn some_fn() -> u32 {
/// 1
/// }
///
/// #[napi]
/// fn create_obj(env: Env) -> Result<JsObject> {
/// let mut obj = env.create_object()?;
/// obj.define_property(&[Property::new("getter")?.with_getter(get_c_callback(some_fn_js_function)?)])?;
/// Ok(obj)
/// }
/// ```
///
/// ```js
/// console.log(createObj().getter) // 1
/// ```
///
pub fn get_c_callback(raw_fn: ExportRegisterCallback) -> Result<crate::Callback> {
FN_REGISTER_MAP
.borrow_mut()
.get(&raw_fn)
.and_then(|(_env, cb, _name)| *cb)
.ok_or_else(|| {
crate::Error::new(
crate::Status::InvalidArg,
"JavaScript function does not exists".to_owned(),
)
})
}
#[no_mangle]
unsafe extern "C" fn napi_register_module_v1(
env: sys::napi_env,

View file

@ -31,7 +31,7 @@ use serde::Serialize;
#[cfg(all(feature = "tokio_rt", feature = "napi4"))]
use std::future::Future;
pub type Callback = extern "C" fn(sys::napi_env, sys::napi_callback_info) -> sys::napi_value;
pub type Callback = unsafe extern "C" fn(sys::napi_env, sys::napi_callback_info) -> sys::napi_value;
#[derive(Clone, Copy)]
/// `Env` is used to represent a context that the underlying N-API implementation can use to persist VM-specific state.

View file

@ -2,18 +2,33 @@ use std::convert::From;
use std::ffi::CString;
use std::ptr;
use crate::{sys, Callback, Result};
use crate::{sys, Callback, NapiRaw, Result};
#[derive(Clone, Default)]
#[derive(Clone)]
pub struct Property {
pub name: CString,
getter: sys::napi_callback,
setter: sys::napi_callback,
method: sys::napi_callback,
attrs: PropertyAttributes,
value: sys::napi_value,
pub(crate) is_ctor: bool,
}
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(),
}
}
}
#[repr(i32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum PropertyAttributes {
@ -75,6 +90,11 @@ impl Property {
self
}
pub fn with_value<T: NapiRaw>(mut self, value: &T) -> Self {
self.value = unsafe { T::raw(value) };
self
}
pub(crate) fn raw(&self) -> sys::napi_property_descriptor {
sys::napi_property_descriptor {
utf8name: self.name.as_ptr(),
@ -82,7 +102,7 @@ impl Property {
method: self.method,
getter: self.getter,
setter: self.setter,
value: ptr::null_mut(),
value: self.value,
attributes: self.attrs.into(),
data: ptr::null_mut(),
}