Last active
January 3, 2022 15:41
-
-
Save vadimkantorov/1fca5a6515962f106c103a77819c37e2 to your computer and use it in GitHub Desktop.
A Lua repl that copies local variables onto globals for inspection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local torch_repl = repl | |
function repl(restoreGlobals) | |
-- copied from http://stackoverflow.com/questions/33068607/how-can-i-use-the-torch-repl-for-debugging | |
require 'trepl' | |
restoreGlobals = restoreGlobals or false | |
-- optionally make a shallow copy of _G | |
local oldG = {} | |
if restoreGlobals then | |
for k, v in pairs(_G) do | |
oldG[k] = v | |
end | |
end | |
-- copy upvalues to _G | |
local i = 1 | |
local func = debug.getinfo(2, "f").func | |
while true do | |
local k, v = debug.getupvalue(func, i) | |
if k ~= nil then | |
_G[k] = v | |
else | |
break | |
end | |
i = i + 1 | |
end | |
-- copy locals to _G | |
local i = 1 | |
while true do | |
local k, v = debug.getlocal(2, i) | |
if k ~= nil then | |
_G[k] = v | |
else | |
break | |
end | |
i = i + 1 | |
end | |
torch_repl() | |
if restoreGlobals then | |
_G = oldG | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment