lua-users home
lua-l archive

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


It was thus said that the Great Laurent Faillie once stated:
> Hi Sean,
> 
> 
> On 29/06/2017 23:51, Sean Conner wrote:
> >It was thus said that the Great Laurent FAILLIE once stated:
> >
> >Wrong file---the code is in selene.c (and similar in SelMQTT.c). But the
> >one thing that *really* stands out is this:
> >
> >    lua_xmove( L, tstate, 1 );
> >
> >   You might want to double check the documentation for lua_xmove() 
> >because
> >it's not doing what you think it's doing.  I'm not sure that's what's
> >causing the issue you are seeing, but it's probably causing *some* issue.
> >It's here to push the function from the main state to the slave one as 
> >per lua_pcall()'s documentation.
> It is here to push the Lua function to "launch" into the slave's state, 
> from lua_*call() functions documentation.
> In Selene.c, the function is Detach() first and only argument.
> In SelMQTT.c, the function is retrieved from LUA_REGISTRY.

  I don't know how it works then.  Because here's the code in question (from
selene.c):

        lua_State *tstate = luaL_newstate();
        assert(tstate);
        luaL_openlibs( tstate );
        init_shared_Lua( tstate );
        init_SelFIFO( tstate );
        lua_xmove( L, tstate, 1 );

And now the descrption from the Lua 5.1 documentation:

	lua_xmove	[-?, +?, -]

	void lua_xmove (lua_State *from, lua_State *to, int n);

	Exchange values between different threads of the *same* global
	state.

	This function pops n values from the stack from, and pushes them
	onto the stack to.

You aren't moving data from the same global state, so that it's working is
... puzzling.  When I tried the following (using Lua 5.1):

	#include <stdio.h>

	#include <lua.h>
	#include <lualib.h>
	#include <lauxlib.h>
  
	int main(void)
	{
	  lua_State *L1 = luaL_newstate();
	  lua_State *L2 = luaL_newstate();
	  
	  luaL_openlibs(L1);
	  luaL_openlibs(L2);
	
	  luaL_loadstring(L1,"function foo() io.stdout:write('hello world\\n') end");
	  lua_call(L1,0,0);
	  
	  lua_getglobal(L1,"foo");
	  lua_call(L1,0,0);
	  
	  lua_getglobal(L1,"foo");
	  lua_xmove(L1,L2,1);
	  printf(">>> %s\n",luaL_typename(L2,-1));
	  lua_call(L2,0,0);
	  
	  lua_close(L1);
	  lua_close(L2);
	  
	  return 0;
	}

I get:

[spc]lucy:/tmp>./a.out 
hello world
>>> function
PANIC: unprotected error in call to Lua API ([string "function foo()
io.stdout:write('hello world..."]:1: attempt to index field 'stdout' (a userdata value))
[spc]lucy:/tmp>

  Which, now that I think on it, is kind of like your problem, isn't it?

> I don't think there is any trouble here as my home's dashboard is 
> running for months on 24/7 basis and this part of code is called on 
> several MQTT message arrivals which happening several time per hours 
> (most of message handling is done using another mechanism as dealing 
> with graphics so not suitable to be done asynchronously).
> The same code is used on the automation tools as well which drives 
> shutters and other things.
> 
> Both of them have months of uptime without trouble so I guess they are 
> stable.
> 
> But back to my issue : for me, it's a bit illogical to get a metatable 
> but not being able to call it's "member". Perhaps I missed something 
> when initialing the slave's state ... but can't find what. And all other 
> stuffs are working find fine in slave ...
> I think I would have to jump into Lua's own code to understand what's 
> happening ...

  -spc