blob: 85462b3a8b737cb260b4c7bad0d6c535b6c1d777 [file] [log] [blame]
Damiend99b0522013-12-21 18:17:45 +00001// All Micro Python objects are at least this type
2// It must be of pointer size
3
4typedef machine_ptr_t mp_obj_t;
5typedef machine_const_ptr_t mp_const_obj_t;
6
7// Integers that fit in a pointer have this type
8// (do we need to expose this in the public API?)
9
10typedef machine_int_t mp_small_int_t;
11
12// The machine floating-point type used for float and complex numbers
Damien660365e2013-12-17 18:27:24 +000013
14#if MICROPY_ENABLE_FLOAT
Damiend99b0522013-12-21 18:17:45 +000015typedef machine_float_t mp_float_t;
Damien660365e2013-12-17 18:27:24 +000016#endif
17
Damien George97209d32014-01-07 15:58:30 +000018// Anything that wants to be a Micro Python object must have
19// mp_obj_base_t as its first member (except NULL and small ints)
Damien660365e2013-12-17 18:27:24 +000020
ian-v7a16fad2014-01-06 09:52:29 -080021struct _mp_obj_type_t;
Damiend99b0522013-12-21 18:17:45 +000022struct _mp_obj_base_t {
ian-v7a16fad2014-01-06 09:52:29 -080023 const struct _mp_obj_type_t *type;
Damiend99b0522013-12-21 18:17:45 +000024};
ian-v7a16fad2014-01-06 09:52:29 -080025typedef struct _mp_obj_base_t mp_obj_base_t;
Damiend99b0522013-12-21 18:17:45 +000026
27// The NULL object is used to indicate the absence of an object
28// It *cannot* be used when an mp_obj_t is expected, except where explicitly allowed
29
30#define MP_OBJ_NULL ((mp_obj_t)NULL)
31
Damien George38a2da62014-01-08 17:33:12 +000032// These macros check for small int, qstr or object, and access small int and qstr values
33// - xxxx...xxx1: a small int, bits 1 and above are the value
34// - xxxx...xx10: a qstr, bits 2 and above are the value
35// - xxxx...xx00: a pointer to an mp_obj_base_t
Damiend99b0522013-12-21 18:17:45 +000036
Paul Sokolovsky757ac812014-01-12 17:06:25 +020037// In SMALL_INT, next-to-highest bits is used as sign, so both must match for value in range
38#define MP_OBJ_FITS_SMALL_INT(n) ((((n) ^ ((n) << 1)) & WORD_MSBIT_HIGH) == 0)
Damiend99b0522013-12-21 18:17:45 +000039#define MP_OBJ_IS_SMALL_INT(o) ((((mp_small_int_t)(o)) & 1) != 0)
Damien George38a2da62014-01-08 17:33:12 +000040#define MP_OBJ_IS_QSTR(o) ((((mp_small_int_t)(o)) & 3) == 2)
41#define MP_OBJ_IS_OBJ(o) ((((mp_small_int_t)(o)) & 3) == 0)
Damien George7c9c6672014-01-25 00:17:36 +000042#define MP_OBJ_IS_TYPE(o, t) (MP_OBJ_IS_OBJ(o) && (((mp_obj_base_t*)(o))->type == (t))) // this does not work for checking a string, use below macro for that
Paul Sokolovsky2ca84aa2014-01-26 01:57:48 +020043#define MP_OBJ_IS_INT(o) (MP_OBJ_IS_SMALL_INT(o) || MP_OBJ_IS_TYPE(o, &int_type))
Damien George5fa93b62014-01-22 14:35:10 +000044#define MP_OBJ_IS_STR(o) (MP_OBJ_IS_QSTR(o) || MP_OBJ_IS_TYPE(o, &str_type))
Damien George38a2da62014-01-08 17:33:12 +000045
Damiend99b0522013-12-21 18:17:45 +000046#define MP_OBJ_SMALL_INT_VALUE(o) (((mp_small_int_t)(o)) >> 1)
Damien George38a2da62014-01-08 17:33:12 +000047#define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)(((small_int) << 1) | 1))
48
49#define MP_OBJ_QSTR_VALUE(o) (((mp_small_int_t)(o)) >> 2)
50#define MP_OBJ_NEW_QSTR(qstr) ((mp_obj_t)((((machine_uint_t)qstr) << 2) | 2))
Damiend99b0522013-12-21 18:17:45 +000051
52// These macros are used to declare and define constant function objects
53// You can put "static" in front of the definitions to make them local
54
55#define MP_DECLARE_CONST_FUN_OBJ(obj_name) extern const mp_obj_fun_native_t obj_name
56
Paul Sokolovsky0473e272014-02-02 17:49:01 +020057#define MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, is_kw, n_args_min, n_args_max, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, {is_kw, n_args_min}, n_args_max, (void *)fun_name}
John R. Lenton270112f2014-01-07 18:01:08 +000058#define MP_DEFINE_CONST_FUN_OBJ_0(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, 0, 0, (mp_fun_0_t)fun_name)
59#define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, 1, 1, (mp_fun_1_t)fun_name)
60#define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, 2, 2, (mp_fun_2_t)fun_name)
61#define MP_DEFINE_CONST_FUN_OBJ_3(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, 3, 3, (mp_fun_3_t)fun_name)
62#define MP_DEFINE_CONST_FUN_OBJ_VAR(obj_name, n_args_min, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, n_args_min, (~((machine_uint_t)0)), (mp_fun_var_t)fun_name)
63#define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, n_args_min, n_args_max, (mp_fun_var_t)fun_name)
John R. Lenton88cb1e62014-01-13 19:55:18 +000064#define MP_DEFINE_CONST_FUN_OBJ_KW(obj_name, n_args_min, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, true, n_args_min, (~((machine_uint_t)0)), (mp_fun_kw_t)fun_name)
John R. Lentonc06763a2014-01-07 17:29:16 +000065
Damien Georgeeae16442014-01-11 19:22:29 +000066// These macros are used to declare and define constant staticmethond and classmethod objects
67// You can put "static" in front of the definitions to make them local
68
Damien George64131f32014-02-06 20:31:44 +000069#define MP_DECLARE_CONST_STATICMETHOD_OBJ(obj_name) extern const mp_obj_static_class_method_t obj_name
70#define MP_DECLARE_CONST_CLASSMETHOD_OBJ(obj_name) extern const mp_obj_static_class_method_t obj_name
Damien Georgeeae16442014-01-11 19:22:29 +000071
Damien George64131f32014-02-06 20:31:44 +000072#define MP_DEFINE_CONST_STATICMETHOD_OBJ(obj_name, fun_name) const mp_obj_static_class_method_t obj_name = {{&mp_type_staticmethod}, fun_name}
73#define MP_DEFINE_CONST_CLASSMETHOD_OBJ(obj_name, fun_name) const mp_obj_static_class_method_t obj_name = {{&mp_type_classmethod}, fun_name}
Damien Georgeeae16442014-01-11 19:22:29 +000074
John R. Lentonc06763a2014-01-07 17:29:16 +000075// Need to declare this here so we are not dependent on map.h
76struct _mp_map_t;
Damien George062478e2014-01-09 20:57:50 +000077struct _mp_map_elem_t;
78enum _mp_map_lookup_kind_t;
Damiend99b0522013-12-21 18:17:45 +000079
80// Type definitions for methods
81
82typedef mp_obj_t (*mp_fun_0_t)(void);
83typedef mp_obj_t (*mp_fun_1_t)(mp_obj_t);
84typedef mp_obj_t (*mp_fun_2_t)(mp_obj_t, mp_obj_t);
John R. Lenton45a87442014-01-04 01:15:01 +000085typedef mp_obj_t (*mp_fun_3_t)(mp_obj_t, mp_obj_t, mp_obj_t);
Damiend99b0522013-12-21 18:17:45 +000086typedef mp_obj_t (*mp_fun_t)(void);
Damien Georgea11ceca2014-01-19 16:02:09 +000087typedef mp_obj_t (*mp_fun_var_t)(uint n, const mp_obj_t *);
Damien George20006db2014-01-18 14:10:48 +000088typedef mp_obj_t (*mp_fun_kw_t)(uint n, const mp_obj_t *, struct _mp_map_t *);
Damiend99b0522013-12-21 18:17:45 +000089
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020090typedef enum {
91 PRINT_STR, PRINT_REPR
92} mp_print_kind_t;
93
94typedef void (*mp_print_fun_t)(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o, mp_print_kind_t kind);
Damien George20006db2014-01-18 14:10:48 +000095typedef mp_obj_t (*mp_make_new_fun_t)(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args);
96typedef mp_obj_t (*mp_call_fun_t)(mp_obj_t fun, uint n_args, uint n_kw, const mp_obj_t *args);
Damiend99b0522013-12-21 18:17:45 +000097typedef mp_obj_t (*mp_unary_op_fun_t)(int op, mp_obj_t);
98typedef mp_obj_t (*mp_binary_op_fun_t)(int op, mp_obj_t, mp_obj_t);
Damien George20006db2014-01-18 14:10:48 +000099typedef void (*mp_load_attr_fun_t)(mp_obj_t self_in, qstr attr, mp_obj_t *dest); // for fail, do nothing; for attr, dest[0] = value; for method, dest[0] = method, dest[1] = self
Damien George062478e2014-01-09 20:57:50 +0000100typedef bool (*mp_store_attr_fun_t)(mp_obj_t self_in, qstr attr, mp_obj_t value); // return true if store succeeded
Paul Sokolovsky6d8edf62014-01-18 13:10:51 +0200101typedef bool (*mp_store_item_fun_t)(mp_obj_t self_in, mp_obj_t index, mp_obj_t value); // return true if store succeeded
Damiend99b0522013-12-21 18:17:45 +0000102
103typedef struct _mp_method_t {
Damien660365e2013-12-17 18:27:24 +0000104 const char *name;
Damiend99b0522013-12-21 18:17:45 +0000105 mp_const_obj_t fun;
106} mp_method_t;
107
Paul Sokolovsky5b15daf2014-01-07 20:12:26 +0200108// Buffer protocol
109typedef struct _buffer_info_t {
110 // if we'd bother to support various versions of structure
111 // (with different number of fields), we can distinguish
112 // them with ver = sizeof(struct). Cons: overkill for *micro*?
113 //int ver; // ?
114
115 void *buf;
116 machine_int_t len;
117
118 // Rationale: have array.array and have SIMD operations on them
119 // Cons: users can pass item size to processing functions themselves,
120 // though that's not "plug&play"
121 // int itemsize;
122
123 // Rationale: to load arbitrary-sized sprites directly to LCD
124 // Cons: a bit adhoc usecase
125 // int stride;
126} buffer_info_t;
127#define BUFFER_READ (1)
128#define BUFFER_WRITE (2)
129#define BUFFER_RW (BUFFER_READ | BUFFER_WRITE)
130typedef struct _mp_buffer_p_t {
131 machine_int_t (*get_buffer)(mp_obj_t obj, buffer_info_t *bufinfo, int flags);
132} mp_buffer_p_t;
133
134// Stream protocol
135typedef struct _mp_stream_p_t {
136 // On error, functions should return -1 and fill in *errcode (values are
137 // implementation-dependent, but will be exposed to user, e.g. via exception).
138 machine_int_t (*read)(mp_obj_t obj, void *buf, machine_uint_t size, int *errcode);
139 machine_int_t (*write)(mp_obj_t obj, const void *buf, machine_uint_t size, int *errcode);
140 // add seek() ?
141} mp_stream_p_t;
142
Damiend99b0522013-12-21 18:17:45 +0000143struct _mp_obj_type_t {
144 mp_obj_base_t base;
145 const char *name;
146 mp_print_fun_t print;
Damien George71c51812014-01-04 20:21:15 +0000147 mp_make_new_fun_t make_new; // to make an instance of the type
Damiend99b0522013-12-21 18:17:45 +0000148
Damien George20006db2014-01-18 14:10:48 +0000149 mp_call_fun_t call;
Damiend99b0522013-12-21 18:17:45 +0000150 mp_unary_op_fun_t unary_op; // can return NULL if op not supported
151 mp_binary_op_fun_t binary_op; // can return NULL if op not supported
152
153 mp_fun_1_t getiter;
154 mp_fun_1_t iternext;
155
Paul Sokolovsky5b15daf2014-01-07 20:12:26 +0200156 // Alternatively, pointer(s) to interfaces to save space
157 // in mp_obj_type_t at the expense of extra pointer and extra dereference
158 // when actually used.
159 mp_buffer_p_t buffer_p;
160 mp_stream_p_t stream_p;
161
ian-v7a16fad2014-01-06 09:52:29 -0800162 const mp_method_t *methods;
Damiend99b0522013-12-21 18:17:45 +0000163
Damien George062478e2014-01-09 20:57:50 +0000164 mp_load_attr_fun_t load_attr;
165 mp_store_attr_fun_t store_attr;
Paul Sokolovsky6d8edf62014-01-18 13:10:51 +0200166 // Implements container[index] = val; note that load_item is implemented
167 // by binary_op(RT_BINARY_OP_SUBSCR)
168 mp_store_item_fun_t store_item;
Damien George004cdce2014-01-09 21:43:51 +0000169
170 // these are for dynamically created types (classes)
171 mp_obj_t bases_tuple;
172 mp_obj_t locals_dict;
Damien George062478e2014-01-09 20:57:50 +0000173
Damiend99b0522013-12-21 18:17:45 +0000174 /*
175 What we might need to add here:
176
Damiend99b0522013-12-21 18:17:45 +0000177 store_subscr list dict
178
179 len str tuple list map
180 abs float complex
181 hash bool int none str
182 equal int str
Damiend99b0522013-12-21 18:17:45 +0000183 get_array_n tuple list
184
185 unpack seq list tuple
Damiend99b0522013-12-21 18:17:45 +0000186 */
187};
188
ian-v7a16fad2014-01-06 09:52:29 -0800189typedef struct _mp_obj_type_t mp_obj_type_t;
Damiend99b0522013-12-21 18:17:45 +0000190
191// Constant objects, globally accessible
192
193extern const mp_obj_type_t mp_const_type;
194extern const mp_obj_t mp_const_none;
195extern const mp_obj_t mp_const_false;
196extern const mp_obj_t mp_const_true;
Damien George71c51812014-01-04 20:21:15 +0000197extern const mp_obj_t mp_const_empty_tuple;
Damien Georgee9906ac2014-01-04 18:44:46 +0000198extern const mp_obj_t mp_const_ellipsis;
Damiend99b0522013-12-21 18:17:45 +0000199extern const mp_obj_t mp_const_stop_iteration; // special object indicating end of iteration (not StopIteration exception!)
200
Damiend99b0522013-12-21 18:17:45 +0000201// General API for objects
202
Damien George5fa93b62014-01-22 14:35:10 +0000203mp_obj_t mp_obj_new_type(const char *name, mp_obj_t bases_tuple, mp_obj_t locals_dict);
Damiend99b0522013-12-21 18:17:45 +0000204mp_obj_t mp_obj_new_none(void);
205mp_obj_t mp_obj_new_bool(bool value);
Damien George6baf76e2013-12-30 22:32:17 +0000206mp_obj_t mp_obj_new_cell(mp_obj_t obj);
Damiend99b0522013-12-21 18:17:45 +0000207mp_obj_t mp_obj_new_int(machine_int_t value);
Paul Sokolovsky48b35722014-01-12 17:30:48 +0200208mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value);
209mp_obj_t mp_obj_new_int_from_long_str(const char *s);
Damien George5fa93b62014-01-22 14:35:10 +0000210mp_obj_t mp_obj_new_str(const byte* data, uint len, bool make_qstr_if_not_already);
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200211mp_obj_t mp_obj_new_bytes(const byte* data, uint len);
Damiend99b0522013-12-21 18:17:45 +0000212#if MICROPY_ENABLE_FLOAT
213mp_obj_t mp_obj_new_float(mp_float_t val);
214mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag);
215#endif
216mp_obj_t mp_obj_new_exception(qstr id);
217mp_obj_t mp_obj_new_exception_msg(qstr id, const char *msg);
218mp_obj_t mp_obj_new_exception_msg_1_arg(qstr id, const char *fmt, const char *a1);
219mp_obj_t mp_obj_new_exception_msg_2_args(qstr id, const char *fmt, const char *a1, const char *a2);
Damien George6c73ca12014-01-08 18:11:23 +0000220mp_obj_t mp_obj_new_exception_msg_varg(qstr id, const char *fmt, ...); // counts args by number of % symbols in fmt, excluding %%; can only handle void* sizes (ie no float/double!)
Damiend99b0522013-12-21 18:17:45 +0000221mp_obj_t mp_obj_new_range(int start, int stop, int step);
222mp_obj_t mp_obj_new_range_iterator(int cur, int stop, int step);
Paul Sokolovsky90750022014-02-01 15:05:04 +0200223mp_obj_t mp_obj_new_fun_bc(int n_args, mp_obj_t def_args, uint n_state, const byte *code);
Damiend99b0522013-12-21 18:17:45 +0000224mp_obj_t mp_obj_new_fun_asm(uint n_args, void *fun);
Damien Georged0691cc2014-01-29 20:30:52 +0000225mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun);
Damien George0ff88392014-01-02 20:57:05 +0000226mp_obj_t mp_obj_new_gen_instance(const byte *bytecode, uint n_state, int n_args, const mp_obj_t *args);
Damiend99b0522013-12-21 18:17:45 +0000227mp_obj_t mp_obj_new_closure(mp_obj_t fun, mp_obj_t closure_tuple);
John R. Lentonc06763a2014-01-07 17:29:16 +0000228mp_obj_t mp_obj_new_tuple(uint n, const mp_obj_t *items);
Damiend99b0522013-12-21 18:17:45 +0000229mp_obj_t mp_obj_new_list(uint n, mp_obj_t *items);
Damiend99b0522013-12-21 18:17:45 +0000230mp_obj_t mp_obj_new_dict(int n_args);
231mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items);
Paul Sokolovsky1c6de112014-01-03 02:41:17 +0200232mp_obj_t mp_obj_new_slice(mp_obj_t start, mp_obj_t stop, mp_obj_t step);
Damien George35e2a4e2014-02-05 00:51:47 +0000233mp_obj_t mp_obj_new_super(mp_obj_t type, mp_obj_t obj);
Damien George20006db2014-01-18 14:10:48 +0000234mp_obj_t mp_obj_new_bound_meth(mp_obj_t meth, mp_obj_t self);
Damien George7c9c6672014-01-25 00:17:36 +0000235mp_obj_t mp_obj_new_getitem_iter(mp_obj_t *args);
Damien George28708622014-01-02 21:30:26 +0000236mp_obj_t mp_obj_new_module(qstr module_name);
Damiend99b0522013-12-21 18:17:45 +0000237
Damien George5fa93b62014-01-22 14:35:10 +0000238mp_obj_type_t *mp_obj_get_type(mp_obj_t o_in);
Damiend99b0522013-12-21 18:17:45 +0000239const char *mp_obj_get_type_str(mp_obj_t o_in);
240
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200241void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind);
242void mp_obj_print(mp_obj_t o, mp_print_kind_t kind);
Damien George136b1492014-01-19 12:38:49 +0000243void mp_obj_print_exception(mp_obj_t exc);
Damiend99b0522013-12-21 18:17:45 +0000244
245bool mp_obj_is_callable(mp_obj_t o_in);
246machine_int_t mp_obj_hash(mp_obj_t o_in);
247bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2);
248bool mp_obj_less(mp_obj_t o1, mp_obj_t o2);
249
250machine_int_t mp_obj_get_int(mp_obj_t arg);
251#if MICROPY_ENABLE_FLOAT
252mp_float_t mp_obj_get_float(mp_obj_t self_in);
253void mp_obj_get_complex(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
254#endif
Damien George5fa93b62014-01-22 14:35:10 +0000255//qstr mp_obj_get_qstr(mp_obj_t arg);
Damiend99b0522013-12-21 18:17:45 +0000256mp_obj_t *mp_obj_get_array_fixed_n(mp_obj_t o, machine_int_t n);
257uint mp_get_index(const mp_obj_type_t *type, machine_uint_t len, mp_obj_t index);
John R. Lenton4bee76e2014-01-10 11:25:03 +0000258mp_obj_t mp_obj_len_maybe(mp_obj_t o_in); /* may return NULL */
Damiend99b0522013-12-21 18:17:45 +0000259
260// none
261extern const mp_obj_type_t none_type;
262
263// bool
264extern const mp_obj_type_t bool_type;
John R. Lentonb8698fc2014-01-11 00:58:59 +0000265#define MP_BOOL(x) (x ? mp_const_true : mp_const_false)
Damiend99b0522013-12-21 18:17:45 +0000266
267// cell
268mp_obj_t mp_obj_cell_get(mp_obj_t self_in);
269void mp_obj_cell_set(mp_obj_t self_in, mp_obj_t obj);
270
Damien George71c51812014-01-04 20:21:15 +0000271// int
272extern const mp_obj_type_t int_type;
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200273// For long int, returns value truncated to machine_int_t
274machine_int_t mp_obj_int_get(mp_obj_t self_in);
275// Will rains exception if value doesn't fit into machine_int_t
276machine_int_t mp_obj_int_get_checked(mp_obj_t self_in);
Damien George71c51812014-01-04 20:21:15 +0000277
Damienb86e3f92013-12-29 17:17:43 +0000278// exception
279extern const mp_obj_type_t exception_type;
280qstr mp_obj_exception_get_type(mp_obj_t self_in);
Damien George136b1492014-01-19 12:38:49 +0000281void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, machine_uint_t line, qstr block);
282void mp_obj_exception_get_traceback(mp_obj_t self_in, machine_uint_t *n, machine_uint_t **values);
Damienb86e3f92013-12-29 17:17:43 +0000283
Damiend99b0522013-12-21 18:17:45 +0000284// str
285extern const mp_obj_type_t str_type;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200286mp_obj_t mp_obj_str_builder_start(const mp_obj_type_t *type, uint len, byte **data);
Damien George5fa93b62014-01-22 14:35:10 +0000287mp_obj_t mp_obj_str_builder_end(mp_obj_t o_in);
288bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2);
289uint mp_obj_str_get_hash(mp_obj_t self_in);
290uint mp_obj_str_get_len(mp_obj_t self_in);
Damien Georgeb829b5c2014-01-25 13:51:19 +0000291qstr mp_obj_str_get_qstr(mp_obj_t self_in); // use this if you will anyway convert the string to a qstr
Damien George5fa93b62014-01-22 14:35:10 +0000292const char *mp_obj_str_get_str(mp_obj_t self_in); // use this only if you need the string to be null terminated
Damien George698ec212014-02-08 18:17:23 +0000293const char *mp_obj_str_get_data(mp_obj_t self_in, uint *len);
Paul Sokolovsky0b7e29c2014-01-28 03:40:06 +0200294void mp_str_print_quoted(void (*print)(void *env, const char *fmt, ...), void *env, const byte *str_data, uint str_len);
Damiend99b0522013-12-21 18:17:45 +0000295
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200296// bytes
297extern const mp_obj_type_t bytes_type;
298
Damiend99b0522013-12-21 18:17:45 +0000299#if MICROPY_ENABLE_FLOAT
300// float
301extern const mp_obj_type_t float_type;
302mp_float_t mp_obj_float_get(mp_obj_t self_in);
Damien Georgee2e3d112014-01-06 22:13:00 +0000303mp_obj_t mp_obj_float_binary_op(int op, mp_float_t lhs_val, mp_obj_t rhs);
Damiend99b0522013-12-21 18:17:45 +0000304
305// complex
306extern const mp_obj_type_t complex_type;
307void mp_obj_complex_get(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
Damien Georgee2e3d112014-01-06 22:13:00 +0000308mp_obj_t mp_obj_complex_binary_op(int op, mp_float_t lhs_real, mp_float_t lhs_imag, mp_obj_t rhs_in);
Damiend99b0522013-12-21 18:17:45 +0000309#endif
310
311// tuple
312extern const mp_obj_type_t tuple_type;
313void mp_obj_tuple_get(mp_obj_t self_in, uint *len, mp_obj_t **items);
John R. Lenton07205ec2014-01-13 02:31:00 +0000314void mp_obj_tuple_del(mp_obj_t self_in);
Damiend99b0522013-12-21 18:17:45 +0000315
316// list
317extern const mp_obj_type_t list_type;
318mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg);
319void mp_obj_list_get(mp_obj_t self_in, uint *len, mp_obj_t **items);
320void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value);
Damien George20006db2014-01-18 14:10:48 +0000321mp_obj_t mp_obj_list_sort(uint n_args, const mp_obj_t *args, struct _mp_map_t *kwargs);
Damiend99b0522013-12-21 18:17:45 +0000322
John R. Lenton39b174e2014-01-15 01:10:09 +0000323// map (the python builtin, not the dict implementation detail)
324extern const mp_obj_type_t map_type;
325
John R. Lenton9daa7892014-01-14 23:55:01 +0000326// enumerate
327extern const mp_obj_type_t enumerate_type;
328
John R. Lentonfca456b2014-01-15 01:37:08 +0000329// filter
330extern const mp_obj_type_t filter_type;
331
Damiend99b0522013-12-21 18:17:45 +0000332// dict
333extern const mp_obj_type_t dict_type;
Damiendae7eb72013-12-29 22:32:51 +0000334uint mp_obj_dict_len(mp_obj_t self_in);
Damiend99b0522013-12-21 18:17:45 +0000335mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value);
Damien George062478e2014-01-09 20:57:50 +0000336struct _mp_map_t *mp_obj_dict_get_map(mp_obj_t self_in);
Damiend99b0522013-12-21 18:17:45 +0000337
338// set
Damien George71c51812014-01-04 20:21:15 +0000339extern const mp_obj_type_t set_type;
Damiend99b0522013-12-21 18:17:45 +0000340void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item);
341
Paul Sokolovsky1c6de112014-01-03 02:41:17 +0200342// slice
343extern const mp_obj_type_t slice_type;
344void mp_obj_slice_get(mp_obj_t self_in, machine_int_t *start, machine_int_t *stop, machine_int_t *step);
345
John R. Lenton07205ec2014-01-13 02:31:00 +0000346// zip
347extern const mp_obj_type_t zip_type;
348
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200349// array
350extern const mp_obj_type_t array_type;
Paul Sokolovsky33996682014-01-21 23:30:10 +0200351uint mp_obj_array_len(mp_obj_t self_in);
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200352mp_obj_t mp_obj_new_bytearray_by_ref(uint n, void *items);
John R. Lenton07205ec2014-01-13 02:31:00 +0000353
Damiend99b0522013-12-21 18:17:45 +0000354// functions
Damien George66028ab2014-01-03 14:03:48 +0000355typedef struct _mp_obj_fun_native_t { // need this so we can define const objects (to go in ROM)
Damiend99b0522013-12-21 18:17:45 +0000356 mp_obj_base_t base;
Paul Sokolovsky0473e272014-02-02 17:49:01 +0200357 struct {
358 bool is_kw : 1;
359 machine_uint_t n_args_min : (8 * sizeof(machine_uint_t) - 1); // inclusive
360 };
Damiend99b0522013-12-21 18:17:45 +0000361 machine_uint_t n_args_max; // inclusive
Damien660365e2013-12-17 18:27:24 +0000362 void *fun;
Damien George66028ab2014-01-03 14:03:48 +0000363 // TODO add mp_map_t *globals
364 // for const function objects, make an empty, const map
365 // such functions won't be able to access the global scope, but that's probably okay
Damiend99b0522013-12-21 18:17:45 +0000366} mp_obj_fun_native_t;
Damien George97209d32014-01-07 15:58:30 +0000367
Damiend99b0522013-12-21 18:17:45 +0000368extern const mp_obj_type_t fun_native_type;
369extern const mp_obj_type_t fun_bc_type;
370void mp_obj_fun_bc_get(mp_obj_t self_in, int *n_args, uint *n_state, const byte **code);
Damien660365e2013-12-17 18:27:24 +0000371
Paul Sokolovskydff3f892014-01-20 18:37:30 +0200372mp_obj_t mp_identity(mp_obj_t self);
Paul Sokolovsky557c9d52014-02-08 21:57:19 +0200373MP_DECLARE_CONST_FUN_OBJ(mp_identity_obj);
Paul Sokolovskydff3f892014-01-20 18:37:30 +0200374
Damien George35e2a4e2014-02-05 00:51:47 +0000375// super
376extern const mp_obj_type_t super_type;
377
Damiend99b0522013-12-21 18:17:45 +0000378// generator
379extern const mp_obj_type_t gen_instance_type;
Damien660365e2013-12-17 18:27:24 +0000380
Damien George28708622014-01-02 21:30:26 +0000381// module
382extern const mp_obj_type_t module_type;
383mp_obj_t mp_obj_new_module(qstr module_name);
Paul Sokolovskyd720ab52014-01-20 00:03:34 +0200384mp_obj_t mp_obj_module_get(qstr module_name);
Damien George28708622014-01-02 21:30:26 +0000385struct _mp_map_t *mp_obj_module_get_globals(mp_obj_t self_in);
Damien Georgeeae16442014-01-11 19:22:29 +0000386
387// staticmethod and classmethod types; defined here so we can make const versions
388
389extern const mp_obj_type_t mp_type_staticmethod;
390extern const mp_obj_type_t mp_type_classmethod;
391
Damien George64131f32014-02-06 20:31:44 +0000392// this structure is used for instances of both staticmethod and classmethod
393typedef struct _mp_obj_static_class_method_t {
Damien Georgeeae16442014-01-11 19:22:29 +0000394 mp_obj_base_t base;
395 mp_obj_t fun;
Damien George64131f32014-02-06 20:31:44 +0000396} mp_obj_static_class_method_t;
Paul Sokolovsky439542f2014-01-21 00:19:19 +0200397
398// sequence helpers
399void mp_seq_multiply(const void *items, uint item_sz, uint len, uint times, void *dest);
Paul Sokolovsky7364af22014-02-02 02:38:22 +0200400bool m_seq_get_fast_slice_indexes(machine_uint_t len, mp_obj_t slice, machine_uint_t *begin, machine_uint_t *end);
Paul Sokolovsky13cfabd2014-02-02 03:32:55 +0200401#define m_seq_copy(dest, src, len, item_sz) memcpy(dest, src, len * sizeof(item_sz))
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200402bool mp_seq_cmp_bytes(int op, const byte *data1, uint len1, const byte *data2, uint len2);