[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: calling table:function from c/c++
- From: "Nick Trout" <ntrout@...>
- Date: Mon, 31 Mar 2003 12:02:09 -0800
> -- test.lua
> test = {}
> function test:fn (num1, num2)
> return num1 + num2
> end
>
>
> now, I wanted to call this function from c/c++,
>
>
> lua_State *L = lua_open(0);
> lua_baselibopen(L);
> lua_dofile (L, "test.lua");
> lua_getglobal(L, "test:fn");
> lua_pushnumber(L, 100);
> lua_pushnumber(L, 200);
> lua_call(L, 2, 1);
> printf("result : %d\n", (int)lua_tonumber(L, -1));
> lua_close(L);
>
>
> which doesn't work. The program complains about calling nil function.
> My rough guess is that the function "test:fn" belongs to the
> table "test", and not globally known. But, I don't know how to
> correct it.
Lua script:
test:fn(100, 200)
lua2c generates the following:
static int MAIN(lua_State *L)
{
lua_getglobal(L,"test");
lua_pushstring(L,"fn");
lua_gettable(L,-2);
lua_insert(L,-2);
lua_pushnumber(L,100);
lua_pushnumber(L,200);
lua_call(L,3,0);
return 0;
}
so you can use:
lua_getglobal(L,"test");
lua_pushstring(L,"fn");
lua_gettable(L,-2);
lua_insert(L,-2);
lua_pushnumber(L,100);
lua_pushnumber(L,200);
lua_call(L,3,0);
from http://doris.sourceforge.net/lua/weblua.php
Just type code into the text area and hit lua2c.
Nick