1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
use crate::{api, prelude::*};
use std::{ffi::CString, mem::MaybeUninit, os::raw::c_char};

#[derive(Copy, Clone, Debug)]
pub struct JsObject(pub(crate) JsValue);

impl JsObject {
    pub(crate) fn from_value(value: JsValue) -> JsObject {
        JsObject(value)
    }

    pub fn new(env: NapiEnv) -> NapiResult<JsObject> {
        let value = napi_call!(=napi_create_object, env);
        Ok(JsObject(JsValue::from_raw(env, value)))
    }

    /// This method is equivalent to calling napi_get_property with a napi_value created from the
    /// string passed in as utf8Name.
    pub fn set_named_property(
        &mut self,
        key: impl AsRef<str>,
        value: impl NapiValueT,
    ) -> NapiResult<()> {
        let name = CString::new(key.as_ref()).map_err(|_| NapiStatus::StringExpected)?;
        napi_call!(
            napi_set_named_property,
            self.env(),
            self.raw(),
            name.as_ptr(),
            value.raw(),
        )
    }

    /// This API returns the names of the enumerable properties of object as an array of strings.
    /// The properties of object whose key is a symbol will not be included.
    pub fn get_property_names(&self) -> NapiResult<JsArray> {
        let names = napi_call!(=napi_get_property_names, self.env(), self.raw());
        Ok(JsArray::from_raw(self.env(), names))
    }

    #[cfg(feature = "v6")]
    /// This API returns an array containing the names of the available properties of this object.
    pub fn get_all_property_names(
        &self,
        mode: NapiKeyCollectionMode,
        filter: NapiKeyFilter,
        conversion: NapiKeyConversion,
    ) -> NapiResult<JsArray> {
        let names = napi_call!(
            =napi_get_all_property_names,
            self.env(),
            self.raw(),
            mode as _,
            filter,
            conversion as _,
        );
        Ok(JsArray::from_raw(self.env(), names))
    }

    /// Set value by string-like key.
    pub fn set<T: NapiValueT>(&mut self, key: impl AsRef<str>, value: T) -> NapiResult<()> {
        let name = self.env().string(key.as_ref())?;
        let value = self.set_property(name, value)?;
        Ok(())
    }

    /// This API set a property on the Object passed in.
    pub fn set_property(&mut self, key: impl NapiValueT, value: impl NapiValueT) -> NapiResult<()> {
        napi_call!(
            napi_set_property,
            self.env(),
            self.raw(),
            key.raw(),
            value.raw(),
        )
    }

    /// This API gets the requested property from the Object passed in.
    pub fn get_property(&self, key: impl NapiValueT) -> NapiResult<JsValue> {
        let value = napi_call!(=napi_get_property, self.env(), self.raw(), key.raw());
        Ok(JsValue::from_raw(self.env(), value))
    }

    /// Get value by string-like key.
    pub fn get<T: NapiValueT>(&self, key: impl AsRef<str>) -> NapiResult<T> {
        let name = self.env().string(key.as_ref())?;
        let value = self.get_property(name)?;
        Ok(T::from_raw(self.env(), value.raw()))
    }

    /// This API checks if the Object passed in has the named property.
    pub fn has_property(&self, key: impl NapiValueT) -> NapiResult<bool> {
        Ok(napi_call!(=napi_has_property, self.env(), self.raw(), key.raw()))
    }

    /// This API attempts to delete the key own property from object.
    pub fn delete_property(&self, key: impl NapiValueT) -> NapiResult<bool> {
        Ok(napi_call!(=napi_delete_property, self.env(), self.raw(), key.raw()))
    }

    /// This API checks if the Object passed in has the named own property. key must be a string or
    /// a symbol, or an error will be thrown. Node-API will not perform any conversion between data
    /// types.
    pub fn has_own_property(
        &mut self,
        key: impl NapiValueT,
        value: impl NapiValueT,
    ) -> NapiResult<bool> {
        Ok(napi_call!(
            =napi_has_own_property,
            self.env(),
            self.raw(),
            key.raw(),
        ))
    }

    /// This method is equivalent to calling napi_get_property with a napi_value created
    /// from the string passed in as utf8Name.
    pub fn get_named_property(&self, key: impl AsRef<str>) -> NapiResult<JsValue> {
        let name = napi_s!(key.as_ref())?;
        let value = napi_call!(
            =napi_get_named_property,
            self.env(),
            self.raw(),
            name.as_ptr(),
        );
        Ok(JsValue::from_raw(self.env(), value))
    }

    /// This method is equivalent to calling napi_has_property with a napi_value created from the
    /// string passed in as utf8Name.
    pub fn has_named_property(&self, key: impl AsRef<str>) -> NapiResult<bool> {
        let name = napi_s!(key.as_ref())?;
        Ok(napi_call!(
            =napi_has_named_property,
            self.env(),
            self.raw(),
            name.as_ptr(),
        ))
    }

    /// This API sets an element on the Object passed in.
    pub fn set_element(&mut self, index: u32, value: impl NapiValueT) -> NapiResult<()> {
        napi_call!(napi_set_element, self.env(), self.raw(), index, value.raw())
    }

    /// This API gets the element at the requested index.
    pub fn get_element(&mut self, index: u32) -> NapiResult<JsValue> {
        let result = napi_call!(
            =napi_get_element,
            self.env(),
            self.raw(),
            index,
        );

        Ok(JsValue::from_raw(self.env(), result))
    }

    /// This API returns if the Object passed in has an element at the requested index.
    pub fn has_element(&mut self, index: u32) -> NapiResult<bool> {
        Ok(napi_call!(
            =napi_has_element,
            self.env(),
            self.raw(),
            index,
        ))
    }

    /// This API attempts to delete the specified index from object.
    pub fn delete_element(&mut self, index: u32) -> NapiResult<bool> {
        Ok(napi_call!(
            =napi_delete_element,
            self.env(),
            self.raw(),
            index,
        ))
    }

    #[cfg(feature = "v8")]
    #[doc = "Object.freeze()"]
    pub fn freeze(&mut self) -> NapiResult<()> {
        napi_call!(napi_object_freeze, self.env(), self.raw())
    }

    #[cfg(feature = "v8")]
    #[doc = "Object.seal()"]
    pub fn seal(&mut self) -> NapiResult<()> {
        napi_call!(napi_object_seal, self.env(), self.raw())
    }
}

napi_value_t!(JsObject);

impl NapiValueCheck for JsObject {
    fn check(&self) -> NapiResult<bool> {
        Ok(self.kind()? == NapiValuetype::Object)
    }
}