lua-users home
lua-l archive

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


On Thu, 13 Feb 2020 at 18:49, Lee Shallis <gb2985@gmail.com> wrote:
> I'll start with an example of how the function is called:
> handle:write(0xBADF00D,{0xC0,0xFF,0xEE})
> Would this be the right way to extra each value in the second argument's table?
>>
>> int lua_proc_change_data( lua_State *L ) {
>> proc_handle_t **handle = (proc_handle_t**)
>> luaL_checkudata(L,1,PROC_HANDLE_CLASS);
>> intptr_t addr = luaL_checkinteger(L,2);
>> intptr_t size, i;
>> uchar * array;
>> if ( !lua_istable(L,3) ) {
>> lua_pushinteger(L,0);
>> return 1;
>> }
>> size = lua_len(L,3);
>> if ( size < 1 ) {
>> lua_pushinteger(L,0);
>> return 1;
>> }
>> array = calloc( size, 1 );
>> for ( i = 0; i < size; ++i ) {
>> lua_pushinteger(L,i+1);
>> lua_gettable(L,3);
>> array[i] = lua_checkinteger(L,-1);
>> }
>> size = proc_change_data( NULL, *handle, addr, array, size );
>> free(array);
>> if ( size < 1 ) {
>> lua_pushinteger(L,0);
>> return 1;
>> }
>> lua_pushinteger(L,0);
>> return 1;
>> }

That should work for small tables as second argument. The right way
needs a lua_pop(L,1) after lua_checkinteger(L,-1).

As a side note, you should send your emails to this mailing list in
plain text, and when you include code it would help to have it
indented. That was so hard to read I probably won't bother next time I
see your big font and your fake quote.