blob: c6da45728ad165f3eccad82fbd0f07bde4aba69d [file] [log] [blame]
Paul Sokolovsky427905c2014-01-18 19:24:47 +02001#include <string.h>
Paul Sokolovsky427905c2014-01-18 19:24:47 +02002#include <assert.h>
3
4#include "nlr.h"
5#include "misc.h"
6#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00007#include "qstr.h"
Paul Sokolovsky427905c2014-01-18 19:24:47 +02008#include "obj.h"
Paul Sokolovsky427905c2014-01-18 19:24:47 +02009#include "runtime0.h"
10#include "runtime.h"
Paul Sokolovskyc2033242014-02-14 20:21:50 +020011#include "binary.h"
Paul Sokolovsky427905c2014-01-18 19:24:47 +020012
13typedef struct _mp_obj_array_t {
14 mp_obj_base_t base;
Damien George51047752014-02-26 17:40:52 +000015 machine_uint_t typecode : 8;
16 // free is number of unused elements after len used elements
17 // alloc size = len + free
18 machine_uint_t free : (8 * sizeof(machine_uint_t) - 8);
Paul Sokolovsky427905c2014-01-18 19:24:47 +020019 machine_uint_t len; // in elements
20 void *items;
21} mp_obj_array_t;
22
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020023STATIC mp_obj_t array_iterator_new(mp_obj_t array_in);
24STATIC mp_obj_array_t *array_new(char typecode, uint n);
25STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg);
Paul Sokolovsky427905c2014-01-18 19:24:47 +020026
27/******************************************************************************/
28/* array */
29
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020030STATIC 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 +020031 mp_obj_array_t *o = o_in;
32 if (o->typecode == BYTEARRAY_TYPECODE) {
Paul Sokolovsky18014212014-01-28 03:40:48 +020033 print(env, "bytearray(b", o->typecode);
34 mp_str_print_quoted(print, env, o->items, o->len);
Paul Sokolovsky427905c2014-01-18 19:24:47 +020035 } else {
Paul Sokolovsky7e652af2014-01-28 03:14:20 +020036 print(env, "array('%c'", o->typecode);
Paul Sokolovsky18014212014-01-28 03:40:48 +020037 if (o->len > 0) {
Damien George32ca1642014-04-18 22:04:06 +010038 print(env, ", [");
Paul Sokolovsky18014212014-01-28 03:40:48 +020039 for (int i = 0; i < o->len; i++) {
40 if (i > 0) {
41 print(env, ", ");
42 }
Paul Sokolovskyef9124f2014-04-11 03:46:09 +030043 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 +020044 }
Paul Sokolovsky18014212014-01-28 03:40:48 +020045 print(env, "]");
Paul Sokolovsky427905c2014-01-18 19:24:47 +020046 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +020047 }
Paul Sokolovsky7e652af2014-01-28 03:14:20 +020048 print(env, ")");
Paul Sokolovsky427905c2014-01-18 19:24:47 +020049}
50
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020051STATIC mp_obj_t array_construct(char typecode, mp_obj_t initializer) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +020052 uint len;
53 // Try to create array of exact len if initializer len is known
54 mp_obj_t len_in = mp_obj_len_maybe(initializer);
55 if (len_in == MP_OBJ_NULL) {
56 len = 0;
57 } else {
58 len = MP_OBJ_SMALL_INT_VALUE(len_in);
59 }
60
61 mp_obj_array_t *array = array_new(typecode, len);
62
Damien Georged17926d2014-03-30 13:35:08 +010063 mp_obj_t iterable = mp_getiter(initializer);
Paul Sokolovsky427905c2014-01-18 19:24:47 +020064 mp_obj_t item;
65 int i = 0;
Damien Georgeea8d06c2014-04-17 23:19:36 +010066 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +020067 if (len == 0) {
68 array_append(array, item);
69 } else {
Paul Sokolovskyef9124f2014-04-11 03:46:09 +030070 mp_binary_set_val_array(typecode, array->items, i++, item);
Paul Sokolovsky427905c2014-01-18 19:24:47 +020071 }
72 }
73
74 return array;
75}
76
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020077STATIC mp_obj_t array_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
Damien George32ca1642014-04-18 22:04:06 +010078 mp_check_nargs(n_args, 1, 2, n_kw, false);
79
80 // get typecode
Paul Sokolovsky11973b42014-01-28 02:31:52 +020081 uint l;
Damien George698ec212014-02-08 18:17:23 +000082 const char *typecode = mp_obj_str_get_data(args[0], &l);
Paul Sokolovsky11973b42014-01-28 02:31:52 +020083
Damien George32ca1642014-04-18 22:04:06 +010084 if (n_args == 1) {
85 // 1 arg: make an empty array
86 return array_new(*typecode, 0);
87 } else {
88 // 2 args: construct the array from the given iterator
89 return array_construct(*typecode, args[1]);
90 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +020091}
92
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +030093STATIC mp_obj_t bytearray_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
Damien George32ca1642014-04-18 22:04:06 +010094 mp_check_nargs(n_args, 0, 1, n_kw, false);
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +030095
Damien George32ca1642014-04-18 22:04:06 +010096 if (n_args == 0) {
97 // no args: construct an empty bytearray
98 return array_new(BYTEARRAY_TYPECODE, 0);
99 } else if (MP_OBJ_IS_SMALL_INT(args[0])) {
100 // 1 arg, an integer: construct a blank bytearray of that length
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300101 uint len = MP_OBJ_SMALL_INT_VALUE(args[0]);
Paul Sokolovskyb9cf3d32014-04-08 04:42:44 +0300102 mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, len);
103 memset(o->items, 0, len);
104 return o;
Damien George32ca1642014-04-18 22:04:06 +0100105 } else {
106 // 1 arg, an iterator: construct the bytearray from that
107 return array_construct(BYTEARRAY_TYPECODE, args[0]);
Paul Sokolovskyb9cf3d32014-04-08 04:42:44 +0300108 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200109}
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200110
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200111STATIC mp_obj_t array_unary_op(int op, mp_obj_t o_in) {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200112 mp_obj_array_t *o = o_in;
113 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100114 case MP_UNARY_OP_BOOL: return MP_BOOL(o->len != 0);
115 case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(o->len);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100116 default: return MP_OBJ_NOT_SUPPORTED;
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200117 }
118}
119
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200120STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg) {
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300121 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 +0200122 mp_obj_array_t *self = self_in;
123 if (self->free == 0) {
Paul Sokolovskyc2033242014-02-14 20:21:50 +0200124 int item_sz = mp_binary_get_size(self->typecode);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200125 // TODO: alloc policy
126 self->free = 8;
127 self->items = m_realloc(self->items, item_sz * self->len, item_sz * (self->len + self->free));
128 }
Paul Sokolovskyef9124f2014-04-11 03:46:09 +0300129 mp_binary_set_val_array(self->typecode, self->items, self->len++, arg);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200130 self->free--;
131 return mp_const_none; // return None, as per CPython
132}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200133STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_append_obj, array_append);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200134
Damien George729f7b42014-04-17 22:10:53 +0100135STATIC 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 +0100136 if (value == MP_OBJ_NULL) {
Damien George32ca1642014-04-18 22:04:06 +0100137 // delete item
138 // TODO implement
Damien George729f7b42014-04-17 22:10:53 +0100139 return MP_OBJ_NOT_SUPPORTED;
140 } else {
141 mp_obj_array_t *o = self_in;
142 uint index = mp_get_index(o->base.type, o->len, index_in, false);
143 if (value == MP_OBJ_SENTINEL) {
144 // load
145 return mp_binary_get_val_array(o->typecode, o->items, index);
146 } else {
147 // store
148 mp_binary_set_val_array(o->typecode, o->items, index, value);
149 return mp_const_none;
150 }
Damien Georgef4c9b332014-04-08 21:32:29 +0100151 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200152}
153
Damien George57a4b4f2014-04-18 22:29:21 +0100154STATIC 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 +0200155 mp_obj_array_t *o = o_in;
156 bufinfo->buf = o->items;
Paul Sokolovskyc2033242014-02-14 20:21:50 +0200157 bufinfo->len = o->len * mp_binary_get_size(o->typecode);
Damien George57a4b4f2014-04-18 22:29:21 +0100158 bufinfo->typecode = o->typecode;
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200159 return 0;
160}
161
Damien George9b196cd2014-03-26 21:47:19 +0000162STATIC const mp_map_elem_t array_locals_dict_table[] = {
163 { MP_OBJ_NEW_QSTR(MP_QSTR_append), (mp_obj_t)&array_append_obj },
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200164};
165
Damien George9b196cd2014-03-26 21:47:19 +0000166STATIC MP_DEFINE_CONST_DICT(array_locals_dict, array_locals_dict_table);
167
Damien Georgecaac5422014-03-25 14:18:18 +0000168const mp_obj_type_t mp_type_array = {
Damien Georgec5966122014-02-15 16:10:44 +0000169 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000170 .name = MP_QSTR_array,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200171 .print = array_print,
172 .make_new = array_make_new,
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200173 .getiter = array_iterator_new,
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200174 .unary_op = array_unary_op,
Damien George729f7b42014-04-17 22:10:53 +0100175 .subscr = array_subscr,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200176 .buffer_p = { .get_buffer = array_get_buffer },
Damien George9b196cd2014-03-26 21:47:19 +0000177 .locals_dict = (mp_obj_t)&array_locals_dict,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200178};
179
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300180const mp_obj_type_t mp_type_bytearray = {
181 { &mp_type_type },
182 .name = MP_QSTR_bytearray,
183 .print = array_print,
184 .make_new = bytearray_make_new,
185 .getiter = array_iterator_new,
186 .unary_op = array_unary_op,
Damien George729f7b42014-04-17 22:10:53 +0100187 .subscr = array_subscr,
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300188 .buffer_p = { .get_buffer = array_get_buffer },
189 .locals_dict = (mp_obj_t)&array_locals_dict,
190};
191
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200192STATIC mp_obj_array_t *array_new(char typecode, uint n) {
Damien George32ca1642014-04-18 22:04:06 +0100193 int typecode_size = mp_binary_get_size(typecode);
194 if (typecode_size <= 0) {
195 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "bad typecode"));
196 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200197 mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300198 o->base.type = (typecode == BYTEARRAY_TYPECODE) ? &mp_type_bytearray : &mp_type_array;
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200199 o->typecode = typecode;
200 o->free = 0;
201 o->len = n;
Damien George32ca1642014-04-18 22:04:06 +0100202 o->items = m_malloc(typecode_size * o->len);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200203 return o;
204}
205
Paul Sokolovsky33996682014-01-21 23:30:10 +0200206uint mp_obj_array_len(mp_obj_t self_in) {
207 return ((mp_obj_array_t *)self_in)->len;
208}
209
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200210mp_obj_t mp_obj_new_bytearray(uint n, void *items) {
211 mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, n);
212 memcpy(o->items, items, n);
213 return o;
214}
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200215
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200216// Create bytearray which references specified memory area
217mp_obj_t mp_obj_new_bytearray_by_ref(uint n, void *items) {
218 mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
Damien Georgecaac5422014-03-25 14:18:18 +0000219 o->base.type = &mp_type_array;
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200220 o->typecode = BYTEARRAY_TYPECODE;
221 o->free = 0;
222 o->len = n;
223 o->items = items;
224 return o;
225}
226
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200227/******************************************************************************/
228/* array iterator */
229
230typedef struct _mp_obj_array_it_t {
231 mp_obj_base_t base;
232 mp_obj_array_t *array;
233 machine_uint_t cur;
234} mp_obj_array_it_t;
235
Damien Georgecaac5422014-03-25 14:18:18 +0000236STATIC mp_obj_t array_it_iternext(mp_obj_t self_in) {
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200237 mp_obj_array_it_t *self = self_in;
238 if (self->cur < self->array->len) {
Paul Sokolovskyef9124f2014-04-11 03:46:09 +0300239 return mp_binary_get_val_array(self->array->typecode, self->array->items, self->cur++);
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200240 } else {
Damien Georgeea8d06c2014-04-17 23:19:36 +0100241 return MP_OBJ_STOP_ITERATION;
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200242 }
243}
244
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200245STATIC const mp_obj_type_t array_it_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000246 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000247 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +0300248 .getiter = mp_identity,
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200249 .iternext = array_it_iternext,
250};
251
Damien Georgecaac5422014-03-25 14:18:18 +0000252STATIC mp_obj_t array_iterator_new(mp_obj_t array_in) {
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200253 mp_obj_array_t *array = array_in;
254 mp_obj_array_it_t *o = m_new_obj(mp_obj_array_it_t);
255 o->base.type = &array_it_type;
256 o->array = array;
257 o->cur = 0;
258 return o;
259}