blob: cfab6eaa659b6885c02038f745ec31fb4dab636f [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
32static mp_obj_array_t *array_new(char typecode, uint n);
33static mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg);
34
35/******************************************************************************/
36/* array */
37
38static machine_int_t array_get_el_size(char typecode) {
39 // This assumes that unsigned and signed types are of the same type,
40 // which is invariant for [u]intN_t.
41 switch (typecode) {
42 case BYTEARRAY_TYPECODE:
43 case 'b':
44 case 'B':
45 return sizeof(int8_t);
46 case 'h':
47 case 'H':
48 return sizeof(int16_t);
49 case 'i':
50 case 'I':
51 return sizeof(int32_t);
52 case 'l':
53 case 'L':
54 return sizeof(int32_t);
55 }
56 return -1;
57}
58
59static machine_int_t array_get_el(mp_obj_array_t *o, int index) {
60 machine_int_t val = 0;
61 switch (o->typecode) {
62 case 'b':
63 val = ((int8_t*)o->items)[index];
64 break;
65 case BYTEARRAY_TYPECODE:
66 case 'B':
67 val = ((uint8_t*)o->items)[index];
68 break;
69 case 'h':
70 val = ((int16_t*)o->items)[index];
71 break;
72 case 'H':
73 val = ((uint16_t*)o->items)[index];
74 break;
75 case 'i':
76 val = ((int32_t*)o->items)[index];
77 break;
78 case 'I':
79 val = ((uint32_t*)o->items)[index];
80 break;
81 case 'l':
82 val = ((int32_t*)o->items)[index];
83 break;
84 case 'L':
85 val = ((uint32_t*)o->items)[index];
86 break;
87 }
88 return val;
89}
90
91static void array_set_el(mp_obj_array_t *o, int index, mp_obj_t val_in) {
92 machine_int_t val = mp_obj_int_get(val_in);
93 switch (o->typecode) {
94 case 'b':
95 ((int8_t*)o->items)[index] = val;
96 break;
97 case BYTEARRAY_TYPECODE:
98 case 'B':
99 ((uint8_t*)o->items)[index] = val;
100 break;
101 case 'h':
102 ((int16_t*)o->items)[index] = val;
103 break;
104 case 'H':
105 ((uint16_t*)o->items)[index] = val;
106 break;
107 case 'i':
108 ((int32_t*)o->items)[index] = val;
109 break;
110 case 'I':
111 ((uint32_t*)o->items)[index] = val;
112 break;
113 case 'l':
114 ((int32_t*)o->items)[index] = val;
115 break;
116 case 'L':
117 ((uint32_t*)o->items)[index] = val;
118 break;
119 }
120}
121
122
123static void array_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
124 mp_obj_array_t *o = o_in;
125 if (o->typecode == BYTEARRAY_TYPECODE) {
126 print(env, "bytearray([", o->typecode);
127 } else {
128 print(env, "array('%c', [", o->typecode);
129 }
130 for (int i = 0; i < o->len; i++) {
131 if (i > 0) {
132 print(env, ", ");
133 }
134 print(env, "%d", array_get_el(o, i));
135 }
136 print(env, "])");
137}
138
139static mp_obj_t array_construct(char typecode, mp_obj_t initializer) {
140 uint len;
141 // Try to create array of exact len if initializer len is known
142 mp_obj_t len_in = mp_obj_len_maybe(initializer);
143 if (len_in == MP_OBJ_NULL) {
144 len = 0;
145 } else {
146 len = MP_OBJ_SMALL_INT_VALUE(len_in);
147 }
148
149 mp_obj_array_t *array = array_new(typecode, len);
150
151 mp_obj_t iterable = rt_getiter(initializer);
152 mp_obj_t item;
153 int i = 0;
154 while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
155 if (len == 0) {
156 array_append(array, item);
157 } else {
158 array_set_el(array, i++, item);
159 }
160 }
161
162 return array;
163}
164
165static mp_obj_t array_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
166 switch (n_args) {
167 case 2:
168 {
169 const char *code = qstr_str(mp_obj_str_get(args[0]));
170 mp_obj_t initializer = args[1];
171 return array_construct(*code, initializer);
172 }
173
174 default:
175 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "unexpected # of arguments, %d given", n_args));
176 }
177 return NULL;
178}
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,
242 .binary_op = array_binary_op,
243 .store_item = array_store_item,
244 .methods = array_type_methods,
245 .buffer_p = { .get_buffer = array_get_buffer },
246};
247
248static mp_obj_array_t *array_new(char typecode, uint n) {
249 mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
250 o->base.type = &array_type;
251 o->typecode = typecode;
252 o->free = 0;
253 o->len = n;
254 o->items = m_malloc(array_get_el_size(typecode) * o->len);
255 return o;
256}
257
Paul Sokolovsky33996682014-01-21 23:30:10 +0200258uint mp_obj_array_len(mp_obj_t self_in) {
259 return ((mp_obj_array_t *)self_in)->len;
260}
261
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200262mp_obj_t mp_obj_new_bytearray(uint n, void *items) {
263 mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, n);
264 memcpy(o->items, items, n);
265 return o;
266}