lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Sat, Aug 01, 2009 at 09:00:05PM +0530, Subhash_Patil wrote:
> int Draw_Rect(lua_State*Lua_VM)
> {
>         int i,j[5], n;
> 
>         n = lua_gettop(Lua_VM);
>         printf("stack size:%d\n",n );
> 
>        for (i = 1; i <= n; i++)
>         {
>                 if ( ! lua_isnumber(Lua_VM, -1))
>                 {
>                         printf("from stack not an integer:%d\n",i );
>                 }
>                 else
>                 {
>                         j[(i-1)] = lua_tonumber(Lua_VM, -1);
>                         lua_pop(Lua_VM, 1);
>                         printf("from stack %d\n",j[(i-1)]);
>                 }
>                 if (  lua_isnumber(Lua_VM, n-1))
>                 {
>                         printf("from stack n-1 is an integer:%d\n",n-1 );
>                 }
>         }
>         GUI_DrawFilledRect(j[4] , j[3] , j[2] , j[1] , j[0] );
>         return 0;
> }

To investigate depth and content of stack You may use function
void stackDump(lua_State *L) as shown in  book 'Programming in Lua'.
You can change it a little if You want it to be little more verbose.
Your handling of stack in Draw_... functions is wrong.
For one you do not handle errors.
When condition  'if ( ! lua_isnumber(Lua_VM, -1))' is
true you need to raise an error (not only print debugging info).
Another condition 'if (  lua_isnumber(Lua_VM, n-1))' is referring
to constant stack slot and this slot becomes invalid after two
lua_pop(Lua_VM, 1)
You should ask yourself what happens if Lua script calls Your function
with wrong number of argument and take care of this case.  I would
suggest to use function in shape I posted to You inprevoiuss mail
eventually adding some call to stackDump until you have understanding
what is happening on the stack.

>         lua_setglobal(Lua_VM, " DrawRect");
>         lua_setglobal(Lua_VM, " DrawFill_Circle");

>From Lua manual reference:
 void lua_setglobal (lua_State *L, const char *name);

   Pops a value from the stack and sets  it as the new value of global  name.

This means you need first to push function on the stack and then call
lua_setglobal. Do this:

        lua_pushcfunction(Lua_VM, Draw_Rect);
        lua_setglobal(Lua_VM, "DrawRect");
        lua_pushcfunction(Lua_VM, DrawFill_Circle);
        lua_setglobal(Lua_VM, "DrawFill_Circle");

> /************startup.lua*************
> 
> io.write("Hello File load successful from LUA \n")
> SCSL_DrawFill_Circle(0,300,100,50)

You registered function to Lua as " DrawFill_Circle" not as
SCSL_DrawFill_Circle.. Also note that you do not want to include space
as the first char in function name.

Good luck
Martin