@@ -71,9 +71,10 @@ typedef struct _mp_obj_base_t mp_obj_base_t;
7171//#define MP_OBJ_IS_SMALL_INT(o) ((((mp_int_t)(o)) & 1) != 0)
7272//#define MP_OBJ_IS_QSTR(o) ((((mp_int_t)(o)) & 3) == 2)
7373//#define MP_OBJ_IS_OBJ(o) ((((mp_int_t)(o)) & 3) == 0)
74- #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
74+ #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
7575#define MP_OBJ_IS_INT (o ) (MP_OBJ_IS_SMALL_INT(o) || MP_OBJ_IS_TYPE(o, &mp_type_int))
7676#define MP_OBJ_IS_STR (o ) (MP_OBJ_IS_QSTR(o) || MP_OBJ_IS_TYPE(o, &mp_type_str))
77+ #define MP_OBJ_IS_FUN (o ) (MP_OBJ_IS_OBJ(o) && (((mp_obj_base_t*)(o))->type->binary_op == mp_obj_fun_binary_op))
7778
7879#define MP_OBJ_SMALL_INT_VALUE (o ) (((mp_int_t)(o)) >> 1)
7980#define MP_OBJ_NEW_SMALL_INT (small_int ) ((mp_obj_t)((((mp_int_t)(small_int)) << 1) | 1))
@@ -84,9 +85,9 @@ typedef struct _mp_obj_base_t mp_obj_base_t;
8485// These macros are used to declare and define constant function objects
8586// You can put "static" in front of the definitions to make them local
8687
87- #define MP_DECLARE_CONST_FUN_OBJ (obj_name ) extern const mp_obj_fun_native_t obj_name
88+ #define MP_DECLARE_CONST_FUN_OBJ (obj_name ) extern const mp_obj_fun_builtin_t obj_name
8889
89- #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}
90+ #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}
9091#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)
9192#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)
9293#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)
@@ -178,7 +179,6 @@ typedef mp_obj_t (*mp_fun_0_t)(void);
178179typedef mp_obj_t (* mp_fun_1_t )(mp_obj_t );
179180typedef mp_obj_t (* mp_fun_2_t )(mp_obj_t , mp_obj_t );
180181typedef mp_obj_t (* mp_fun_3_t )(mp_obj_t , mp_obj_t , mp_obj_t );
181- typedef mp_obj_t (* mp_fun_t )(void );
182182typedef mp_obj_t (* mp_fun_var_t )(uint n , const mp_obj_t * );
183183typedef mp_obj_t (* mp_fun_kw_t )(uint n , const mp_obj_t * , mp_map_t * );
184184
@@ -304,7 +304,7 @@ extern const mp_obj_type_t mp_type_zip;
304304extern const mp_obj_type_t mp_type_array ;
305305extern const mp_obj_type_t mp_type_super ;
306306extern const mp_obj_type_t mp_type_gen_instance ;
307- extern const mp_obj_type_t mp_type_fun_native ;
307+ extern const mp_obj_type_t mp_type_fun_builtin ;
308308extern const mp_obj_type_t mp_type_fun_bc ;
309309extern const mp_obj_type_t mp_type_module ;
310310extern const mp_obj_type_t mp_type_staticmethod ;
@@ -377,9 +377,10 @@ mp_obj_t mp_obj_new_exception_arg1(const mp_obj_type_t *exc_type, mp_obj_t arg);
377377mp_obj_t mp_obj_new_exception_args (const mp_obj_type_t * exc_type , uint n_args , const mp_obj_t * args );
378378mp_obj_t mp_obj_new_exception_msg (const mp_obj_type_t * exc_type , const char * msg );
379379mp_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!)
380- mp_obj_t mp_obj_new_fun_bc (uint scope_flags , qstr * args , uint n_pos_args , uint n_kwonly_args , mp_obj_t def_args , mp_obj_t def_kw_args , const byte * code );
381- mp_obj_t mp_obj_new_fun_viper (uint n_args , void * fun , mp_uint_t type_sig );
382- mp_obj_t mp_obj_new_fun_asm (uint n_args , void * fun );
380+ mp_obj_t mp_obj_new_fun_bc (mp_uint_t scope_flags , qstr * args , 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 );
381+ mp_obj_t mp_obj_new_fun_native (mp_uint_t n_args , void * fun_data );
382+ mp_obj_t mp_obj_new_fun_viper (mp_uint_t n_args , void * fun_data , mp_uint_t type_sig );
383+ mp_obj_t mp_obj_new_fun_asm (mp_uint_t n_args , void * fun_data );
383384mp_obj_t mp_obj_new_gen_wrap (mp_obj_t fun );
384385mp_obj_t mp_obj_new_closure (mp_obj_t fun , uint n_closed , const mp_obj_t * closed );
385386mp_obj_t mp_obj_new_tuple (uint n , const mp_obj_t * items );
@@ -525,17 +526,15 @@ mp_obj_t mp_obj_new_bytearray_by_ref(uint n, void *items);
525526
526527// functions
527528#define MP_OBJ_FUN_ARGS_MAX (0xffff) // to set maximum value in n_args_max below
528- typedef struct _mp_obj_fun_native_t { // need this so we can define const objects (to go in ROM)
529+ typedef struct _mp_obj_fun_builtin_t { // use this to make const objects that go in ROM
529530 mp_obj_base_t base ;
530531 bool is_kw : 1 ;
531- uint n_args_min : 15 ; // inclusive
532- uint n_args_max : 16 ; // inclusive
533- void * fun ;
534- // TODO add mp_map_t *globals
535- // for const function objects, make an empty, const map
536- // such functions won't be able to access the global scope, but that's probably okay
537- } mp_obj_fun_native_t ;
532+ mp_uint_t n_args_min : 15 ; // inclusive
533+ mp_uint_t n_args_max : 16 ; // inclusive
534+ void * fun ; // must be a pointer to a callable function in ROM
535+ } mp_obj_fun_builtin_t ;
538536
537+ mp_obj_t mp_obj_fun_binary_op (int op , mp_obj_t lhs_in , mp_obj_t rhs_in );
539538const char * mp_obj_fun_get_name (mp_const_obj_t fun );
540539const char * mp_obj_code_get_name (const byte * code_info );
541540
0 commit comments