Read the Lua table from C

I'm trying to pass a Lua table to my C program, but I don't know how to do it.

My Lua Code:

local stages = {} stages[1] = stage1 stages[2] = stage2 stages[3] = stage3 lstage.buildpollingtable(stages) 

My C code:

 static int lstage_build_polling_table (lua_State * L) { luaL_checktype(L, 1, LUA_TTABLE); lua_getfield(L, 1, "stage1"); lua_getfield(L, 1, "stage2"); lua_getfield(L, 1, "stage3"); stage_t s1 = lstage_tostage(L, -3); stage_t s2 = lstage_tostage(L, -2); stage_t s3 = lstage_tostage(L, -1); printf("%d\n",s1->priority); printf("%d\n",s2->priority); printf("%d\n",s3->priority); return 1; } 

What do I need to do to start on all elements? This code generates the following error:

Bad argument # -3 to 'buildpollingtable' (lstage-Stage * expected, got table)

Can someone explain what I'm doing wrong?

+6
source share
1 answer

There are no fields in your table with the name stage1 , etc., only fields 1 , 2 , 3 . Therefore try

 lua_rawgeti(L,1,1); lua_rawgeti(L,1,2); lua_rawgeti(L,1,3); 

instead

 lua_getfield(L, 1, "stage1"); lua_getfield(L, 1, "stage2"); lua_getfield(L, 1, "stage3"); 
+4
source

Source: https://habr.com/ru/post/977388/


All Articles