blob: 19705afc0213edcd2f44c8a2799bdc1ba1501d7c [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) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +020080 mp_obj_array_t *o = o_in;
81 if (o->typecode == BYTEARRAY_TYPECODE) {
Damien George54671862014-07-17 21:56:32 +010082 print(env, "bytearray(b");
Paul Sokolovsky2ec38a12014-06-13 21:23:00 +030083 mp_str_print_quoted(print, env, o->items, o->len, true);
Paul Sokolovsky427905c2014-01-18 19:24:47 +020084 } else {
Paul Sokolovsky7e652af2014-01-28 03:14:20 +020085 print(env, "array('%c'", o->typecode);
Paul Sokolovsky18014212014-01-28 03:40:48 +020086 if (o->len > 0) {
Damien George32ca1642014-04-18 22:04:06 +010087 print(env, ", [");
Damien George42f3de92014-10-03 17:44:14 +000088 for (mp_uint_t i = 0; i < o->len; i++) {
Paul Sokolovsky18014212014-01-28 03:40:48 +020089 if (i > 0) {
90 print(env, ", ");
91 }
Paul Sokolovskyef9124f2014-04-11 03:46:09 +030092 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 +020093 }
Paul Sokolovsky18014212014-01-28 03:40:48 +020094 print(env, "]");
Paul Sokolovsky427905c2014-01-18 19:24:47 +020095 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +020096 }
Paul Sokolovsky7e652af2014-01-28 03:14:20 +020097 print(env, ")");
Paul Sokolovsky427905c2014-01-18 19:24:47 +020098}
Damien Georgedd4f4532014-10-23 13:34:35 +010099#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200100
Damien Georgedd4f4532014-10-23 13:34:35 +0100101#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
102STATIC mp_obj_array_t *array_new(char typecode, mp_uint_t n) {
103 int typecode_size = mp_binary_get_size('@', typecode, NULL);
104 if (typecode_size <= 0) {
105 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "bad typecode"));
106 }
107 mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
108 #if MICROPY_PY_BUILTINS_BYTEARRAY && MICROPY_PY_ARRAY
109 o->base.type = (typecode == BYTEARRAY_TYPECODE) ? &mp_type_bytearray : &mp_type_array;
110 #elif MICROPY_PY_BUILTINS_BYTEARRAY
111 o->base.type = &mp_type_bytearray;
112 #else
113 o->base.type = &mp_type_array;
114 #endif
115 o->typecode = typecode;
116 o->free = 0;
117 o->len = n;
118 o->items = m_malloc(typecode_size * o->len);
119 return o;
120}
121#endif
122
123#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200124STATIC mp_obj_t array_construct(char typecode, mp_obj_t initializer) {
Damien George32ef3a32014-12-04 15:46:14 +0000125 // bytearrays can be raw-initialised from anything with the buffer protocol
126 // other arrays can only be raw-initialised from bytes and bytearray objects
127 mp_buffer_info_t bufinfo;
128 if (((MICROPY_PY_BUILTINS_BYTEARRAY
129 && typecode == BYTEARRAY_TYPECODE)
130 || (MICROPY_PY_ARRAY
131 && (MP_OBJ_IS_TYPE(initializer, &mp_type_bytes)
132 || MP_OBJ_IS_TYPE(initializer, &mp_type_bytearray))))
133 && mp_get_buffer(initializer, &bufinfo, MP_BUFFER_READ)) {
134 // construct array from raw bytes
135 // we round-down the len to make it a multiple of sz (CPython raises error)
136 int sz = mp_binary_get_size('@', typecode, NULL);
137 mp_uint_t len = bufinfo.len / sz;
138 mp_obj_array_t *o = array_new(typecode, len);
139 memcpy(o->items, bufinfo.buf, len * sz);
140 return o;
141 }
142
143 mp_uint_t len;
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200144 // Try to create array of exact len if initializer len is known
145 mp_obj_t len_in = mp_obj_len_maybe(initializer);
146 if (len_in == MP_OBJ_NULL) {
147 len = 0;
148 } else {
149 len = MP_OBJ_SMALL_INT_VALUE(len_in);
150 }
151
152 mp_obj_array_t *array = array_new(typecode, len);
153
Damien Georged17926d2014-03-30 13:35:08 +0100154 mp_obj_t iterable = mp_getiter(initializer);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200155 mp_obj_t item;
Damien George42f3de92014-10-03 17:44:14 +0000156 mp_uint_t i = 0;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100157 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200158 if (len == 0) {
159 array_append(array, item);
160 } else {
Paul Sokolovskyef9124f2014-04-11 03:46:09 +0300161 mp_binary_set_val_array(typecode, array->items, i++, item);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200162 }
163 }
164
165 return array;
166}
Damien Georgedd4f4532014-10-23 13:34:35 +0100167#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200168
Damien Georgedd4f4532014-10-23 13:34:35 +0100169#if MICROPY_PY_ARRAY
Damien Georgeecc88e92014-08-30 00:35:11 +0100170STATIC 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 +0100171 mp_arg_check_num(n_args, n_kw, 1, 2, false);
Damien George32ca1642014-04-18 22:04:06 +0100172
173 // get typecode
Damien Georged182b982014-08-30 14:19:41 +0100174 mp_uint_t l;
Damien George698ec212014-02-08 18:17:23 +0000175 const char *typecode = mp_obj_str_get_data(args[0], &l);
Paul Sokolovsky11973b42014-01-28 02:31:52 +0200176
Damien George32ca1642014-04-18 22:04:06 +0100177 if (n_args == 1) {
178 // 1 arg: make an empty array
179 return array_new(*typecode, 0);
180 } else {
Damien George32ef3a32014-12-04 15:46:14 +0000181 // 2 args: construct the array from the given object
Damien George32ca1642014-04-18 22:04:06 +0100182 return array_construct(*typecode, args[1]);
183 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200184}
Damien Georgedd4f4532014-10-23 13:34:35 +0100185#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200186
Damien Georgedd4f4532014-10-23 13:34:35 +0100187#if MICROPY_PY_BUILTINS_BYTEARRAY
Damien Georgeecc88e92014-08-30 00:35:11 +0100188STATIC 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 +0100189 mp_arg_check_num(n_args, n_kw, 0, 1, false);
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300190
Damien George32ca1642014-04-18 22:04:06 +0100191 if (n_args == 0) {
192 // no args: construct an empty bytearray
193 return array_new(BYTEARRAY_TYPECODE, 0);
194 } else if (MP_OBJ_IS_SMALL_INT(args[0])) {
195 // 1 arg, an integer: construct a blank bytearray of that length
Damien George32ef3a32014-12-04 15:46:14 +0000196 mp_uint_t len = MP_OBJ_SMALL_INT_VALUE(args[0]);
Paul Sokolovskyb9cf3d32014-04-08 04:42:44 +0300197 mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, len);
198 memset(o->items, 0, len);
199 return o;
Damien George32ca1642014-04-18 22:04:06 +0100200 } else {
Damien George32ef3a32014-12-04 15:46:14 +0000201 // 1 arg: construct the bytearray from that
Damien George32ca1642014-04-18 22:04:06 +0100202 return array_construct(BYTEARRAY_TYPECODE, args[0]);
Paul Sokolovskyb9cf3d32014-04-08 04:42:44 +0300203 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200204}
Damien Georgedd4f4532014-10-23 13:34:35 +0100205#endif
206
207#if MICROPY_PY_BUILTINS_MEMORYVIEW
208STATIC 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) {
209 // TODO possibly allow memoryview constructor to take start/stop so that one
210 // can do memoryview(b, 4, 8) instead of memoryview(b)[4:8] (uses less RAM)
211
212 mp_arg_check_num(n_args, n_kw, 1, 1, false);
213
214 mp_buffer_info_t bufinfo;
215 mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
216
217 mp_obj_array_t *self = m_new_obj(mp_obj_array_t);
218 self->base.type = type_in;
219 self->typecode = bufinfo.typecode;
220 self->free = 0;
221 self->len = bufinfo.len / mp_binary_get_size('@', bufinfo.typecode, NULL); // element len
222 self->items = bufinfo.buf;
223
224 // test if the object can be written to
225 if (mp_get_buffer(args[0], &bufinfo, MP_BUFFER_RW)) {
Damien Georgede3c8062014-10-26 13:20:50 +0000226 self->typecode |= 0x80; // used to indicate writable buffer
Damien Georgedd4f4532014-10-23 13:34:35 +0100227 }
228
229 return self;
230}
231#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200232
Damien Georgeecc88e92014-08-30 00:35:11 +0100233STATIC mp_obj_t array_unary_op(mp_uint_t op, mp_obj_t o_in) {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200234 mp_obj_array_t *o = o_in;
235 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100236 case MP_UNARY_OP_BOOL: return MP_BOOL(o->len != 0);
237 case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(o->len);
Damien George6ac5dce2014-05-21 19:42:43 +0100238 default: return MP_OBJ_NULL; // op not supported
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200239 }
240}
241
Damien Georgeecc88e92014-08-30 00:35:11 +0100242STATIC 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 +0000243 mp_obj_array_t *lhs = lhs_in;
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300244 switch (op) {
Damien Georgeb2e73112014-11-30 00:00:55 +0000245 case MP_BINARY_OP_ADD: {
Damien Georgec7ca01a2014-11-30 14:01:33 +0000246 // allow to add anything that has the buffer protocol (extension to CPython)
247 mp_buffer_info_t lhs_bufinfo;
248 mp_buffer_info_t rhs_bufinfo;
249 array_get_buffer(lhs_in, &lhs_bufinfo, MP_BUFFER_READ);
250 mp_get_buffer_raise(rhs_in, &rhs_bufinfo, MP_BUFFER_READ);
251
252 int sz = mp_binary_get_size('@', lhs_bufinfo.typecode, NULL);
253
254 // convert byte count to element count (in case rhs is not multiple of sz)
255 mp_uint_t rhs_len = rhs_bufinfo.len / sz;
256
257 // note: lhs->len is element count of lhs, lhs_bufinfo.len is byte count
258 mp_obj_array_t *res = array_new(lhs_bufinfo.typecode, lhs->len + rhs_len);
259 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 +0000260 return res;
261 }
262
263 case MP_BINARY_OP_INPLACE_ADD: {
264 #if MICROPY_PY_BUILTINS_MEMORYVIEW
265 if (lhs->base.type == &mp_type_memoryview) {
266 return MP_OBJ_NULL; // op not supported
267 }
268 #endif
269 array_extend(lhs, rhs_in);
270 return lhs;
271 }
272
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300273 case MP_BINARY_OP_EQUAL: {
274 mp_buffer_info_t lhs_bufinfo;
275 mp_buffer_info_t rhs_bufinfo;
276 array_get_buffer(lhs_in, &lhs_bufinfo, MP_BUFFER_READ);
277 if (!mp_get_buffer(rhs_in, &rhs_bufinfo, MP_BUFFER_READ)) {
Damien Georgede3c8062014-10-26 13:20:50 +0000278 return mp_const_false;
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300279 }
280 return MP_BOOL(mp_seq_cmp_bytes(op, lhs_bufinfo.buf, lhs_bufinfo.len, rhs_bufinfo.buf, rhs_bufinfo.len));
281 }
Damien Georgeb2e73112014-11-30 00:00:55 +0000282
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300283 default:
284 return MP_OBJ_NULL; // op not supported
285 }
286}
287
Damien Georgeb2e73112014-11-30 00:00:55 +0000288#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200289STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg) {
Damien Georgeb2e73112014-11-30 00:00:55 +0000290 // self is not a memoryview, so we don't need to use (& TYPECODE_MASK)
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300291 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 +0200292 mp_obj_array_t *self = self_in;
Damien Georgeb2e73112014-11-30 00:00:55 +0000293
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200294 if (self->free == 0) {
Paul Sokolovsky1355cf42014-04-19 01:25:49 +0300295 int item_sz = mp_binary_get_size('@', self->typecode, NULL);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200296 // TODO: alloc policy
297 self->free = 8;
298 self->items = m_realloc(self->items, item_sz * self->len, item_sz * (self->len + self->free));
Paul Sokolovskya2240672014-04-28 00:16:57 +0300299 mp_seq_clear(self->items, self->len + 1, self->len + self->free, item_sz);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200300 }
Paul Sokolovskyef9124f2014-04-11 03:46:09 +0300301 mp_binary_set_val_array(self->typecode, self->items, self->len++, arg);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200302 self->free--;
303 return mp_const_none; // return None, as per CPython
304}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200305STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_append_obj, array_append);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200306
Damien Georgeb2e73112014-11-30 00:00:55 +0000307STATIC mp_obj_t array_extend(mp_obj_t self_in, mp_obj_t arg_in) {
308 // self is not a memoryview, so we don't need to use (& TYPECODE_MASK)
309 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_array) || MP_OBJ_IS_TYPE(self_in, &mp_type_bytearray));
310 mp_obj_array_t *self = self_in;
311
Damien Georgec7ca01a2014-11-30 14:01:33 +0000312 // allow to extend by anything that has the buffer protocol (extension to CPython)
313 mp_buffer_info_t arg_bufinfo;
314 mp_get_buffer_raise(arg_in, &arg_bufinfo, MP_BUFFER_READ);
Damien Georgeb2e73112014-11-30 00:00:55 +0000315
316 int sz = mp_binary_get_size('@', self->typecode, NULL);
317
Damien Georgec7ca01a2014-11-30 14:01:33 +0000318 // convert byte count to element count
319 mp_uint_t len = arg_bufinfo.len / sz;
320
Damien Georgeb2e73112014-11-30 00:00:55 +0000321 // make sure we have enough room to extend
Damien Georgec7ca01a2014-11-30 14:01:33 +0000322 // TODO: alloc policy; at the moment we go conservative
323 if (self->free < len) {
324 self->items = m_realloc(self->items, (self->len + self->free) * sz, (self->len + len) * sz);
325 self->free = 0;
326 } else {
327 self->free -= len;
Damien Georgeb2e73112014-11-30 00:00:55 +0000328 }
329
330 // extend
Damien Georgec7ca01a2014-11-30 14:01:33 +0000331 mp_seq_copy((byte*)self->items + self->len * sz, arg_bufinfo.buf, len * sz, byte);
332 self->len += len;
Damien Georgeb2e73112014-11-30 00:00:55 +0000333
334 return mp_const_none;
335}
336STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_extend_obj, array_extend);
337#endif
338
Damien George729f7b42014-04-17 22:10:53 +0100339STATIC 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 +0100340 if (value == MP_OBJ_NULL) {
Damien George32ca1642014-04-18 22:04:06 +0100341 // delete item
342 // TODO implement
Paul Sokolovsky26905252014-04-20 20:58:33 +0300343 // TODO: confirmed that both bytearray and array.array support
344 // slice deletion
Damien George6ac5dce2014-05-21 19:42:43 +0100345 return MP_OBJ_NULL; // op not supported
Damien George729f7b42014-04-17 22:10:53 +0100346 } else {
347 mp_obj_array_t *o = self_in;
Damien Georgec49ddb92014-06-01 13:49:35 +0100348 if (0) {
349#if MICROPY_PY_BUILTINS_SLICE
350 } else if (MP_OBJ_IS_TYPE(index_in, &mp_type_slice)) {
Paul Sokolovsky26905252014-04-20 20:58:33 +0300351 if (value != MP_OBJ_SENTINEL) {
352 // Only getting a slice is suported so far, not assignment
353 // TODO: confirmed that both bytearray and array.array support
354 // slice assignment (incl. of different size)
Damien George6ac5dce2014-05-21 19:42:43 +0100355 return MP_OBJ_NULL; // op not supported
Paul Sokolovsky26905252014-04-20 20:58:33 +0300356 }
Paul Sokolovskyde4b9322014-05-25 21:21:57 +0300357 mp_bound_slice_t slice;
358 if (!mp_seq_get_fast_slice_indexes(o->len, index_in, &slice)) {
Paul Sokolovsky5fd5af92014-05-25 22:12:56 +0300359 nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
Damien George11de8392014-06-05 18:57:38 +0100360 "only slices with step=1 (aka None) are supported"));
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300361 }
Damien Georgedd4f4532014-10-23 13:34:35 +0100362 mp_obj_array_t *res;
Damien Georgede3c8062014-10-26 13:20:50 +0000363 int sz = mp_binary_get_size('@', o->typecode & TYPECODE_MASK, NULL);
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300364 assert(sz > 0);
Damien Georgedd4f4532014-10-23 13:34:35 +0100365 if (0) {
366 // dummy
367 #if MICROPY_PY_BUILTINS_MEMORYVIEW
368 } else if (o->base.type == &mp_type_memoryview) {
369 res = m_new_obj(mp_obj_array_t);
370 *res = *o;
Damien Georgede3c8062014-10-26 13:20:50 +0000371 res->free += slice.start;
Damien Georgedd4f4532014-10-23 13:34:35 +0100372 res->len = slice.stop - slice.start;
Damien Georgedd4f4532014-10-23 13:34:35 +0100373 #endif
374 } else {
375 res = array_new(o->typecode, slice.stop - slice.start);
stijn49c47da2014-10-28 17:20:52 +0100376 memcpy(res->items, (uint8_t*)o->items + slice.start * sz, (slice.stop - slice.start) * sz);
Damien Georgedd4f4532014-10-23 13:34:35 +0100377 }
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300378 return res;
Damien Georgec49ddb92014-06-01 13:49:35 +0100379#endif
Damien George729f7b42014-04-17 22:10:53 +0100380 } else {
Damien George42f3de92014-10-03 17:44:14 +0000381 mp_uint_t index = mp_get_index(o->base.type, o->len, index_in, false);
Damien Georgede3c8062014-10-26 13:20:50 +0000382 #if MICROPY_PY_BUILTINS_MEMORYVIEW
383 if (o->base.type == &mp_type_memoryview) {
384 index += o->free;
385 if (value != MP_OBJ_SENTINEL && (o->typecode & 0x80) == 0) {
386 // store to read-only memoryview
Damien Georgedd4f4532014-10-23 13:34:35 +0100387 return MP_OBJ_NULL;
388 }
Damien Georgede3c8062014-10-26 13:20:50 +0000389 }
390 #endif
391 if (value == MP_OBJ_SENTINEL) {
392 // load
393 return mp_binary_get_val_array(o->typecode & TYPECODE_MASK, o->items, index);
394 } else {
395 // store
396 mp_binary_set_val_array(o->typecode & TYPECODE_MASK, o->items, index, value);
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300397 return mp_const_none;
398 }
Damien George729f7b42014-04-17 22:10:53 +0100399 }
Damien Georgef4c9b332014-04-08 21:32:29 +0100400 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200401}
402
Damien George4d917232014-08-30 14:28:06 +0100403STATIC 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 +0200404 mp_obj_array_t *o = o_in;
Damien Georgede3c8062014-10-26 13:20:50 +0000405 int sz = mp_binary_get_size('@', o->typecode & TYPECODE_MASK, NULL);
406 bufinfo->buf = o->items;
407 bufinfo->len = o->len * sz;
408 bufinfo->typecode = o->typecode & TYPECODE_MASK;
Damien Georgedd4f4532014-10-23 13:34:35 +0100409 #if MICROPY_PY_BUILTINS_MEMORYVIEW
Damien Georgede3c8062014-10-26 13:20:50 +0000410 if (o->base.type == &mp_type_memoryview) {
411 if ((o->typecode & 0x80) == 0 && (flags & MP_BUFFER_WRITE)) {
412 // read-only memoryview
413 return 1;
414 }
stijn49c47da2014-10-28 17:20:52 +0100415 bufinfo->buf = (uint8_t*)bufinfo->buf + (mp_uint_t)o->free * sz;
Damien Georgedd4f4532014-10-23 13:34:35 +0100416 }
417 #endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200418 return 0;
419}
420
Damien Georgedd4f4532014-10-23 13:34:35 +0100421#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
Damien George9b196cd2014-03-26 21:47:19 +0000422STATIC const mp_map_elem_t array_locals_dict_table[] = {
423 { MP_OBJ_NEW_QSTR(MP_QSTR_append), (mp_obj_t)&array_append_obj },
Damien Georgeb2e73112014-11-30 00:00:55 +0000424 { MP_OBJ_NEW_QSTR(MP_QSTR_extend), (mp_obj_t)&array_extend_obj },
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200425};
426
Damien George9b196cd2014-03-26 21:47:19 +0000427STATIC MP_DEFINE_CONST_DICT(array_locals_dict, array_locals_dict_table);
Damien Georgedd4f4532014-10-23 13:34:35 +0100428#endif
Damien George9b196cd2014-03-26 21:47:19 +0000429
Damien Georgedd4f4532014-10-23 13:34:35 +0100430#if MICROPY_PY_ARRAY
Damien Georgecaac5422014-03-25 14:18:18 +0000431const mp_obj_type_t mp_type_array = {
Damien Georgec5966122014-02-15 16:10:44 +0000432 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000433 .name = MP_QSTR_array,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200434 .print = array_print,
435 .make_new = array_make_new,
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200436 .getiter = array_iterator_new,
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200437 .unary_op = array_unary_op,
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300438 .binary_op = array_binary_op,
Damien George729f7b42014-04-17 22:10:53 +0100439 .subscr = array_subscr,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200440 .buffer_p = { .get_buffer = array_get_buffer },
Damien George9b196cd2014-03-26 21:47:19 +0000441 .locals_dict = (mp_obj_t)&array_locals_dict,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200442};
Damien Georgedd4f4532014-10-23 13:34:35 +0100443#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200444
Damien Georgedd4f4532014-10-23 13:34:35 +0100445#if MICROPY_PY_BUILTINS_BYTEARRAY
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300446const mp_obj_type_t mp_type_bytearray = {
447 { &mp_type_type },
448 .name = MP_QSTR_bytearray,
449 .print = array_print,
450 .make_new = bytearray_make_new,
451 .getiter = array_iterator_new,
452 .unary_op = array_unary_op,
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300453 .binary_op = array_binary_op,
Damien George729f7b42014-04-17 22:10:53 +0100454 .subscr = array_subscr,
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300455 .buffer_p = { .get_buffer = array_get_buffer },
456 .locals_dict = (mp_obj_t)&array_locals_dict,
457};
Damien Georgedd4f4532014-10-23 13:34:35 +0100458#endif
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300459
Damien Georgedd4f4532014-10-23 13:34:35 +0100460#if MICROPY_PY_BUILTINS_MEMORYVIEW
461const mp_obj_type_t mp_type_memoryview = {
462 { &mp_type_type },
463 .name = MP_QSTR_memoryview,
464 .make_new = memoryview_make_new,
465 .getiter = array_iterator_new,
466 .unary_op = array_unary_op,
467 .binary_op = array_binary_op,
468 .subscr = array_subscr,
469 .buffer_p = { .get_buffer = array_get_buffer },
470};
471#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200472
Damien Georgedd4f4532014-10-23 13:34:35 +0100473/* unused
Damien Georged182b982014-08-30 14:19:41 +0100474mp_uint_t mp_obj_array_len(mp_obj_t self_in) {
Paul Sokolovsky33996682014-01-21 23:30:10 +0200475 return ((mp_obj_array_t *)self_in)->len;
476}
Damien Georgedd4f4532014-10-23 13:34:35 +0100477*/
Paul Sokolovsky33996682014-01-21 23:30:10 +0200478
Damien Georgedd4f4532014-10-23 13:34:35 +0100479#if MICROPY_PY_BUILTINS_BYTEARRAY
Damien George4abff752014-08-30 14:59:21 +0100480mp_obj_t mp_obj_new_bytearray(mp_uint_t n, void *items) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200481 mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, n);
482 memcpy(o->items, items, n);
483 return o;
484}
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200485
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200486// Create bytearray which references specified memory area
Damien Georged182b982014-08-30 14:19:41 +0100487mp_obj_t mp_obj_new_bytearray_by_ref(mp_uint_t n, void *items) {
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200488 mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
Damien Georgedd4f4532014-10-23 13:34:35 +0100489 o->base.type = &mp_type_bytearray;
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200490 o->typecode = BYTEARRAY_TYPECODE;
491 o->free = 0;
492 o->len = n;
493 o->items = items;
494 return o;
495}
Damien Georgedd4f4532014-10-23 13:34:35 +0100496#endif
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200497
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200498/******************************************************************************/
Damien Georgedd4f4532014-10-23 13:34:35 +0100499// array iterator
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200500
501typedef struct _mp_obj_array_it_t {
502 mp_obj_base_t base;
503 mp_obj_array_t *array;
Damien Georgede3c8062014-10-26 13:20:50 +0000504 mp_uint_t offset;
Damien George40f3c022014-07-03 13:25:24 +0100505 mp_uint_t cur;
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200506} mp_obj_array_it_t;
507
Damien Georgecaac5422014-03-25 14:18:18 +0000508STATIC mp_obj_t array_it_iternext(mp_obj_t self_in) {
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200509 mp_obj_array_it_t *self = self_in;
510 if (self->cur < self->array->len) {
Damien Georgede3c8062014-10-26 13:20:50 +0000511 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 +0200512 } else {
Damien Georgeea8d06c2014-04-17 23:19:36 +0100513 return MP_OBJ_STOP_ITERATION;
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200514 }
515}
516
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200517STATIC const mp_obj_type_t array_it_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000518 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000519 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +0300520 .getiter = mp_identity,
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200521 .iternext = array_it_iternext,
522};
523
Damien Georgecaac5422014-03-25 14:18:18 +0000524STATIC mp_obj_t array_iterator_new(mp_obj_t array_in) {
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200525 mp_obj_array_t *array = array_in;
Damien Georgede3c8062014-10-26 13:20:50 +0000526 mp_obj_array_it_t *o = m_new0(mp_obj_array_it_t, 1);
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200527 o->base.type = &array_it_type;
528 o->array = array;
Damien Georgede3c8062014-10-26 13:20:50 +0000529 #if MICROPY_PY_BUILTINS_MEMORYVIEW
530 if (array->base.type == &mp_type_memoryview) {
531 o->offset = array->free;
532 }
533 #endif
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200534 return o;
535}
Paul Sokolovskycb78f862014-06-27 20:39:09 +0300536
Damien Georgedd4f4532014-10-23 13:34:35 +0100537#endif // MICROPY_PY_ARRAY || MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_BUILTINS_MEMORYVIEW