1 /*
2  * This file is part of the MicroPython project, http://micropython.org/
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2013, 2014 Damien P. George
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  */
26 
27 #include <string.h>
28 #include <assert.h>
29 
30 #include "py/objlist.h"
31 #include "py/runtime.h"
32 #include "py/stackctrl.h"
33 
34 STATIC mp_obj_t mp_obj_new_list_iterator(mp_obj_t list, size_t cur, mp_obj_iter_buf_t *iter_buf);
35 STATIC mp_obj_list_t *list_new(size_t n);
36 STATIC mp_obj_t list_extend(mp_obj_t self_in, mp_obj_t arg_in);
37 STATIC mp_obj_t list_pop(size_t n_args, const mp_obj_t *args);
38 
39 // TODO: Move to mpconfig.h
40 #define LIST_MIN_ALLOC 4
41 
42 /******************************************************************************/
43 /* list                                                                       */
44 
list_print(const mp_print_t * print,mp_obj_t o_in,mp_print_kind_t kind)45 STATIC void list_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
46     mp_obj_list_t *o = MP_OBJ_TO_PTR(o_in);
47     const char *item_separator = ", ";
48     if (!(MICROPY_PY_UJSON && kind == PRINT_JSON)) {
49         kind = PRINT_REPR;
50     } else {
51         #if MICROPY_PY_UJSON_SEPARATORS
52         item_separator = MP_PRINT_GET_EXT(print)->item_separator;
53         #endif
54     }
55     mp_print_str(print, "[");
56     for (size_t i = 0; i < o->len; i++) {
57         if (i > 0) {
58             mp_print_str(print, item_separator);
59         }
60         mp_obj_print_helper(print, o->items[i], kind);
61     }
62     mp_print_str(print, "]");
63 }
64 
list_extend_from_iter(mp_obj_t list,mp_obj_t iterable)65 STATIC mp_obj_t list_extend_from_iter(mp_obj_t list, mp_obj_t iterable) {
66     mp_obj_t iter = mp_getiter(iterable, NULL);
67     mp_obj_t item;
68     while ((item = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
69         mp_obj_list_append(list, item);
70     }
71     return list;
72 }
73 
list_make_new(const mp_obj_type_t * type_in,size_t n_args,size_t n_kw,const mp_obj_t * args)74 STATIC mp_obj_t list_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
75     (void)type_in;
76     mp_arg_check_num(n_args, n_kw, 0, 1, false);
77 
78     switch (n_args) {
79         case 0:
80             // return a new, empty list
81             return mp_obj_new_list(0, NULL);
82 
83         case 1:
84         default: {
85             // make list from iterable
86             // TODO: optimize list/tuple
87             mp_obj_t list = mp_obj_new_list(0, NULL);
88             return list_extend_from_iter(list, args[0]);
89         }
90     }
91 }
92 
list_unary_op(mp_unary_op_t op,mp_obj_t self_in)93 STATIC mp_obj_t list_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
94     mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
95     switch (op) {
96         case MP_UNARY_OP_BOOL:
97             return mp_obj_new_bool(self->len != 0);
98         case MP_UNARY_OP_LEN:
99             return MP_OBJ_NEW_SMALL_INT(self->len);
100         #if MICROPY_PY_SYS_GETSIZEOF
101         case MP_UNARY_OP_SIZEOF: {
102             size_t sz = sizeof(*self) + sizeof(mp_obj_t) * self->alloc;
103             return MP_OBJ_NEW_SMALL_INT(sz);
104         }
105         #endif
106         default:
107             return MP_OBJ_NULL;      // op not supported
108     }
109 }
110 
list_binary_op(mp_binary_op_t op,mp_obj_t lhs,mp_obj_t rhs)111 STATIC mp_obj_t list_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs) {
112     mp_obj_list_t *o = MP_OBJ_TO_PTR(lhs);
113     switch (op) {
114         case MP_BINARY_OP_ADD: {
115             if (!mp_obj_is_type(rhs, &mp_type_list)) {
116                 return MP_OBJ_NULL; // op not supported
117             }
118             mp_obj_list_t *p = MP_OBJ_TO_PTR(rhs);
119             mp_obj_list_t *s = list_new(o->len + p->len);
120             mp_seq_cat(s->items, o->items, o->len, p->items, p->len, mp_obj_t);
121             return MP_OBJ_FROM_PTR(s);
122         }
123         case MP_BINARY_OP_INPLACE_ADD: {
124             list_extend(lhs, rhs);
125             return lhs;
126         }
127         case MP_BINARY_OP_MULTIPLY: {
128             mp_int_t n;
129             if (!mp_obj_get_int_maybe(rhs, &n)) {
130                 return MP_OBJ_NULL; // op not supported
131             }
132             if (n < 0) {
133                 n = 0;
134             }
135             mp_obj_list_t *s = list_new(o->len * n);
136             mp_seq_multiply(o->items, sizeof(*o->items), o->len, n, s->items);
137             return MP_OBJ_FROM_PTR(s);
138         }
139         case MP_BINARY_OP_EQUAL:
140         case MP_BINARY_OP_LESS:
141         case MP_BINARY_OP_LESS_EQUAL:
142         case MP_BINARY_OP_MORE:
143         case MP_BINARY_OP_MORE_EQUAL: {
144             if (!mp_obj_is_type(rhs, &mp_type_list)) {
145                 if (op == MP_BINARY_OP_EQUAL) {
146                     return mp_const_false;
147                 }
148                 return MP_OBJ_NULL; // op not supported
149             }
150 
151             mp_obj_list_t *another = MP_OBJ_TO_PTR(rhs);
152             bool res = mp_seq_cmp_objs(op, o->items, o->len, another->items, another->len);
153             return mp_obj_new_bool(res);
154         }
155 
156         default:
157             return MP_OBJ_NULL; // op not supported
158     }
159 }
160 
list_subscr(mp_obj_t self_in,mp_obj_t index,mp_obj_t value)161 STATIC mp_obj_t list_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
162     if (value == MP_OBJ_NULL) {
163         // delete
164         #if MICROPY_PY_BUILTINS_SLICE
165         if (mp_obj_is_type(index, &mp_type_slice)) {
166             mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
167             mp_bound_slice_t slice;
168             if (!mp_seq_get_fast_slice_indexes(self->len, index, &slice)) {
169                 mp_raise_NotImplementedError(NULL);
170             }
171 
172             mp_int_t len_adj = slice.start - slice.stop;
173             assert(len_adj <= 0);
174             mp_seq_replace_slice_no_grow(self->items, self->len, slice.start, slice.stop, self->items /*NULL*/, 0, sizeof(*self->items));
175             // Clear "freed" elements at the end of list
176             mp_seq_clear(self->items, self->len + len_adj, self->len, sizeof(*self->items));
177             self->len += len_adj;
178             return mp_const_none;
179         }
180         #endif
181         mp_obj_t args[2] = {self_in, index};
182         list_pop(2, args);
183         return mp_const_none;
184     } else if (value == MP_OBJ_SENTINEL) {
185         // load
186         mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
187         #if MICROPY_PY_BUILTINS_SLICE
188         if (mp_obj_is_type(index, &mp_type_slice)) {
189             mp_bound_slice_t slice;
190             if (!mp_seq_get_fast_slice_indexes(self->len, index, &slice)) {
191                 return mp_seq_extract_slice(self->len, self->items, &slice);
192             }
193             mp_obj_list_t *res = list_new(slice.stop - slice.start);
194             mp_seq_copy(res->items, self->items + slice.start, res->len, mp_obj_t);
195             return MP_OBJ_FROM_PTR(res);
196         }
197         #endif
198         size_t index_val = mp_get_index(self->base.type, self->len, index, false);
199         return self->items[index_val];
200     } else {
201         #if MICROPY_PY_BUILTINS_SLICE
202         if (mp_obj_is_type(index, &mp_type_slice)) {
203             mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
204             size_t value_len;
205             mp_obj_t *value_items;
206             mp_obj_get_array(value, &value_len, &value_items);
207             mp_bound_slice_t slice_out;
208             if (!mp_seq_get_fast_slice_indexes(self->len, index, &slice_out)) {
209                 mp_raise_NotImplementedError(NULL);
210             }
211             mp_int_t len_adj = value_len - (slice_out.stop - slice_out.start);
212             if (len_adj > 0) {
213                 if (self->len + len_adj > self->alloc) {
214                     // TODO: Might optimize memory copies here by checking if block can
215                     // be grown inplace or not
216                     self->items = m_renew(mp_obj_t, self->items, self->alloc, self->len + len_adj);
217                     self->alloc = self->len + len_adj;
218                 }
219                 mp_seq_replace_slice_grow_inplace(self->items, self->len,
220                     slice_out.start, slice_out.stop, value_items, value_len, len_adj, sizeof(*self->items));
221             } else {
222                 mp_seq_replace_slice_no_grow(self->items, self->len,
223                     slice_out.start, slice_out.stop, value_items, value_len, sizeof(*self->items));
224                 // Clear "freed" elements at the end of list
225                 mp_seq_clear(self->items, self->len + len_adj, self->len, sizeof(*self->items));
226                 // TODO: apply allocation policy re: alloc_size
227             }
228             self->len += len_adj;
229             return mp_const_none;
230         }
231         #endif
232         mp_obj_list_store(self_in, index, value);
233         return mp_const_none;
234     }
235 }
236 
list_getiter(mp_obj_t o_in,mp_obj_iter_buf_t * iter_buf)237 STATIC mp_obj_t list_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) {
238     return mp_obj_new_list_iterator(o_in, 0, iter_buf);
239 }
240 
mp_obj_list_append(mp_obj_t self_in,mp_obj_t arg)241 mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg) {
242     mp_check_self(mp_obj_is_type(self_in, &mp_type_list));
243     mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
244     if (self->len >= self->alloc) {
245         self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc * 2);
246         self->alloc *= 2;
247         mp_seq_clear(self->items, self->len + 1, self->alloc, sizeof(*self->items));
248     }
249     self->items[self->len++] = arg;
250     return mp_const_none; // return None, as per CPython
251 }
252 
list_extend(mp_obj_t self_in,mp_obj_t arg_in)253 STATIC mp_obj_t list_extend(mp_obj_t self_in, mp_obj_t arg_in) {
254     mp_check_self(mp_obj_is_type(self_in, &mp_type_list));
255     if (mp_obj_is_type(arg_in, &mp_type_list)) {
256         mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
257         mp_obj_list_t *arg = MP_OBJ_TO_PTR(arg_in);
258 
259         if (self->len + arg->len > self->alloc) {
260             // TODO: use alloc policy for "4"
261             self->items = m_renew(mp_obj_t, self->items, self->alloc, self->len + arg->len + 4);
262             self->alloc = self->len + arg->len + 4;
263             mp_seq_clear(self->items, self->len + arg->len, self->alloc, sizeof(*self->items));
264         }
265 
266         memcpy(self->items + self->len, arg->items, sizeof(mp_obj_t) * arg->len);
267         self->len += arg->len;
268     } else {
269         list_extend_from_iter(self_in, arg_in);
270     }
271     return mp_const_none; // return None, as per CPython
272 }
273 
list_pop(size_t n_args,const mp_obj_t * args)274 STATIC mp_obj_t list_pop(size_t n_args, const mp_obj_t *args) {
275     mp_check_self(mp_obj_is_type(args[0], &mp_type_list));
276     mp_obj_list_t *self = MP_OBJ_TO_PTR(args[0]);
277     if (self->len == 0) {
278         mp_raise_msg(&mp_type_IndexError, MP_ERROR_TEXT("pop from empty list"));
279     }
280     size_t index = mp_get_index(self->base.type, self->len, n_args == 1 ? MP_OBJ_NEW_SMALL_INT(-1) : args[1], false);
281     mp_obj_t ret = self->items[index];
282     self->len -= 1;
283     memmove(self->items + index, self->items + index + 1, (self->len - index) * sizeof(mp_obj_t));
284     // Clear stale pointer from slot which just got freed to prevent GC issues
285     self->items[self->len] = MP_OBJ_NULL;
286     if (self->alloc > LIST_MIN_ALLOC && self->alloc > 2 * self->len) {
287         self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc / 2);
288         self->alloc /= 2;
289     }
290     return ret;
291 }
292 
mp_quicksort(mp_obj_t * head,mp_obj_t * tail,mp_obj_t key_fn,mp_obj_t binop_less_result)293 STATIC void mp_quicksort(mp_obj_t *head, mp_obj_t *tail, mp_obj_t key_fn, mp_obj_t binop_less_result) {
294     MP_STACK_CHECK();
295     while (head < tail) {
296         mp_obj_t *h = head - 1;
297         mp_obj_t *t = tail;
298         mp_obj_t v = key_fn == MP_OBJ_NULL ? tail[0] : mp_call_function_1(key_fn, tail[0]); // get pivot using key_fn
299         for (;;) {
300             do {++h;
301             } while (h < t && mp_binary_op(MP_BINARY_OP_LESS, key_fn == MP_OBJ_NULL ? h[0] : mp_call_function_1(key_fn, h[0]), v) == binop_less_result);
302             do {--t;
303             } while (h < t && mp_binary_op(MP_BINARY_OP_LESS, v, key_fn == MP_OBJ_NULL ? t[0] : mp_call_function_1(key_fn, t[0])) == binop_less_result);
304             if (h >= t) {
305                 break;
306             }
307             mp_obj_t x = h[0];
308             h[0] = t[0];
309             t[0] = x;
310         }
311         mp_obj_t x = h[0];
312         h[0] = tail[0];
313         tail[0] = x;
314         // do the smaller recursive call first, to keep stack within O(log(N))
315         if (t - head < tail - h - 1) {
316             mp_quicksort(head, t, key_fn, binop_less_result);
317             head = h + 1;
318         } else {
319             mp_quicksort(h + 1, tail, key_fn, binop_less_result);
320             tail = t;
321         }
322     }
323 }
324 
325 // TODO Python defines sort to be stable but ours is not
mp_obj_list_sort(size_t n_args,const mp_obj_t * pos_args,mp_map_t * kw_args)326 mp_obj_t mp_obj_list_sort(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
327     static const mp_arg_t allowed_args[] = {
328         { MP_QSTR_key, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
329         { MP_QSTR_reverse, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
330     };
331 
332     // parse args
333     struct {
334         mp_arg_val_t key, reverse;
335     } args;
336     mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args,
337         MP_ARRAY_SIZE(allowed_args), allowed_args, (mp_arg_val_t *)&args);
338 
339     mp_check_self(mp_obj_is_type(pos_args[0], &mp_type_list));
340     mp_obj_list_t *self = MP_OBJ_TO_PTR(pos_args[0]);
341 
342     if (self->len > 1) {
343         mp_quicksort(self->items, self->items + self->len - 1,
344             args.key.u_obj == mp_const_none ? MP_OBJ_NULL : args.key.u_obj,
345             args.reverse.u_bool ? mp_const_false : mp_const_true);
346     }
347 
348     return mp_const_none;
349 }
350 
list_clear(mp_obj_t self_in)351 STATIC mp_obj_t list_clear(mp_obj_t self_in) {
352     mp_check_self(mp_obj_is_type(self_in, &mp_type_list));
353     mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
354     self->len = 0;
355     self->items = m_renew(mp_obj_t, self->items, self->alloc, LIST_MIN_ALLOC);
356     self->alloc = LIST_MIN_ALLOC;
357     mp_seq_clear(self->items, 0, self->alloc, sizeof(*self->items));
358     return mp_const_none;
359 }
360 
list_copy(mp_obj_t self_in)361 STATIC mp_obj_t list_copy(mp_obj_t self_in) {
362     mp_check_self(mp_obj_is_type(self_in, &mp_type_list));
363     mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
364     return mp_obj_new_list(self->len, self->items);
365 }
366 
list_count(mp_obj_t self_in,mp_obj_t value)367 STATIC mp_obj_t list_count(mp_obj_t self_in, mp_obj_t value) {
368     mp_check_self(mp_obj_is_type(self_in, &mp_type_list));
369     mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
370     return mp_seq_count_obj(self->items, self->len, value);
371 }
372 
list_index(size_t n_args,const mp_obj_t * args)373 STATIC mp_obj_t list_index(size_t n_args, const mp_obj_t *args) {
374     mp_check_self(mp_obj_is_type(args[0], &mp_type_list));
375     mp_obj_list_t *self = MP_OBJ_TO_PTR(args[0]);
376     return mp_seq_index_obj(self->items, self->len, n_args, args);
377 }
378 
list_insert(mp_obj_t self_in,mp_obj_t idx,mp_obj_t obj)379 STATIC mp_obj_t list_insert(mp_obj_t self_in, mp_obj_t idx, mp_obj_t obj) {
380     mp_check_self(mp_obj_is_type(self_in, &mp_type_list));
381     mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
382     // insert has its own strange index logic
383     mp_int_t index = MP_OBJ_SMALL_INT_VALUE(idx);
384     if (index < 0) {
385         index += self->len;
386     }
387     if (index < 0) {
388         index = 0;
389     }
390     if ((size_t)index > self->len) {
391         index = self->len;
392     }
393 
394     mp_obj_list_append(self_in, mp_const_none);
395 
396     for (mp_int_t i = self->len - 1; i > index; i--) {
397         self->items[i] = self->items[i - 1];
398     }
399     self->items[index] = obj;
400 
401     return mp_const_none;
402 }
403 
mp_obj_list_remove(mp_obj_t self_in,mp_obj_t value)404 mp_obj_t mp_obj_list_remove(mp_obj_t self_in, mp_obj_t value) {
405     mp_check_self(mp_obj_is_type(self_in, &mp_type_list));
406     mp_obj_t args[] = {self_in, value};
407     args[1] = list_index(2, args);
408     list_pop(2, args);
409 
410     return mp_const_none;
411 }
412 
list_reverse(mp_obj_t self_in)413 STATIC mp_obj_t list_reverse(mp_obj_t self_in) {
414     mp_check_self(mp_obj_is_type(self_in, &mp_type_list));
415     mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
416 
417     mp_int_t len = self->len;
418     for (mp_int_t i = 0; i < len / 2; i++) {
419         mp_obj_t a = self->items[i];
420         self->items[i] = self->items[len - i - 1];
421         self->items[len - i - 1] = a;
422     }
423 
424     return mp_const_none;
425 }
426 
427 STATIC MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append);
428 STATIC MP_DEFINE_CONST_FUN_OBJ_2(list_extend_obj, list_extend);
429 STATIC MP_DEFINE_CONST_FUN_OBJ_1(list_clear_obj, list_clear);
430 STATIC MP_DEFINE_CONST_FUN_OBJ_1(list_copy_obj, list_copy);
431 STATIC MP_DEFINE_CONST_FUN_OBJ_2(list_count_obj, list_count);
432 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_index_obj, 2, 4, list_index);
433 STATIC MP_DEFINE_CONST_FUN_OBJ_3(list_insert_obj, list_insert);
434 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop);
435 STATIC MP_DEFINE_CONST_FUN_OBJ_2(list_remove_obj, mp_obj_list_remove);
436 STATIC MP_DEFINE_CONST_FUN_OBJ_1(list_reverse_obj, list_reverse);
437 STATIC MP_DEFINE_CONST_FUN_OBJ_KW(list_sort_obj, 1, mp_obj_list_sort);
438 
439 STATIC const mp_rom_map_elem_t list_locals_dict_table[] = {
440     { MP_ROM_QSTR(MP_QSTR_append), MP_ROM_PTR(&list_append_obj) },
441     { MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&list_clear_obj) },
442     { MP_ROM_QSTR(MP_QSTR_copy), MP_ROM_PTR(&list_copy_obj) },
443     { MP_ROM_QSTR(MP_QSTR_count), MP_ROM_PTR(&list_count_obj) },
444     { MP_ROM_QSTR(MP_QSTR_extend), MP_ROM_PTR(&list_extend_obj) },
445     { MP_ROM_QSTR(MP_QSTR_index), MP_ROM_PTR(&list_index_obj) },
446     { MP_ROM_QSTR(MP_QSTR_insert), MP_ROM_PTR(&list_insert_obj) },
447     { MP_ROM_QSTR(MP_QSTR_pop), MP_ROM_PTR(&list_pop_obj) },
448     { MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&list_remove_obj) },
449     { MP_ROM_QSTR(MP_QSTR_reverse), MP_ROM_PTR(&list_reverse_obj) },
450     { MP_ROM_QSTR(MP_QSTR_sort), MP_ROM_PTR(&list_sort_obj) },
451 };
452 
453 STATIC MP_DEFINE_CONST_DICT(list_locals_dict, list_locals_dict_table);
454 
455 const mp_obj_type_t mp_type_list = {
456     { &mp_type_type },
457     .name = MP_QSTR_list,
458     .print = list_print,
459     .make_new = list_make_new,
460     .unary_op = list_unary_op,
461     .binary_op = list_binary_op,
462     .subscr = list_subscr,
463     .getiter = list_getiter,
464     .locals_dict = (mp_obj_dict_t *)&list_locals_dict,
465 };
466 
mp_obj_list_init(mp_obj_list_t * o,size_t n)467 void mp_obj_list_init(mp_obj_list_t *o, size_t n) {
468     o->base.type = &mp_type_list;
469     o->alloc = n < LIST_MIN_ALLOC ? LIST_MIN_ALLOC : n;
470     o->len = n;
471     o->items = m_new(mp_obj_t, o->alloc);
472     mp_seq_clear(o->items, n, o->alloc, sizeof(*o->items));
473 }
474 
list_new(size_t n)475 STATIC mp_obj_list_t *list_new(size_t n) {
476     mp_obj_list_t *o = m_new_obj(mp_obj_list_t);
477     mp_obj_list_init(o, n);
478     return o;
479 }
480 
mp_obj_new_list(size_t n,mp_obj_t * items)481 mp_obj_t mp_obj_new_list(size_t n, mp_obj_t *items) {
482     mp_obj_list_t *o = list_new(n);
483     if (items != NULL) {
484         for (size_t i = 0; i < n; i++) {
485             o->items[i] = items[i];
486         }
487     }
488     return MP_OBJ_FROM_PTR(o);
489 }
490 
mp_obj_list_get(mp_obj_t self_in,size_t * len,mp_obj_t ** items)491 void mp_obj_list_get(mp_obj_t self_in, size_t *len, mp_obj_t **items) {
492     mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
493     *len = self->len;
494     *items = self->items;
495 }
496 
mp_obj_list_set_len(mp_obj_t self_in,size_t len)497 void mp_obj_list_set_len(mp_obj_t self_in, size_t len) {
498     // trust that the caller knows what it's doing
499     // TODO realloc if len got much smaller than alloc
500     mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
501     self->len = len;
502 }
503 
mp_obj_list_store(mp_obj_t self_in,mp_obj_t index,mp_obj_t value)504 void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
505     mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
506     size_t i = mp_get_index(self->base.type, self->len, index, false);
507     self->items[i] = value;
508 }
509 
510 /******************************************************************************/
511 /* list iterator                                                              */
512 
513 typedef struct _mp_obj_list_it_t {
514     mp_obj_base_t base;
515     mp_fun_1_t iternext;
516     mp_obj_t list;
517     size_t cur;
518 } mp_obj_list_it_t;
519 
list_it_iternext(mp_obj_t self_in)520 STATIC mp_obj_t list_it_iternext(mp_obj_t self_in) {
521     mp_obj_list_it_t *self = MP_OBJ_TO_PTR(self_in);
522     mp_obj_list_t *list = MP_OBJ_TO_PTR(self->list);
523     if (self->cur < list->len) {
524         mp_obj_t o_out = list->items[self->cur];
525         self->cur += 1;
526         return o_out;
527     } else {
528         return MP_OBJ_STOP_ITERATION;
529     }
530 }
531 
mp_obj_new_list_iterator(mp_obj_t list,size_t cur,mp_obj_iter_buf_t * iter_buf)532 mp_obj_t mp_obj_new_list_iterator(mp_obj_t list, size_t cur, mp_obj_iter_buf_t *iter_buf) {
533     assert(sizeof(mp_obj_list_it_t) <= sizeof(mp_obj_iter_buf_t));
534     mp_obj_list_it_t *o = (mp_obj_list_it_t *)iter_buf;
535     o->base.type = &mp_type_polymorph_iter;
536     o->iternext = list_it_iternext;
537     o->list = list;
538     o->cur = cur;
539     return MP_OBJ_FROM_PTR(o);
540 }
541