From 49ec8917df353300acafcaff104eea4571b00f96 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Tue, 23 Aug 2022 17:02:41 +0800 Subject: [PATCH] feat(napi): implement as_object and validate for ClassInstance (#1284) --- .../src/bindgen_runtime/js_values/class.rs | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/crates/napi/src/bindgen_runtime/js_values/class.rs b/crates/napi/src/bindgen_runtime/js_values/class.rs index 9005ce33..a2349276 100644 --- a/crates/napi/src/bindgen_runtime/js_values/class.rs +++ b/crates/napi/src/bindgen_runtime/js_values/class.rs @@ -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; @@ -17,6 +20,10 @@ impl ClassInstance { 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 NapiRaw for ClassInstance { @@ -25,6 +32,31 @@ impl NapiRaw for ClassInstance { } } +impl TypeName for ClassInstance +where + &'static T: TypeName, +{ + fn type_name() -> &'static str { + type_name::<&T>() + } + + fn value_type() -> ValueType { + <&T>::value_type() + } +} + +impl ValidateNapiValue for ClassInstance +where + &'static T: ValidateNapiValue, +{ + unsafe fn validate( + env: sys::napi_env, + napi_val: sys::napi_value, + ) -> crate::Result { + unsafe { <&T>::validate(env, napi_val) } + } +} + impl FromNapiValue for ClassInstance { unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> crate::Result { let mut value = ptr::null_mut();