浏览代码

Check for recursions when printing Lua tables in the test

bel 11 年之前
父节点
当前提交
1b9430718d
共有 1 个文件被更改,包括 12 次插入3 次删除
  1. 12 3
      test/page2.lua

+ 12 - 3
test/page2.lua

@@ -16,12 +16,19 @@ function print_if_available(tab, name)
   end
 end
 
-function recurse(tab)
+function recurse(tab, excl)
+  excl = excl or {}
   mg.write("<ul>\n")
   for k,v in pairs(tab) do
     if type(v) == "table" then
       mg.write("<li>" .. tostring(k) .. ":</li>\n")
-      recurse(v)
+      if excl[v] then
+        -- cyclic
+      else
+        excl[v] = true
+        recurse(v, excl)
+        excl[v] = false
+      end
     else
       mg.write("<li>" .. tostring(k) .. " = " .. tostring(v) .. "</li>\n")
     end
@@ -40,6 +47,8 @@ mg.write("</ul>\n")
 print_if_available(sqlite3, "sqlite3 binding")
 print_if_available(lfs, "lua file system")
 
+--recurse(_G)
+
 -- Print mg library
 libname = "mg"
 print_if_available(_G[libname], libname .. " library")
@@ -75,4 +84,4 @@ end
 mg.write([[
 </p>
 </body></html>
-]])
+]])