Quantcast

Code cleanup.

Johnny C. Lam [10-15-13 - 01:24]
Code cleanup.

- Simplify constructors for OOP classes.
- Fix leak to global namespace.
- Be cleaner about using "function static" variables.
- List private functions/methods used, including base Lua API functions.
- Quiet harmless warnings in compiler.pl slightly.

git-svn-id: svn://svn.curseforge.net/wow/ovale/mainline/trunk@1040 d5049fe3-3747-40f7-a4b5-f36d6801af5f
Filename
OvaleAura.lua
OvaleCompile.lua
OvaleDamageTaken.lua
OvaleFuture.lua
OvalePaperDoll.lua
OvalePool.lua
OvalePoolGC.lua
OvaleQueue.lua
OvaleState.lua
compiler.pl
diff --git a/OvaleAura.lua b/OvaleAura.lua
index 4bcca3c..6cfb176 100644
--- a/OvaleAura.lua
+++ b/OvaleAura.lua
@@ -32,9 +32,9 @@ local API_GetTime = GetTime
 local API_UnitAura = UnitAura

 -- aura pool
-local self_pool = OvalePool:NewPool("OvaleAura_pool")
+local self_pool = OvalePool("OvaleAura_pool")
 -- self_aura[guid] pool
-local self_aura_pool = OvalePool:NewPool("OvaleAura_aura_pool")
+local self_aura_pool = OvalePool("OvaleAura_aura_pool")
 -- player's GUID
 local self_player_guid = nil
 -- self_aura[guid][filter][spellId][casterGUID] = { aura properties }
@@ -42,10 +42,6 @@ local self_aura = {}
 -- self_serial[guid] = aura age
 local self_serial = {}

--- Static properties used by "GetAura" method.
-local aura_GetAura = {}
-local newAura_GetAura = {}
-
 local OVALE_UNKNOWN_GUID = 0

 local OVALE_AURA_DEBUG = "aura"
@@ -466,26 +462,23 @@ end
 function OvaleAura:GetAura(unitId, spellId, filter, mine, auraFound)
 	local guid = OvaleGUID:GetGUID(unitId)
 	if OvaleData.buffSpellList[spellId] then
-		if auraFound then wipe(newAura_GetAura) end
-		local newStart, newEnding, newStacks, newGain
+		local newAura, newStart, newEnding, newStacks, newGain
 		for auraId in pairs(OvaleData.buffSpellList[spellId]) do
-			if auraFound then wipe(aura_GetAura) end
-			local start, ending, stacks, gain = self:GetAuraByGUID(guid, auraId, filter, mine, unitId, aura_GetAura)
+			local aura = self_pool:Get()
+			local start, ending, stacks, gain = self:GetAuraByGUID(guid, auraId, filter, mine, unitId, aura)
 			if start and (not newStart or stacks > newStacks) then
+				if newAura then
+					self_pool:Release(newAura)
+				end
+				newAura = aura
 				newStart = start
 				newEnding = ending
 				newStacks = stacks
 				newGain = gain
-				if auraFound then
-					wipe(newAura_GetAura)
-					for k, v in pairs(aura_GetAura) do
-						newAura_GetAura[k] = v
-					end
-				end
 			end
 		end
 		if auraFound then
-			for k, v in pairs(newAura_GetAura) do
+			for k, v in pairs(newAura) do
 				auraFound[k] = v
 			end
 		end
diff --git a/OvaleCompile.lua b/OvaleCompile.lua
index b9d51af..6c73201 100644
--- a/OvaleCompile.lua
+++ b/OvaleCompile.lua
@@ -40,7 +40,7 @@ local API_GetItemInfo = GetItemInfo
 local API_GetSpellInfo = GetSpellInfo

 local self_node = {}
-local self_pool = OvalePool:NewPool("OvaleCompile_pool")
+local self_pool = OvalePool("OvaleCompile_pool")
 local self_defines = {}
 local self_customFunctions = {}
 local self_missingSpellList = {}
diff --git a/OvaleDamageTaken.lua b/OvaleDamageTaken.lua
index 58ae81d..37d0cf4 100644
--- a/OvaleDamageTaken.lua
+++ b/OvaleDamageTaken.lua
@@ -25,7 +25,7 @@ local API_GetTime = GetTime
 -- Player's GUID.
 local self_player_guid = nil
 -- Damage event pool.
-local self_pool = OvalePool:NewPool("OvaleDamageTaken_pool")
+local self_pool = OvalePool("OvaleDamageTaken_pool")
 -- Damage event queue: new events are inserted at the front of the queue.
 local self_damageEvent = OvaleQueue:NewDeque("OvaleDamageTaken_damageEvent")
 -- Time window (past number of seconds) for which damage events are stored.
