blob: 2d210d91578d10a873c92d4981f73be904b18a63 [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>
30
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030031#include "mpconfig.h"
Paul Sokolovsky427905c2014-01-18 19:24:47 +020032#include "nlr.h"
33#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000034#include "qstr.h"
Paul Sokolovsky427905c2014-01-18 19:24:47 +020035#include "obj.h"
Paul Sokolovsky427905c2014-01-18 19:24:47 +020036#include "runtime0.h"
37#include "runtime.h"
Paul Sokolovskyc2033242014-02-14 20:21:50 +020038#include "binary.h"
Paul Sokolovsky427905c2014-01-18 19:24:47 +020039
Damien Georgedd4f4532014-10-23 13:34:35 +010040#if MICROPY_PY_ARRAY || MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_BUILTINS_MEMORYVIEW
Paul Sokolovskycb78f862014-06-27 20:39:09 +030041
Damien Georgede3c8062014-10-26 13:20:50 +000042// About memoryview object: We want to reuse as much code as possible from
43// array, and keep the memoryview object 4 words in size so it fits in 1 GC
44// block. Also, memoryview must keep a pointer to the base of the buffer so
45// that the buffer is not GC'd if the original parent object is no longer
46// around (we are assuming that all memoryview'able objects return a pointer
47// which points to the start of a GC chunk). Given the above constraints we
48// do the following:
49// - typecode high bit is set if the buffer is read-write (else read-only)
50// - free is the offset in elements to the first item in the memoryview
51// - len is the length in elements
52// - items points to the start of the original buffer
53// Note that we don't handle the case where the original buffer might change
54// size due to a resize of the original parent object.
55
56// make (& TYPECODE_MASK) a null operation if memorview not enabled
57#if MICROPY_PY_BUILTINS_MEMORYVIEW
58#define TYPECODE_MASK (0x7f)
59#else
60#define TYPECODE_MASK (~(mp_uint_t)1)
61#endif
62
Paul Sokolovsky427905c2014-01-18 19:24:47 +020063typedef struct _mp_obj_array_t {
64 mp_obj_base_t base;
Damien George40f3c022014-07-03 13:25:24 +010065 mp_uint_t typecode : 8;
Damien George51047752014-02-26 17:40:52 +000066 // free is number of unused elements after len used elements
67 // alloc size = len + free
Damien George40f3c022014-07-03 13:25:24 +010068 mp_uint_t free : (8 * sizeof(mp_uint_t) - 8);
69 mp_uint_t len; // in elements
Paul Sokolovsky427905c2014-01-18 19:24:47 +020070 void *items;
71} mp_obj_array_t;
72
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020073STATIC mp_obj_t array_iterator_new(mp_obj_t array_in);
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020074STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg);
Damien George4d917232014-08-30 14:28:06 +010075STATIC 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 +020076
77/******************************************************************************/
Damien Georgedd4f4532014-10-23 13:34:35 +010078// array
Paul Sokolovsky427905c2014-01-18 19:24:47 +020079
Damien Georgedd4f4532014-10-23 13:34:35 +010080#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020081STATIC 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 +020082 mp_obj_array_t *o = o_in;
83 if (o->typecode == BYTEARRAY_TYPECODE) {
Damien George54671862014-07-17 21:56:32 +010084 print(env, "bytearray(b");
Paul Sokolovsky2ec38a12014-06-13 21:23:00 +030085 mp_str_print_quoted(print, env, o->items, o->len, true);
Paul Sokolovsky427905c2014-01-18 19:24:47 +020086 } else {
Paul Sokolovsky7e652af2014-01-28 03:14:20 +020087 print(env, "array('%c'", o->typecode);
Paul Sokolovsky18014212014-01-28 03:40:48 +020088 if (o->len > 0) {
Damien George32ca1642014-04-18 22:04:06 +010089 print(env, ", [");
Damien George42f3de92014-10-03 17:44:14 +000090 for (mp_uint_t i = 0; i < o->len; i++) {
Paul Sokolovsky18014212014-01-28 03:40:48 +020091 if (i > 0) {
92 print(env, ", ");
93 }
Paul Sokolovskyef9124f2014-04-11 03:46:09 +030094 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 +020095 }
Paul Sokolovsky18014212014-01-28 03:40:48 +020096 print(env, "]");
Paul Sokolovsky427905c2014-01-18 19:24:47 +020097 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +020098 }
Paul Sokolovsky7e652af2014-01-28 03:14:20 +020099 print(env, ")");
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200100}
Damien Georgedd4f4532014-10-23 13:34:35 +0100101#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200102
Damien Georgedd4f4532014-10-23 13:34:35 +0100103#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
104STATIC mp_obj_array_t *array_new(char typecode, mp_uint_t n) {
105 int typecode_size = mp_binary_get_size('@', typecode, NULL);
106 if (typecode_size <= 0) {
107 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "bad typecode"));
108 }
109 mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
110 #if MICROPY_PY_BUILTINS_BYTEARRAY && MICROPY_PY_ARRAY
111 o->base.type = (typecode == BYTEARRAY_TYPECODE) ? &mp_type_bytearray : &mp_type_array;
112 #elif MICROPY_PY_BUILTINS_BYTEARRAY
113 o->base.type = &mp_type_bytearray;
114 #else
115 o->base.type = &mp_type_array;
116 #endif
117 o->typecode = typecode;
118 o->free = 0;
119 o->len = n;
120 o->items = m_malloc(typecode_size * o->len);
121 return o;
122}
123#endif
124
125#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200126STATIC mp_obj_t array_construct(char typecode, mp_obj_t initializer) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200127 uint len;
128 // Try to create array of exact len if initializer len is known
129 mp_obj_t len_in = mp_obj_len_maybe(initializer);
130 if (len_in == MP_OBJ_NULL) {
131 len = 0;
132 } else {
133 len = MP_OBJ_SMALL_INT_VALUE(len_in);
134 }
135
136 mp_obj_array_t *array = array_new(typecode, len);
137
Damien Georged17926d2014-03-30 13:35:08 +0100138 mp_obj_t iterable = mp_getiter(initializer);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200139 mp_obj_t item;
Damien George42f3de92014-10-03 17:44:14 +0000140 mp_uint_t i = 0;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100141 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200142 if (len == 0) {
143 array_append(array, item);
144 } else {
Paul Sokolovskyef9124f2014-04-11 03:46:09 +0300145 mp_binary_set_val_array(typecode, array->items, i++, item);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200146 }
147 }
148
149 return array;
150}
Damien Georgedd4f4532014-10-23 13:34:35 +0100151#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200152
Damien Georgedd4f4532014-10-23 13:34:35 +0100153#if MICROPY_PY_ARRAY
Damien Georgeecc88e92014-08-30 00:35:11 +0100154STATIC 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 +0100155 mp_arg_check_num(n_args, n_kw, 1, 2, false);
Damien George32ca1642014-04-18 22:04:06 +0100156
157 // get typecode
Damien Georged182b982014-08-30 14:19:41 +0100158 mp_uint_t l;
Damien George698ec212014-02-08 18:17:23 +0000159 const char *typecode = mp_obj_str_get_data(args[0], &l);
Paul Sokolovsky11973b42014-01-28 02:31:52 +0200160
Damien George32ca1642014-04-18 22:04:06 +0100161 if (n_args == 1) {
162 // 1 arg: make an empty array
163 return array_new(*typecode, 0);
164 } else {
165 // 2 args: construct the array from the given iterator
166 return array_construct(*typecode, args[1]);
167 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200168}
Damien Georgedd4f4532014-10-23 13:34:35 +0100169#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200170
Damien Georgedd4f4532014-10-23 13:34:35 +0100171#if MICROPY_PY_BUILTINS_BYTEARRAY
Damien Georgeecc88e92014-08-30 00:35:11 +0100172STATIC 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 +0100173 mp_arg_check_num(n_args, n_kw, 0, 1, false);
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300174
Damien George32ca1642014-04-18 22:04:06 +0100175 if (n_args == 0) {
176 // no args: construct an empty bytearray
177 return array_new(BYTEARRAY_TYPECODE, 0);
178 } else if (MP_OBJ_IS_SMALL_INT(args[0])) {
179 // 1 arg, an integer: construct a blank bytearray of that length
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300180 uint len = MP_OBJ_SMALL_INT_VALUE(args[0]);
Paul Sokolovskyb9cf3d32014-04-08 04:42:44 +0300181 mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, len);
182 memset(o->items, 0, len);
183 return o;
Damien George32ca1642014-04-18 22:04:06 +0100184 } else {
185 // 1 arg, an iterator: construct the bytearray from that
186 return array_construct(BYTEARRAY_TYPECODE, args[0]);
Paul Sokolovskyb9cf3d32014-04-08 04:42:44 +0300187 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200188}
Damien Georgedd4f4532014-10-23 13:34:35 +0100189#endif
190
191#if MICROPY_PY_BUILTINS_MEMORYVIEW
192STATIC 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) {
193 // TODO possibly allow memoryview constructor to take start/stop so that one
194 // can do memoryview(b, 4, 8) instead of memoryview(b)[4:8] (uses less RAM)
195
196 mp_arg_check_num(n_args, n_kw, 1, 1, false);
197
198 mp_buffer_info_t bufinfo;
199 mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
200
201 mp_obj_array_t *self = m_new_obj(mp_obj_array_t);
202 self->base.type = type_in;
203 self->typecode = bufinfo.typecode;
204 self->free = 0;
205 self->len = bufinfo.len / mp_binary_get_size('@', bufinfo.typecode, NULL); // element len
206 self->items = bufinfo.buf;
207
208 // test if the object can be written to
209 if (mp_get_buffer(args[0], &bufinfo, MP_BUFFER_RW)) {
Damien Georgede3c8062014-10-26 13:20:50 +0000210 self->typecode |= 0x80; // used to indicate writable buffer
Damien Georgedd4f4532014-10-23 13:34:35 +0100211 }
212
213 return self;
214}
215#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200216
Damien Georgeecc88e92014-08-30 00:35:11 +0100217STATIC mp_obj_t array_unary_op(mp_uint_t op, mp_obj_t o_in) {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200218 mp_obj_array_t *o = o_in;
219 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100220 case MP_UNARY_OP_BOOL: return MP_BOOL(o->len != 0);
221 case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(o->len);
Damien George6ac5dce2014-05-21 19:42:43 +0100222 default: return MP_OBJ_NULL; // op not supported
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200223 }
224}
225
Damien Georgeecc88e92014-08-30 00:35:11 +0100226STATIC mp_obj_t array_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300227 switch (op) {
228 case MP_BINARY_OP_EQUAL: {
229 mp_buffer_info_t lhs_bufinfo;
230 mp_buffer_info_t rhs_bufinfo;
231 array_get_buffer(lhs_in, &lhs_bufinfo, MP_BUFFER_READ);
232 if (!mp_get_buffer(rhs_in, &rhs_bufinfo, MP_BUFFER_READ)) {
Damien Georgede3c8062014-10-26 13:20:50 +0000233 return mp_const_false;
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300234 }
235 return MP_BOOL(mp_seq_cmp_bytes(op, lhs_bufinfo.buf, lhs_bufinfo.len, rhs_bufinfo.buf, rhs_bufinfo.len));
236 }
237 default:
238 return MP_OBJ_NULL; // op not supported
239 }
240}
241
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200242STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg) {
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300243 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 +0200244 mp_obj_array_t *self = self_in;
245 if (self->free == 0) {
Paul Sokolovsky1355cf42014-04-19 01:25:49 +0300246 int item_sz = mp_binary_get_size('@', self->typecode, NULL);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200247 // TODO: alloc policy
248 self->free = 8;
249 self->items = m_realloc(self->items, item_sz * self->len, item_sz * (self->len + self->free));
Paul Sokolovskya2240672014-04-28 00:16:57 +0300250 mp_seq_clear(self->items, self->len + 1, self->len + self->free, item_sz);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200251 }
Paul Sokolovskyef9124f2014-04-11 03:46:09 +0300252 mp_binary_set_val_array(self->typecode, self->items, self->len++, arg);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200253 self->free--;
254 return mp_const_none; // return None, as per CPython
255}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200256STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_append_obj, array_append);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200257
Damien George729f7b42014-04-17 22:10:53 +0100258STATIC 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 +0100259 if (value == MP_OBJ_NULL) {
Damien George32ca1642014-04-18 22:04:06 +0100260 // delete item
261 // TODO implement
Paul Sokolovsky26905252014-04-20 20:58:33 +0300262 // TODO: confirmed that both bytearray and array.array support
263 // slice deletion
Damien George6ac5dce2014-05-21 19:42:43 +0100264 return MP_OBJ_NULL; // op not supported
Damien George729f7b42014-04-17 22:10:53 +0100265 } else {
266 mp_obj_array_t *o = self_in;
Damien Georgec49ddb92014-06-01 13:49:35 +0100267 if (0) {
268#if MICROPY_PY_BUILTINS_SLICE
269 } else if (MP_OBJ_IS_TYPE(index_in, &mp_type_slice)) {
Paul Sokolovsky26905252014-04-20 20:58:33 +0300270 if (value != MP_OBJ_SENTINEL) {
271 // Only getting a slice is suported so far, not assignment
272 // TODO: confirmed that both bytearray and array.array support
273 // slice assignment (incl. of different size)
Damien George6ac5dce2014-05-21 19:42:43 +0100274 return MP_OBJ_NULL; // op not supported
Paul Sokolovsky26905252014-04-20 20:58:33 +0300275 }
Paul Sokolovskyde4b9322014-05-25 21:21:57 +0300276 mp_bound_slice_t slice;
277 if (!mp_seq_get_fast_slice_indexes(o->len, index_in, &slice)) {
Paul Sokolovsky5fd5af92014-05-25 22:12:56 +0300278 nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
Damien George11de8392014-06-05 18:57:38 +0100279 "only slices with step=1 (aka None) are supported"));
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300280 }
Damien Georgedd4f4532014-10-23 13:34:35 +0100281 mp_obj_array_t *res;
Damien Georgede3c8062014-10-26 13:20:50 +0000282 int sz = mp_binary_get_size('@', o->typecode & TYPECODE_MASK, NULL);
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300283 assert(sz > 0);
Damien Georgedd4f4532014-10-23 13:34:35 +0100284 if (0) {
285 // dummy
286 #if MICROPY_PY_BUILTINS_MEMORYVIEW
287 } else if (o->base.type == &mp_type_memoryview) {
288 res = m_new_obj(mp_obj_array_t);
289 *res = *o;
Damien Georgede3c8062014-10-26 13:20:50 +0000290 res->free += slice.start;
Damien Georgedd4f4532014-10-23 13:34:35 +0100291 res->len = slice.stop - slice.start;
Damien Georgedd4f4532014-10-23 13:34:35 +0100292 #endif
293 } else {
294 res = array_new(o->typecode, slice.stop - slice.start);
295 memcpy(res->items, o->items + slice.start * sz, (slice.stop - slice.start) * sz);
296 }
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300297 return res;
Damien Georgec49ddb92014-06-01 13:49:35 +0100298#endif
Damien George729f7b42014-04-17 22:10:53 +0100299 } else {
Damien George42f3de92014-10-03 17:44:14 +0000300 mp_uint_t index = mp_get_index(o->base.type, o->len, index_in, false);
Damien Georgede3c8062014-10-26 13:20:50 +0000301 #if MICROPY_PY_BUILTINS_MEMORYVIEW
302 if (o->base.type == &mp_type_memoryview) {
303 index += o->free;
304 if (value != MP_OBJ_SENTINEL && (o->typecode & 0x80) == 0) {
305 // store to read-only memoryview
Damien Georgedd4f4532014-10-23 13:34:35 +0100306 return MP_OBJ_NULL;
307 }
Damien Georgede3c8062014-10-26 13:20:50 +0000308 }
309 #endif
310 if (value == MP_OBJ_SENTINEL) {
311 // load
312 return mp_binary_get_val_array(o->typecode & TYPECODE_MASK, o->items, index);
313 } else {
314 // store
315 mp_binary_set_val_array(o->typecode & TYPECODE_MASK, o->items, index, value);
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300316 return mp_const_none;
317 }
Damien George729f7b42014-04-17 22:10:53 +0100318 }
Damien Georgef4c9b332014-04-08 21:32:29 +0100319 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200320}
321
Damien George4d917232014-08-30 14:28:06 +0100322STATIC 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 +0200323 mp_obj_array_t *o = o_in;
Damien Georgede3c8062014-10-26 13:20:50 +0000324 int sz = mp_binary_get_size('@', o->typecode & TYPECODE_MASK, NULL);
325 bufinfo->buf = o->items;
326 bufinfo->len = o->len * sz;
327 bufinfo->typecode = o->typecode & TYPECODE_MASK;
Damien Georgedd4f4532014-10-23 13:34:35 +0100328 #if MICROPY_PY_BUILTINS_MEMORYVIEW
Damien Georgede3c8062014-10-26 13:20:50 +0000329 if (o->base.type == &mp_type_memoryview) {
330 if ((o->typecode & 0x80) == 0 && (flags & MP_BUFFER_WRITE)) {
331 // read-only memoryview
332 return 1;
333 }
334 bufinfo->buf += (mp_uint_t)o->free * sz;
Damien Georgedd4f4532014-10-23 13:34:35 +0100335 }
336 #endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200337 return 0;
338}
339
Damien Georgedd4f4532014-10-23 13:34:35 +0100340#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
Damien George9b196cd2014-03-26 21:47:19 +0000341STATIC const mp_map_elem_t array_locals_dict_table[] = {
342 { MP_OBJ_NEW_QSTR(MP_QSTR_append), (mp_obj_t)&array_append_obj },
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200343};
344
Damien George9b196cd2014-03-26 21:47:19 +0000345STATIC MP_DEFINE_CONST_DICT(array_locals_dict, array_locals_dict_table);
Damien Georgedd4f4532014-10-23 13:34:35 +0100346#endif
Damien George9b196cd2014-03-26 21:47:19 +0000347
Damien Georgedd4f4532014-10-23 13:34:35 +0100348#if MICROPY_PY_ARRAY
Damien Georgecaac5422014-03-25 14:18:18 +0000349const mp_obj_type_t mp_type_array = {
Damien Georgec5966122014-02-15 16:10:44 +0000350 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000351 .name = MP_QSTR_array,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200352 .print = array_print,
353 .make_new = array_make_new,
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200354 .getiter = array_iterator_new,
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200355 .unary_op = array_unary_op,
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300356 .binary_op = array_binary_op,
Damien George729f7b42014-04-17 22:10:53 +0100357 .subscr = array_subscr,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200358 .buffer_p = { .get_buffer = array_get_buffer },
Damien George9b196cd2014-03-26 21:47:19 +0000359 .locals_dict = (mp_obj_t)&array_locals_dict,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200360};
Damien Georgedd4f4532014-10-23 13:34:35 +0100361#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200362
Damien Georgedd4f4532014-10-23 13:34:35 +0100363#if MICROPY_PY_BUILTINS_BYTEARRAY
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300364const mp_obj_type_t mp_type_bytearray = {
365 { &mp_type_type },
366 .name = MP_QSTR_bytearray,
367 .print = array_print,
368 .make_new = bytearray_make_new,
369 .getiter = array_iterator_new,
370 .unary_op = array_unary_op,
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300371 .binary_op = array_binary_op,
Damien George729f7b42014-04-17 22:10:53 +0100372 .subscr = array_subscr,
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300373 .buffer_p = { .get_buffer = array_get_buffer },
374 .locals_dict = (mp_obj_t)&array_locals_dict,
375};
Damien Georgedd4f4532014-10-23 13:34:35 +0100376#endif
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300377
Damien Georgedd4f4532014-10-23 13:34:35 +0100378#if MICROPY_PY_BUILTINS_MEMORYVIEW
379const mp_obj_type_t mp_type_memoryview = {
380 { &mp_type_type },
381 .name = MP_QSTR_memoryview,
382 .make_new = memoryview_make_new,
383 .getiter = array_iterator_new,
384 .unary_op = array_unary_op,
385 .binary_op = array_binary_op,
386 .subscr = array_subscr,
387 .buffer_p = { .get_buffer = array_get_buffer },
388};
389#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200390
Damien Georgedd4f4532014-10-23 13:34:35 +0100391/* unused
Damien Georged182b982014-08-30 14:19:41 +0100392mp_uint_t mp_obj_array_len(mp_obj_t self_in) {
Paul Sokolovsky33996682014-01-21 23:30:10 +0200393 return ((mp_obj_array_t *)self_in)->len;
394}
Damien Georgedd4f4532014-10-23 13:34:35 +0100395*/
Paul Sokolovsky33996682014-01-21 23:30:10 +0200396
Damien Georgedd4f4532014-10-23 13:34:35 +0100397#if MICROPY_PY_BUILTINS_BYTEARRAY
Damien George4abff752014-08-30 14:59:21 +0100398mp_obj_t mp_obj_new_bytearray(mp_uint_t n, void *items) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200399 mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, n);
400 memcpy(o->items, items, n);
401 return o;
402}
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200403
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200404// Create bytearray which references specified memory area
Damien Georged182b982014-08-30 14:19:41 +0100405mp_obj_t mp_obj_new_bytearray_by_ref(mp_uint_t n, void *items) {
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200406 mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
Damien Georgedd4f4532014-10-23 13:34:35 +0100407 o->base.type = &mp_type_bytearray;
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200408 o->typecode = BYTEARRAY_TYPECODE;
409 o->free = 0;
410 o->len = n;
411 o->items = items;
412 return o;
413}
Damien Georgedd4f4532014-10-23 13:34:35 +0100414#endif
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200415
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200416/******************************************************************************/
Damien Georgedd4f4532014-10-23 13:34:35 +0100417// array iterator
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200418
419typedef struct _mp_obj_array_it_t {
420 mp_obj_base_t base;
421 mp_obj_array_t *array;
Damien Georgede3c8062014-10-26 13:20:50 +0000422 mp_uint_t offset;
Damien George40f3c022014-07-03 13:25:24 +0100423 mp_uint_t cur;
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200424} mp_obj_array_it_t;
425
Damien Georgecaac5422014-03-25 14:18:18 +0000426STATIC mp_obj_t array_it_iternext(mp_obj_t self_in) {
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200427 mp_obj_array_it_t *self = self_in;
428 if (self->cur < self->array->len) {
Damien Georgede3c8062014-10-26 13:20:50 +0000429 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 +0200430 } else {
Damien Georgeea8d06c2014-04-17 23:19:36 +0100431 return MP_OBJ_STOP_ITERATION;
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200432 }
433}
434
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200435STATIC const mp_obj_type_t array_it_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000436 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000437 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +0300438 .getiter = mp_identity,
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200439 .iternext = array_it_iternext,
440};
441
Damien Georgecaac5422014-03-25 14:18:18 +0000442STATIC mp_obj_t array_iterator_new(mp_obj_t array_in) {
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200443 mp_obj_array_t *array = array_in;
Damien Georgede3c8062014-10-26 13:20:50 +0000444 mp_obj_array_it_t *o = m_new0(mp_obj_array_it_t, 1);
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200445 o->base.type = &array_it_type;
446 o->array = array;
Damien Georgede3c8062014-10-26 13:20:50 +0000447 #if MICROPY_PY_BUILTINS_MEMORYVIEW
448 if (array->base.type == &mp_type_memoryview) {
449 o->offset = array->free;
450 }
451 #endif
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200452 return o;
453}
Paul Sokolovskycb78f862014-06-27 20:39:09 +0300454
Damien Georgedd4f4532014-10-23 13:34:35 +0100455#endif // MICROPY_PY_ARRAY || MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_BUILTINS_MEMORYVIEW