Commit Graph

504 Commits

Author SHA1 Message Date
Roberto Ierusalimschy
14e416355f Added suport for Fixed Buffers
A fixed buffer keeps a binary chunk "forever", so that the program
does not need to copy some of its parts when loading it.
2023-09-05 15:30:45 -03:00
Roberto Ierusalimschy
96f7714237 Field 'Proto.is_vararg' uses only one bit
So that the other bits can be used for other purposes.
2023-08-30 10:44:28 -03:00
Roberto Ierusalimschy
ab6a949522 Merge branch 'master' into nextversion 2023-06-22 11:41:48 -03:00
Roberto Ierusalimschy
9be74ccc21 Several functions turned 'static'
Several functions that were already being used only inside their
own file have been declared as 'static'.
2023-05-22 14:47:54 -03:00
Roberto Ierusalimschy
1de2f31694 Corrected support for 16-bit systems
We still need access to a 16-bit system to correctly test
these changes.
2023-03-09 11:10:04 -03:00
Roberto Ierusalimschy
152b51955a Removed GC checks from function calls
Function calls do not create new objects. (It may use memory with
stack reallocation, but now that is irrelevant to the GC.)
2022-11-24 10:20:15 -03:00
Roberto Ierusalimschy
ee645472eb Stack reallocation done with a single realloc
To avoid the need of both the old and the new stack addresses valid
at the same time, to correct the pointers to the stack, these pointers
are changed to offsets before the reallocation and then changed back
to pointers after the reallocation.
2022-10-31 15:06:20 -03:00
Roberto Ierusalimschy
413a393e62 Stack indices changed to union's
That will allow to change pointers to offsets while reallocating
the stack.
2022-10-29 12:06:37 -03:00
Roberto Ierusalimschy
d61b0c6028 More checks and documentation for uses of EXTRA_STACK 2022-05-26 15:14:54 -03:00
Roberto Ierusalimschy
196bb94d66 Bug: 'lua_settop' may use an invalid pointer to stack 2022-05-25 17:41:39 -03:00
Roberto Ierusalimschy
4a00f61276 'lua_checkstack' doesn't need to check stack overflow
'luaD_growstack' already checks that. This commit also fixes an
internal bug in 'luaD_growstack': a large 'n' could cause an arithmetic
overflow when computing 'needed'.
2022-05-23 10:38:03 -03:00
Roberto Ierusalimschy
f3cfd5bf2b Details
Comments + manual + identation + asserts about stack limits that were
not allowing the use of the full stack
2022-04-01 13:55:44 -03:00
Roberto Ierusalimschy
cf613cdc6f Bug: finalizers can be called with an invalid stack
The call to 'checkstackGC' can run finalizers, which will find an
inconsistent CallInfo, as 'ci' is half updated at the point of call.
2021-12-15 11:29:07 -03:00
Roberto Ierusalimschy
6b3e116d44 Corrected bug in 'luaD_tryfuncTM'
The pointer to the metamethod can be invalidated by a finalizer that
can run during a GC in 'checkstackGCp'. (This commit also fixes a
detail in the manual.) Bug introduced in commit 91673a8ec.
2021-11-16 14:35:06 -03:00
Roberto Ierusalimschy
74d99057a5 Bug: C stack overflow with coroutines
'coroutine.resume' did not increment counter of C calls when
continuing execution after a protected error (that is,
while running 'precover').
2021-11-03 15:04:18 -03:00
Roberto Ierusalimschy
1fce5bea81 More uniform implementation for tail calls
'luaD_pretailcall' mimics 'luaD_precall', handling call metamethods
and calling C functions directly. That makes the code in the
interpreter loop simpler.

