script/dom/
testbindingiterable.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5// check-tidy: no specs after this line
6
7use dom_struct::dom_struct;
8use js::rust::HandleObject;
9
10use crate::dom::bindings::cell::DomRefCell;
11use crate::dom::bindings::codegen::Bindings::TestBindingIterableBinding::TestBindingIterableMethods;
12use crate::dom::bindings::error::Fallible;
13use crate::dom::bindings::reflector::{Reflector, reflect_dom_object_with_proto};
14use crate::dom::bindings::root::DomRoot;
15use crate::dom::bindings::str::DOMString;
16use crate::dom::globalscope::GlobalScope;
17use crate::script_runtime::CanGc;
18
19#[dom_struct]
20pub(crate) struct TestBindingIterable {
21    reflector: Reflector,
22    vals: DomRefCell<Vec<DOMString>>,
23}
24
25impl TestBindingIterable {
26    fn new(
27        global: &GlobalScope,
28        proto: Option<HandleObject>,
29        can_gc: CanGc,
30    ) -> DomRoot<TestBindingIterable> {
31        reflect_dom_object_with_proto(
32            Box::new(TestBindingIterable {
33                reflector: Reflector::new(),
34                vals: DomRefCell::new(vec![]),
35            }),
36            global,
37            proto,
38            can_gc,
39        )
40    }
41}
42
43impl TestBindingIterableMethods<crate::DomTypeHolder> for TestBindingIterable {
44    fn Constructor(
45        global: &GlobalScope,
46        proto: Option<HandleObject>,
47        can_gc: CanGc,
48    ) -> Fallible<DomRoot<TestBindingIterable>> {
49        Ok(TestBindingIterable::new(global, proto, can_gc))
50    }
51
52    fn Add(&self, v: DOMString) {
53        self.vals.borrow_mut().push(v);
54    }
55    fn Length(&self) -> u32 {
56        self.vals.borrow().len() as u32
57    }
58    fn GetItem(&self, n: u32) -> DOMString {
59        self.IndexedGetter(n).unwrap_or_default()
60    }
61    fn IndexedGetter(&self, n: u32) -> Option<DOMString> {
62        self.vals.borrow().get(n as usize).cloned()
63    }
64}