mirror of
https://github.com/ArduPilot/ardupilot
synced 2025-02-21 15:23:57 -04:00
AP_Scripting: Load a sandbox enviorment
This commit is contained in:
parent
645afbab82
commit
2c3fe0cbcf
@ -16,6 +16,7 @@
|
||||
#include <AP_Scripting/AP_Scripting.h>
|
||||
#include <AP_HAL/AP_HAL.h>
|
||||
#include <GCS_MAVLink/GCS.h>
|
||||
#include <AP_ROMFS/AP_ROMFS.h>
|
||||
|
||||
#include "lua_bindings.h"
|
||||
|
||||
@ -80,18 +81,24 @@ void AP_Scripting::thread(void) {
|
||||
luaL_openlibs(state);
|
||||
load_lua_bindings(state);
|
||||
|
||||
// luaL_loadstring(state, "gcs.send_text(string.format(\"1 + 2 = %d\", 1+2))");
|
||||
|
||||
// load the sandbox creation function
|
||||
if (luaL_dofile(state, "sandbox.lua")) {
|
||||
gcs().send_text(MAV_SEVERITY_CRITICAL, "SFATAL: %s", lua_tostring(state, -1));
|
||||
uint32_t sandbox_size;
|
||||
char *sandbox_data = (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;
|
||||
}
|
||||
|
||||
luaL_loadfile(state, "test.lua");
|
||||
lua_getglobal(state, "get_sandbox_env"); //find the sandbox creation function
|
||||
if (luaL_dostring(state, sandbox_data)) {
|
||||
gcs().send_text(MAV_SEVERITY_CRITICAL, "Scripting: Loading sandbox: %s", lua_tostring(state, -1));
|
||||
return;
|
||||
}
|
||||
free(sandbox_data);
|
||||
|
||||
luaL_loadstring(state, "gcs.send_text(string.format(\"math.cos(1 + 2) = %f\", math.cos(1+2)))");
|
||||
lua_getglobal(state, "get_sandbox_env"); // find the sandbox creation function
|
||||
if (lua_pcall(state, 0, LUA_MULTRET, 0)) {
|
||||
gcs().send_text(MAV_SEVERITY_CRITICAL, "SFATAL: %s", lua_tostring(state, -1));
|
||||
gcs().send_text(MAV_SEVERITY_CRITICAL, "Scripting: Could not create sandbox: %s", lua_tostring(state, -1));
|
||||
return;
|
||||
}
|
||||
lua_setupvalue(state, -2, 1);
|
||||
@ -111,7 +118,7 @@ void AP_Scripting::thread(void) {
|
||||
}
|
||||
const uint32_t runEnd = AP_HAL::micros();
|
||||
const uint32_t endMem = hal.util->available_memory();
|
||||
gcs().send_text(MAV_SEVERITY_INFO, "Execution: %d Memory: %d", runEnd - loadEnd, startMem - endMem);
|
||||
gcs().send_text(MAV_SEVERITY_INFO, "Time: %d Memory: %d", runEnd - loadEnd, startMem - endMem);
|
||||
}
|
||||
}
|
||||
|
||||
|
32
libraries/AP_Scripting/scripts/sandbox.lua
Normal file
32
libraries/AP_Scripting/scripts/sandbox.lua
Normal file
@ -0,0 +1,32 @@
|
||||
-- sample sandbox environment
|
||||
function get_sandbox_env ()
|
||||
return {
|
||||
ipairs = ipairs,
|
||||
next = next,
|
||||
pairs = pairs,
|
||||
pcall = pcall,
|
||||
tonumber = tonumber,
|
||||
tostring = tostring,
|
||||
type = type,
|
||||
unpack = unpack,
|
||||
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 },
|
||||
utf8 = { char = utf8.char, charpattern = utf8.charpattern, codes = utf8.codes,
|
||||
codepoint = utf8.codepoint, len = utf8.len, offsets = utf8.offsets},
|
||||
os = { clock = os.clock, difftime = os.difftime, time = os.time },
|
||||
gcs = { send_text = gcs.send_text},
|
||||
}
|
||||
end
|
Loading…
Reference in New Issue
Block a user