feat(napi): implement as_object and validate for ClassInstance (#1284)
This commit is contained in:
parent
99e17c7294
commit
49ec8917df
1 changed files with 33 additions and 1 deletions
|
@ -3,7 +3,10 @@ use std::ops::{Deref, DerefMut};
|
|||
use std::ptr;
|
||||
|
||||
use super::Object;
|
||||
use crate::{bindgen_runtime::FromNapiValue, check_status, sys, NapiRaw};
|
||||
use crate::{
|
||||
bindgen_runtime::{FromNapiValue, TypeName, ValidateNapiValue},
|
||||
check_status, sys, Env, NapiRaw, NapiValue, ValueType,
|
||||
};
|
||||
|
||||
pub type This<T = Object> = T;
|
||||
|
||||
|
@ -17,6 +20,10 @@ impl<T: 'static> ClassInstance<T> {
|
|||
pub fn new(value: sys::napi_value, inner: &'static mut T) -> Self {
|
||||
Self { value, inner }
|
||||
}
|
||||
|
||||
pub fn as_object(&self, env: Env) -> Object {
|
||||
unsafe { Object::from_raw_unchecked(env.raw(), self.value) }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: 'static> NapiRaw for ClassInstance<T> {
|
||||
|
@ -25,6 +32,31 @@ impl<T: 'static> NapiRaw for ClassInstance<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: 'static> TypeName for ClassInstance<T>
|
||||
where
|
||||
&'static T: TypeName,
|
||||
{
|
||||
fn type_name() -> &'static str {
|
||||
type_name::<&T>()
|
||||
}
|
||||
|
||||
fn value_type() -> ValueType {
|
||||
<&T>::value_type()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: 'static> ValidateNapiValue for ClassInstance<T>
|
||||
where
|
||||
&'static T: ValidateNapiValue,
|
||||
{
|
||||
unsafe fn validate(
|
||||
env: sys::napi_env,
|
||||
napi_val: sys::napi_value,
|
||||
) -> crate::Result<sys::napi_value> {
|
||||
unsafe { <&T>::validate(env, napi_val) }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: 'static> FromNapiValue for ClassInstance<T> {
|
||||
unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> crate::Result<Self> {
|
||||
let mut value = ptr::null_mut();
|
||||
|
|
Loading…
Reference in a new issue