blob: 25d9daea97d5e12c218dbe68b69481d461684335 [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
57#define TYPECODE_MASK (~(mp_uint_t)1)
58#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
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020079STATIC void array_print(void (*print)(void *env, const char *fmt, ...), void *env, 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 George54671862014-07-17 21:56:32 +010083 print(env, "bytearray(b");
Paul Sokolovsky2ec38a12014-06-13 21:23:00 +030084 mp_str_print_quoted(print, env, o->items, o->len, true);
Paul Sokolovsky427905c2014-01-18 19:24:47 +020085 } else {
Paul Sokolovsky7e652af2014-01-28 03:14:20 +020086 print(env, "array('%c'", o->typecode);
Paul Sokolovsky18014212014-01-28 03:40:48 +020087 if (o->len > 0) {
Damien George32ca1642014-04-18 22:04:06 +010088 print(env, ", [");
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) {
91 print(env, ", ");
92 }
Paul Sokolovskyef9124f2014-04-11 03:46:09 +030093 mp_obj_print_helper(print, env, mp_binary_get_val_array(o->typecode, o->items, i), PRINT_REPR);
Paul Sokolovsky7e652af2014-01-28 03:14:20 +020094 }
Paul Sokolovsky18014212014-01-28 03:40:48 +020095 print(env, "]");
Paul Sokolovsky427905c2014-01-18 19:24:47 +020096 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +020097 }
Paul Sokolovsky7e652af2014-01-28 03:14:20 +020098 print(env, ")");
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;
119 o->items = m_malloc(typecode_size * o->len);
120 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
211STATIC 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) {
212 // TODO possibly allow memoryview constructor to take start/stop so that one
213 // can do memoryview(b, 4, 8) instead of memoryview(b)[4:8] (uses less RAM)
214
215 mp_arg_check_num(n_args, n_kw, 1, 1, false);
216
217 mp_buffer_info_t bufinfo;
218 mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
219
220 mp_obj_array_t *self = m_new_obj(mp_obj_array_t);
221 self->base.type = type_in;
222 self->typecode = bufinfo.typecode;
223 self->free = 0;
224 self->len = bufinfo.len / mp_binary_get_size('@', bufinfo.typecode, NULL); // element len
225 self->items = bufinfo.buf;
226
227 // test if the object can be written to
228 if (mp_get_buffer(args[0], &bufinfo, MP_BUFFER_RW)) {
Damien Georgede3c8062014-10-26 13:20:50 +0000229 self->typecode |= 0x80; // used to indicate writable buffer
Damien Georgedd4f4532014-10-23 13:34:35 +0100230 }
231
232 return self;
233}
234#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200235
Damien Georgeecc88e92014-08-30 00:35:11 +0100236STATIC mp_obj_t array_unary_op(mp_uint_t op, mp_obj_t o_in) {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200237 mp_obj_array_t *o = o_in;
238 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100239 case MP_UNARY_OP_BOOL: return MP_BOOL(o->len != 0);
240 case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(o->len);
Damien George6ac5dce2014-05-21 19:42:43 +0100241 default: return MP_OBJ_NULL; // op not supported
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200242 }
243}
244
Damien Georgeecc88e92014-08-30 00:35:11 +0100245STATIC 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 +0000246 mp_obj_array_t *lhs = lhs_in;
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300247 switch (op) {
Damien Georgeb2e73112014-11-30 00:00:55 +0000248 case MP_BINARY_OP_ADD: {
Damien Georgec7ca01a2014-11-30 14:01:33 +0000249 // allow to add anything that has the buffer protocol (extension to CPython)
250 mp_buffer_info_t lhs_bufinfo;
251 mp_buffer_info_t rhs_bufinfo;
252 array_get_buffer(lhs_in, &lhs_bufinfo, MP_BUFFER_READ);
253 mp_get_buffer_raise(rhs_in, &rhs_bufinfo, MP_BUFFER_READ);
254
255 int sz = mp_binary_get_size('@', lhs_bufinfo.typecode, NULL);
256
257 // convert byte count to element count (in case rhs is not multiple of sz)
258 mp_uint_t rhs_len = rhs_bufinfo.len / sz;
259
260 // note: lhs->len is element count of lhs, lhs_bufinfo.len is byte count
261 mp_obj_array_t *res = array_new(lhs_bufinfo.typecode, lhs->len + rhs_len);
262 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 +0000263 return res;
264 }
265
266 case MP_BINARY_OP_INPLACE_ADD: {
267 #if MICROPY_PY_BUILTINS_MEMORYVIEW
268 if (lhs->base.type == &mp_type_memoryview) {
269 return MP_OBJ_NULL; // op not supported
270 }
271 #endif
272 array_extend(lhs, rhs_in);
273 return lhs;
274 }
275
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300276 case MP_BINARY_OP_EQUAL: {
277 mp_buffer_info_t lhs_bufinfo;
278 mp_buffer_info_t rhs_bufinfo;
279 array_get_buffer(lhs_in, &lhs_bufinfo, MP_BUFFER_READ);
280 if (!mp_get_buffer(rhs_in, &rhs_bufinfo, MP_BUFFER_READ)) {
Damien Georgede3c8062014-10-26 13:20:50 +0000281 return mp_const_false;
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300282 }
283 return MP_BOOL(mp_seq_cmp_bytes(op, lhs_bufinfo.buf, lhs_bufinfo.len, rhs_bufinfo.buf, rhs_bufinfo.len));
284 }
Damien Georgeb2e73112014-11-30 00:00:55 +0000285
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300286 default:
287 return MP_OBJ_NULL; // op not supported
288 }
289}
290
Damien Georgeb2e73112014-11-30 00:00:55 +0000291#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200292STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg) {
Damien Georgeb2e73112014-11-30 00:00:55 +0000293 // self is not a memoryview, so we don't need to use (& TYPECODE_MASK)
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300294 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 +0200295 mp_obj_array_t *self = self_in;
Damien Georgeb2e73112014-11-30 00:00:55 +0000296
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200297 if (self->free == 0) {
Paul Sokolovsky1355cf42014-04-19 01:25:49 +0300298 int item_sz = mp_binary_get_size('@', self->typecode, NULL);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200299 // TODO: alloc policy
300 self->free = 8;
301 self->items = m_realloc(self->items, item_sz * self->len, item_sz * (self->len + self->free));
Paul Sokolovskya2240672014-04-28 00:16:57 +0300302 mp_seq_clear(self->items, self->len + 1, self->len + self->free, item_sz);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200303 }
Paul Sokolovskyef9124f2014-04-11 03:46:09 +0300304 mp_binary_set_val_array(self->typecode, self->items, self->len++, arg);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200305 self->free--;
306 return mp_const_none; // return None, as per CPython
307}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200308STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_append_obj, array_append);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200309
Damien Georgeb2e73112014-11-30 00:00:55 +0000310STATIC mp_obj_t array_extend(mp_obj_t self_in, mp_obj_t arg_in) {
311 // self is not a memoryview, so we don't need to use (& TYPECODE_MASK)
312 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_array) || MP_OBJ_IS_TYPE(self_in, &mp_type_bytearray));
313 mp_obj_array_t *self = self_in;
314
Damien Georgec7ca01a2014-11-30 14:01:33 +0000315 // allow to extend by anything that has the buffer protocol (extension to CPython)
316 mp_buffer_info_t arg_bufinfo;
317 mp_get_buffer_raise(arg_in, &arg_bufinfo, MP_BUFFER_READ);
Damien Georgeb2e73112014-11-30 00:00:55 +0000318
319 int sz = mp_binary_get_size('@', self->typecode, NULL);
320
Damien Georgec7ca01a2014-11-30 14:01:33 +0000321 // convert byte count to element count
322 mp_uint_t len = arg_bufinfo.len / sz;
323
Damien Georgeb2e73112014-11-30 00:00:55 +0000324 // make sure we have enough room to extend
Damien Georgec7ca01a2014-11-30 14:01:33 +0000325 // TODO: alloc policy; at the moment we go conservative
326 if (self->free < len) {
327 self->items = m_realloc(self->items, (self->len + self->free) * sz, (self->len + len) * sz);
328 self->free = 0;
329 } else {
330 self->free -= len;
Damien Georgeb2e73112014-11-30 00:00:55 +0000331 }
332
333 // extend
Damien Georgec7ca01a2014-11-30 14:01:33 +0000334 mp_seq_copy((byte*)self->items + self->len * sz, arg_bufinfo.buf, len * sz, byte);
335 self->len += len;
Damien Georgeb2e73112014-11-30 00:00:55 +0000336
337 return mp_const_none;
338}
339STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_extend_obj, array_extend);
340#endif
341
Damien George729f7b42014-04-17 22:10:53 +0100342STATIC 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 +0100343 if (value == MP_OBJ_NULL) {
Damien George32ca1642014-04-18 22:04:06 +0100344 // delete item
345 // TODO implement
Paul Sokolovsky26905252014-04-20 20:58:33 +0300346 // TODO: confirmed that both bytearray and array.array support
347 // slice deletion
Damien George6ac5dce2014-05-21 19:42:43 +0100348 return MP_OBJ_NULL; // op not supported
Damien George729f7b42014-04-17 22:10:53 +0100349 } else {
350 mp_obj_array_t *o = self_in;
Damien Georgec49ddb92014-06-01 13:49:35 +0100351 if (0) {
352#if MICROPY_PY_BUILTINS_SLICE
353 } else if (MP_OBJ_IS_TYPE(index_in, &mp_type_slice)) {
Paul Sokolovsky26905252014-04-20 20:58:33 +0300354 if (value != MP_OBJ_SENTINEL) {
355 // Only getting a slice is suported so far, not assignment
356 // TODO: confirmed that both bytearray and array.array support
357 // slice assignment (incl. of different size)
Damien George6ac5dce2014-05-21 19:42:43 +0100358 return MP_OBJ_NULL; // op not supported
Paul Sokolovsky26905252014-04-20 20:58:33 +0300359 }
Paul Sokolovskyde4b9322014-05-25 21:21:57 +0300360 mp_bound_slice_t slice;
361 if (!mp_seq_get_fast_slice_indexes(o->len, index_in, &slice)) {
Paul Sokolovsky5fd5af92014-05-25 22:12:56 +0300362 nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
Damien George11de8392014-06-05 18:57:38 +0100363 "only slices with step=1 (aka None) are supported"));
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300364 }
Damien Georgedd4f4532014-10-23 13:34:35 +0100365 mp_obj_array_t *res;
Damien Georgede3c8062014-10-26 13:20:50 +0000366 int sz = mp_binary_get_size('@', o->typecode & TYPECODE_MASK, NULL);
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300367 assert(sz > 0);
Damien Georgedd4f4532014-10-23 13:34:35 +0100368 if (0) {
369 // dummy
370 #if MICROPY_PY_BUILTINS_MEMORYVIEW
371 } else if (o->base.type == &mp_type_memoryview) {
372 res = m_new_obj(mp_obj_array_t);
373 *res = *o;
Damien Georgede3c8062014-10-26 13:20:50 +0000374 res->free += slice.start;
Damien Georgedd4f4532014-10-23 13:34:35 +0100375 res->len = slice.stop - slice.start;
Damien Georgedd4f4532014-10-23 13:34:35 +0100376 #endif
377 } else {
378 res = array_new(o->typecode, slice.stop - slice.start);
stijn49c47da2014-10-28 17:20:52 +0100379 memcpy(res->items, (uint8_t*)o->items + slice.start * sz, (slice.stop - slice.start) * sz);
Damien Georgedd4f4532014-10-23 13:34:35 +0100380 }
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300381 return res;
Damien Georgec49ddb92014-06-01 13:49:35 +0100382#endif
Damien George729f7b42014-04-17 22:10:53 +0100383 } else {
Damien George42f3de92014-10-03 17:44:14 +0000384 mp_uint_t index = mp_get_index(o->base.type, o->len, index_in, false);
Damien Georgede3c8062014-10-26 13:20:50 +0000385 #if MICROPY_PY_BUILTINS_MEMORYVIEW
386 if (o->base.type == &mp_type_memoryview) {
387 index += o->free;
388 if (value != MP_OBJ_SENTINEL && (o->typecode & 0x80) == 0) {
389 // store to read-only memoryview
Damien Georgedd4f4532014-10-23 13:34:35 +0100390 return MP_OBJ_NULL;
391 }
Damien Georgede3c8062014-10-26 13:20:50 +0000392 }
393 #endif
394 if (value == MP_OBJ_SENTINEL) {
395 // load
396 return mp_binary_get_val_array(o->typecode & TYPECODE_MASK, o->items, index);
397 } else {
398 // store
399 mp_binary_set_val_array(o->typecode & TYPECODE_MASK, o->items, index, value);
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300400 return mp_const_none;
401 }
Damien George729f7b42014-04-17 22:10:53 +0100402 }
Damien Georgef4c9b332014-04-08 21:32:29 +0100403 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200404}
405
Damien George4d917232014-08-30 14:28:06 +0100406STATIC 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 +0200407 mp_obj_array_t *o = o_in;
Damien Georgede3c8062014-10-26 13:20:50 +0000408 int sz = mp_binary_get_size('@', o->typecode & TYPECODE_MASK, NULL);
409 bufinfo->buf = o->items;
410 bufinfo->len = o->len * sz;
411 bufinfo->typecode = o->typecode & TYPECODE_MASK;
Damien Georgedd4f4532014-10-23 13:34:35 +0100412 #if MICROPY_PY_BUILTINS_MEMORYVIEW
Damien Georgede3c8062014-10-26 13:20:50 +0000413 if (o->base.type == &mp_type_memoryview) {
414 if ((o->typecode & 0x80) == 0 && (flags & MP_BUFFER_WRITE)) {
415 // read-only memoryview
416 return 1;
417 }
stijn49c47da2014-10-28 17:20:52 +0100418 bufinfo->buf = (uint8_t*)bufinfo->buf + (mp_uint_t)o->free * sz;
Damien Georgedd4f4532014-10-23 13:34:35 +0100419 }
420 #endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200421 return 0;
422}
423
Damien Georgedd4f4532014-10-23 13:34:35 +0100424#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
Damien George9b196cd2014-03-26 21:47:19 +0000425STATIC const mp_map_elem_t array_locals_dict_table[] = {
426 { MP_OBJ_NEW_QSTR(MP_QSTR_append), (mp_obj_t)&array_append_obj },
Damien Georgeb2e73112014-11-30 00:00:55 +0000427 { MP_OBJ_NEW_QSTR(MP_QSTR_extend), (mp_obj_t)&array_extend_obj },
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200428};
429
Damien George9b196cd2014-03-26 21:47:19 +0000430STATIC MP_DEFINE_CONST_DICT(array_locals_dict, array_locals_dict_table);
Damien Georgedd4f4532014-10-23 13:34:35 +0100431#endif
Damien George9b196cd2014-03-26 21:47:19 +0000432
Damien Georgedd4f4532014-10-23 13:34:35 +0100433#if MICROPY_PY_ARRAY
Damien Georgecaac5422014-03-25 14:18:18 +0000434const mp_obj_type_t mp_type_array = {
Damien Georgec5966122014-02-15 16:10:44 +0000435 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000436 .name = MP_QSTR_array,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200437 .print = array_print,
438 .make_new = array_make_new,
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200439 .getiter = array_iterator_new,
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200440 .unary_op = array_unary_op,
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300441 .binary_op = array_binary_op,
Damien George729f7b42014-04-17 22:10:53 +0100442 .subscr = array_subscr,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200443 .buffer_p = { .get_buffer = array_get_buffer },
Damien George9b196cd2014-03-26 21:47:19 +0000444 .locals_dict = (mp_obj_t)&array_locals_dict,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200445};
Damien Georgedd4f4532014-10-23 13:34:35 +0100446#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200447
Damien Georgedd4f4532014-10-23 13:34:35 +0100448#if MICROPY_PY_BUILTINS_BYTEARRAY
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300449const mp_obj_type_t mp_type_bytearray = {
450 { &mp_type_type },
451 .name = MP_QSTR_bytearray,
452 .print = array_print,
453 .make_new = bytearray_make_new,
454 .getiter = array_iterator_new,
455 .unary_op = array_unary_op,
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300456 .binary_op = array_binary_op,
Damien George729f7b42014-04-17 22:10:53 +0100457 .subscr = array_subscr,
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300458 .buffer_p = { .get_buffer = array_get_buffer },
459 .locals_dict = (mp_obj_t)&array_locals_dict,
460};
Damien Georgedd4f4532014-10-23 13:34:35 +0100461#endif
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300462
Damien Georgedd4f4532014-10-23 13:34:35 +0100463#if MICROPY_PY_BUILTINS_MEMORYVIEW
464const mp_obj_type_t mp_type_memoryview = {
465 { &mp_type_type },
466 .name = MP_QSTR_memoryview,
467 .make_new = memoryview_make_new,
468 .getiter = array_iterator_new,
469 .unary_op = array_unary_op,
470 .binary_op = array_binary_op,
471 .subscr = array_subscr,
472 .buffer_p = { .get_buffer = array_get_buffer },
473};
474#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200475
Damien Georgedd4f4532014-10-23 13:34:35 +0100476/* unused
Damien Georged182b982014-08-30 14:19:41 +0100477mp_uint_t mp_obj_array_len(mp_obj_t self_in) {
Paul Sokolovsky33996682014-01-21 23:30:10 +0200478 return ((mp_obj_array_t *)self_in)->len;
479}
Damien Georgedd4f4532014-10-23 13:34:35 +0100480*/
Paul Sokolovsky33996682014-01-21 23:30:10 +0200481
Damien Georgedd4f4532014-10-23 13:34:35 +0100482#if MICROPY_PY_BUILTINS_BYTEARRAY
Damien George4abff752014-08-30 14:59:21 +0100483mp_obj_t mp_obj_new_bytearray(mp_uint_t n, void *items) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200484 mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, n);
485 memcpy(o->items, items, n);
486 return o;
487}
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200488
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200489// Create bytearray which references specified memory area
Damien Georged182b982014-08-30 14:19:41 +0100490mp_obj_t mp_obj_new_bytearray_by_ref(mp_uint_t n, void *items) {
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200491 mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
Damien Georgedd4f4532014-10-23 13:34:35 +0100492 o->base.type = &mp_type_bytearray;
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200493 o->typecode = BYTEARRAY_TYPECODE;
494 o->free = 0;
495 o->len = n;
496 o->items = items;
497 return o;
498}
Damien Georgedd4f4532014-10-23 13:34:35 +0100499#endif
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200500
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200501/******************************************************************************/
Damien Georgedd4f4532014-10-23 13:34:35 +0100502// array iterator
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200503
504typedef struct _mp_obj_array_it_t {
505 mp_obj_base_t base;
506 mp_obj_array_t *array;
Damien Georgede3c8062014-10-26 13:20:50 +0000507 mp_uint_t offset;
Damien George40f3c022014-07-03 13:25:24 +0100508 mp_uint_t cur;
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200509} mp_obj_array_it_t;
510
Damien Georgecaac5422014-03-25 14:18:18 +0000511STATIC mp_obj_t array_it_iternext(mp_obj_t self_in) {
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200512 mp_obj_array_it_t *self = self_in;
513 if (self->cur < self->array->len) {
Damien Georgede3c8062014-10-26 13:20:50 +0000514 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 +0200515 } else {
Damien Georgeea8d06c2014-04-17 23:19:36 +0100516 return MP_OBJ_STOP_ITERATION;
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200517 }
518}
519
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200520STATIC const mp_obj_type_t array_it_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000521 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000522 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +0300523 .getiter = mp_identity,
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200524 .iternext = array_it_iternext,
525};
526
Damien Georgecaac5422014-03-25 14:18:18 +0000527STATIC mp_obj_t array_iterator_new(mp_obj_t array_in) {
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200528 mp_obj_array_t *array = array_in;
Damien Georgede3c8062014-10-26 13:20:50 +0000529 mp_obj_array_it_t *o = m_new0(mp_obj_array_it_t, 1);
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200530 o->base.type = &array_it_type;
531 o->array = array;
Damien Georgede3c8062014-10-26 13:20:50 +0000532 #if MICROPY_PY_BUILTINS_MEMORYVIEW
533 if (array->base.type == &mp_type_memoryview) {
534 o->offset = array->free;
535 }
536 #endif
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200537 return o;
538}
Paul Sokolovskycb78f862014-06-27 20:39:09 +0300539
Damien Georgedd4f4532014-10-23 13:34:35 +0100540#endif // MICROPY_PY_ARRAY || MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_BUILTINS_MEMORYVIEW