Exiting lua from a C function

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Exiting lua from a C function

David Diaz
Hi !!!

Here's an approach to what we are doing now: from our C++ engine
we throw events. Events are just calls to LUA functions into
different light-threads. Inside an event, we can suspend the
execution of the LUA script returning back to engine execution.
Sometimes we don't want to resume those events, we just want to
cancel the event. At this point, we would need a way to gracesly
exit from the LUA script execution.

The matter is, is there any way to cancel a script execution
from within a C fucntion ?

It would be something like:
LUA_endExecution( state )

Any ideas ?

Thanks,
David.



Reply | Threaded
Open this post in threaded view
|

RE: Exiting lua from a C function

Peter Prade
> The matter is, is there any way to cancel a script execution
> from within a C fucntion ?
>
> It would be something like:
> LUA_endExecution( state )

if you call lua_error(), script execution will stop:

from the reference manual:
= void lua_error (lua_State *L, const char *message);
- This function never returns. If lua_error is called from a C function
- that has been called from Lua, then the corresponding Lua execution
- terminates, as if an error had occurred inside Lua code. Otherwise,
- the whole host program terminates with a call to exit(EXIT_FAILURE).
- Before terminating execution, the message is passed to the error
- handler function, _ERRORMESSAGE (see Section 4.7). If message is NULL,
- then _ERRORMESSAGE is not called.

if you don't like the error message you will get, look at the source of that
function and make your own, similar function.

Cheers,
Peter


Reply | Threaded
Open this post in threaded view
|

Re: Exiting lua from a C function

worldpretending
Sorry to respond to an old thread, but I thought I may as well
link my question to the previous one on the same issue. (Besides,
it proves I searched the archives before posting.)

--- In lua-l@y..., "Peter Prade" <prade@p...> wrote:
> > The matter is, is there any way to cancel a script execution
> > from within a C fucntion ?
> >
> > It would be something like:
> > LUA_endExecution( state )
> 
> if you call lua_error(), script execution will stop:
> 
> from the reference manual:
> = void lua_error (lua_State *L, const char *message);
> - This function never returns. If lua_error is called from a C function
> - that has been called from Lua, then the corresponding Lua execution
> - terminates, as if an error had occurred inside Lua code. 

This function would appear to terminate the whole script that is
being executed. I would like to know if there is a mechanism that
would let me temporarily exit - that is, pause execution of a Lua
script while preserving all the variables so that I can resume it
at a later stage. Basically, some simple context-switching. The idea
is that I can have a Wait function in a script that pauses script
execution for the specified time. Deciding when to continue the
script would be handled by the enclosing C++ program, but obviously
I need to know whether jumping out of the script while leaving
everything intact is even possible (and if so, if it's practical)
first.

--
Ben Sizer.


Reply | Threaded
Open this post in threaded view
|

Re: Exiting lua from a C function

Thatcher Ulrich
On Feb 05, 2002 at 01:36 -0000, worldpretending wrote:
> Sorry to respond to an old thread, but I thought I may as well
> link my question to the previous one on the same issue. (Besides,
> it proves I searched the archives before posting.)
> 
> --- In lua-l@y..., "Peter Prade" <prade@p...> wrote:
> > > The matter is, is there any way to cancel a script execution
> > > from within a C fucntion ?
> > >
> > > It would be something like:
> > > LUA_endExecution( state )
> > 
> > if you call lua_error(), script execution will stop:
> > 
> > from the reference manual:
> > = void lua_error (lua_State *L, const char *message);
> > - This function never returns. If lua_error is called from a C function
> > - that has been called from Lua, then the corresponding Lua execution
> > - terminates, as if an error had occurred inside Lua code. 
> 
> This function would appear to terminate the whole script that is
> being executed. I would like to know if there is a mechanism that
> would let me temporarily exit - that is, pause execution of a Lua
> script while preserving all the variables so that I can resume it
> at a later stage. Basically, some simple context-switching. The idea
> is that I can have a Wait function in a script that pauses script
> execution for the specified time. Deciding when to continue the
> script would be handled by the enclosing C++ program, but obviously
> I need to know whether jumping out of the script while leaving
> everything intact is even possible (and if so, if it's practical)
> first.

Sounds like you're asking for "yield".  The answer is, yes it does
exist, as a patch for Lua 4.0.  See
http://www.lua-users.org/wiki/PowerPatches , under "yield patch".

-- 
Thatcher Ulrich <[hidden email]>
http://tulrich.com