lua_pcall returns a value 2
I have a test.lua file that contains the following code
I am trying to run that code using the dofile method:
The return value from the dofile method is 0
The return value from the lua_pcall method is: 2
what can i do to fix it ? why am i getting an error with such a simple code ??
Code:
function lFunction()
print("Hello From Lua!")
endI am trying to run that code using the dofile method:
Code:
.....
lua_State *L = lua_open();
luaL_openlibs(L);
int returnvalue1 = luaL_dofile(L,"test.lua");
cout<<"The return value from the dofile method is: "<<returnvalue1<<" \n";
lua_getglobal(L, "lFunction");
lua_pcall(L, 0, 0, 0);
int returnvalue2= lua_pcall(L, 0, 0, 0);
cout<<"The return value from the lua_pcall method is: "<<returnvalue2<<" \n";
lua_close(L);
.....The return value from the dofile method is 0
The return value from the lua_pcall method is: 2
what can i do to fix it ? why am i getting an error with such a simple code ??
you have (accidentally, I guess) 2 calls to lua_pcall, the first of which will consume the function, and the second of which will error on the empty stack.
You should really use better error handling to make errors more meaningful than simple numbers.
Documentation of lua_pcall:
http://pgl.yoyo.org/luai/i/lua_pcall
(The return value is a string holding the error message)
A return value of two corresponds to LUA_ERRRUN, meaning an error running the code, according to the headers
To get your proper error message, get the value on the top of the stack and print it:
[/quote]
Documentation of lua_pcall:
http://pgl.yoyo.org/luai/i/lua_pcall
Quote:In case of runtime errors, this function will be called with the error message and its return value will be the message returned on the stack by lua_pcall.
(The return value is a string holding the error message)
A return value of two corresponds to LUA_ERRRUN, meaning an error running the code, according to the headers
Code:
/* thread status; 0 is OK */
#define LUA_YIELD 1
#define LUA_ERRRUN 2
#define LUA_ERRSYNTAX 3
#define LUA_ERRMEM 4
#define LUA_ERRERR 5To get your proper error message, get the value on the top of the stack and print it:
Code:
lua_State *L = lua_open();
luaL_openlibs(L);
int errorCode = 0;
if(errorCode = luaL_dofile(L,"test.lua"))
{
puts(lua_tostring(L, -1));
abort();
}
lua_getglobal(L, "lFunction");
if(errorCode = lua_pcall(L, 0, 0, 0))
{
puts(lua_tostring(L, -1));
abort();
}
lua_close(L);
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Lua and lua_pcall problems | ChrisS | 2 | 4,818 |
Nov 5, 2005 10:24 PM Last Post: phydeaux |
|

