1818// args[0] is function from class body
1919// args[1] is class name
2020// args[2:] are base objects
21- mp_obj_t mp_builtin___build_class__ (int n_args , const mp_obj_t * args ) {
21+ static mp_obj_t mp_builtin___build_class__ (int n_args , const mp_obj_t * args ) {
2222 assert (2 <= n_args );
2323
2424 // we differ from CPython: we set the new __locals__ object here
@@ -62,14 +62,16 @@ mp_obj_t mp_builtin___build_class__(int n_args, const mp_obj_t *args) {
6262
6363MP_DEFINE_CONST_FUN_OBJ_VAR (mp_builtin___build_class___obj , 2 , mp_builtin___build_class__ );
6464
65- mp_obj_t mp_builtin___repl_print__ (mp_obj_t o ) {
65+ static mp_obj_t mp_builtin___repl_print__ (mp_obj_t o ) {
6666 if (o != mp_const_none ) {
6767 mp_obj_print (o );
6868 printf ("\n" );
6969 }
7070 return mp_const_none ;
7171}
7272
73+ MP_DEFINE_CONST_FUN_OBJ_1 (mp_builtin___repl_print___obj , mp_builtin___repl_print__ );
74+
7375mp_obj_t mp_builtin_abs (mp_obj_t o_in ) {
7476 if (MP_OBJ_IS_SMALL_INT (o_in )) {
7577 mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE (o_in );
@@ -97,7 +99,9 @@ mp_obj_t mp_builtin_abs(mp_obj_t o_in) {
9799 }
98100}
99101
100- mp_obj_t mp_builtin_all (mp_obj_t o_in ) {
102+ MP_DEFINE_CONST_FUN_OBJ_1 (mp_builtin_abs_obj , mp_builtin_abs );
103+
104+ static mp_obj_t mp_builtin_all (mp_obj_t o_in ) {
101105 mp_obj_t iterable = rt_getiter (o_in );
102106 mp_obj_t item ;
103107 while ((item = rt_iternext (iterable )) != mp_const_stop_iteration ) {
@@ -108,7 +112,9 @@ mp_obj_t mp_builtin_all(mp_obj_t o_in) {
108112 return mp_const_true ;
109113}
110114
111- mp_obj_t mp_builtin_any (mp_obj_t o_in ) {
115+ MP_DEFINE_CONST_FUN_OBJ_1 (mp_builtin_all_obj , mp_builtin_all );
116+
117+ static mp_obj_t mp_builtin_any (mp_obj_t o_in ) {
112118 mp_obj_t iterable = rt_getiter (o_in );
113119 mp_obj_t item ;
114120 while ((item = rt_iternext (iterable )) != mp_const_stop_iteration ) {
@@ -119,15 +125,19 @@ mp_obj_t mp_builtin_any(mp_obj_t o_in) {
119125 return mp_const_false ;
120126}
121127
122- mp_obj_t mp_builtin_callable (mp_obj_t o_in ) {
128+ MP_DEFINE_CONST_FUN_OBJ_1 (mp_builtin_any_obj , mp_builtin_any );
129+
130+ static mp_obj_t mp_builtin_callable (mp_obj_t o_in ) {
123131 if (mp_obj_is_callable (o_in )) {
124132 return mp_const_true ;
125133 } else {
126134 return mp_const_false ;
127135 }
128136}
129137
130- mp_obj_t mp_builtin_chr (mp_obj_t o_in ) {
138+ MP_DEFINE_CONST_FUN_OBJ_1 (mp_builtin_callable_obj , mp_builtin_callable );
139+
140+ static mp_obj_t mp_builtin_chr (mp_obj_t o_in ) {
131141 int ord = mp_obj_get_int (o_in );
132142 if (0 <= ord && ord <= 0x10ffff ) {
133143 char * str = m_new (char , 2 );
@@ -139,7 +149,9 @@ mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
139149 }
140150}
141151
142- mp_obj_t mp_builtin_divmod (mp_obj_t o1_in , mp_obj_t o2_in ) {
152+ MP_DEFINE_CONST_FUN_OBJ_1 (mp_builtin_chr_obj , mp_builtin_chr );
153+
154+ static mp_obj_t mp_builtin_divmod (mp_obj_t o1_in , mp_obj_t o2_in ) {
143155 if (MP_OBJ_IS_SMALL_INT (o1_in ) && MP_OBJ_IS_SMALL_INT (o2_in )) {
144156 mp_small_int_t i1 = MP_OBJ_SMALL_INT_VALUE (o1_in );
145157 mp_small_int_t i2 = MP_OBJ_SMALL_INT_VALUE (o2_in );
@@ -152,6 +164,8 @@ mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) {
152164 }
153165}
154166
167+ MP_DEFINE_CONST_FUN_OBJ_2 (mp_builtin_divmod_obj , mp_builtin_divmod );
168+
155169static mp_obj_t mp_builtin_hash (mp_obj_t o_in ) {
156170 // TODO hash will generally overflow small integer; can we safely truncate it?
157171 return mp_obj_new_int (mp_obj_hash (o_in ));
@@ -165,7 +179,7 @@ static mp_obj_t mp_builtin_iter(mp_obj_t o_in) {
165179
166180MP_DEFINE_CONST_FUN_OBJ_1 (mp_builtin_iter_obj , mp_builtin_iter );
167181
168- mp_obj_t mp_builtin_len (mp_obj_t o_in ) {
182+ static mp_obj_t mp_builtin_len (mp_obj_t o_in ) {
169183 mp_obj_t len = mp_obj_len_maybe (o_in );
170184 if (len == NULL ) {
171185 nlr_jump (mp_obj_new_exception_msg_varg (MP_QSTR_TypeError , "object of type '%s' has no len()" , mp_obj_get_type_str (o_in )));
@@ -174,7 +188,9 @@ mp_obj_t mp_builtin_len(mp_obj_t o_in) {
174188 }
175189}
176190
177- mp_obj_t mp_builtin_max (int n_args , const mp_obj_t * args ) {
191+ MP_DEFINE_CONST_FUN_OBJ_1 (mp_builtin_len_obj , mp_builtin_len );
192+
193+ static mp_obj_t mp_builtin_max (int n_args , const mp_obj_t * args ) {
178194 if (n_args == 1 ) {
179195 // given an iterable
180196 mp_obj_t iterable = rt_getiter (args [0 ]);
@@ -201,7 +217,9 @@ mp_obj_t mp_builtin_max(int n_args, const mp_obj_t *args) {
201217 }
202218}
203219
204- mp_obj_t mp_builtin_min (int n_args , const mp_obj_t * args ) {
220+ MP_DEFINE_CONST_FUN_OBJ_VAR (mp_builtin_max_obj , 1 , mp_builtin_max );
221+
222+ static mp_obj_t mp_builtin_min (int n_args , const mp_obj_t * args ) {
205223 if (n_args == 1 ) {
206224 // given an iterable
207225 mp_obj_t iterable = rt_getiter (args [0 ]);
@@ -228,6 +246,8 @@ mp_obj_t mp_builtin_min(int n_args, const mp_obj_t *args) {
228246 }
229247}
230248
249+ MP_DEFINE_CONST_FUN_OBJ_VAR (mp_builtin_min_obj , 1 , mp_builtin_min );
250+
231251static mp_obj_t mp_builtin_next (mp_obj_t o ) {
232252 mp_obj_t ret = rt_iternext (o );
233253 if (ret == mp_const_stop_iteration ) {
@@ -239,7 +259,7 @@ static mp_obj_t mp_builtin_next(mp_obj_t o) {
239259
240260MP_DEFINE_CONST_FUN_OBJ_1 (mp_builtin_next_obj , mp_builtin_next );
241261
242- mp_obj_t mp_builtin_ord (mp_obj_t o_in ) {
262+ static mp_obj_t mp_builtin_ord (mp_obj_t o_in ) {
243263 const char * str = qstr_str (mp_obj_get_qstr (o_in ));
244264 if (strlen (str ) == 1 ) {
245265 return mp_obj_new_int (str [0 ]);
@@ -248,15 +268,19 @@ mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
248268 }
249269}
250270
251- mp_obj_t mp_builtin_pow (int n_args , const mp_obj_t * args ) {
271+ MP_DEFINE_CONST_FUN_OBJ_1 (mp_builtin_ord_obj , mp_builtin_ord );
272+
273+ static mp_obj_t mp_builtin_pow (int n_args , const mp_obj_t * args ) {
274+ assert (2 <= n_args && n_args <= 3 );
252275 switch (n_args ) {
253276 case 2 : return rt_binary_op (RT_BINARY_OP_POWER , args [0 ], args [1 ]);
254- case 3 : return rt_binary_op (RT_BINARY_OP_MODULO , rt_binary_op (RT_BINARY_OP_POWER , args [0 ], args [1 ]), args [2 ]); // TODO optimise...
255- default : nlr_jump (mp_obj_new_exception_msg_varg (MP_QSTR_TypeError , "pow expected at most 3 arguments, got %d" , n_args ));
277+ default : return rt_binary_op (RT_BINARY_OP_MODULO , rt_binary_op (RT_BINARY_OP_POWER , args [0 ], args [1 ]), args [2 ]); // TODO optimise...
256278 }
257279}
258280
259- mp_obj_t mp_builtin_print (int n_args , const mp_obj_t * args ) {
281+ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (mp_builtin_pow_obj , 2 , 3 , mp_builtin_pow );
282+
283+ static mp_obj_t mp_builtin_print (int n_args , const mp_obj_t * args ) {
260284 for (int i = 0 ; i < n_args ; i ++ ) {
261285 if (i > 0 ) {
262286 printf (" " );
@@ -273,21 +297,25 @@ mp_obj_t mp_builtin_print(int n_args, const mp_obj_t *args) {
273297 return mp_const_none ;
274298}
275299
276- mp_obj_t mp_builtin_range (int n_args , const mp_obj_t * args ) {
300+ MP_DEFINE_CONST_FUN_OBJ_VAR (mp_builtin_print_obj , 0 , mp_builtin_print );
301+
302+ static mp_obj_t mp_builtin_range (int n_args , const mp_obj_t * args ) {
303+ assert (1 <= n_args && n_args <= 3 );
277304 switch (n_args ) {
278305 case 1 : return mp_obj_new_range (0 , mp_obj_get_int (args [0 ]), 1 );
279306 case 2 : return mp_obj_new_range (mp_obj_get_int (args [0 ]), mp_obj_get_int (args [1 ]), 1 );
280- case 3 : return mp_obj_new_range (mp_obj_get_int (args [0 ]), mp_obj_get_int (args [1 ]), mp_obj_get_int (args [2 ]));
281- default : nlr_jump (mp_obj_new_exception_msg_varg (MP_QSTR_TypeError , "range expected at most 3 arguments, got %d" , n_args ));
307+ default : return mp_obj_new_range (mp_obj_get_int (args [0 ]), mp_obj_get_int (args [1 ]), mp_obj_get_int (args [2 ]));
282308 }
283309}
284310
285- mp_obj_t mp_builtin_sum (int n_args , const mp_obj_t * args ) {
311+ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (mp_builtin_range_obj , 1 , 3 , mp_builtin_range );
312+
313+ static mp_obj_t mp_builtin_sum (int n_args , const mp_obj_t * args ) {
314+ assert (1 <= n_args && n_args <= 2 );
286315 mp_obj_t value ;
287316 switch (n_args ) {
288317 case 1 : value = mp_obj_new_int (0 ); break ;
289- case 2 : value = args [1 ]; break ;
290- default : nlr_jump (mp_obj_new_exception_msg_varg (MP_QSTR_TypeError , "sum expected at most 2 arguments, got %d" , n_args ));
318+ default : value = args [1 ]; break ;
291319 }
292320 mp_obj_t iterable = rt_getiter (args [0 ]);
293321 mp_obj_t item ;
@@ -296,3 +324,5 @@ mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) {
296324 }
297325 return value ;
298326}
327+
328+ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (mp_builtin_sum_obj , 1 , 2 , mp_builtin_sum );
0 commit comments