From da006d427d5fa7b88f286e774d285f50545e4854 Mon Sep 17 00:00:00 2001 From: Thomas Watson Date: Thu, 25 Jul 2024 15:24:14 -0500 Subject: [PATCH] AP_Scripting: add test for require() after rescheduling It can end up broken due to incorrect assumptions about the executed function's upvalues, which might change after rescheduling. --- .../tests/scripting_require_test_2.lua | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 libraries/AP_Scripting/tests/scripting_require_test_2.lua diff --git a/libraries/AP_Scripting/tests/scripting_require_test_2.lua b/libraries/AP_Scripting/tests/scripting_require_test_2.lua new file mode 100644 index 0000000000..f0bd9d2704 --- /dev/null +++ b/libraries/AP_Scripting/tests/scripting_require_test_2.lua @@ -0,0 +1,35 @@ +-- main require tests are in scripting_test.lua + +-- DO NOT EDIT!!!! it's very easy to make this accidentally pass even when the +-- original problem is still present!! we do some very careful work to check +-- that require works even when the function's first upvalue is not the script +-- environment. + +local loop_time = 500 -- number of ms between runs + +-- need to shadow gcs to make the upvalues right +local gcs = gcs -- luacheck: ignore + +local passes = 0 -- run both before and after scheduling + +local require_global = require("test/nested") + +local function update() + -- need to send before requiring to make the upvalues right + gcs:send_text(6, "testing") + local require_local = require("test/nested") -- should not crash + + -- validate we got the same object (object contents validated in main test) + if require_local == require_global then + passes = passes + 1 + else + gcs:send_text(0, "Failed: require returned different objects") + end + if passes >= 3 then + gcs:send_text(3, "Require test 2 passed") + end + + return update, loop_time +end + +return update() -- run immediately before starting to reschedule