[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Can C code recognize, whether Lua inside some loop/block? (for/while/repeat/if)
- From: Flyer31 Test <flyer31@...>
- Date: Wed, 16 Feb 2022 15:59:39 +0100
PS: Just one more general question: I assume that it is quite clear
that if I would try to do this with upvalues (so 100-1000 functions of
same type would start "hanging in RAM", each with their separate timer
upvalue), then this would cost quite a bit more than 4 byte per
function-upvalue pair?
... I could of course also use index values which the user then would
have to define... but this would be "too much fuzz" for the user. So
for this "90% very simple / boring code" in my loop function, I
preferred very much this sort of "auto-indexing" of the different
timers.
I also offer the possibility, that the user can specify a variable for
other cases (which typically should be less than 10% of invocations),
e.g. like:
local result -- this gets the result true/false
result, MySpecialTimer = delay( 'Input4711', MySpecialTimer)
For this "special invoke" then, the global variable "MySpecialTimer"
would save the timer value.
But in normal invoke the user could just use the "auto-index" version:
local result -- this gets the result true/false
result = delay( 'Input4711')
... just this auto-index construct will work then only, if it is used
in "very boring / straight forward" code part inside my main loop
function, so outside of any loops / gotos/ sub-functions.
On Wed, Feb 16, 2022 at 10:43 AM Roberto Ierusalimschy
<roberto@inf.puc-rio.br> wrote:
>
> > In general, you cannot [1]. In particular, there is no way to detect if
> > Lua is executing within a loop. The following Lua code:
> >
> > [...]
>
> Not to mention optimizations, gotos, and other strange constructions:
>
> - if x then return end
> FOO()
>
> Syntactically, the call is not inside an 'if', but semantically it is.
>
> - repeat FOO() until true
>
> Syntactically, the call is inside a loop, but semantically it isn't.
> (See the opcodes!)
>
> -- Roberto