[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: passing lua table from c++ to lua script
- From: Wesley Smith <wesley.hoke@...>
- Date: Sun, 20 May 2012 01:44:08 +0000
I think you need to do move the function one stack index below the
table before calling the function. Try calling lua_insert(L, -2)
after you grab the function. Also, it's often useful for debugging to
have a C function that can print out the stack so you can see exactly
what's going on.
wes
On Sun, May 20, 2012 at 1:38 AM, Maha Akkari <maha.akkari@gmail.com> wrote:
> hello,
>
> I have spent the past 6 hours trying to solve this ! and i coulnt get
> anywhere :s
>
> I want to be able to create a lua table in a c++ file and then pass that to
> a lua script file, which has the following lua function:
>
> function MTable (t)
> local n=#t
> for i=1,n do
> print(t[i])
> end
> end
>
> i dynamically created a one dimensional array with two strings:
>
> lua_newtable(L);
> lua_pushstring(L,"10.10.1.1");
> lua_pushstring(L,"10.10.1.2");
> lua_rawseti(L,-3,2);
> lua_rawseti(L,-2,1);
>
> so now i have the table on top of the stack.
> I have verified it by writting this :if( lua_istable(L,lua_gettop(L)))
> which returned 1, which means it is a table.
>
> then I did this:
> lua_getglobal(L, "MTable"); // push the lua function onto the stack
>
> uint32_t result = lua_pcall(L, 1, 0, 0); //argument 1 is for the table
> if (result) {
> printf(stderr, "Failed to run script: %s\n", lua_tostring(L, -1));
> exit(1);
> }
>
> so I got that error:
> Failed to run script: attempt to call a table value
>
> Kindly note that the file has several other functions that i am calling
> successfully from c++.
>
> can somebody please help me solve this error ? can this be a bug from LUA?
> cz i followed the steps very correctly...i guess !