From e5bdc1329aa937542ee5afbbf663e07e842a82d5 Mon Sep 17 00:00:00 2001 From: "Franz Heinzmann (Frando)" Date: Sat, 26 Jan 2019 23:33:47 +0100 Subject: [PATCH] Add more object functions. --- src/lib.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index f08881d4..07ecf051 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -667,10 +667,68 @@ impl<'env> Value<'env, Object> { Value::::from_raw(self.env, raw_value).try_into() } + pub fn get_property_names(&self) -> Result> { + let mut raw_value = ptr::null_mut(); + let status = unsafe { + sys::napi_get_property_names( + self.raw_env(), + self.raw_value(), + &mut raw_value + ) + }; + check_status(status)?; + Value::::from_raw(self.env, raw_value).try_into() + } + pub fn set_index<'a, T>(&mut self, index: usize, value: Value) -> Result<()> { self.set_property(self.env.create_int64(index as i64), value) } + pub fn get_index(&self, index: u32) -> Result> { + let mut raw_value = ptr::null_mut(); + let status = unsafe { + sys::napi_get_element( + self.raw_env(), + self.raw_value(), + index, + &mut raw_value + ) + }; + check_status(status)?; + Value::::from_raw(self.env, raw_value).try_into() + } + + pub fn is_array(&self) -> Result { + let mut is_array = false; + let status = unsafe { + sys::napi_is_array( + self.raw_env(), + self.raw_value(), + &mut is_array, + ) + }; + check_status(status)?; + Ok(is_array) + } + + pub fn get_array_length(&self) -> Result { + if self.is_array()? != true { + return Err(Error { + status: Status::ArrayExpected, + }) + } + let mut length: u32 = 0; + let status = unsafe { + sys::napi_get_array_length( + self.raw_env(), + self.raw_value(), + &mut length + ) + }; + check_status(status)?; + Ok(length) + } + fn raw_value(&self) -> sys::napi_value { self.raw_value }