blob: 1b1f82414014a51993826f5986efd7699c78974b [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
Damien George9ddbe292014-12-29 01:02:19 +000026#ifndef __MICROPY_INCLUDED_PY_OBJ_H__
27#define __MICROPY_INCLUDED_PY_OBJ_H__
28
29#include "py/mpconfig.h"
30#include "py/misc.h"
31#include "py/qstr.h"
Damien George04b91472014-05-03 23:27:38 +010032
Damien Georgeea8d06c2014-04-17 23:19:36 +010033// A Micro Python object is a machine word having the following form:
34// - xxxx...xxx1 : a small int, bits 1 and above are the value
35// - xxxx...xx10 : a qstr, bits 2 and above are the value
36// - xxxx...xx00 : a pointer to an mp_obj_base_t (unless a fake object)
37
Damiend99b0522013-12-21 18:17:45 +000038// All Micro Python objects are at least this type
39// It must be of pointer size
40
41typedef machine_ptr_t mp_obj_t;
42typedef machine_const_ptr_t mp_const_obj_t;
43
Damien George97209d32014-01-07 15:58:30 +000044// Anything that wants to be a Micro Python object must have
Damien Georgeea8d06c2014-04-17 23:19:36 +010045// mp_obj_base_t as its first member (except small ints and qstrs)
Damien660365e2013-12-17 18:27:24 +000046
ian-v7a16fad2014-01-06 09:52:29 -080047struct _mp_obj_type_t;
Damiend99b0522013-12-21 18:17:45 +000048struct _mp_obj_base_t {
ian-v7a16fad2014-01-06 09:52:29 -080049 const struct _mp_obj_type_t *type;
Damiend99b0522013-12-21 18:17:45 +000050};
ian-v7a16fad2014-01-06 09:52:29 -080051typedef struct _mp_obj_base_t mp_obj_base_t;
Damiend99b0522013-12-21 18:17:45 +000052
Damien Georgeea8d06c2014-04-17 23:19:36 +010053// These fake objects are used to indicate certain things in arguments or return
54// values, and should only be used when explicitly allowed.
55//
Damien George6ac5dce2014-05-21 19:42:43 +010056// - MP_OBJ_NULL : used to indicate the absence of an object, or unsupported operation.
Damien Georgeea8d06c2014-04-17 23:19:36 +010057// - MP_OBJ_STOP_ITERATION : used instead of throwing a StopIteration, for efficiency.
58// - MP_OBJ_SENTINEL : used for various internal purposes where one needs
59// an object which is unique from all other objects, including MP_OBJ_NULL.
60//
61// For debugging purposes they are all different. For non-debug mode, we alias
62// as many as we can to MP_OBJ_NULL because it's cheaper to load/compare 0.
Damiend99b0522013-12-21 18:17:45 +000063
Damien George8788b132015-01-25 18:35:54 +000064#ifdef NDEBUG
Damien Georgeea8d06c2014-04-17 23:19:36 +010065#define MP_OBJ_NULL ((mp_obj_t)0)
Damien Georgeea8d06c2014-04-17 23:19:36 +010066#define MP_OBJ_STOP_ITERATION ((mp_obj_t)0)
67#define MP_OBJ_SENTINEL ((mp_obj_t)4)
68#else
69#define MP_OBJ_NULL ((mp_obj_t)0)
Damien George6ac5dce2014-05-21 19:42:43 +010070#define MP_OBJ_STOP_ITERATION ((mp_obj_t)4)
71#define MP_OBJ_SENTINEL ((mp_obj_t)8)
Damien Georgeea8d06c2014-04-17 23:19:36 +010072#endif
Damien George729f7b42014-04-17 22:10:53 +010073
Damien George38a2da62014-01-08 17:33:12 +000074// These macros check for small int, qstr or object, and access small int and qstr values
Damiend99b0522013-12-21 18:17:45 +000075
Damien Georgea5c82a82014-04-11 11:16:53 +000076// these macros have now become inline functions; see below
Damien George40f3c022014-07-03 13:25:24 +010077//#define MP_OBJ_IS_SMALL_INT(o) ((((mp_int_t)(o)) & 1) != 0)
78//#define MP_OBJ_IS_QSTR(o) ((((mp_int_t)(o)) & 3) == 2)
79//#define MP_OBJ_IS_OBJ(o) ((((mp_int_t)(o)) & 3) == 0)
Damien George3c658a42014-08-24 16:28:17 +010080#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 int, str or fun; use below macros for that
Damien George11840942014-04-11 22:30:09 +010081#define MP_OBJ_IS_INT(o) (MP_OBJ_IS_SMALL_INT(o) || MP_OBJ_IS_TYPE(o, &mp_type_int))
82#define MP_OBJ_IS_STR(o) (MP_OBJ_IS_QSTR(o) || MP_OBJ_IS_TYPE(o, &mp_type_str))
Damien Georgea75b02e2014-08-27 09:20:30 +010083#define MP_OBJ_IS_STR_OR_BYTES(o) (MP_OBJ_IS_QSTR(o) || (MP_OBJ_IS_OBJ(o) && ((mp_obj_base_t*)(o))->type->binary_op == mp_obj_str_binary_op))
Damien Georgee233a552015-01-11 21:07:15 +000084#define MP_OBJ_IS_FUN(o) (MP_OBJ_IS_OBJ(o) && (((mp_obj_base_t*)(o))->type->name == MP_QSTR_function))
Damien George38a2da62014-01-08 17:33:12 +000085
Damien George40f3c022014-07-03 13:25:24 +010086#define MP_OBJ_SMALL_INT_VALUE(o) (((mp_int_t)(o)) >> 1)
Damien Georgebb4c6f32014-07-31 10:49:14 +010087#define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)((((mp_int_t)(small_int)) << 1) | 1))
Damien George38a2da62014-01-08 17:33:12 +000088
Damien George963a5a32015-01-16 17:47:07 +000089#define MP_OBJ_QSTR_VALUE(o) (((mp_uint_t)(o)) >> 2)
Damien George9ddbe292014-12-29 01:02:19 +000090#define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 2) | 2))
Damiend99b0522013-12-21 18:17:45 +000091
92// These macros are used to declare and define constant function objects
93// You can put "static" in front of the definitions to make them local
94
Damien George3c658a42014-08-24 16:28:17 +010095#define MP_DECLARE_CONST_FUN_OBJ(obj_name) extern const mp_obj_fun_builtin_t obj_name
Damiend99b0522013-12-21 18:17:45 +000096
Damien George3c658a42014-08-24 16:28:17 +010097#define MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, is_kw, n_args_min, n_args_max, fun_name) const mp_obj_fun_builtin_t obj_name = {{&mp_type_fun_builtin}, is_kw, n_args_min, n_args_max, (void *)fun_name}
John R. Lenton270112f2014-01-07 18:01:08 +000098#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)
99#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)
100#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)
101#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)
Damien Georged5e81822014-02-26 17:47:05 +0000102#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, MP_OBJ_FUN_ARGS_MAX, (mp_fun_var_t)fun_name)
John R. Lenton270112f2014-01-07 18:01:08 +0000103#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)
Damien Georged5e81822014-02-26 17:47:05 +0000104#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, MP_OBJ_FUN_ARGS_MAX, (mp_fun_kw_t)fun_name)
John R. Lentonc06763a2014-01-07 17:29:16 +0000105
Damien George78d702c2014-12-09 16:19:48 +0000106// These macros are used to define constant map/dict objects
Damien George9b196cd2014-03-26 21:47:19 +0000107// You can put "static" in front of the definition to make it local
108
Damien George78d702c2014-12-09 16:19:48 +0000109#define MP_DEFINE_CONST_MAP(map_name, table_name) \
110 const mp_map_t map_name = { \
111 .all_keys_are_qstrs = 1, \
112 .table_is_fixed_array = 1, \
113 .used = MP_ARRAY_SIZE(table_name), \
114 .alloc = MP_ARRAY_SIZE(table_name), \
115 .table = (mp_map_elem_t*)table_name, \
116 }
117
Damien George9b196cd2014-03-26 21:47:19 +0000118#define MP_DEFINE_CONST_DICT(dict_name, table_name) \
119 const mp_obj_dict_t dict_name = { \
Damien George3e1a5c12014-03-29 13:43:38 +0000120 .base = {&mp_type_dict}, \
Damien George9b196cd2014-03-26 21:47:19 +0000121 .map = { \
122 .all_keys_are_qstrs = 1, \
123 .table_is_fixed_array = 1, \
Damien George3b603f22014-11-29 14:39:27 +0000124 .used = MP_ARRAY_SIZE(table_name), \
125 .alloc = MP_ARRAY_SIZE(table_name), \
Damien George9b196cd2014-03-26 21:47:19 +0000126 .table = (mp_map_elem_t*)table_name, \
127 }, \
128 }
129
Damien Georgeeae16442014-01-11 19:22:29 +0000130// These macros are used to declare and define constant staticmethond and classmethod objects
131// You can put "static" in front of the definitions to make them local
132
Damien George64131f32014-02-06 20:31:44 +0000133#define MP_DECLARE_CONST_STATICMETHOD_OBJ(obj_name) extern const mp_obj_static_class_method_t obj_name
134#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 +0000135
Damien George64131f32014-02-06 20:31:44 +0000136#define MP_DEFINE_CONST_STATICMETHOD_OBJ(obj_name, fun_name) const mp_obj_static_class_method_t obj_name = {{&mp_type_staticmethod}, fun_name}
137#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 +0000138
Damien Georgedf6567e2014-03-30 13:54:02 +0100139// Underlying map/hash table implementation (not dict object or map function)
140
141typedef struct _mp_map_elem_t {
142 mp_obj_t key;
143 mp_obj_t value;
144} mp_map_elem_t;
145
146// TODO maybe have a truncated mp_map_t for fixed tables, since alloc=used
147// put alloc last in the structure, so the truncated version does not need it
148// this would save 1 ROM word for all ROM objects that have a locals_dict
149// would also need a trucated dict structure
150
151typedef struct _mp_map_t {
Damien George40f3c022014-07-03 13:25:24 +0100152 mp_uint_t all_keys_are_qstrs : 1;
153 mp_uint_t table_is_fixed_array : 1;
154 mp_uint_t used : (8 * sizeof(mp_uint_t) - 2);
155 mp_uint_t alloc;
Damien Georgedf6567e2014-03-30 13:54:02 +0100156 mp_map_elem_t *table;
157} mp_map_t;
158
Damien George95004e52014-04-05 17:17:19 +0100159// These can be or'd together
Damien Georgedf6567e2014-03-30 13:54:02 +0100160typedef enum _mp_map_lookup_kind_t {
161 MP_MAP_LOOKUP, // 0
162 MP_MAP_LOOKUP_ADD_IF_NOT_FOUND, // 1
163 MP_MAP_LOOKUP_REMOVE_IF_FOUND, // 2
Damien Georgedf6567e2014-03-30 13:54:02 +0100164} mp_map_lookup_kind_t;
165
Paul Sokolovskye5dbe1e2014-11-26 21:17:16 +0200166extern const mp_map_t mp_const_empty_map;
167
Damien George40f3c022014-07-03 13:25:24 +0100168static inline bool MP_MAP_SLOT_IS_FILLED(const mp_map_t *map, mp_uint_t pos) { return ((map)->table[pos].key != MP_OBJ_NULL && (map)->table[pos].key != MP_OBJ_SENTINEL); }
Damien George8b0535e2014-04-05 21:53:54 +0100169
Damien George93965e72014-08-30 13:23:35 +0100170void mp_map_init(mp_map_t *map, mp_uint_t n);
171void mp_map_init_fixed_table(mp_map_t *map, mp_uint_t n, const mp_obj_t *table);
172mp_map_t *mp_map_new(mp_uint_t n);
Damien Georgedf6567e2014-03-30 13:54:02 +0100173void mp_map_deinit(mp_map_t *map);
174void mp_map_free(mp_map_t *map);
175mp_map_elem_t* mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t lookup_kind);
176void mp_map_clear(mp_map_t *map);
Paul Sokolovskye3f58c82014-04-05 04:14:22 +0300177void mp_map_dump(mp_map_t *map);
Damien Georgedf6567e2014-03-30 13:54:02 +0100178
179// Underlying set implementation (not set object)
180
181typedef struct _mp_set_t {
Damien George40f3c022014-07-03 13:25:24 +0100182 mp_uint_t alloc;
183 mp_uint_t used;
Damien Georgedf6567e2014-03-30 13:54:02 +0100184 mp_obj_t *table;
185} mp_set_t;
186
Damien George40f3c022014-07-03 13:25:24 +0100187static inline bool MP_SET_SLOT_IS_FILLED(const mp_set_t *set, mp_uint_t pos) { return ((set)->table[pos] != MP_OBJ_NULL && (set)->table[pos] != MP_OBJ_SENTINEL); }
Damien George8b0535e2014-04-05 21:53:54 +0100188
Damien George93965e72014-08-30 13:23:35 +0100189void mp_set_init(mp_set_t *set, mp_uint_t n);
Damien Georgedf6567e2014-03-30 13:54:02 +0100190mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind);
Damien George95004e52014-04-05 17:17:19 +0100191mp_obj_t mp_set_remove_first(mp_set_t *set);
Damien Georgedf6567e2014-03-30 13:54:02 +0100192void mp_set_clear(mp_set_t *set);
Damiend99b0522013-12-21 18:17:45 +0000193
194// Type definitions for methods
195
196typedef mp_obj_t (*mp_fun_0_t)(void);
197typedef mp_obj_t (*mp_fun_1_t)(mp_obj_t);
198typedef mp_obj_t (*mp_fun_2_t)(mp_obj_t, mp_obj_t);
John R. Lenton45a87442014-01-04 01:15:01 +0000199typedef mp_obj_t (*mp_fun_3_t)(mp_obj_t, mp_obj_t, mp_obj_t);
Damien Georgeecc88e92014-08-30 00:35:11 +0100200typedef mp_obj_t (*mp_fun_var_t)(mp_uint_t n, const mp_obj_t *);
201typedef mp_obj_t (*mp_fun_kw_t)(mp_uint_t n, const mp_obj_t *, mp_map_t *);
Damiend99b0522013-12-21 18:17:45 +0000202
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200203typedef enum {
Paul Sokolovskyd8351ca2014-05-02 01:51:25 +0300204 PRINT_STR = 0,
205 PRINT_REPR = 1,
206 PRINT_EXC = 2, // Special format for printing exception in unhandled exception message
Damien George612045f2014-09-17 22:56:34 +0100207 PRINT_JSON = 3,
208 PRINT_EXC_SUBCLASS = 0x80, // Internal flag for printing exception subclasses
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200209} mp_print_kind_t;
210
211typedef void (*mp_print_fun_t)(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o, mp_print_kind_t kind);
Damien Georgeecc88e92014-08-30 00:35:11 +0100212typedef mp_obj_t (*mp_make_new_fun_t)(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args);
213typedef mp_obj_t (*mp_call_fun_t)(mp_obj_t fun, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args);
214typedef mp_obj_t (*mp_unary_op_fun_t)(mp_uint_t op, mp_obj_t);
215typedef mp_obj_t (*mp_binary_op_fun_t)(mp_uint_t op, mp_obj_t, mp_obj_t);
Damien George20006db2014-01-18 14:10:48 +0000216typedef 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 Georgef4c9b332014-04-08 21:32:29 +0100217typedef bool (*mp_store_attr_fun_t)(mp_obj_t self_in, qstr attr, mp_obj_t value); // return true if store succeeded; if value==MP_OBJ_NULL then delete
Damien George729f7b42014-04-17 22:10:53 +0100218typedef mp_obj_t (*mp_subscr_fun_t)(mp_obj_t self_in, mp_obj_t index, mp_obj_t value);
Damiend99b0522013-12-21 18:17:45 +0000219
220typedef struct _mp_method_t {
Damien Georgec12b2212014-03-26 20:15:40 +0000221 qstr name;
Damiend99b0522013-12-21 18:17:45 +0000222 mp_const_obj_t fun;
223} mp_method_t;
224
Paul Sokolovsky5b15daf2014-01-07 20:12:26 +0200225// Buffer protocol
Damien George57a4b4f2014-04-18 22:29:21 +0100226typedef struct _mp_buffer_info_t {
Paul Sokolovsky5b15daf2014-01-07 20:12:26 +0200227 // if we'd bother to support various versions of structure
228 // (with different number of fields), we can distinguish
229 // them with ver = sizeof(struct). Cons: overkill for *micro*?
230 //int ver; // ?
231
Damien George42f3de92014-10-03 17:44:14 +0000232 void *buf; // can be NULL if len == 0
233 mp_uint_t len; // in bytes
234 int typecode; // as per binary.h
Paul Sokolovsky5b15daf2014-01-07 20:12:26 +0200235
236 // Rationale: to load arbitrary-sized sprites directly to LCD
237 // Cons: a bit adhoc usecase
238 // int stride;
Damien George57a4b4f2014-04-18 22:29:21 +0100239} mp_buffer_info_t;
240#define MP_BUFFER_READ (1)
241#define MP_BUFFER_WRITE (2)
242#define MP_BUFFER_RW (MP_BUFFER_READ | MP_BUFFER_WRITE)
Paul Sokolovsky5b15daf2014-01-07 20:12:26 +0200243typedef struct _mp_buffer_p_t {
Damien George4d917232014-08-30 14:28:06 +0100244 mp_int_t (*get_buffer)(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
Paul Sokolovsky5b15daf2014-01-07 20:12:26 +0200245} mp_buffer_p_t;
Damien George4d917232014-08-30 14:28:06 +0100246bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
247void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
Paul Sokolovsky5b15daf2014-01-07 20:12:26 +0200248
249// Stream protocol
Damien George963a5a32015-01-16 17:47:07 +0000250#define MP_STREAM_ERROR ((mp_uint_t)-1)
Paul Sokolovsky5b15daf2014-01-07 20:12:26 +0200251typedef struct _mp_stream_p_t {
Damien Georgeadf0f2a2014-07-27 22:38:58 +0100252 // On error, functions should return MP_STREAM_ERROR and fill in *errcode (values
253 // are implementation-dependent, but will be exposed to user, e.g. via exception).
254 mp_uint_t (*read)(mp_obj_t obj, void *buf, mp_uint_t size, int *errcode);
255 mp_uint_t (*write)(mp_obj_t obj, const void *buf, mp_uint_t size, int *errcode);
Paul Sokolovskyf4a6a572014-11-17 00:16:14 +0200256 mp_uint_t (*ioctl)(mp_obj_t obj, mp_uint_t request, mp_uint_t arg, int *errcode);
Damien George4d917232014-08-30 14:28:06 +0100257 mp_uint_t is_text : 1; // default is bytes, set this for text stream
Paul Sokolovsky5b15daf2014-01-07 20:12:26 +0200258} mp_stream_p_t;
259
Paul Sokolovsky838eb1f2014-11-17 00:16:14 +0200260// Stream ioctl request codes
261#define MP_STREAM_FLUSH (1)
262#define MP_STREAM_SEEK (2)
263#define MP_STREAM_POLL (3)
264
265// Argument structure for MP_STREAM_SEEK
266struct mp_stream_seek_t {
267 mp_off_t offset;
268 int whence;
269};
270
Damiend99b0522013-12-21 18:17:45 +0000271struct _mp_obj_type_t {
272 mp_obj_base_t base;
Damien Georgea71c83a2014-02-15 11:34:50 +0000273 qstr name;
Damiend99b0522013-12-21 18:17:45 +0000274 mp_print_fun_t print;
Damien George71c51812014-01-04 20:21:15 +0000275 mp_make_new_fun_t make_new; // to make an instance of the type
Damiend99b0522013-12-21 18:17:45 +0000276
Damien George20006db2014-01-18 14:10:48 +0000277 mp_call_fun_t call;
Damien George6ac5dce2014-05-21 19:42:43 +0100278 mp_unary_op_fun_t unary_op; // can return MP_OBJ_NULL if op not supported
279 mp_binary_op_fun_t binary_op; // can return MP_OBJ_NULL if op not supported
Damiend99b0522013-12-21 18:17:45 +0000280
Damien Georgea71c83a2014-02-15 11:34:50 +0000281 mp_load_attr_fun_t load_attr;
Damien George1d24ea52014-04-08 21:11:49 +0100282 mp_store_attr_fun_t store_attr; // if value is MP_OBJ_NULL, then delete that attribute
Damien Georgef4c9b332014-04-08 21:32:29 +0100283
Damien George729f7b42014-04-17 22:10:53 +0100284 mp_subscr_fun_t subscr; // implements load, store, delete subscripting
285 // value=MP_OBJ_NULL means delete, value=MP_OBJ_SENTINEL means load, else store
Damien George6ac5dce2014-05-21 19:42:43 +0100286 // can return MP_OBJ_NULL if op not supported
Damien Georgea71c83a2014-02-15 11:34:50 +0000287
Damiend99b0522013-12-21 18:17:45 +0000288 mp_fun_1_t getiter;
Damien Georged0a5bf32014-05-10 13:55:11 +0100289 mp_fun_1_t iternext; // may return MP_OBJ_STOP_ITERATION as an optimisation instead of raising StopIteration() (with no args)
Damiend99b0522013-12-21 18:17:45 +0000290
Paul Sokolovsky5b15daf2014-01-07 20:12:26 +0200291 mp_buffer_p_t buffer_p;
Damien George27e735f2014-04-05 23:02:23 +0100292 const mp_stream_p_t *stream_p;
Paul Sokolovsky5b15daf2014-01-07 20:12:26 +0200293
Damien George004cdce2014-01-09 21:43:51 +0000294 // these are for dynamically created types (classes)
295 mp_obj_t bases_tuple;
296 mp_obj_t locals_dict;
Damien George062478e2014-01-09 20:57:50 +0000297
Damiend99b0522013-12-21 18:17:45 +0000298 /*
299 What we might need to add here:
300
Damiend99b0522013-12-21 18:17:45 +0000301 len str tuple list map
302 abs float complex
303 hash bool int none str
304 equal int str
Damiend99b0522013-12-21 18:17:45 +0000305
306 unpack seq list tuple
Damiend99b0522013-12-21 18:17:45 +0000307 */
308};
309
ian-v7a16fad2014-01-06 09:52:29 -0800310typedef struct _mp_obj_type_t mp_obj_type_t;
Damiend99b0522013-12-21 18:17:45 +0000311
Damien Georgec5966122014-02-15 16:10:44 +0000312// Constant types, globally accessible
Damien Georgec5966122014-02-15 16:10:44 +0000313extern const mp_obj_type_t mp_type_type;
Damien George3e1a5c12014-03-29 13:43:38 +0000314extern const mp_obj_type_t mp_type_object;
315extern const mp_obj_type_t mp_type_NoneType;
316extern const mp_obj_type_t mp_type_bool;
317extern const mp_obj_type_t mp_type_int;
318extern const mp_obj_type_t mp_type_str;
319extern const mp_obj_type_t mp_type_bytes;
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300320extern const mp_obj_type_t mp_type_bytearray;
Damien Georgedd4f4532014-10-23 13:34:35 +0100321extern const mp_obj_type_t mp_type_memoryview;
Damien George3e1a5c12014-03-29 13:43:38 +0000322extern const mp_obj_type_t mp_type_float;
323extern const mp_obj_type_t mp_type_complex;
324extern const mp_obj_type_t mp_type_tuple;
325extern const mp_obj_type_t mp_type_list;
326extern const mp_obj_type_t mp_type_map; // map (the python builtin, not the dict implementation detail)
327extern const mp_obj_type_t mp_type_enumerate;
328extern const mp_obj_type_t mp_type_filter;
329extern const mp_obj_type_t mp_type_dict;
Damien George71d31122014-04-17 18:18:55 +0100330extern const mp_obj_type_t mp_type_range;
Damien George3e1a5c12014-03-29 13:43:38 +0000331extern const mp_obj_type_t mp_type_set;
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300332extern const mp_obj_type_t mp_type_frozenset;
Damien George3e1a5c12014-03-29 13:43:38 +0000333extern const mp_obj_type_t mp_type_slice;
334extern const mp_obj_type_t mp_type_zip;
335extern const mp_obj_type_t mp_type_array;
336extern const mp_obj_type_t mp_type_super;
337extern const mp_obj_type_t mp_type_gen_instance;
Damien George3c658a42014-08-24 16:28:17 +0100338extern const mp_obj_type_t mp_type_fun_builtin;
Damien George3e1a5c12014-03-29 13:43:38 +0000339extern const mp_obj_type_t mp_type_fun_bc;
340extern const mp_obj_type_t mp_type_module;
341extern const mp_obj_type_t mp_type_staticmethod;
342extern const mp_obj_type_t mp_type_classmethod;
Damien George777b0f32014-04-13 18:59:45 +0100343extern const mp_obj_type_t mp_type_property;
Paul Sokolovskycb9dc082014-04-26 20:26:14 +0300344extern const mp_obj_type_t mp_type_stringio;
Paul Sokolovskya47b64a2014-05-15 07:28:19 +0300345extern const mp_obj_type_t mp_type_bytesio;
Damien George4c03b3a2014-08-12 18:33:40 +0100346extern const mp_obj_type_t mp_type_reversed;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000347
348// Exceptions
Damien Georgec5966122014-02-15 16:10:44 +0000349extern const mp_obj_type_t mp_type_BaseException;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000350extern const mp_obj_type_t mp_type_ArithmeticError;
351extern const mp_obj_type_t mp_type_AssertionError;
352extern const mp_obj_type_t mp_type_AttributeError;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000353extern const mp_obj_type_t mp_type_EOFError;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000354extern const mp_obj_type_t mp_type_Exception;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000355extern const mp_obj_type_t mp_type_GeneratorExit;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000356extern const mp_obj_type_t mp_type_ImportError;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000357extern const mp_obj_type_t mp_type_IndentationError;
358extern const mp_obj_type_t mp_type_IndexError;
Damien George124df6f2014-10-25 18:19:55 +0100359extern const mp_obj_type_t mp_type_KeyboardInterrupt;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000360extern const mp_obj_type_t mp_type_KeyError;
361extern const mp_obj_type_t mp_type_LookupError;
362extern const mp_obj_type_t mp_type_MemoryError;
363extern const mp_obj_type_t mp_type_NameError;
364extern const mp_obj_type_t mp_type_NotImplementedError;
365extern const mp_obj_type_t mp_type_OSError;
366extern const mp_obj_type_t mp_type_OverflowError;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000367extern const mp_obj_type_t mp_type_RuntimeError;
Damien Georgeffb5cfc2014-03-25 14:29:40 +0000368extern const mp_obj_type_t mp_type_StopIteration;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000369extern const mp_obj_type_t mp_type_SyntaxError;
Damien George7a4ddd22014-05-24 23:32:19 +0100370extern const mp_obj_type_t mp_type_SystemExit;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000371extern const mp_obj_type_t mp_type_TypeError;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000372extern const mp_obj_type_t mp_type_ValueError;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000373extern const mp_obj_type_t mp_type_ZeroDivisionError;
374
Damiend99b0522013-12-21 18:17:45 +0000375// Constant objects, globally accessible
Damien George07ddab52014-03-29 13:15:08 +0000376// The macros are for convenience only
377#define mp_const_none ((mp_obj_t)&mp_const_none_obj)
378#define mp_const_false ((mp_obj_t)&mp_const_false_obj)
379#define mp_const_true ((mp_obj_t)&mp_const_true_obj)
Damien George20f59e12014-10-11 17:56:43 +0100380#define mp_const_empty_bytes ((mp_obj_t)&mp_const_empty_bytes_obj)
Damien George07ddab52014-03-29 13:15:08 +0000381#define mp_const_empty_tuple ((mp_obj_t)&mp_const_empty_tuple_obj)
382extern const struct _mp_obj_none_t mp_const_none_obj;
383extern const struct _mp_obj_bool_t mp_const_false_obj;
384extern const struct _mp_obj_bool_t mp_const_true_obj;
Damien George20f59e12014-10-11 17:56:43 +0100385extern const struct _mp_obj_str_t mp_const_empty_bytes_obj;
Damien George07ddab52014-03-29 13:15:08 +0000386extern const struct _mp_obj_tuple_t mp_const_empty_tuple_obj;
387extern const struct _mp_obj_ellipsis_t mp_const_ellipsis_obj;
Damien George6902eed2014-04-04 10:52:59 +0000388extern const struct _mp_obj_exception_t mp_const_MemoryError_obj;
Damien George07ddab52014-03-29 13:15:08 +0000389extern const struct _mp_obj_exception_t mp_const_GeneratorExit_obj;
Damiend99b0522013-12-21 18:17:45 +0000390
Damiend99b0522013-12-21 18:17:45 +0000391// General API for objects
392
Damien Georgea71c83a2014-02-15 11:34:50 +0000393mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict);
Damiend99b0522013-12-21 18:17:45 +0000394mp_obj_t mp_obj_new_none(void);
395mp_obj_t mp_obj_new_bool(bool value);
Damien George6baf76e2013-12-30 22:32:17 +0000396mp_obj_t mp_obj_new_cell(mp_obj_t obj);
Damien George40f3c022014-07-03 13:25:24 +0100397mp_obj_t mp_obj_new_int(mp_int_t value);
398mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value);
Damien Georged182b982014-08-30 14:19:41 +0100399mp_obj_t mp_obj_new_int_from_str_len(const char **str, mp_uint_t len, bool neg, mp_uint_t base);
Damien George9d68e9c2014-03-12 15:38:15 +0000400mp_obj_t mp_obj_new_int_from_ll(long long val); // this must return a multi-precision integer object (or raise an overflow exception)
Damien George95307432014-09-10 22:10:33 +0100401mp_obj_t mp_obj_new_int_from_ull(unsigned long long val); // this must return a multi-precision integer object (or raise an overflow exception)
Damien Georged182b982014-08-30 14:19:41 +0100402mp_obj_t mp_obj_new_str(const char* data, mp_uint_t len, bool make_qstr_if_not_already);
Damien George0b9ee862015-01-21 19:14:25 +0000403mp_obj_t mp_obj_new_str_from_vstr(const mp_obj_type_t *type, vstr_t *vstr);
Damien Georged182b982014-08-30 14:19:41 +0100404mp_obj_t mp_obj_new_bytes(const byte* data, mp_uint_t len);
Damien George5318cc02014-12-10 22:37:07 +0000405mp_obj_t mp_obj_new_bytearray(mp_uint_t n, void *items);
406mp_obj_t mp_obj_new_bytearray_by_ref(mp_uint_t n, void *items);
Damien Georgefb510b32014-06-01 13:32:54 +0100407#if MICROPY_PY_BUILTINS_FLOAT
Paul Sokolovsky5f680942014-12-30 00:34:54 +0200408mp_obj_t mp_obj_new_int_from_float(mp_float_t val);
Damiend99b0522013-12-21 18:17:45 +0000409mp_obj_t mp_obj_new_float(mp_float_t val);
410mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag);
411#endif
Damien Georgec5966122014-02-15 16:10:44 +0000412mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type);
Paul Sokolovskydec31bb2014-04-22 00:01:13 +0300413mp_obj_t mp_obj_new_exception_arg1(const mp_obj_type_t *exc_type, mp_obj_t arg);
Damien Georged182b982014-08-30 14:19:41 +0100414mp_obj_t mp_obj_new_exception_args(const mp_obj_type_t *exc_type, mp_uint_t n_args, const mp_obj_t *args);
Damien Georgec5966122014-02-15 16:10:44 +0000415mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, const char *msg);
416mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...); // counts args by number of % symbols in fmt, excluding %%; can only handle void* sizes (ie no float/double!)
Damien George1084b0f2014-10-25 15:07:02 +0100417mp_obj_t mp_obj_new_fun_bc(mp_uint_t scope_flags, mp_uint_t n_pos_args, mp_uint_t n_kwonly_args, mp_obj_t def_args, mp_obj_t def_kw_args, const byte *code);
Damien George3c658a42014-08-24 16:28:17 +0100418mp_obj_t mp_obj_new_fun_native(mp_uint_t n_args, void *fun_data);
419mp_obj_t mp_obj_new_fun_viper(mp_uint_t n_args, void *fun_data, mp_uint_t type_sig);
420mp_obj_t mp_obj_new_fun_asm(mp_uint_t n_args, void *fun_data);
Damien Georged0691cc2014-01-29 20:30:52 +0000421mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun);
Damien Georged182b982014-08-30 14:19:41 +0100422mp_obj_t mp_obj_new_closure(mp_obj_t fun, mp_uint_t n_closed, const mp_obj_t *closed);
Damien George9c4cbe22014-08-30 14:04:14 +0100423mp_obj_t mp_obj_new_tuple(mp_uint_t n, const mp_obj_t *items);
424mp_obj_t mp_obj_new_list(mp_uint_t n, mp_obj_t *items);
Damien George93965e72014-08-30 13:23:35 +0100425mp_obj_t mp_obj_new_dict(mp_uint_t n_args);
426mp_obj_t mp_obj_new_set(mp_uint_t n_args, mp_obj_t *items);
Paul Sokolovsky1c6de112014-01-03 02:41:17 +0200427mp_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 +0000428mp_obj_t mp_obj_new_super(mp_obj_t type, mp_obj_t obj);
Damien George20006db2014-01-18 14:10:48 +0000429mp_obj_t mp_obj_new_bound_meth(mp_obj_t meth, mp_obj_t self);
Damien George7c9c6672014-01-25 00:17:36 +0000430mp_obj_t mp_obj_new_getitem_iter(mp_obj_t *args);
Damien George28708622014-01-02 21:30:26 +0000431mp_obj_t mp_obj_new_module(qstr module_name);
Damiend99b0522013-12-21 18:17:45 +0000432
Paul Sokolovsky7aca1ca2014-05-11 02:26:42 +0300433mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in);
434const char *mp_obj_get_type_str(mp_const_obj_t o_in);
Damien George9e6e9352014-03-26 18:37:06 +0000435bool mp_obj_is_subclass_fast(mp_const_obj_t object, mp_const_obj_t classinfo); // arguments should be type objects
Paul Sokolovskyea970802014-05-11 03:16:04 +0300436mp_obj_t mp_instance_cast_to_native_base(mp_const_obj_t self_in, mp_const_obj_t native_type);
Damiend99b0522013-12-21 18:17:45 +0000437
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200438void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind);
439void mp_obj_print(mp_obj_t o, mp_print_kind_t kind);
Paul Sokolovsky46c3ab22014-12-06 14:29:09 +0200440void mp_obj_print_exception(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t exc);
Damiend99b0522013-12-21 18:17:45 +0000441
Damien George4d917232014-08-30 14:28:06 +0100442bool mp_obj_is_true(mp_obj_t arg);
Damien Georgea5c82a82014-04-11 11:16:53 +0000443
444// TODO make these all lower case when they have proven themselves
Damien George40f3c022014-07-03 13:25:24 +0100445static inline bool MP_OBJ_IS_OBJ(mp_const_obj_t o) { return ((((mp_int_t)(o)) & 3) == 0); }
446static inline bool MP_OBJ_IS_SMALL_INT(mp_const_obj_t o) { return ((((mp_int_t)(o)) & 1) != 0); }
Damien Georgedb049c22014-04-12 00:08:40 +0100447//static inline bool MP_OBJ_IS_TYPE(mp_const_obj_t o, const mp_obj_type_t *t) { return (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
448//static inline bool MP_OBJ_IS_INT(mp_const_obj_t o) { return (MP_OBJ_IS_SMALL_INT(o) || MP_OBJ_IS_TYPE(o, &mp_type_int)); } // returns true if o is a small int or long int
449static inline bool mp_obj_is_integer(mp_const_obj_t o) { return MP_OBJ_IS_INT(o) || MP_OBJ_IS_TYPE(o, &mp_type_bool); } // returns true if o is bool, small int or long int
Damien George40f3c022014-07-03 13:25:24 +0100450static inline bool MP_OBJ_IS_QSTR(mp_const_obj_t o) { return ((((mp_int_t)(o)) & 3) == 2); }
Damien Georgedb049c22014-04-12 00:08:40 +0100451//static inline bool MP_OBJ_IS_STR(mp_const_obj_t o) { return (MP_OBJ_IS_QSTR(o) || MP_OBJ_IS_TYPE(o, &mp_type_str)); }
Damien Georgea5c82a82014-04-11 11:16:53 +0000452
Damiend99b0522013-12-21 18:17:45 +0000453bool mp_obj_is_callable(mp_obj_t o_in);
Damien George40f3c022014-07-03 13:25:24 +0100454mp_int_t mp_obj_hash(mp_obj_t o_in);
Damiend99b0522013-12-21 18:17:45 +0000455bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2);
Damiend99b0522013-12-21 18:17:45 +0000456
Damien George40f3c022014-07-03 13:25:24 +0100457mp_int_t mp_obj_get_int(mp_const_obj_t arg);
458bool mp_obj_get_int_maybe(mp_const_obj_t arg, mp_int_t *value);
Damien Georgefb510b32014-06-01 13:32:54 +0100459#if MICROPY_PY_BUILTINS_FLOAT
Damiend99b0522013-12-21 18:17:45 +0000460mp_float_t mp_obj_get_float(mp_obj_t self_in);
461void mp_obj_get_complex(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
462#endif
Damien George5fa93b62014-01-22 14:35:10 +0000463//qstr mp_obj_get_qstr(mp_obj_t arg);
Damien George9c4cbe22014-08-30 14:04:14 +0100464void mp_obj_get_array(mp_obj_t o, mp_uint_t *len, mp_obj_t **items);
465void mp_obj_get_array_fixed_n(mp_obj_t o, mp_uint_t len, mp_obj_t **items);
466mp_uint_t mp_get_index(const mp_obj_type_t *type, mp_uint_t len, mp_obj_t index, bool is_slice);
Damien Georgec7687ad2014-08-22 21:48:30 +0100467mp_obj_t mp_obj_id(mp_obj_t o_in);
Damien George4c03b3a2014-08-12 18:33:40 +0100468mp_obj_t mp_obj_len(mp_obj_t o_in);
Damien Georgea5c82a82014-04-11 11:16:53 +0000469mp_obj_t mp_obj_len_maybe(mp_obj_t o_in); /* may return MP_OBJ_NULL */
Damien George729f7b42014-04-17 22:10:53 +0100470mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t val);
Damiend99b0522013-12-21 18:17:45 +0000471
Damiend99b0522013-12-21 18:17:45 +0000472// bool
Damien Georgea5c82a82014-04-11 11:16:53 +0000473// TODO make lower case when it has proven itself
Damien George40f3c022014-07-03 13:25:24 +0100474static inline mp_obj_t MP_BOOL(mp_int_t x) { return x ? mp_const_true : mp_const_false; }
Damiend99b0522013-12-21 18:17:45 +0000475
476// cell
477mp_obj_t mp_obj_cell_get(mp_obj_t self_in);
478void mp_obj_cell_set(mp_obj_t self_in, mp_obj_t obj);
479
Damien George71c51812014-01-04 20:21:15 +0000480// int
Damien George40f3c022014-07-03 13:25:24 +0100481// For long int, returns value truncated to mp_int_t
Damien Georgebe6d8be2014-12-05 23:13:52 +0000482mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in);
483// Will raise exception if value doesn't fit into mp_int_t
484mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in);
Damien Georgefb510b32014-06-01 13:32:54 +0100485#if MICROPY_PY_BUILTINS_FLOAT
Damien Georgeeabdf672014-03-22 20:54:01 +0000486mp_float_t mp_obj_int_as_float(mp_obj_t self_in);
487#endif
Damien George71c51812014-01-04 20:21:15 +0000488
Damienb86e3f92013-12-29 17:17:43 +0000489// exception
Paul Sokolovskyd8351ca2014-05-02 01:51:25 +0300490#define mp_obj_is_native_exception_instance(o) (mp_obj_get_type(o)->make_new == mp_obj_exception_make_new)
Damien Georgec5966122014-02-15 16:10:44 +0000491bool mp_obj_is_exception_type(mp_obj_t self_in);
492bool mp_obj_is_exception_instance(mp_obj_t self_in);
Damien George4bcd04b2014-09-24 14:05:40 +0100493bool mp_obj_exception_match(mp_obj_t exc, mp_const_obj_t exc_type);
Damien Georgec5966122014-02-15 16:10:44 +0000494void mp_obj_exception_clear_traceback(mp_obj_t self_in);
Damien George40f3c022014-07-03 13:25:24 +0100495void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, mp_uint_t line, qstr block);
496void mp_obj_exception_get_traceback(mp_obj_t self_in, mp_uint_t *n, mp_uint_t **values);
Paul Sokolovskyaf1ae302014-03-26 19:17:20 +0200497mp_obj_t mp_obj_exception_get_value(mp_obj_t self_in);
Damien Georgeecc88e92014-08-30 00:35:11 +0100498mp_obj_t mp_obj_exception_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args);
Dave Hylands5b7fd202014-07-01 23:46:53 -0700499mp_obj_t mp_alloc_emergency_exception_buf(mp_obj_t size_in);
500void mp_init_emergency_exception_buf(void);
Damienb86e3f92013-12-29 17:17:43 +0000501
Damiend99b0522013-12-21 18:17:45 +0000502// str
Damien George5fa93b62014-01-22 14:35:10 +0000503bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2);
Damien Georged182b982014-08-30 14:19:41 +0100504mp_uint_t mp_obj_str_get_hash(mp_obj_t self_in);
505mp_uint_t mp_obj_str_get_len(mp_obj_t self_in);
Damien Georgeb829b5c2014-01-25 13:51:19 +0000506qstr 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 +0000507const char *mp_obj_str_get_str(mp_obj_t self_in); // use this only if you need the string to be null terminated
Damien Georged182b982014-08-30 14:19:41 +0100508const char *mp_obj_str_get_data(mp_obj_t self_in, mp_uint_t *len);
Paul Sokolovskyb4efac12014-06-08 01:13:35 +0300509mp_obj_t mp_obj_str_intern(mp_obj_t str);
Damien Georged182b982014-08-30 14:19:41 +0100510void mp_str_print_quoted(void (*print)(void *env, const char *fmt, ...), void *env, const byte *str_data, mp_uint_t str_len, bool is_bytes);
Damiend99b0522013-12-21 18:17:45 +0000511
Damien Georgefb510b32014-06-01 13:32:54 +0100512#if MICROPY_PY_BUILTINS_FLOAT
Damiend99b0522013-12-21 18:17:45 +0000513// float
Damien George0c36da02014-03-08 15:24:39 +0000514typedef struct _mp_obj_float_t {
515 mp_obj_base_t base;
516 mp_float_t value;
517} mp_obj_float_t;
Damiend99b0522013-12-21 18:17:45 +0000518mp_float_t mp_obj_float_get(mp_obj_t self_in);
Damien Georgeecc88e92014-08-30 00:35:11 +0100519mp_obj_t mp_obj_float_binary_op(mp_uint_t op, mp_float_t lhs_val, mp_obj_t rhs); // can return MP_OBJ_NULL if op not supported
Damien George8594ce22014-09-13 18:43:09 +0100520void mp_obj_float_divmod(mp_float_t *x, mp_float_t *y);
Damiend99b0522013-12-21 18:17:45 +0000521
522// complex
Damiend99b0522013-12-21 18:17:45 +0000523void mp_obj_complex_get(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
Damien Georgeecc88e92014-08-30 00:35:11 +0100524mp_obj_t mp_obj_complex_binary_op(mp_uint_t op, mp_float_t lhs_real, mp_float_t lhs_imag, mp_obj_t rhs_in); // can return MP_OBJ_NULL if op not supported
Damiend99b0522013-12-21 18:17:45 +0000525#endif
526
527// tuple
Damien George9c4cbe22014-08-30 14:04:14 +0100528void mp_obj_tuple_get(mp_obj_t self_in, mp_uint_t *len, mp_obj_t **items);
John R. Lenton07205ec2014-01-13 02:31:00 +0000529void mp_obj_tuple_del(mp_obj_t self_in);
Damien George40f3c022014-07-03 13:25:24 +0100530mp_int_t mp_obj_tuple_hash(mp_obj_t self_in);
Damiend99b0522013-12-21 18:17:45 +0000531
532// list
Paul Sokolovsky18bef252014-04-13 06:17:29 +0300533struct _mp_obj_list_t;
Damien George9c4cbe22014-08-30 14:04:14 +0100534void mp_obj_list_init(struct _mp_obj_list_t *o, mp_uint_t n);
Damiend99b0522013-12-21 18:17:45 +0000535mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg);
Damien George9c4cbe22014-08-30 14:04:14 +0100536void mp_obj_list_get(mp_obj_t self_in, mp_uint_t *len, mp_obj_t **items);
537void mp_obj_list_set_len(mp_obj_t self_in, mp_uint_t len);
Damiend99b0522013-12-21 18:17:45 +0000538void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value);
Damien Georgeecc88e92014-08-30 00:35:11 +0100539mp_obj_t mp_obj_list_sort(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs);
Damiend99b0522013-12-21 18:17:45 +0000540
541// dict
Damien Georgedf6567e2014-03-30 13:54:02 +0100542typedef struct _mp_obj_dict_t {
543 mp_obj_base_t base;
544 mp_map_t map;
545} mp_obj_dict_t;
Damien George93965e72014-08-30 13:23:35 +0100546void mp_obj_dict_init(mp_obj_dict_t *dict, mp_uint_t n_args);
547mp_uint_t mp_obj_dict_len(mp_obj_t self_in);
Paul Sokolovsky75ce9252014-06-05 20:02:15 +0300548mp_obj_t mp_obj_dict_get(mp_obj_t self_in, mp_obj_t index);
Damiend99b0522013-12-21 18:17:45 +0000549mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value);
Damien George66edc5d2014-04-05 13:25:13 +0100550mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key);
Damien Georgedf6567e2014-03-30 13:54:02 +0100551mp_map_t *mp_obj_dict_get_map(mp_obj_t self_in);
Damiend99b0522013-12-21 18:17:45 +0000552
553// set
554void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item);
555
Paul Sokolovsky1c6de112014-01-03 02:41:17 +0200556// slice
Paul Sokolovskyafaaf532014-05-25 01:39:27 +0300557void mp_obj_slice_get(mp_obj_t self_in, mp_obj_t *start, mp_obj_t *stop, mp_obj_t *step);
Paul Sokolovsky1c6de112014-01-03 02:41:17 +0200558
Damiend99b0522013-12-21 18:17:45 +0000559// functions
Damien Georged5e81822014-02-26 17:47:05 +0000560#define MP_OBJ_FUN_ARGS_MAX (0xffff) // to set maximum value in n_args_max below
Damien George3c658a42014-08-24 16:28:17 +0100561typedef struct _mp_obj_fun_builtin_t { // use this to make const objects that go in ROM
Damiend99b0522013-12-21 18:17:45 +0000562 mp_obj_base_t base;
Damien George51047752014-02-26 17:40:52 +0000563 bool is_kw : 1;
Damien George3c658a42014-08-24 16:28:17 +0100564 mp_uint_t n_args_min : 15; // inclusive
565 mp_uint_t n_args_max : 16; // inclusive
566 void *fun; // must be a pointer to a callable function in ROM
567} mp_obj_fun_builtin_t;
Damien George97209d32014-01-07 15:58:30 +0000568
Paul Sokolovskyab7bf282014-05-17 11:08:33 +0300569const char *mp_obj_fun_get_name(mp_const_obj_t fun);
Paul Sokolovskyc3103b52014-05-01 22:20:07 +0300570const char *mp_obj_code_get_name(const byte *code_info);
Damien660365e2013-12-17 18:27:24 +0000571
Paul Sokolovskydff3f892014-01-20 18:37:30 +0200572mp_obj_t mp_identity(mp_obj_t self);
Paul Sokolovsky557c9d52014-02-08 21:57:19 +0200573MP_DECLARE_CONST_FUN_OBJ(mp_identity_obj);
Paul Sokolovskydff3f892014-01-20 18:37:30 +0200574
Damien George28708622014-01-02 21:30:26 +0000575// module
Damien George0c36da02014-03-08 15:24:39 +0000576typedef struct _mp_obj_module_t {
577 mp_obj_base_t base;
578 qstr name;
Damien George8b0535e2014-04-05 21:53:54 +0100579 mp_obj_dict_t *globals;
Damien George0c36da02014-03-08 15:24:39 +0000580} mp_obj_module_t;
Damien George8b0535e2014-04-05 21:53:54 +0100581mp_obj_dict_t *mp_obj_module_get_globals(mp_obj_t self_in);
Paul Sokolovskye5a37592014-10-25 21:04:13 +0300582// check if given module object is a package
583bool mp_obj_is_package(mp_obj_t module);
Damien Georgeeae16442014-01-11 19:22:29 +0000584
585// staticmethod and classmethod types; defined here so we can make const versions
Damien George64131f32014-02-06 20:31:44 +0000586// this structure is used for instances of both staticmethod and classmethod
587typedef struct _mp_obj_static_class_method_t {
Damien Georgeeae16442014-01-11 19:22:29 +0000588 mp_obj_base_t base;
589 mp_obj_t fun;
Damien George64131f32014-02-06 20:31:44 +0000590} mp_obj_static_class_method_t;
Paul Sokolovsky439542f2014-01-21 00:19:19 +0200591
Damien George777b0f32014-04-13 18:59:45 +0100592// property
593const mp_obj_t *mp_obj_property_get(mp_obj_t self_in);
594
Paul Sokolovsky439542f2014-01-21 00:19:19 +0200595// sequence helpers
Paul Sokolovskyde4b9322014-05-25 21:21:57 +0300596
597// slice indexes resolved to particular sequence
598typedef struct {
Damien George40f3c022014-07-03 13:25:24 +0100599 mp_uint_t start;
600 mp_uint_t stop;
601 mp_int_t step;
Paul Sokolovskyde4b9322014-05-25 21:21:57 +0300602} mp_bound_slice_t;
603
Damien Georged182b982014-08-30 14:19:41 +0100604void mp_seq_multiply(const void *items, mp_uint_t item_sz, mp_uint_t len, mp_uint_t times, void *dest);
Damien Georgec49ddb92014-06-01 13:49:35 +0100605#if MICROPY_PY_BUILTINS_SLICE
Damien George40f3c022014-07-03 13:25:24 +0100606bool mp_seq_get_fast_slice_indexes(mp_uint_t len, mp_obj_t slice, mp_bound_slice_t *indexes);
Damien Georgec49ddb92014-06-01 13:49:35 +0100607#endif
Paul Sokolovskyd915a522014-05-10 21:36:33 +0300608#define mp_seq_copy(dest, src, len, item_t) memcpy(dest, src, len * sizeof(item_t))
609#define mp_seq_cat(dest, src1, len1, src2, len2, item_t) { memcpy(dest, src1, (len1) * sizeof(item_t)); memcpy(dest + (len1), src2, (len2) * sizeof(item_t)); }
Damien George4d917232014-08-30 14:28:06 +0100610bool mp_seq_cmp_bytes(mp_uint_t op, const byte *data1, mp_uint_t len1, const byte *data2, mp_uint_t len2);
611bool mp_seq_cmp_objs(mp_uint_t op, const mp_obj_t *items1, mp_uint_t len1, const mp_obj_t *items2, mp_uint_t len2);
Damien Georged182b982014-08-30 14:19:41 +0100612mp_obj_t mp_seq_index_obj(const mp_obj_t *items, mp_uint_t len, mp_uint_t n_args, const mp_obj_t *args);
613mp_obj_t mp_seq_count_obj(const mp_obj_t *items, mp_uint_t len, mp_obj_t value);
614mp_obj_t mp_seq_extract_slice(mp_uint_t len, const mp_obj_t *seq, mp_bound_slice_t *indexes);
Paul Sokolovskya2240672014-04-28 00:16:57 +0300615// Helper to clear stale pointers from allocated, but unused memory, to preclude GC problems
616#define mp_seq_clear(start, len, alloc_len, item_sz) memset((byte*)(start) + (len) * (item_sz), 0, ((alloc_len) - (len)) * (item_sz))
Paul Sokolovsky94d82462014-05-10 22:23:00 +0300617#define mp_seq_replace_slice_no_grow(dest, dest_len, beg, end, slice, slice_len, item_t) \
618 /*printf("memcpy(%p, %p, %d)\n", dest + beg, slice, slice_len * sizeof(item_t));*/ \
619 memcpy(dest + beg, slice, slice_len * sizeof(item_t)); \
Damien George17ae2392014-08-29 21:07:54 +0100620 /*printf("memmove(%p, %p, %d)\n", dest + (beg + slice_len), dest + end, (dest_len - end) * sizeof(item_t));*/ \
621 memmove(dest + (beg + slice_len), dest + end, (dest_len - end) * sizeof(item_t));
Paul Sokolovsky2705f4c2014-05-25 02:36:12 +0300622
623#define mp_seq_replace_slice_grow_inplace(dest, dest_len, beg, end, slice, slice_len, len_adj, item_t) \
624 /*printf("memmove(%p, %p, %d)\n", dest + beg + len_adj, dest + beg, (dest_len - beg) * sizeof(item_t));*/ \
625 memmove(dest + beg + len_adj, dest + beg, (dest_len - beg) * sizeof(item_t)); \
626 memcpy(dest + beg, slice, slice_len * sizeof(item_t));
Damien George9ddbe292014-12-29 01:02:19 +0000627
628#endif // __MICROPY_INCLUDED_PY_OBJ_H__