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 | * Copyright (c) 2014 Paul Sokolovsky |
8 | * |
9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
10 | * of this software and associated documentation files (the "Software"), to deal |
11 | * in the Software without restriction, including without limitation the rights |
12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13 | * copies of the Software, and to permit persons to whom the Software is |
14 | * furnished to do so, subject to the following conditions: |
15 | * |
16 | * The above copyright notice and this permission notice shall be included in |
17 | * all copies or substantial portions of the Software. |
18 | * |
19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
25 | * THE SOFTWARE. |
26 | */ |
27 | |
28 | #include <string.h> |
29 | #include <assert.h> |
30 | #include <stdint.h> |
31 | |
32 | #include "py/runtime.h" |
33 | #include "py/binary.h" |
34 | #include "py/objstr.h" |
35 | #include "py/objarray.h" |
36 | |
37 | #if MICROPY_PY_ARRAY || MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_BUILTINS_MEMORYVIEW |
38 | |
39 | // About memoryview object: We want to reuse as much code as possible from |
40 | // array, and keep the memoryview object 4 words in size so it fits in 1 GC |
41 | // block. Also, memoryview must keep a pointer to the base of the buffer so |
42 | // that the buffer is not GC'd if the original parent object is no longer |
43 | // around (we are assuming that all memoryview'able objects return a pointer |
44 | // which points to the start of a GC chunk). Given the above constraints we |
45 | // do the following: |
46 | // - typecode high bit is set if the buffer is read-write (else read-only) |
47 | // - free is the offset in elements to the first item in the memoryview |
48 | // - len is the length in elements |
49 | // - items points to the start of the original buffer |
50 | // Note that we don't handle the case where the original buffer might change |
51 | // size due to a resize of the original parent object. |
52 | |
53 | #if MICROPY_PY_BUILTINS_MEMORYVIEW |
54 | #define TYPECODE_MASK (0x7f) |
55 | #define memview_offset free |
56 | #else |
57 | // make (& TYPECODE_MASK) a null operation if memorview not enabled |
58 | #define TYPECODE_MASK (~(size_t)0) |
59 | // memview_offset should not be accessed if memoryview is not enabled, |
60 | // so not defined to catch errors |
61 | #endif |
62 | |
63 | STATIC mp_obj_t array_iterator_new(mp_obj_t array_in, mp_obj_iter_buf_t *iter_buf); |
64 | STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg); |
65 | STATIC mp_obj_t array_extend(mp_obj_t self_in, mp_obj_t arg_in); |
66 | STATIC mp_int_t array_get_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, mp_uint_t flags); |
67 | |
68 | /******************************************************************************/ |
69 | // array |
70 | |
71 | #if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY |
72 | STATIC void array_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) { |
73 | (void)kind; |
74 | mp_obj_array_t *o = MP_OBJ_TO_PTR(o_in); |
75 | if (o->typecode == BYTEARRAY_TYPECODE) { |
76 | mp_print_str(print, "bytearray(b" ); |
77 | mp_str_print_quoted(print, o->items, o->len, true); |
78 | } else { |
79 | mp_printf(print, "array('%c'" , o->typecode); |
80 | if (o->len > 0) { |
81 | mp_print_str(print, ", [" ); |
82 | for (size_t i = 0; i < o->len; i++) { |
83 | if (i > 0) { |
84 | mp_print_str(print, ", " ); |
85 | } |
86 | mp_obj_print_helper(print, mp_binary_get_val_array(o->typecode, o->items, i), PRINT_REPR); |
87 | } |
88 | mp_print_str(print, "]" ); |
89 | } |
90 | } |
91 | mp_print_str(print, ")" ); |
92 | } |
93 | #endif |
94 | |
95 | #if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY |
96 | STATIC mp_obj_array_t *array_new(char typecode, size_t n) { |
97 | int typecode_size = mp_binary_get_size('@', typecode, NULL); |
98 | mp_obj_array_t *o = m_new_obj(mp_obj_array_t); |
99 | #if MICROPY_PY_BUILTINS_BYTEARRAY && MICROPY_PY_ARRAY |
100 | o->base.type = (typecode == BYTEARRAY_TYPECODE) ? &mp_type_bytearray : &mp_type_array; |
101 | #elif MICROPY_PY_BUILTINS_BYTEARRAY |
102 | o->base.type = &mp_type_bytearray; |
103 | #else |
104 | o->base.type = &mp_type_array; |
105 | #endif |
106 | o->typecode = typecode; |
107 | o->free = 0; |
108 | o->len = n; |
109 | o->items = m_new(byte, typecode_size * o->len); |
110 | return o; |
111 | } |
112 | #endif |
113 | |
114 | #if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY |
115 | STATIC mp_obj_t array_construct(char typecode, mp_obj_t initializer) { |
116 | // bytearrays can be raw-initialised from anything with the buffer protocol |
117 | // other arrays can only be raw-initialised from bytes and bytearray objects |
118 | mp_buffer_info_t bufinfo; |
119 | if (((MICROPY_PY_BUILTINS_BYTEARRAY |
120 | && typecode == BYTEARRAY_TYPECODE) |
121 | || (MICROPY_PY_ARRAY |
122 | && (mp_obj_is_type(initializer, &mp_type_bytes) |
123 | || (MICROPY_PY_BUILTINS_BYTEARRAY && mp_obj_is_type(initializer, &mp_type_bytearray))))) |
124 | && mp_get_buffer(initializer, &bufinfo, MP_BUFFER_READ)) { |
125 | // construct array from raw bytes |
126 | // we round-down the len to make it a multiple of sz (CPython raises error) |
127 | size_t sz = mp_binary_get_size('@', typecode, NULL); |
128 | size_t len = bufinfo.len / sz; |
129 | mp_obj_array_t *o = array_new(typecode, len); |
130 | memcpy(o->items, bufinfo.buf, len * sz); |
131 | return MP_OBJ_FROM_PTR(o); |
132 | } |
133 | |
134 | size_t len; |
135 | // Try to create array of exact len if initializer len is known |
136 | mp_obj_t len_in = mp_obj_len_maybe(initializer); |
137 | if (len_in == MP_OBJ_NULL) { |
138 | len = 0; |
139 | } else { |
140 | len = MP_OBJ_SMALL_INT_VALUE(len_in); |
141 | } |
142 | |
143 | mp_obj_array_t *array = array_new(typecode, len); |
144 | |
145 | mp_obj_t iterable = mp_getiter(initializer, NULL); |
146 | mp_obj_t item; |
147 | size_t i = 0; |
148 | while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { |
149 | if (len == 0) { |
150 | array_append(MP_OBJ_FROM_PTR(array), item); |
151 | } else { |
152 | mp_binary_set_val_array(typecode, array->items, i++, item); |
153 | } |
154 | } |
155 | |
156 | return MP_OBJ_FROM_PTR(array); |
157 | } |
158 | #endif |
159 | |
160 | #if MICROPY_PY_ARRAY |
161 | STATIC mp_obj_t array_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { |
162 | (void)type_in; |
163 | mp_arg_check_num(n_args, n_kw, 1, 2, false); |
164 | |
165 | // get typecode |
166 | const char *typecode = mp_obj_str_get_str(args[0]); |
167 | |
168 | if (n_args == 1) { |
169 | // 1 arg: make an empty array |
170 | return MP_OBJ_FROM_PTR(array_new(*typecode, 0)); |
171 | } else { |
172 | // 2 args: construct the array from the given object |
173 | return array_construct(*typecode, args[1]); |
174 | } |
175 | } |
176 | #endif |
177 | |
178 | #if MICROPY_PY_BUILTINS_BYTEARRAY |
179 | STATIC mp_obj_t bytearray_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { |
180 | (void)type_in; |
181 | // Can take 2nd/3rd arg if constructs from str |
182 | mp_arg_check_num(n_args, n_kw, 0, 3, false); |
183 | |
184 | if (n_args == 0) { |
185 | // no args: construct an empty bytearray |
186 | return MP_OBJ_FROM_PTR(array_new(BYTEARRAY_TYPECODE, 0)); |
187 | } else if (mp_obj_is_int(args[0])) { |
188 | // 1 arg, an integer: construct a blank bytearray of that length |
189 | mp_uint_t len = mp_obj_get_int(args[0]); |
190 | mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, len); |
191 | memset(o->items, 0, len); |
192 | return MP_OBJ_FROM_PTR(o); |
193 | } else { |
194 | // 1 arg: construct the bytearray from that |
195 | return array_construct(BYTEARRAY_TYPECODE, args[0]); |
196 | } |
197 | } |
198 | #endif |
199 | |
200 | #if MICROPY_PY_BUILTINS_MEMORYVIEW |
201 | |
202 | mp_obj_t mp_obj_new_memoryview(byte typecode, size_t nitems, void *items) { |
203 | mp_obj_array_t *self = m_new_obj(mp_obj_array_t); |
204 | self->base.type = &mp_type_memoryview; |
205 | self->typecode = typecode; |
206 | self->memview_offset = 0; |
207 | self->len = nitems; |
208 | self->items = items; |
209 | return MP_OBJ_FROM_PTR(self); |
210 | } |
211 | |
212 | STATIC mp_obj_t memoryview_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { |
213 | (void)type_in; |
214 | |
215 | // TODO possibly allow memoryview constructor to take start/stop so that one |
216 | // can do memoryview(b, 4, 8) instead of memoryview(b)[4:8] (uses less RAM) |
217 | |
218 | mp_arg_check_num(n_args, n_kw, 1, 1, false); |
219 | |
220 | mp_buffer_info_t bufinfo; |
221 | mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ); |
222 | |
223 | mp_obj_array_t *self = MP_OBJ_TO_PTR(mp_obj_new_memoryview(bufinfo.typecode, |
224 | bufinfo.len / mp_binary_get_size('@', bufinfo.typecode, NULL), |
225 | bufinfo.buf)); |
226 | |
227 | // test if the object can be written to |
228 | if (mp_get_buffer(args[0], &bufinfo, MP_BUFFER_RW)) { |
229 | self->typecode |= MP_OBJ_ARRAY_TYPECODE_FLAG_RW; // indicate writable buffer |
230 | } |
231 | |
232 | return MP_OBJ_FROM_PTR(self); |
233 | } |
234 | |
235 | #if MICROPY_PY_BUILTINS_MEMORYVIEW_ITEMSIZE |
236 | STATIC void memoryview_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { |
237 | if (dest[0] != MP_OBJ_NULL) { |
238 | return; |
239 | } |
240 | if (attr == MP_QSTR_itemsize) { |
241 | mp_obj_array_t *self = MP_OBJ_TO_PTR(self_in); |
242 | dest[0] = MP_OBJ_NEW_SMALL_INT(mp_binary_get_size('@', self->typecode & TYPECODE_MASK, NULL)); |
243 | } |
244 | } |
245 | #endif |
246 | |
247 | #endif |
248 | |
249 | STATIC mp_obj_t array_unary_op(mp_unary_op_t op, mp_obj_t o_in) { |
250 | mp_obj_array_t *o = MP_OBJ_TO_PTR(o_in); |
251 | switch (op) { |
252 | case MP_UNARY_OP_BOOL: |
253 | return mp_obj_new_bool(o->len != 0); |
254 | case MP_UNARY_OP_LEN: |
255 | return MP_OBJ_NEW_SMALL_INT(o->len); |
256 | default: |
257 | return MP_OBJ_NULL; // op not supported |
258 | } |
259 | } |
260 | |
261 | STATIC mp_obj_t array_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) { |
262 | mp_obj_array_t *lhs = MP_OBJ_TO_PTR(lhs_in); |
263 | switch (op) { |
264 | case MP_BINARY_OP_ADD: { |
265 | // allow to add anything that has the buffer protocol (extension to CPython) |
266 | mp_buffer_info_t lhs_bufinfo; |
267 | mp_buffer_info_t rhs_bufinfo; |
268 | array_get_buffer(lhs_in, &lhs_bufinfo, MP_BUFFER_READ); |
269 | mp_get_buffer_raise(rhs_in, &rhs_bufinfo, MP_BUFFER_READ); |
270 | |
271 | size_t sz = mp_binary_get_size('@', lhs_bufinfo.typecode, NULL); |
272 | |
273 | // convert byte count to element count (in case rhs is not multiple of sz) |
274 | size_t rhs_len = rhs_bufinfo.len / sz; |
275 | |
276 | // note: lhs->len is element count of lhs, lhs_bufinfo.len is byte count |
277 | mp_obj_array_t *res = array_new(lhs_bufinfo.typecode, lhs->len + rhs_len); |
278 | mp_seq_cat((byte *)res->items, lhs_bufinfo.buf, lhs_bufinfo.len, rhs_bufinfo.buf, rhs_len * sz, byte); |
279 | return MP_OBJ_FROM_PTR(res); |
280 | } |
281 | |
282 | case MP_BINARY_OP_INPLACE_ADD: { |
283 | #if MICROPY_PY_BUILTINS_MEMORYVIEW |
284 | if (lhs->base.type == &mp_type_memoryview) { |
285 | return MP_OBJ_NULL; // op not supported |
286 | } |
287 | #endif |
288 | array_extend(lhs_in, rhs_in); |
289 | return lhs_in; |
290 | } |
291 | |
292 | case MP_BINARY_OP_CONTAINS: { |
293 | #if MICROPY_PY_BUILTINS_BYTEARRAY |
294 | // Can search string only in bytearray |
295 | mp_buffer_info_t lhs_bufinfo; |
296 | mp_buffer_info_t rhs_bufinfo; |
297 | if (mp_get_buffer(rhs_in, &rhs_bufinfo, MP_BUFFER_READ)) { |
298 | if (!mp_obj_is_type(lhs_in, &mp_type_bytearray)) { |
299 | return mp_const_false; |
300 | } |
301 | array_get_buffer(lhs_in, &lhs_bufinfo, MP_BUFFER_READ); |
302 | return mp_obj_new_bool( |
303 | find_subbytes(lhs_bufinfo.buf, lhs_bufinfo.len, rhs_bufinfo.buf, rhs_bufinfo.len, 1) != NULL); |
304 | } |
305 | #endif |
306 | |
307 | // Otherwise, can only look for a scalar numeric value in an array |
308 | if (mp_obj_is_int(rhs_in) || mp_obj_is_float(rhs_in)) { |
309 | mp_raise_NotImplementedError(NULL); |
310 | } |
311 | |
312 | return mp_const_false; |
313 | } |
314 | |
315 | case MP_BINARY_OP_EQUAL: { |
316 | mp_buffer_info_t lhs_bufinfo; |
317 | mp_buffer_info_t rhs_bufinfo; |
318 | array_get_buffer(lhs_in, &lhs_bufinfo, MP_BUFFER_READ); |
319 | if (!mp_get_buffer(rhs_in, &rhs_bufinfo, MP_BUFFER_READ)) { |
320 | return mp_const_false; |
321 | } |
322 | return mp_obj_new_bool(mp_seq_cmp_bytes(op, lhs_bufinfo.buf, lhs_bufinfo.len, rhs_bufinfo.buf, rhs_bufinfo.len)); |
323 | } |
324 | |
325 | default: |
326 | return MP_OBJ_NULL; // op not supported |
327 | } |
328 | } |
329 | |
330 | #if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY |
331 | STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg) { |
332 | // self is not a memoryview, so we don't need to use (& TYPECODE_MASK) |
333 | assert((MICROPY_PY_BUILTINS_BYTEARRAY && mp_obj_is_type(self_in, &mp_type_bytearray)) |
334 | || (MICROPY_PY_ARRAY && mp_obj_is_type(self_in, &mp_type_array))); |
335 | mp_obj_array_t *self = MP_OBJ_TO_PTR(self_in); |
336 | |
337 | if (self->free == 0) { |
338 | size_t item_sz = mp_binary_get_size('@', self->typecode, NULL); |
339 | // TODO: alloc policy |
340 | self->free = 8; |
341 | self->items = m_renew(byte, self->items, item_sz * self->len, item_sz * (self->len + self->free)); |
342 | mp_seq_clear(self->items, self->len + 1, self->len + self->free, item_sz); |
343 | } |
344 | mp_binary_set_val_array(self->typecode, self->items, self->len, arg); |
345 | // only update length/free if set succeeded |
346 | self->len++; |
347 | self->free--; |
348 | return mp_const_none; // return None, as per CPython |
349 | } |
350 | STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_append_obj, array_append); |
351 | |
352 | STATIC mp_obj_t array_extend(mp_obj_t self_in, mp_obj_t arg_in) { |
353 | // self is not a memoryview, so we don't need to use (& TYPECODE_MASK) |
354 | assert((MICROPY_PY_BUILTINS_BYTEARRAY && mp_obj_is_type(self_in, &mp_type_bytearray)) |
355 | || (MICROPY_PY_ARRAY && mp_obj_is_type(self_in, &mp_type_array))); |
356 | mp_obj_array_t *self = MP_OBJ_TO_PTR(self_in); |
357 | |
358 | // allow to extend by anything that has the buffer protocol (extension to CPython) |
359 | mp_buffer_info_t arg_bufinfo; |
360 | mp_get_buffer_raise(arg_in, &arg_bufinfo, MP_BUFFER_READ); |
361 | |
362 | size_t sz = mp_binary_get_size('@', self->typecode, NULL); |
363 | |
364 | // convert byte count to element count |
365 | size_t len = arg_bufinfo.len / sz; |
366 | |
367 | // make sure we have enough room to extend |
368 | // TODO: alloc policy; at the moment we go conservative |
369 | if (self->free < len) { |
370 | self->items = m_renew(byte, self->items, (self->len + self->free) * sz, (self->len + len) * sz); |
371 | self->free = 0; |
372 | } else { |
373 | self->free -= len; |
374 | } |
375 | |
376 | // extend |
377 | mp_seq_copy((byte *)self->items + self->len * sz, arg_bufinfo.buf, len * sz, byte); |
378 | self->len += len; |
379 | |
380 | return mp_const_none; |
381 | } |
382 | STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_extend_obj, array_extend); |
383 | #endif |
384 | |
385 | STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value) { |
386 | if (value == MP_OBJ_NULL) { |
387 | // delete item |
388 | // TODO implement |
389 | // TODO: confirmed that both bytearray and array.array support |
390 | // slice deletion |
391 | return MP_OBJ_NULL; // op not supported |
392 | } else { |
393 | mp_obj_array_t *o = MP_OBJ_TO_PTR(self_in); |
394 | #if MICROPY_PY_BUILTINS_SLICE |
395 | if (mp_obj_is_type(index_in, &mp_type_slice)) { |
396 | mp_bound_slice_t slice; |
397 | if (!mp_seq_get_fast_slice_indexes(o->len, index_in, &slice)) { |
398 | mp_raise_NotImplementedError(MP_ERROR_TEXT("only slices with step=1 (aka None) are supported" )); |
399 | } |
400 | if (value != MP_OBJ_SENTINEL) { |
401 | #if MICROPY_PY_ARRAY_SLICE_ASSIGN |
402 | // Assign |
403 | size_t src_len; |
404 | void *src_items; |
405 | size_t item_sz = mp_binary_get_size('@', o->typecode & TYPECODE_MASK, NULL); |
406 | if (mp_obj_is_obj(value) && ((mp_obj_base_t *)MP_OBJ_TO_PTR(value))->type->subscr == array_subscr) { |
407 | // value is array, bytearray or memoryview |
408 | mp_obj_array_t *src_slice = MP_OBJ_TO_PTR(value); |
409 | if (item_sz != mp_binary_get_size('@', src_slice->typecode & TYPECODE_MASK, NULL)) { |
410 | compat_error: |
411 | mp_raise_ValueError(MP_ERROR_TEXT("lhs and rhs should be compatible" )); |
412 | } |
413 | src_len = src_slice->len; |
414 | src_items = src_slice->items; |
415 | #if MICROPY_PY_BUILTINS_MEMORYVIEW |
416 | if (mp_obj_is_type(value, &mp_type_memoryview)) { |
417 | src_items = (uint8_t *)src_items + (src_slice->memview_offset * item_sz); |
418 | } |
419 | #endif |
420 | } else if (mp_obj_is_type(value, &mp_type_bytes)) { |
421 | if (item_sz != 1) { |
422 | goto compat_error; |
423 | } |
424 | mp_buffer_info_t bufinfo; |
425 | mp_get_buffer_raise(value, &bufinfo, MP_BUFFER_READ); |
426 | src_len = bufinfo.len; |
427 | src_items = bufinfo.buf; |
428 | } else { |
429 | mp_raise_NotImplementedError(MP_ERROR_TEXT("array/bytes required on right side" )); |
430 | } |
431 | |
432 | // TODO: check src/dst compat |
433 | mp_int_t len_adj = src_len - (slice.stop - slice.start); |
434 | uint8_t *dest_items = o->items; |
435 | #if MICROPY_PY_BUILTINS_MEMORYVIEW |
436 | if (o->base.type == &mp_type_memoryview) { |
437 | if (!(o->typecode & MP_OBJ_ARRAY_TYPECODE_FLAG_RW)) { |
438 | // store to read-only memoryview not allowed |
439 | return MP_OBJ_NULL; |
440 | } |
441 | if (len_adj != 0) { |
442 | goto compat_error; |
443 | } |
444 | dest_items += o->memview_offset * item_sz; |
445 | } |
446 | #endif |
447 | if (len_adj > 0) { |
448 | if ((size_t)len_adj > o->free) { |
449 | // TODO: alloc policy; at the moment we go conservative |
450 | o->items = m_renew(byte, o->items, (o->len + o->free) * item_sz, (o->len + len_adj) * item_sz); |
451 | o->free = len_adj; |
452 | dest_items = o->items; |
453 | } |
454 | mp_seq_replace_slice_grow_inplace(dest_items, o->len, |
455 | slice.start, slice.stop, src_items, src_len, len_adj, item_sz); |
456 | } else { |
457 | mp_seq_replace_slice_no_grow(dest_items, o->len, |
458 | slice.start, slice.stop, src_items, src_len, item_sz); |
459 | // Clear "freed" elements at the end of list |
460 | // TODO: This is actually only needed for typecode=='O' |
461 | mp_seq_clear(dest_items, o->len + len_adj, o->len, item_sz); |
462 | // TODO: alloc policy after shrinking |
463 | } |
464 | o->free -= len_adj; |
465 | o->len += len_adj; |
466 | return mp_const_none; |
467 | #else |
468 | return MP_OBJ_NULL; // op not supported |
469 | #endif |
470 | } |
471 | |
472 | mp_obj_array_t *res; |
473 | size_t sz = mp_binary_get_size('@', o->typecode & TYPECODE_MASK, NULL); |
474 | assert(sz > 0); |
475 | #if MICROPY_PY_BUILTINS_MEMORYVIEW |
476 | if (o->base.type == &mp_type_memoryview) { |
477 | res = m_new_obj(mp_obj_array_t); |
478 | *res = *o; |
479 | res->memview_offset += slice.start; |
480 | res->len = slice.stop - slice.start; |
481 | } else |
482 | #endif |
483 | { |
484 | res = array_new(o->typecode, slice.stop - slice.start); |
485 | memcpy(res->items, (uint8_t *)o->items + slice.start * sz, (slice.stop - slice.start) * sz); |
486 | } |
487 | return MP_OBJ_FROM_PTR(res); |
488 | } else |
489 | #endif |
490 | { |
491 | size_t index = mp_get_index(o->base.type, o->len, index_in, false); |
492 | #if MICROPY_PY_BUILTINS_MEMORYVIEW |
493 | if (o->base.type == &mp_type_memoryview) { |
494 | index += o->memview_offset; |
495 | if (value != MP_OBJ_SENTINEL && !(o->typecode & MP_OBJ_ARRAY_TYPECODE_FLAG_RW)) { |
496 | // store to read-only memoryview |
497 | return MP_OBJ_NULL; |
498 | } |
499 | } |
500 | #endif |
501 | if (value == MP_OBJ_SENTINEL) { |
502 | // load |
503 | return mp_binary_get_val_array(o->typecode & TYPECODE_MASK, o->items, index); |
504 | } else { |
505 | // store |
506 | mp_binary_set_val_array(o->typecode & TYPECODE_MASK, o->items, index, value); |
507 | return mp_const_none; |
508 | } |
509 | } |
510 | } |
511 | } |
512 | |
513 | STATIC mp_int_t array_get_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) { |
514 | mp_obj_array_t *o = MP_OBJ_TO_PTR(o_in); |
515 | size_t sz = mp_binary_get_size('@', o->typecode & TYPECODE_MASK, NULL); |
516 | bufinfo->buf = o->items; |
517 | bufinfo->len = o->len * sz; |
518 | bufinfo->typecode = o->typecode & TYPECODE_MASK; |
519 | #if MICROPY_PY_BUILTINS_MEMORYVIEW |
520 | if (o->base.type == &mp_type_memoryview) { |
521 | if (!(o->typecode & MP_OBJ_ARRAY_TYPECODE_FLAG_RW) && (flags & MP_BUFFER_WRITE)) { |
522 | // read-only memoryview |
523 | return 1; |
524 | } |
525 | bufinfo->buf = (uint8_t *)bufinfo->buf + (size_t)o->memview_offset * sz; |
526 | } |
527 | #else |
528 | (void)flags; |
529 | #endif |
530 | return 0; |
531 | } |
532 | |
533 | #if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY |
534 | STATIC const mp_rom_map_elem_t array_locals_dict_table[] = { |
535 | { MP_ROM_QSTR(MP_QSTR_append), MP_ROM_PTR(&array_append_obj) }, |
536 | { MP_ROM_QSTR(MP_QSTR_extend), MP_ROM_PTR(&array_extend_obj) }, |
537 | #if MICROPY_CPYTHON_COMPAT |
538 | { MP_ROM_QSTR(MP_QSTR_decode), MP_ROM_PTR(&bytes_decode_obj) }, |
539 | #endif |
540 | }; |
541 | |
542 | STATIC MP_DEFINE_CONST_DICT(array_locals_dict, array_locals_dict_table); |
543 | #endif |
544 | |
545 | #if MICROPY_PY_ARRAY |
546 | const mp_obj_type_t mp_type_array = { |
547 | { &mp_type_type }, |
548 | .name = MP_QSTR_array, |
549 | .print = array_print, |
550 | .make_new = array_make_new, |
551 | .getiter = array_iterator_new, |
552 | .unary_op = array_unary_op, |
553 | .binary_op = array_binary_op, |
554 | .subscr = array_subscr, |
555 | .buffer_p = { .get_buffer = array_get_buffer }, |
556 | .locals_dict = (mp_obj_dict_t *)&array_locals_dict, |
557 | }; |
558 | #endif |
559 | |
560 | #if MICROPY_PY_BUILTINS_BYTEARRAY |
561 | const mp_obj_type_t mp_type_bytearray = { |
562 | { &mp_type_type }, |
563 | .flags = MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE, |
564 | .name = MP_QSTR_bytearray, |
565 | .print = array_print, |
566 | .make_new = bytearray_make_new, |
567 | .getiter = array_iterator_new, |
568 | .unary_op = array_unary_op, |
569 | .binary_op = array_binary_op, |
570 | .subscr = array_subscr, |
571 | .buffer_p = { .get_buffer = array_get_buffer }, |
572 | .locals_dict = (mp_obj_dict_t *)&array_locals_dict, |
573 | }; |
574 | #endif |
575 | |
576 | #if MICROPY_PY_BUILTINS_MEMORYVIEW |
577 | const mp_obj_type_t mp_type_memoryview = { |
578 | { &mp_type_type }, |
579 | .flags = MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE, |
580 | .name = MP_QSTR_memoryview, |
581 | .make_new = memoryview_make_new, |
582 | .getiter = array_iterator_new, |
583 | .unary_op = array_unary_op, |
584 | .binary_op = array_binary_op, |
585 | #if MICROPY_PY_BUILTINS_MEMORYVIEW_ITEMSIZE |
586 | .attr = memoryview_attr, |
587 | #endif |
588 | .subscr = array_subscr, |
589 | .buffer_p = { .get_buffer = array_get_buffer }, |
590 | }; |
591 | #endif |
592 | |
593 | /* unused |
594 | size_t mp_obj_array_len(mp_obj_t self_in) { |
595 | return ((mp_obj_array_t *)self_in)->len; |
596 | } |
597 | */ |
598 | |
599 | #if MICROPY_PY_BUILTINS_BYTEARRAY |
600 | mp_obj_t mp_obj_new_bytearray(size_t n, void *items) { |
601 | mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, n); |
602 | memcpy(o->items, items, n); |
603 | return MP_OBJ_FROM_PTR(o); |
604 | } |
605 | |
606 | // Create bytearray which references specified memory area |
607 | mp_obj_t mp_obj_new_bytearray_by_ref(size_t n, void *items) { |
608 | mp_obj_array_t *o = m_new_obj(mp_obj_array_t); |
609 | o->base.type = &mp_type_bytearray; |
610 | o->typecode = BYTEARRAY_TYPECODE; |
611 | o->free = 0; |
612 | o->len = n; |
613 | o->items = items; |
614 | return MP_OBJ_FROM_PTR(o); |
615 | } |
616 | #endif |
617 | |
618 | /******************************************************************************/ |
619 | // array iterator |
620 | |
621 | typedef struct _mp_obj_array_it_t { |
622 | mp_obj_base_t base; |
623 | mp_obj_array_t *array; |
624 | size_t offset; |
625 | size_t cur; |
626 | } mp_obj_array_it_t; |
627 | |
628 | STATIC mp_obj_t array_it_iternext(mp_obj_t self_in) { |
629 | mp_obj_array_it_t *self = MP_OBJ_TO_PTR(self_in); |
630 | if (self->cur < self->array->len) { |
631 | return mp_binary_get_val_array(self->array->typecode & TYPECODE_MASK, self->array->items, self->offset + self->cur++); |
632 | } else { |
633 | return MP_OBJ_STOP_ITERATION; |
634 | } |
635 | } |
636 | |
637 | STATIC const mp_obj_type_t mp_type_array_it = { |
638 | { &mp_type_type }, |
639 | .name = MP_QSTR_iterator, |
640 | .getiter = mp_identity_getiter, |
641 | .iternext = array_it_iternext, |
642 | }; |
643 | |
644 | STATIC mp_obj_t array_iterator_new(mp_obj_t array_in, mp_obj_iter_buf_t *iter_buf) { |
645 | assert(sizeof(mp_obj_array_t) <= sizeof(mp_obj_iter_buf_t)); |
646 | mp_obj_array_t *array = MP_OBJ_TO_PTR(array_in); |
647 | mp_obj_array_it_t *o = (mp_obj_array_it_t *)iter_buf; |
648 | o->base.type = &mp_type_array_it; |
649 | o->array = array; |
650 | o->offset = 0; |
651 | o->cur = 0; |
652 | #if MICROPY_PY_BUILTINS_MEMORYVIEW |
653 | if (array->base.type == &mp_type_memoryview) { |
654 | o->offset = array->memview_offset; |
655 | } |
656 | #endif |
657 | return MP_OBJ_FROM_PTR(o); |
658 | } |
659 | |
660 | #endif // MICROPY_PY_ARRAY || MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_BUILTINS_MEMORYVIEW |
661 | |