Browse Source

AP_Scripting: Boxed numerics and maunal bindings use generated arg checker

master
Iampete1 3 years ago committed by Andrew Tridgell
parent
commit
2c8a11885f
  1. 3
      libraries/AP_Scripting/generator/src/main.c
  2. 25
      libraries/AP_Scripting/lua_bindings.cpp
  3. 36
      libraries/AP_Scripting/lua_boxed_numerics.cpp

3
libraries/AP_Scripting/generator/src/main.c

@ -2339,7 +2339,7 @@ void emit_sandbox(void) { @@ -2339,7 +2339,7 @@ void emit_sandbox(void) {
void emit_argcheck_helper(void) {
// tagging this with NOINLINE can save a large amount of flash
// but until we need it we will allow the compilier to choose to inline this for us
fprintf(source, "static int binding_argcheck(lua_State *L, int expected_arg_count) {\n");
fprintf(source, "int binding_argcheck(lua_State *L, int expected_arg_count) {\n");
fprintf(source, " const int args = lua_gettop(L);\n");
fprintf(source, " if (args > expected_arg_count) {\n");
fprintf(source, " return luaL_argerror(L, args, \"too many arguments\");\n");
@ -2754,6 +2754,7 @@ int main(int argc, char **argv) { @@ -2754,6 +2754,7 @@ int main(int argc, char **argv) {
fprintf(header, "void load_generated_bindings(lua_State *L);\n");
fprintf(header, "void load_generated_sandbox(lua_State *L);\n");
fprintf(header, "int binding_argcheck(lua_State *L, int expected_arg_count);\n");
fclose(header);
header = NULL;

25
libraries/AP_Scripting/lua_bindings.cpp

@ -12,24 +12,9 @@ @@ -12,24 +12,9 @@
extern const AP_HAL::HAL& hal;
int check_arguments(lua_State *L, int expected_arguments, const char *fn_name);
int check_arguments(lua_State *L, int expected_arguments, const char *fn_name) {
#if defined(AP_SCRIPTING_CHECKS) && AP_SCRIPTING_CHECKS >= 1
if (expected_arguments < 0) {
AP_HAL::panic("Lua: Attempted to check for negative arguments");
}
#endif
const int args = lua_gettop(L);
if (args != expected_arguments) {
return luaL_error(L, "%s expected %d arguments got %d", fn_name, expected_arguments, args);
}
return 0;
}
// millis
int lua_millis(lua_State *L) {
check_arguments(L, 0, "millis");
binding_argcheck(L, 0);
new_uint32_t(L);
*check_uint32_t(L, -1) = AP_HAL::millis();
@ -39,7 +24,7 @@ int lua_millis(lua_State *L) { @@ -39,7 +24,7 @@ int lua_millis(lua_State *L) {
// micros
int lua_micros(lua_State *L) {
check_arguments(L, 0, "micros");
binding_argcheck(L, 0);
new_uint32_t(L);
*check_uint32_t(L, -1) = AP_HAL::micros();
@ -48,7 +33,7 @@ int lua_micros(lua_State *L) { @@ -48,7 +33,7 @@ int lua_micros(lua_State *L) {
}
int lua_mission_receive(lua_State *L) {
check_arguments(L, 0, "mission_receive");
binding_argcheck(L, 0);
ObjectBuffer<struct AP_Scripting::scripting_mission_cmd> *input = AP::scripting()->mission_data;
@ -331,7 +316,7 @@ int lua_get_i2c_device(lua_State *L) { @@ -331,7 +316,7 @@ int lua_get_i2c_device(lua_State *L) {
#if HAL_MAX_CAN_PROTOCOL_DRIVERS
int lua_get_CAN_device(lua_State *L) {
check_arguments(L, 1, "CAN:get_device");
binding_argcheck(L, 1);
const uint32_t raw_buffer_len = coerce_to_uint32_t(L, 1);
luaL_argcheck(L, ((raw_buffer_len >= 1U) && (raw_buffer_len <= 25U)), 1, "argument out of range");
@ -352,7 +337,7 @@ int lua_get_CAN_device(lua_State *L) { @@ -352,7 +337,7 @@ int lua_get_CAN_device(lua_State *L) {
int lua_get_CAN_device2(lua_State *L) {
check_arguments(L, 1, "CAN:get_device2");
binding_argcheck(L, 1);
const uint32_t raw_buffer_len = coerce_to_uint32_t(L, 1);
luaL_argcheck(L, ((raw_buffer_len >= 1U) && (raw_buffer_len <= 25U)), 1, "argument out of range");

36
libraries/AP_Scripting/lua_boxed_numerics.cpp

@ -50,12 +50,7 @@ int lua_new_uint32_t(lua_State *L) { @@ -50,12 +50,7 @@ int lua_new_uint32_t(lua_State *L) {
#define UINT32_T_BOX_OP(name, sym) \
int uint32_t___##name(lua_State *L) { \
const int args = lua_gettop(L); \
if (args > 2) { \
return luaL_argerror(L, args, "too many arguments"); \
} else if (args < 2) { \
return luaL_argerror(L, args, "too few arguments"); \
} \
binding_argcheck(L, 2); \
\
uint32_t v1 = coerce_to_uint32_t(L, 1); \
uint32_t v2 = coerce_to_uint32_t(L, 2); \
@ -79,13 +74,7 @@ UINT32_T_BOX_OP(shr, >>) @@ -79,13 +74,7 @@ UINT32_T_BOX_OP(shr, >>)
#define UINT32_T_BOX_OP_BOOL(name, sym) \
int uint32_t___##name(lua_State *L) { \
const int args = lua_gettop(L); \
luaL_checkstack(L, 1, "Out of stack"); \
if (args > 2) { \
return luaL_argerror(L, args, "too many arguments"); \
} else if (args < 2) { \
return luaL_argerror(L, args, "too few arguments"); \
} \
binding_argcheck(L, 2); \
\
uint32_t v1 = coerce_to_uint32_t(L, 1); \
uint32_t v2 = coerce_to_uint32_t(L, 2); \
@ -100,11 +89,7 @@ UINT32_T_BOX_OP_BOOL(le, <=) @@ -100,11 +89,7 @@ UINT32_T_BOX_OP_BOOL(le, <=)
#define UINT32_T_BOX_OP_UNARY(name, sym) \
int uint32_t___##name(lua_State *L) { \
const int args = lua_gettop(L); \
luaL_checkstack(L, 1, "Out of stack"); \
if (args != 1) { \
return luaL_argerror(L, args, "Expected 1 argument"); \
} \
binding_argcheck(L, 1); \
\
uint32_t v1 = coerce_to_uint32_t(L, 1); \
\
@ -117,10 +102,7 @@ UINT32_T_BOX_OP_BOOL(le, <=) @@ -117,10 +102,7 @@ UINT32_T_BOX_OP_BOOL(le, <=)
UINT32_T_BOX_OP_UNARY(bnot, ~)
int uint32_t_toint(lua_State *L) {
const int args = lua_gettop(L);
if (args != 1) {
return luaL_argerror(L, args, "Expected 1 argument");
}
binding_argcheck(L, 1);
uint32_t v = *static_cast<uint32_t *>(luaL_checkudata(L, 1, "uint32_t"));
@ -130,10 +112,7 @@ int uint32_t_toint(lua_State *L) { @@ -130,10 +112,7 @@ int uint32_t_toint(lua_State *L) {
}
int uint32_t_tofloat(lua_State *L) {
const int args = lua_gettop(L);
if (args != 1) {
return luaL_argerror(L, args, "Expected 1 argument");
}
binding_argcheck(L, 1);
uint32_t v = *static_cast<uint32_t *>(luaL_checkudata(L, 1, "uint32_t"));
@ -143,10 +122,7 @@ int uint32_t_tofloat(lua_State *L) { @@ -143,10 +122,7 @@ int uint32_t_tofloat(lua_State *L) {
}
int uint32_t___tostring(lua_State *L) {
const int args = lua_gettop(L);
if (args != 1) {
return luaL_argerror(L, args, "Expected 1 argument");
}
binding_argcheck(L, 1);
uint32_t v = *static_cast<uint32_t *>(luaL_checkudata(L, 1, "uint32_t"));

Loading…
Cancel
Save