blob: 33cd7ecc3bd5ac7e4f7ba76dcea8991877cde05b [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);
105 if (typecode_size <= 0) {
106 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)
133 || MP_OBJ_IS_TYPE(initializer, &mp_type_bytearray))))
134 && 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)
137 int sz = mp_binary_get_size('@', typecode, NULL);
138 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
265 int sz = mp_binary_get_size('@', lhs_bufinfo.typecode, NULL);
266
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)
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300304 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_array) || MP_OBJ_IS_TYPE(self_in, &mp_type_bytearray));
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200305 mp_obj_array_t *self = self_in;
Damien Georgeb2e73112014-11-30 00:00:55 +0000306
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200307 if (self->free == 0) {
Paul Sokolovsky1355cf42014-04-19 01:25:49 +0300308 int item_sz = mp_binary_get_size('@', self->typecode, NULL);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200309 // TODO: alloc policy
310 self->free = 8;
Damien George4d77e1a2015-02-27 09:34:51 +0000311 self->items = m_renew(byte, self->items, item_sz * self->len, item_sz * (self->len + self->free));
Paul Sokolovskya2240672014-04-28 00:16:57 +0300312 mp_seq_clear(self->items, self->len + 1, self->len + self->free, item_sz);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200313 }
Paul Sokolovskyef9124f2014-04-11 03:46:09 +0300314 mp_binary_set_val_array(self->typecode, self->items, self->len++, arg);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200315 self->free--;
316 return mp_const_none; // return None, as per CPython
317}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200318STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_append_obj, array_append);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200319
Damien Georgeb2e73112014-11-30 00:00:55 +0000320STATIC mp_obj_t array_extend(mp_obj_t self_in, mp_obj_t arg_in) {
321 // self is not a memoryview, so we don't need to use (& TYPECODE_MASK)
322 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_array) || MP_OBJ_IS_TYPE(self_in, &mp_type_bytearray));
323 mp_obj_array_t *self = self_in;
324
Damien Georgec7ca01a2014-11-30 14:01:33 +0000325 // allow to extend by anything that has the buffer protocol (extension to CPython)
326 mp_buffer_info_t arg_bufinfo;
327 mp_get_buffer_raise(arg_in, &arg_bufinfo, MP_BUFFER_READ);
Damien Georgeb2e73112014-11-30 00:00:55 +0000328
329 int sz = mp_binary_get_size('@', self->typecode, NULL);
330
Damien Georgec7ca01a2014-11-30 14:01:33 +0000331 // convert byte count to element count
332 mp_uint_t len = arg_bufinfo.len / sz;
333
Damien Georgeb2e73112014-11-30 00:00:55 +0000334 // make sure we have enough room to extend
Damien Georgec7ca01a2014-11-30 14:01:33 +0000335 // TODO: alloc policy; at the moment we go conservative
336 if (self->free < len) {
Damien George4d77e1a2015-02-27 09:34:51 +0000337 self->items = m_renew(byte, self->items, (self->len + self->free) * sz, (self->len + len) * sz);
Damien Georgec7ca01a2014-11-30 14:01:33 +0000338 self->free = 0;
339 } else {
340 self->free -= len;
Damien Georgeb2e73112014-11-30 00:00:55 +0000341 }
342
343 // extend
Damien Georgec7ca01a2014-11-30 14:01:33 +0000344 mp_seq_copy((byte*)self->items + self->len * sz, arg_bufinfo.buf, len * sz, byte);
345 self->len += len;
Damien Georgeb2e73112014-11-30 00:00:55 +0000346
347 return mp_const_none;
348}
349STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_extend_obj, array_extend);
350#endif
351
Damien George729f7b42014-04-17 22:10:53 +0100352STATIC 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 +0100353 if (value == MP_OBJ_NULL) {
Damien George32ca1642014-04-18 22:04:06 +0100354 // delete item
355 // TODO implement
Paul Sokolovsky26905252014-04-20 20:58:33 +0300356 // TODO: confirmed that both bytearray and array.array support
357 // slice deletion
Damien George6ac5dce2014-05-21 19:42:43 +0100358 return MP_OBJ_NULL; // op not supported
Damien George729f7b42014-04-17 22:10:53 +0100359 } else {
360 mp_obj_array_t *o = self_in;
Damien Georgec49ddb92014-06-01 13:49:35 +0100361 if (0) {
362#if MICROPY_PY_BUILTINS_SLICE
363 } else if (MP_OBJ_IS_TYPE(index_in, &mp_type_slice)) {
Paul Sokolovskyde4b9322014-05-25 21:21:57 +0300364 mp_bound_slice_t slice;
365 if (!mp_seq_get_fast_slice_indexes(o->len, index_in, &slice)) {
Paul Sokolovsky5fd5af92014-05-25 22:12:56 +0300366 nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
Damien George11de8392014-06-05 18:57:38 +0100367 "only slices with step=1 (aka None) are supported"));
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300368 }
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200369 if (value != MP_OBJ_SENTINEL) {
370 #if MICROPY_PY_ARRAY_SLICE_ASSIGN
371 // Assign
Paul Sokolovsky56beb012015-04-16 00:48:36 +0300372 mp_uint_t src_len;
373 void *src_items;
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200374 int item_sz = mp_binary_get_size('@', o->typecode, NULL);
Paul Sokolovsky56beb012015-04-16 00:48:36 +0300375 if (MP_OBJ_IS_TYPE(value, &mp_type_array) || MP_OBJ_IS_TYPE(value, &mp_type_bytearray)) {
376 mp_obj_array_t *src_slice = value;
377 if (item_sz != mp_binary_get_size('@', src_slice->typecode, NULL)) {
378 compat_error:
379 mp_not_implemented("lhs and rhs should be compatible");
380 }
381 src_len = src_slice->len;
382 src_items = src_slice->items;
383 } else if (MP_OBJ_IS_TYPE(value, &mp_type_bytes)) {
384 if (item_sz != 1) {
385 goto compat_error;
386 }
387 mp_buffer_info_t bufinfo;
388 mp_get_buffer_raise(value, &bufinfo, MP_BUFFER_READ);
389 src_len = bufinfo.len;
390 src_items = bufinfo.buf;
391 } else {
392 mp_not_implemented("array/bytes required on right side");
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200393 }
394
395 // TODO: check src/dst compat
Paul Sokolovsky56beb012015-04-16 00:48:36 +0300396 mp_int_t len_adj = src_len - (slice.stop - slice.start);
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200397 if (len_adj > 0) {
398 if (len_adj > o->free) {
399 // TODO: alloc policy; at the moment we go conservative
Damien Georged8914522015-02-27 09:54:12 +0000400 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 +0200401 o->free = 0;
402 }
403 mp_seq_replace_slice_grow_inplace(o->items, o->len,
Paul Sokolovsky56beb012015-04-16 00:48:36 +0300404 slice.start, slice.stop, src_items, src_len, len_adj, item_sz);
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200405 } else {
406 mp_seq_replace_slice_no_grow(o->items, o->len,
Paul Sokolovsky56beb012015-04-16 00:48:36 +0300407 slice.start, slice.stop, src_items, src_len, item_sz);
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200408 // Clear "freed" elements at the end of list
409 // TODO: This is actually only needed for typecode=='O'
410 mp_seq_clear(o->items, o->len + len_adj, o->len, item_sz);
411 // TODO: alloc policy after shrinking
412 }
413 o->len += len_adj;
414 return mp_const_none;
415 #else
416 return MP_OBJ_NULL; // op not supported
417 #endif
418 }
419
Damien Georgedd4f4532014-10-23 13:34:35 +0100420 mp_obj_array_t *res;
Damien Georgede3c8062014-10-26 13:20:50 +0000421 int sz = mp_binary_get_size('@', o->typecode & TYPECODE_MASK, NULL);
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300422 assert(sz > 0);
Damien Georgedd4f4532014-10-23 13:34:35 +0100423 if (0) {
424 // dummy
425 #if MICROPY_PY_BUILTINS_MEMORYVIEW
426 } else if (o->base.type == &mp_type_memoryview) {
427 res = m_new_obj(mp_obj_array_t);
428 *res = *o;
Damien Georgede3c8062014-10-26 13:20:50 +0000429 res->free += slice.start;
Damien Georgedd4f4532014-10-23 13:34:35 +0100430 res->len = slice.stop - slice.start;
Damien Georgedd4f4532014-10-23 13:34:35 +0100431 #endif
432 } else {
433 res = array_new(o->typecode, slice.stop - slice.start);
stijn49c47da2014-10-28 17:20:52 +0100434 memcpy(res->items, (uint8_t*)o->items + slice.start * sz, (slice.stop - slice.start) * sz);
Damien Georgedd4f4532014-10-23 13:34:35 +0100435 }
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300436 return res;
Damien Georgec49ddb92014-06-01 13:49:35 +0100437#endif
Damien George729f7b42014-04-17 22:10:53 +0100438 } else {
Damien George42f3de92014-10-03 17:44:14 +0000439 mp_uint_t index = mp_get_index(o->base.type, o->len, index_in, false);
Damien Georgede3c8062014-10-26 13:20:50 +0000440 #if MICROPY_PY_BUILTINS_MEMORYVIEW
441 if (o->base.type == &mp_type_memoryview) {
442 index += o->free;
443 if (value != MP_OBJ_SENTINEL && (o->typecode & 0x80) == 0) {
444 // store to read-only memoryview
Damien Georgedd4f4532014-10-23 13:34:35 +0100445 return MP_OBJ_NULL;
446 }
Damien Georgede3c8062014-10-26 13:20:50 +0000447 }
448 #endif
449 if (value == MP_OBJ_SENTINEL) {
450 // load
451 return mp_binary_get_val_array(o->typecode & TYPECODE_MASK, o->items, index);
452 } else {
453 // store
454 mp_binary_set_val_array(o->typecode & TYPECODE_MASK, o->items, index, value);
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300455 return mp_const_none;
456 }
Damien George729f7b42014-04-17 22:10:53 +0100457 }
Damien Georgef4c9b332014-04-08 21:32:29 +0100458 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200459}
460
Damien George4d917232014-08-30 14:28:06 +0100461STATIC 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 +0200462 mp_obj_array_t *o = o_in;
Damien Georgede3c8062014-10-26 13:20:50 +0000463 int sz = mp_binary_get_size('@', o->typecode & TYPECODE_MASK, NULL);
464 bufinfo->buf = o->items;
465 bufinfo->len = o->len * sz;
466 bufinfo->typecode = o->typecode & TYPECODE_MASK;
Damien Georgedd4f4532014-10-23 13:34:35 +0100467 #if MICROPY_PY_BUILTINS_MEMORYVIEW
Damien Georgede3c8062014-10-26 13:20:50 +0000468 if (o->base.type == &mp_type_memoryview) {
469 if ((o->typecode & 0x80) == 0 && (flags & MP_BUFFER_WRITE)) {
470 // read-only memoryview
471 return 1;
472 }
stijn49c47da2014-10-28 17:20:52 +0100473 bufinfo->buf = (uint8_t*)bufinfo->buf + (mp_uint_t)o->free * sz;
Damien Georgedd4f4532014-10-23 13:34:35 +0100474 }
475 #endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200476 return 0;
477}
478
Damien Georgedd4f4532014-10-23 13:34:35 +0100479#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
Damien George9b196cd2014-03-26 21:47:19 +0000480STATIC const mp_map_elem_t array_locals_dict_table[] = {
481 { MP_OBJ_NEW_QSTR(MP_QSTR_append), (mp_obj_t)&array_append_obj },
Damien Georgeb2e73112014-11-30 00:00:55 +0000482 { MP_OBJ_NEW_QSTR(MP_QSTR_extend), (mp_obj_t)&array_extend_obj },
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200483};
484
Damien George9b196cd2014-03-26 21:47:19 +0000485STATIC MP_DEFINE_CONST_DICT(array_locals_dict, array_locals_dict_table);
Damien Georgedd4f4532014-10-23 13:34:35 +0100486#endif
Damien George9b196cd2014-03-26 21:47:19 +0000487
Damien Georgedd4f4532014-10-23 13:34:35 +0100488#if MICROPY_PY_ARRAY
Damien Georgecaac5422014-03-25 14:18:18 +0000489const mp_obj_type_t mp_type_array = {
Damien Georgec5966122014-02-15 16:10:44 +0000490 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000491 .name = MP_QSTR_array,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200492 .print = array_print,
493 .make_new = array_make_new,
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200494 .getiter = array_iterator_new,
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200495 .unary_op = array_unary_op,
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300496 .binary_op = array_binary_op,
Damien George729f7b42014-04-17 22:10:53 +0100497 .subscr = array_subscr,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200498 .buffer_p = { .get_buffer = array_get_buffer },
Damien George9b196cd2014-03-26 21:47:19 +0000499 .locals_dict = (mp_obj_t)&array_locals_dict,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200500};
Damien Georgedd4f4532014-10-23 13:34:35 +0100501#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200502
Damien Georgedd4f4532014-10-23 13:34:35 +0100503#if MICROPY_PY_BUILTINS_BYTEARRAY
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300504const mp_obj_type_t mp_type_bytearray = {
505 { &mp_type_type },
506 .name = MP_QSTR_bytearray,
507 .print = array_print,
508 .make_new = bytearray_make_new,
509 .getiter = array_iterator_new,
510 .unary_op = array_unary_op,
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300511 .binary_op = array_binary_op,
Damien George729f7b42014-04-17 22:10:53 +0100512 .subscr = array_subscr,
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300513 .buffer_p = { .get_buffer = array_get_buffer },
514 .locals_dict = (mp_obj_t)&array_locals_dict,
515};
Damien Georgedd4f4532014-10-23 13:34:35 +0100516#endif
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300517
Damien Georgedd4f4532014-10-23 13:34:35 +0100518#if MICROPY_PY_BUILTINS_MEMORYVIEW
519const mp_obj_type_t mp_type_memoryview = {
520 { &mp_type_type },
521 .name = MP_QSTR_memoryview,
522 .make_new = memoryview_make_new,
523 .getiter = array_iterator_new,
524 .unary_op = array_unary_op,
525 .binary_op = array_binary_op,
526 .subscr = array_subscr,
527 .buffer_p = { .get_buffer = array_get_buffer },
528};
529#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200530
Damien Georgedd4f4532014-10-23 13:34:35 +0100531/* unused
Damien Georged182b982014-08-30 14:19:41 +0100532mp_uint_t mp_obj_array_len(mp_obj_t self_in) {
Paul Sokolovsky33996682014-01-21 23:30:10 +0200533 return ((mp_obj_array_t *)self_in)->len;
534}
Damien Georgedd4f4532014-10-23 13:34:35 +0100535*/
Paul Sokolovsky33996682014-01-21 23:30:10 +0200536
Damien Georgedd4f4532014-10-23 13:34:35 +0100537#if MICROPY_PY_BUILTINS_BYTEARRAY
Damien George4abff752014-08-30 14:59:21 +0100538mp_obj_t mp_obj_new_bytearray(mp_uint_t n, void *items) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200539 mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, n);
540 memcpy(o->items, items, n);
541 return o;
542}
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200543
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200544// Create bytearray which references specified memory area
Damien Georged182b982014-08-30 14:19:41 +0100545mp_obj_t mp_obj_new_bytearray_by_ref(mp_uint_t n, void *items) {
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200546 mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
Damien Georgedd4f4532014-10-23 13:34:35 +0100547 o->base.type = &mp_type_bytearray;
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200548 o->typecode = BYTEARRAY_TYPECODE;
549 o->free = 0;
550 o->len = n;
551 o->items = items;
552 return o;
553}
Damien Georgedd4f4532014-10-23 13:34:35 +0100554#endif
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200555
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200556/******************************************************************************/
Damien Georgedd4f4532014-10-23 13:34:35 +0100557// array iterator
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200558
559typedef struct _mp_obj_array_it_t {
560 mp_obj_base_t base;
561 mp_obj_array_t *array;
Damien Georgede3c8062014-10-26 13:20:50 +0000562 mp_uint_t offset;
Damien George40f3c022014-07-03 13:25:24 +0100563 mp_uint_t cur;
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200564} mp_obj_array_it_t;
565
Damien Georgecaac5422014-03-25 14:18:18 +0000566STATIC mp_obj_t array_it_iternext(mp_obj_t self_in) {
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200567 mp_obj_array_it_t *self = self_in;
568 if (self->cur < self->array->len) {
Damien Georgede3c8062014-10-26 13:20:50 +0000569 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 +0200570 } else {
Damien Georgeea8d06c2014-04-17 23:19:36 +0100571 return MP_OBJ_STOP_ITERATION;
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200572 }
573}
574
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200575STATIC const mp_obj_type_t array_it_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000576 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000577 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +0300578 .getiter = mp_identity,
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200579 .iternext = array_it_iternext,
580};
581
Damien Georgecaac5422014-03-25 14:18:18 +0000582STATIC mp_obj_t array_iterator_new(mp_obj_t array_in) {
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200583 mp_obj_array_t *array = array_in;
Damien Georgede3c8062014-10-26 13:20:50 +0000584 mp_obj_array_it_t *o = m_new0(mp_obj_array_it_t, 1);
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200585 o->base.type = &array_it_type;
586 o->array = array;
Damien Georgede3c8062014-10-26 13:20:50 +0000587 #if MICROPY_PY_BUILTINS_MEMORYVIEW
588 if (array->base.type == &mp_type_memoryview) {
589 o->offset = array->free;
590 }
591 #endif
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200592 return o;
593}
Paul Sokolovskycb78f862014-06-27 20:39:09 +0300594
Damien Georgedd4f4532014-10-23 13:34:35 +0100595#endif // MICROPY_PY_ARRAY || MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_BUILTINS_MEMORYVIEW