lua-users home
lua-l archive

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


On Thu, Jul 30, 2009 at 08:45:05PM +0530, Subhash_Patil wrote:
> I get all the prints in the Lua_main function. I see all the prints
> on the console but not the rectangle.
>
> Can you help me out in this regard.

In your example there is too many errors, including some unrelated to
Lua, and you can not really expect it to be functional.

As first step I would recommend to add a option (switch) to your
compiler let it print warnings on all errors. I use gcc -Wall.

Let me first point some errors in C.
In your function:

> SCSL_Draw_Rect(lua_State*L)
> {
>         GUI_Color_t asvDrawColor = {31, 31, 0};
> 
>         int i,j[3];
>         int n = lua_gettop(L);
> 
>       for (i = 1; i <= n; i++){
>         if (!lua_isnumber(L, i)){
>            lua_pushstring(L, "incorrect argument");
>            lua_error(L);
>          }
>           j[(i-1)] = lua_tonumber(L, i);
> 
>           printf("from stack %d\n",j[(i-1)]);
>       }
>         ML_GUI_DrawRectangle(j[1], j[2], j[3], &asvDrawColor);
> }

> SCSL_Draw_Rect has no declared return type, and no return value, so I
> guess it defaults to 0 as an int. Assuming that lua_pushcfunction with
> a NULL parameter doesn't throw an error, I'd expect lua_call to throw.

It very poor practise to not include return value in function
definition. It default to int but it is always better to be
explicit. I would even recommend to declare it as:
LUALIB_API int SCSL_Draw_Rect(lua_State*L)
And when you do not end function with
return 0;
it does not default to returning zero. If return statement is omitted
returned value is undefined (it can be anything - including zero).
Therefore you must always end function with return statement.

You define variable j as array of 3 elements but subsequently you pack
in it as many numbers as your Lua function gets arguments (it may be
any number of arguments). I guess you want to use 4 argument in this
case so you should define j as
int j[4]
and accept only first 4 argument from Lua stack. If you force
buffer overflow you can not expect anything good to happen!

Also I guess ML_GUI_DrawRectangle function need 5 arguments, but
you are passing it only 4. Maybe you want to write
ML_GUI_DrawRectangle(j[0], j[1], j[2], j[3], &asvDrawColor);

I would write this function something like:

LUALIB_API int SCSL_Draw_Rect(lua_State*L)
{
        GUI_Color_t asvDrawColor = {31, 31, 0};

        int j[4];
        int n = lua_gettop(L);
	if (n != 4) {
	      luaL_error(L, "wrong number of argument passed (expected 4, got %d)", n);
	}
        for (; n; n--) {
            j[(n-1)] = luaL_checkint(L, n);
	}
        ML_GUI_DrawRectangle(j[0], j[1], j[2], j[3], &asvDrawColor);
	return 0;
}

Now to your Lua_main function

> Int Lua_main( void )
> {
>             int error;
>             lua_State *L = lua_open();   /* opens Lua */
>             luaL_Reg Creg;
> 
>             lua_Integer n;
>             luaL_openlibs(L);
> 
>             printf("luaL_openlibs\n");
>             lua_pushcfunction(L, SCSL_Draw_Rect(L));
>             printf("lua_pushcfunction\n");
> 
> /*            n = 0;
>             lua_pushinteger(L, n);*/
>             n = 100;
>             lua_pushinteger(L, n);
>             n = 100;
>             printf("lua_pushinteger\n");
>             lua_pushinteger(L, n);
>             n = 300;
>             lua_pushinteger(L, n);
>             n = 300;
>             lua_pushinteger(L, n);
>             printf("lua_call\n");
>             lua_call( L, 5, 0);
>             printf("lua_call\n");
>             lua_close(L);
> 
>             return 0;
> }

As Duncan Cross and Jerome Vuarand said you should use
lua_pushcfunction(L, SCSL_Draw_Rect);
   and
lua_call( L, 4, 0);
instead of
lua_pushcfunction(L, SCSL_Draw_Rect(L));
  and
lua_call( L, 5, 0);

Since this is for testing only I would personally drop all
lua_pushfunction, lua_pushinteger and lua_call and instead write

    if (luaL_dostring(L, "SCSL_Draw_Rect(100, 100, 300, 300)") {
        const char *err = luaL_optstring(L, -1, NULL);
	if (err) {
	     puts(err);
	     lua_pop(L, 1);
	}
     }
Of course this assume that you have registered SCSL_Draw_Rect as Lua
function which can not be seen from your snippet. But there is
>             luaL_Reg Creg;
so I assume you know what it is for and you use it.

I do fully agree with Jerome that the code as you passed in here is so
bad that symptoms you describe are the only one that shows. 
> I get all the prints in the Lua_main function. I see all the prints
> on the console but not the rectangle.

Also it in not clear where you use this function
> Int Gui_Draw_Rectangle( x,y,Width,Height, /*(Gui_color_t *)color*/){
> Gui_color_t color; // I have had coded this for testing LUA; I am not comfortable with tables yet

You could look at wiki for many helpful article. Some of them
http://lua-users.org/wiki/SimpleLuaApiExample)
http://lua-users.org/wiki/BindingCodeToLua
http://lua-users.org/wiki/UserDataExample
http://lua-users.org/wiki/UserDataWithPointerExample
http://lua-users.org/wiki/UserDataEnvironmentExamples
http://lua-users.org/wiki/BindingWithMembersAndMethods

You should really activate all warning with your compiler and try to
solve them.

Martin