Skip to content

Commit f62d33a

Browse files
committed
Consolidate rt_make_function_[0123] to rt_make_function_n.
1 parent f88a72a commit f62d33a

9 files changed

Lines changed: 47 additions & 84 deletions

File tree

py/objfun.c

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -98,42 +98,13 @@ const mp_obj_type_t fun_native_type = {
9898
.call_n_kw = fun_native_call_n_kw,
9999
};
100100

101-
mp_obj_t rt_make_function_0(mp_fun_0_t fun) {
101+
// fun must have the correct signature for n_args fixed arguments
102+
mp_obj_t rt_make_function_n(int n_args, void *fun) {
102103
mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
103104
o->base.type = &fun_native_type;
104105
o->is_kw = false;
105-
o->n_args_min = 0;
106-
o->n_args_max = 0;
107-
o->fun = fun;
108-
return o;
109-
}
110-
111-
mp_obj_t rt_make_function_1(mp_fun_1_t fun) {
112-
mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
113-
o->base.type = &fun_native_type;
114-
o->is_kw = false;
115-
o->n_args_min = 1;
116-
o->n_args_max = 1;
117-
o->fun = fun;
118-
return o;
119-
}
120-
121-
mp_obj_t rt_make_function_2(mp_fun_2_t fun) {
122-
mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
123-
o->base.type = &fun_native_type;
124-
o->is_kw = false;
125-
o->n_args_min = 2;
126-
o->n_args_max = 2;
127-
o->fun = fun;
128-
return o;
129-
}
130-
131-
mp_obj_t rt_make_function_3(mp_fun_3_t fun) {
132-
mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
133-
o->base.type = &fun_native_type;
134-
o->is_kw = false;
135-
o->n_args_min = 3;
136-
o->n_args_max = 3;
106+
o->n_args_min = n_args;
107+
o->n_args_max = n_args;
137108
o->fun = fun;
138109
return o;
139110
}

py/objstr.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include <stdarg.h>
44
#include <string.h>
55
#include <assert.h>
6-
#include <sys/types.h>
76

87
#include "nlr.h"
98
#include "misc.h"

py/runtime.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -587,12 +587,7 @@ mp_obj_t rt_make_function_from_id(int unique_code_id) {
587587
fun = mp_obj_new_fun_bc(c->n_args, c->n_locals + c->n_stack, c->u_byte.code);
588588
break;
589589
case MP_CODE_NATIVE:
590-
switch (c->n_args) {
591-
case 0: fun = rt_make_function_0(c->u_native.fun); break;
592-
case 1: fun = rt_make_function_1((mp_fun_1_t)c->u_native.fun); break;
593-
case 2: fun = rt_make_function_2((mp_fun_2_t)c->u_native.fun); break;
594-
default: assert(0); fun = mp_const_none;
595-
}
590+
fun = rt_make_function_n(c->n_args, c->u_native.fun);
596591
break;
597592
case MP_CODE_INLINE_ASM:
598593
fun = mp_obj_new_fun_asm(c->n_args, c->u_inline_asm.fun);

py/runtime.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ void rt_store_global(qstr qstr, mp_obj_t obj);
1212
mp_obj_t rt_unary_op(int op, mp_obj_t arg);
1313
mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs);
1414
mp_obj_t rt_make_function_from_id(int unique_code_id);
15-
mp_obj_t rt_make_function_0(mp_fun_0_t f);
16-
mp_obj_t rt_make_function_1(mp_fun_1_t f);
17-
mp_obj_t rt_make_function_2(mp_fun_2_t f);
18-
mp_obj_t rt_make_function_var(int n_args_min, mp_fun_var_t f);
15+
mp_obj_t rt_make_function_n(int n_args, void *fun); // fun must have the correct signature for n_args fixed arguments
16+
mp_obj_t rt_make_function_var(int n_args_min, mp_fun_var_t fun);
1917
mp_obj_t rt_make_function_var_between(int n_args_min, int n_args_max, mp_fun_var_t fun); // min and max are inclusive
2018
mp_obj_t rt_make_closure_from_id(int unique_code_id, mp_obj_t closure_tuple);
2119
mp_obj_t rt_call_function_0(mp_obj_t fun);

stm/audio.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ void audio_init(void) {
9191

9292
// Python interface
9393
mp_obj_t m = mp_obj_new_module(qstr_from_str_static("audio"));
94-
rt_store_attr(m, qstr_from_str_static("dac"), rt_make_function_1(pyb_audio_dac));
95-
rt_store_attr(m, qstr_from_str_static("is_full"), rt_make_function_0(pyb_audio_is_full));
96-
rt_store_attr(m, qstr_from_str_static("fill"), rt_make_function_1(pyb_audio_fill));
94+
rt_store_attr(m, qstr_from_str_static("dac"), rt_make_function_n(1, pyb_audio_dac));
95+
rt_store_attr(m, qstr_from_str_static("is_full"), rt_make_function_n(0, pyb_audio_is_full));
96+
rt_store_attr(m, qstr_from_str_static("fill"), rt_make_function_n(1, pyb_audio_fill));
9797
rt_store_name(qstr_from_str_static("audio"), m);
9898
}

stm/lcd.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,13 @@ void lcd_init(void) {
220220

221221
// Python interface
222222
mp_obj_t m = mp_obj_new_module(qstr_from_str_static("lcd"));
223-
rt_store_attr(m, qstr_from_str_static("lcd8"), rt_make_function_2(lcd_draw_pixel_8));
224-
rt_store_attr(m, qstr_from_str_static("clear"), rt_make_function_0(lcd_pix_clear));
225-
rt_store_attr(m, qstr_from_str_static("get"), rt_make_function_2(lcd_pix_get));
226-
rt_store_attr(m, qstr_from_str_static("set"), rt_make_function_2(lcd_pix_set));
227-
rt_store_attr(m, qstr_from_str_static("reset"), rt_make_function_2(lcd_pix_reset));
228-
rt_store_attr(m, qstr_from_str_static("show"), rt_make_function_0(lcd_pix_show));
229-
rt_store_attr(m, qstr_from_str_static("text"), rt_make_function_1(lcd_print));
223+
rt_store_attr(m, qstr_from_str_static("lcd8"), rt_make_function_n(2, lcd_draw_pixel_8));
224+
rt_store_attr(m, qstr_from_str_static("clear"), rt_make_function_n(0, lcd_pix_clear));
225+
rt_store_attr(m, qstr_from_str_static("get"), rt_make_function_n(2, lcd_pix_get));
226+
rt_store_attr(m, qstr_from_str_static("set"), rt_make_function_n(2, lcd_pix_set));
227+
rt_store_attr(m, qstr_from_str_static("reset"), rt_make_function_n(2, lcd_pix_reset));
228+
rt_store_attr(m, qstr_from_str_static("show"), rt_make_function_n(0, lcd_pix_show));
229+
rt_store_attr(m, qstr_from_str_static("text"), rt_make_function_n(1, lcd_print));
230230
rt_store_name(qstr_from_str_static("lcd"), m);
231231
}
232232

stm/main.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -812,36 +812,36 @@ int main(void) {
812812

813813
// add some functions to the python namespace
814814
{
815-
rt_store_name(qstr_from_str_static("help"), rt_make_function_0(pyb_help));
815+
rt_store_name(qstr_from_str_static("help"), rt_make_function_n(0, pyb_help));
816816

817817
mp_obj_t m = mp_obj_new_module(qstr_from_str_static("pyb"));
818-
rt_store_attr(m, qstr_from_str_static("info"), rt_make_function_0(pyb_info));
819-
rt_store_attr(m, qstr_from_str_static("sd_test"), rt_make_function_0(pyb_sd_test));
820-
rt_store_attr(m, qstr_from_str_static("stop"), rt_make_function_0(pyb_stop));
821-
rt_store_attr(m, qstr_from_str_static("standby"), rt_make_function_0(pyb_standby));
822-
rt_store_attr(m, qstr_from_str_static("source_dir"), rt_make_function_1(pyb_source_dir));
823-
rt_store_attr(m, qstr_from_str_static("main"), rt_make_function_1(pyb_main));
824-
rt_store_attr(m, qstr_from_str_static("sync"), rt_make_function_0(pyb_sync));
825-
rt_store_attr(m, qstr_from_str_static("gc"), rt_make_function_0(pyb_gc));
826-
rt_store_attr(m, qstr_from_str_static("delay"), rt_make_function_1(pyb_delay));
827-
rt_store_attr(m, qstr_from_str_static("led"), rt_make_function_1(pyb_led));
818+
rt_store_attr(m, qstr_from_str_static("info"), rt_make_function_n(0, pyb_info));
819+
rt_store_attr(m, qstr_from_str_static("sd_test"), rt_make_function_n(0, pyb_sd_test));
820+
rt_store_attr(m, qstr_from_str_static("stop"), rt_make_function_n(0, pyb_stop));
821+
rt_store_attr(m, qstr_from_str_static("standby"), rt_make_function_n(0, pyb_standby));
822+
rt_store_attr(m, qstr_from_str_static("source_dir"), rt_make_function_n(1, pyb_source_dir));
823+
rt_store_attr(m, qstr_from_str_static("main"), rt_make_function_n(1, pyb_main));
824+
rt_store_attr(m, qstr_from_str_static("sync"), rt_make_function_n(0, pyb_sync));
825+
rt_store_attr(m, qstr_from_str_static("gc"), rt_make_function_n(0, pyb_gc));
826+
rt_store_attr(m, qstr_from_str_static("delay"), rt_make_function_n(1, pyb_delay));
827+
rt_store_attr(m, qstr_from_str_static("led"), rt_make_function_n(1, pyb_led));
828828
rt_store_attr(m, qstr_from_str_static("switch"), (mp_obj_t)&pyb_switch_obj);
829-
rt_store_attr(m, qstr_from_str_static("servo"), rt_make_function_2(pyb_servo_set));
830-
rt_store_attr(m, qstr_from_str_static("pwm"), rt_make_function_2(pyb_pwm_set));
829+
rt_store_attr(m, qstr_from_str_static("servo"), rt_make_function_n(2, pyb_servo_set));
830+
rt_store_attr(m, qstr_from_str_static("pwm"), rt_make_function_n(2, pyb_pwm_set));
831831
rt_store_attr(m, qstr_from_str_static("accel"), (mp_obj_t)&pyb_mma_read_obj);
832832
rt_store_attr(m, qstr_from_str_static("mma_read"), (mp_obj_t)&pyb_mma_read_all_obj);
833833
rt_store_attr(m, qstr_from_str_static("mma_mode"), (mp_obj_t)&pyb_mma_write_mode_obj);
834-
rt_store_attr(m, qstr_from_str_static("hid"), rt_make_function_1(pyb_hid_send_report));
835-
rt_store_attr(m, qstr_from_str_static("time"), rt_make_function_0(pyb_rtc_read));
836-
rt_store_attr(m, qstr_from_str_static("rand"), rt_make_function_0(pyb_rng_get));
837-
rt_store_attr(m, qstr_from_str_static("Led"), rt_make_function_1(pyb_Led));
838-
rt_store_attr(m, qstr_from_str_static("Servo"), rt_make_function_1(pyb_Servo));
839-
rt_store_attr(m, qstr_from_str_static("I2C"), rt_make_function_2(pyb_I2C));
834+
rt_store_attr(m, qstr_from_str_static("hid"), rt_make_function_n(1, pyb_hid_send_report));
835+
rt_store_attr(m, qstr_from_str_static("time"), rt_make_function_n(0, pyb_rtc_read));
836+
rt_store_attr(m, qstr_from_str_static("rand"), rt_make_function_n(0, pyb_rng_get));
837+
rt_store_attr(m, qstr_from_str_static("Led"), rt_make_function_n(1, pyb_Led));
838+
rt_store_attr(m, qstr_from_str_static("Servo"), rt_make_function_n(1, pyb_Servo));
839+
rt_store_attr(m, qstr_from_str_static("I2C"), rt_make_function_n(2, pyb_I2C));
840840
rt_store_attr(m, qstr_from_str_static("gpio"), (mp_obj_t)&pyb_gpio_obj);
841-
rt_store_attr(m, qstr_from_str_static("Usart"), rt_make_function_2(pyb_Usart));
841+
rt_store_attr(m, qstr_from_str_static("Usart"), rt_make_function_n(2, pyb_Usart));
842842
rt_store_name(qstr_from_str_static("pyb"), m);
843843

844-
rt_store_name(qstr_from_str_static("open"), rt_make_function_2(pyb_io_open));
844+
rt_store_name(qstr_from_str_static("open"), rt_make_function_n(2, pyb_io_open));
845845
}
846846

847847
// print a message to the LCD

stm/pybwlan.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -357,11 +357,11 @@ void pyb_wlan_init(void) {
357357

358358
mp_obj_t m = mp_obj_new_module(qstr_from_str_static("wlan"));
359359
rt_store_attr(m, qstr_from_str_static("connect"), rt_make_function_var(0, pyb_wlan_connect));
360-
rt_store_attr(m, qstr_from_str_static("disconnect"), rt_make_function_0(pyb_wlan_disconnect));
361-
rt_store_attr(m, qstr_from_str_static("ip"), rt_make_function_0(pyb_wlan_get_ip));
362-
rt_store_attr(m, qstr_from_str_static("get_host"), rt_make_function_1(pyb_wlan_get_host));
363-
rt_store_attr(m, qstr_from_str_static("http_get"), rt_make_function_2(pyb_wlan_http_get));
364-
rt_store_attr(m, qstr_from_str_static("serve"), rt_make_function_0(pyb_wlan_serve));
360+
rt_store_attr(m, qstr_from_str_static("disconnect"), rt_make_function_n(0, pyb_wlan_disconnect));
361+
rt_store_attr(m, qstr_from_str_static("ip"), rt_make_function_n(0, pyb_wlan_get_ip));
362+
rt_store_attr(m, qstr_from_str_static("get_host"), rt_make_function_n(1, pyb_wlan_get_host));
363+
rt_store_attr(m, qstr_from_str_static("http_get"), rt_make_function_n(2, pyb_wlan_http_get));
364+
rt_store_attr(m, qstr_from_str_static("serve"), rt_make_function_n(0, pyb_wlan_serve));
365365
rt_store_name(qstr_from_str_static("wlan"), m);
366366
}
367367

stm/timer.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ void timer_init(void) {
7272

7373
// Python interface
7474
mp_obj_t m = mp_obj_new_module(qstr_from_str_static("timer"));
75-
rt_store_attr(m, qstr_from_str_static("callback"), rt_make_function_1(timer_py_set_callback));
76-
rt_store_attr(m, qstr_from_str_static("period"), rt_make_function_1(timer_py_set_period));
77-
rt_store_attr(m, qstr_from_str_static("prescaler"), rt_make_function_1(timer_py_set_prescaler));
78-
rt_store_attr(m, qstr_from_str_static("value"), rt_make_function_0(timer_py_get_value));
75+
rt_store_attr(m, qstr_from_str_static("callback"), rt_make_function_n(1, timer_py_set_callback));
76+
rt_store_attr(m, qstr_from_str_static("period"), rt_make_function_n(1, timer_py_set_period));
77+
rt_store_attr(m, qstr_from_str_static("prescaler"), rt_make_function_n(1, timer_py_set_prescaler));
78+
rt_store_attr(m, qstr_from_str_static("value"), rt_make_function_n(0, timer_py_get_value));
7979
rt_store_name(qstr_from_str_static("timer"), m);
8080
}
8181

0 commit comments

Comments
 (0)