[Orxonox-commit 2058] r6774 - in code/branches/gamestates3: data/lua src/libraries/core src/orxonox/items
rgrieder at orxonox.net
rgrieder at orxonox.net
Fri Apr 23 11:46:11 CEST 2010
Author: rgrieder
Date: 2010-04-23 11:46:11 +0200 (Fri, 23 Apr 2010)
New Revision: 6774
Modified:
code/branches/gamestates3/data/lua/Strict.lua
code/branches/gamestates3/src/libraries/core/LuaState.cc
code/branches/gamestates3/src/libraries/core/LuaState.h
code/branches/gamestates3/src/orxonox/items/MultiStateEngine.cc
Log:
Activated Strict.lua
That means you can no longer assign to or access an undeclared global variable on function scope.
Declaring: either assign anything to the variable (including nil) on file scope
or use the function global("myVar", "myVar2", ..., "myVarN") (mind the strings!)
Modified: code/branches/gamestates3/data/lua/Strict.lua
===================================================================
--- code/branches/gamestates3/data/lua/Strict.lua 2010-04-23 09:41:30 UTC (rev 6773)
+++ code/branches/gamestates3/data/lua/Strict.lua 2010-04-23 09:46:11 UTC (rev 6774)
@@ -12,15 +12,17 @@
setmetatable(_G, mt)
end
-__STRICT = false
+__STRICT = true
mt.__declared = {}
mt.__newindex = function (t, n, v)
- if __STRICT and not mt.__declared[n] then
- local d = debug.getinfo(2, "S")
- local w = d and d.what or "C"
- if w ~= "main" and w ~= "C" then
- error("assign to undeclared variable '"..n.."'", 2)
+ if not mt.__declared[n] then
+ if __STRICT then
+ local d = debug.getinfo(2, "S")
+ local w = d and d.what or "C"
+ if w ~= "main" and w ~= "C" then
+ error("Assigning to undeclared global variable '"..n.."'", 2)
+ end
end
mt.__declared[n] = true
end
@@ -28,12 +30,20 @@
end
mt.__index = function (t, n)
- if not mt.__declared[n] and debug.getinfo(2, "S").what ~= "C" then
- error("variable '"..n.."' is not declared", 2)
+ if not mt.__declared[n] then
+ local d = debug.getinfo(2, "S")
+ local w = d and d.what or "C"
+ if w ~= "C" then
+ error("Global variable '"..n.."' was not declared", 2)
+ else
+ mt.__declared[n] = true
+ end
end
return rawget(t, n)
end
function global(...)
- for _, v in ipairs{...} do mt.__declared[v] = true end
+ for _, v in ipairs{...} do
+ mt.__declared[v] = true
+ end
end
Modified: code/branches/gamestates3/src/libraries/core/LuaState.cc
===================================================================
--- code/branches/gamestates3/src/libraries/core/LuaState.cc 2010-04-23 09:41:30 UTC (rev 6773)
+++ code/branches/gamestates3/src/libraries/core/LuaState.cc 2010-04-23 09:46:11 UTC (rev 6774)
@@ -52,7 +52,7 @@
// Do this after declaring toluaInterfaces_s and instances_s to avoid larger problems
DeclareToluaInterface(Core);
- LuaState::LuaState()
+ LuaState::LuaState(bool bStrict)
: bIsRunning_(false)
, includeParseFunction_(NULL)
{
@@ -82,6 +82,18 @@
tolua_pushusertype(luaState_, static_cast<void*>(this), "orxonox::LuaState");
lua_setglobal(luaState_, "luaState");
+ // Strict.lua ensures that global variables are not declared inside a function scope
+ if (bStrict)
+ {
+ if (!this->doFile("Strict.lua"))
+ ThrowException(InitialisationFailed, "Running Strict.lua failed");
+ }
+ else
+ {
+ // Add dummy function for declaring global variables
+ this->doString("global = function(...) end");
+ }
+
// Parse init script
if (!this->doFile("LuaStateInit.lua"))
ThrowException(InitialisationFailed, "Running LuaStateInit.lua failed");
Modified: code/branches/gamestates3/src/libraries/core/LuaState.h
===================================================================
--- code/branches/gamestates3/src/libraries/core/LuaState.h 2010-04-23 09:41:30 UTC (rev 6773)
+++ code/branches/gamestates3/src/libraries/core/LuaState.h 2010-04-23 09:46:11 UTC (rev 6774)
@@ -67,7 +67,7 @@
class _CoreExport LuaState // tolua_export
{ // tolua_export
public:
- LuaState();
+ LuaState(bool bStrict = true);
~LuaState();
bool doFile(const std::string& filename); // tolua_export
Modified: code/branches/gamestates3/src/orxonox/items/MultiStateEngine.cc
===================================================================
--- code/branches/gamestates3/src/orxonox/items/MultiStateEngine.cc 2010-04-23 09:41:30 UTC (rev 6773)
+++ code/branches/gamestates3/src/orxonox/items/MultiStateEngine.cc 2010-04-23 09:46:11 UTC (rev 6774)
@@ -60,7 +60,7 @@
this->defEngineSndBoost_ = new WorldSound(this);
this->defEngineSndNormal_->setLooping(true);
this->defEngineSndBoost_->setLooping(true);
- this->lua_ = new LuaState();
+ this->lua_ = new LuaState(false);
}
else
{
More information about the Orxonox-commit
mailing list