lua-users home
lua-l archive

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


You're using absolute offsets in lua_isnumber() and lua_tonumber().

Opening libs leaves items on the stack, so accessing stack indices 1 and
2 will access those values.  Use relative offsets instead:

 		if (!lua_isnumber(L, -2) || !lua_isnumber(L, -1))
 		{
 			lua_pushstring(L,"invalid values");
 			lua_error(L);
 		}
 		else 
 		{
 			width = (int)lua_tonumber(L, -2);
 			height = (int)lua_tonumber(L, -1);
 		}

-Kevin

> 
> I have program which reads two values width and height from 
> simple lua file: width=200 
> height=300
> 
> And it works nice if I dont include additional libraries 
> (lua_baselibopen(L);, lua_strlibopen(L); , etc.), but if I 
> include them it does not work, it does not get values instead 
> it gets some non numeric values. Here is the code:
> 
> extern "C"
> {
>  #include <lua.h>
>  #include <lualib.h>
>  #include <lauxlib.h>
> }
> 
> int width, height; /* global variables */
> lua_State *L;
> void load (char *filename);
> 
> int main()
> {
> 	load("luatest.lua");
> 	return 0;
> }
> 
> void load (char *filename) {
> 	L = lua_open();
> //	lua_baselibopen(L);
> //	lua_strlibopen(L);
> //	lua_tablibopen(L);
> //	lua_iolibopen(L);
> //	lua_mathlibopen(L);
> 	if (lua_dofile(L, filename) != 0)
> 	{
> 		lua_pushstring(L,"cannot run configuration file");
> 		lua_error(L);
> 	}
> 	else 
> 	{
> 		lua_getglobal(L, "width");
> 		lua_getglobal(L, "height");
> 		if (!lua_isnumber(L, 1) || !lua_isnumber(L, 2))
> 		{
> 			lua_pushstring(L,"invalid values");
> 			lua_error(L);
> 		}
> 		else 
> 		{
> 			width = (int)lua_tonumber(L, 1);
> 			height = (int)lua_tonumber(L, 2);
> 		}
> 	}
> 	lua_close(L);
> }
> 
> If I uncoment commented lines it doesnt work, if I comment 
> them it works.
> 
> Thank you in advance.
> 
> 
>