Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use %p (pointer) format as basis for object ids #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions dump.lua
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,37 @@ local function dump_state(file, options)
processed[obj] = processed_counter;
return processed_counter;
end
local function skip(obj)
processed[obj] = 0;
end

if pcall(string.format, "%p", "") then
local format = string.format;
local function new_id(obj)
local t = type(obj);
if t == "function" or t == "string" or t == "table" or t == "userdata" or t == "thread" then
return format("%s:%p", t, obj);
elseif t == "number" or t == "boolean" or t == "nil" then
return format("%s:%q", t, obj);
else
error("don't know how to reference a "..t)
end
end

function skip(obj)
processed[ new_id(obj) ] = 0;
end

function get_id(obj) --> string | 0
if nil == obj then return 0; end
local id = new_id(obj);
if not processed[id] then
push(obj);
processed[id] = 0;
end
return id;
end
end

local escapes = {
["\""] = "\\\"", ["\\"] = "\\\\", ["\b"] = "\\b",
Expand Down Expand Up @@ -159,8 +190,8 @@ local function dump_state(file, options)
thread_mt = debug.getmetatable(debug.getregistry()[1]);
};

processed[dump_state] = 0;
processed[root] = 0;
skip(dump_state);
skip(root);

push(root);

Expand Down