changeset 62:619e4b9c3cd9

Stop recursion loops
author John@Yosemite-PC
date Mon, 26 Mar 2012 19:20:27 -0400
parents 9b1588bd4398
children 00cb497201d0
files Utility.lua
diffstat 1 files changed, 16 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/Utility.lua	Mon Mar 26 09:51:57 2012 -0400
+++ b/Utility.lua	Mon Mar 26 19:20:27 2012 -0400
@@ -2,6 +2,7 @@
 local pairs=pairs
 local tostring=tostring
 local type=type
+local table=table
 local getmetatable=getmetatable
 local setmetatable=setmetatable
 
@@ -37,11 +38,12 @@
   return setmetatable(u, getmetatable(t))
 end
 
-function PrintTable(table, depth)
+function PrintTable(tabl, depth, history)
     depth = depth or ""
-    if not table then return end
+    history = history or {}
+    if not tabl then return end
     if #depth > 3*5 then print(depth.."Recursion too deep - stopping"); return end
-    for i,v in pairs(table) do 
+    for i,v in pairs(tabl) do 
         if( type(v) == "string" ) then
             print(depth .. i ..  " - " .. v) 
         elseif( type(v) == "number" ) then
@@ -49,7 +51,17 @@
         elseif( type(v) == "table" ) then
             --if v == table then return end
             print(depth .. tostring(i) .." - ") 
-            PrintTable(v,depth.."   ")
+            if history then
+                for i,vh in pairs(history) do
+                    if vh==tabl then
+                        print(depth.. " - circular reference detected; halting print")
+                        return
+                    end
+                end
+            end
+            table.insert(history,tabl)
+            PrintTable(v,depth.."   ",history)
+            table.remove(history)
         elseif( type(v) == "boolean" ) then
             print(depth .. i .. " - " .. tostring(v))
         elseif( type(v) == "function" ) then