1use std::ops::{Deref, DerefMut};
8
9use mozjs::jsapi::{
10 IsSetObject, JSObject, NewSetObject, SetAdd, SetClear, SetDelete, SetEntries, SetForEach, SetHas, SetKeys, SetSize,
11};
12
13use crate::conversions::ToValue as _;
14use crate::{Context, Function, Local, Object, Value};
15
16pub struct Set<'s> {
17 set: Local<'s, *mut JSObject>,
18}
19
20impl<'s> Set<'s> {
21 pub fn new(cx: &'s Context) -> Set<'s> {
23 Set {
24 set: cx.root(unsafe { NewSetObject(cx.as_ptr()) }),
25 }
26 }
27
28 pub fn from(cx: &Context, object: Local<'s, *mut JSObject>) -> Option<Set<'s>> {
32 Set::is_set(cx, &object).then_some(Set { set: object })
33 }
34
35 pub unsafe fn from_unchecked(object: Local<'s, *mut JSObject>) -> Set<'s> {
40 Set { set: object }
41 }
42
43 pub fn size(&self, cx: &Context) -> u32 {
45 unsafe { SetSize(cx.as_ptr(), self.handle().into()) }
46 }
47
48 pub fn has(&self, cx: &Context, key: &Value) -> bool {
50 let mut has = false;
51 unsafe { SetHas(cx.as_ptr(), self.handle().into(), key.handle().into(), &raw mut has) && has }
52 }
53
54 pub fn add(&self, cx: &Context, key: &Value) -> bool {
56 unsafe { SetAdd(cx.as_ptr(), self.handle().into(), key.handle().into()) }
57 }
58
59 pub fn delete(&self, cx: &Context, key: &Value) -> bool {
61 let mut rval = false;
62 unsafe { SetDelete(cx.as_ptr(), self.handle().into(), key.handle().into(), &raw mut rval) && rval }
63 }
64
65 pub fn clear(&self, cx: &Context) -> bool {
67 unsafe { SetClear(cx.as_ptr(), self.handle().into()) }
68 }
69
70 pub fn keys<'cx>(&self, cx: &'cx Context) -> Object<'cx> {
72 let mut keys = Value::undefined(cx);
73 unsafe {
74 SetKeys(cx.as_ptr(), self.handle().into(), keys.handle_mut().into());
75 }
76 keys.to_object(cx)
77 }
78
79 pub fn entries<'cx>(&self, cx: &'cx Context) -> Object<'cx> {
82 let mut entries = Value::undefined(cx);
83 unsafe {
84 SetEntries(cx.as_ptr(), self.handle().into(), entries.handle_mut().into());
85 }
86 entries.to_object(cx)
87 }
88
89 pub fn for_each(&self, cx: &Context, callback: &Function, this: &Object) -> bool {
91 unsafe {
92 SetForEach(
93 cx.as_ptr(),
94 self.handle().into(),
95 callback.as_value(cx).handle().into(),
96 this.as_value(cx).handle().into(),
97 )
98 }
99 }
100
101 pub fn is_set(cx: &Context, object: &Local<*mut JSObject>) -> bool {
103 let mut set = false;
104 unsafe { IsSetObject(cx.as_ptr(), object.handle().into(), &raw mut set) && set }
105 }
106}
107
108impl<'s> Deref for Set<'s> {
109 type Target = Local<'s, *mut JSObject>;
110
111 fn deref(&self) -> &Self::Target {
112 &self.set
113 }
114}
115
116impl DerefMut for Set<'_> {
117 fn deref_mut(&mut self) -> &mut Self::Target {
118 &mut self.set
119 }
120}