1.问题的起因
项目里使用了一个pretty的lua库,它提供了一个打印Lua table的功能,大体做法就是分别用ipairs 和 pairs分别尝试打印table中的值。但是当打印protobuf decode的table时就报错了。
2.问题跟踪
protobuf使用的是cloudwu的pbc。
通过跟踪protobuf的代码,发现protobuf decode返回的table是有metatable的,ipairs会尝试使用整数索引遍历table,如果table不存在整数索引的kv,则会去自动查询metatable。由于kv不存在,后续就报错了。后续的报错与protobuf有关,这里不展开了,这里只关注ipairs。
3.example
local t = {
}
setmetatable(t, {
__index = {
1,