blob: c91c553e3ca8c3eb2034258f36bd624cb03dcef8 [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 Georgec5966122014-02-15 16:10:44 +000079 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 +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 Sokolovsky427905c2014-01-18 19:24:47 +020094 return array_construct(BYTEARRAY_TYPECODE, arg);
95}
96MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_bytearray_obj, mp_builtin_bytearray);
97
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020098STATIC mp_obj_t array_unary_op(int op, mp_obj_t o_in) {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +020099 mp_obj_array_t *o = o_in;
100 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100101 case MP_UNARY_OP_BOOL: return MP_BOOL(o->len != 0);
102 case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(o->len);
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200103 default: return MP_OBJ_NULL; // op not supported
104 }
105}
106
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200107STATIC mp_obj_t array_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200108 mp_obj_array_t *o = lhs;
109 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100110 case MP_BINARY_OP_SUBSCR:
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200111 {
xbe9e1e8cd2014-03-12 22:57:16 -0700112 uint index = mp_get_index(o->base.type, o->len, rhs, false);
Paul Sokolovskyc2033242014-02-14 20:21:50 +0200113 return mp_binary_get_val(o->typecode, o->items, index);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200114 }
115
116 default:
117 // op not supported
118 return MP_OBJ_NULL;
119 }
120}
121
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200122STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg) {
Damien Georgecaac5422014-03-25 14:18:18 +0000123 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_array));
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200124 mp_obj_array_t *self = self_in;
125 if (self->free == 0) {
Paul Sokolovskyc2033242014-02-14 20:21:50 +0200126 int item_sz = mp_binary_get_size(self->typecode);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200127 // TODO: alloc policy
128 self->free = 8;
129 self->items = m_realloc(self->items, item_sz * self->len, item_sz * (self->len + self->free));
130 }
Paul Sokolovskyc2033242014-02-14 20:21:50 +0200131 mp_binary_set_val(self->typecode, self->items, self->len++, arg);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200132 self->free--;
133 return mp_const_none; // return None, as per CPython
134}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200135STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_append_obj, array_append);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200136
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200137STATIC 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 +0200138 mp_obj_array_t *o = self_in;
xbe9e1e8cd2014-03-12 22:57:16 -0700139 uint index = mp_get_index(o->base.type, o->len, index_in, false);
Paul Sokolovskyc2033242014-02-14 20:21:50 +0200140 mp_binary_set_val(o->typecode, o->items, index, value);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200141 return true;
142}
143
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200144STATIC machine_int_t array_get_buffer(mp_obj_t o_in, buffer_info_t *bufinfo, int flags) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200145 mp_obj_array_t *o = o_in;
146 bufinfo->buf = o->items;
Paul Sokolovskyc2033242014-02-14 20:21:50 +0200147 bufinfo->len = o->len * mp_binary_get_size(o->typecode);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200148 return 0;
149}
150
Damien George9b196cd2014-03-26 21:47:19 +0000151STATIC const mp_map_elem_t array_locals_dict_table[] = {
152 { MP_OBJ_NEW_QSTR(MP_QSTR_append), (mp_obj_t)&array_append_obj },
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200153};
154
Damien George9b196cd2014-03-26 21:47:19 +0000155STATIC MP_DEFINE_CONST_DICT(array_locals_dict, array_locals_dict_table);
156
Damien Georgecaac5422014-03-25 14:18:18 +0000157const mp_obj_type_t mp_type_array = {
Damien Georgec5966122014-02-15 16:10:44 +0000158 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000159 .name = MP_QSTR_array,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200160 .print = array_print,
161 .make_new = array_make_new,
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200162 .getiter = array_iterator_new,
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200163 .unary_op = array_unary_op,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200164 .binary_op = array_binary_op,
165 .store_item = array_store_item,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200166 .buffer_p = { .get_buffer = array_get_buffer },
Damien George9b196cd2014-03-26 21:47:19 +0000167 .locals_dict = (mp_obj_t)&array_locals_dict,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200168};
169
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200170STATIC mp_obj_array_t *array_new(char typecode, uint n) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200171 mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
Damien Georgecaac5422014-03-25 14:18:18 +0000172 o->base.type = &mp_type_array;
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200173 o->typecode = typecode;
174 o->free = 0;
175 o->len = n;
Paul Sokolovskyc2033242014-02-14 20:21:50 +0200176 o->items = m_malloc(mp_binary_get_size(typecode) * o->len);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200177 return o;
178}
179
Paul Sokolovsky33996682014-01-21 23:30:10 +0200180uint mp_obj_array_len(mp_obj_t self_in) {
181 return ((mp_obj_array_t *)self_in)->len;
182}
183
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200184mp_obj_t mp_obj_new_bytearray(uint n, void *items) {
185 mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, n);
186 memcpy(o->items, items, n);
187 return o;
188}
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200189
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200190// Create bytearray which references specified memory area
191mp_obj_t mp_obj_new_bytearray_by_ref(uint n, void *items) {
192 mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
Damien Georgecaac5422014-03-25 14:18:18 +0000193 o->base.type = &mp_type_array;
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200194 o->typecode = BYTEARRAY_TYPECODE;
195 o->free = 0;
196 o->len = n;
197 o->items = items;
198 return o;
199}
200
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200201/******************************************************************************/
202/* array iterator */
203
204typedef struct _mp_obj_array_it_t {
205 mp_obj_base_t base;
206 mp_obj_array_t *array;
207 machine_uint_t cur;
208} mp_obj_array_it_t;
209
Damien Georgecaac5422014-03-25 14:18:18 +0000210STATIC mp_obj_t array_it_iternext(mp_obj_t self_in) {
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200211 mp_obj_array_it_t *self = self_in;
212 if (self->cur < self->array->len) {
Paul Sokolovskyc2033242014-02-14 20:21:50 +0200213 return mp_binary_get_val(self->array->typecode, self->array->items, self->cur++);
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200214 } else {
Damien George66eaf842014-03-26 19:27:58 +0000215 return MP_OBJ_NULL;
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200216 }
217}
218
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200219STATIC const mp_obj_type_t array_it_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000220 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000221 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +0300222 .getiter = mp_identity,
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}