Skip to content

Commit c06763a

Browse files
committed
This implements a better (more python-conformant) list.sort.
It's not really about that, though; it's about me figuring out a sane way forward for keyword-argument functions (and function metadata). But it's useful as is, and shouldn't break any existing code, so here you have it; I'm going to park it in my mind for a bit while sorting out the rest of the dict branch.
1 parent 880ce2d commit c06763a

25 files changed

Lines changed: 181 additions & 216 deletions

py/obj.h

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,16 @@ struct _mp_obj_base_t {
4343

4444
#define MP_DECLARE_CONST_FUN_OBJ(obj_name) extern const mp_obj_fun_native_t obj_name
4545

46-
#define MP_DEFINE_CONST_FUN_OBJ_0(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, 0, 0, fun_name}
47-
#define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, 1, 1, fun_name}
48-
#define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, 2, 2, fun_name}
49-
#define MP_DEFINE_CONST_FUN_OBJ_3(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, 3, 3, fun_name}
50-
#define MP_DEFINE_CONST_FUN_OBJ_VAR(obj_name, n_args_min, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, n_args_min, (~((machine_uint_t)0)), fun_name}
51-
#define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, n_args_min, n_args_max, fun_name}
46+
#define MP_DEFINE_CONST_FUN_OBJ_0(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, false, 0, 0, fun_name}
47+
#define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, false, 1, 1, fun_name}
48+
#define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, false, 2, 2, fun_name}
49+
#define MP_DEFINE_CONST_FUN_OBJ_3(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, false, 3, 3, fun_name}
50+
#define MP_DEFINE_CONST_FUN_OBJ_VAR(obj_name, n_args_min, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, false, n_args_min, (~((machine_uint_t)0)), fun_name}
51+
#define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, false, n_args_min, n_args_max, fun_name}
52+
#define MP_DEFINE_CONST_FUN_OBJ_KW(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, true, 0, (~((machine_uint_t)0)), fun_name}
53+
54+
// Need to declare this here so we are not dependent on map.h
55+
struct _mp_map_t;
5256

5357
// Type definitions for methods
5458

@@ -58,10 +62,12 @@ typedef mp_obj_t (*mp_fun_2_t)(mp_obj_t, mp_obj_t);
5862
typedef mp_obj_t (*mp_fun_3_t)(mp_obj_t, mp_obj_t, mp_obj_t);
5963
typedef mp_obj_t (*mp_fun_t)(void);
6064
typedef mp_obj_t (*mp_fun_var_t)(int n, const mp_obj_t *);
65+
typedef mp_obj_t (*mp_fun_kw_t)(mp_obj_t*, struct _mp_map_t*);
6166

6267
typedef void (*mp_print_fun_t)(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o);
6368
typedef mp_obj_t (*mp_make_new_fun_t)(mp_obj_t type_in, int n_args, const mp_obj_t *args); // args are in reverse order in the array
6469
typedef mp_obj_t (*mp_call_n_fun_t)(mp_obj_t fun, int n_args, const mp_obj_t *args); // args are in reverse order in the array
70+
typedef mp_obj_t (*mp_call_n_kw_fun_t)(mp_obj_t fun, int n_args, int n_kw, const mp_obj_t *args); // args are in reverse order in the array
6571
typedef mp_obj_t (*mp_unary_op_fun_t)(int op, mp_obj_t);
6672
typedef mp_obj_t (*mp_binary_op_fun_t)(int op, mp_obj_t, mp_obj_t);
6773

@@ -77,6 +83,7 @@ struct _mp_obj_type_t {
7783
mp_make_new_fun_t make_new; // to make an instance of the type
7884

7985
mp_call_n_fun_t call_n;
86+
mp_call_n_kw_fun_t call_n_kw;
8087
mp_unary_op_fun_t unary_op; // can return NULL if op not supported
8188
mp_binary_op_fun_t binary_op; // can return NULL if op not supported
8289

@@ -118,10 +125,6 @@ extern const mp_obj_t mp_const_empty_tuple;
118125
extern const mp_obj_t mp_const_ellipsis;
119126
extern const mp_obj_t mp_const_stop_iteration; // special object indicating end of iteration (not StopIteration exception!)
120127

121-
// Need to declare this here so we are not dependent on map.h
122-
123-
struct _mp_map_t;
124-
125128
// General API for objects
126129

127130
mp_obj_t mp_obj_new_none(void);
@@ -144,8 +147,8 @@ mp_obj_t mp_obj_new_fun_asm(uint n_args, void *fun);
144147
mp_obj_t mp_obj_new_gen_wrap(uint n_locals, uint n_stack, mp_obj_t fun);
145148
mp_obj_t mp_obj_new_gen_instance(const byte *bytecode, uint n_state, int n_args, const mp_obj_t *args);
146149
mp_obj_t mp_obj_new_closure(mp_obj_t fun, mp_obj_t closure_tuple);
147-
mp_obj_t mp_obj_new_tuple(uint n, mp_obj_t *items);
148-
mp_obj_t mp_obj_new_tuple_reverse(uint n, mp_obj_t *items);
150+
mp_obj_t mp_obj_new_tuple(uint n, const mp_obj_t *items);
151+
mp_obj_t mp_obj_new_tuple_reverse(uint n, const mp_obj_t *items);
149152
mp_obj_t mp_obj_new_list(uint n, mp_obj_t *items);
150153
mp_obj_t mp_obj_new_list_reverse(uint n, mp_obj_t *items);
151154
mp_obj_t mp_obj_new_dict(int n_args);
@@ -234,7 +237,8 @@ void mp_obj_slice_get(mp_obj_t self_in, machine_int_t *start, machine_int_t *sto
234237
// functions
235238
typedef struct _mp_obj_fun_native_t { // need this so we can define const objects (to go in ROM)
236239
mp_obj_base_t base;
237-
machine_uint_t n_args_min; // inclusive
240+
bool is_kw : 1;
241+
machine_uint_t n_args_min : (sizeof(machine_uint_t) - 1); // inclusive
238242
machine_uint_t n_args_max; // inclusive
239243
void *fun;
240244
// TODO add mp_map_t *globals

py/objbool.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,10 @@ static mp_obj_t bool_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args
3232
}
3333

3434
const mp_obj_type_t bool_type = {
35-
{ &mp_const_type },
36-
"bool",
37-
bool_print, // print
38-
bool_make_new, // make_new
39-
NULL, // call_n
40-
NULL, // unary_op
41-
NULL, // binary_op
42-
NULL, // getiter
43-
NULL, // iternext
35+
.base = { &mp_const_type },
36+
.name = "bool",
37+
.print = bool_print,
38+
.make_new = bool_make_new,
4439
.methods = {{NULL, NULL},},
4540
};
4641

py/objboundmeth.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,9 @@ mp_obj_t bound_meth_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) {
3434
}
3535

3636
const mp_obj_type_t bound_meth_type = {
37-
{ &mp_const_type },
38-
"bound_method",
39-
NULL, // print
40-
NULL, // make_new
41-
bound_meth_call_n, // call_n
42-
NULL, // unary_op
43-
NULL, // binary_op
44-
NULL, // getiter
45-
NULL, // iternext
37+
.base = { &mp_const_type },
38+
.name = "bound_method",
39+
.call_n = bound_meth_call_n,
4640
.methods = {{NULL, NULL},},
4741
};
4842

py/objcell.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,8 @@ void mp_obj_cell_set(mp_obj_t self_in, mp_obj_t obj) {
2424
}
2525

2626
const mp_obj_type_t cell_type = {
27-
{ &mp_const_type },
28-
"cell",
29-
NULL, // print
30-
NULL, // make_new
31-
NULL, // call_n
32-
NULL, // unary_op
33-
NULL, // binary_op
34-
NULL, // getiter
35-
NULL, // iternext
27+
.base = { &mp_const_type },
28+
.name = "cell",
3629
.methods = {{NULL, NULL},},
3730
};
3831

py/objclass.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,9 @@ mp_map_t *mp_obj_class_get_locals(mp_obj_t self_in) {
6161
}
6262

6363
const mp_obj_type_t class_type = {
64-
{ &mp_const_type },
65-
"class",
66-
NULL, // print
67-
NULL, // make_new
68-
class_call_n, // call_n
69-
NULL, // unary_op
70-
NULL, // binary_op
71-
NULL, // getiter
72-
NULL, // iternext
64+
.base = { &mp_const_type },
65+
.name = "class",
66+
.call_n = class_call_n,
7367
.methods = {{NULL, NULL},},
7468
};
7569

py/objclosure.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,9 @@ mp_obj_t closure_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) {
3333
}
3434

3535
const mp_obj_type_t closure_type = {
36-
{ &mp_const_type },
37-
"closure",
38-
NULL, // print
39-
NULL, // make_new
40-
closure_call_n, // call_n
41-
NULL, // unary_op
42-
NULL, // binary_op
43-
NULL, // getiter
44-
NULL, // iternext
36+
.base = { &mp_const_type },
37+
.name = "closure",
38+
.call_n = closure_call_n,
4539
.methods = {{NULL, NULL},},
4640
};
4741

py/objcomplex.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,12 @@ static mp_obj_t complex_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
8585
}
8686

