blob: b394d7746138b3078197b866b66a8e66da77625c [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
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Paul Sokolovsky427905c2014-01-18 19:24:47 +020027#include <string.h>
Paul Sokolovsky427905c2014-01-18 19:24:47 +020028#include <assert.h>
29
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030030#include "mpconfig.h"
Paul Sokolovsky427905c2014-01-18 19:24:47 +020031#include "nlr.h"
32#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000033#include "qstr.h"
Paul Sokolovsky427905c2014-01-18 19:24:47 +020034#include "obj.h"
Paul Sokolovsky427905c2014-01-18 19:24:47 +020035#include "runtime0.h"
36#include "runtime.h"
Paul Sokolovskyc2033242014-02-14 20:21:50 +020037#include "binary.h"
Paul Sokolovsky427905c2014-01-18 19:24:47 +020038
39typedef struct _mp_obj_array_t {
40 mp_obj_base_t base;
Damien George51047752014-02-26 17:40:52 +000041 machine_uint_t typecode : 8;
42 // free is number of unused elements after len used elements
43 // alloc size = len + free
44 machine_uint_t free : (8 * sizeof(machine_uint_t) - 8);
Paul Sokolovsky427905c2014-01-18 19:24:47 +020045 machine_uint_t len; // in elements
46 void *items;
47} mp_obj_array_t;
48
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020049STATIC mp_obj_t array_iterator_new(mp_obj_t array_in);
50STATIC mp_obj_array_t *array_new(char typecode, uint n);
51STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg);
Paul Sokolovsky427905c2014-01-18 19:24:47 +020052
53/******************************************************************************/
54/* array */
55
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020056STATIC 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 +020057 mp_obj_array_t *o = o_in;
58 if (o->typecode == BYTEARRAY_TYPECODE) {
Paul Sokolovsky18014212014-01-28 03:40:48 +020059 print(env, "bytearray(b", o->typecode);
60 mp_str_print_quoted(print, env, o->items, o->len);
Paul Sokolovsky427905c2014-01-18 19:24:47 +020061 } else {
Paul Sokolovsky7e652af2014-01-28 03:14:20 +020062 print(env, "array('%c'", o->typecode);
Paul Sokolovsky18014212014-01-28 03:40:48 +020063 if (o->len > 0) {
Damien George32ca1642014-04-18 22:04:06 +010064 print(env, ", [");
Paul Sokolovsky18014212014-01-28 03:40:48 +020065 for (int i = 0; i < o->len; i++) {
66 if (i > 0) {
67 print(env, ", ");
68 }
Paul Sokolovskyef9124f2014-04-11 03:46:09 +030069 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 +020070 }
Paul Sokolovsky18014212014-01-28 03:40:48 +020071 print(env, "]");
Paul Sokolovsky427905c2014-01-18 19:24:47 +020072 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +020073 }
Paul Sokolovsky7e652af2014-01-28 03:14:20 +020074 print(env, ")");
Paul Sokolovsky427905c2014-01-18 19:24:47 +020075}
76
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020077STATIC mp_obj_t array_construct(char typecode, mp_obj_t initializer) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +020078 uint len;
79 // Try to create array of exact len if initializer len is known
80 mp_obj_t len_in = mp_obj_len_maybe(initializer);
81 if (len_in == MP_OBJ_NULL) {
82 len = 0;
83 } else {
84 len = MP_OBJ_SMALL_INT_VALUE(len_in);
85 }
86
87 mp_obj_array_t *array = array_new(typecode, len);
88
Damien Georged17926d2014-03-30 13:35:08 +010089 mp_obj_t iterable = mp_getiter(initializer);
Paul Sokolovsky427905c2014-01-18 19:24:47 +020090 mp_obj_t item;
91 int i = 0;
Damien Georgeea8d06c2014-04-17 23:19:36 +010092 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +020093 if (len == 0) {
94 array_append(array, item);
95 } else {
Paul Sokolovskyef9124f2014-04-11 03:46:09 +030096 mp_binary_set_val_array(typecode, array->items, i++, item);
Paul Sokolovsky427905c2014-01-18 19:24:47 +020097 }
98 }
99
100 return array;
101}
102
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200103STATIC 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 +0100104 mp_arg_check_num(n_args, n_kw, 1, 2, false);
Damien George32ca1642014-04-18 22:04:06 +0100105
106 // get typecode
Paul Sokolovsky11973b42014-01-28 02:31:52 +0200107 uint l;
Damien George698ec212014-02-08 18:17:23 +0000108 const char *typecode = mp_obj_str_get_data(args[0], &l);
Paul Sokolovsky11973b42014-01-28 02:31:52 +0200109
Damien George32ca1642014-04-18 22:04:06 +0100110 if (n_args == 1) {
111 // 1 arg: make an empty array
112 return array_new(*typecode, 0);
113 } else {
114 // 2 args: construct the array from the given iterator
115 return array_construct(*typecode, args[1]);
116 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200117}
118
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300119STATIC 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 +0100120 mp_arg_check_num(n_args, n_kw, 0, 1, false);
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300121
Damien George32ca1642014-04-18 22:04:06 +0100122 if (n_args == 0) {
123 // no args: construct an empty bytearray
124 return array_new(BYTEARRAY_TYPECODE, 0);
125 } else if (MP_OBJ_IS_SMALL_INT(args[0])) {
126 // 1 arg, an integer: construct a blank bytearray of that length
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300127 uint len = MP_OBJ_SMALL_INT_VALUE(args[0]);
Paul Sokolovskyb9cf3d32014-04-08 04:42:44 +0300128 mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, len);
129 memset(o->items, 0, len);
130 return o;
Damien George32ca1642014-04-18 22:04:06 +0100131 } else {
132 // 1 arg, an iterator: construct the bytearray from that
133 return array_construct(BYTEARRAY_TYPECODE, args[0]);
Paul Sokolovskyb9cf3d32014-04-08 04:42:44 +0300134 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200135}
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200136
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200137STATIC mp_obj_t array_unary_op(int op, mp_obj_t o_in) {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200138 mp_obj_array_t *o = o_in;
139 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100140 case MP_UNARY_OP_BOOL: return MP_BOOL(o->len != 0);
141 case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(o->len);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100142 default: return MP_OBJ_NOT_SUPPORTED;
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200143 }
144}
145
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200146STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg) {
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300147 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 +0200148 mp_obj_array_t *self = self_in;
149 if (self->free == 0) {
Paul Sokolovsky1355cf42014-04-19 01:25:49 +0300150 int item_sz = mp_binary_get_size('@', self->typecode, NULL);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200151 // TODO: alloc policy
152 self->free = 8;
153 self->items = m_realloc(self->items, item_sz * self->len, item_sz * (self->len + self->free));
Paul Sokolovskya2240672014-04-28 00:16:57 +0300154 mp_seq_clear(self->items, self->len + 1, self->len + self->free, item_sz);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200155 }
Paul Sokolovskyef9124f2014-04-11 03:46:09 +0300156 mp_binary_set_val_array(self->typecode, self->items, self->len++, arg);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200157 self->free--;
158 return mp_const_none; // return None, as per CPython
159}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200160STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_append_obj, array_append);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200161
Damien George729f7b42014-04-17 22:10:53 +0100162STATIC 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 +0100163 if (value == MP_OBJ_NULL) {
Damien George32ca1642014-04-18 22:04:06 +0100164 // delete item
165 // TODO implement
Paul Sokolovsky26905252014-04-20 20:58:33 +0300166 // TODO: confirmed that both bytearray and array.array support
167 // slice deletion
Damien George729f7b42014-04-17 22:10:53 +0100168 return MP_OBJ_NOT_SUPPORTED;
169 } else {
170 mp_obj_array_t *o = self_in;
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300171 if (MP_OBJ_IS_TYPE(index_in, &mp_type_slice)) {
Paul Sokolovsky26905252014-04-20 20:58:33 +0300172 if (value != MP_OBJ_SENTINEL) {
173 // Only getting a slice is suported so far, not assignment
174 // TODO: confirmed that both bytearray and array.array support
175 // slice assignment (incl. of different size)
176 return MP_OBJ_NOT_SUPPORTED;
177 }
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300178 machine_uint_t start, stop;
Paul Sokolovskyd915a522014-05-10 21:36:33 +0300179 if (!mp_seq_get_fast_slice_indexes(o->len, index_in, &start, &stop)) {
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300180 assert(0);
181 }
182 mp_obj_array_t *res = array_new(o->typecode, stop - start);
183 int sz = mp_binary_get_size('@', o->typecode, NULL);
184 assert(sz > 0);
185 byte *p = o->items;
186 memcpy(res->items, p + start * sz, (stop - start) * sz);
187 return res;
Damien George729f7b42014-04-17 22:10:53 +0100188 } else {
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300189 uint index = mp_get_index(o->base.type, o->len, index_in, false);
190 if (value == MP_OBJ_SENTINEL) {
191 // load
192 return mp_binary_get_val_array(o->typecode, o->items, index);
193 } else {
194 // store
195 mp_binary_set_val_array(o->typecode, o->items, index, value);
196 return mp_const_none;
197 }
Damien George729f7b42014-04-17 22:10:53 +0100198 }
Damien Georgef4c9b332014-04-08 21:32:29 +0100199 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200200}
201
Damien George57a4b4f2014-04-18 22:29:21 +0100202STATIC 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 +0200203 mp_obj_array_t *o = o_in;
204 bufinfo->buf = o->items;
Paul Sokolovsky1355cf42014-04-19 01:25:49 +0300205 bufinfo->len = o->len * mp_binary_get_size('@', o->typecode, NULL);
Damien George57a4b4f2014-04-18 22:29:21 +0100206 bufinfo->typecode = o->typecode;
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200207 return 0;
208}
209
Damien George9b196cd2014-03-26 21:47:19 +0000210STATIC const mp_map_elem_t array_locals_dict_table[] = {
211 { MP_OBJ_NEW_QSTR(MP_QSTR_append), (mp_obj_t)&array_append_obj },
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200212};
213
Damien George9b196cd2014-03-26 21:47:19 +0000214STATIC MP_DEFINE_CONST_DICT(array_locals_dict, array_locals_dict_table);
215
Damien Georgecaac5422014-03-25 14:18:18 +0000216const mp_obj_type_t mp_type_array = {
Damien Georgec5966122014-02-15 16:10:44 +0000217 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000218 .name = MP_QSTR_array,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200219 .print = array_print,
220 .make_new = array_make_new,
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200221 .getiter = array_iterator_new,
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200222 .unary_op = array_unary_op,
Damien George729f7b42014-04-17 22:10:53 +0100223 .subscr = array_subscr,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200224 .buffer_p = { .get_buffer = array_get_buffer },
Damien George9b196cd2014-03-26 21:47:19 +0000225 .locals_dict = (mp_obj_t)&array_locals_dict,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200226};
227
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300228const mp_obj_type_t mp_type_bytearray = {
229 { &mp_type_type },
230 .name = MP_QSTR_bytearray,
231 .print = array_print,
232 .make_new = bytearray_make_new,
233 .getiter = array_iterator_new,
234 .unary_op = array_unary_op,
Damien George729f7b42014-04-17 22:10:53 +0100235 .subscr = array_subscr,
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300236 .buffer_p = { .get_buffer = array_get_buffer },
237 .locals_dict = (mp_obj_t)&array_locals_dict,
238};
239
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200240STATIC mp_obj_array_t *array_new(char typecode, uint n) {
Paul Sokolovsky1355cf42014-04-19 01:25:49 +0300241 int typecode_size = mp_binary_get_size('@', typecode, NULL);
Damien George32ca1642014-04-18 22:04:06 +0100242 if (typecode_size <= 0) {
243 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "bad typecode"));
244 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200245 mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300246 o->base.type = (typecode == BYTEARRAY_TYPECODE) ? &mp_type_bytearray : &mp_type_array;
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200247 o->typecode = typecode;
248 o->free = 0;
249 o->len = n;
Damien George32ca1642014-04-18 22:04:06 +0100250 o->items = m_malloc(typecode_size * o->len);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200251 return o;
252}
253
Paul Sokolovsky33996682014-01-21 23:30:10 +0200254uint mp_obj_array_len(mp_obj_t self_in) {
255 return ((mp_obj_array_t *)self_in)->len;
256}
257
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200258mp_obj_t mp_obj_new_bytearray(uint n, void *items) {
259 mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, n);
260 memcpy(o->items, items, n);
261 return o;
262}
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200263
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200264// Create bytearray which references specified memory area
265mp_obj_t mp_obj_new_bytearray_by_ref(uint n, void *items) {
266 mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
Damien Georgecaac5422014-03-25 14:18:18 +0000267 o->base.type = &mp_type_array;
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200268 o->typecode = BYTEARRAY_TYPECODE;
269 o->free = 0;
270 o->len = n;
271 o->items = items;
272 return o;
273}
274
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200275/******************************************************************************/
276/* array iterator */
277
278typedef struct _mp_obj_array_it_t {
279 mp_obj_base_t base;
280 mp_obj_array_t *array;
281 machine_uint_t cur;
282} mp_obj_array_it_t;
283
Damien Georgecaac5422014-03-25 14:18:18 +0000284STATIC mp_obj_t array_it_iternext(mp_obj_t self_in) {
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200285 mp_obj_array_it_t *self = self_in;
286 if (self->cur < self->array->len) {
Paul Sokolovskyef9124f2014-04-11 03:46:09 +0300287 return mp_binary_get_val_array(self->array->typecode, self->array->items, self->cur++);
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200288 } else {
Damien Georgeea8d06c2014-04-17 23:19:36 +0100289 return MP_OBJ_STOP_ITERATION;
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200290 }
291}
292
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200293STATIC const mp_obj_type_t array_it_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000294 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000295 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +0300296 .getiter = mp_identity,
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200297 .iternext = array_it_iternext,
298};
299
Damien Georgecaac5422014-03-25 14:18:18 +0000300STATIC mp_obj_t array_iterator_new(mp_obj_t array_in) {
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200301 mp_obj_array_t *array = array_in;
302 mp_obj_array_it_t *o = m_new_obj(mp_obj_array_it_t);
303 o->base.type = &array_it_type;
304 o->array = array;
305 o->cur = 0;
306 return o;
307}