blob: f96d6f4237dfa525a597ccece645bb8f6b885b8d [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: {
Damien Georgec7ca01a2014-11-30 14:01:33 +0000232 // allow to add anything that has the buffer protocol (extension to CPython)
233 mp_buffer_info_t lhs_bufinfo;
234 mp_buffer_info_t rhs_bufinfo;
235 array_get_buffer(lhs_in, &lhs_bufinfo, MP_BUFFER_READ);
236 mp_get_buffer_raise(rhs_in, &rhs_bufinfo, MP_BUFFER_READ);
237
238 int sz = mp_binary_get_size('@', lhs_bufinfo.typecode, NULL);
239
240 // convert byte count to element count (in case rhs is not multiple of sz)
241 mp_uint_t rhs_len = rhs_bufinfo.len / sz;
242
243 // note: lhs->len is element count of lhs, lhs_bufinfo.len is byte count
244 mp_obj_array_t *res = array_new(lhs_bufinfo.typecode, lhs->len + rhs_len);
245 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 +0000246 return res;
247 }
248
249 case MP_BINARY_OP_INPLACE_ADD: {
250 #if MICROPY_PY_BUILTINS_MEMORYVIEW
251 if (lhs->base.type == &mp_type_memoryview) {
252 return MP_OBJ_NULL; // op not supported
253 }
254 #endif
255 array_extend(lhs, rhs_in);
256 return lhs;
257 }
258
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300259 case MP_BINARY_OP_EQUAL: {
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 if (!mp_get_buffer(rhs_in, &rhs_bufinfo, MP_BUFFER_READ)) {
Damien Georgede3c8062014-10-26 13:20:50 +0000264 return mp_const_false;
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300265 }
266 return MP_BOOL(mp_seq_cmp_bytes(op, lhs_bufinfo.buf, lhs_bufinfo.len, rhs_bufinfo.buf, rhs_bufinfo.len));
267 }
Damien Georgeb2e73112014-11-30 00:00:55 +0000268
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300269 default:
270 return MP_OBJ_NULL; // op not supported
271 }
272}
273
Damien Georgeb2e73112014-11-30 00:00:55 +0000274#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200275STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg) {
Damien Georgeb2e73112014-11-30 00:00:55 +0000276 // self is not a memoryview, so we don't need to use (& TYPECODE_MASK)
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300277 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 +0200278 mp_obj_array_t *self = self_in;
Damien Georgeb2e73112014-11-30 00:00:55 +0000279
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200280 if (self->free == 0) {
Paul Sokolovsky1355cf42014-04-19 01:25:49 +0300281 int item_sz = mp_binary_get_size('@', self->typecode, NULL);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200282 // TODO: alloc policy
283 self->free = 8;
284 self->items = m_realloc(self->items, item_sz * self->len, item_sz * (self->len + self->free));
Paul Sokolovskya2240672014-04-28 00:16:57 +0300285 mp_seq_clear(self->items, self->len + 1, self->len + self->free, item_sz);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200286 }
Paul Sokolovskyef9124f2014-04-11 03:46:09 +0300287 mp_binary_set_val_array(self->typecode, self->items, self->len++, arg);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200288 self->free--;
289 return mp_const_none; // return None, as per CPython
290}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200291STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_append_obj, array_append);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200292
Damien Georgeb2e73112014-11-30 00:00:55 +0000293STATIC mp_obj_t array_extend(mp_obj_t self_in, mp_obj_t arg_in) {
294 // self is not a memoryview, so we don't need to use (& TYPECODE_MASK)
295 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_array) || MP_OBJ_IS_TYPE(self_in, &mp_type_bytearray));
296 mp_obj_array_t *self = self_in;
297
Damien Georgec7ca01a2014-11-30 14:01:33 +0000298 // allow to extend by anything that has the buffer protocol (extension to CPython)
299 mp_buffer_info_t arg_bufinfo;
300 mp_get_buffer_raise(arg_in, &arg_bufinfo, MP_BUFFER_READ);
Damien Georgeb2e73112014-11-30 00:00:55 +0000301
302 int sz = mp_binary_get_size('@', self->typecode, NULL);
303
Damien Georgec7ca01a2014-11-30 14:01:33 +0000304 // convert byte count to element count
305 mp_uint_t len = arg_bufinfo.len / sz;
306
Damien Georgeb2e73112014-11-30 00:00:55 +0000307 // make sure we have enough room to extend
Damien Georgec7ca01a2014-11-30 14:01:33 +0000308 // TODO: alloc policy; at the moment we go conservative
309 if (self->free < len) {
310 self->items = m_realloc(self->items, (self->len + self->free) * sz, (self->len + len) * sz);
311 self->free = 0;
312 } else {
313 self->free -= len;
Damien Georgeb2e73112014-11-30 00:00:55 +0000314 }
315
316 // extend
Damien Georgec7ca01a2014-11-30 14:01:33 +0000317 mp_seq_copy((byte*)self->items + self->len * sz, arg_bufinfo.buf, len * sz, byte);
318 self->len += len;
Damien Georgeb2e73112014-11-30 00:00:55 +0000319
320 return mp_const_none;
321}
322STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_extend_obj, array_extend);
323#endif
324
Damien George729f7b42014-04-17 22:10:53 +0100325STATIC 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 +0100326 if (value == MP_OBJ_NULL) {
Damien George32ca1642014-04-18 22:04:06 +0100327 // delete item
328 // TODO implement
Paul Sokolovsky26905252014-04-20 20:58:33 +0300329 // TODO: confirmed that both bytearray and array.array support
330 // slice deletion
Damien George6ac5dce2014-05-21 19:42:43 +0100331 return MP_OBJ_NULL; // op not supported
Damien George729f7b42014-04-17 22:10:53 +0100332 } else {
333 mp_obj_array_t *o = self_in;
Damien Georgec49ddb92014-06-01 13:49:35 +0100334 if (0) {
335#if MICROPY_PY_BUILTINS_SLICE
336 } else if (MP_OBJ_IS_TYPE(index_in, &mp_type_slice)) {
Paul Sokolovsky26905252014-04-20 20:58:33 +0300337 if (value != MP_OBJ_SENTINEL) {
338 // Only getting a slice is suported so far, not assignment
339 // TODO: confirmed that both bytearray and array.array support
340 // slice assignment (incl. of different size)
Damien George6ac5dce2014-05-21 19:42:43 +0100341 return MP_OBJ_NULL; // op not supported
Paul Sokolovsky26905252014-04-20 20:58:33 +0300342 }
Paul Sokolovskyde4b9322014-05-25 21:21:57 +0300343 mp_bound_slice_t slice;
344 if (!mp_seq_get_fast_slice_indexes(o->len, index_in, &slice)) {
Paul Sokolovsky5fd5af92014-05-25 22:12:56 +0300345 nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
Damien George11de8392014-06-05 18:57:38 +0100346 "only slices with step=1 (aka None) are supported"));
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300347 }
Damien Georgedd4f4532014-10-23 13:34:35 +0100348 mp_obj_array_t *res;
Damien Georgede3c8062014-10-26 13:20:50 +0000349 int sz = mp_binary_get_size('@', o->typecode & TYPECODE_MASK, NULL);
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300350 assert(sz > 0);
Damien Georgedd4f4532014-10-23 13:34:35 +0100351 if (0) {
352 // dummy
353 #if MICROPY_PY_BUILTINS_MEMORYVIEW
354 } else if (o->base.type == &mp_type_memoryview) {
355 res = m_new_obj(mp_obj_array_t);
356 *res = *o;
Damien Georgede3c8062014-10-26 13:20:50 +0000357 res->free += slice.start;
Damien Georgedd4f4532014-10-23 13:34:35 +0100358 res->len = slice.stop - slice.start;
Damien Georgedd4f4532014-10-23 13:34:35 +0100359 #endif
360 } else {
361 res = array_new(o->typecode, slice.stop - slice.start);
stijn49c47da2014-10-28 17:20:52 +0100362 memcpy(res->items, (uint8_t*)o->items + slice.start * sz, (slice.stop - slice.start) * sz);
Damien Georgedd4f4532014-10-23 13:34:35 +0100363 }
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300364 return res;
Damien Georgec49ddb92014-06-01 13:49:35 +0100365#endif
Damien George729f7b42014-04-17 22:10:53 +0100366 } else {
Damien George42f3de92014-10-03 17:44:14 +0000367 mp_uint_t index = mp_get_index(o->base.type, o->len, index_in, false);
Damien Georgede3c8062014-10-26 13:20:50 +0000368 #if MICROPY_PY_BUILTINS_MEMORYVIEW
369 if (o->base.type == &mp_type_memoryview) {
370 index += o->free;
371 if (value != MP_OBJ_SENTINEL && (o->typecode & 0x80) == 0) {
372 // store to read-only memoryview
Damien Georgedd4f4532014-10-23 13:34:35 +0100373 return MP_OBJ_NULL;
374 }
Damien Georgede3c8062014-10-26 13:20:50 +0000375 }
376 #endif
377 if (value == MP_OBJ_SENTINEL) {
378 // load
379 return mp_binary_get_val_array(o->typecode & TYPECODE_MASK, o->items, index);
380 } else {
381 // store
382 mp_binary_set_val_array(o->typecode & TYPECODE_MASK, o->items, index, value);
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300383 return mp_const_none;
384 }
Damien George729f7b42014-04-17 22:10:53 +0100385 }
Damien Georgef4c9b332014-04-08 21:32:29 +0100386 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200387}
388
Damien George4d917232014-08-30 14:28:06 +0100389STATIC 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 +0200390 mp_obj_array_t *o = o_in;
Damien Georgede3c8062014-10-26 13:20:50 +0000391 int sz = mp_binary_get_size('@', o->typecode & TYPECODE_MASK, NULL);
392 bufinfo->buf = o->items;
393 bufinfo->len = o->len * sz;
394 bufinfo->typecode = o->typecode & TYPECODE_MASK;
Damien Georgedd4f4532014-10-23 13:34:35 +0100395 #if MICROPY_PY_BUILTINS_MEMORYVIEW
Damien Georgede3c8062014-10-26 13:20:50 +0000396 if (o->base.type == &mp_type_memoryview) {
397 if ((o->typecode & 0x80) == 0 && (flags & MP_BUFFER_WRITE)) {
398 // read-only memoryview
399 return 1;
400 }
stijn49c47da2014-10-28 17:20:52 +0100401 bufinfo->buf = (uint8_t*)bufinfo->buf + (mp_uint_t)o->free * sz;
Damien Georgedd4f4532014-10-23 13:34:35 +0100402 }
403 #endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200404 return 0;
405}
406
Damien Georgedd4f4532014-10-23 13:34:35 +0100407#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
Damien George9b196cd2014-03-26 21:47:19 +0000408STATIC const mp_map_elem_t array_locals_dict_table[] = {
409 { MP_OBJ_NEW_QSTR(MP_QSTR_append), (mp_obj_t)&array_append_obj },
Damien Georgeb2e73112014-11-30 00:00:55 +0000410 { MP_OBJ_NEW_QSTR(MP_QSTR_extend), (mp_obj_t)&array_extend_obj },
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200411};
412
Damien George9b196cd2014-03-26 21:47:19 +0000413STATIC MP_DEFINE_CONST_DICT(array_locals_dict, array_locals_dict_table);
Damien Georgedd4f4532014-10-23 13:34:35 +0100414#endif
Damien George9b196cd2014-03-26 21:47:19 +0000415
Damien Georgedd4f4532014-10-23 13:34:35 +0100416#if MICROPY_PY_ARRAY
Damien Georgecaac5422014-03-25 14:18:18 +0000417const mp_obj_type_t mp_type_array = {
Damien Georgec5966122014-02-15 16:10:44 +0000418 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000419 .name = MP_QSTR_array,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200420 .print = array_print,
421 .make_new = array_make_new,
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200422 .getiter = array_iterator_new,
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200423 .unary_op = array_unary_op,
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300424 .binary_op = array_binary_op,
Damien George729f7b42014-04-17 22:10:53 +0100425 .subscr = array_subscr,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200426 .buffer_p = { .get_buffer = array_get_buffer },
Damien George9b196cd2014-03-26 21:47:19 +0000427 .locals_dict = (mp_obj_t)&array_locals_dict,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200428};
Damien Georgedd4f4532014-10-23 13:34:35 +0100429#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200430
Damien Georgedd4f4532014-10-23 13:34:35 +0100431#if MICROPY_PY_BUILTINS_BYTEARRAY
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300432const mp_obj_type_t mp_type_bytearray = {
433 { &mp_type_type },
434 .name = MP_QSTR_bytearray,
435 .print = array_print,
436 .make_new = bytearray_make_new,
437 .getiter = array_iterator_new,
438 .unary_op = array_unary_op,
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300439 .binary_op = array_binary_op,
Damien George729f7b42014-04-17 22:10:53 +0100440 .subscr = array_subscr,
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300441 .buffer_p = { .get_buffer = array_get_buffer },
442 .locals_dict = (mp_obj_t)&array_locals_dict,
443};
Damien Georgedd4f4532014-10-23 13:34:35 +0100444#endif
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300445
Damien Georgedd4f4532014-10-23 13:34:35 +0100446#if MICROPY_PY_BUILTINS_MEMORYVIEW
447const mp_obj_type_t mp_type_memoryview = {
448 { &mp_type_type },
449 .name = MP_QSTR_memoryview,
450 .make_new = memoryview_make_new,
451 .getiter = array_iterator_new,
452 .unary_op = array_unary_op,
453 .binary_op = array_binary_op,
454 .subscr = array_subscr,
455 .buffer_p = { .get_buffer = array_get_buffer },
456};
457#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200458
Damien Georgedd4f4532014-10-23 13:34:35 +0100459/* unused
Damien Georged182b982014-08-30 14:19:41 +0100460mp_uint_t mp_obj_array_len(mp_obj_t self_in) {
Paul Sokolovsky33996682014-01-21 23:30:10 +0200461 return ((mp_obj_array_t *)self_in)->len;
462}
Damien Georgedd4f4532014-10-23 13:34:35 +0100463*/
Paul Sokolovsky33996682014-01-21 23:30:10 +0200464
Damien Georgedd4f4532014-10-23 13:34:35 +0100465#if MICROPY_PY_BUILTINS_BYTEARRAY
Damien George4abff752014-08-30 14:59:21 +0100466mp_obj_t mp_obj_new_bytearray(mp_uint_t n, void *items) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200467 mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, n);
468 memcpy(o->items, items, n);
469 return o;
470}
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200471
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200472// Create bytearray which references specified memory area
Damien Georged182b982014-08-30 14:19:41 +0100473mp_obj_t mp_obj_new_bytearray_by_ref(mp_uint_t n, void *items) {
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200474 mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
Damien Georgedd4f4532014-10-23 13:34:35 +0100475 o->base.type = &mp_type_bytearray;
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200476 o->typecode = BYTEARRAY_TYPECODE;
477 o->free = 0;
478 o->len = n;
479 o->items = items;
480 return o;
481}
Damien Georgedd4f4532014-10-23 13:34:35 +0100482#endif
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200483
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200484/******************************************************************************/
Damien Georgedd4f4532014-10-23 13:34:35 +0100485// array iterator
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200486
487typedef struct _mp_obj_array_it_t {
488 mp_obj_base_t base;
489 mp_obj_array_t *array;
Damien Georgede3c8062014-10-26 13:20:50 +0000490 mp_uint_t offset;
Damien George40f3c022014-07-03 13:25:24 +0100491 mp_uint_t cur;
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200492} mp_obj_array_it_t;
493
Damien Georgecaac5422014-03-25 14:18:18 +0000494STATIC mp_obj_t array_it_iternext(mp_obj_t self_in) {
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200495 mp_obj_array_it_t *self = self_in;
496 if (self->cur < self->array->len) {
Damien Georgede3c8062014-10-26 13:20:50 +0000497 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 +0200498 } else {
Damien Georgeea8d06c2014-04-17 23:19:36 +0100499 return MP_OBJ_STOP_ITERATION;
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200500 }
501}
502
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200503STATIC const mp_obj_type_t array_it_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000504 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000505 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +0300506 .getiter = mp_identity,
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200507 .iternext = array_it_iternext,
508};
509
Damien Georgecaac5422014-03-25 14:18:18 +0000510STATIC mp_obj_t array_iterator_new(mp_obj_t array_in) {
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200511 mp_obj_array_t *array = array_in;
Damien Georgede3c8062014-10-26 13:20:50 +0000512 mp_obj_array_it_t *o = m_new0(mp_obj_array_it_t, 1);
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200513 o->base.type = &array_it_type;
514 o->array = array;
Damien Georgede3c8062014-10-26 13:20:50 +0000515 #if MICROPY_PY_BUILTINS_MEMORYVIEW
516 if (array->base.type == &mp_type_memoryview) {
517 o->offset = array->free;
518 }
519 #endif
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200520 return o;
521}
Paul Sokolovskycb78f862014-06-27 20:39:09 +0300522
Damien Georgedd4f4532014-10-23 13:34:35 +0100523#endif // MICROPY_PY_ARRAY || MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_BUILTINS_MEMORYVIEW