From 6b2304c43a0fccba2c640aa670b4a0e724164ce8 Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Sun, 9 Jun 2024 17:35:43 +0200 Subject: [PATCH] Berry automatic rounding of float to int when calling C mapped functions --- CHANGELOG.md | 1 + .../berry_mapping/src/be_class_wrapper.c | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85f5e02321d7..e85c1ab4efe8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/libesp32/berry_mapping/src/be_class_wrapper.c b/lib/libesp32/berry_mapping/src/be_class_wrapper.c index 1c567306be65..0e06ec1de5e4 100644 --- a/lib/libesp32/berry_mapping/src/be_class_wrapper.c +++ b/lib/libesp32/berry_mapping/src/be_class_wrapper.c @@ -13,6 +13,13 @@ #include "be_exec.h" #include #include +#include + +#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); @@ -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); }