blob: 7046541e761f80db39a55d6bb2438bd936bf903f [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
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030032#include "mpconfig.h"
Paul Sokolovsky427905c2014-01-18 19:24:47 +020033#include "nlr.h"
34#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000035#include "qstr.h"
Paul Sokolovsky427905c2014-01-18 19:24:47 +020036#include "obj.h"
Paul Sokolovsky427905c2014-01-18 19:24:47 +020037#include "runtime0.h"
38#include "runtime.h"
Paul Sokolovskyc2033242014-02-14 20:21:50 +020039#include "binary.h"
Paul Sokolovsky427905c2014-01-18 19:24:47 +020040
Damien Georgedd4f4532014-10-23 13:34:35 +010041#if MICROPY_PY_ARRAY || MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_BUILTINS_MEMORYVIEW
Paul Sokolovskycb78f862014-06-27 20:39:09 +030042
Damien Georgede3c8062014-10-26 13:20:50 +000043// About memoryview object: We want to reuse as much code as possible from
44// array, and keep the memoryview object 4 words in size so it fits in 1 GC
45// block. Also, memoryview must keep a pointer to the base of the buffer so
46// that the buffer is not GC'd if the original parent object is no longer
47// around (we are assuming that all memoryview'able objects return a pointer
48// which points to the start of a GC chunk). Given the above constraints we
49// do the following:
50// - typecode high bit is set if the buffer is read-write (else read-only)
51// - free is the offset in elements to the first item in the memoryview
52// - len is the length in elements
53// - items points to the start of the original buffer
54// Note that we don't handle the case where the original buffer might change
55// size due to a resize of the original parent object.
56
57// make (& TYPECODE_MASK) a null operation if memorview not enabled
58#if MICROPY_PY_BUILTINS_MEMORYVIEW
59#define TYPECODE_MASK (0x7f)
60#else
61#define TYPECODE_MASK (~(mp_uint_t)1)
62#endif
63
Paul Sokolovsky427905c2014-01-18 19:24:47 +020064typedef struct _mp_obj_array_t {
65 mp_obj_base_t base;
Damien George40f3c022014-07-03 13:25:24 +010066 mp_uint_t typecode : 8;
Damien George51047752014-02-26 17:40:52 +000067 // free is number of unused elements after len used elements
68 // alloc size = len + free
Damien George40f3c022014-07-03 13:25:24 +010069 mp_uint_t free : (8 * sizeof(mp_uint_t) - 8);
70 mp_uint_t len; // in elements
Paul Sokolovsky427905c2014-01-18 19:24:47 +020071 void *items;
72} mp_obj_array_t;
73
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020074STATIC mp_obj_t array_iterator_new(mp_obj_t array_in);
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020075STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg);
Damien Georgeb2e73112014-11-30 00:00:55 +000076STATIC mp_obj_t array_extend(mp_obj_t self_in, mp_obj_t arg_in);
Damien George4d917232014-08-30 14:28:06 +010077STATIC 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 +020078
79/******************************************************************************/
Damien Georgedd4f4532014-10-23 13:34:35 +010080// array
Paul Sokolovsky427905c2014-01-18 19:24:47 +020081
Damien Georgedd4f4532014-10-23 13:34:35 +010082#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020083STATIC void array_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +020084 mp_obj_array_t *o = o_in;
85 if (o->typecode == BYTEARRAY_TYPECODE) {
Damien George54671862014-07-17 21:56:32 +010086 print(env, "bytearray(b");
Paul Sokolovsky2ec38a12014-06-13 21:23:00 +030087 mp_str_print_quoted(print, env, o->items, o->len, true);
Paul Sokolovsky427905c2014-01-18 19:24:47 +020088 } else {
Paul Sokolovsky7e652af2014-01-28 03:14:20 +020089 print(env, "array('%c'", o->typecode);
Paul Sokolovsky18014212014-01-28 03:40:48 +020090 if (o->len > 0) {
Damien George32ca1642014-04-18 22:04:06 +010091 print(env, ", [");
Damien George42f3de92014-10-03 17:44:14 +000092 for (mp_uint_t i = 0; i < o->len; i++) {
Paul Sokolovsky18014212014-01-28 03:40:48 +020093 if (i > 0) {
94 print(env, ", ");
95 }
Paul Sokolovskyef9124f2014-04-11 03:46:09 +030096 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 +020097 }
Paul Sokolovsky18014212014-01-28 03:40:48 +020098 print(env, "]");
Paul Sokolovsky427905c2014-01-18 19:24:47 +020099 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200100 }
Paul Sokolovsky7e652af2014-01-28 03:14:20 +0200101 print(env, ")");
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200102}
Damien Georgedd4f4532014-10-23 13:34:35 +0100103#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200104
Damien Georgedd4f4532014-10-23 13:34:35 +0100105#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
106STATIC mp_obj_array_t *array_new(char typecode, mp_uint_t n) {
107 int typecode_size = mp_binary_get_size('@', typecode, NULL);
108 if (typecode_size <= 0) {
109 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "bad typecode"));
110 }
111 mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
112 #if MICROPY_PY_BUILTINS_BYTEARRAY && MICROPY_PY_ARRAY
113 o->base.type = (typecode == BYTEARRAY_TYPECODE) ? &mp_type_bytearray : &mp_type_array;
114 #elif MICROPY_PY_BUILTINS_BYTEARRAY
115 o->base.type = &mp_type_bytearray;
116 #else
117 o->base.type = &mp_type_array;
118 #endif
119 o->typecode = typecode;
120 o->free = 0;
121 o->len = n;
122 o->items = m_malloc(typecode_size * o->len);
123 return o;
124}
125#endif
126
127#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200128STATIC mp_obj_t array_construct(char typecode, mp_obj_t initializer) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200129 uint len;
130 // Try to create array of exact len if initializer len is known
131 mp_obj_t len_in = mp_obj_len_maybe(initializer);
132 if (len_in == MP_OBJ_NULL) {
133 len = 0;
134 } else {
135 len = MP_OBJ_SMALL_INT_VALUE(len_in);
136 }
137
138 mp_obj_array_t *array = array_new(typecode, len);
139
Damien Georged17926d2014-03-30 13:35:08 +0100140 mp_obj_t iterable = mp_getiter(initializer);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200141 mp_obj_t item;
Damien George42f3de92014-10-03 17:44:14 +0000142 mp_uint_t i = 0;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100143 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200144 if (len == 0) {
145 array_append(array, item);
146 } else {
Paul Sokolovskyef9124f2014-04-11 03:46:09 +0300147 mp_binary_set_val_array(typecode, array->items, i++, item);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200148 }
149 }
150
151 return array;
152}
Damien Georgedd4f4532014-10-23 13:34:35 +0100153#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200154
Damien Georgedd4f4532014-10-23 13:34:35 +0100155#if MICROPY_PY_ARRAY
Damien Georgeecc88e92014-08-30 00:35:11 +0100156STATIC 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 Georgea3f94e02014-04-20 00:13:22 +0100157 mp_arg_check_num(n_args, n_kw, 1, 2, false);
Damien George32ca1642014-04-18 22:04:06 +0100158
159 // get typecode
Damien Georged182b982014-08-30 14:19:41 +0100160 mp_uint_t l;
Damien George698ec212014-02-08 18:17:23 +0000161 const char *typecode = mp_obj_str_get_data(args[0], &l);
Paul Sokolovsky11973b42014-01-28 02:31:52 +0200162
Damien George32ca1642014-04-18 22:04:06 +0100163 if (n_args == 1) {
164 // 1 arg: make an empty array
165 return array_new(*typecode, 0);
166 } else {
167 // 2 args: construct the array from the given iterator
168 return array_construct(*typecode, args[1]);
169 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200170}
Damien Georgedd4f4532014-10-23 13:34:35 +0100171#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200172
Damien Georgedd4f4532014-10-23 13:34:35 +0100173#if MICROPY_PY_BUILTINS_BYTEARRAY
Damien Georgeecc88e92014-08-30 00:35:11 +0100174STATIC 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 Georgea3f94e02014-04-20 00:13:22 +0100175 mp_arg_check_num(n_args, n_kw, 0, 1, false);
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300176
Damien George32ca1642014-04-18 22:04:06 +0100177 if (n_args == 0) {
178 // no args: construct an empty bytearray
179 return array_new(BYTEARRAY_TYPECODE, 0);
180 } else if (MP_OBJ_IS_SMALL_INT(args[0])) {
181 // 1 arg, an integer: construct a blank bytearray of that length
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300182 uint len = MP_OBJ_SMALL_INT_VALUE(args[0]);
Paul Sokolovskyb9cf3d32014-04-08 04:42:44 +0300183 mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, len);
184 memset(o->items, 0, len);
185 return o;
Damien George32ca1642014-04-18 22:04:06 +0100186 } else {
187 // 1 arg, an iterator: construct the bytearray from that
188 return array_construct(BYTEARRAY_TYPECODE, args[0]);
Paul Sokolovskyb9cf3d32014-04-08 04:42:44 +0300189 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200190}
Damien Georgedd4f4532014-10-23 13:34:35 +0100191#endif
192
193#if MICROPY_PY_BUILTINS_MEMORYVIEW
194STATIC 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) {
195 // TODO possibly allow memoryview constructor to take start/stop so that one
196 // can do memoryview(b, 4, 8) instead of memoryview(b)[4:8] (uses less RAM)
197
198 mp_arg_check_num(n_args, n_kw, 1, 1, false);
199
200 mp_buffer_info_t bufinfo;
201 mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
202
203 mp_obj_array_t *self = m_new_obj(mp_obj_array_t);
204 self->base.type = type_in;
205 self->typecode = bufinfo.typecode;
206 self->free = 0;
207 self->len = bufinfo.len / mp_binary_get_size('@', bufinfo.typecode, NULL); // element len
208 self->items = bufinfo.buf;
209
210 // test if the object can be written to
211 if (mp_get_buffer(args[0], &bufinfo, MP_BUFFER_RW)) {
Damien Georgede3c8062014-10-26 13:20:50 +0000212 self->typecode |= 0x80; // used to indicate writable buffer
Damien Georgedd4f4532014-10-23 13:34:35 +0100213 }
214
215 return self;
216}
217#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200218
Damien Georgeecc88e92014-08-30 00:35:11 +0100219STATIC mp_obj_t array_unary_op(mp_uint_t op, mp_obj_t o_in) {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200220 mp_obj_array_t *o = o_in;
221 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100222 case MP_UNARY_OP_BOOL: return MP_BOOL(o->len != 0);
223 case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(o->len);
Damien George6ac5dce2014-05-21 19:42:43 +0100224 default: return MP_OBJ_NULL; // op not supported
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200225 }
226}
227
Damien Georgeecc88e92014-08-30 00:35:11 +0100228STATIC 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 +0000229 mp_obj_array_t *lhs = lhs_in;
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300230 switch (op) {
Damien Georgeb2e73112014-11-30 00:00:55 +0000231 case MP_BINARY_OP_ADD: {
232 #if MICROPY_PY_BUILTINS_MEMORYVIEW
233 if (lhs->base.type == &mp_type_memoryview) {
234 return MP_OBJ_NULL; // op not supported
235 }
236 #endif
237 // if we get here then lhs is not a memoryview, so we don't need to use (& TYPECODE_MASK)
238 if (mp_obj_get_type(rhs_in) != lhs->base.type) {
239 return MP_OBJ_NULL; // op not supported
240 }
241 mp_obj_array_t *rhs = rhs_in;
242 if (lhs->typecode != rhs->typecode) {
243 return MP_OBJ_NULL; // op not supported
244 }
245 int sz = mp_binary_get_size('@', lhs->typecode, NULL);
246 mp_obj_array_t *res = array_new(lhs->typecode, lhs->len + rhs->len);
247 mp_seq_cat((byte*)res->items, lhs->items, lhs->len * sz, rhs->items, rhs->len * sz, byte);
248 return res;
249 }
250
251 case MP_BINARY_OP_INPLACE_ADD: {
252 #if MICROPY_PY_BUILTINS_MEMORYVIEW
253 if (lhs->base.type == &mp_type_memoryview) {
254 return MP_OBJ_NULL; // op not supported
255 }
256 #endif
257 array_extend(lhs, rhs_in);
258 return lhs;
259 }
260
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300261 case MP_BINARY_OP_EQUAL: {
262 mp_buffer_info_t lhs_bufinfo;
263 mp_buffer_info_t rhs_bufinfo;
264 array_get_buffer(lhs_in, &lhs_bufinfo, MP_BUFFER_READ);
265 if (!mp_get_buffer(rhs_in, &rhs_bufinfo, MP_BUFFER_READ)) {
Damien Georgede3c8062014-10-26 13:20:50 +0000266 return mp_const_false;
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300267 }
268 return MP_BOOL(mp_seq_cmp_bytes(op, lhs_bufinfo.buf, lhs_bufinfo.len, rhs_bufinfo.buf, rhs_bufinfo.len));
269 }
Damien Georgeb2e73112014-11-30 00:00:55 +0000270
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300271 default:
272 return MP_OBJ_NULL; // op not supported
273 }
274}
275
Damien Georgeb2e73112014-11-30 00:00:55 +0000276#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200277STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg) {
Damien Georgeb2e73112014-11-30 00:00:55 +0000278 // self is not a memoryview, so we don't need to use (& TYPECODE_MASK)
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300279 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 +0200280 mp_obj_array_t *self = self_in;
Damien Georgeb2e73112014-11-30 00:00:55 +0000281
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200282 if (self->free == 0) {
Paul Sokolovsky1355cf42014-04-19 01:25:49 +0300283 int item_sz = mp_binary_get_size('@', self->typecode, NULL);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200284 // TODO: alloc policy
285 self->free = 8;
286 self->items = m_realloc(self->items, item_sz * self->len, item_sz * (self->len + self->free));
Paul Sokolovskya2240672014-04-28 00:16:57 +0300287 mp_seq_clear(self->items, self->len + 1, self->len + self->free, item_sz);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200288 }
Paul Sokolovskyef9124f2014-04-11 03:46:09 +0300289 mp_binary_set_val_array(self->typecode, self->items, self->len++, arg);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200290 self->free--;
291 return mp_const_none; // return None, as per CPython
292}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200293STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_append_obj, array_append);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200294
Damien Georgeb2e73112014-11-30 00:00:55 +0000295STATIC mp_obj_t array_extend(mp_obj_t self_in, mp_obj_t arg_in) {
296 // self is not a memoryview, so we don't need to use (& TYPECODE_MASK)
297 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_array) || MP_OBJ_IS_TYPE(self_in, &mp_type_bytearray));
298 mp_obj_array_t *self = self_in;
299
300 // check for compatible types (array & array, or bytearray & bytearray)
301 if (mp_obj_get_type(arg_in) != self->base.type) {
302 type_error:
303 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
304 "incompatible type for array operation"));
305 }
306
307 // check for compatible typecode
308 mp_obj_array_t *arg = arg_in;
309 if (self->typecode != arg->typecode) {
310 goto type_error;
311 }
312
313 int sz = mp_binary_get_size('@', self->typecode, NULL);
314
315 // make sure we have enough room to extend
316 if (self->free < arg->len) {
317 // TODO: alloc policy; at the moment we go conservative
318 self->items = m_realloc(self->items, (self->len + self->free) * sz, (self->len + arg->len) * sz);
319 self->free += arg->len;
320 }
321
322 // extend
323 mp_seq_copy((byte*)self->items + self->len * sz, arg->items, arg->len * sz, byte);
324 self->len += arg->len;
325 self->free -= arg->len;
326
327 return mp_const_none;
328}
329STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_extend_obj, array_extend);
330#endif
331
Damien George729f7b42014-04-17 22:10:53 +0100332STATIC 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 +0100333 if (value == MP_OBJ_NULL) {
Damien George32ca1642014-04-18 22:04:06 +0100334 // delete item
335 // TODO implement
Paul Sokolovsky26905252014-04-20 20:58:33 +0300336 // TODO: confirmed that both bytearray and array.array support
337 // slice deletion
Damien George6ac5dce2014-05-21 19:42:43 +0100338 return MP_OBJ_NULL; // op not supported
Damien George729f7b42014-04-17 22:10:53 +0100339 } else {
340 mp_obj_array_t *o = self_in;
Damien Georgec49ddb92014-06-01 13:49:35 +0100341 if (0) {
342#if MICROPY_PY_BUILTINS_SLICE
343 } else if (MP_OBJ_IS_TYPE(index_in, &mp_type_slice)) {
Paul Sokolovsky26905252014-04-20 20:58:33 +0300344 if (value != MP_OBJ_SENTINEL) {
345 // Only getting a slice is suported so far, not assignment
346 // TODO: confirmed that both bytearray and array.array support
347 // slice assignment (incl. of different size)
Damien George6ac5dce2014-05-21 19:42:43 +0100348 return MP_OBJ_NULL; // op not supported
Paul Sokolovsky26905252014-04-20 20:58:33 +0300349 }
Paul Sokolovskyde4b9322014-05-25 21:21:57 +0300350 mp_bound_slice_t slice;
351 if (!mp_seq_get_fast_slice_indexes(o->len, index_in, &slice)) {
Paul Sokolovsky5fd5af92014-05-25 22:12:56 +0300352 nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
Damien George11de8392014-06-05 18:57:38 +0100353 "only slices with step=1 (aka None) are supported"));
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300354 }
Damien Georgedd4f4532014-10-23 13:34:35 +0100355 mp_obj_array_t *res;
Damien Georgede3c8062014-10-26 13:20:50 +0000356 int sz = mp_binary_get_size('@', o->typecode & TYPECODE_MASK, NULL);
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300357 assert(sz > 0);
Damien Georgedd4f4532014-10-23 13:34:35 +0100358 if (0) {
359 // dummy
360 #if MICROPY_PY_BUILTINS_MEMORYVIEW
361 } else if (o->base.type == &mp_type_memoryview) {
362 res = m_new_obj(mp_obj_array_t);
363 *res = *o;
Damien Georgede3c8062014-10-26 13:20:50 +0000364 res->free += slice.start;
Damien Georgedd4f4532014-10-23 13:34:35 +0100365 res->len = slice.stop - slice.start;
Damien Georgedd4f4532014-10-23 13:34:35 +0100366 #endif
367 } else {
368 res = array_new(o->typecode, slice.stop - slice.start);
stijn49c47da2014-10-28 17:20:52 +0100369 memcpy(res->items, (uint8_t*)o->items + slice.start * sz, (slice.stop - slice.start) * sz);
Damien Georgedd4f4532014-10-23 13:34:35 +0100370 }
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300371 return res;
Damien Georgec49ddb92014-06-01 13:49:35 +0100372#endif
Damien George729f7b42014-04-17 22:10:53 +0100373 } else {
Damien George42f3de92014-10-03 17:44:14 +0000374 mp_uint_t index = mp_get_index(o->base.type, o->len, index_in, false);
Damien Georgede3c8062014-10-26 13:20:50 +0000375 #if MICROPY_PY_BUILTINS_MEMORYVIEW
376 if (o->base.type == &mp_type_memoryview) {
377 index += o->free;
378 if (value != MP_OBJ_SENTINEL && (o->typecode & 0x80) == 0) {
379 // store to read-only memoryview
Damien Georgedd4f4532014-10-23 13:34:35 +0100380 return MP_OBJ_NULL;
381 }
Damien Georgede3c8062014-10-26 13:20:50 +0000382 }
383 #endif
384 if (value == MP_OBJ_SENTINEL) {
385 // load
386 return mp_binary_get_val_array(o->typecode & TYPECODE_MASK, o->items, index);
387 } else {
388 // store
389 mp_binary_set_val_array(o->typecode & TYPECODE_MASK, o->items, index, value);
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300390 return mp_const_none;
391 }
Damien George729f7b42014-04-17 22:10:53 +0100392 }
Damien Georgef4c9b332014-04-08 21:32:29 +0100393 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200394}
395
Damien George4d917232014-08-30 14:28:06 +0100396STATIC 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 +0200397 mp_obj_array_t *o = o_in;
Damien Georgede3c8062014-10-26 13:20:50 +0000398 int sz = mp_binary_get_size('@', o->typecode & TYPECODE_MASK, NULL);
399 bufinfo->buf = o->items;
400 bufinfo->len = o->len * sz;
401 bufinfo->typecode = o->typecode & TYPECODE_MASK;
Damien Georgedd4f4532014-10-23 13:34:35 +0100402 #if MICROPY_PY_BUILTINS_MEMORYVIEW
Damien Georgede3c8062014-10-26 13:20:50 +0000403 if (o->base.type == &mp_type_memoryview) {
404 if ((o->typecode & 0x80) == 0 && (flags & MP_BUFFER_WRITE)) {
405 // read-only memoryview
406 return 1;
407 }
stijn49c47da2014-10-28 17:20:52 +0100408 bufinfo->buf = (uint8_t*)bufinfo->buf + (mp_uint_t)o->free * sz;
Damien Georgedd4f4532014-10-23 13:34:35 +0100409 }
410 #endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200411 return 0;
412}
413
Damien Georgedd4f4532014-10-23 13:34:35 +0100414#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
Damien George9b196cd2014-03-26 21:47:19 +0000415STATIC const mp_map_elem_t array_locals_dict_table[] = {
416 { MP_OBJ_NEW_QSTR(MP_QSTR_append), (mp_obj_t)&array_append_obj },
Damien Georgeb2e73112014-11-30 00:00:55 +0000417 { MP_OBJ_NEW_QSTR(MP_QSTR_extend), (mp_obj_t)&array_extend_obj },
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200418};
419
Damien George9b196cd2014-03-26 21:47:19 +0000420STATIC MP_DEFINE_CONST_DICT(array_locals_dict, array_locals_dict_table);
Damien Georgedd4f4532014-10-23 13:34:35 +0100421#endif
Damien George9b196cd2014-03-26 21:47:19 +0000422
Damien Georgedd4f4532014-10-23 13:34:35 +0100423#if MICROPY_PY_ARRAY
Damien Georgecaac5422014-03-25 14:18:18 +0000424const mp_obj_type_t mp_type_array = {
Damien Georgec5966122014-02-15 16:10:44 +0000425 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000426 .name = MP_QSTR_array,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200427 .print = array_print,
428 .make_new = array_make_new,
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200429 .getiter = array_iterator_new,
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200430 .unary_op = array_unary_op,
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300431 .binary_op = array_binary_op,
Damien George729f7b42014-04-17 22:10:53 +0100432 .subscr = array_subscr,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200433 .buffer_p = { .get_buffer = array_get_buffer },
Damien George9b196cd2014-03-26 21:47:19 +0000434 .locals_dict = (mp_obj_t)&array_locals_dict,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200435};
Damien Georgedd4f4532014-10-23 13:34:35 +0100436#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200437
Damien Georgedd4f4532014-10-23 13:34:35 +0100438#if MICROPY_PY_BUILTINS_BYTEARRAY
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300439const mp_obj_type_t mp_type_bytearray = {
440 { &mp_type_type },
441 .name = MP_QSTR_bytearray,
442 .print = array_print,
443 .make_new = bytearray_make_new,
444 .getiter = array_iterator_new,
445 .unary_op = array_unary_op,
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300446 .binary_op = array_binary_op,
Damien George729f7b42014-04-17 22:10:53 +0100447 .subscr = array_subscr,
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300448 .buffer_p = { .get_buffer = array_get_buffer },
449 .locals_dict = (mp_obj_t)&array_locals_dict,
450};
Damien Georgedd4f4532014-10-23 13:34:35 +0100451#endif
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300452
Damien Georgedd4f4532014-10-23 13:34:35 +0100453#if MICROPY_PY_BUILTINS_MEMORYVIEW
454const mp_obj_type_t mp_type_memoryview = {
455 { &mp_type_type },
456 .name = MP_QSTR_memoryview,
457 .make_new = memoryview_make_new,
458 .getiter = array_iterator_new,
459 .unary_op = array_unary_op,
460 .binary_op = array_binary_op,
461 .subscr = array_subscr,
462 .buffer_p = { .get_buffer = array_get_buffer },
463};
464#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200465
Damien Georgedd4f4532014-10-23 13:34:35 +0100466/* unused
Damien Georged182b982014-08-30 14:19:41 +0100467mp_uint_t mp_obj_array_len(mp_obj_t self_in) {
Paul Sokolovsky33996682014-01-21 23:30:10 +0200468 return ((mp_obj_array_t *)self_in)->len;
469}
Damien Georgedd4f4532014-10-23 13:34:35 +0100470*/
Paul Sokolovsky33996682014-01-21 23:30:10 +0200471
Damien Georgedd4f4532014-10-23 13:34:35 +0100472#if MICROPY_PY_BUILTINS_BYTEARRAY
Damien George4abff752014-08-30 14:59:21 +0100473mp_obj_t mp_obj_new_bytearray(mp_uint_t n, void *items) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200474 mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, n);
475 memcpy(o->items, items, n);
476 return o;
477}
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200478
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200479// Create bytearray which references specified memory area
Damien Georged182b982014-08-30 14:19:41 +0100480mp_obj_t mp_obj_new_bytearray_by_ref(mp_uint_t n, void *items) {
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200481 mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
Damien Georgedd4f4532014-10-23 13:34:35 +0100482 o->base.type = &mp_type_bytearray;
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200483 o->typecode = BYTEARRAY_TYPECODE;
484 o->free = 0;
485 o->len = n;
486 o->items = items;
487 return o;
488}
Damien Georgedd4f4532014-10-23 13:34:35 +0100489#endif
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200490
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200491/******************************************************************************/
Damien Georgedd4f4532014-10-23 13:34:35 +0100492// array iterator
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200493
494typedef struct _mp_obj_array_it_t {
495 mp_obj_base_t base;
496 mp_obj_array_t *array;
Damien Georgede3c8062014-10-26 13:20:50 +0000497 mp_uint_t offset;
Damien George40f3c022014-07-03 13:25:24 +0100498 mp_uint_t cur;
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200499} mp_obj_array_it_t;
500
Damien Georgecaac5422014-03-25 14:18:18 +0000501STATIC mp_obj_t array_it_iternext(mp_obj_t self_in) {
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200502 mp_obj_array_it_t *self = self_in;
503 if (self->cur < self->array->len) {
Damien Georgede3c8062014-10-26 13:20:50 +0000504 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 +0200505 } else {
Damien Georgeea8d06c2014-04-17 23:19:36 +0100506 return MP_OBJ_STOP_ITERATION;
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200507 }
508}
509
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200510STATIC const mp_obj_type_t array_it_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000511 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000512 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +0300513 .getiter = mp_identity,
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200514 .iternext = array_it_iternext,
515};
516
Damien Georgecaac5422014-03-25 14:18:18 +0000517STATIC mp_obj_t array_iterator_new(mp_obj_t array_in) {
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200518 mp_obj_array_t *array = array_in;
Damien Georgede3c8062014-10-26 13:20:50 +0000519 mp_obj_array_it_t *o = m_new0(mp_obj_array_it_t, 1);
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200520 o->base.type = &array_it_type;
521 o->array = array;
Damien Georgede3c8062014-10-26 13:20:50 +0000522 #if MICROPY_PY_BUILTINS_MEMORYVIEW
523 if (array->base.type == &mp_type_memoryview) {
524 o->offset = array->free;
525 }
526 #endif
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200527 return o;
528}
Paul Sokolovskycb78f862014-06-27 20:39:09 +0300529
Damien Georgedd4f4532014-10-23 13:34:35 +0100530#endif // MICROPY_PY_ARRAY || MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_BUILTINS_MEMORYVIEW