<commit_before>local overview = 
[[
    LIB_NAME       : debug
    "getinfo"      : call with a function or with a number indicating the stack level.
                   : return a table of decription of the arg.
                   : notice: low efficiency, use second arg to specified concrete info you need.
    "traceback"    : return a traceback string.
    "getlocal"     : getlocal variable info
    "getupvalue" : get 
]]

local debugLibTest = { 
    cases = {} 
}

local traceback = function()
    for level=1, math.huge do
        local info = debug.getinfo(level, "Sl")
        if not info then
            break
        end
        if info.what == "C" then
            print(level, "C function")
        else
            print(string.format("[%s]: %d", info.short_src, info.currentline))
        end
    end
end

debugLibTest.cases["getinfo"] = 
{
    f = function()
        traceback(traceback)
    end
}

debugLibTest.cases["traceback"] = {
    f = function()
        local traceStr = debug.traceback()
        print("traceStr: " .. tostring(traceStr))
    end
}

debugLibTest.cases["getlocal"] = {
    f = function(a, b)
        local x
        do local c = a -b end
        while true do
            local name, value = debug.getlocal(1, a)
            if not name then
                break
            end
            print(name, value)
            a = a + 1
        end
    end,
    args = {10, 20}
}

debugLibTest.cases["getupvalue"] = {
    f = function(name)
        local val, found 

        -- try local
        for i=1,math.huge do
            local n, v = debug.getlocal(2, i)
            if not n then break end
            if n == name then
                val = v
                found = true
            end
        end
        if found then
            return val
        end

        -- try non-local 
        local func = debug.getinfo(2, "f").func
        for i=1, math.huge do
            local n, v = debug.getupvalue(func, i)
            if not n then
                break
            end
            if n == name then
                return v
            end
        end

        -- still not found, try env.
        return getfenv(func)[name]
    end,
    -- args = {"traceback"}
    -- args = {"overview"}
    args = {"valForUpvalue"} -- defined in the debugLibTest.test function 
}

debugLibTest.cases["trace coroutine"] = {
    f = function()
        co = coroutine.create(function()
            local x = 10
            coroutine.yield()
            error("some error")
        end)

        coroutine.resume(co)
        print(debug.traceback(co))
    end
}

-- how to print vaargs 
local ap = function(val)<commit_msg>debug lib<commit_after>local overview = 
[[
    LIB_NAME        : debug
    "getinfo"       : call with a function or with a number indicating the stack level.
                    : return a table of decription of the arg.
                    : notice: low efficiency, use second arg to specified concrete info you need.
    "traceback"     : return a traceback string.
    "getlocal"      : getlocal variable info
    "getupvalue"    : get non-local variable info
    "sethook"       :
]]

local debugLibTest = { 
    cases = {} 
}

local traceback = function()
    for level=1, math.huge do
        local info = debug.getinfo(level, "Sl")
        if not info then
            break
        end
        if info.what == "C" then
            print(level, "C function")
        else
            print(string.format("[%s]: %d", info.short_src, info.currentline))
        end
    end
end

debugLibTest.cases["getinfo"] = 
{
    f = function()
        traceback(traceback)
    end
}

debugLibTest.cases["traceback"] = {
    f = function()
        local traceStr = debug.traceback()
        print("traceStr: " .. tostring(traceStr))
    end
}

debugLibTest.cases["getlocal"] = {
    f = function(a, b)
        local x
        do local c = a -b end
        while true do
            local name, value = debug.getlocal(1, a)
            if not name then
                break
            end
            print(name, value)
            a = a + 1
        end
    end,
    args = {10, 20}
}

debugLibTest.cases["getupvalue"] = {
    f = function(name)
        local val, found 

        -- try local
        for i=1,math.huge do
            local n, v = debug.getlocal(2, i)
            if not n then break end
            if n == name then
                val = v
                found = true
            end
        end
        if found then
            return val
        end

        -- try non-local 
        local func = debug.getinfo(2, "f").func
        for i=1, math.huge do
            local n, v = debug.getupvalue(func, i)
            if not n then
                break
            end
            if n == name then
                return v
            end
        end

        -- still not found, try env.
        return getfenv(func)[name]
    end,
    -- args = {"traceback"}
    -- args = {"overview"}
    args = {"valForUpvalue"} -- defined in the debugLibTest.test function 
}

debugLibTest.cases["trace coroutine"] = {
    f = function()
        co = coroutine.create(function()
            local x = 10
            coroutine.yield()
            error("some error")
        end)

        coroutine.resume(co)
        print(debug.traceback(co))
    end
}

debugLibTest.cases["sethook"] = {
    f = function()
        debug.sethook(print, "l")
        print("hello world")
        print("hello world")
        print("hello world")
        print("hello world")
        print("hello world")
    end
}

-- how to print vaargs 
local ap = function(val)
{'fraction_non_alphanumeric': 0.09951931636104683, 'fraction_numerical': 0.004628805412141713, 'mean_word_length': 1.4738000880669309, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5254742', 'n_tokens_mistral': 1666, 'n_tokens_neox': 1633, 'n_words': 504}