8787
const mp_obj_type_t complex_type = {
88-
{ &mp_const_type },
89-
"complex",
90-
complex_print, // print
91-
complex_make_new, // make_new
92-
NULL, // call_n
93-
complex_unary_op, // unary_op
94-
complex_binary_op, // binary_op
95-
NULL, // getiter
96-
NULL, // iternext
88+
.base = { &mp_const_type },
89+
.name = "complex",
90+
.print = complex_print,
91+
.make_new = complex_make_new,
92+
.unary_op = complex_unary_op,
93+
.binary_op = complex_binary_op,
9794
.methods = { { NULL, NULL }, },
9895
};
9996

py/objdict.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,11 @@ static mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
6161
}
6262

6363
const mp_obj_type_t dict_type = {
64-
{ &mp_const_type },
65-
"dict",
64+
.base = { &mp_const_type },
65+
.name = "dict",
6666
.print = dict_print,
6767
.make_new = dict_make_new,
6868
.binary_op = dict_binary_op,
69-
.getiter = NULL,
7069
.methods = {{NULL, NULL},},
7170
};
7271

py/objexcept.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,9 @@ void exception_print(void (*print)(void *env, const char *fmt, ...), void *env,
3636
}
3737

3838
const mp_obj_type_t exception_type = {
39-
{ &mp_const_type },
40-
"exception",
41-
exception_print, // print
42-
NULL, // make_new
43-
NULL, // call_n
44-
NULL, // unary_op
45-
NULL, // binary_op
46-
NULL, // getiter
47-
NULL, // iternext
39+
.base = { &mp_const_type },
40+
.name = "exception",
41+
.print = exception_print,
4842
.methods = {{NULL, NULL},},
4943
};
5044

py/objfloat.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ static mp_obj_t float_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
6262
}
6363

6464
const mp_obj_type_t float_type = {
65-
{ &mp_const_type },
66-
"float",
65+
.base = { &mp_const_type },
66+
.name = "float",
6767
.print = float_print,
6868
.make_new = float_make_new,
6969
.unary_op = float_unary_op,

0 commit comments

Comments
 (0)