Skip to content

Commit

Permalink
table.lua: Add support for decoding bitfield types
Browse files Browse the repository at this point in the history
  • Loading branch information
vmg committed Mar 30, 2016
1 parent e6b7c74 commit fb7d642
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/lua/bcc/table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -273,25 +273,31 @@ local function _decode_table_type(desc)
return t
end

local fields = ""
local fields = {}
local struct = t[3] or "struct"

for _, value in ipairs(t[2]) do
local f = nil

if #value == 2 then
fields = fields .. string.format("%s %s; ", _dec(value[2]), value[1])
f = string.format("%s %s;", _dec(value[2]), value[1])
elseif #value == 3 then
if type(value[3]) == "table" then
fields = fields .. string.format("%s %s[%d]; ", _dec(value[2]), value[1], value[3][1])
else
error("not implemented: "..json_desc)
f = string.format("%s %s[%d];", _dec(value[2]), value[1], value[3][1])
elseif type(value[3]) == "number" then
local t = _dec(value[2])
assert(t == "int" or t == "unsigned int",
"bitfields can only appear in [unsigned] int types")
f = string.format("%s %s:%d;", t, value[1], value[3])
end
else
error("failed to decode type "..json_desc)
end

assert(f ~= nil, "failed to decode type "..json_desc)
table.insert(fields, f)
end

assert(struct == "struct" or struct == "union", "unknown complex type: "..struct)
return string.format("%s { %s}", struct, fields)
return string.format("%s { %s }", struct, table.concat(fields, " "))
end
return _dec(json.parse(json_desc))
end
Expand Down

0 comments on commit fb7d642

Please sign in to comment.