I'm using Lua inside a C application, and I have two tables. I want to create a third table that, while empty, will index values from my first two tables. I wrote the following simple example in Lua -
a = { one="1", two="2" }
b = { three="3", four="4" }
meta = { __index = function(t,k)
if a[k] == nil then return b[k]
else return a[k] end
end }
c = {}
setmetatable(c, meta)
print(c.one) -- prints "1"
print(c.four) -- prints "4"
My question is, what is the most effective way to do this from the C API? I've been able to do this by creating a new table, pushing the above Lua code chunk to that table, then calling setmetatable() on it but this seems less than optimal. Is there a better way?