blob: 91fcec624a2a7c1cc666e2802f2831cdce842e67 [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
40typedef struct _mp_obj_array_t {
41 mp_obj_base_t base;
Damien George51047752014-02-26 17:40:52 +000042 machine_uint_t typecode : 8;
43 // free is number of unused elements after len used elements
44 // alloc size = len + free
45 machine_uint_t free : (8 * sizeof(machine_uint_t) - 8);
Paul Sokolovsky427905c2014-01-18 19:24:47 +020046 machine_uint_t len; // in elements
47 void *items;
48} mp_obj_array_t;
49
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020050STATIC mp_obj_t array_iterator_new(mp_obj_t array_in);
51STATIC mp_obj_array_t *array_new(char typecode, uint n);
52STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg);
Paul Sokolovsky427905c2014-01-18 19:24:47 +020053
54/******************************************************************************/
55/* array */
56
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020057STATIC 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 +020058 mp_obj_array_t *o = o_in;
59 if (o->typecode == BYTEARRAY_TYPECODE) {
Paul Sokolovsky18014212014-01-28 03:40:48 +020060 print(env, "bytearray(b", o->typecode);
61 mp_str_print_quoted(print, env, o->items, o->len);
Paul Sokolovsky427905c2014-01-18 19:24:47 +020062 } else {
Paul Sokolovsky7e652af2014-01-28 03:14:20 +020063 print(env, "array('%c'", o->typecode);
Paul Sokolovsky18014212014-01-28 03:40:48 +020064 if (o->len > 0) {
Damien George32ca1642014-04-18 22:04:06 +010065 print(env, ", [");
Paul Sokolovsky18014212014-01-28 03:40:48 +020066 for (int i = 0; i < o->len; i++) {
67 if (i > 0) {
68 print(env, ", ");
69 }
Paul Sokolovskyef9124f2014-04-11 03:46:09 +030070 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 +020071 }
Paul Sokolovsky18014212014-01-28 03:40:48 +020072 print(env, "]");
Paul Sokolovsky427905c2014-01-18 19:24:47 +020073 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +020074 }
Paul Sokolovsky7e652af2014-01-28 03:14:20 +020075 print(env, ")");
Paul Sokolovsky427905c2014-01-18 19:24:47 +020076}
77
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020078STATIC mp_obj_t array_construct(char typecode, mp_obj_t initializer) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +020079 uint len;
80 // Try to create array of exact len if initializer len is known
81 mp_obj_t len_in = mp_obj_len_maybe(initializer);
82 if (len_in == MP_OBJ_NULL) {
83 len = 0;
84 } else {
85 len = MP_OBJ_SMALL_INT_VALUE(len_in);
86 }
87
88 mp_obj_array_t *array = array_new(typecode, len);
89
Damien Georged17926d2014-03-30 13:35:08 +010090 mp_obj_t iterable = mp_getiter(initializer);
Paul Sokolovsky427905c2014-01-18 19:24:47 +020091 mp_obj_t item;
92 int i = 0;
Damien Georgeea8d06c2014-04-17 23:19:36 +010093 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +020094 if (len == 0) {
95 array_append(array, item);
96 } else {
Paul Sokolovskyef9124f2014-04-11 03:46:09 +030097 mp_binary_set_val_array(typecode, array->items, i++, item);
Paul Sokolovsky427905c2014-01-18 19:24:47 +020098 }
99 }
100
101 return array;
102}
103
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200104STATIC mp_obj_t array_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
Damien Georgea3f94e02014-04-20 00:13:22 +0100105 mp_arg_check_num(n_args, n_kw, 1, 2, false);
Damien George32ca1642014-04-18 22:04:06 +0100106
107 // get typecode
Paul Sokolovsky11973b42014-01-28 02:31:52 +0200108 uint l;
Damien George698ec212014-02-08 18:17:23 +0000109 const char *typecode = mp_obj_str_get_data(args[0], &l);
Paul Sokolovsky11973b42014-01-28 02:31:52 +0200110
Damien George32ca1642014-04-18 22:04:06 +0100111 if (n_args == 1) {
112 // 1 arg: make an empty array
113 return array_new(*typecode, 0);
114 } else {
115 // 2 args: construct the array from the given iterator
116 return array_construct(*typecode, args[1]);
117 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200118}
119
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300120STATIC mp_obj_t bytearray_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
Damien Georgea3f94e02014-04-20 00:13:22 +0100121 mp_arg_check_num(n_args, n_kw, 0, 1, false);
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300122
Damien George32ca1642014-04-18 22:04:06 +0100123 if (n_args == 0) {
124 // no args: construct an empty bytearray
125 return array_new(BYTEARRAY_TYPECODE, 0);
126 } else if (MP_OBJ_IS_SMALL_INT(args[0])) {
127 // 1 arg, an integer: construct a blank bytearray of that length
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300128 uint len = MP_OBJ_SMALL_INT_VALUE(args[0]);
Paul Sokolovskyb9cf3d32014-04-08 04:42:44 +0300129 mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, len);
130 memset(o->items, 0, len);
131 return o;
Damien George32ca1642014-04-18 22:04:06 +0100132 } else {
133 // 1 arg, an iterator: construct the bytearray from that
134 return array_construct(BYTEARRAY_TYPECODE, args[0]);
Paul Sokolovskyb9cf3d32014-04-08 04:42:44 +0300135 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200136}
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200137
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200138STATIC mp_obj_t array_unary_op(int op, mp_obj_t o_in) {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200139 mp_obj_array_t *o = o_in;
140 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100141 case MP_UNARY_OP_BOOL: return MP_BOOL(o->len != 0);
142 case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(o->len);
Damien George6ac5dce2014-05-21 19:42:43 +0100143 default: return MP_OBJ_NULL; // op not supported
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200144 }
145}
146
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200147STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg) {
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300148 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 +0200149 mp_obj_array_t *self = self_in;
150 if (self->free == 0) {
Paul Sokolovsky1355cf42014-04-19 01:25:49 +0300151 int item_sz = mp_binary_get_size('@', self->typecode, NULL);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200152 // TODO: alloc policy
153 self->free = 8;
154 self->items = m_realloc(self->items, item_sz * self->len, item_sz * (self->len + self->free));
Paul Sokolovskya2240672014-04-28 00:16:57 +0300155 mp_seq_clear(self->items, self->len + 1, self->len + self->free, item_sz);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200156 }
Paul Sokolovskyef9124f2014-04-11 03:46:09 +0300157 mp_binary_set_val_array(self->typecode, self->items, self->len++, arg);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200158 self->free--;
159 return mp_const_none; // return None, as per CPython
160}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200161STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_append_obj, array_append);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200162
Damien George729f7b42014-04-17 22:10:53 +0100163STATIC 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 +0100164 if (value == MP_OBJ_NULL) {
Damien George32ca1642014-04-18 22:04:06 +0100165 // delete item
166 // TODO implement
Paul Sokolovsky26905252014-04-20 20:58:33 +0300167 // TODO: confirmed that both bytearray and array.array support
168 // slice deletion
Damien George6ac5dce2014-05-21 19:42:43 +0100169 return MP_OBJ_NULL; // op not supported
Damien George729f7b42014-04-17 22:10:53 +0100170 } else {
171 mp_obj_array_t *o = self_in;
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300172 if (MP_OBJ_IS_TYPE(index_in, &mp_type_slice)) {
Paul Sokolovsky26905252014-04-20 20:58:33 +0300173 if (value != MP_OBJ_SENTINEL) {
174 // Only getting a slice is suported so far, not assignment
175 // TODO: confirmed that both bytearray and array.array support
176 // slice assignment (incl. of different size)
Damien George6ac5dce2014-05-21 19:42:43 +0100177 return MP_OBJ_NULL; // op not supported
Paul Sokolovsky26905252014-04-20 20:58:33 +0300178 }
Paul Sokolovskyde4b9322014-05-25 21:21:57 +0300179 mp_bound_slice_t slice;
180 if (!mp_seq_get_fast_slice_indexes(o->len, index_in, &slice)) {
Paul Sokolovsky5fd5af92014-05-25 22:12:56 +0300181 nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
182 "Only slices with step=1 (aka None) are supported"));
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300183 }
Paul Sokolovskyde4b9322014-05-25 21:21:57 +0300184 mp_obj_array_t *res = array_new(o->typecode, slice.stop - slice.start);
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300185 int sz = mp_binary_get_size('@', o->typecode, NULL);
186 assert(sz > 0);
187 byte *p = o->items;
Paul Sokolovskyde4b9322014-05-25 21:21:57 +0300188 memcpy(res->items, p + slice.start * sz, (slice.stop - slice.start) * sz);
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300189 return res;
Damien George729f7b42014-04-17 22:10:53 +0100190 } else {
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300191 uint index = mp_get_index(o->base.type, o->len, index_in, false);
192 if (value == MP_OBJ_SENTINEL) {
193 // load
194 return mp_binary_get_val_array(o->typecode, o->items, index);
195 } else {
196 // store
197 mp_binary_set_val_array(o->typecode, o->items, index, value);
198 return mp_const_none;
199 }
Damien George729f7b42014-04-17 22:10:53 +0100200 }
Damien Georgef4c9b332014-04-08 21:32:29 +0100201 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200202}
203
Damien George57a4b4f2014-04-18 22:29:21 +0100204STATIC machine_int_t array_get_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, int flags) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200205 mp_obj_array_t *o = o_in;
206 bufinfo->buf = o->items;
Paul Sokolovsky1355cf42014-04-19 01:25:49 +0300207 bufinfo->len = o->len * mp_binary_get_size('@', o->typecode, NULL);
Damien George57a4b4f2014-04-18 22:29:21 +0100208 bufinfo->typecode = o->typecode;
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200209 return 0;
210}
211
Damien George9b196cd2014-03-26 21:47:19 +0000212STATIC const mp_map_elem_t array_locals_dict_table[] = {
213 { MP_OBJ_NEW_QSTR(MP_QSTR_append), (mp_obj_t)&array_append_obj },
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200214};
215
Damien George9b196cd2014-03-26 21:47:19 +0000216STATIC MP_DEFINE_CONST_DICT(array_locals_dict, array_locals_dict_table);
217
Damien Georgecaac5422014-03-25 14:18:18 +0000218const mp_obj_type_t mp_type_array = {
Damien Georgec5966122014-02-15 16:10:44 +0000219 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000220 .name = MP_QSTR_array,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200221 .print = array_print,
222 .make_new = array_make_new,
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200223 .getiter = array_iterator_new,
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200224 .unary_op = array_unary_op,
Damien George729f7b42014-04-17 22:10:53 +0100225 .subscr = array_subscr,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200226 .buffer_p = { .get_buffer = array_get_buffer },
Damien George9b196cd2014-03-26 21:47:19 +0000227 .locals_dict = (mp_obj_t)&array_locals_dict,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200228};
229
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300230const mp_obj_type_t mp_type_bytearray = {
231 { &mp_type_type },
232 .name = MP_QSTR_bytearray,
233 .print = array_print,
234 .make_new = bytearray_make_new,
235 .getiter = array_iterator_new,
236 .unary_op = array_unary_op,
Damien George729f7b42014-04-17 22:10:53 +0100237 .subscr = array_subscr,
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300238 .buffer_p = { .get_buffer = array_get_buffer },
239 .locals_dict = (mp_obj_t)&array_locals_dict,
240};
241
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200242STATIC mp_obj_array_t *array_new(char typecode, uint n) {
Paul Sokolovsky1355cf42014-04-19 01:25:49 +0300243 int typecode_size = mp_binary_get_size('@', typecode, NULL);
Damien George32ca1642014-04-18 22:04:06 +0100244 if (typecode_size <= 0) {
245 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "bad typecode"));
246 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200247 mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300248 o->base.type = (typecode == BYTEARRAY_TYPECODE) ? &mp_type_bytearray : &mp_type_array;
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200249 o->typecode = typecode;
250 o->free = 0;
251 o->len = n;
Damien George32ca1642014-04-18 22:04:06 +0100252 o->items = m_malloc(typecode_size * o->len);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200253 return o;
254}
255
Paul Sokolovsky33996682014-01-21 23:30:10 +0200256uint mp_obj_array_len(mp_obj_t self_in) {
257 return ((mp_obj_array_t *)self_in)->len;
258}
259
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200260mp_obj_t mp_obj_new_bytearray(uint n, void *items) {
261 mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, n);
262 memcpy(o->items, items, n);
263 return o;
264}
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200265
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200266// Create bytearray which references specified memory area
267mp_obj_t mp_obj_new_bytearray_by_ref(uint n, void *items) {
268 mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
Damien Georgecaac5422014-03-25 14:18:18 +0000269 o->base.type = &mp_type_array;
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200270 o->typecode = BYTEARRAY_TYPECODE;
271 o->free = 0;
272 o->len = n;
273 o->items = items;
274 return o;
275}
276
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200277/******************************************************************************/
278/* array iterator */
279
280typedef struct _mp_obj_array_it_t {
281 mp_obj_base_t base;
282 mp_obj_array_t *array;
283 machine_uint_t cur;
284} mp_obj_array_it_t;
285
Damien Georgecaac5422014-03-25 14:18:18 +0000286STATIC mp_obj_t array_it_iternext(mp_obj_t self_in) {
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200287 mp_obj_array_it_t *self = self_in;
288 if (self->cur < self->array->len) {
Paul Sokolovskyef9124f2014-04-11 03:46:09 +0300289 return mp_binary_get_val_array(self->array->typecode, self->array->items, self->cur++);
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200290 } else {
Damien Georgeea8d06c2014-04-17 23:19:36 +0100291 return MP_OBJ_STOP_ITERATION;
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200292 }
293}
294
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200295STATIC const mp_obj_type_t array_it_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000296 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000297 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +0300298 .getiter = mp_identity,
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200299 .iternext = array_it_iternext,
300};
301
Damien Georgecaac5422014-03-25 14:18:18 +0000302STATIC mp_obj_t array_iterator_new(mp_obj_t array_in) {
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200303 mp_obj_array_t *array = array_in;
304 mp_obj_array_it_t *o = m_new_obj(mp_obj_array_it_t);
305 o->base.type = &array_it_type;
306 o->array = array;
307 o->cur = 0;
308 return o;
309}