Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C & Lua: luaL_dostring return value

Tags:

c

lua

in my C file I call luaL_dostring like this:

luaL_dostring(L, "return 'somestring'");

How do I read this return value in C after this line?

Thanks.

Edit: Thanks for the help.

I'd like to add that to remove the element after retrieving it, you use:

lua_pop(L, 1);
like image 685
Rok Povsic Avatar asked Jan 21 '23 11:01

Rok Povsic


1 Answers

The value is left on the Lua stack. In order to retrieve the value, use one of the lua_toXXXX functions, with -1 as the index argument (-1 refers to the top of the stack). Alternatively, use lua_gettop() to get the size of the stack.

In your case, use this:

luaL_dostring(L, "return 'somestring'");
const char * str = lua_tostring(L, -1);
like image 108
Michal Kottman Avatar answered Jan 28 '23 21:01

Michal Kottman