[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Need advice on how to get table index of argument
- From: Andrew Gierth <andrew@...>
- Date: Fri, 14 Feb 2020 04:50:50 +0000
>>>>> "Lee" == Lee Shallis <gb2985@gmail.com> writes:
Lee> I'll start with an example of how the function is called:
Lee> handle:write(0xBADF00D,{0xC0,0xFF,0xEE})
Lee> Would this be the right way to extra each value in the second
Lee> argument's table?
>> if ( !lua_istable(L,3) ) {
In cases like this you have to ask: does the object actually _have_ to
be a table, or could it be anything that's indexable with integers?
If the latter, then lua_istable is too strict a check.
>> array = calloc( size, 1 );
This will leak the allocated array permanently if any of the
checkinteger calls throws an error. You can use a temporary userdata
which will be GC'd, or some other variation.
>> for ( i = 0; i < size; ++i ) {
>> lua_pushinteger(L,i+1);
>> lua_gettable(L,3);
See lua_geti() to combine these two calls
>> array[i] = lua_checkinteger(L,-1);
You need to pop the value off the stack after using it.
And I second the other comments about providing code as plain text with
proper indentation, not mangled into html.
--
Andrew.