diff --git a/OvaleFuture.lua b/OvaleFuture.lua
index d450862..dc0be41 100644
--- a/OvaleFuture.lua
+++ b/OvaleFuture.lua
@@ -41,7 +41,7 @@ local self_playerName = nil
 local self_activeSpellcast = {}
 -- self_lastSpellcast[targetGUID][spellId] is the most recent spell that has landed successfully on the target.
 local self_lastSpellcast = {}
-local self_pool = OvalePool:NewPool("OvaleFuture_pool")
+local self_pool = OvalePool("OvaleFuture_pool")

 -- Used to track the most recent spellcast started with UNIT_SPELLCAST_SENT.
 local self_lastLineID = nil
diff --git a/OvalePaperDoll.lua b/OvalePaperDoll.lua
index 65c13ce..e3fcd21 100644
--- a/OvalePaperDoll.lua
+++ b/OvalePaperDoll.lua
@@ -41,7 +41,7 @@ local API_UnitSpellHaste = UnitSpellHaste
 local API_UnitStat = UnitStat

 -- Snapshot table pool.
-local self_pool = OvalePool:NewPool("OvalePaperDoll_pool")
+local self_pool = OvalePool("OvalePaperDoll_pool")
 -- Snapshot queue: new snapshots are inserted at the front of the queue.
 local self_snapshot = OvaleQueue:NewDeque("OvalePaperDoll_snapshot")
 -- Time window (past number of seconds) for which snapshots are stored.
diff --git a/OvalePool.lua b/OvalePool.lua
index 1453772..e7222f6 100644
--- a/OvalePool.lua
+++ b/OvalePool.lua
@@ -12,21 +12,34 @@ local _, Ovale = ...
 local OvalePool = {}
 Ovale.OvalePool = OvalePool

+--<private-static-properties>
+local assert = assert
+local setmetatable = setmetatable
+local tinsert = table.insert
+local tremove = table.remove
+local wipe = table.wipe
+--</private-static-properties>
+
 --<public-static-properties>
 OvalePool.name = "OvalePool"
 OvalePool.pool = nil
 OvalePool.size = 0
 OvalePool.unused = 0
+OvalePool.__index = OvalePool
+do
+	setmetatable(OvalePool, { __call = function(_, ...) return NewPool(...) end })
+end
 --</public-static-properties>

---<public-static-methods>
-function OvalePool:NewPool(name)
-	obj = { name = name }
-	setmetatable(obj, { __index = self })
+--<private-static-methods>
+function NewPool(...)
+	local obj = setmetatable({ name = ... }, OvalePool)
 	obj:Reset()
 	return obj
 end
+--</private-static-methods>

+--<public-static-methods>
 function OvalePool:Get()
 	assert(self.pool)
 	local item = tremove(self.pool)
diff --git a/OvalePoolGC.lua b/OvalePoolGC.lua
index 13bce90..7a1259b 100644
--- a/OvalePoolGC.lua
+++ b/OvalePoolGC.lua
@@ -12,21 +12,31 @@ local _, Ovale = ...
 local OvalePoolGC = {}
 Ovale.OvalePoolGC = OvalePoolGC

+--<private-static-properties>
+local setmetatable = setmetatable
+--</private-static-properties>
+
 --<public-static-properties>
 OvalePoolGC.name = "OvalePoolGC"
 OvalePoolGC.pool = nil
 OvalePoolGC.size = 0
 OvalePoolGC.unused = 0
+OvalePoolGC.__index = OvalePoolGC
+do
+	setmetatable(OvalePoolGC, { __call = function(_, ...) return NewPool(...) end })
+end
+
 --</public-static-properties>

---<public-static-methods>
-function OvalePoolGC:NewPool(name)
-	obj = { name = name }
-	setmetatable(obj, { __index = self })
+--<private-static-methods>
+function NewPool(...)
+	local obj = setmetatable({ name = ... }, OvalePoolGC)
 	obj:Reset()
 	return obj
 end
+--</private-static-methods>

+--<public-static-methods>
 function OvalePoolGC:Get()
 	-- Keep running count of total number of tables allocated.
 	self.size = self.size + 1
diff --git a/OvaleQueue.lua b/OvaleQueue.lua
index ad421ee..3693a49 100644
--- a/OvaleQueue.lua
+++ b/OvaleQueue.lua
@@ -12,10 +12,15 @@ local _, Ovale = ...
 local OvaleQueue = {}
 Ovale.OvaleQueue = OvaleQueue

