Skip to content

Commit 02eea0d

Browse files
committed
py: Make struct-initializing macros compatible with C++.
This requires explicitly naming and initializing all members so add that where needed and possible. For MP_DEFINE_NLR_JUMP_CALLBACK_FUNCTION_1 this would require initializing the .callback member, but that's a bit of a waste since the macro is always followed by a call to nlr_push_jump_callback() to initialize exactly that member, so rewrite the macro without initializers. Signed-off-by: stijn <[email protected]>
1 parent 076e071 commit 02eea0d

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

py/obj.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -371,25 +371,25 @@ typedef struct _mp_rom_obj_t { mp_const_obj_t o; } mp_rom_obj_t;
371371

372372
#define MP_DEFINE_CONST_FUN_OBJ_0(obj_name, fun_name) \
373373
const mp_obj_fun_builtin_fixed_t obj_name = \
374-
{{&mp_type_fun_builtin_0}, .fun._0 = fun_name}
374+
{.base = {.type = &mp_type_fun_builtin_0}, .fun = {._0 = fun_name}}
375375
#define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name) \
376376
const mp_obj_fun_builtin_fixed_t obj_name = \
377-
{{&mp_type_fun_builtin_1}, .fun._1 = fun_name}
377+
{.base = {.type = &mp_type_fun_builtin_1}, .fun = {._1 = fun_name}}
378378
#define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name) \
379379
const mp_obj_fun_builtin_fixed_t obj_name = \
380-
{{&mp_type_fun_builtin_2}, .fun._2 = fun_name}
380+
{.base = {.type = &mp_type_fun_builtin_2}, .fun = {._2 = fun_name}}
381381
#define MP_DEFINE_CONST_FUN_OBJ_3(obj_name, fun_name) \
382382
const mp_obj_fun_builtin_fixed_t obj_name = \
383-
{{&mp_type_fun_builtin_3}, .fun._3 = fun_name}
383+
{.base = {.type = &mp_type_fun_builtin_3}, .fun = {._3 = fun_name}}
384384
#define MP_DEFINE_CONST_FUN_OBJ_VAR(obj_name, n_args_min, fun_name) \
385385
const mp_obj_fun_builtin_var_t obj_name = \
386-
{{&mp_type_fun_builtin_var}, MP_OBJ_FUN_MAKE_SIG(n_args_min, MP_OBJ_FUN_ARGS_MAX, false), .fun.var = fun_name}
386+
{.base = {.type = &mp_type_fun_builtin_var}, .sig = MP_OBJ_FUN_MAKE_SIG(n_args_min, MP_OBJ_FUN_ARGS_MAX, false), .fun = {.var = fun_name}}
387387
#define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name) \
388388
const mp_obj_fun_builtin_var_t obj_name = \
389-
{{&mp_type_fun_builtin_var}, MP_OBJ_FUN_MAKE_SIG(n_args_min, n_args_max, false), .fun.var = fun_name}
389+
{.base = {.type = &mp_type_fun_builtin_var}, .sig = MP_OBJ_FUN_MAKE_SIG(n_args_min, n_args_max, false), .fun = {.var = fun_name}}
390390
#define MP_DEFINE_CONST_FUN_OBJ_KW(obj_name, n_args_min, fun_name) \
391391
const mp_obj_fun_builtin_var_t obj_name = \
392-
{{&mp_type_fun_builtin_var}, MP_OBJ_FUN_MAKE_SIG(n_args_min, MP_OBJ_FUN_ARGS_MAX, true), .fun.kw = fun_name}
392+
{.base = {.type = &mp_type_fun_builtin_var}, .sig = MP_OBJ_FUN_MAKE_SIG(n_args_min, MP_OBJ_FUN_ARGS_MAX, true), .fun = {.kw = fun_name}}
393393

394394
// These macros are used to define constant map/dict objects
395395
// You can put "static" in front of the definition to make it local

py/objtuple.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ extern const mp_obj_type_t mp_type_attrtuple;
5050

5151
#define MP_DEFINE_ATTRTUPLE(tuple_obj_name, fields, nitems, ...) \
5252
const mp_rom_obj_tuple_t tuple_obj_name = { \
53-
.base = {&mp_type_attrtuple}, \
53+
.base = {.type = &mp_type_attrtuple}, \
5454
.len = nitems, \
5555
.items = { __VA_ARGS__, MP_ROM_PTR((void *)fields) } \
5656
}

py/runtime.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@
3030
#include "py/pystack.h"
3131
#include "py/cstack.h"
3232

33-
// For use with mp_call_function_1_from_nlr_jump_callback.
33+
// Initialize an nlr_jump_callback_node_call_function_1_t struct for use with
34+
// nlr_push_jump_callback(&ctx.callback, mp_call_function_1_from_nlr_jump_callback);
3435
#define MP_DEFINE_NLR_JUMP_CALLBACK_FUNCTION_1(ctx, f, a) \
35-
nlr_jump_callback_node_call_function_1_t ctx = { \
36-
.func = (void (*)(void *))(f), \
37-
.arg = (a), \
38-
}
36+
nlr_jump_callback_node_call_function_1_t ctx; \
37+
ctx.func = (void (*)(void *))(f); \
38+
ctx.arg = (a)
3939

4040
typedef enum {
4141
MP_VM_RETURN_NORMAL,

0 commit comments

Comments
 (0)