local _G = getfenv(0)

local nullfunc = setfenv(function() end, getfenv(0))
_G.jit.attach = nullfunc

local hash = ""
local signatures = {}

local function sort_keys(t)
    local new_table = {}

    for k, v in pairs(t) do
        table.insert(new_table, k)
    end

    table.sort(new_table)

    local ret_table = {}

    for _, v in ipairs(new_table) do
        table.insert(ret_table, v)
    end

    return ret_table
end

local function generate_signature(proto)
    local info = jit.util.funcinfo(proto, 0)

    local signature_data = {info.source, info.loc, info.bytecodes}

    for pc = 1, 64 do
        local ins, op = jit.util.funcbc(proto, pc)

        if not ins then break end

        table.insert(signature_data, ins)
        table.insert(signature_data, op)
    end

    return table.concat(signature_data)
end

jit.attach(function(proto)
    local signature = generate_signature(proto)

    if not signatures[signature] then
        signatures[signature] = true

        local hash_data = {}

        local sorted = sort_keys(signatures)

        for _, signature in ipairs(sorted) do
            table.insert(hash_data, signature)
        end

        hash = util.CRC(table.concat(hash_data))

        print("NEW HASH", hash)
    end
end, "bc")












