lua-users home
lua-l archive

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


3: The luaopen_* methods cannot be called directly from C; they should
be called as Lua C functions like so:
lua_pushcfunction(L, luaopen_io);
lua_pushliteral(L, "io");
lua_call(L, 1, 0);
Or, like so:
lua_cpall(L, luaopen_io, "io");

2: One way of doing it might be:
lua_pushlightuserdata( myLua, reinterpret_cast<void*>(&myClass) );
lua_pushcclosure ( myLua, MyClass::MyFuncCode, 1 );
lua_setglobal ( myLua, "my_func" );

then from within MyFuncCode:
MyClass *self = reinterpret_cast<MyClass*>(lua_touserdata(L,
lua_upvalueindex(1)));

1: If you mean a Lua thread (i.e. coroutine), then where is the
problem? If you mean C thread, then just don't export a function which
allows creation of C threads.

On Fri, Jan 23, 2009 at 11:00 AM, Ziv Barber <zivbarber@gmail.com> wrote:
> Hi,
>
> First, I have to say that Lua is a great scripting language, easy to add to
> any program, compiled on many compilers, not need additional libraries -
> which is great for many project - thanks!
>
> Now, for my problems with Lua:
>
> I added Lua few months ago to a Windows based GUI application and I found
> some limitations with Lua:
>
> 1. I didn't find out how to stop a script from running from another thread.
> 2. I didn't find out how to give a pointer to Lua when registering a new
> function so when this function will be called it'll get it.
>
> For example:
>
> lua_State *myLua;
> MyClass myClass;
>
> ...
>
> lua_pushcclosure ( myLua, MyClass::MyFuncCode, 0 );
> lua_setglobal ( myLua, "my_func" );
>
> MyClass::MyFuncCode is static but I want also to have the "this" pointer to
> myClass when MyClass::MyFuncCode is executed.
>
> 3. Somehow registering the IO functions always failed:
>
> // This code always fail:
> luaopen_io ( myLua );
> lua_settop ( myLua, 0 );
>
> // This code fail too:
> luaopen_loadlib ( myLua );
> lua_settop ( myLua, 0 );
>
> LUA_API void lua_replace (lua_State *L, int idx) {
>   StkId o;
>   lua_lock(L);
>   /* explicit test for incompatible code */
>   if (idx == LUA_ENVIRONINDEX && L->ci == L->base_ci)
>     luaG_runerror(L, "no calling environment");   <----
> ***************************** it crash here
>   api_checknelems(L, 1);
>   o = index2adr(L, idx);
>   api_checkvalidindex(L, o);
>   if (idx == LUA_ENVIRONINDEX) {
>     Closure *func = curr_func(L);
>     api_check(L, ttistable(L->top - 1));
>     func->c.env = hvalue(L->top - 1);
>     luaC_barrier(L, func, L->top - 1);
>   }
>   else {
>     setobj(L, o, L->top - 1);
>     if (idx < LUA_GLOBALSINDEX)  /* function upvalue? */
>       luaC_barrier(L, curr_func(L), L->top - 1);
>   }
>   L->top--;
>   lua_unlock(L);
> }
>
> What's the meaning of "no calling environment"?
>
> thanks!
>
>