All pastes #1277394 Raw Edit

Dumping complex values in Lua

public lua v1 · immutable
#1277394 ·published 2008-12-06 01:01 UTC
rendered paste body
dump                = {}dump.depth          = 0dump._string        = function(s) return string.format("%q", s) enddump._number        = function(n) return tonumber(n) enddump._boolean       = function(b) if (b) then return "true" end return "false" enddump._userdata      = function(u) return "<userdata>" enddump._function      = function(f) return "<function>" enddump._nil           = function(n) return "nil" enddump._thread        = function(t) return "<thread>" enddump._lightuserdata = function(l) return "<lightuserdata>" enddump._table = function(t)    local buf = ''    if (dump.depth > 0) then        buf = buf .. '{\n'    end    dump.depth = dump.depth + 1;    for k,v in pairs(t) do        buf = buf..string.format('%s[%s] = %s;\n',              string.rep("\t", dump.depth - 1),              dump["_" .. type(k)](k),              dump["_" .. type(v)](v))    end    dump.depth = dump.depth - 1;    if (dump.depth > 0) then        buf = buf .. string.rep("\t", dump.depth - 1) .. '}'    end    return bufend