blob: 4f9fa49bcb541ed6d8d799c064e6d44bdde9152f [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) {
38 print(env, ", [", o->typecode);
39 for (int i = 0; i < o->len; i++) {
40 if (i > 0) {
41 print(env, ", ");
42 }
Paul Sokolovskyc2033242014-02-14 20:21:50 +020043 mp_obj_print_helper(print, env, mp_binary_get_val(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 Georged17926d2014-03-30 13:35:08 +010066 while ((item = mp_iternext(iterable)) != MP_OBJ_NULL) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +020067 if (len == 0) {
68 array_append(array, item);
69 } else {
Paul Sokolovskyc2033242014-02-14 20:21:50 +020070 mp_binary_set_val(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) {
Paul Sokolovsky11973b42014-01-28 02:31:52 +020078 if (n_args < 1 || n_args > 2) {
Damien Georgeea13f402014-04-05 18:32:08 +010079 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "unexpected # of arguments, %d given", n_args));
Paul Sokolovsky427905c2014-01-18 19:24:47 +020080 }
Paul Sokolovsky11973b42014-01-28 02:31:52 +020081 // TODO check args
82 uint l;
Damien George698ec212014-02-08 18:17:23 +000083 const char *typecode = mp_obj_str_get_data(args[0], &l);
Paul Sokolovsky11973b42014-01-28 02:31:52 +020084 if (n_args == 1) {
85 return array_new(*typecode, 0);
86 }
87
88 return array_construct(*typecode, args[1]);
Paul Sokolovsky427905c2014-01-18 19:24:47 +020089}
90
91// This is top-level factory function, not virtual method
92// TODO: "bytearray" really should be type, not function
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020093STATIC mp_obj_t mp_builtin_bytearray(mp_obj_t arg) {
Paul Sokolovskyb9cf3d32014-04-08 04:42:44 +030094 if (MP_OBJ_IS_SMALL_INT(arg)) {
95 uint len = MP_OBJ_SMALL_INT_VALUE(arg);
96 mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, len);
97 memset(o->items, 0, len);
98 return o;
99 }
100
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200101 return array_construct(BYTEARRAY_TYPECODE, arg);
102}
103MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_bytearray_obj, mp_builtin_bytearray);
104
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200105STATIC mp_obj_t array_unary_op(int op, mp_obj_t o_in) {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200106 mp_obj_array_t *o = o_in;
107 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100108 case MP_UNARY_OP_BOOL: return MP_BOOL(o->len != 0);
109 case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(o->len);
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200110 default: return MP_OBJ_NULL; // op not supported
111 }
112}
113
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200114STATIC mp_obj_t array_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200115 mp_obj_array_t *o = lhs;
116 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100117 case MP_BINARY_OP_SUBSCR:
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200118 {
xbe9e1e8cd2014-03-12 22:57:16 -0700119 uint index = mp_get_index(o->base.type, o->len, rhs, false);
Paul Sokolovskyc2033242014-02-14 20:21:50 +0200120 return mp_binary_get_val(o->typecode, o->items, index);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200121 }
122
123 default:
124 // op not supported
125 return MP_OBJ_NULL;
126 }
127}
128
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200129STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg) {
Damien Georgecaac5422014-03-25 14:18:18 +0000130 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_array));
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200131 mp_obj_array_t *self = self_in;
132 if (self->free == 0) {
Paul Sokolovskyc2033242014-02-14 20:21:50 +0200133 int item_sz = mp_binary_get_size(self->typecode);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200134 // TODO: alloc policy
135 self->free = 8;
136 self->items = m_realloc(self->items, item_sz * self->len, item_sz * (self->len + self->free));
137 }
Paul Sokolovskyc2033242014-02-14 20:21:50 +0200138 mp_binary_set_val(self->typecode, self->items, self->len++, arg);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200139 self->free--;
140 return mp_const_none; // return None, as per CPython
141}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200142STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_append_obj, array_append);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200143
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200144STATIC bool array_store_item(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200145 mp_obj_array_t *o = self_in;
xbe9e1e8cd2014-03-12 22:57:16 -0700146 uint index = mp_get_index(o->base.type, o->len, index_in, false);
Paul Sokolovskyc2033242014-02-14 20:21:50 +0200147 mp_binary_set_val(o->typecode, o->items, index, value);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200148 return true;
149}
150
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200151STATIC machine_int_t array_get_buffer(mp_obj_t o_in, buffer_info_t *bufinfo, int flags) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200152 mp_obj_array_t *o = o_in;
153 bufinfo->buf = o->items;
Paul Sokolovskyc2033242014-02-14 20:21:50 +0200154 bufinfo->len = o->len * mp_binary_get_size(o->typecode);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200155 return 0;
156}
157
Damien George9b196cd2014-03-26 21:47:19 +0000158STATIC const mp_map_elem_t array_locals_dict_table[] = {
159 { MP_OBJ_NEW_QSTR(MP_QSTR_append), (mp_obj_t)&array_append_obj },
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200160};
161
Damien George9b196cd2014-03-26 21:47:19 +0000162STATIC MP_DEFINE_CONST_DICT(array_locals_dict, array_locals_dict_table);
163
Damien Georgecaac5422014-03-25 14:18:18 +0000164const mp_obj_type_t mp_type_array = {
Damien Georgec5966122014-02-15 16:10:44 +0000165 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000166 .name = MP_QSTR_array,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200167 .print = array_print,
168 .make_new = array_make_new,
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200169 .getiter = array_iterator_new,
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200170 .unary_op = array_unary_op,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200171 .binary_op = array_binary_op,
172 .store_item = array_store_item,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200173 .buffer_p = { .get_buffer = array_get_buffer },
Damien George9b196cd2014-03-26 21:47:19 +0000174 .locals_dict = (mp_obj_t)&array_locals_dict,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200175};
176
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200177STATIC mp_obj_array_t *array_new(char typecode, uint n) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200178 mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
Damien Georgecaac5422014-03-25 14:18:18 +0000179 o->base.type = &mp_type_array;
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200180 o->typecode = typecode;
181 o->free = 0;
182 o->len = n;
Paul Sokolovskyc2033242014-02-14 20:21:50 +0200183 o->items = m_malloc(mp_binary_get_size(typecode) * o->len);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200184 return o;
185}
186
Paul Sokolovsky33996682014-01-21 23:30:10 +0200187uint mp_obj_array_len(mp_obj_t self_in) {
188 return ((mp_obj_array_t *)self_in)->len;
189}
190
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200191mp_obj_t mp_obj_new_bytearray(uint n, void *items) {
192 mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, n);
193 memcpy(o->items, items, n);
194 return o;
195}
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200196
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200197// Create bytearray which references specified memory area
198mp_obj_t mp_obj_new_bytearray_by_ref(uint n, void *items) {
199 mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
Damien Georgecaac5422014-03-25 14:18:18 +0000200 o->base.type = &mp_type_array;
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200201 o->typecode = BYTEARRAY_TYPECODE;
202 o->free = 0;
203 o->len = n;
204 o->items = items;
205 return o;
206}
207
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200208/******************************************************************************/
209/* array iterator */
210
211typedef struct _mp_obj_array_it_t {
212 mp_obj_base_t base;
213 mp_obj_array_t *array;
214 machine_uint_t cur;
215} mp_obj_array_it_t;
216
Damien Georgecaac5422014-03-25 14:18:18 +0000217STATIC mp_obj_t array_it_iternext(mp_obj_t self_in) {
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200218 mp_obj_array_it_t *self = self_in;
219 if (self->cur < self->array->len) {
Paul Sokolovskyc2033242014-02-14 20:21:50 +0200220 return mp_binary_get_val(self->array->typecode, self->array->items, self->cur++);
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200221 } else {
Damien George66eaf842014-03-26 19:27:58 +0000222 return MP_OBJ_NULL;
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200223 }
224}
225
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200226STATIC const mp_obj_type_t array_it_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000227 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000228 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +0300229 .getiter = mp_identity,
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200230 .iternext = array_it_iternext,
231};
232
Damien Georgecaac5422014-03-25 14:18:18 +0000233STATIC mp_obj_t array_iterator_new(mp_obj_t array_in) {
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200234 mp_obj_array_t *array = array_in;
235 mp_obj_array_it_t *o = m_new_obj(mp_obj_array_it_t);
236 o->base.type = &array_it_type;
237 o->array = array;
238 o->cur = 0;
239 return o;
240}