Question

A table in lua is defined as below

tab = {"Sunday", 14.5, "Tuesday", "Wednesday",
        63, -92, "Saturday", "Saturday", 111}

Lua call a c function, and the tab table is set as a param, this c function should return the table after it has been updated

new_tab_result = call_c_function(..,tab)

I whould like to amend all string values and set them to "DEFAULT", and return the table after the amend to lua.

C Code

while (lua_next(L, 6) != 0)  
{
...
else if(lua_isstring(L, -1))     
{
    lua_pushstring(L, "DEFAULT");
    lua_replace(L, -2);
    k = luaL_checkstring(L, -1);
    log("%s",k) // "DEFAULT"

}
...
lua_pop(L, 1);
}

return 1;
}

Lua Code

for key,value in pairs(new_tab_result) do
  DebugLog(key.."-"..value)
end

result

 1-Sunday
 2-14.5
 3-Tuesday
 4-Wednesday
 5-63
 6--92
 7-Saturday
 8-Saturday
 9-111

String value still have the initial value, while it should has been defaulted to "DEFAULT"

Was it helpful?

Solution

lua_replace works on the stack, not on the table. Use lua_settable or lua_setfield.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top