From 076cf0bdc8072fd5e7c40ec1eacc25dfdb0a2556 Mon Sep 17 00:00:00 2001 From: Michael du Breuil Date: Tue, 15 Oct 2019 13:51:31 -0700 Subject: [PATCH] AP_Scripting: Add a one arg variant of uint32_t, and expose table unpack --- libraries/AP_Scripting/lua_boxed_numerics.cpp | 32 +++++++++++-------- libraries/AP_Scripting/scripts/sandbox.lua | 2 +- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/libraries/AP_Scripting/lua_boxed_numerics.cpp b/libraries/AP_Scripting/lua_boxed_numerics.cpp index 4dda7feffd..c9c577137b 100644 --- a/libraries/AP_Scripting/lua_boxed_numerics.cpp +++ b/libraries/AP_Scripting/lua_boxed_numerics.cpp @@ -4,19 +4,6 @@ extern const AP_HAL::HAL& hal; -int new_uint32_t(lua_State *L) { - luaL_checkstack(L, 2, "Out of stack"); - *static_cast(lua_newuserdata(L, sizeof(uint32_t))) = 0; // allocated memory is already zerod, no need to manipulate this - luaL_getmetatable(L, "uint32_t"); - lua_setmetatable(L, -2); - return 1; -} - -uint32_t * check_uint32_t(lua_State *L, int arg) { - void *data = luaL_checkudata(L, arg, "uint32_t"); - return static_cast(data); -} - static uint32_t coerce_to_uint32_t(lua_State *L, int arg) { { // userdata const uint32_t * ud = static_cast(luaL_testudata(L, arg, "uint32_t")); @@ -47,6 +34,25 @@ static uint32_t coerce_to_uint32_t(lua_State *L, int arg) { return luaL_argerror(L, arg, "Unable to coerce to uint32_t"); } +int new_uint32_t(lua_State *L) { + luaL_checkstack(L, 2, "Out of stack"); + + const int args = lua_gettop(L); + if (args > 1) { + return luaL_argerror(L, args, "too many arguments"); + } + + *static_cast(lua_newuserdata(L, sizeof(uint32_t))) = (args == 1) ? coerce_to_uint32_t(L, 1) : 0; + luaL_getmetatable(L, "uint32_t"); + lua_setmetatable(L, -2); + return 1; +} + +uint32_t * check_uint32_t(lua_State *L, int arg) { + void *data = luaL_checkudata(L, arg, "uint32_t"); + return static_cast(data); +} + #define UINT32_T_BOX_OP(name, sym) \ static int uint32_t___##name(lua_State *L) { \ const int args = lua_gettop(L); \ diff --git a/libraries/AP_Scripting/scripts/sandbox.lua b/libraries/AP_Scripting/scripts/sandbox.lua index 5cb85f1a65..3eaaa9c5a8 100644 --- a/libraries/AP_Scripting/scripts/sandbox.lua +++ b/libraries/AP_Scripting/scripts/sandbox.lua @@ -25,7 +25,7 @@ function get_sandbox_env () rad = math.rad, random = math.random, sin = math.sin, sinh = math.sinh, sqrt = math.sqrt, tan = math.tan, tanh = math.tanh }, table = { insert = table.insert, maxn = table.maxn, remove = table.remove, - sort = table.sort }, + sort = table.sort, unpack = table.unpack }, utf8 = { char = utf8.char, charpattern = utf8.charpattern, codes = utf8.codes, codepoint = utf8.codepoint, len = utf8.len, offsets = utf8.offsets},