how abort a execution of script lua ?

72 views
Skip to first unread message

Denis Dos Santos Silva

unread,
Jun 6, 2024, 5:52:27 PMJun 6
to lua-l
Hi all!
looking lua documentation can't found a function to "abort execution of script".

question: it's possible abort (with throw a error) function ?

NOTE: lua os.exit() call c exit()

--- example
init c/app
init vm
load/run script (long running)
 abort execution (disk full or network failure) with flag of interrupt script
load/run another script
--- example

kind regards,

Sean Conner

unread,
Jun 6, 2024, 6:41:54 PMJun 6
to lu...@googlegroups.com
It was thus said that the Great Denis Dos Santos Silva once stated:
> Hi all!
> looking lua documentation can't found a function to "abort execution of
> script".
>
> question: it's possible abort (with throw a error) function ?
>
> *NOTE: lua os.exit() call c exit()*
>
> --- example
> init c/app
> init vm
> load/run script (long running)
> abort execution (disk full or network failure) with flag of interrupt
> script
> load/run another script
> --- example

Calling os.exit() with a non-zero parameter is generally how it's done,
even in the C world where a program needs to indicate it stopped for some
error condition. The shell script can then check the value returned and do
something based on the value.

-spc

Denis Dos Santos Silva

unread,
Jun 6, 2024, 7:59:15 PMJun 6
to lua-l
the issue with calling os.exit() call exit() then abort c/host application

maybe so like __halt_compiler() of php
https://www.php.net/manual/en/function.halt-compiler.php

Sean Conner

unread,
Jun 6, 2024, 9:18:00 PMJun 6
to lu...@googlegroups.com
It was thus said that the Great Denis Dos Santos Silva once stated:
> the issue with calling os.exit() call exit() then abort c/host application
>
> maybe so like __halt_compiler() of php
> https://www.php.net/manual/en/function.halt-compiler.php

The example you gave:

> > > --- example
> > > init c/app
> > > init vm
> > > load/run script (long running)
> > > abort execution (disk full or network failure) with flag of interrupt
> > > script
> > > load/run another script
> > > --- example

appeared to me as a shell script, but otherwise, it wasn't clear what you
meant by "abort execution of the script."

Perhaps the Lua function error() works? Here's what the Lua manual says
about it (https://www.lua.org/manual/5.4/manual.html#pdf-error):

error (message [, level])

Raises an error (see §2.3) with message as the error object. This
function never returns.

Usually, error adds some information about the error position at the
beginning of the message, if the message is a string. The level
argument specifies how to get the error position. With level 1 (the
default), the error position is where the error function was called.
Level 2 points the error to where the function that called error was
called; and so on. Passing a level 0 avoids the addition of error
position information to the message.

You can "catch" the error with pcall() (and with lua_pcall() with the C
API).

-spc

Sainan

unread,
Jun 7, 2024, 1:53:53 AMJun 7
to lu...@googlegroups.com
PHP's __halt_compiler kills the parser, not the VM.

An embedded environment would be wise to replace or remove os.exit. For example, an embedded environment in a game may use a coroutine-based scheduler that is ticked each game tick; in that case, you would simply clear the table of coroutines.

Thijs Schreijer

unread,
Jun 7, 2024, 3:55:38 AMJun 7
to lu...@googlegroups.com
> Perhaps the Lua function error() works? Here's what the Lua manual says
> about it (https://www.lua.org/manual/5.4/manual.html#pdf-error):
>
> error (message [, level])
>
> Raises an error (see §2.3) with message as the error object. This
> function never returns.
>
> Usually, error adds some information about the error position at the
> beginning of the message, if the message is a string. The level
> argument specifies how to get the error position. With level 1 (the
> default), the error position is where the error function was called.
> Level 2 points the error to where the function that called error was
> called; and so on. Passing a level 0 avoids the addition of error
> position information to the message.
>
> You can "catch" the error with pcall() (and with lua_pcall() with the C
> API).
>
> -spc
>

The Lua code running might also have pcall to catch any error thrown, and then your pcall will not get it. In corowatch[1] I had this issue. So after a coroutine is set to be terminated, it updates the debughook to run every 1 instruction (is far lower during normal operation for performance reasons). On each instruction it will throw the error again, and again, and again... this will ensure you bypass all pcalls until the coroutine finally dies.

Thijs

[1] https://github.com/Tieske/corowatch/blob/43f7d7075de72a97474332a68d878daa801c5df8/src/corowatch.lua#L90-L91

Denis Dos Santos Silva

unread,
Jun 7, 2024, 2:02:06 PMJun 7
to lua-l
Thanks everyone for the tips.
Reply all
Reply to author
Forward
0 new messages