blob: eed21d9463b37d19fd5b60cd6ea31bc77b457366 [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"
9#include "map.h"
10#include "runtime0.h"
11#include "runtime.h"
Paul Sokolovskyc2033242014-02-14 20:21:50 +020012#include "binary.h"
Paul Sokolovsky427905c2014-01-18 19:24:47 +020013
14typedef struct _mp_obj_array_t {
15 mp_obj_base_t base;
Damien George51047752014-02-26 17:40:52 +000016 machine_uint_t typecode : 8;
17 // free is number of unused elements after len used elements
18 // alloc size = len + free
19 machine_uint_t free : (8 * sizeof(machine_uint_t) - 8);
Paul Sokolovsky427905c2014-01-18 19:24:47 +020020 machine_uint_t len; // in elements
21 void *items;
22} mp_obj_array_t;
23
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020024STATIC mp_obj_t array_iterator_new(mp_obj_t array_in);
25STATIC mp_obj_array_t *array_new(char typecode, uint n);
26STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg);
Paul Sokolovsky427905c2014-01-18 19:24:47 +020027
28/******************************************************************************/
29/* array */
30
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020031STATIC 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 +020032 mp_obj_array_t *o = o_in;
33 if (o->typecode == BYTEARRAY_TYPECODE) {
Paul Sokolovsky18014212014-01-28 03:40:48 +020034 print(env, "bytearray(b", o->typecode);
35 mp_str_print_quoted(print, env, o->items, o->len);
Paul Sokolovsky427905c2014-01-18 19:24:47 +020036 } else {
Paul Sokolovsky7e652af2014-01-28 03:14:20 +020037 print(env, "array('%c'", o->typecode);
Paul Sokolovsky18014212014-01-28 03:40:48 +020038 if (o->len > 0) {
39 print(env, ", [", o->typecode);
40 for (int i = 0; i < o->len; i++) {
41 if (i > 0) {
42 print(env, ", ");
43 }
Paul Sokolovskyc2033242014-02-14 20:21:50 +020044 mp_obj_print_helper(print, env, mp_binary_get_val(o->typecode, o->items, i), PRINT_REPR);
Paul Sokolovsky7e652af2014-01-28 03:14:20 +020045 }
Paul Sokolovsky18014212014-01-28 03:40:48 +020046 print(env, "]");
Paul Sokolovsky427905c2014-01-18 19:24:47 +020047 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +020048 }
Paul Sokolovsky7e652af2014-01-28 03:14:20 +020049 print(env, ")");
Paul Sokolovsky427905c2014-01-18 19:24:47 +020050}
51
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020052STATIC mp_obj_t array_construct(char typecode, mp_obj_t initializer) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +020053 uint len;
54 // Try to create array of exact len if initializer len is known
55 mp_obj_t len_in = mp_obj_len_maybe(initializer);
56 if (len_in == MP_OBJ_NULL) {
57 len = 0;
58 } else {
59 len = MP_OBJ_SMALL_INT_VALUE(len_in);
60 }
61
62 mp_obj_array_t *array = array_new(typecode, len);
63
64 mp_obj_t iterable = rt_getiter(initializer);
65 mp_obj_t item;
66 int i = 0;
Damien George66eaf842014-03-26 19:27:58 +000067 while ((item = rt_iternext(iterable)) != MP_OBJ_NULL) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +020068 if (len == 0) {
69 array_append(array, item);
70 } else {
Paul Sokolovskyc2033242014-02-14 20:21:50 +020071 mp_binary_set_val(typecode, array->items, i++, item);
Paul Sokolovsky427905c2014-01-18 19:24:47 +020072 }
73 }
74
75 return array;
76}
77
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020078STATIC 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 +020079 if (n_args < 1 || n_args > 2) {
Damien Georgec5966122014-02-15 16:10:44 +000080 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "unexpected # of arguments, %d given", n_args));
Paul Sokolovsky427905c2014-01-18 19:24:47 +020081 }
Paul Sokolovsky11973b42014-01-28 02:31:52 +020082 // TODO check args
83 uint l;
Damien George698ec212014-02-08 18:17:23 +000084 const char *typecode = mp_obj_str_get_data(args[0], &l);
Paul Sokolovsky11973b42014-01-28 02:31:52 +020085 if (n_args == 1) {
86 return array_new(*typecode, 0);
87 }
88
89 return array_construct(*typecode, args[1]);
Paul Sokolovsky427905c2014-01-18 19:24:47 +020090}
91
92// This is top-level factory function, not virtual method
93// TODO: "bytearray" really should be type, not function
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020094STATIC mp_obj_t mp_builtin_bytearray(mp_obj_t arg) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +020095 return array_construct(BYTEARRAY_TYPECODE, arg);
96}
97MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_bytearray_obj, mp_builtin_bytearray);
98
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020099STATIC mp_obj_t array_unary_op(int op, mp_obj_t o_in) {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200100 mp_obj_array_t *o = o_in;
101 switch (op) {
102 case RT_UNARY_OP_BOOL: return MP_BOOL(o->len != 0);
103 case RT_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(o->len);
104 default: return MP_OBJ_NULL; // op not supported
105 }
106}
107
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200108STATIC mp_obj_t array_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200109 mp_obj_array_t *o = lhs;
110 switch (op) {
111 case RT_BINARY_OP_SUBSCR:
112 {
xbe9e1e8cd2014-03-12 22:57:16 -0700113 uint index = mp_get_index(o->base.type, o->len, rhs, false);
Paul Sokolovskyc2033242014-02-14 20:21:50 +0200114 return mp_binary_get_val(o->typecode, o->items, index);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200115 }
116
117 default:
118 // op not supported
119 return MP_OBJ_NULL;
120 }
121}
122
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200123STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg) {
Damien Georgecaac5422014-03-25 14:18:18 +0000124 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_array));
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200125 mp_obj_array_t *self = self_in;
126 if (self->free == 0) {
Paul Sokolovskyc2033242014-02-14 20:21:50 +0200127 int item_sz = mp_binary_get_size(self->typecode);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200128 // TODO: alloc policy
129 self->free = 8;
130 self->items = m_realloc(self->items, item_sz * self->len, item_sz * (self->len + self->free));
131 }
Paul Sokolovskyc2033242014-02-14 20:21:50 +0200132 mp_binary_set_val(self->typecode, self->items, self->len++, arg);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200133 self->free--;
134 return mp_const_none; // return None, as per CPython
135}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200136STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_append_obj, array_append);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200137
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200138STATIC 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 +0200139 mp_obj_array_t *o = self_in;
xbe9e1e8cd2014-03-12 22:57:16 -0700140 uint index = mp_get_index(o->base.type, o->len, index_in, false);
Paul Sokolovskyc2033242014-02-14 20:21:50 +0200141 mp_binary_set_val(o->typecode, o->items, index, value);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200142 return true;
143}
144
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200145STATIC machine_int_t array_get_buffer(mp_obj_t o_in, buffer_info_t *bufinfo, int flags) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200146 mp_obj_array_t *o = o_in;
147 bufinfo->buf = o->items;
Paul Sokolovskyc2033242014-02-14 20:21:50 +0200148 bufinfo->len = o->len * mp_binary_get_size(o->typecode);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200149 return 0;
150}
151
Damien George9b196cd2014-03-26 21:47:19 +0000152STATIC const mp_map_elem_t array_locals_dict_table[] = {
153 { MP_OBJ_NEW_QSTR(MP_QSTR_append), (mp_obj_t)&array_append_obj },
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200154};
155
Damien George9b196cd2014-03-26 21:47:19 +0000156STATIC MP_DEFINE_CONST_DICT(array_locals_dict, array_locals_dict_table);
157
Damien Georgecaac5422014-03-25 14:18:18 +0000158const mp_obj_type_t mp_type_array = {
Damien Georgec5966122014-02-15 16:10:44 +0000159 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000160 .name = MP_QSTR_array,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200161 .print = array_print,
162 .make_new = array_make_new,
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200163 .getiter = array_iterator_new,
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200164 .unary_op = array_unary_op,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200165 .binary_op = array_binary_op,
166 .store_item = array_store_item,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200167 .buffer_p = { .get_buffer = array_get_buffer },
Damien George9b196cd2014-03-26 21:47:19 +0000168 .locals_dict = (mp_obj_t)&array_locals_dict,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200169};
170
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200171STATIC mp_obj_array_t *array_new(char typecode, uint n) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200172 mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
Damien Georgecaac5422014-03-25 14:18:18 +0000173 o->base.type = &mp_type_array;
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200174 o->typecode = typecode;
175 o->free = 0;
176 o->len = n;
Paul Sokolovskyc2033242014-02-14 20:21:50 +0200177 o->items = m_malloc(mp_binary_get_size(typecode) * o->len);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200178 return o;
179}
180
Paul Sokolovsky33996682014-01-21 23:30:10 +0200181uint mp_obj_array_len(mp_obj_t self_in) {
182 return ((mp_obj_array_t *)self_in)->len;
183}
184
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200185mp_obj_t mp_obj_new_bytearray(uint n, void *items) {
186 mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, n);
187 memcpy(o->items, items, n);
188 return o;
189}
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200190
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200191// Create bytearray which references specified memory area
192mp_obj_t mp_obj_new_bytearray_by_ref(uint n, void *items) {
193 mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
Damien Georgecaac5422014-03-25 14:18:18 +0000194 o->base.type = &mp_type_array;
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200195 o->typecode = BYTEARRAY_TYPECODE;
196 o->free = 0;
197 o->len = n;
198 o->items = items;
199 return o;
200}
201
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200202/******************************************************************************/
203/* array iterator */
204
205typedef struct _mp_obj_array_it_t {
206 mp_obj_base_t base;
207 mp_obj_array_t *array;
208 machine_uint_t cur;
209} mp_obj_array_it_t;
210
Damien Georgecaac5422014-03-25 14:18:18 +0000211STATIC mp_obj_t array_it_iternext(mp_obj_t self_in) {
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200212 mp_obj_array_it_t *self = self_in;
213 if (self->cur < self->array->len) {
Paul Sokolovskyc2033242014-02-14 20:21:50 +0200214 return mp_binary_get_val(self->array->typecode, self->array->items, self->cur++);
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200215 } else {
Damien George66eaf842014-03-26 19:27:58 +0000216 return MP_OBJ_NULL;
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200217 }
218}
219
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200220STATIC const mp_obj_type_t array_it_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000221 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000222 .name = MP_QSTR_iterator,
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200223 .iternext = array_it_iternext,
224};
225
Damien Georgecaac5422014-03-25 14:18:18 +0000226STATIC mp_obj_t array_iterator_new(mp_obj_t array_in) {
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200227 mp_obj_array_t *array = array_in;
228 mp_obj_array_it_t *o = m_new_obj(mp_obj_array_it_t);
229 o->base.type = &array_it_type;
230 o->array = array;
231 o->cur = 0;
232 return o;
233}