Skip to content

Commit e45c1db

Browse files
committed
py: Allow viper functions to take up to 4 arguments.
Addresses issue adafruit#1380.
1 parent 7693ef3 commit e45c1db

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

py/objfun.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ typedef mp_uint_t (*viper_fun_0_t)(void);
397397
typedef mp_uint_t (*viper_fun_1_t)(mp_uint_t);
398398
typedef mp_uint_t (*viper_fun_2_t)(mp_uint_t, mp_uint_t);
399399
typedef mp_uint_t (*viper_fun_3_t)(mp_uint_t, mp_uint_t, mp_uint_t);
400+
typedef mp_uint_t (*viper_fun_4_t)(mp_uint_t, mp_uint_t, mp_uint_t, mp_uint_t);
400401

401402
STATIC mp_obj_t fun_viper_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
402403
mp_obj_fun_viper_t *self = self_in;
@@ -414,7 +415,15 @@ STATIC mp_obj_t fun_viper_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_k
414415
ret = ((viper_fun_2_t)fun)(mp_convert_obj_to_native(args[0], self->type_sig >> 2), mp_convert_obj_to_native(args[1], self->type_sig >> 4));
415416
} else if (n_args == 3) {
416417
ret = ((viper_fun_3_t)fun)(mp_convert_obj_to_native(args[0], self->type_sig >> 2), mp_convert_obj_to_native(args[1], self->type_sig >> 4), mp_convert_obj_to_native(args[2], self->type_sig >> 6));
418+
} else if (n_args == 4) {
419+
ret = ((viper_fun_4_t)fun)(
420+
mp_convert_obj_to_native(args[0], self->type_sig >> 2),
421+
mp_convert_obj_to_native(args[1], self->type_sig >> 4),
422+
mp_convert_obj_to_native(args[2], self->type_sig >> 6),
423+
mp_convert_obj_to_native(args[3], self->type_sig >> 8)
424+
);
417425
} else {
426+
// TODO 5 or more arguments not supported for viper call
418427
assert(0);
419428
ret = 0;
420429
}

tests/micropython/viper_args.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# test calling viper functions with different number of args
2+
3+
@micropython.viper
4+
def f0():
5+
print(0)
6+
f0()
7+
8+
@micropython.viper
9+
def f1(x1:int):
10+
print(x1)
11+
f1(1)
12+
13+
@micropython.viper
14+
def f2(x1:int, x2:int):
15+
print(x1, x2)
16+
f2(1, 2)
17+
18+
@micropython.viper
19+
def f3(x1:int, x2:int, x3:int):
20+
print(x1, x2, x3)
21+
f3(1, 2, 3)
22+
23+
@micropython.viper
24+
def f4(x1:int, x2:int, x3:int, x4:int):
25+
print(x1, x2, x3, x4)
26+
f4(1, 2, 3, 4)
27+
28+
# only up to 4 arguments currently supported
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
0
2+
1
3+
1 2
4+
1 2 3
5+
1 2 3 4

0 commit comments

Comments
 (0)