Feature/strict lua (#1556)

Make all LuaState strict to check whether uninitialized variables are used
Adapt the existing assets to work with the strict state
This commit is contained in:
Alexander Bock
2021-04-15 13:01:00 +02:00
committed by GitHub
parent 54e5050b53
commit d32a09ba80
18 changed files with 46 additions and 84 deletions

View File

@@ -13,8 +13,7 @@ local Charon = {
Translation = {
Type = "SpiceTranslation",
Target = "CHARON",
Observer = "PLUTO BARYCENTER",
Kernels = NewHorizonsKernels
Observer = "PLUTO BARYCENTER"
},
Rotation = {
Type = "SpiceRotation",

View File

@@ -11,8 +11,7 @@ local Kerberos = {
Translation = {
Type = "SpiceTranslation",
Target = "KERBEROS",
Observer = "PLUTO BARYCENTER",
Kernels = PlutoKernels
Observer = "PLUTO BARYCENTER"
}
},
Renderable = {

View File

@@ -11,8 +11,7 @@ local Nix = {
Translation = {
Type = "SpiceTranslation",
Target = "NIX",
Observer = "PLUTO BARYCENTER",
Kernels = PlutoKernels
Observer = "PLUTO BARYCENTER"
}
},
Renderable = {

View File

@@ -3,7 +3,6 @@ local transforms = asset.require('../transforms')
local kernels = asset.require('../kernels').PlutoKernels
local Styx = {
Identifier = "Styx",
Parent = transforms.PlutoBarycenter.Identifier,
@@ -11,8 +10,7 @@ local Styx = {
Translation = {
Type = "SpiceTranslation",
Target = "STYX",
Observer = "PLUTO BARYCENTER",
Kernels = PlutoKernels
Observer = "PLUTO BARYCENTER"
}
},
Renderable = {

View File

@@ -13,8 +13,7 @@ local Pluto = {
Translation = {
Type = "SpiceTranslation",
Target = "PLUTO",
Observer = "PLUTO BARYCENTER",
Kernels = NewHorizonsKernels
Observer = "PLUTO BARYCENTER"
},
Rotation = {
Type = "SpiceRotation",

View File

@@ -18,13 +18,8 @@ asset.onInitialize(function ()
openspace.globebrowsing.addBlendingLayersFromDirectory(vrts, assetGlobe);
flipbook = helper.createFlipbook(assetPrefix, assetGlobe, 19);
function nextFlip()
helper.nextFlipbookPage(flipbook);
end
function previousFlip()
helper.previousFlipbookPage(flipbook);
end
rawset(_G, "nextFlip", function() helper.nextFlipbookPage(flipbook) end)
rawset(_G, "previousFlip", function() helper.previousFlipbookPage(flipbook) end)
openspace.bindKey("p", "nextFlip()", "Show the next Apollo 11 flipbook image.", "Next A11 flip", "/Missions/Apollo/11")
openspace.bindKey("o", "previousFlip()","Show the previous Apollo 11 flipbook image.", "Prev A11 flip", "/Missions/Apollo/11")

View File

@@ -172,7 +172,7 @@ local MessengerTrail = {
Type = "SpiceTranslation",
Target = "MESSENGER",
Observer = "MERCURY BARYCENTER",
Kernels = Kernels
Kernels = LocalKernels
},
Color = { 0.288, 0.375, 0.934 },
EnableFade = false,

View File

@@ -1,6 +1,7 @@
local assetHelper = asset.require('util/asset_helper')
local transforms = asset.require('./transforms')
local sunTransforms = asset.require('scene/solarsystem/sun/transforms')
local NewHorizonsKernels = asset.require('./kernels').NewHorizonsKernels
local assets = asset.syncedResource({
Name = "Pluto Assets",

View File

@@ -3,7 +3,7 @@ local proceduralGlobes = asset.require('util/procedural_globe')
local transforms = asset.require('./transforms')
local kernels = asset.require('./kernels')
local kernel081 = kernels.nep081
local kernel087 = kernels.nep087
local kernel086 = kernels.nep086
local kernel088 = kernels.nep088

View File

@@ -13,7 +13,7 @@ local registerSpiceKernels = function (spiceAsset, kernels)
end)
spiceAsset.onDeinitialize(function ()
for i = #kernels, 1, -1 do
kernel = kernels[i]
local kernel = kernels[i]
openspace.spice.unloadKernel(kernel)
end
end)
@@ -75,7 +75,7 @@ local registerDashboardItems = function (dashboardAsset, items)
)
dashboardAsset.onDeinitialize(function ()
for i = #items, 1, -1 do
item = items[i]
local item = items[i]
openspace.dashboard.removeDashboardItem(item.Identifier)
end
end)
@@ -103,7 +103,7 @@ local registerSceneGraphNodesAndExport = function (sceneAsset, nodes, override)
end)
sceneAsset.onDeinitialize(function ()
for i = #nodes, 1, -1 do
node = nodes[i]
local node = nodes[i]
openspace.removeSceneGraphNode(node.Identifier)
end
end)

View File

@@ -68,7 +68,7 @@ local createGlobes = function(t)
end
end
result = {}
local result = {}
for i, v in ipairs(t) do
local globe = nil

View File

@@ -10,7 +10,7 @@ local backend = asset.syncedResource({
asset.onInitialize(function ()
-- Unzip the server bundle
dest = backend .. "/backend"
local dest = backend .. "/backend"
if not openspace.directoryExists(dest) then
openspace.unzipFile(backend .. "/backend.zip", dest, true)
end

View File

@@ -24,7 +24,7 @@ end
-- Check format of a set of 3 TLE file lines and return nonzero if there is a format error
function isValidTLEFileFormat(lineArr)
function isEmpty(s) return s == nil or s == '' end
local function isEmpty(s) return s == nil or s == '' end
if isEmpty(lineArr[1]) or isEmpty(lineArr[2]) or isEmpty(lineArr[3]) then
return false

View File

@@ -118,7 +118,7 @@ end
openspace.globebrowsing.createTemporalGibsGdalXml = function (layerName, startDate, endDate, timeResolution, resolution, format, temporalFormat)
temporalFormat = temporalFormat or 'YYYY-MM-DD'
temporalTemplate =
local temporalTemplate =
"<OpenSpaceTemporalGDALDataset>" ..
"<OpenSpaceTimeStart>" .. startDate .. "</OpenSpaceTimeStart>" ..
"<OpenSpaceTimeEnd>" .. endDate .. "</OpenSpaceTimeEnd>" ..
@@ -130,7 +130,7 @@ openspace.globebrowsing.createTemporalGibsGdalXml = function (layerName, startDa
end
openspace.globebrowsing.createGibsGdalXml = function (layerName, date, resolution, format)
tileLevel = 5
local tileLevel = 5
-- These resolutions are defined by GIBS: https://wiki.earthdata.nasa.gov/display/GIBS/GIBS+API+for+Developers#GIBSAPIforDevelopers-Script-levelAccessviaGDAL
if resolution == "2km" then
tileLevel = 5
@@ -153,7 +153,7 @@ openspace.globebrowsing.createGibsGdalXml = function (layerName, date, resolutio
return ""
end
rasterCount = 3
local rasterCount = 3
if format == "jpg" then
if layerName == "ASTER_GDEM_Greyscale_Shaded_Relief" then
rasterCount = 1
@@ -167,7 +167,7 @@ openspace.globebrowsing.createGibsGdalXml = function (layerName, date, resolutio
return ""
end
gdalWmsTemplate =
local gdalWmsTemplate =
"<GDAL_WMS>" ..
"<Service name=\"TMS\">" ..
"<ServerUrl>https://gibs.earthdata.nasa.gov/wmts/epsg4326/best/" ..
@@ -198,14 +198,21 @@ openspace.globebrowsing.createGibsGdalXml = function (layerName, date, resolutio
end
openspace.globebrowsing.parseInfoFile = function (file)
Name = nil
Identifier = nil
Description = nil
ColorFile = nil
HeightFile = nil
-- We are loading these values from an external info file and since we are switching
-- to a strict Lua, we need to predefine these global variables
local function declare(name)
rawset(_G, name, "")
end
declare("Name")
declare("Identifier")
declare("Description")
declare("ColorFile")
declare("HeightFile")
declare("Location")
local dir = openspace.directoryForPath(file)
file_func, error = loadfile(file)
local file_func, error = loadfile(file)
if file_func then
file_func()
else
@@ -213,6 +220,15 @@ openspace.globebrowsing.parseInfoFile = function (file)
return nil, nil, nil, nil
end
-- Hoist the global variables into local space
local Name = rawget(_G, "Name")
local Identifier = rawget(_G, "Identifier")
local Description = rawget(_G, "Description")
local ColorFile = rawget(_G, "ColorFile")
local HeightFile = rawget(_G, "HeightFile")
local Location = rawget(_G, "Location")
-- Now we can start
local name = Name or Identifier
local identifier = Identifier or Name
@@ -222,7 +238,7 @@ openspace.globebrowsing.parseInfoFile = function (file)
end
local color = nil
if ColorFile then
if ColorFile and ColorFile ~= "" then
color = {
Identifier = identifier,
Name = name,
@@ -233,7 +249,7 @@ openspace.globebrowsing.parseInfoFile = function (file)
end
local height = nil
if HeightFile then
if HeightFile and HeightFile ~= "" then
height = {
Identifier = identifier,
Name = name,

View File

@@ -1,40 +0,0 @@
-- 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

View File

@@ -37,7 +37,6 @@ 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
@@ -650,9 +649,6 @@ 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);