How to get the reture value of luaL_dostring

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to get the reture value of luaL_dostring

mos-4

Hi!
        I had thought the following will work::
       
        lua_State *L=lua_open();
        int error = luaL_dostring(L,"return 10");
        printf("%d\n",lua_tonumber(L,-1));

        --output
        0

        not 10.  What can I do then?

mos


Reply | Threaded
Open this post in threaded view
|

Re: How to get the reture value of luaL_dostring

Luiz Henrique de Figueiredo
> lua_State *L=lua_open();
> int error = luaL_dostring(L,"return 10");
> printf("%d\n",lua_tonumber(L,-1));
>
> --output
> 0
>
> not 10.  What can I do then?

In Lua 5.1, luaL_dostring is defined as
        luaL_loadstring(L, s) || lua_pcall(L, 0, 0, 0)

and so it ignores returns.

Try this:

#undef luaL_dostring
#define luaL_dostring(L,s) \
        (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0))
Reply | Threaded
Open this post in threaded view
|

Re: How to get the reture value of luaL_dostring

mos-4


>In Lua 5.1, luaL_dostring is defined as
> luaL_loadstring(L, s) || lua_pcall(L, 0, 0, 0)

>and so it ignores returns.

>Try this:

>#undef luaL_dostring
>#define luaL_dostring(L,s) \
> (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0))

such is the case, thanks for urgent help!

mos