Skip to content

Commit c86e4db

Browse files
authored
Berry automatic rounding of float to int when calling C mapped functions (#21601)
1 parent bdc1596 commit c86e4db

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ All notable changes to this project will be documented in this file.
99
- Matter support for Air Quality sensors (#21559)
1010
- Matter support for bridged Air Quality (#21597)
1111
- HASPmota rounds to nearest int values passed as 'real' (#21599)
12+
- Berry automatic rounding of float to int when calling C mapped functions
1213

1314
### Breaking Changed
1415

lib/libesp32/berry_mapping/src/be_class_wrapper.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
#include "be_exec.h"
1414
#include <string.h>
1515
#include <stdlib.h>
16+
#include <math.h>
17+
18+
#if BE_USE_SINGLE_FLOAT
19+
#define mathfunc(func) func##f
20+
#else
21+
#define mathfunc(func) func
22+
#endif
1623

1724
/* Ubuntu 22.04 LTS seems to have an invalid or missing signature for strtok_r, forcing a correct one */
1825
extern char *strtok_r(char *str, const char *delim, char **saveptr);
@@ -227,7 +234,15 @@ intptr_t be_convert_single_elt(bvm *vm, int idx, const char * arg_type, int *buf
227234
type_ok = type_ok || (arg_type[0] == provided_type && arg_type[1] == 0); // or type is a match (single char only)
228235
type_ok = type_ok || (ret == 0 && arg_type_len != 1); // or NULL is accepted for an instance
229236
type_ok = type_ok || (ret == 0 && arg_type[0] == 's' && arg_type[1] == 0); // accept nil for string, can be dangerous
230-
237+
if (!type_ok) {
238+
if ((provided_type == 'f') && (arg_type[0] == 'i') && (arg_type[1] == 0)) {
239+
// special case: float is accepted as int
240+
breal v_real = be_toreal(vm, idx);
241+
ret = mathfunc(round)(v_real);
242+
provided_type = 'i';
243+
type_ok = btrue;
244+
}
245+
}
231246
if (!type_ok) {
232247
be_raisef(vm, "type_error", "Unexpected argument type '%c', expected '%s'", provided_type, arg_type);
233248
}

0 commit comments

Comments
 (0)