Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Berry automatic rounding of float to int when calling C mapped functions #21601

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ All notable changes to this project will be documented in this file.
- Matter support for Air Quality sensors (#21559)
- Matter support for bridged Air Quality (#21597)
- HASPmota rounds to nearest int values passed as 'real' (#21599)
- Berry automatic rounding of float to int when calling C mapped functions

### Breaking Changed

Expand Down
17 changes: 16 additions & 1 deletion lib/libesp32/berry_mapping/src/be_class_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
#include "be_exec.h"
#include <string.h>
#include <stdlib.h>
#include <math.h>

#if BE_USE_SINGLE_FLOAT
#define mathfunc(func) func##f
#else
#define mathfunc(func) func
#endif

/* Ubuntu 22.04 LTS seems to have an invalid or missing signature for strtok_r, forcing a correct one */
extern char *strtok_r(char *str, const char *delim, char **saveptr);
Expand Down Expand Up @@ -227,7 +234,15 @@ intptr_t be_convert_single_elt(bvm *vm, int idx, const char * arg_type, int *buf
type_ok = type_ok || (arg_type[0] == provided_type && arg_type[1] == 0); // or type is a match (single char only)
type_ok = type_ok || (ret == 0 && arg_type_len != 1); // or NULL is accepted for an instance
type_ok = type_ok || (ret == 0 && arg_type[0] == 's' && arg_type[1] == 0); // accept nil for string, can be dangerous

if (!type_ok) {
if ((provided_type == 'f') && (arg_type[0] == 'i') && (arg_type[1] == 0)) {
// special case: float is accepted as int
breal v_real = be_toreal(vm, idx);
ret = mathfunc(round)(v_real);
provided_type = 'i';
type_ok = btrue;
}
}
if (!type_ok) {
be_raisef(vm, "type_error", "Unexpected argument type '%c', expected '%s'", provided_type, arg_type);
}
Expand Down