This commit also goes back to emulating the tail call in 'luaD_precall'
with a goto, as C compilers may not do proper tail calls and the C
stack can overflow much sooner than the Lua stack (which grows as the
metamethod is added to it).
2021-10-29 13:41:24 -03:00
Roberto Ierusalimschy
3699446aaf Removed goto's in 'luaD_precall'
(plus a detail in lauxlib.h.)
2021-10-18 11:58:40 -03:00
Roberto Ierusalimschy
2ff3471722 Using 'inline' in some functions
According to ISO C, "making a function an inline function suggests that
calls to the function be as fast as possible." (Not available in C89.)
2021-09-15 11:18:41 -03:00
Roberto Ierusalimschy
91673a8ec0 'luaD_tryfuncTM' checks stack space by itself 2021-08-18 12:05:06 -03:00
Roberto Ierusalimschy
41871f1803 Undo simplification of tail calls (commit 901d760)
Not that simpler and slower.
2021-08-18 11:21:33 -03:00
Roberto Ierusalimschy
dbdc74dc55 Simplification in the parameters of 'luaD_precall'
The parameters 'nresults' and 'delta1', in 'luaD_precall', were never
meaningful simultaneously. So, they were combined in a single parameter
'retdel'.
2021-06-30 12:53:21 -03:00
Roberto Ierusalimschy
04e19712a5 C functions can be tail called, too
A tail call to a C function can have the behavior of a "real" tail
call, reusing the stack frame of the caller.
2021-06-14 13:28:21 -03:00
Roberto Ierusalimschy
901d760093 Simpler implementation for tail calls
Tail calls handled by 'luaD_precall', like regular calls, to avoid
code duplication.
2021-06-11 13:41:07 -03:00
Roberto Ierusalimschy
f9d857a81b Stack reallocation done in two phases
$he stack reallocation is done in two steps (allocation + free) because
the correction of the pointers pointing into the stack must be done
while both addresses (the old stack and the new one) are valid.  In ISO
C, any pointer use after the pointer has been deallocated is undefined
behavior. The compiler option '-fsanitize=pointer-subtract' (plus what
it needs to work) complained about the old implementation.
2021-02-27 12:56:09 -03:00
Roberto Ierusalimschy
59c88f846d Broadening the use of branch hints
More uses of macros 'likely'/'unlikely' (renamed to
'l_likely'/'l_unlikely'), both in range (extended to the
libraries) and in scope (extended to hooks, stack growth).
2021-02-24 11:14:44 -03:00
Roberto Ierusalimschy
bc970005ce '__close' methods can yield in the return of a C function
When, inside a coroutine, a C function with to-be-closed slots return,
the corresponding metamethods can yield. ('__close' metamethods called
through 'lua_closeslot' still cannot yield, as there is no continuation
to go when resuming.)
2021-02-12 13:36:30 -03:00
Roberto Ierusalimschy
4e47f81188 New implementation for to-be-closed variables
To-be-closed variables are linked in their own list, embedded into the
stack elements. (Due to alignment, this information does not change
the size of the stack elements in most architectures.)  This new list
does not produce garbage and avoids memory errors when creating tbc
variables.
2021-02-09 14:00:05 -03:00
Roberto Ierusalimschy
2bfa13e520 Fixed some bugs around stack reallocation
Long time without using HARDSTACKTESTS...
2021-02-05 11:00:28 -03:00
Roberto Ierusalimschy
949187b049 Optimizations for line hook
The function 'changedline' tries harder to avoid calling
'luaG_getfuncline' plus small changes in the use of 'L->oldpc'.
2021-01-28 14:40:29 -03:00
Roberto Ierusalimschy
58aa09a0b9 Small improvements in hooks
- 'L->top' is set once in 'luaD_hook', instead of being set in
'luaD_hookcall' and 'rethook';

- resume discard arguments when returning after an yield inside a hook
(arguments may interfere with the coroutine stack);

