2022-11-25 20:37:01 -04:00
|
|
|
--[[
|
|
|
|
test the load function for loading new code from strings
|
|
|
|
--]]
|
2024-05-03 15:22:25 -03:00
|
|
|
---@diagnostic disable: undefined-global
|
2022-11-25 20:37:01 -04:00
|
|
|
|
|
|
|
gcs:send_text(0,"Testing load() method")
|
|
|
|
|
|
|
|
-- a function written as a string. This could come from a file
|
|
|
|
-- or any other source (eg. mavlink)
|
|
|
|
-- Note that the [[ xxx ]] syntax is just a multi-line string
|
2023-02-13 22:45:19 -04:00
|
|
|
|
2022-11-25 20:37:01 -04:00
|
|
|
local func_str = [[
|
|
|
|
function TestFunc(x,y)
|
|
|
|
return math.sin(x) + math.cos(y)
|
|
|
|
end
|
|
|
|
]]
|
|
|
|
|
|
|
|
function test_load()
|
|
|
|
-- load the code into the global environment
|
|
|
|
local f,errloc,err = load(func_str,"TestFunc", "t", _ENV)
|
|
|
|
if not f then
|
|
|
|
gcs:send_text(0,string.format("Error %s: %s", errloc, err))
|
|
|
|
return
|
|
|
|
end
|
|
|
|
-- run the code within a protected call to catch any errors
|
2023-12-11 17:19:48 -04:00
|
|
|
local success, err2 = pcall(f)
|
2022-11-25 20:37:01 -04:00
|
|
|
if not success then
|
2023-12-11 17:19:48 -04:00
|
|
|
gcs:send_text(0, string.format("Failed to load TestFunc: %s", err2))
|
2022-11-25 20:37:01 -04:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- we now have the new function
|
|
|
|
gcs:send_text(0, string.format("TestFunc(3,4) -> %f", TestFunc(3,4)))
|
|
|
|
end
|
|
|
|
|
|
|
|
test_load()
|