blob: 343a3f6e9316b75d3613afdcb19a7a9be689486b [file] [log] [blame]
Paul Sokolovsky427905c2014-01-18 19:24:47 +02001#include <stdlib.h>
2#include <stdint.h>
3#include <string.h>
4#include <stdarg.h>
5#include <assert.h>
6
7#include "nlr.h"
8#include "misc.h"
9#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +000010#include "qstr.h"
Paul Sokolovsky427905c2014-01-18 19:24:47 +020011#include "obj.h"
12#include "map.h"
13#include "runtime0.h"
14#include "runtime.h"
15
16// Use special typecode to differentiate repr() of bytearray vs array.array('B')
17// (underlyingly they're same).
18#define BYTEARRAY_TYPECODE 0
19
20typedef struct _mp_obj_array_t {
21 mp_obj_base_t base;
22 struct {
23 machine_uint_t typecode : 8;
24 // free is number of unused elements after len used elements
25 // alloc size = len + free
26 machine_uint_t free : (8 * sizeof(machine_uint_t) - 8);
27 };
28 machine_uint_t len; // in elements
29 void *items;
30} mp_obj_array_t;
31
Paul Sokolovsky09ce0592014-01-21 23:45:19 +020032static mp_obj_t array_iterator_new(mp_obj_t array_in);
Paul Sokolovsky427905c2014-01-18 19:24:47 +020033static mp_obj_array_t *array_new(char typecode, uint n);
34static mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg);
35
36/******************************************************************************/
37/* array */
38
39static machine_int_t array_get_el_size(char typecode) {
40 // This assumes that unsigned and signed types are of the same type,
41 // which is invariant for [u]intN_t.
42 switch (typecode) {
43 case BYTEARRAY_TYPECODE:
44 case 'b':
45 case 'B':
46 return sizeof(int8_t);
47 case 'h':
48 case 'H':
49 return sizeof(int16_t);
50 case 'i':
51 case 'I':
52 return sizeof(int32_t);
53 case 'l':
54 case 'L':
55 return sizeof(int32_t);
56 }
57 return -1;
58}
59
60static machine_int_t array_get_el(mp_obj_array_t *o, int index) {
61 machine_int_t val = 0;
62 switch (o->typecode) {
63 case 'b':
64 val = ((int8_t*)o->items)[index];
65 break;
66 case BYTEARRAY_TYPECODE:
67 case 'B':
68 val = ((uint8_t*)o->items)[index];
69 break;
70 case 'h':
71 val = ((int16_t*)o->items)[index];
72 break;
73 case 'H':
74 val = ((uint16_t*)o->items)[index];
75 break;
76 case 'i':
77 val = ((int32_t*)o->items)[index];
78 break;
79 case 'I':
80 val = ((uint32_t*)o->items)[index];
81 break;
82 case 'l':
83 val = ((int32_t*)o->items)[index];
84 break;
85 case 'L':
86 val = ((uint32_t*)o->items)[index];
87 break;
88 }
89 return val;
90}
91
92static void array_set_el(mp_obj_array_t *o, int index, mp_obj_t val_in) {
93 machine_int_t val = mp_obj_int_get(val_in);
94 switch (o->typecode) {
95 case 'b':
96 ((int8_t*)o->items)[index] = val;
97 break;
98 case BYTEARRAY_TYPECODE:
99 case 'B':
100 ((uint8_t*)o->items)[index] = val;
101 break;
102 case 'h':
103 ((int16_t*)o->items)[index] = val;
104 break;
105 case 'H':
106 ((uint16_t*)o->items)[index] = val;
107 break;
108 case 'i':
109 ((int32_t*)o->items)[index] = val;
110 break;
111 case 'I':
112 ((uint32_t*)o->items)[index] = val;
113 break;
114 case 'l':
115 ((int32_t*)o->items)[index] = val;
116 break;
117 case 'L':
118 ((uint32_t*)o->items)[index] = val;
119 break;
120 }
121}
122
123
124static void array_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
125 mp_obj_array_t *o = o_in;
126 if (o->typecode == BYTEARRAY_TYPECODE) {
127 print(env, "bytearray([", o->typecode);
128 } else {
129 print(env, "array('%c', [", o->typecode);
130 }
131 for (int i = 0; i < o->len; i++) {
132 if (i > 0) {
133 print(env, ", ");
134 }
135 print(env, "%d", array_get_el(o, i));
136 }
137 print(env, "])");
138}
139
140static mp_obj_t array_construct(char typecode, mp_obj_t initializer) {
141 uint len;
142 // Try to create array of exact len if initializer len is known
143 mp_obj_t len_in = mp_obj_len_maybe(initializer);
144 if (len_in == MP_OBJ_NULL) {
145 len = 0;
146 } else {
147 len = MP_OBJ_SMALL_INT_VALUE(len_in);
148 }
149
150 mp_obj_array_t *array = array_new(typecode, len);
151
152 mp_obj_t iterable = rt_getiter(initializer);
153 mp_obj_t item;
154 int i = 0;
155 while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
156 if (len == 0) {
157 array_append(array, item);
158 } else {
159 array_set_el(array, i++, item);
160 }
161 }
162
163 return array;
164}
165
166static mp_obj_t array_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
167 switch (n_args) {
168 case 2:
169 {
170 const char *code = qstr_str(mp_obj_str_get(args[0]));
171 mp_obj_t initializer = args[1];
172 return array_construct(*code, initializer);
173 }
174
175 default:
176 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "unexpected # of arguments, %d given", n_args));
177 }
178 return NULL;
179}
180
181// This is top-level factory function, not virtual method
182// TODO: "bytearray" really should be type, not function
183static mp_obj_t mp_builtin_bytearray(mp_obj_t arg) {
184 return array_construct(BYTEARRAY_TYPECODE, arg);
185}
186MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_bytearray_obj, mp_builtin_bytearray);
187
188static mp_obj_t array_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
189 mp_obj_array_t *o = lhs;
190 switch (op) {
191 case RT_BINARY_OP_SUBSCR:
192 {
193 uint index = mp_get_index(o->base.type, o->len, rhs);
194 machine_int_t val = array_get_el(o, index);
195 return mp_obj_new_int(val);
196 }
197
198 default:
199 // op not supported
200 return MP_OBJ_NULL;
201 }
202}
203
204static mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg) {
205 assert(MP_OBJ_IS_TYPE(self_in, &array_type));
206 mp_obj_array_t *self = self_in;
207 if (self->free == 0) {
208 int item_sz = array_get_el_size(self->typecode);
209 // TODO: alloc policy
210 self->free = 8;
211 self->items = m_realloc(self->items, item_sz * self->len, item_sz * (self->len + self->free));
212 }
213 array_set_el(self, self->len++, arg);
214 self->free--;
215 return mp_const_none; // return None, as per CPython
216}
217static MP_DEFINE_CONST_FUN_OBJ_2(array_append_obj, array_append);
218
219static bool array_store_item(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value) {
220 mp_obj_array_t *o = self_in;
221 uint index = mp_get_index(o->base.type, o->len, index_in);
222 array_set_el(o, index, value);
223 return true;
224}
225
226static machine_int_t array_get_buffer(mp_obj_t o_in, buffer_info_t *bufinfo, int flags) {
227 mp_obj_array_t *o = o_in;
228 bufinfo->buf = o->items;
229 bufinfo->len = o->len * array_get_el_size(o->typecode);
230 return 0;
231}
232
233static const mp_method_t array_type_methods[] = {
234 { "append", &array_append_obj },
235 { NULL, NULL },
236};
237
238const mp_obj_type_t array_type = {
239 { &mp_const_type },
240 "array",
241 .print = array_print,
242 .make_new = array_make_new,
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200243 .getiter = array_iterator_new,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200244 .binary_op = array_binary_op,
245 .store_item = array_store_item,
246 .methods = array_type_methods,
247 .buffer_p = { .get_buffer = array_get_buffer },
248};
249
250static mp_obj_array_t *array_new(char typecode, uint n) {
251 mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
252 o->base.type = &array_type;
253 o->typecode = typecode;
254 o->free = 0;
255 o->len = n;
256 o->items = m_malloc(array_get_el_size(typecode) * o->len);
257 return o;
258}
259
Paul Sokolovsky33996682014-01-21 23:30:10 +0200260uint mp_obj_array_len(mp_obj_t self_in) {
261 return ((mp_obj_array_t *)self_in)->len;
262}
263
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200264mp_obj_t mp_obj_new_bytearray(uint n, void *items) {
265 mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, n);
266 memcpy(o->items, items, n);
267 return o;
268}
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200269
270/******************************************************************************/
271/* array iterator */
272
273typedef struct _mp_obj_array_it_t {
274 mp_obj_base_t base;
275 mp_obj_array_t *array;
276 machine_uint_t cur;
277} mp_obj_array_it_t;
278
279mp_obj_t array_it_iternext(mp_obj_t self_in) {
280 mp_obj_array_it_t *self = self_in;
281 if (self->cur < self->array->len) {
282 machine_int_t val = array_get_el(self->array, self->cur++);
283 return mp_obj_new_int(val);
284 } else {
285 return mp_const_stop_iteration;
286 }
287}
288
289static const mp_obj_type_t array_it_type = {
290 { &mp_const_type },
291 "array_iterator",
292 .iternext = array_it_iternext,
293};
294
295mp_obj_t array_iterator_new(mp_obj_t array_in) {
296 mp_obj_array_t *array = array_in;
297 mp_obj_array_it_t *o = m_new_obj(mp_obj_array_it_t);
298 o->base.type = &array_it_type;
299 o->array = array;
300 o->cur = 0;
301 return o;
302}