blob: 88f7888112dc439266d4748db393b078d9b757f8 [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) {
Paul Sokolovsky11973b42014-01-28 02:31:52 +0200167 if (n_args < 1 || n_args > 2) {
168 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "unexpected # of arguments, %d given", n_args));
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200169 }
Paul Sokolovsky11973b42014-01-28 02:31:52 +0200170 // TODO check args
171 uint l;
172 const byte *typecode = mp_obj_str_get_data(args[0], &l);
173 if (n_args == 1) {
174 return array_new(*typecode, 0);
175 }
176
177 return array_construct(*typecode, args[1]);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200178}
179
180// This is top-level factory function, not virtual method
181// TODO: "bytearray" really should be type, not function
182static mp_obj_t mp_builtin_bytearray(mp_obj_t arg) {
183 return array_construct(BYTEARRAY_TYPECODE, arg);
184}
185MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_bytearray_obj, mp_builtin_bytearray);
186
187static mp_obj_t array_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
188 mp_obj_array_t *o = lhs;
189 switch (op) {
190 case RT_BINARY_OP_SUBSCR:
191 {
192 uint index = mp_get_index(o->base.type, o->len, rhs);
193 machine_int_t val = array_get_el(o, index);
194 return mp_obj_new_int(val);
195 }
196
197 default:
198 // op not supported
199 return MP_OBJ_NULL;
200 }
201}
202
203static mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg) {
204 assert(MP_OBJ_IS_TYPE(self_in, &array_type));
205 mp_obj_array_t *self = self_in;
206 if (self->free == 0) {
207 int item_sz = array_get_el_size(self->typecode);
208 // TODO: alloc policy
209 self->free = 8;
210 self->items = m_realloc(self->items, item_sz * self->len, item_sz * (self->len + self->free));
211 }
212 array_set_el(self, self->len++, arg);
213 self->free--;
214 return mp_const_none; // return None, as per CPython
215}
216static MP_DEFINE_CONST_FUN_OBJ_2(array_append_obj, array_append);
217
218static bool array_store_item(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value) {
219 mp_obj_array_t *o = self_in;
220 uint index = mp_get_index(o->base.type, o->len, index_in);
221 array_set_el(o, index, value);
222 return true;
223}
224
225static machine_int_t array_get_buffer(mp_obj_t o_in, buffer_info_t *bufinfo, int flags) {
226 mp_obj_array_t *o = o_in;
227 bufinfo->buf = o->items;
228 bufinfo->len = o->len * array_get_el_size(o->typecode);
229 return 0;
230}
231
232static const mp_method_t array_type_methods[] = {
233 { "append", &array_append_obj },
234 { NULL, NULL },
235};
236
237const mp_obj_type_t array_type = {
238 { &mp_const_type },
239 "array",
240 .print = array_print,
241 .make_new = array_make_new,
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200242 .getiter = array_iterator_new,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200243 .binary_op = array_binary_op,
244 .store_item = array_store_item,
245 .methods = array_type_methods,
246 .buffer_p = { .get_buffer = array_get_buffer },
247};
248
249static mp_obj_array_t *array_new(char typecode, uint n) {
250 mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
251 o->base.type = &array_type;
252 o->typecode = typecode;
253 o->free = 0;
254 o->len = n;
255 o->items = m_malloc(array_get_el_size(typecode) * o->len);
256 return o;
257}
258
Paul Sokolovsky33996682014-01-21 23:30:10 +0200259uint mp_obj_array_len(mp_obj_t self_in) {
260 return ((mp_obj_array_t *)self_in)->len;
261}
262
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200263mp_obj_t mp_obj_new_bytearray(uint n, void *items) {
264 mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, n);
265 memcpy(o->items, items, n);
266 return o;
267}
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200268
269/******************************************************************************/
270/* array iterator */
271
272typedef struct _mp_obj_array_it_t {
273 mp_obj_base_t base;
274 mp_obj_array_t *array;
275 machine_uint_t cur;
276} mp_obj_array_it_t;
277
278mp_obj_t array_it_iternext(mp_obj_t self_in) {
279 mp_obj_array_it_t *self = self_in;
280 if (self->cur < self->array->len) {
281 machine_int_t val = array_get_el(self->array, self->cur++);
282 return mp_obj_new_int(val);
283 } else {
284 return mp_const_stop_iteration;
285 }
286}
287
288static const mp_obj_type_t array_it_type = {
289 { &mp_const_type },
290 "array_iterator",
291 .iternext = array_it_iternext,
292};
293
294mp_obj_t array_iterator_new(mp_obj_t array_in) {
295 mp_obj_array_t *array = array_in;
296 mp_obj_array_it_t *o = m_new_obj(mp_obj_array_it_t);
297 o->base.type = &array_it_type;
298 o->array = array;
299 o->cur = 0;
300 return o;
301}