|
What are you exactly worried about? If you're writing code in C, then the stack is only one of your lesser concerns I would think.
> -----Original Message-----
> From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org] On
> Behalf Of Austin Einter
> Sent: dinsdag 24 juni 2014 9:58
> To: Lua mailing list
> Subject: Re: Simple Function Call From C language
>
> Hi Sean
> Your example worked pretty good.
> Thanks.
> I have some more doubt. If I want to avoid function stuff, just execute
> chunk, and get the result in C code, how best I can do it.
> Wrote below code, and working ok, please let me know if there is a better
> way to do it.
> And my MAJOR worry is this way am I bringing up any issue in stack (in C or
> in Lua) content, how do I make sure I am not messing up with stack content.
>
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <lua5.2/lua.h>
> #include <lua5.2/lualib.h>
> #include <lua5.2/lauxlib.h>
>
> const char *c_function = "local a=5 local b=10 local c=a+b return c";
>
> int main(void)
> {
> lua_State *L;
> int r;
>
> L = (lua_State *) luaL_newstate();
> luaL_openlibs(L); /* open all built-in Lua libraries */
> luaL_dostring(L,c_function);
> r = luaL_checkinteger(L, -1);
> printf("\nResult = %d",r);
> lua_close(L);
> return EXIT_SUCCESS;
> }
>
>
>
>
> On Tue, Jun 24, 2014 at 9:12 AM, Sean Conner <sean@conman.org> wrote:
> It was thus said that the Great Austin Einter once stated:
> > Hi
> > Thanks a lot. Referring your links I could do a small test program.
> However
> > I am not able to get the output of lua function in C code. Here is my
> > example code. Please let me know, how to collect the lua function output
> in
> > C code. After I call lua_loadstring, I want to print function output in
> > Code.., please let me know how to do it...
> #include <stdio.h>
> #include <stdlib.h>
> #include <lua.h>
> #include <lualib.h>
> #include <lualib.h>
>
> const char *c_function = "function fact(n) if n == 0 then return 1 else
> return n * fact(n-1) end end"
>
> int main(void)
> {
> lua_State *L;
> int r;
>
> L = luaL_newstate();
> luaL_openlibs(L); /* open all built-in Lua libraries */
>
> /*------------------------------------------------------------------------
> ; the following two lines are basically luaL_dostring(). The first line
> ; compiles the given Lua code into Lua bytecode and returns the result on
> ; the Lua stack (not to be confused with the C stack---they are
> ; different). For the example above, the resulting code needs to be
> ; executed to defien the fact() function. Since luaL_loadstring() returns
> ; the compiled code on the Lua stack, we can simply call lua_pcall() to
> ; execute it.
> ;
> ; You might think that the function fact() will be defined at this point,
> ; but no, all that's been done by luaL_loadstring() is to generate the
> ; code that will define the fact() function.
> ;------------------------------------------------------------------------
> */
>
> luaL_loadstring(L,c_function);
> lua_pcall(L,0,0,0);
>
> /*-----------------------------------------------------------------------
> ; now that we have fact() defined, we can now call it. This just follows
> ; the example given for the description of lua_call() in the manual.
> ;----------------------------------------------------------------------*/
>
> lua_getglobal(L,"fact"); /* this is our function, defined globally */
> lua_pushinteger(L,4); /* this call fact(4) */
> lua_call(L,1,1); /* call fact, with one argument, and expect
> one result */
>
> r = luaL_checkinteger(L,-1); /* the top of the Lua stack should contain
> our result */
> printf("Factorial: %d",r);
>
> lua_close(L);
> return EXIT_SUCCESS;
> }
>
> -spc
>