[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Exit chunk
- From: Rici Lake <lua@...>
- Date: Wed, 20 Oct 2004 17:09:25 -0500
The simplest way to do that is with error; you can catch the errors
with pcall.
> do local exit = {};
>> function breakscript(x) if x then error(exit) end end
>> function doscript(chunk)
>> local ok, err = pcall(chunk)
>> if not ok and err ~= exit then error(err, 0) end
>> end
>> end
> function X(str) local chunk = assert(loadstring(str, "in")); return
doscript(chunk) end
>
> X [[ print "hello"; breakscript(); print "world"]]
hello
world
>
> X [[ print "hello"; breakscript(1); print "world"]]
hello
-- Note that it does not affect normal error handling
> X [[ print(a.foo); breakscript(1); print "world"]]
[string "in"]:1: attempt to index global `a' (a nil value)
stack traceback:
[C]: in function `error'
stdin:5: in function `doscript'
(tail call): ?
stdin:1: in main chunk
[C]: ?
On 20-Oct-04, at 4:49 PM, Brett Kapilik wrote:
Hi,
I know that this has been addressed before, but I can't find it for
the life of me. My question is:
Is there an integrated way (or even a hack) to halt the execution of a
Lua chunk. What I would like is something like:
print ("hello");
breakfromscript;
print ("world");
would produce:
hello
That is, the fictional "breakfromscript" call would stop excution of
the current script (from pcall). Kind of like doing a return from a
function.
This would be really helpful as a way to short circuit script
executions in lengthy scripts. I know that there are all sorts of
"good" ways to do this like using if.. else and using functions, but
it wouldbe helpful to our customers if there was a way to just tell
the Lua engine to simply stop executing the current script chunk.
I am even willing to put a small hack into the Lua engine source code
if one exists.
Thanks!
- Brett