+--<private-static-properties>
+local setmetatable = setmetatable
+--</private-static-properties>
+
 --<public-static-properties>
 OvaleQueue.name = "OvaleQueue"
 OvaleQueue.first = 0
 OvaleQueue.last = -1
+OvaleQueue.__index = OvaleQueue
 --</public-static-properties>

 --<private-static-methods>
@@ -38,9 +43,7 @@ end

 --<public-static-methods>
 function OvaleQueue:NewDeque(name)
-	obj = { name = name, first = 0, last = -1 }
-	setmetatable(obj, { __index = self })
-	return obj
+	return setmetatable({ name = name, first = 0, last = -1 }, OvaleQueue)
 end

 function OvaleQueue:InsertFront(element)
diff --git a/OvaleState.lua b/OvaleState.lua
index 592e081..dab9d58 100644
--- a/OvaleState.lua
+++ b/OvaleState.lua
@@ -45,10 +45,6 @@ local MAX_COMBO_POINTS = MAX_COMBO_POINTS
 local self_runes = {}
 local self_runesCD = {}

--- Static properties used by "GetAura" method.
-local aura_GetAura = {}
-local newAura_GetAura = {}
-
 -- Aura IDs for Eclipse buffs.
 local LUNAR_ECLIPSE = 48518
 local SOLAR_ECLIPSE = 48517
@@ -653,35 +649,40 @@ function OvaleState:GetAuraByGUID(guid, spellId, filter, mine, unitId, auraFound
 	end
 end

-function OvaleState:GetAura(unitId, spellId, filter, mine, auraFound)
-	local guid = OvaleGUID:GetGUID(unitId)
-	if OvaleData.buffSpellList[spellId] then
-		if auraFound then wipe(newAura_GetAura) end
-		local newStart, newEnding, newStacks, newGain
-		for auraId in pairs(OvaleData.buffSpellList[spellId]) do
-			if auraFound then wipe(aura_GetAura) end
-			local start, ending, stacks, gain = self:GetAuraByGUID(guid, auraId, filter, mine, unitId, aura_GetAura)
-			if start and (not newStart or stacks > newStacks) then
-				newStart = start
-				newEnding = ending
-				newStacks = stacks
-				newGain = gain
-				if auraFound then
-					wipe(newAura_GetAura)
-					for k, v in pairs(aura_GetAura) do
-						newAura_GetAura[k] = v
+do
+	local aura = {}
+	local newAura = {}
+
+	function OvaleState:GetAura(unitId, spellId, filter, mine, auraFound)
+		local guid = OvaleGUID:GetGUID(unitId)
+		if OvaleData.buffSpellList[spellId] then
+			if auraFound then wipe(newAura) end
+			local newStart, newEnding, newStacks, newGain
+			for auraId in pairs(OvaleData.buffSpellList[spellId]) do
+				if auraFound then wipe(aura) end
+				local start, ending, stacks, gain = self:GetAuraByGUID(guid, auraId, filter, mine, unitId, aura)
+				if start and (not newStart or stacks > newStacks) then
+					newStart = start
+					newEnding = ending
+					newStacks = stacks
+					newGain = gain
+					if auraFound then
+						wipe(newAura)
+						for k, v in pairs(aura) do
+							newAura[k] = v
+						end
 					end
 				end
 			end
-		end
-		if auraFound then
-			for k, v in pairs(newAura_GetAura) do
-				auraFound[k] = v
+			if auraFound then
+				for k, v in pairs(newAura) do
+					auraFound[k] = v
+				end
 			end
+			return newStart, newEnding, newStacks, newGain
+		else
+			return self:GetAuraByGUID(guid, spellId, filter, mine, unitId, auraFound)
 		end
-		return newStart, newEnding, newStacks, newGain
-	else
-		return self:GetAuraByGUID(guid, spellId, filter, mine, unitId, auraFound)
 	end
 end

diff --git a/compiler.pl b/compiler.pl
index 54f3a53..f843410 100644
--- a/compiler.pl
+++ b/compiler.pl
@@ -99,6 +99,13 @@ $sp{Ovale}{OvalePoolGC} = true;
 $sp{Ovale}{OvaleSkada} = true;
 $sp{Ovale}{OvaleState} = true;

+$sp{OvaleQueue}{Front} = true;
+$sp{OvaleQueue}{FrontToBackIterator} = true;
+$sp{OvaleQueue}{InsertBack} = true;
+$sp{OvaleQueue}{InsertFront} = true;
+$sp{OvaleQueue}{NewDeque} = true;
+$sp{OvaleQueue}{RemoveFront} = true;
+
 opendir(DIR, ".");
 while (defined($r = readdir(DIR)))
 {