lua-users home
lua-l archive

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



Am 08.12.2012 um 22:17 schrieb Kevin Martin:


On 8 Dec 2012, at 18:54, Philipp Kraus wrote:

Can you send me a working example for 5.2? I try to push a table with function pointer
to call them in my script. I try to create this:

     lua_newtable(m_lua);
     luaL_setfuncs( m_lua, &l_functions[0], 0);
     lua_pushvalue(m_lua, -1);
     lua_setglobal(m_lua, p_libname.c_str());

but in my script I can not call any of my functions (they don't exist). I would like to put
a set of "global functions" to the script.

I'm a little confused, I thought your earlier posts were about subtables, but in this example it just looks like you're trying to put your functions in one single global table?

This example puts two functions in a table, and then sets the table as a global


If you're prepared to make the package system available to your scripts, it may be better to use require, luaL_requiref, and luaL_newlib


okay I have found my error:

luaL_newlib( m_lua, &l_functions[0] );
lua_setglobal( m_lua, "pre.sub" );

so I have implementated a splitting of the string and a recursiv lua_newtable & lua_setfield creation:

void pushTable( packagename, isfirst, islast, luaL_Reg* ptr )
{


if !islast
{
if !isfirst
     lua_newtable(L)
     
      pushTable( next(packagename), current_package_isfirst, next_package_islast(packagename), ptr)
           if !isfirst
     lua_setfield(L)
} else
     lua_newlib( L, ptr )

if isfirst
     lua_setglobal( L, packagename )
}


How I can search if a table exists eg I call the function like

pushTable( ["first", "second", "third"], true, false, ptr1 )
pushTable( ["first", "second", "forth"], true, false, ptr2 )

In this case the second table must be append with the data, how can I check if the table exists and append a dataset?

Thanks

Phil