From 2ce936db94ad1b036085be70bda1c26f18c4a23b Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Tue, 4 Oct 2022 20:11:38 +1100 Subject: [PATCH] AP_Scripting: fixed use of clock and time in lua not available on stm32 --- libraries/AP_Scripting/lua/src/lauxlib.c | 12 ++++++++++++ libraries/AP_Scripting/lua/src/lauxlib.h | 2 ++ libraries/AP_Scripting/lua/src/lstate.c | 5 +++++ libraries/AP_Scripting/lua/src/ltablib.c | 4 ++++ 4 files changed, 23 insertions(+) diff --git a/libraries/AP_Scripting/lua/src/lauxlib.c b/libraries/AP_Scripting/lua/src/lauxlib.c index 0822d94959..1b3872e309 100644 --- a/libraries/AP_Scripting/lua/src/lauxlib.c +++ b/libraries/AP_Scripting/lua/src/lauxlib.c @@ -1044,3 +1044,15 @@ LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) { (LUAI_UACNUMBER)ver, (LUAI_UACNUMBER)*v); } + +/* + 32 bit random number generator for lua internals + */ +uint32_t lua_random32(void) +{ + static uint32_t m_z = 1234; + static uint32_t m_w = 76542; + m_z = 36969 * (m_z & 0xFFFFu) + (m_z >> 16); + m_w = 18000 * (m_w & 0xFFFFu) + (m_w >> 16); + return ((m_z << 16) + m_w); +} diff --git a/libraries/AP_Scripting/lua/src/lauxlib.h b/libraries/AP_Scripting/lua/src/lauxlib.h index 9857d3a835..6a6195a5ba 100644 --- a/libraries/AP_Scripting/lua/src/lauxlib.h +++ b/libraries/AP_Scripting/lua/src/lauxlib.h @@ -36,6 +36,8 @@ typedef struct luaL_Reg { #define LUAL_NUMSIZES (sizeof(lua_Integer)*16 + sizeof(lua_Number)) +uint32_t lua_random32(void); + LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver, size_t sz); #define luaL_checkversion(L) \ luaL_checkversion_(L, LUA_VERSION_NUM, LUAL_NUMSIZES) diff --git a/libraries/AP_Scripting/lua/src/lstate.c b/libraries/AP_Scripting/lua/src/lstate.c index 25580bff24..796deb781b 100644 --- a/libraries/AP_Scripting/lua/src/lstate.c +++ b/libraries/AP_Scripting/lua/src/lstate.c @@ -26,6 +26,7 @@ #include "lstring.h" #include "ltable.h" #include "ltm.h" +#include "lauxlib.h" // lua code does lots of casting, these warnings are not helpful #pragma GCC diagnostic ignored "-Wcast-align" @@ -44,10 +45,14 @@ ** a macro to help the creation of a unique random seed when a state is ** created; the seed is used to randomize hashes. */ +#if defined(ARDUPILOT_BUILD) +#define luai_makeseed() lua_random32() +#else #if !defined(luai_makeseed) #include #define luai_makeseed() cast(unsigned int, time(NULL)) #endif +#endif diff --git a/libraries/AP_Scripting/lua/src/ltablib.c b/libraries/AP_Scripting/lua/src/ltablib.c index c5349578ec..d2b04fe5e2 100644 --- a/libraries/AP_Scripting/lua/src/ltablib.c +++ b/libraries/AP_Scripting/lua/src/ltablib.c @@ -256,6 +256,9 @@ typedef unsigned int IdxT; ** is to copy them to an array of a known type and use the array values. */ static unsigned int l_randomizePivot (void) { +#if defined(ARDUPILOT_BUILD) + return lua_random32(); +#else clock_t c = clock(); time_t t = time(NULL); unsigned int buff[sof(c) + sof(t)]; @@ -265,6 +268,7 @@ static unsigned int l_randomizePivot (void) { for (i = 0; i < sof(buff); i++) rnd += buff[i]; return rnd; +#endif } #endif /* } */