Skip to content

Commit 1b0aab6

Browse files
committed
py: Change struct and macro for builtin fun so they can be type checked.
1 parent 3d2daa2 commit 1b0aab6

8 files changed

Lines changed: 51 additions & 24 deletions

File tree

bare-arm/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ mp_import_stat_t mp_import_stat(const char *path) {
4545
return MP_IMPORT_STAT_NO_EXIST;
4646
}
4747

48-
mp_obj_t mp_builtin_open(uint n_args, const mp_obj_t *args, mp_map_t *kwargs) {
48+
mp_obj_t mp_builtin_open(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
4949
return mp_const_none;
5050
}
5151
MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open);

py/modbuiltins.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,11 +552,19 @@ STATIC mp_obj_t mp_builtin_hasattr(mp_obj_t object_in, mp_obj_t attr_in) {
552552
}
553553
MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_hasattr_obj, mp_builtin_hasattr);
554554

555+
STATIC mp_obj_t mp_builtin_globals(void) {
556+
return MP_OBJ_FROM_PTR(mp_globals_get());
557+
}
558+
MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_globals_obj, mp_builtin_globals);
559+
560+
STATIC mp_obj_t mp_builtin_locals(void) {
561+
return MP_OBJ_FROM_PTR(mp_locals_get());
562+
}
563+
MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_locals_obj, mp_builtin_locals);
564+
555565
// These are defined in terms of MicroPython API functions right away
556566
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_id_obj, mp_obj_id);
557567
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_len_obj, mp_obj_len);
558-
MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_globals_obj, mp_globals_get);
559-
MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_locals_obj, mp_locals_get);
560568

561569
STATIC const mp_rom_map_elem_t mp_module_builtins_globals_table[] = {
562570
// built-in core functions

py/obj.h

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -270,14 +270,27 @@ static inline bool mp_obj_is_integer(mp_const_obj_t o) { return MP_OBJ_IS_INT(o)
270270

271271
#define MP_DECLARE_CONST_FUN_OBJ(obj_name) extern const mp_obj_fun_builtin_t obj_name
272272

273-
#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(*)(void))fun_name}
274-
#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)
275-
#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)
276-
#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)
277-
#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)
278-
#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)
279-
#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)
280-
#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)
273+
#define MP_DEFINE_CONST_FUN_OBJ_0(obj_name, fun_name) \
274+
const mp_obj_fun_builtin_t obj_name = \
275+
{{&mp_type_fun_builtin}, false, 0, 0, .fun._0 = fun_name}
276+
#define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name) \
277+
const mp_obj_fun_builtin_t obj_name = \
278+
{{&mp_type_fun_builtin}, false, 1, 1, .fun._1 = fun_name}
279+
#define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name) \
280+
const mp_obj_fun_builtin_t obj_name = \
281+
{{&mp_type_fun_builtin}, false, 2, 2, .fun._2 = fun_name}
282+
#define MP_DEFINE_CONST_FUN_OBJ_3(obj_name, fun_name) \
283+
const mp_obj_fun_builtin_t obj_name = \
284+
{{&mp_type_fun_builtin}, false, 3, 3, .fun._3 = fun_name}
285+
#define MP_DEFINE_CONST_FUN_OBJ_VAR(obj_name, n_args_min, fun_name) \
286+
const mp_obj_fun_builtin_t obj_name = \
287+
{{&mp_type_fun_builtin}, false, n_args_min, MP_OBJ_FUN_ARGS_MAX, .fun.var = fun_name}
288+
#define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name) \
289+
const mp_obj_fun_builtin_t obj_name = \
290+
{{&mp_type_fun_builtin}, false, n_args_min, n_args_max, .fun.var = fun_name}
291+
#define MP_DEFINE_CONST_FUN_OBJ_KW(obj_name, n_args_min, fun_name) \
292+
const mp_obj_fun_builtin_t obj_name = \
293+
{{&mp_type_fun_builtin}, true, n_args_min, MP_OBJ_FUN_ARGS_MAX, .fun.kw = fun_name}
281294

282295
// These macros are used to define constant map/dict objects
283296
// You can put "static" in front of the definition to make it local
@@ -744,7 +757,14 @@ typedef struct _mp_obj_fun_builtin_t { // use this to make const objects that go
744757
bool is_kw : 1;
745758
mp_uint_t n_args_min : 15; // inclusive
746759
mp_uint_t n_args_max : 16; // inclusive
747-
void (*fun)(void); // must be a pointer to a callable function in ROM
760+
union {
761+
mp_fun_0_t _0;
762+
mp_fun_1_t _1;
763+
mp_fun_2_t _2;
764+
mp_fun_3_t _3;
765+
mp_fun_var_t var;
766+
mp_fun_kw_t kw;
767+
} fun;
748768
} mp_obj_fun_builtin_t;
749769

750770
qstr mp_obj_fun_get_name(mp_const_obj_t fun);

py/objfun.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,31 +66,31 @@ STATIC mp_obj_t fun_builtin_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n
6666
mp_map_t kw_args;
6767
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
6868

69-
return ((mp_fun_kw_t)self->fun)(n_args, args, &kw_args);
69+
return self->fun.kw(n_args, args, &kw_args);
7070

7171
} else if (self->n_args_min <= 3 && self->n_args_min == self->n_args_max) {
7272
// function requires a fixed number of arguments
7373

7474
// dispatch function call
7575
switch (self->n_args_min) {
7676
case 0:
77-
return ((mp_fun_0_t)self->fun)();
77+
return self->fun._0();
7878

7979
case 1:
80-
return ((mp_fun_1_t)self->fun)(args[0]);
80+
return self->fun._1(args[0]);
8181

8282
case 2:
83-
return ((mp_fun_2_t)self->fun)(args[0], args[1]);
83+
return self->fun._2(args[0], args[1]);
8484

8585
case 3:
8686
default:
87-
return ((mp_fun_3_t)self->fun)(args[0], args[1], args[2]);
87+
return self->fun._3(args[0], args[1], args[2]);
8888
}
8989

9090
} else {
9191
// function takes a variable number of arguments, but no keywords
9292

93-
return ((mp_fun_var_t)self->fun)(n_args, args);
93+
return self->fun.var(n_args, args);
9494
}
9595
}
9696