- yield inside a hook asserts it has no arguments.
2021-01-26 16:53:51 -03:00
Roberto Ierusalimschy
1f81baffad Janitorial work
Comments, code details, identation.
2021-01-25 10:39:18 -03:00
Roberto Ierusalimschy
0e9254dfa0 Correct order of return hooks vs. close metamethods
The return hook should be called only after closing variables (which
are still part of the function). C functions were calling the hook
before the metamethods.
2021-01-21 10:27:22 -03:00
Roberto Ierusalimschy
6ccd24eff5 Simpler handling of errors when creating tbc variables
New field 'lua_State.ptbc' keeps to-be-closed variable until its
upvalue is created, so that it can be closed in case of a
memory-allocation error.
2021-01-19 10:03:13 -03:00
Roberto Ierusalimschy
d0f34d9137 Allow yields in '__close' metamethods ater errors
Completes commit b07fc10e91. '__close' metamethods can yield even
when they are being called due to an error. '__close' metamethods from
C functions are still not allowed to yield.
2021-01-18 11:40:45 -03:00
Roberto Ierusalimschy
b07fc10e91 Allow yields inside '__close' metamethods
Initial implementation to allow yields inside '__close' metamethods.
This current version still does not allow a '__close' metamethod
to yield when called due to an error. '__close' metamethods from
C functions also are not allowed to yield.
2021-01-13 13:54:10 -03:00
Roberto Ierusalimschy
ce101dcaf7 Handles '__close' errors in coroutines in "coroutine style"
Errors in '__close' metamethods in coroutines are handled by the same
logic that handles other errors, through 'recover'.
2020-12-30 11:20:22 -03:00
Roberto Ierusalimschy
4bd10b6fe8 Better error messages for calling non-callable objects
When available, use the calling code to find a suitable name for what
was being called; this is particularly useful for errors of non-callable
metamethods. This commit also improved the debug information for
order metamethods.
2020-12-29 13:15:54 -03:00
Roberto Ierusalimschy
6188f3a654 Reset thread before panicking
Before panicking, it is simpler to reset the thread instead of closing
its variables and adjust the top manually.
2020-12-28 16:34:07 -03:00
Roberto Ierusalimschy
7af27ef59d Cleaner handling of errors in '__close' metamethods
Instead of protecting each individual metamethod call, protect the
entire call to 'luaF_close'.
2020-12-28 11:40:30 -03:00
Roberto Ierusalimschy
d282652561 Bug when growing a stack
When a stack grows, its extra area can be in use, and it becomes part
of the common area. So, the extra area must be kept correct all the
times. (Bug introduced by commit 5aa36e894f5.)
2020-11-08 11:52:26 -03:00
Roberto Ierusalimschy
94cbe46511 Details
- small corrections in the manual
- ldo.c: 'docall' -> 'ccall' ('docall' already used in 'lua.c')
- comments
2020-10-30 10:18:54 -03:00
Roberto Ierusalimschy
171dcd7d74 'recover' finish of 'luaD_pcall' should follow the original 2020-10-12 12:29:09 -03:00
Roberto Ierusalimschy
5aa36e894f No more field 'lua_State.stacksize'
The stack size is derived from 'stack_last', when needed. Moreover,
the handling of stack sizes is more consistent, always excluding the
extra space except when allocating/deallocating the array.
2020-10-12 12:29:09 -03:00
Roberto Ierusalimschy
9ecd446141 Avoid shrinking stacks to often
Shrink a stack only when the final stack size can be at most 2/3 the
previous size with half of its entries empty. This commit also
improves the clarity of 'luaD_growstack'.
2020-10-12 12:29:09 -03:00
Roberto Ierusalimschy
490d42b5f8 Correct handling of 'luaV_execute' invocations
The previous stackless implementations marked all 'luaV_execute'
invocations as fresh. However, re-entering 'luaV_execute' when
resuming a coroutine should not be a fresh invocation. (It works
because 'unroll' called 'luaV_execute' for each call entry, but
it was slower than letting 'luaV_execute' finish all non-fresh
invocations.)
2020-10-12 12:29:09 -03:00
Roberto Ierusalimschy
287b302acb Revision of stackless implementation
- more organized handling of 'nCcalls'
- comments
- deprecation of 'setcstacklimit'
2020-10-12 12:29:09 -03:00
Roberto Ierusalimschy
5d8ce05b3f Back to a stackless implementation
A "with stack" implementation gains too little in performance to be
worth all the noise from C-stack overflows.

This commit is almost a sketch, to test performance. There are several
pending stuff:

- review control of C-stack overflow and error messages;
- what to do with setcstacklimit;
- review comments;
- review unroll of Lua calls.
2020-10-12 12:29:09 -03:00
Roberto Ierusalimschy
34affe7a63 Fixed bug: 'luaD_callnoyield' called twice in a row
In luaD_callnoyield, when there is a possible stack overflow, it
zeros the number of CallInfos to force a check when calling the
function. However, if the "function" is not a function, the code will
raise an error before checking the stack. Then, the error handling calls
luaD_callnoyield again and nCcalls is decremented again, crossing the
stack redzone without raising an error. (This loop can only happens
once, because the error handler must be a function.  But once is enough
to cross the redzone.)
2020-07-17 14:54:26 -03:00
Roberto Ierusalimschy
a2195644d8 Fixed bug: invalid 'oldpc' when returning to a function
The field 'L->oldpc' is not always updated when control returns to a
function; an invalid value can seg. fault when computing 'changedline'.
(One example is an error in a finalizer; control can return to
'luaV_execute' without executing 'luaD_poscall'.) Instead of trying to
fix all possible corner cases, it seems safer to be resilient to invalid
values for 'oldpc'. Valid but wrong values at most cause an extra call
to a line hook.
2020-07-17 11:01:05 -03:00
Roberto Ierusalimschy
e1d8770f12 Fixed bug: wrong stack limit when entering a coroutine
When entering a coroutine, the computation of nCcalls added 'from->nci'
to correct for preallocated CallInfos, but 'nci' includes also the
Callinfos already used.
2020-07-13 13:39:02 -03:00