2014-01-18 17:24:47 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "nlr.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "mpconfig.h"
|
2014-01-21 21:40:13 +00:00
|
|
|
#include "qstr.h"
|
2014-01-18 17:24:47 +00:00
|
|
|
#include "obj.h"
|
|
|
|
#include "map.h"
|
|
|
|
#include "runtime0.h"
|
|
|
|
#include "runtime.h"
|
2014-02-14 18:21:50 +00:00
|
|
|
#include "binary.h"
|
2014-01-18 17:24:47 +00:00
|
|
|
|
|
|
|
typedef struct _mp_obj_array_t {
|
|
|
|
mp_obj_base_t base;
|
2014-02-26 17:40:52 +00:00
|
|
|
machine_uint_t typecode : 8;
|
|
|
|
// free is number of unused elements after len used elements
|
|
|
|
// alloc size = len + free
|
|
|
|
machine_uint_t free : (8 * sizeof(machine_uint_t) - 8);
|
2014-01-18 17:24:47 +00:00
|
|
|
machine_uint_t len; // in elements
|
|
|
|
void *items;
|
|
|
|
} mp_obj_array_t;
|
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t array_iterator_new(mp_obj_t array_in);
|
|
|
|
STATIC mp_obj_array_t *array_new(char typecode, uint n);
|
|
|
|
STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg);
|
2014-01-18 17:24:47 +00:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* array */
|
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC void array_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
|
2014-01-18 17:24:47 +00:00
|
|
|
mp_obj_array_t *o = o_in;
|
|
|
|
if (o->typecode == BYTEARRAY_TYPECODE) {
|
2014-01-28 01:40:48 +00:00
|
|
|
print(env, "bytearray(b", o->typecode);
|
|
|
|
mp_str_print_quoted(print, env, o->items, o->len);
|
2014-01-18 17:24:47 +00:00
|
|
|
} else {
|
2014-01-28 01:14:20 +00:00
|
|
|
print(env, "array('%c'", o->typecode);
|
2014-01-28 01:40:48 +00:00
|
|
|
if (o->len > 0) {
|
|
|
|
print(env, ", [", o->typecode);
|
|
|
|
for (int i = 0; i < o->len; i++) {
|
|
|
|
if (i > 0) {
|
|
|
|
print(env, ", ");
|
|
|
|
}
|
2014-02-14 18:21:50 +00:00
|
|
|
mp_obj_print_helper(print, env, mp_binary_get_val(o->typecode, o->items, i), PRINT_REPR);
|
2014-01-28 01:14:20 +00:00
|
|
|
}
|
2014-01-28 01:40:48 +00:00
|
|
|
print(env, "]");
|
2014-01-18 17:24:47 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-28 01:14:20 +00:00
|
|
|
print(env, ")");
|
2014-01-18 17:24:47 +00:00
|
|
|
}
|
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t array_construct(char typecode, mp_obj_t initializer) {
|
2014-01-18 17:24:47 +00:00
|
|
|
uint len;
|
|
|
|
// Try to create array of exact len if initializer len is known
|
|
|
|
mp_obj_t len_in = mp_obj_len_maybe(initializer);
|
|
|
|
if (len_in == MP_OBJ_NULL) {
|
|
|
|
len = 0;
|
|
|
|
} else {
|
|
|
|
len = MP_OBJ_SMALL_INT_VALUE(len_in);
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_array_t *array = array_new(typecode, len);
|
|
|
|
|
|
|
|
mp_obj_t iterable = rt_getiter(initializer);
|
|
|
|
mp_obj_t item;
|
|
|
|
int i = 0;
|
|
|
|
while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
|
|
|
|
if (len == 0) {
|
|
|
|
array_append(array, item);
|
|
|
|
} else {
|
2014-02-14 18:21:50 +00:00
|
|
|
mp_binary_set_val(typecode, array->items, i++, item);
|
2014-01-18 17:24:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t array_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
|
2014-01-28 00:31:52 +00:00
|
|
|
if (n_args < 1 || n_args > 2) {
|
2014-02-15 16:10:44 +00:00
|
|
|
nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "unexpected # of arguments, %d given", n_args));
|
2014-01-18 17:24:47 +00:00
|
|
|
}
|
2014-01-28 00:31:52 +00:00
|
|
|
// TODO check args
|
|
|
|
uint l;
|
2014-02-08 18:17:23 +00:00
|
|
|
const char *typecode = mp_obj_str_get_data(args[0], &l);
|
2014-01-28 00:31:52 +00:00
|
|
|
if (n_args == 1) {
|
|
|
|
return array_new(*typecode, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_construct(*typecode, args[1]);
|
2014-01-18 17:24:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is top-level factory function, not virtual method
|
|
|
|
// TODO: "bytearray" really should be type, not function
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t mp_builtin_bytearray(mp_obj_t arg) {
|
2014-01-18 17:24:47 +00:00
|
|
|
return array_construct(BYTEARRAY_TYPECODE, arg);
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_bytearray_obj, mp_builtin_bytearray);
|
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t array_unary_op(int op, mp_obj_t o_in) {
|
2014-01-30 02:37:19 +00:00
|
|
|
mp_obj_array_t *o = o_in;
|
|
|
|
switch (op) {
|
|
|
|
case RT_UNARY_OP_BOOL: return MP_BOOL(o->len != 0);
|
|
|
|
case RT_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(o->len);
|
|
|
|
default: return MP_OBJ_NULL; // op not supported
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t array_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
|
2014-01-18 17:24:47 +00:00
|
|
|
mp_obj_array_t *o = lhs;
|
|
|
|
switch (op) {
|
|
|
|
case RT_BINARY_OP_SUBSCR:
|
|
|
|
{
|
|
|
|
uint index = mp_get_index(o->base.type, o->len, rhs);
|
2014-02-14 18:21:50 +00:00
|
|
|
return mp_binary_get_val(o->typecode, o->items, index);
|
2014-01-18 17:24:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
// op not supported
|
|
|
|
return MP_OBJ_NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg) {
|
2014-01-18 17:24:47 +00:00
|
|
|
assert(MP_OBJ_IS_TYPE(self_in, &array_type));
|
|
|
|
mp_obj_array_t *self = self_in;
|
|
|
|
if (self->free == 0) {
|
2014-02-14 18:21:50 +00:00
|
|
|
int item_sz = mp_binary_get_size(self->typecode);
|
2014-01-18 17:24:47 +00:00
|
|
|
// TODO: alloc policy
|
|
|
|
self->free = 8;
|
|
|
|
self->items = m_realloc(self->items, item_sz * self->len, item_sz * (self->len + self->free));
|
|
|
|
}
|
2014-02-14 18:21:50 +00:00
|
|
|
mp_binary_set_val(self->typecode, self->items, self->len++, arg);
|
2014-01-18 17:24:47 +00:00
|
|
|
self->free--;
|
|
|
|
return mp_const_none; // return None, as per CPython
|
|
|
|
}
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_append_obj, array_append);
|
2014-01-18 17:24:47 +00:00
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC bool array_store_item(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value) {
|
2014-01-18 17:24:47 +00:00
|
|
|
mp_obj_array_t *o = self_in;
|
|
|
|
uint index = mp_get_index(o->base.type, o->len, index_in);
|
2014-02-14 18:21:50 +00:00
|
|
|
mp_binary_set_val(o->typecode, o->items, index, value);
|
2014-01-18 17:24:47 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC machine_int_t array_get_buffer(mp_obj_t o_in, buffer_info_t *bufinfo, int flags) {
|
2014-01-18 17:24:47 +00:00
|
|
|
mp_obj_array_t *o = o_in;
|
|
|
|
bufinfo->buf = o->items;
|
2014-02-14 18:21:50 +00:00
|
|
|
bufinfo->len = o->len * mp_binary_get_size(o->typecode);
|
2014-01-18 17:24:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC const mp_method_t array_type_methods[] = {
|
2014-01-18 17:24:47 +00:00
|
|
|
{ "append", &array_append_obj },
|
|
|
|
{ NULL, NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
const mp_obj_type_t array_type = {
|
2014-02-15 16:10:44 +00:00
|
|
|
{ &mp_type_type },
|
2014-02-15 11:34:50 +00:00
|
|
|
.name = MP_QSTR_array,
|
2014-01-18 17:24:47 +00:00
|
|
|
.print = array_print,
|
|
|
|
.make_new = array_make_new,
|
2014-01-21 21:45:19 +00:00
|
|
|
.getiter = array_iterator_new,
|
2014-01-30 02:37:19 +00:00
|
|
|
.unary_op = array_unary_op,
|
2014-01-18 17:24:47 +00:00
|
|
|
.binary_op = array_binary_op,
|
|
|
|
.store_item = array_store_item,
|
|
|
|
.methods = array_type_methods,
|
|
|
|
.buffer_p = { .get_buffer = array_get_buffer },
|
|
|
|
};
|
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_array_t *array_new(char typecode, uint n) {
|
2014-01-18 17:24:47 +00:00
|
|
|
mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
|
|
|
|
o->base.type = &array_type;
|
|
|
|
o->typecode = typecode;
|
|
|
|
o->free = 0;
|
|
|
|
o->len = n;
|
2014-02-14 18:21:50 +00:00
|
|
|
o->items = m_malloc(mp_binary_get_size(typecode) * o->len);
|
2014-01-18 17:24:47 +00:00
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2014-01-21 21:30:10 +00:00
|
|
|
uint mp_obj_array_len(mp_obj_t self_in) {
|
|
|
|
return ((mp_obj_array_t *)self_in)->len;
|
|
|
|
}
|
|
|
|
|
2014-01-18 17:24:47 +00:00
|
|
|
mp_obj_t mp_obj_new_bytearray(uint n, void *items) {
|
|
|
|
mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, n);
|
|
|
|
memcpy(o->items, items, n);
|
|
|
|
return o;
|
|
|
|
}
|
2014-01-21 21:45:19 +00:00
|
|
|
|
2014-01-28 22:21:41 +00:00
|
|
|
// Create bytearray which references specified memory area
|
|
|
|
mp_obj_t mp_obj_new_bytearray_by_ref(uint n, void *items) {
|
|
|
|
mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
|
|
|
|
o->base.type = &array_type;
|
|
|
|
o->typecode = BYTEARRAY_TYPECODE;
|
|
|
|
o->free = 0;
|
|
|
|
o->len = n;
|
|
|
|
o->items = items;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2014-01-21 21:45:19 +00:00
|
|
|
/******************************************************************************/
|
|
|
|
/* array iterator */
|
|
|
|
|
|
|
|
typedef struct _mp_obj_array_it_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
mp_obj_array_t *array;
|
|
|
|
machine_uint_t cur;
|
|
|
|
} mp_obj_array_it_t;
|
|
|
|
|
|
|
|
mp_obj_t array_it_iternext(mp_obj_t self_in) {
|
|
|
|
mp_obj_array_it_t *self = self_in;
|
|
|
|
if (self->cur < self->array->len) {
|
2014-02-14 18:21:50 +00:00
|
|
|
return mp_binary_get_val(self->array->typecode, self->array->items, self->cur++);
|
2014-01-21 21:45:19 +00:00
|
|
|
} else {
|
|
|
|
return mp_const_stop_iteration;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC const mp_obj_type_t array_it_type = {
|
2014-02-15 16:10:44 +00:00
|
|
|
{ &mp_type_type },
|
2014-02-15 11:34:50 +00:00
|
|
|
.name = MP_QSTR_iterator,
|
2014-01-21 21:45:19 +00:00
|
|
|
.iternext = array_it_iternext,
|
|
|
|
};
|
|
|
|
|
|
|
|
mp_obj_t array_iterator_new(mp_obj_t array_in) {
|
|
|
|
mp_obj_array_t *array = array_in;
|
|
|
|
mp_obj_array_it_t *o = m_new_obj(mp_obj_array_it_t);
|
|
|
|
o->base.type = &array_it_type;
|
|
|
|
o->array = array;
|
|
|
|
o->cur = 0;
|
|
|
|
return o;
|
|
|
|
}
|