return value of lua_dostring

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

return value of lua_dostring

Luiz Henrique de Figueiredo
>How can I get the return value of the Lua chunk executed by lua_dostring
>from the C side, i.e. like getting the result returned by the Lua dostring
>function?

Just like the return values of any function.
Here is an example:

  lua_State *L=lua_open(0);
  printf("%d\n",lua_gettop(L));
  lua_dostring(L,"return 1,'a'");
  printf("%d\n",lua_gettop(L));
  printf("%s\n",lua_tostring(L,-2));
  printf("%s\n",lua_tostring(L,-1));

In general, you should take the k top values in the stack, where k is
the difference between lua_gettop(L) before calling lua_dostring and
lua_gettop(L) after calling it, that is, for indices from -1 to -k.
--lhf

Reply | Threaded
Open this post in threaded view
|

Re: return value of lua_dostring

Reuben Thomas-3
> Just like the return values of any function.

Thanks. I presume that means that as per the notice "you are responsible for
controlling stack overflow", that means that after calling lua_dostring it
is necessary to call lua_settop(L, 0) to remove any remaining stack elements
before calling lua_dostring again (to avoid eventual stack overflow)?

This used to be handled by lua_beginblock/lua_endblock, didn't it?

-- 
http://sc3d.org/rrt/ | computation, n.  automated pedantry


Reply | Threaded
Open this post in threaded view
|

Re: return value of lua_dostring

Luiz Henrique de Figueiredo
In reply to this post by Luiz Henrique de Figueiredo
>> Just like the return values of any function.
>
>Thanks. I presume that means that as per the notice "you are responsible for
>controlling stack overflow", that means that after calling lua_dostring it
>is necessary to call lua_settop(L, 0) to remove any remaining stack elements
>before calling lua_dostring again (to avoid eventual stack overflow)?

Yes, but lua_settop(L,n), where was n=lua_gettop(L) before the call to
lua_dostring. Or lua_pop(L,k), where k is the number of values returned
by lua_dostring (computed as the difference of lua_gettop(L) before and after
the call.

>This used to be handled by lua_beginblock/lua_endblock, didn't it?

Yes.
--lhf