lua-users home
lua-l archive

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


On Fri, 5 Jun 2020 02:48:03 -0400, Sean Conner <sean@conman.org> wrote:

> It was thus said that the Great Milan Obuch once stated:
> > Hi,  
> 
>   Hello.
> 
> > When plugin is being loaded, a function is registered with Claws
> > Mail hook mechanism which is to be called on creating mail compose
> > window. When I try to create new message or reply to existing one,
> > compose window is being created and my function is being called
> > with an argument - C pointer to structure describing the window. In
> > my C function I use luaL_dofile() to load a script from file and
> > execute it.
> > 
> > And here comes the question - how can I use this C pointer as an
> > argument for my script? I know I can do
> > 
> > lua_pushlightuserdata(L,(void *)compose_window_argument);
> > luaL_dofile(L,"my_script_file");
> > lua_pop(L,1);
> > 
> > but I found no way to access this argument from my_script_file.   
> 
>   There is a way, but you can't use luaL_dofile().  First off, change
> the C code to:
> 
> 	int rc;
> 
> 	rc = luaL_loadfile(L,"my_script_file");
> 	if (rc != LUA_OKAY) error(); 
> 	lua_pushlightuserdata(L,(void *)compose_window_argument);
> 	rc = lua_pcall(L,1,LUA_MULTRET,0);
> 	if (rc != LUA_OKAY) error();
> 
> (minimal error checking added---handle as appropriate).  Then in your
> script:
> 
> 	local window = ...
> 
> or
> 
> 	local window = select(1,...)
> 
>   When Lua loads any code, it is compiled and returned as a function,
> which when run, will do the action of the script.  As long as there
> are parameters on the stack, the script can reference them via '...'
> (or select()).
> 

Now looking in Lua 5.2 Reference Manual, entry for luaL_loadfile -
equivalent for luaL_loadfilex with mode equal NULL. Now another
redirection for mode in entry for luaL_loadfilex - to lua_load and
finally there to load... somehow hard to follow :) Anyway, now I
understand first call plus next line error handling. Now lua_pcall...
look for lua_call as well...

OK, understand, I think. Thank you!

Regards,
Milan