1use crate::object::{alloc_ctor, ctor_get, ctor_set, Obj, TAG_ARRAY};
9use crate::object::{box_, dec, inc, is_scalar, unbox};
10use crate::object::{held, held_mut, hold, take_held, Hold};
11
12impl Hold for Vec<Obj> {
14 const TAG: u8 = TAG_ARRAY;
15
16 unsafe fn copy(&self) -> Vec<Obj> {
17 for &item in self {
18 inc(item);
19 }
20 self.clone()
21 }
22}
23
24pub(crate) unsafe fn free(o: Obj) {
26 drop(take_held::<Vec<Obj>>(o));
27}
28
29pub fn from_vec(items: Vec<Obj>) -> Obj {
31 hold(items)
32}
33
34pub unsafe fn into_vec(a: Obj) -> Vec<Obj> {
37 take_held(a)
38}
39
40pub unsafe fn elements<'a>(a: Obj) -> &'a [Obj] {
42 held::<Vec<Obj>>(a)
43}
44
45unsafe fn exclusive<'a>(a: Obj) -> (Obj, &'a mut Vec<Obj>) {
48 held_mut(a)
49}
50
51unsafe fn index(i: Obj, len: usize) -> Option<usize> {
53 (is_scalar(i) && unbox(i) < len).then(|| unbox(i))
54}
55
56#[cold]
57fn out_of_bounds() -> ! {
58 panic!("inlean: index out of bounds")
59}
60
61pub unsafe fn array_size(a: Obj) -> usize {
62 elements(a).len()
63}
64
65pub unsafe fn array_get_size(a: Obj) -> Obj {
66 box_(elements(a).len())
67}
68
69pub unsafe fn mk_empty_array_with_capacity(capacity: Obj) -> Obj {
70 from_vec(Vec::with_capacity(if is_scalar(capacity) { unbox(capacity).min(1 << 20) } else { 0 }))
71}
72
73pub unsafe fn array_mk(list: Obj) -> Obj {
75 let mut items = Vec::new();
76 let mut rest = list;
77 while !is_scalar(rest) {
78 let head = ctor_get(rest, 0);
79 inc(head);
80 items.push(head);
81 rest = ctor_get(rest, 1);
82 }
83 dec(list);
84 from_vec(items)
85}
86
87pub unsafe fn array_to_list(a: Obj) -> Obj {
89 let mut list = box_(0);
90 for &item in elements(a).iter().rev() {
91 inc(item);
92 let cell = alloc_ctor(1, 2, 0, 0);
93 ctor_set(cell, 0, item);
94 ctor_set(cell, 1, list);
95 list = cell;
96 }
97 dec(a);
98 list
99}
100
101pub unsafe fn array_uget_borrowed(a: Obj, i: usize) -> Obj {
102 elements(a)[i]
103}
104
105pub unsafe fn array_uget(a: Obj, i: usize) -> Obj {
106 let item = elements(a)[i];
107 inc(item);
108 item
109}
110
111pub unsafe fn array_fget_borrowed(a: Obj, i: Obj) -> Obj {
112 elements(a)[unbox(i)]
113}
114
115pub unsafe fn array_fget(a: Obj, i: Obj) -> Obj {
116 array_uget(a, unbox(i))
117}
118
119pub unsafe fn array_get_borrowed(_default: Obj, a: Obj, i: Obj) -> Obj {
121 match index(i, elements(a).len()) {
122 Some(i) => elements(a)[i],
123 None => out_of_bounds(),
124 }
125}
126
127pub unsafe fn array_get(default: Obj, a: Obj, i: Obj) -> Obj {
128 let item = array_get_borrowed(default, a, i);
129 inc(item);
130 item
131}
132
133pub unsafe fn array_uset(a: Obj, i: usize, value: Obj) -> Obj {
134 let (a, items) = exclusive(a);
135 dec(std::mem::replace(&mut items[i], value));
136 a
137}
138
139pub unsafe fn array_fset(a: Obj, i: Obj, value: Obj) -> Obj {
140 array_uset(a, unbox(i), value)
141}
142
143pub unsafe fn array_set(a: Obj, i: Obj, value: Obj) -> Obj {
145 match index(i, elements(a).len()) {
146 Some(i) => array_uset(a, i, value),
147 None => out_of_bounds(),
148 }
149}
150
151pub unsafe fn array_push(a: Obj, value: Obj) -> Obj {
152 let (a, items) = exclusive(a);
153 items.push(value);
154 a
155}
156
157pub unsafe fn array_pop(a: Obj) -> Obj {
159 let (a, items) = exclusive(a);
160 if let Some(last) = items.pop() {
161 dec(last);
162 }
163 a
164}
165
166pub unsafe fn array_uswap(a: Obj, i: usize, j: usize) -> Obj {
167 let (a, items) = exclusive(a);
168 items.swap(i, j);
169 a
170}
171
172pub unsafe fn array_fswap(a: Obj, i: Obj, j: Obj) -> Obj {
173 array_uswap(a, unbox(i), unbox(j))
174}
175
176pub unsafe fn array_swap(a: Obj, i: Obj, j: Obj) -> Obj {
178 let len = elements(a).len();
179 match (index(i, len), index(j, len)) {
180 (Some(i), Some(j)) => array_uswap(a, i, j),
181 _ => a,
182 }
183}
184
185pub unsafe fn mk_array(n: Obj, value: Obj) -> Obj {
187 if !is_scalar(n) {
188 panic!("inlean: out of memory: the array is too large");
189 }
190 let n = unbox(n);
191 match n {
192 0 => dec(value),
193 _ => crate::object::inc_n(value, n as u32 - 1),
194 }
195 dec(box_(n));
196 from_vec(vec![value; n])
197}
198
199pub unsafe fn array_mark_linear(a: Obj) -> Obj {
202 a
203}
204
205pub unsafe fn array_propagate_mark(_source: Obj, target: Obj) -> Obj {
207 target
208}
209
210pub(crate) const OPS: &[&str] = &[
213 "array_size", "array_get_size", "mk_empty_array_with_capacity", "array_mk", "array_to_list",
214 "array_uget", "array_uget_borrowed", "array_fget", "array_fget_borrowed", "array_get", "array_get_borrowed",
215 "array_uset", "array_fset", "array_set", "array_push", "array_pop", "array_uswap", "array_fswap",
216 "array_swap", "mk_array", "array_mark_linear", "array_propagate_mark",
217];