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
use error::Result;
use ffi;
use object::Object;
use std::marker::PhantomData;
use types::Ref;
use util::protect_duktape_closure;
use value::{FromValue, ToValue};

/// Reference to a JavaScript array.
#[derive(Clone, Debug)]
pub struct Array<'ducc>(pub(crate) Ref<'ducc>);

impl<'ducc> Array<'ducc> {
    /// Consumes the array and returns it as a JavaScript object. This is inexpensive, since an
    /// array *is* an object.
    pub fn into_object(self) -> Object<'ducc> {
        Object(self.0)
    }

    /// Get the value using the given array index. Returns `Value::Undefined` if no element at the
    /// index exists.
    ///
    /// # Errors
    ///
    /// This function returns an error if:
    ///
    /// * `FromValue::from_value` fails for the element
    pub fn get<V: FromValue<'ducc>>(&self, index: u32) -> Result<V> {
        let ducc = self.0.ducc;
        let value = unsafe {
            assert_stack!(ducc.ctx, 0, {
                ducc.push_ref(&self.0);
                protect_duktape_closure(ducc.ctx, 1, 1, |ctx| {
                    ffi::duk_get_prop_index(ctx, -1, index);
                })?;
                ducc.pop_value()
            })
        };
        V::from_value(value, ducc)
    }

    /// Sets an array element using the given index and value.
    ///
    /// # Errors
    ///
    /// This function returns an error if:
    ///
    /// * `ToValue::to_value` fails for the value
    pub fn set<V: ToValue<'ducc>>(&self, index: u32, value: V) -> Result<()> {
        let ducc = self.0.ducc;
        let value = value.to_value(ducc)?;
        unsafe {
            assert_stack!(ducc.ctx, 0, {
                ducc.push_ref(&self.0);
                ducc.push_value(value);
                protect_duktape_closure(ducc.ctx, 2, 0, |ctx| {
                    ffi::duk_put_prop_index(ctx, -2, index);
                })
            })
        }
    }

    /// Returns the number of elements in the array using the calculation
    /// `Math.floor(ToNumber(array.length))`. This function can return an error if the `ToNumber`
    /// implementation fails or if the `length` getter fails.
    pub fn len(&self) -> Result<usize> {
        let ducc = self.0.ducc;
        unsafe {
            assert_stack!(ducc.ctx, 0, {
                ducc.push_ref(&self.0);
                protect_duktape_closure(ducc.ctx, 1, 0, |ctx| {
                    ffi::duk_get_length(ctx, -1)
                })
            })
        }
    }

    /// Pushes an element to the end of the array. This is a shortcut for `set` using `len` as the
    /// index.
    pub fn push<V: ToValue<'ducc>>(&self, value: V) -> Result<()> {
        self.set(self.len()? as u32, value)
    }

    /// Returns an iterator over the array's indexable values.
    pub fn elements<V: FromValue<'ducc>>(self) -> Elements<'ducc, V> {
        Elements {
            array: self,
            index: 0,
            len: None,
            _phantom: PhantomData,
        }
    }
}

pub struct Elements<'ducc, V> {
    array: Array<'ducc>,
    index: u32,
    len: Option<usize>,
    _phantom: PhantomData<V>,
}

impl<'ducc, V: FromValue<'ducc>> Iterator for Elements<'ducc, V> {
    type Item = Result<V>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.len.is_none() {
            self.len = Some(match self.array.len() {
                Ok(len) => len,
                Err(err) => return Some(Err(err)),
            });
        }

        if self.index as usize >= self.len.unwrap() {
            return None;
        }

        let result = self.array.get(self.index);
        self.index += 1;
        Some(result)
    }
}