diff --git a/CHANGELOG.md b/CHANGELOG.md index e1162e7e0..dd615b6e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ All notable changes to this project will be documented in this file. - ESP32 Platform from 2025.09.30 to 2025.10.30, Framework (Arduino Core) from v3.1.3.250808 to v3.1.4 and IDF from v5.3.3.250801 to v5.3.4.250826 (#23971) ### Fixed +- Berry fixed 'be_top is non zero' warning when calling C mapped functions ### Removed diff --git a/lib/libesp32/berry_mapping/src/be_class_wrapper.c b/lib/libesp32/berry_mapping/src/be_class_wrapper.c index 6fa399555..403b606f3 100644 --- a/lib/libesp32/berry_mapping/src/be_class_wrapper.c +++ b/lib/libesp32/berry_mapping/src/be_class_wrapper.c @@ -11,6 +11,7 @@ #include "be_mapping.h" #include "be_exec.h" +#include "be_vm.h" #include #include #include @@ -580,8 +581,17 @@ int be_call_c_func(bvm *vm, const void * func, const char * return_type, const c if (return_type != NULL && return_type[0] == '&') { if (c_args < 8) { p[c_args] = (intptr_t) &return_len; } } + // We do some special trickery here, we need to set be_top to zero + // but in the same time we need to keep argument '1' to set the instance + // later. So we increment first vm->reg and we set vm->top to the same value + uint32_t reg_save = (argc > 0) ? 1 : 0; + vm->reg += reg_save; + vm->top = vm->reg; intptr_t ret = 0; if (f) ret = (*f)(p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); + // now we restore vm->reg + vm->reg -= reg_save; + vm->top = vm->reg + reg_save; // berry_log_C("be_call_c_func '%s' -> '%s': (%i,%i,%i,%i,%i,%i) -> %i", return_type, arg_type, p[0], p[1], p[2], p[3], p[4], p[5], ret); if ((return_type == NULL) || (strlen(return_type) == 0)) { be_return_nil(vm); } // does not return