From 2cf731316b04c36f15f8649e57a60678096c251c Mon Sep 17 00:00:00 2001 From: LongYinan Date: Thu, 16 Jan 2020 22:10:06 +0800 Subject: [PATCH] feat: add more function --- src/lib.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 480c4cd2..ac7bff72 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,7 +12,7 @@ use std::string::String as RustString; mod executor; pub mod sys; -pub use sys::Status; +pub use sys::{napi_valuetype, Status}; pub type Result = std::result::Result; pub type Callback = extern "C" fn(sys::napi_env, sys::napi_callback_info) -> sys::napi_value; @@ -834,6 +834,17 @@ impl<'env> Value<'env, Object> { Ok(is_array) } + pub fn is_buffer(&self) -> Result { + let mut is_buffer = false; + let status = unsafe { sys::napi_is_buffer(self.raw_env(), self.raw_value(), &mut is_buffer) }; + check_status(status)?; + Ok(is_buffer) + } + + pub fn to_buffer(&self) -> Value<'env, Buffer> { + Value::from_raw(self.env, self.raw_value) + } + pub fn get_array_length(&self) -> Result { if self.is_array()? != true { return Err(Error { @@ -905,6 +916,12 @@ impl<'env> Value<'env, Function> { } } +impl<'env> Value<'env, Any> { + pub fn get_type(&self) -> sys::napi_valuetype { + get_raw_type(self.env.0, self.raw_value) + } +} + impl Drop for Ref { fn drop(&mut self) { unsafe {