lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Sunday, March 13, 2011 10:59 PM, HyperHacker said

>> // C library
>> nParms = lua_istable(L,2) ? lua_objlen(L,2) : 0;
>>
>> for (idx = 1; idx <= nParms; ++idx) {
>>   int type = // how do I get the type of the parameter?
>>
> For type, look up lua_type, although lua_isnumber and friends are
> usually what you want.
> Also since your 'params' table has no integer keys, lua_objlen() will
> return zero. You want lua_next() to iterate named keys.

Thanks, I'll change that to use lua_next(). Or maybe I'll have to do both. If there are positional parameters (eg, query = "select sum(x) from tbl where key = :1 and fig = :2"), then the parm would look like parm = { 'fooby', 88 }. I'll work through that, so the question is still how to access tables on the stack using the C API.

I understand how to use lua_type and lua_isnumber, at least once I'm able to get the index of an item on the stack. What I don't understand is how to get the index of items that are in a table in the stack. I've been looking at section 24.2 and the stack dumping example. It would be great if it showed how to dump recursively, but when it finds a table, it just dumps "table" to stdout.

I know how to do this in pure Lua (well, I think that I do), it's just

function tbldmp(t,indent)
 if indent == nil then indent = '..' end
 for (k,v) in pairs(t) do 
   if v then
     print(indent..' '..k..' -> '..v)
     if type(v) == 'table' then tbldmp(v,indent..'..') end
   else
     print(indent..' '..k..' -> nil')
   end
end

How do I do something like that with the C API?

Mike