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
//! A Rust API wrapper for Boa's `SetIterator` Builtin ECMAScript Object
use std::ops::Deref;

use boa_gc::{Finalize, Trace};

use crate::{
    builtins::set::SetIterator,
    error::JsNativeError,
    object::{JsObject, JsObjectType},
    value::TryFromJs,
    Context, JsResult, JsValue,
};

/// `JsSetIterator` provides a wrapper for Boa's implementation of the ECMAScript `SetIterator` object
#[derive(Debug, Clone, Finalize, Trace)]
pub struct JsSetIterator {
    inner: JsObject,
}

impl JsSetIterator {
    /// Create a `JsSetIterator` from a `JsObject`.
    /// If object is not a `SetIterator`, throw `TypeError`.
    pub fn from_object(object: JsObject) -> JsResult<Self> {
        if object.is::<SetIterator>() {
            Ok(Self { inner: object })
        } else {
            Err(JsNativeError::typ()
                .with_message("object is not a SetIterator")
                .into())
        }
    }
    /// Advances the `JsSetIterator` and gets the next result in the `JsSet`.
    pub fn next(&self, context: &mut Context) -> JsResult<JsValue> {
        SetIterator::next(&self.inner.clone().into(), &[JsValue::Null], context)
    }
}

impl From<JsSetIterator> for JsObject {
    #[inline]
    fn from(o: JsSetIterator) -> Self {
        o.inner.clone()
    }
}

impl From<JsSetIterator> for JsValue {
    #[inline]
    fn from(o: JsSetIterator) -> Self {
        o.inner.clone().into()
    }
}

impl Deref for JsSetIterator {
    type Target = JsObject;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl JsObjectType for JsSetIterator {}

impl TryFromJs for JsSetIterator {
    fn try_from_js(value: &JsValue, _context: &mut Context) -> JsResult<Self> {
        match value {
            JsValue::Object(o) => Self::from_object(o.clone()),
            _ => Err(JsNativeError::typ()
                .with_message("value is not a SetIterator object")
                .into()),
        }
    }
}