qemu-arm/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ mp_import_stat_t mp_import_stat(const char *path) {
5252
return MP_IMPORT_STAT_NO_EXIST;
5353
}
5454

55-
mp_obj_t mp_builtin_open(uint n_args, const mp_obj_t *args, mp_map_t *kwargs) {
55+
mp_obj_t mp_builtin_open(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
5656
return mp_const_none;
5757
}
5858
MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open);

qemu-arm/test_main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ mp_import_stat_t mp_import_stat(const char *path) {
8282
return MP_IMPORT_STAT_NO_EXIST;
8383
}
8484

85-
mp_obj_t mp_builtin_open(uint n_args, const mp_obj_t *args, mp_map_t *kwargs) {
85+
mp_obj_t mp_builtin_open(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
8686
return mp_const_none;
8787
}
8888
MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open);

unix/input.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ void prompt_write_history(void) {
158158
#endif
159159
}
160160

161-
STATIC mp_obj_t mp_builtin_input(uint n_args, const mp_obj_t *args) {
161+
STATIC mp_obj_t mp_builtin_input(mp_uint_t n_args, const mp_obj_t *args) {
162162
if (n_args == 1) {
163163
mp_obj_print(args[0], PRINT_STR);
164164
}
@@ -171,5 +171,4 @@ STATIC mp_obj_t mp_builtin_input(uint n_args, const mp_obj_t *args) {
171171
free(line);
172172
return o;
173173
}
174-
175174
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_input_obj, 0, 1, mp_builtin_input);

unix/moduselect.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ typedef struct _mp_obj_poll_t {
5252
} mp_obj_poll_t;
5353

5454
/// \method register(obj[, eventmask])
55-
STATIC mp_obj_t poll_register(uint n_args, const mp_obj_t *args) {
55+
STATIC mp_obj_t poll_register(mp_uint_t n_args, const mp_obj_t *args) {
5656
mp_obj_poll_t *self = MP_OBJ_TO_PTR(args[0]);
5757
int fd = mp_obj_get_int(args[1]);
5858
mp_uint_t flags;
@@ -129,7 +129,7 @@ MP_DEFINE_CONST_FUN_OBJ_3(poll_modify_obj, poll_modify);
129129

130130
/// \method poll([timeout])
131131
/// Timeout is in milliseconds.
132-
STATIC mp_obj_t poll_poll(uint n_args, const mp_obj_t *args) {
132+
STATIC mp_obj_t poll_poll(mp_uint_t n_args, const mp_obj_t *args) {
133133
mp_obj_poll_t *self = MP_OBJ_TO_PTR(args[0]);
134134

135135
// work out timeout (it's given already in ms)

0 commit comments

Comments
 (0)