mirror of
https://github.com/ArduPilot/ardupilot
synced 2025-01-21 16:18:29 -04:00
AP_Scripting: Reduce memory associated with the sandbox
This commit is contained in:
parent
8292e9d4a5
commit
c1a599d0bd
@ -452,33 +452,33 @@ static int luaB_tostring (lua_State *L) {
|
||||
|
||||
static const luaL_Reg base_funcs[] = {
|
||||
{"assert", luaB_assert},
|
||||
{"collectgarbage", luaB_collectgarbage},
|
||||
{"dofile", luaB_dofile},
|
||||
// {"collectgarbage", luaB_collectgarbage},
|
||||
// {"dofile", luaB_dofile},
|
||||
{"error", luaB_error},
|
||||
{"getmetatable", luaB_getmetatable},
|
||||
// {"getmetatable", luaB_getmetatable},
|
||||
{"ipairs", luaB_ipairs},
|
||||
{"loadfile", luaB_loadfile},
|
||||
{"load", luaB_load},
|
||||
// {"loadfile", luaB_loadfile},
|
||||
// {"load", luaB_load},
|
||||
#if defined(LUA_COMPAT_LOADSTRING)
|
||||
{"loadstring", luaB_load},
|
||||
#endif
|
||||
{"next", luaB_next},
|
||||
{"pairs", luaB_pairs},
|
||||
{"pcall", luaB_pcall},
|
||||
{"print", luaB_print},
|
||||
{"rawequal", luaB_rawequal},
|
||||
{"rawlen", luaB_rawlen},
|
||||
{"rawget", luaB_rawget},
|
||||
{"rawset", luaB_rawset},
|
||||
{"select", luaB_select},
|
||||
{"setmetatable", luaB_setmetatable},
|
||||
// {"print", luaB_print},
|
||||
// {"rawequal", luaB_rawequal},
|
||||
// {"rawlen", luaB_rawlen},
|
||||
// {"rawget", luaB_rawget},
|
||||
// {"rawset", luaB_rawset},
|
||||
// {"select", luaB_select},
|
||||
// {"setmetatable", luaB_setmetatable},
|
||||
{"tonumber", luaB_tonumber},
|
||||
{"tostring", luaB_tostring},
|
||||
{"type", luaB_type},
|
||||
{"xpcall", luaB_xpcall},
|
||||
// {"xpcall", luaB_xpcall},
|
||||
/* placeholders */
|
||||
{"_G", NULL},
|
||||
{"_VERSION", NULL},
|
||||
// {"_G", NULL},
|
||||
// {"_VERSION", NULL},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
@ -496,3 +496,8 @@ LUAMOD_API int luaopen_base (lua_State *L) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
LUAMOD_API int luaopen_base_sandbox(lua_State *L) {
|
||||
luaL_setfuncs(L, base_funcs, 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -705,9 +705,9 @@ static const luaL_Reg iolib[] = {
|
||||
{"lines", io_lines},
|
||||
{"open", io_open},
|
||||
{"output", io_output},
|
||||
{"popen", io_popen},
|
||||
// {"popen", io_popen},
|
||||
{"read", io_read},
|
||||
{"tmpfile", io_tmpfile},
|
||||
// {"tmpfile", io_tmpfile},
|
||||
{"type", io_type},
|
||||
{"write", io_write},
|
||||
{NULL, NULL}
|
||||
|
@ -1542,7 +1542,7 @@ static int str_unpack (lua_State *L) {
|
||||
static const luaL_Reg strlib[] = {
|
||||
{"byte", str_byte},
|
||||
{"char", str_char},
|
||||
{"dump", str_dump},
|
||||
// {"dump", str_dump},
|
||||
{"find", str_find},
|
||||
{"format", str_format},
|
||||
{"gmatch", gmatch},
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
|
||||
LUAMOD_API int (luaopen_base) (lua_State *L);
|
||||
LUAMOD_API int (luaopen_base_sandbox) (lua_State *L);
|
||||
|
||||
#define LUA_COLIBNAME "coroutine"
|
||||
LUAMOD_API int (luaopen_coroutine) (lua_State *L);
|
||||
|
@ -52,6 +52,12 @@ static int lua_millis(lua_State *L) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const luaL_Reg global_functions[] =
|
||||
{
|
||||
{"millis", lua_millis},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
static const luaL_Reg servo_functions[] =
|
||||
{
|
||||
{"set_output_pwm", lua_servo_set_output_pwm},
|
||||
@ -59,12 +65,10 @@ static const luaL_Reg servo_functions[] =
|
||||
};
|
||||
|
||||
void load_lua_bindings(lua_State *L) {
|
||||
lua_pushstring(L, "servo");
|
||||
luaL_newlib(L, servo_functions);
|
||||
lua_setglobal(L, "servo");
|
||||
lua_settable(L, -3);
|
||||
|
||||
load_generated_bindings(L);
|
||||
|
||||
lua_pushcfunction(L, lua_millis);
|
||||
lua_setglobal(L, "millis");
|
||||
luaL_setfuncs(L, global_functions, 0);
|
||||
}
|
||||
|
||||
|
@ -17,7 +17,6 @@
|
||||
#include <AP_HAL/AP_HAL.h>
|
||||
#include <GCS_MAVLink/GCS.h>
|
||||
#include "AP_Scripting.h"
|
||||
#include <AP_ROMFS/AP_ROMFS.h>
|
||||
|
||||
#include "lua_generated_bindings.h"
|
||||
|
||||
@ -94,12 +93,24 @@ lua_scripts::script_info *lua_scripts::load_script(lua_State *L, char *filename)
|
||||
|
||||
|
||||
// find and create a sandbox for the new chunk
|
||||
lua_getglobal(L, "get_sandbox_env");
|
||||
if (lua_pcall(L, 0, LUA_MULTRET, 0)) {
|
||||
gcs().send_text(MAV_SEVERITY_CRITICAL, "Scripting: Could not create sandbox: %s", lua_tostring(L, -1));
|
||||
hal.console->printf("Lua: Scripting: Could not create sandbox: %s", lua_tostring(L, -1));
|
||||
return nullptr;
|
||||
}
|
||||
lua_newtable(L);
|
||||
luaopen_base_sandbox(L);
|
||||
lua_pushstring(L, "math");
|
||||
luaopen_math(L);
|
||||
lua_settable(L, -3);
|
||||
lua_pushstring(L, "table");
|
||||
luaopen_table(L);
|
||||
lua_settable(L, -3);
|
||||
lua_pushstring(L, "string");
|
||||
luaopen_string(L);
|
||||
lua_settable(L, -3);
|
||||
lua_pushstring(L, "io");
|
||||
luaopen_io(L);
|
||||
lua_settable(L, -3);
|
||||
lua_pushstring(L, "utf8");
|
||||
luaopen_utf8(L);
|
||||
lua_settable(L, -3);
|
||||
load_lua_bindings(L);
|
||||
load_generated_sandbox(L);
|
||||
lua_setupvalue(L, -2, 1);
|
||||
|
||||
@ -334,22 +345,9 @@ void lua_scripts::run(void) {
|
||||
return;
|
||||
}
|
||||
lua_atpanic(L, atpanic);
|
||||
luaL_openlibs(L);
|
||||
load_lua_bindings(L);
|
||||
|
||||
// load the sandbox creation function
|
||||
uint32_t sandbox_size;
|
||||
const char *sandbox_data = (const char *)AP_ROMFS::find_decompress("sandbox.lua", sandbox_size);
|
||||
if (sandbox_data == nullptr) {
|
||||
gcs().send_text(MAV_SEVERITY_CRITICAL, "Scripting: Could not find sandbox");
|
||||
return;
|
||||
}
|
||||
|
||||
if (luaL_dostring(L, sandbox_data)) {
|
||||
gcs().send_text(MAV_SEVERITY_CRITICAL, "Scripting: Loading sandbox: %s", lua_tostring(L, -1));
|
||||
return;
|
||||
}
|
||||
AP_ROMFS::free((const uint8_t *)sandbox_data);
|
||||
// luaL_openlibs(L);
|
||||
// load_lua_bindings(L);
|
||||
load_generated_bindings(L);
|
||||
|
||||
// Scan the filesystem in an appropriate manner and autostart scripts
|
||||
load_all_scripts_in_dir(L, SCRIPTING_DIRECTORY);
|
||||
|
@ -1,37 +0,0 @@
|
||||
-- sample sandbox environment
|
||||
function get_sandbox_env ()
|
||||
return {
|
||||
ipairs = ipairs,
|
||||
next = next,
|
||||
pairs = pairs,
|
||||
pcall = pcall,
|
||||
tonumber = tonumber,
|
||||
tostring = tostring,
|
||||
type = type,
|
||||
error = error,
|
||||
unpack = unpack,
|
||||
io = { close = io.close, flush = io.flush, input = io.input, open = io.open, output = io.output,
|
||||
popen = io.popen, read = io.read, type = io.type, write = io.write},
|
||||
string = { byte = string.byte, char = string.char, find = string.find,
|
||||
format = string.format, gmatch = string.gmatch, gsub = string.gsub,
|
||||
len = string.len, lower = string.lower, match = string.match,
|
||||
rep = string.rep, reverse = string.reverse, sub = string.sub,
|
||||
upper = string.upper },
|
||||
math = { abs = math.abs, acos = math.acos, asin = math.asin,
|
||||
atan = math.atan, atan2 = math.atan2, ceil = math.ceil, cos = math.cos,
|
||||
cosh = math.cosh, deg = math.deg, exp = math.exp, floor = math.floor,
|
||||
fmod = math.fmod, frexp = math.frexp, huge = math.huge,
|
||||
ldexp = math.ldexp, log = math.log, log10 = math.log10, max = math.max,
|
||||
min = math.min, modf = math.modf, pi = math.pi, pow = math.pow,
|
||||
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, unpack = table.unpack },
|
||||
utf8 = { char = utf8.char, charpattern = utf8.charpattern, codes = utf8.codes,
|
||||
codepoint = utf8.codepoint, len = utf8.len, offsets = utf8.offsets},
|
||||
|
||||
-- ArduPilot specific
|
||||
millis = millis,
|
||||
servo = { set_output_pwm = servo.set_output_pwm},
|
||||
}
|
||||
end
|
Loading…
Reference in New Issue
Block a user