blob: b14ad5a6539ea7883c28d4b9d04628e3a53ce738 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
Paul Sokolovskyda9f0922014-05-13 08:44:45 +03007 * Copyright (c) 2014 Paul Sokolovsky
Damien George04b91472014-05-03 23:27:38 +01008 *
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
Paul Sokolovsky427905c2014-01-18 19:24:47 +020028#include <string.h>
Paul Sokolovsky427905c2014-01-18 19:24:47 +020029#include <assert.h>
stijn49c47da2014-10-28 17:20:52 +010030#include <stdint.h>
Paul Sokolovsky427905c2014-01-18 19:24:47 +020031
Damien George51dfcb42015-01-01 20:27:54 +000032#include "py/nlr.h"
33#include "py/runtime0.h"
34#include "py/runtime.h"
35#include "py/binary.h"
Paul Sokolovsky427905c2014-01-18 19:24:47 +020036
Damien Georgedd4f4532014-10-23 13:34:35 +010037#if MICROPY_PY_ARRAY || MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_BUILTINS_MEMORYVIEW
Paul Sokolovskycb78f862014-06-27 20:39:09 +030038
Damien Georgede3c8062014-10-26 13:20:50 +000039// 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// make (& TYPECODE_MASK) a null operation if memorview not enabled
54#if MICROPY_PY_BUILTINS_MEMORYVIEW
55#define TYPECODE_MASK (0x7f)
56#else
Paul Sokolovsky16b1f5e2015-03-04 23:01:44 +020057#define TYPECODE_MASK (~(mp_uint_t)0)
Damien Georgede3c8062014-10-26 13:20:50 +000058#endif
59
Paul Sokolovsky427905c2014-01-18 19:24:47 +020060typedef struct _mp_obj_array_t {
61 mp_obj_base_t base;
Damien George40f3c022014-07-03 13:25:24 +010062 mp_uint_t typecode : 8;
Damien George51047752014-02-26 17:40:52 +000063 // free is number of unused elements after len used elements
64 // alloc size = len + free
Damien George40f3c022014-07-03 13:25:24 +010065 mp_uint_t free : (8 * sizeof(mp_uint_t) - 8);
66 mp_uint_t len; // in elements
Paul Sokolovsky427905c2014-01-18 19:24:47 +020067 void *items;
68} mp_obj_array_t;
69
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020070STATIC mp_obj_t array_iterator_new(mp_obj_t array_in);
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020071STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg);
Damien Georgeb2e73112014-11-30 00:00:55 +000072STATIC mp_obj_t array_extend(mp_obj_t self_in, mp_obj_t arg_in);
Damien George4d917232014-08-30 14:28:06 +010073STATIC mp_int_t array_get_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, mp_uint_t flags);
Paul Sokolovsky427905c2014-01-18 19:24:47 +020074
75/******************************************************************************/
Damien Georgedd4f4532014-10-23 13:34:35 +010076// array
Paul Sokolovsky427905c2014-01-18 19:24:47 +020077
Damien Georgedd4f4532014-10-23 13:34:35 +010078#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
Damien George7f9d1d62015-04-09 23:56:15 +010079STATIC void array_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +000080 (void)kind;
Paul Sokolovsky427905c2014-01-18 19:24:47 +020081 mp_obj_array_t *o = o_in;
82 if (o->typecode == BYTEARRAY_TYPECODE) {
Damien George7f9d1d62015-04-09 23:56:15 +010083 mp_print_str(print, "bytearray(b");
84 mp_str_print_quoted(print, o->items, o->len, true);
Paul Sokolovsky427905c2014-01-18 19:24:47 +020085 } else {
Damien George7f9d1d62015-04-09 23:56:15 +010086 mp_printf(print, "array('%c'", o->typecode);
Paul Sokolovsky18014212014-01-28 03:40:48 +020087 if (o->len > 0) {
Damien George7f9d1d62015-04-09 23:56:15 +010088 mp_print_str(print, ", [");
Damien George42f3de92014-10-03 17:44:14 +000089 for (mp_uint_t i = 0; i < o->len; i++) {
Paul Sokolovsky18014212014-01-28 03:40:48 +020090 if (i > 0) {
Damien George7f9d1d62015-04-09 23:56:15 +010091 mp_print_str(print, ", ");
Paul Sokolovsky18014212014-01-28 03:40:48 +020092 }
Damien George7f9d1d62015-04-09 23:56:15 +010093 mp_obj_print_helper(print, mp_binary_get_val_array(o->typecode, o->items, i), PRINT_REPR);
Paul Sokolovsky7e652af2014-01-28 03:14:20 +020094 }
Damien George7f9d1d62015-04-09 23:56:15 +010095 mp_print_str(print, "]");
Paul Sokolovsky427905c2014-01-18 19:24:47 +020096 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +020097 }
Damien George7f9d1d62015-04-09 23:56:15 +010098 mp_print_str(print, ")");
Paul Sokolovsky427905c2014-01-18 19:24:47 +020099}
Damien Georgedd4f4532014-10-23 13:34:35 +0100100#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200101
Damien Georgedd4f4532014-10-23 13:34:35 +0100102#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
103STATIC mp_obj_array_t *array_new(char typecode, mp_uint_t n) {
104 int typecode_size = mp_binary_get_size('@', typecode, NULL);
Kaspar Schleiserf5dd6f72015-05-10 13:04:38 +0200105 if (typecode_size == 0) {
Damien Georgedd4f4532014-10-23 13:34:35 +0100106 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "bad typecode"));
107 }
108 mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
109 #if MICROPY_PY_BUILTINS_BYTEARRAY && MICROPY_PY_ARRAY
110 o->base.type = (typecode == BYTEARRAY_TYPECODE) ? &mp_type_bytearray : &mp_type_array;
111 #elif MICROPY_PY_BUILTINS_BYTEARRAY
112 o->base.type = &mp_type_bytearray;
113 #else
114 o->base.type = &mp_type_array;
115 #endif
116 o->typecode = typecode;
117 o->free = 0;
118 o->len = n;
Damien George4d77e1a2015-02-27 09:34:51 +0000119 o->items = m_new(byte, typecode_size * o->len);
Damien Georgedd4f4532014-10-23 13:34:35 +0100120 return o;
121}
122#endif
123
124#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200125STATIC mp_obj_t array_construct(char typecode, mp_obj_t initializer) {
Damien George32ef3a32014-12-04 15:46:14 +0000126 // bytearrays can be raw-initialised from anything with the buffer protocol
127 // other arrays can only be raw-initialised from bytes and bytearray objects
128 mp_buffer_info_t bufinfo;
129 if (((MICROPY_PY_BUILTINS_BYTEARRAY
130 && typecode == BYTEARRAY_TYPECODE)
131 || (MICROPY_PY_ARRAY
132 && (MP_OBJ_IS_TYPE(initializer, &mp_type_bytes)
Damien George035deae2015-07-02 16:26:57 +0100133 || (MICROPY_PY_BUILTINS_BYTEARRAY && MP_OBJ_IS_TYPE(initializer, &mp_type_bytearray)))))
Damien George32ef3a32014-12-04 15:46:14 +0000134 && mp_get_buffer(initializer, &bufinfo, MP_BUFFER_READ)) {
135 // construct array from raw bytes
136 // we round-down the len to make it a multiple of sz (CPython raises error)
Kaspar Schleiserf5dd6f72015-05-10 13:04:38 +0200137 size_t sz = mp_binary_get_size('@', typecode, NULL);
Damien George32ef3a32014-12-04 15:46:14 +0000138 mp_uint_t len = bufinfo.len / sz;
139 mp_obj_array_t *o = array_new(typecode, len);
140 memcpy(o->items, bufinfo.buf, len * sz);
141 return o;
142 }
143
144 mp_uint_t len;
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200145 // Try to create array of exact len if initializer len is known
146 mp_obj_t len_in = mp_obj_len_maybe(initializer);
147 if (len_in == MP_OBJ_NULL) {
148 len = 0;
149 } else {
150 len = MP_OBJ_SMALL_INT_VALUE(len_in);
151 }
152
153 mp_obj_array_t *array = array_new(typecode, len);
154
Damien Georged17926d2014-03-30 13:35:08 +0100155 mp_obj_t iterable = mp_getiter(initializer);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200156 mp_obj_t item;
Damien George42f3de92014-10-03 17:44:14 +0000157 mp_uint_t i = 0;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100158 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200159 if (len == 0) {
160 array_append(array, item);
161 } else {
Paul Sokolovskyef9124f2014-04-11 03:46:09 +0300162 mp_binary_set_val_array(typecode, array->items, i++, item);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200163 }
164 }
165
166 return array;
167}
Damien Georgedd4f4532014-10-23 13:34:35 +0100168#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200169
Damien Georgedd4f4532014-10-23 13:34:35 +0100170#if MICROPY_PY_ARRAY
Damien Georgeecc88e92014-08-30 00:35:11 +0100171STATIC mp_obj_t array_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000172 (void)type_in;
Damien Georgea3f94e02014-04-20 00:13:22 +0100173 mp_arg_check_num(n_args, n_kw, 1, 2, false);
Damien George32ca1642014-04-18 22:04:06 +0100174
175 // get typecode
Damien Georged182b982014-08-30 14:19:41 +0100176 mp_uint_t l;
Damien George698ec212014-02-08 18:17:23 +0000177 const char *typecode = mp_obj_str_get_data(args[0], &l);
Paul Sokolovsky11973b42014-01-28 02:31:52 +0200178
Damien George32ca1642014-04-18 22:04:06 +0100179 if (n_args == 1) {
180 // 1 arg: make an empty array
181 return array_new(*typecode, 0);
182 } else {
Damien George32ef3a32014-12-04 15:46:14 +0000183 // 2 args: construct the array from the given object
Damien George32ca1642014-04-18 22:04:06 +0100184 return array_construct(*typecode, args[1]);
185 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200186}
Damien Georgedd4f4532014-10-23 13:34:35 +0100187#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200188
Damien Georgedd4f4532014-10-23 13:34:35 +0100189#if MICROPY_PY_BUILTINS_BYTEARRAY
Damien Georgeecc88e92014-08-30 00:35:11 +0100190STATIC mp_obj_t bytearray_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000191 (void)type_in;
Damien Georgea3f94e02014-04-20 00:13:22 +0100192 mp_arg_check_num(n_args, n_kw, 0, 1, false);
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300193
Damien George32ca1642014-04-18 22:04:06 +0100194 if (n_args == 0) {
195 // no args: construct an empty bytearray
196 return array_new(BYTEARRAY_TYPECODE, 0);
Paul Sokolovsky343ca1e2015-01-04 17:19:16 +0200197 } else if (MP_OBJ_IS_INT(args[0])) {
Damien George32ca1642014-04-18 22:04:06 +0100198 // 1 arg, an integer: construct a blank bytearray of that length
Paul Sokolovsky343ca1e2015-01-04 17:19:16 +0200199 mp_uint_t len = mp_obj_get_int(args[0]);
Paul Sokolovskyb9cf3d32014-04-08 04:42:44 +0300200 mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, len);
201 memset(o->items, 0, len);
202 return o;
Damien George32ca1642014-04-18 22:04:06 +0100203 } else {
Damien George32ef3a32014-12-04 15:46:14 +0000204 // 1 arg: construct the bytearray from that
Damien George32ca1642014-04-18 22:04:06 +0100205 return array_construct(BYTEARRAY_TYPECODE, args[0]);
Paul Sokolovskyb9cf3d32014-04-08 04:42:44 +0300206 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200207}
Damien Georgedd4f4532014-10-23 13:34:35 +0100208#endif
209
210#if MICROPY_PY_BUILTINS_MEMORYVIEW
Paul Sokolovskycb0fc062015-03-06 21:35:26 +0200211
212mp_obj_t mp_obj_new_memoryview(byte typecode, mp_uint_t nitems, void *items) {
213 mp_obj_array_t *self = m_new_obj(mp_obj_array_t);
214 self->base.type = &mp_type_memoryview;
215 self->typecode = typecode;
216 self->free = 0;
217 self->len = nitems;
218 self->items = items;
219 return self;
220}
221
Damien Georgedd4f4532014-10-23 13:34:35 +0100222STATIC mp_obj_t memoryview_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
Damien George2e2e4042015-03-19 00:21:29 +0000223 (void)type_in;
224
Damien Georgedd4f4532014-10-23 13:34:35 +0100225 // TODO possibly allow memoryview constructor to take start/stop so that one
226 // can do memoryview(b, 4, 8) instead of memoryview(b)[4:8] (uses less RAM)
227
228 mp_arg_check_num(n_args, n_kw, 1, 1, false);
229
230 mp_buffer_info_t bufinfo;
231 mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
232
Paul Sokolovskycb0fc062015-03-06 21:35:26 +0200233 mp_obj_array_t *self = mp_obj_new_memoryview(bufinfo.typecode,
234 bufinfo.len / mp_binary_get_size('@', bufinfo.typecode, NULL),
235 bufinfo.buf);
Damien Georgedd4f4532014-10-23 13:34:35 +0100236
237 // test if the object can be written to
238 if (mp_get_buffer(args[0], &bufinfo, MP_BUFFER_RW)) {
Damien Georgede3c8062014-10-26 13:20:50 +0000239 self->typecode |= 0x80; // used to indicate writable buffer
Damien Georgedd4f4532014-10-23 13:34:35 +0100240 }
241
242 return self;
243}
244#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200245
Damien Georgeecc88e92014-08-30 00:35:11 +0100246STATIC mp_obj_t array_unary_op(mp_uint_t op, mp_obj_t o_in) {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200247 mp_obj_array_t *o = o_in;
248 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100249 case MP_UNARY_OP_BOOL: return MP_BOOL(o->len != 0);
250 case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(o->len);
Damien George6ac5dce2014-05-21 19:42:43 +0100251 default: return MP_OBJ_NULL; // op not supported
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200252 }
253}
254
Damien Georgeecc88e92014-08-30 00:35:11 +0100255STATIC mp_obj_t array_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damien Georgeb2e73112014-11-30 00:00:55 +0000256 mp_obj_array_t *lhs = lhs_in;
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300257 switch (op) {
Damien Georgeb2e73112014-11-30 00:00:55 +0000258 case MP_BINARY_OP_ADD: {
Damien Georgec7ca01a2014-11-30 14:01:33 +0000259 // allow to add anything that has the buffer protocol (extension to CPython)
260 mp_buffer_info_t lhs_bufinfo;
261 mp_buffer_info_t rhs_bufinfo;
262 array_get_buffer(lhs_in, &lhs_bufinfo, MP_BUFFER_READ);
263 mp_get_buffer_raise(rhs_in, &rhs_bufinfo, MP_BUFFER_READ);
264
Kaspar Schleiserf5dd6f72015-05-10 13:04:38 +0200265 size_t sz = mp_binary_get_size('@', lhs_bufinfo.typecode, NULL);
Damien Georgec7ca01a2014-11-30 14:01:33 +0000266
267 // convert byte count to element count (in case rhs is not multiple of sz)
268 mp_uint_t rhs_len = rhs_bufinfo.len / sz;
269
270 // note: lhs->len is element count of lhs, lhs_bufinfo.len is byte count
271 mp_obj_array_t *res = array_new(lhs_bufinfo.typecode, lhs->len + rhs_len);
272 mp_seq_cat((byte*)res->items, lhs_bufinfo.buf, lhs_bufinfo.len, rhs_bufinfo.buf, rhs_len * sz, byte);
Damien Georgeb2e73112014-11-30 00:00:55 +0000273 return res;
274 }
275
276 case MP_BINARY_OP_INPLACE_ADD: {
277 #if MICROPY_PY_BUILTINS_MEMORYVIEW
278 if (lhs->base.type == &mp_type_memoryview) {
279 return MP_OBJ_NULL; // op not supported
280 }
281 #endif
282 array_extend(lhs, rhs_in);
283 return lhs;
284 }
285
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300286 case MP_BINARY_OP_EQUAL: {
287 mp_buffer_info_t lhs_bufinfo;
288 mp_buffer_info_t rhs_bufinfo;
289 array_get_buffer(lhs_in, &lhs_bufinfo, MP_BUFFER_READ);
290 if (!mp_get_buffer(rhs_in, &rhs_bufinfo, MP_BUFFER_READ)) {
Damien Georgede3c8062014-10-26 13:20:50 +0000291 return mp_const_false;
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300292 }
293 return MP_BOOL(mp_seq_cmp_bytes(op, lhs_bufinfo.buf, lhs_bufinfo.len, rhs_bufinfo.buf, rhs_bufinfo.len));
294 }
Damien Georgeb2e73112014-11-30 00:00:55 +0000295
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300296 default:
297 return MP_OBJ_NULL; // op not supported
298 }
299}
300
Damien Georgeb2e73112014-11-30 00:00:55 +0000301#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200302STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg) {
Damien Georgeb2e73112014-11-30 00:00:55 +0000303 // self is not a memoryview, so we don't need to use (& TYPECODE_MASK)
Damien George035deae2015-07-02 16:26:57 +0100304 assert((MICROPY_PY_BUILTINS_BYTEARRAY && MP_OBJ_IS_TYPE(self_in, &mp_type_bytearray))
305 || (MICROPY_PY_ARRAY && MP_OBJ_IS_TYPE(self_in, &mp_type_array)));
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200306 mp_obj_array_t *self = self_in;
Damien Georgeb2e73112014-11-30 00:00:55 +0000307
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200308 if (self->free == 0) {
Kaspar Schleiserf5dd6f72015-05-10 13:04:38 +0200309 size_t item_sz = mp_binary_get_size('@', self->typecode, NULL);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200310 // TODO: alloc policy
311 self->free = 8;
Damien George4d77e1a2015-02-27 09:34:51 +0000312 self->items = m_renew(byte, self->items, item_sz * self->len, item_sz * (self->len + self->free));
Paul Sokolovskya2240672014-04-28 00:16:57 +0300313 mp_seq_clear(self->items, self->len + 1, self->len + self->free, item_sz);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200314 }
Paul Sokolovskyef9124f2014-04-11 03:46:09 +0300315 mp_binary_set_val_array(self->typecode, self->items, self->len++, arg);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200316 self->free--;
317 return mp_const_none; // return None, as per CPython
318}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200319STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_append_obj, array_append);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200320
Damien Georgeb2e73112014-11-30 00:00:55 +0000321STATIC mp_obj_t array_extend(mp_obj_t self_in, mp_obj_t arg_in) {
322 // self is not a memoryview, so we don't need to use (& TYPECODE_MASK)
Damien George035deae2015-07-02 16:26:57 +0100323 assert((MICROPY_PY_BUILTINS_BYTEARRAY && MP_OBJ_IS_TYPE(self_in, &mp_type_bytearray))
324 || (MICROPY_PY_ARRAY && MP_OBJ_IS_TYPE(self_in, &mp_type_array)));
Damien Georgeb2e73112014-11-30 00:00:55 +0000325 mp_obj_array_t *self = self_in;
326
Damien Georgec7ca01a2014-11-30 14:01:33 +0000327 // allow to extend by anything that has the buffer protocol (extension to CPython)
328 mp_buffer_info_t arg_bufinfo;
329 mp_get_buffer_raise(arg_in, &arg_bufinfo, MP_BUFFER_READ);
Damien Georgeb2e73112014-11-30 00:00:55 +0000330
Kaspar Schleiserf5dd6f72015-05-10 13:04:38 +0200331 size_t sz = mp_binary_get_size('@', self->typecode, NULL);
Damien Georgeb2e73112014-11-30 00:00:55 +0000332
Damien Georgec7ca01a2014-11-30 14:01:33 +0000333 // convert byte count to element count
334 mp_uint_t len = arg_bufinfo.len / sz;
335
Damien Georgeb2e73112014-11-30 00:00:55 +0000336 // make sure we have enough room to extend
Damien Georgec7ca01a2014-11-30 14:01:33 +0000337 // TODO: alloc policy; at the moment we go conservative
338 if (self->free < len) {
Damien George4d77e1a2015-02-27 09:34:51 +0000339 self->items = m_renew(byte, self->items, (self->len + self->free) * sz, (self->len + len) * sz);
Damien Georgec7ca01a2014-11-30 14:01:33 +0000340 self->free = 0;
341 } else {
342 self->free -= len;
Damien Georgeb2e73112014-11-30 00:00:55 +0000343 }
344
345 // extend
Damien Georgec7ca01a2014-11-30 14:01:33 +0000346 mp_seq_copy((byte*)self->items + self->len * sz, arg_bufinfo.buf, len * sz, byte);
347 self->len += len;
Damien Georgeb2e73112014-11-30 00:00:55 +0000348
349 return mp_const_none;
350}
351STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_extend_obj, array_extend);
352#endif
353
Damien George729f7b42014-04-17 22:10:53 +0100354STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value) {
Damien Georgef4c9b332014-04-08 21:32:29 +0100355 if (value == MP_OBJ_NULL) {
Damien George32ca1642014-04-18 22:04:06 +0100356 // delete item
357 // TODO implement
Paul Sokolovsky26905252014-04-20 20:58:33 +0300358 // TODO: confirmed that both bytearray and array.array support
359 // slice deletion
Damien George6ac5dce2014-05-21 19:42:43 +0100360 return MP_OBJ_NULL; // op not supported
Damien George729f7b42014-04-17 22:10:53 +0100361 } else {
362 mp_obj_array_t *o = self_in;
Damien Georgec49ddb92014-06-01 13:49:35 +0100363 if (0) {
364#if MICROPY_PY_BUILTINS_SLICE
365 } else if (MP_OBJ_IS_TYPE(index_in, &mp_type_slice)) {
Paul Sokolovskyde4b9322014-05-25 21:21:57 +0300366 mp_bound_slice_t slice;
367 if (!mp_seq_get_fast_slice_indexes(o->len, index_in, &slice)) {
Paul Sokolovsky5fd5af92014-05-25 22:12:56 +0300368 nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
Damien George11de8392014-06-05 18:57:38 +0100369 "only slices with step=1 (aka None) are supported"));
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300370 }
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200371 if (value != MP_OBJ_SENTINEL) {
372 #if MICROPY_PY_ARRAY_SLICE_ASSIGN
373 // Assign
Paul Sokolovsky56beb012015-04-16 00:48:36 +0300374 mp_uint_t src_len;
375 void *src_items;
Delio Brignoli32aba402015-06-03 07:07:35 +0200376 size_t item_sz = mp_binary_get_size('@', o->typecode & TYPECODE_MASK, NULL);
Damien George4915c2b2015-07-20 16:12:26 +0100377 if (MP_OBJ_IS_OBJ(value) && ((mp_obj_base_t*)value)->type->subscr == array_subscr) {
378 // value is array, bytearray or memoryview
Paul Sokolovsky56beb012015-04-16 00:48:36 +0300379 mp_obj_array_t *src_slice = value;
Delio Brignoli32aba402015-06-03 07:07:35 +0200380 if (item_sz != mp_binary_get_size('@', src_slice->typecode & TYPECODE_MASK, NULL)) {
Paul Sokolovsky56beb012015-04-16 00:48:36 +0300381 compat_error:
Delio Brignoli32aba402015-06-03 07:07:35 +0200382 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "lhs and rhs should be compatible"));
Paul Sokolovsky56beb012015-04-16 00:48:36 +0300383 }
384 src_len = src_slice->len;
385 src_items = src_slice->items;
Damien George4915c2b2015-07-20 16:12:26 +0100386 #if MICROPY_PY_BUILTINS_MEMORYVIEW
387 if (MP_OBJ_IS_TYPE(value, &mp_type_memoryview)) {
388 src_items = (uint8_t*)src_items + (src_slice->free * item_sz);
389 }
390 #endif
Paul Sokolovsky56beb012015-04-16 00:48:36 +0300391 } else if (MP_OBJ_IS_TYPE(value, &mp_type_bytes)) {
392 if (item_sz != 1) {
393 goto compat_error;
394 }
395 mp_buffer_info_t bufinfo;
396 mp_get_buffer_raise(value, &bufinfo, MP_BUFFER_READ);
397 src_len = bufinfo.len;
398 src_items = bufinfo.buf;
399 } else {
400 mp_not_implemented("array/bytes required on right side");
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200401 }
402
403 // TODO: check src/dst compat
Paul Sokolovsky56beb012015-04-16 00:48:36 +0300404 mp_int_t len_adj = src_len - (slice.stop - slice.start);
Delio Brignoli6a388aa2015-06-06 20:14:08 +0200405 uint8_t* dest_items = o->items;
406 #if MICROPY_PY_BUILTINS_MEMORYVIEW
407 if (o->base.type == &mp_type_memoryview) {
408 if (len_adj != 0) {
Delio Brignoli32aba402015-06-03 07:07:35 +0200409 goto compat_error;
410 }
Delio Brignoli6a388aa2015-06-06 20:14:08 +0200411 dest_items += o->free * item_sz;
412 }
413 #endif
414 if (len_adj > 0) {
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200415 if (len_adj > o->free) {
416 // TODO: alloc policy; at the moment we go conservative
Damien Georged8914522015-02-27 09:54:12 +0000417 o->items = m_renew(byte, o->items, (o->len + o->free) * item_sz, (o->len + len_adj) * item_sz);
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200418 o->free = 0;
419 }
Delio Brignoli6a388aa2015-06-06 20:14:08 +0200420 mp_seq_replace_slice_grow_inplace(dest_items, o->len,
Paul Sokolovsky56beb012015-04-16 00:48:36 +0300421 slice.start, slice.stop, src_items, src_len, len_adj, item_sz);
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200422 } else {
Delio Brignoli6a388aa2015-06-06 20:14:08 +0200423 mp_seq_replace_slice_no_grow(dest_items, o->len,
424 slice.start, slice.stop, src_items, src_len, item_sz);
425 // Clear "freed" elements at the end of list
426 // TODO: This is actually only needed for typecode=='O'
427 mp_seq_clear(dest_items, o->len + len_adj, o->len, item_sz);
428 // TODO: alloc policy after shrinking
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200429 }
430 o->len += len_adj;
431 return mp_const_none;
432 #else
433 return MP_OBJ_NULL; // op not supported
434 #endif
435 }
436
Damien Georgedd4f4532014-10-23 13:34:35 +0100437 mp_obj_array_t *res;
Kaspar Schleiserf5dd6f72015-05-10 13:04:38 +0200438 size_t sz = mp_binary_get_size('@', o->typecode & TYPECODE_MASK, NULL);
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300439 assert(sz > 0);
Damien Georgedd4f4532014-10-23 13:34:35 +0100440 if (0) {
441 // dummy
442 #if MICROPY_PY_BUILTINS_MEMORYVIEW
443 } else if (o->base.type == &mp_type_memoryview) {
444 res = m_new_obj(mp_obj_array_t);
445 *res = *o;
Damien Georgede3c8062014-10-26 13:20:50 +0000446 res->free += slice.start;
Damien Georgedd4f4532014-10-23 13:34:35 +0100447 res->len = slice.stop - slice.start;
Damien Georgedd4f4532014-10-23 13:34:35 +0100448 #endif
449 } else {
450 res = array_new(o->typecode, slice.stop - slice.start);
stijn49c47da2014-10-28 17:20:52 +0100451 memcpy(res->items, (uint8_t*)o->items + slice.start * sz, (slice.stop - slice.start) * sz);
Damien Georgedd4f4532014-10-23 13:34:35 +0100452 }
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300453 return res;
Damien Georgec49ddb92014-06-01 13:49:35 +0100454#endif
Damien George729f7b42014-04-17 22:10:53 +0100455 } else {
Damien George42f3de92014-10-03 17:44:14 +0000456 mp_uint_t index = mp_get_index(o->base.type, o->len, index_in, false);
Damien Georgede3c8062014-10-26 13:20:50 +0000457 #if MICROPY_PY_BUILTINS_MEMORYVIEW
458 if (o->base.type == &mp_type_memoryview) {
459 index += o->free;
460 if (value != MP_OBJ_SENTINEL && (o->typecode & 0x80) == 0) {
461 // store to read-only memoryview
Damien Georgedd4f4532014-10-23 13:34:35 +0100462 return MP_OBJ_NULL;
463 }
Damien Georgede3c8062014-10-26 13:20:50 +0000464 }
465 #endif
466 if (value == MP_OBJ_SENTINEL) {
467 // load
468 return mp_binary_get_val_array(o->typecode & TYPECODE_MASK, o->items, index);
469 } else {
470 // store
471 mp_binary_set_val_array(o->typecode & TYPECODE_MASK, o->items, index, value);
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300472 return mp_const_none;
473 }
Damien George729f7b42014-04-17 22:10:53 +0100474 }
Damien Georgef4c9b332014-04-08 21:32:29 +0100475 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200476}
477
Damien George4d917232014-08-30 14:28:06 +0100478STATIC mp_int_t array_get_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200479 mp_obj_array_t *o = o_in;
Kaspar Schleiserf5dd6f72015-05-10 13:04:38 +0200480 size_t sz = mp_binary_get_size('@', o->typecode & TYPECODE_MASK, NULL);
Damien Georgede3c8062014-10-26 13:20:50 +0000481 bufinfo->buf = o->items;
482 bufinfo->len = o->len * sz;
483 bufinfo->typecode = o->typecode & TYPECODE_MASK;
Damien Georgedd4f4532014-10-23 13:34:35 +0100484 #if MICROPY_PY_BUILTINS_MEMORYVIEW
Damien Georgede3c8062014-10-26 13:20:50 +0000485 if (o->base.type == &mp_type_memoryview) {
486 if ((o->typecode & 0x80) == 0 && (flags & MP_BUFFER_WRITE)) {
487 // read-only memoryview
488 return 1;
489 }
stijn49c47da2014-10-28 17:20:52 +0100490 bufinfo->buf = (uint8_t*)bufinfo->buf + (mp_uint_t)o->free * sz;
Damien Georgedd4f4532014-10-23 13:34:35 +0100491 }
492 #endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200493 return 0;
494}
495
Damien Georgedd4f4532014-10-23 13:34:35 +0100496#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
Damien George9b196cd2014-03-26 21:47:19 +0000497STATIC const mp_map_elem_t array_locals_dict_table[] = {
498 { MP_OBJ_NEW_QSTR(MP_QSTR_append), (mp_obj_t)&array_append_obj },
Damien Georgeb2e73112014-11-30 00:00:55 +0000499 { MP_OBJ_NEW_QSTR(MP_QSTR_extend), (mp_obj_t)&array_extend_obj },
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200500};
501
Damien George9b196cd2014-03-26 21:47:19 +0000502STATIC MP_DEFINE_CONST_DICT(array_locals_dict, array_locals_dict_table);
Damien Georgedd4f4532014-10-23 13:34:35 +0100503#endif
Damien George9b196cd2014-03-26 21:47:19 +0000504
Damien Georgedd4f4532014-10-23 13:34:35 +0100505#if MICROPY_PY_ARRAY
Damien Georgecaac5422014-03-25 14:18:18 +0000506const mp_obj_type_t mp_type_array = {
Damien Georgec5966122014-02-15 16:10:44 +0000507 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000508 .name = MP_QSTR_array,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200509 .print = array_print,
510 .make_new = array_make_new,
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200511 .getiter = array_iterator_new,
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200512 .unary_op = array_unary_op,
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300513 .binary_op = array_binary_op,
Damien George729f7b42014-04-17 22:10:53 +0100514 .subscr = array_subscr,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200515 .buffer_p = { .get_buffer = array_get_buffer },
Damien George9b196cd2014-03-26 21:47:19 +0000516 .locals_dict = (mp_obj_t)&array_locals_dict,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200517};
Damien Georgedd4f4532014-10-23 13:34:35 +0100518#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200519
Damien Georgedd4f4532014-10-23 13:34:35 +0100520#if MICROPY_PY_BUILTINS_BYTEARRAY
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300521const mp_obj_type_t mp_type_bytearray = {
522 { &mp_type_type },
523 .name = MP_QSTR_bytearray,
524 .print = array_print,
525 .make_new = bytearray_make_new,
526 .getiter = array_iterator_new,
527 .unary_op = array_unary_op,
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300528 .binary_op = array_binary_op,
Damien George729f7b42014-04-17 22:10:53 +0100529 .subscr = array_subscr,
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300530 .buffer_p = { .get_buffer = array_get_buffer },
531 .locals_dict = (mp_obj_t)&array_locals_dict,
532};
Damien Georgedd4f4532014-10-23 13:34:35 +0100533#endif
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300534
Damien Georgedd4f4532014-10-23 13:34:35 +0100535#if MICROPY_PY_BUILTINS_MEMORYVIEW
536const mp_obj_type_t mp_type_memoryview = {
537 { &mp_type_type },
538 .name = MP_QSTR_memoryview,
539 .make_new = memoryview_make_new,
540 .getiter = array_iterator_new,
541 .unary_op = array_unary_op,
542 .binary_op = array_binary_op,
543 .subscr = array_subscr,
544 .buffer_p = { .get_buffer = array_get_buffer },
545};
546#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200547
Damien Georgedd4f4532014-10-23 13:34:35 +0100548/* unused
Damien Georged182b982014-08-30 14:19:41 +0100549mp_uint_t mp_obj_array_len(mp_obj_t self_in) {
Paul Sokolovsky33996682014-01-21 23:30:10 +0200550 return ((mp_obj_array_t *)self_in)->len;
551}
Damien Georgedd4f4532014-10-23 13:34:35 +0100552*/
Paul Sokolovsky33996682014-01-21 23:30:10 +0200553
Damien Georgedd4f4532014-10-23 13:34:35 +0100554#if MICROPY_PY_BUILTINS_BYTEARRAY
Damien George4abff752014-08-30 14:59:21 +0100555mp_obj_t mp_obj_new_bytearray(mp_uint_t n, void *items) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200556 mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, n);
557 memcpy(o->items, items, n);
558 return o;
559}
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200560
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200561// Create bytearray which references specified memory area
Damien Georged182b982014-08-30 14:19:41 +0100562mp_obj_t mp_obj_new_bytearray_by_ref(mp_uint_t n, void *items) {
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200563 mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
Damien Georgedd4f4532014-10-23 13:34:35 +0100564 o->base.type = &mp_type_bytearray;
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200565 o->typecode = BYTEARRAY_TYPECODE;
566 o->free = 0;
567 o->len = n;
568 o->items = items;
569 return o;
570}
Damien Georgedd4f4532014-10-23 13:34:35 +0100571#endif
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200572
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200573/******************************************************************************/
Damien Georgedd4f4532014-10-23 13:34:35 +0100574// array iterator
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200575
576typedef struct _mp_obj_array_it_t {
577 mp_obj_base_t base;
578 mp_obj_array_t *array;
Damien Georgede3c8062014-10-26 13:20:50 +0000579 mp_uint_t offset;
Damien George40f3c022014-07-03 13:25:24 +0100580 mp_uint_t cur;
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200581} mp_obj_array_it_t;
582
Damien Georgecaac5422014-03-25 14:18:18 +0000583STATIC mp_obj_t array_it_iternext(mp_obj_t self_in) {
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200584 mp_obj_array_it_t *self = self_in;
585 if (self->cur < self->array->len) {
Damien Georgede3c8062014-10-26 13:20:50 +0000586 return mp_binary_get_val_array(self->array->typecode & TYPECODE_MASK, self->array->items, self->offset + self->cur++);
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200587 } else {
Damien Georgeea8d06c2014-04-17 23:19:36 +0100588 return MP_OBJ_STOP_ITERATION;
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200589 }
590}
591
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200592STATIC const mp_obj_type_t array_it_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000593 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000594 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +0300595 .getiter = mp_identity,
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200596 .iternext = array_it_iternext,
597};
598
Damien Georgecaac5422014-03-25 14:18:18 +0000599STATIC mp_obj_t array_iterator_new(mp_obj_t array_in) {
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200600 mp_obj_array_t *array = array_in;
Damien Georgede3c8062014-10-26 13:20:50 +0000601 mp_obj_array_it_t *o = m_new0(mp_obj_array_it_t, 1);
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200602 o->base.type = &array_it_type;
603 o->array = array;
Damien Georgede3c8062014-10-26 13:20:50 +0000604 #if MICROPY_PY_BUILTINS_MEMORYVIEW
605 if (array->base.type == &mp_type_memoryview) {
606 o->offset = array->free;
607 }
608 #endif
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200609 return o;
610}
Paul Sokolovskycb78f862014-06-27 20:39:09 +0300611
Damien Georgedd4f4532014-10-23 13:34:35 +0100612#endif // MICROPY_PY_ARRAY || MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_BUILTINS_MEMORYVIEW