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

cmsgpack.pack(t) got empty #67

Open
ffbh123456 opened this issue Oct 12, 2021 · 1 comment
Open

cmsgpack.pack(t) got empty #67

ffbh123456 opened this issue Oct 12, 2021 · 1 comment

Comments

@ffbh123456
Copy link

ffbh123456 commented Oct 12, 2021

local cmsgpack = require("cmsgpack")

local function to_readonly_table(input_table)
    if input_table == nil then
        return nil
    end
    assert(type(input_table) == 'table')
    local travelled_tables = {}
    local function __read_only(tbl)
        if not travelled_tables[tbl] then
            local tbl_mt = getmetatable(tbl)
            if not tbl_mt then
                tbl_mt = {}
                setmetatable(tbl, tbl_mt)
            end

            local proxy = tbl_mt.__read_only_proxy
            if not proxy then
                proxy = {}
                tbl_mt.__read_only_proxy = proxy
                local proxy_mt = {
                    __index = tbl,
                    __newindex = function(t, k, v)
                        error("error write to a read-only table with key = " .. tostring(k))
                    end,
                    __pairs = function(t)
                        return pairs(tbl)
                    end,
                    __len = function(t)
                        return #tbl
                    end,
                    __read_only_proxy = proxy
                }
                setmetatable(proxy, proxy_mt)
            end
            travelled_tables[tbl] = proxy
            for k, v in pairs(tbl) do
                if type(v) == "table" then
                    tbl[k] = __read_only(v)
                end
            end
        end
        return travelled_tables[tbl]
    end
    return __read_only(input_table)
end

local t = to_readonly_table({
        a = 1,
        b = 2
    })
local tmp = cmsgpack.pack(t)
local origin = cmsgpack.unpack(tmp) -- this one become empty
@ffbh123456
Copy link
Author

When I set the lua table to read-only, cmsgpack.pack returns empty

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant