Skip to content
Snippets Groups Projects
Select Git revision
  • 71d3112f7e34d0bdd70012463cf31871bea57c45
  • wip-bootstrap default
  • dualcore
  • ch3/leds
  • ch3/time
  • master
6 results

obj.h

Blame
  • obj.h 24.39 KiB
    // All Micro Python objects are at least this type
    // It must be of pointer size
    
    typedef machine_ptr_t mp_obj_t;
    typedef machine_const_ptr_t mp_const_obj_t;
    
    // Integers that fit in a pointer have this type
    // (do we need to expose this in the public API?)
    
    typedef machine_int_t mp_small_int_t;
    
    // Anything that wants to be a Micro Python object must have
    // mp_obj_base_t as its first member (except NULL and small ints)
    
    struct _mp_obj_type_t;
    struct _mp_obj_base_t {
        const struct _mp_obj_type_t *type;
    };
    typedef struct _mp_obj_base_t mp_obj_base_t;
    
    // The NULL object is used to indicate the absence of an object
    // It *cannot* be used when an mp_obj_t is expected, except where explicitly allowed
    
    #define MP_OBJ_NULL ((mp_obj_t)0)
    
    // The SENTINEL object is used for various internal purposes where one needs
    // an object which is unique from all other objects, including MP_OBJ_NULL.
    
    #define MP_OBJ_SENTINEL ((mp_obj_t)8)
    
    // These macros check for small int, qstr or object, and access small int and qstr values
    //  - xxxx...xxx1: a small int, bits 1 and above are the value
    //  - xxxx...xx10: a qstr, bits 2 and above are the value
    //  - xxxx...xx00: a pointer to an mp_obj_base_t
    
    // In SMALL_INT, next-to-highest bits is used as sign, so both must match for value in range
    #define MP_SMALL_INT_MIN ((mp_small_int_t)(((machine_int_t)WORD_MSBIT_HIGH) >> 1))
    #define MP_SMALL_INT_MAX ((mp_small_int_t)(~(MP_SMALL_INT_MIN)))
    #define MP_OBJ_FITS_SMALL_INT(n) ((((n) ^ ((n) << 1)) & WORD_MSBIT_HIGH) == 0)
    // these macros have now become inline functions; see below
    //#define MP_OBJ_IS_SMALL_INT(o) ((((mp_small_int_t)(o)) & 1) != 0)
    //#define MP_OBJ_IS_QSTR(o) ((((mp_small_int_t)(o)) & 3) == 2)
    //#define MP_OBJ_IS_OBJ(o) ((((mp_small_int_t)(o)) & 3) == 0)
    #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
    #define MP_OBJ_IS_INT(o) (MP_OBJ_IS_SMALL_INT(o) || MP_OBJ_IS_TYPE(o, &mp_type_int))
    #define MP_OBJ_IS_STR(o) (MP_OBJ_IS_QSTR(o) || MP_OBJ_IS_TYPE(o, &mp_type_str))
    
    #define MP_OBJ_SMALL_INT_VALUE(o) (((mp_small_int_t)(o)) >> 1)
    #define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)(((small_int) << 1) | 1))
    
    #define MP_OBJ_QSTR_VALUE(o) (((mp_small_int_t)(o)) >> 2)
    #define MP_OBJ_NEW_QSTR(qstr) ((mp_obj_t)((((machine_uint_t)qstr) << 2) | 2))
    
    // These macros are used to declare and define constant function objects
    // You can put "static" in front of the definitions to make them local
    
    #define MP_DECLARE_CONST_FUN_OBJ(obj_name) extern const mp_obj_fun_native_t obj_name
    
    #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 = {{&mp_type_fun_native}, is_kw, n_args_min, n_args_max, (void *)fun_name}
    #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)
    #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)
    #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)
    #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)
    #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)
    #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)
    #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)
    
    // This macro is used to define constant dict objects
    // You can put "static" in front of the definition to make it local