Skip to content

Instantly share code, notes, and snippets.

@tridge
Created December 13, 2022 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tridge/5d52813cdd0c14ea389fea576852b2d3 to your computer and use it in GitHub Desktop.
Save tridge/5d52813cdd0c14ea389fea576852b2d3 to your computer and use it in GitHub Desktop.
ArduPilot lua differences from lua 5.4.4
diff -urN lua/lauxlib.c APM/libraries/AP_Scripting/lua/src/lauxlib.c
--- lua/lauxlib.c 2022-12-14 08:41:37.443587208 +1100
+++ APM/libraries/AP_Scripting/lua/src/lauxlib.c 2022-12-14 10:11:20.343728284 +1100
@@ -32,7 +32,6 @@
#define MAX_SIZET ((size_t)(~(size_t)0))
#endif
-
/*
** {======================================================
** Traceback
@@ -790,11 +789,13 @@
}
if (skipcomment(&lf, &c)) /* read initial portion */
lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */
+#if LUA_SUPPORT_LOAD_BINARY
if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */
lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */
if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
skipcomment(&lf, &c); /* re-read initial portion */
}
+#endif
if (c != EOF)
lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */
status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode);
@@ -1008,6 +1009,8 @@
}
+#if !defined(ARDUPILOT_BUILD)
+
static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
(void)ud; (void)osize; /* not used */
if (nsize == 0) {
@@ -1027,6 +1030,7 @@
return 0; /* return to Lua to abort */
}
+#endif // ARDUPILOT_BUILD
/*
** Warning functions:
@@ -1084,6 +1088,7 @@
warnfcont(ud, message, tocont); /* finish processing */
}
+#if !defined(ARDUPILOT_BUILD)
LUALIB_API lua_State *luaL_newstate (void) {
lua_State *L = lua_newstate(l_alloc, NULL);
@@ -1094,6 +1099,8 @@
return L;
}
+#endif // ARDUPILOT_BUILD
+
LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) {
lua_Number v = lua_version(L);
@@ -1104,3 +1111,16 @@
(LUAI_UACNUMBER)ver, (LUAI_UACNUMBER)v);
}
+#ifdef ARDUPILOT_BUILD
+/*
+ 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);
+}
+#endif // ARDUPILOT_BUILD
diff -urN lua/lauxlib.h APM/libraries/AP_Scripting/lua/src/lauxlib.h
--- lua/lauxlib.h 2022-12-14 08:41:37.443587208 +1100
+++ APM/libraries/AP_Scripting/lua/src/lauxlib.h 2022-12-14 10:00:39.144339334 +1100
@@ -43,6 +43,10 @@
#define LUAL_NUMSIZES (sizeof(lua_Integer)*16 + sizeof(lua_Number))
+#ifdef ARDUPILOT_BUILD
+uint32_t lua_random32(void);
+#endif
+
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 -urN lua/lbaselib.c APM/libraries/AP_Scripting/lua/src/lbaselib.c
--- lua/lbaselib.c 2022-12-14 08:41:37.443587208 +1100
+++ APM/libraries/AP_Scripting/lua/src/lbaselib.c 2022-12-14 10:00:39.144339334 +1100
@@ -20,6 +20,9 @@
#include "lauxlib.h"
#include "lualib.h"
+#if defined(ARDUPILOT_BUILD)
+#pragma GCC diagnostic ignored "-Wunused-function"
+#endif
static int luaB_print (lua_State *L) {
int n = lua_gettop(L); /* number of arguments */
@@ -505,16 +508,23 @@
static const luaL_Reg base_funcs[] = {
{"assert", luaB_assert},
+#ifndef ARDUPILOT_BUILD
{"collectgarbage", luaB_collectgarbage},
{"dofile", luaB_dofile},
+#endif
{"error", luaB_error},
+#ifndef ARDUPILOT_BUILD
{"getmetatable", luaB_getmetatable},
+#endif
{"ipairs", luaB_ipairs},
+#ifndef ARDUPILOT_BUILD
{"loadfile", luaB_loadfile},
+#endif
{"load", luaB_load},
{"next", luaB_next},
{"pairs", luaB_pairs},
{"pcall", luaB_pcall},
+#ifndef ARDUPILOT_BUILD
{"print", luaB_print},
{"warn", luaB_warn},
{"rawequal", luaB_rawequal},
@@ -522,14 +532,17 @@
{"rawget", luaB_rawget},
{"rawset", luaB_rawset},
{"select", luaB_select},
+#endif
{"setmetatable", luaB_setmetatable},
{"tonumber", luaB_tonumber},
{"tostring", luaB_tostring},
{"type", luaB_type},
+#ifndef ARDUPILOT_BUILD
{"xpcall", luaB_xpcall},
/* placeholders */
{LUA_GNAME, NULL},
{"_VERSION", NULL},
+#endif
{NULL, NULL}
};
@@ -547,3 +560,11 @@
return 1;
}
+#ifdef ARDUPILOT_BUILD
+LUAMOD_API int luaopen_base_sandbox(lua_State *L) {
+ luaL_setfuncs(L, base_funcs, 0);
+ return 1;
+}
+#endif
+
+
diff -urN lua/lcode.c APM/libraries/AP_Scripting/lua/src/lcode.c
--- lua/lcode.c 2022-12-14 08:41:37.443587208 +1100
+++ APM/libraries/AP_Scripting/lua/src/lcode.c 2022-12-14 10:11:45.644021883 +1100
@@ -7,6 +7,13 @@
#define lcode_c
#define LUA_CORE
+#if defined(ARDUPILOT_BUILD)
+#pragma GCC diagnostic ignored "-Wfloat-equal"
+#if defined(__GNUC__) && __GNUC__ >= 7 || defined(__clang_major__) && __clang_major__ >= 10
+#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
+#endif
+#endif
+
#include "lprefix.h"
diff -urN lua/ldo.c APM/libraries/AP_Scripting/lua/src/ldo.c
--- lua/ldo.c 2022-12-14 08:41:37.447587255 +1100
+++ APM/libraries/AP_Scripting/lua/src/ldo.c 2022-12-14 10:12:09.716301309 +1100
@@ -962,11 +962,15 @@
LClosure *cl;
struct SParser *p = cast(struct SParser *, ud);
int c = zgetc(p->z); /* read first character */
+#if LUA_SUPPORT_LOAD_BINARY
+ // support loading pre-compiled luac
if (c == LUA_SIGNATURE[0]) {
checkmode(L, p->mode, "binary");
cl = luaU_undump(L, p->z, p->name);
}
- else {
+ else
+#endif
+ {
checkmode(L, p->mode, "text");
cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c);
}
diff -urN lua/lfunc.h APM/libraries/AP_Scripting/lua/src/lfunc.h
--- lua/lfunc.h 2022-12-14 08:41:37.447587255 +1100
+++ APM/libraries/AP_Scripting/lua/src/lfunc.h 2022-12-14 10:00:39.144339334 +1100
@@ -26,7 +26,11 @@
** maximum number of upvalues in a closure (both C and Lua). (Value
** must fit in a VM register.)
*/
+#ifndef ARDUPILOT_BUILD
+#define MAXUPVAL 127
+#else
#define MAXUPVAL 255
+#endif
#define upisopen(up) ((up)->v != &(up)->u.value)
diff -urN lua/linit.c APM/libraries/AP_Scripting/lua/src/linit.c
--- lua/linit.c 2022-12-14 08:41:37.447587255 +1100
+++ APM/libraries/AP_Scripting/lua/src/linit.c 2022-12-14 10:00:39.144339334 +1100
@@ -41,15 +41,21 @@
*/
static const luaL_Reg loadedlibs[] = {
{LUA_GNAME, luaopen_base},
+#ifndef ARDUPILOT_BUILD
{LUA_LOADLIBNAME, luaopen_package},
{LUA_COLIBNAME, luaopen_coroutine},
+#endif
{LUA_TABLIBNAME, luaopen_table},
{LUA_IOLIBNAME, luaopen_io},
+#ifndef ARDUPILOT_BUILD
{LUA_OSLIBNAME, luaopen_os},
+#endif
{LUA_STRLIBNAME, luaopen_string},
{LUA_MATHLIBNAME, luaopen_math},
{LUA_UTF8LIBNAME, luaopen_utf8},
+#ifndef ARDUPILOT_BUILD
{LUA_DBLIBNAME, luaopen_debug},
+#endif
{NULL, NULL}
};
diff -urN lua/liolib.c APM/libraries/AP_Scripting/lua/src/liolib.c
--- lua/liolib.c 2022-12-14 08:41:37.447587255 +1100
+++ APM/libraries/AP_Scripting/lua/src/liolib.c 2022-12-14 10:00:39.144339334 +1100
@@ -22,7 +22,9 @@
#include "lauxlib.h"
#include "lualib.h"
-
+#if defined(ARDUPILOT_BUILD)
+#pragma GCC diagnostic ignored "-Wunused-function"
+#endif
/*
@@ -713,6 +715,7 @@
}
+#ifndef ARDUPILOT_BUILD
static int f_setvbuf (lua_State *L) {
static const int mode[] = {_IONBF, _IOFBF, _IOLBF};
static const char *const modenames[] = {"no", "full", "line", NULL};
@@ -722,7 +725,7 @@
int res = setvbuf(f, NULL, mode[op], (size_t)sz);
return luaL_fileresult(L, res == 0, NULL);
}
-
+#endif // ARDUPILOT_BUILD
static int io_flush (lua_State *L) {
@@ -745,9 +748,13 @@
{"lines", io_lines},
{"open", io_open},
{"output", io_output},
+#ifndef ARDUPILOT_BUILD
{"popen", io_popen},
+#endif
{"read", io_read},
+#ifndef ARDUPILOT_BUILD
{"tmpfile", io_tmpfile},
+#endif
{"type", io_type},
{"write", io_write},
{NULL, NULL}
@@ -764,7 +771,9 @@
{"flush", f_flush},
{"seek", f_seek},
{"close", f_close},
+#ifndef ARDUPILOT_BUILD
{"setvbuf", f_setvbuf},
+#endif
{NULL, NULL}
};
diff -urN lua/lmathlib.c APM/libraries/AP_Scripting/lua/src/lmathlib.c
--- lua/lmathlib.c 2022-12-14 08:41:37.451587301 +1100
+++ APM/libraries/AP_Scripting/lua/src/lmathlib.c 2022-12-14 10:12:44.656707050 +1100
@@ -609,7 +609,11 @@
** randomization).
*/
static void randseed (lua_State *L, RanState *state) {
+#ifdef ARDUPILOT_BUILD
+ lua_Unsigned seed1 = (lua_Unsigned)lua_random32();
+#else
lua_Unsigned seed1 = (lua_Unsigned)time(NULL);
+#endif
lua_Unsigned seed2 = (lua_Unsigned)(size_t)L;
setseed(L, state->s, seed1, seed2);
}
diff -urN lua/lparser.c APM/libraries/AP_Scripting/lua/src/lparser.c
--- lua/lparser.c 2022-12-14 08:41:37.451587301 +1100
+++ APM/libraries/AP_Scripting/lua/src/lparser.c 2022-12-14 10:12:54.072816421 +1100
@@ -32,7 +32,11 @@
/* maximum number of local variables per function (must be smaller
than 250, due to the bytecode format) */
+#ifdef ARDUPILOT_BUILD
+#define MAXVARS 100
+#else
#define MAXVARS 200
+#endif
#define hasmultret(k) ((k) == VCALL || (k) == VVARARG)
diff -urN lua/lprefix.h APM/libraries/AP_Scripting/lua/src/lprefix.h
--- lua/lprefix.h 2022-12-14 08:41:37.451587301 +1100
+++ APM/libraries/AP_Scripting/lua/src/lprefix.h 2022-12-14 10:00:39.148339381 +1100
@@ -43,3 +43,11 @@
#endif
+#ifdef ARDUPILOT_BUILD
+// load posix compatibility functions
+#include <AP_Filesystem/posix_compat.h>
+
+#define lua_writestring(s,l) printf("%s", s)
+#define lua_writestringerror(s,l) lua_writestring(s,l)
+#endif // ARDUPILOT_BUILD
+
diff -urN lua/lstate.c APM/libraries/AP_Scripting/lua/src/lstate.c
--- lua/lstate.c 2022-12-14 08:41:37.451587301 +1100
+++ APM/libraries/AP_Scripting/lua/src/lstate.c 2022-12-14 10:13:29.645229708 +1100
@@ -26,6 +26,30 @@
#include "lstring.h"
#include "ltable.h"
#include "ltm.h"
+#include "lauxlib.h"
+
+#ifdef ARDUPILOT_BUILD
+#if !defined(LUAI_GCPAUSE)
+#define LUAI_GCPAUSE 200 /* 200% */
+#endif
+
+#if !defined(LUAI_GCMUL)
+#define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */
+#endif
+#endif // ARDUPILOT_BUILD
+
+/*
+** 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(L) lua_random32()
+#else
+#if !defined(luai_makeseed)
+#include <time.h>
+#define luai_makeseed(L) cast(unsigned int, time(NULL))
+#endif
+#endif
@@ -70,7 +94,11 @@
static unsigned int luai_makeseed (lua_State *L) {
char buff[3 * sizeof(size_t)];
+#ifdef ARDUPILOT_BUILD
+ unsigned int h = cast_uint(lua_random32());
+#else
unsigned int h = cast_uint(time(NULL));
+#endif
int p = 0;
addbuff(buff, p, L); /* heap variable */
addbuff(buff, p, &h); /* local variable */
diff -urN lua/lstring.c APM/libraries/AP_Scripting/lua/src/lstring.c
--- lua/lstring.c 2022-12-14 08:41:37.455587347 +1100
+++ APM/libraries/AP_Scripting/lua/src/lstring.c 2022-12-14 10:13:41.925372416 +1100
@@ -149,7 +149,12 @@
ts = gco2ts(o);
ts->hash = h;
ts->extra = 0;
+#pragma GCC diagnostic push
+#if defined(__GNUC__) && __GNUC__ >= 10
+#pragma GCC diagnostic ignored "-Wstringop-overflow"
+#endif
getstr(ts)[l] = '\0'; /* ending 0 */
+#pragma GCC diagnostic pop
return ts;
}
diff -urN lua/lstring.h APM/libraries/AP_Scripting/lua/src/lstring.h
--- lua/lstring.h 2022-12-14 08:41:37.455587347 +1100
+++ APM/libraries/AP_Scripting/lua/src/lstring.h 2022-12-14 10:00:39.148339381 +1100
@@ -16,7 +16,11 @@
** Memory-allocation error message must be preallocated (it cannot
** be created after memory is exhausted)
*/
+#ifdef ARDUPILOT_BUILD
+#define MEMERRMSG "not enough mem, increase SCR_HEAP_SIZE"
+#else
#define MEMERRMSG "not enough memory"
+#endif
/*
diff -urN lua/lstrlib.c APM/libraries/AP_Scripting/lua/src/lstrlib.c
--- lua/lstrlib.c 2022-12-14 08:41:37.455587347 +1100
+++ APM/libraries/AP_Scripting/lua/src/lstrlib.c 2022-12-14 10:00:39.148339381 +1100
@@ -25,6 +25,13 @@
#include "lauxlib.h"
#include "lualib.h"
+#if defined(ARDUPILOT_BUILD)
+#pragma GCC diagnostic ignored "-Wunused-function"
+#if defined(__GNUC__) && __GNUC__ >= 7 || defined(__clang_major__) && __clang_major__ >= 10
+#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
+#endif
+#endif
+
/*
** maximum number of captures that a pattern can do during
@@ -1830,7 +1837,9 @@
static const luaL_Reg strlib[] = {
{"byte", str_byte},
{"char", str_char},
+#ifndef ARDUPILOT_BUILD
{"dump", str_dump},
+#endif
{"find", str_find},
{"format", str_format},
{"gmatch", gmatch},
diff -urN lua/ltable.c APM/libraries/AP_Scripting/lua/src/ltable.c
--- lua/ltable.c 2022-12-14 08:41:37.455587347 +1100
+++ APM/libraries/AP_Scripting/lua/src/ltable.c 2022-12-14 10:13:56.877546202 +1100
@@ -7,6 +7,13 @@
#define ltable_c
#define LUA_CORE
+#if defined(ARDUPILOT_BUILD)
+#pragma GCC diagnostic ignored "-Wfloat-equal"
+#if defined(__GNUC__) && __GNUC__ >= 7 || defined(__clang_major__) && __clang_major__ >= 10
+#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
+#endif
+#endif
+
#include "lprefix.h"
diff -urN lua/ltablib.c APM/libraries/AP_Scripting/lua/src/ltablib.c
--- lua/ltablib.c 2022-12-14 08:41:37.455587347 +1100
+++ APM/libraries/AP_Scripting/lua/src/ltablib.c 2022-12-14 10:00:39.148339381 +1100
@@ -244,6 +244,9 @@
** 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)];
@@ -253,6 +256,7 @@
for (i = 0; i < sof(buff); i++)
rnd += buff[i];
return rnd;
+#endif
}
#endif /* } */
diff -urN lua/lua.c APM/libraries/AP_Scripting/lua/src/lua.c
--- lua/lua.c 2022-12-14 08:41:37.455587347 +1100
+++ APM/libraries/AP_Scripting/lua/src/lua.c 2022-12-14 10:00:39.148339381 +1100
@@ -1,3 +1,4 @@
+#ifndef ARDUPILOT_BUILD
/*
** $Id: lua.c $
** Lua stand-alone interpreter
@@ -664,3 +665,5 @@
return (result && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE;
}
+#endif // ARDUPILOT_BUILD
+
diff -urN lua/lua.h APM/libraries/AP_Scripting/lua/src/lua.h
--- lua/lua.h 2022-12-14 08:41:37.455587347 +1100
+++ APM/libraries/AP_Scripting/lua/src/lua.h 2022-12-14 10:10:27.219112123 +1100
@@ -15,6 +15,13 @@
#include "luaconf.h"
+#if defined(ARDUPILOT_BUILD) && !defined(__cplusplus)
+// lua code does lots of casting, these warnings are not helpful
+#pragma GCC diagnostic ignored "-Wcast-align"
+
+// lua code does a lot of float comparison
+#pragma GCC diagnostic ignored "-Wfloat-equal"
+#endif
#define LUA_VERSION_MAJOR "5"
#define LUA_VERSION_MINOR "4"
diff -urN lua/luaconf.h APM/libraries/AP_Scripting/lua/src/luaconf.h
--- lua/luaconf.h 2022-12-14 08:41:37.455587347 +1100
+++ APM/libraries/AP_Scripting/lua/src/luaconf.h 2022-12-14 10:00:39.148339381 +1100
@@ -11,6 +11,12 @@
#include <limits.h>
#include <stddef.h>
+/*
+ don't support binary load() by default
+ */
+#ifndef LUA_SUPPORT_LOAD_BINARY
+#define LUA_SUPPORT_LOAD_BINARY 0
+#endif
/*
** ===================================================================
@@ -116,7 +122,9 @@
/*
@@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats.
*/
+#ifndef LUA_32BITS
#define LUA_32BITS 0
+#endif
/*
diff -urN lua/lualib.h APM/libraries/AP_Scripting/lua/src/lualib.h
--- lua/lualib.h 2022-12-14 08:41:37.455587347 +1100
+++ APM/libraries/AP_Scripting/lua/src/lualib.h 2022-12-14 10:00:39.148339381 +1100
@@ -16,6 +16,9 @@
LUAMOD_API int (luaopen_base) (lua_State *L);
+#ifdef ARDUPILOT_BUILD
+LUAMOD_API int (luaopen_base_sandbox) (lua_State *L);
+#endif
#define LUA_COLIBNAME "coroutine"
LUAMOD_API int (luaopen_coroutine) (lua_State *L);
diff -urN lua/lundump.c APM/libraries/AP_Scripting/lua/src/lundump.c
--- lua/lundump.c 2022-12-14 08:41:37.455587347 +1100
+++ APM/libraries/AP_Scripting/lua/src/lundump.c 2022-12-14 10:00:39.148339381 +1100
@@ -4,6 +4,8 @@
** See Copyright Notice in lua.h
*/
+#ifndef ARDUPILOT_BUILD
+
#define lundump_c
#define LUA_CORE
@@ -331,3 +333,4 @@
return cl;
}
+#endif // ARDUPILOT_BUILD
diff -urN lua/lvm.c APM/libraries/AP_Scripting/lua/src/lvm.c
--- lua/lvm.c 2022-12-14 08:41:37.459587394 +1100
+++ APM/libraries/AP_Scripting/lua/src/lvm.c 2022-12-14 10:14:36.154002820 +1100
@@ -1838,3 +1838,5 @@
}
/* }================================================================== */
+
+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment