AP_Scripting: Add a one arg variant of uint32_t, and expose table unpack

This commit is contained in:
Michael du Breuil 2019-10-15 13:51:31 -07:00 committed by Randy Mackay
parent 2cb3e446ab
commit 076cf0bdc8
2 changed files with 20 additions and 14 deletions

View File

@ -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<uint32_t *>(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<uint32_t *>(data);
}
static uint32_t coerce_to_uint32_t(lua_State *L, int arg) {
{ // userdata
const uint32_t * ud = static_cast<uint32_t *>(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<uint32_t *>(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<uint32_t *>(data);
}
#define UINT32_T_BOX_OP(name, sym) \
static int uint32_t___##name(lua_State *L) { \
const int args = lua_gettop(L); \

View File

@ -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},