lua-users home
lua-l archive

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


Hi all,

I'm new to embedding LUA and am loving it. I had a small issue I'm trying to resolve. It seems if I call pushboolean from C and then catch that value on the other side and print it it breaks. It is probably something I'm doing stupidly. Any help would be awesome!

Output:
a
5
b
test
c

Script:
a, b , c = test()

print("a")
print(a)
print("b")
print(b)
print("c")
print(c) --fails on this call

C Code:

extern "C" {

#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include "lobject.h"
}
#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n)))
static int print(lua_State* L);
static int test(lua_State* L);

static const struct luaL_Reg mylib [] = {
  {"print", print},
  {"test", test},
  {NULL, NULL} /* end of array */
};

static int test(lua_State* L)
{
lua_pushinteger(L,5);
lua_pushfstring(L,"test");
lua_pushboolean(L,1);
return 3;
}

static int print(lua_State* L)
{
int nargs = lua_gettop(L);
for (int i=1; i <= nargs; ++i) {
printf( lua_tostring(L, i));
    }
    printf("\r\n");
return 0;
}


int main()
{
    lua_State *l = luaL_newstate();
luaL_openlibs(l);
lua_getglobal(l, "_G");
luaL_setfuncs(l, mylib, 0);
lua_pop(l, 1);

if(luaL_loadfile(l,"D:\\Michael\\Desktop\\lua\\Debug\\test.lua")==0)
{
lua_pcall(l,0,0,0);
}
else
{
printf("Failed to run script: %s\n", lua_tostring(l, -1));
}
    lua_close(l);

    return 0;
}