lua-users home
lua-l archive

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


Rici,

I appreciate the response and the effort you put into the posted Lua
solution.  It's more than I want to try and take on at this point
though, so I think I will go with a lua_CFunction version of block() for
now...

	result = myLib.block(event)

That said, I'd like this function to reside in the same namespace as my
SWIG wrapped functions.  However, the following...

	(void) luaopen_myLib(L);		// SWIG wrapped C
functions

	const struct luaL_reg funcs[] =	// regular Lua C functions
  	{
    		{ "block",  block  },
    		{ NULL,     NULL   }
  	};

  	luaL_register(L, "myLib", funcs);

...results in...

	?:0: attempt to call field 'block' (a nil value)

...when invoked from a script.

Being a SWIG novice, and not much more experienced with Lua, I'm not
quite sure what the issue is.  Anyone have any pointers? 

Thanks,
Brian


-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of Rici Lake
Sent: Wednesday, February 28, 2007 9:14 PM
To: Lua list
Subject: Re: SWIG question


On 28-Feb-07, at 1:50 PM, Brian Hassink wrote:

> Actually, in looking at this some more it appears as though it's not
> possible to put a lua_yield() call within a SWIG wrapped function.
The
> semantics of a SWIG wrapped function are different from that of a
> lua_CFunction.
>
> So, how would one do something like...
>
> 	result = block(self, event)
>
> ...where block() is a SWIG wrapped function that takes the script
> context (i.e. state) and an event as arguments, and blocks (yields)
the
> script until the event occurs, after which the details are copied into

> a
> result structure (resumes)?
>

I don't know a lot about SWIG (and it may not be the optimal binding
system for you, in fact, but that's another issue).

However, it seems like the easiest solution is to write block() in
Lua. There's no shame in that :)

function block(event) return coroutine.yield(event) end

That assumes that you have a supervisor (event loop, I guess)
which handles the coroutines. You could write this in Lua,
as well; I've put an example on the wiki at 
http://lua-users.org/wiki/CoroutinesAsEventHandlers