From 52b020d836952e5662dca6374f6c31b39fc4aead Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Thu, 1 Apr 2021 23:37:35 +0200 Subject: [PATCH] Include strict lua file, throw error in configuration loading when overriding with unused variable - Preventing True/true bug - Fix issue with unused variable in configuration_helper --- scripts/configuration_helper.lua | 2 +- scripts/strict.lua | 40 ++++++++++++++++++++++++++++++++ src/engine/configuration.cpp | 4 ++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 scripts/strict.lua diff --git a/scripts/configuration_helper.lua b/scripts/configuration_helper.lua index 970314a89a..84dd547e75 100644 --- a/scripts/configuration_helper.lua +++ b/scripts/configuration_helper.lua @@ -702,7 +702,7 @@ function sgct.config.single(arg) ) sgctconfiginitializeString = "sgct.config.single" - trackedSpecifier = "tracked=\"true\"" + local trackedSpecifier = "tracked=\"true\"" if (arg["tracked"] ~= nil and arg["tracked"] == false) then trackedSpecifier = "tracked=\"false\"" diff --git a/scripts/strict.lua b/scripts/strict.lua new file mode 100644 index 0000000000..088ef4c62d --- /dev/null +++ b/scripts/strict.lua @@ -0,0 +1,40 @@ +-- strict.lua +-- checks uses of undeclared global variables +-- All global variables must be 'declared' through a regular assignment +-- (even assigning nil will do) in a main chunk before being used +-- anywhere or assigned to inside a function. +-- distributed under the Lua license: http://www.lua.org/license.html + +local getinfo, error, rawset, rawget = debug.getinfo, error, rawset, rawget + +local mt = getmetatable(_G) +if mt == nil then + mt = {} + setmetatable(_G, mt) +end + +mt.__declared = {} + +local function what () + local d = getinfo(3, "S") + return d and d.what or "C" +end + +mt.__newindex = function (t, n, v) + if not mt.__declared[n] then + local w = what() + if w ~= "main" and w ~= "C" then + error("assign to undeclared variable '"..n.."'", 2) + end + mt.__declared[n] = true + end + rawset(t, n, v) +end + +mt.__index = function (t, n) + if not mt.__declared[n] and what() ~= "C" then + error("variable '"..n.."' is not declared", 2) + end + return rawget(t, n) +end + diff --git a/src/engine/configuration.cpp b/src/engine/configuration.cpp index f063bded20..e168e90877 100644 --- a/src/engine/configuration.cpp +++ b/src/engine/configuration.cpp @@ -37,6 +37,7 @@ namespace { // We can't use ${SCRIPTS} here as that hasn't been defined by this point constexpr const char* InitialConfigHelper = "${BASE}/scripts/configuration_helper.lua"; + constexpr const char* StrictLuaScript = "${BASE}/scripts/strict.lua"; struct [[codegen::Dictionary(Configuration)]] Parameters { // The SGCT configuration file that determines the window and view frustum @@ -649,6 +650,9 @@ Configuration loadConfigurationFromFile(const std::string& filename, if (FileSys.fileExists(absPath(InitialConfigHelper))) { ghoul::lua::runScriptFile(result.state, absPath(InitialConfigHelper)); } + if (FileSys.fileExists(absPath(StrictLuaScript))) { + ghoul::lua::runScriptFile(result.state, absPath(StrictLuaScript)); + } // Load the configuration file into the state ghoul::lua::runScriptFile(result.state, filename);