Commit Graph

472 Commits

Author SHA1 Message Date
Roberto Ierusalimschy
40565b4a08 Revamp of GC parameters
More uniformity when handling GC parameters + avoid divisions by 100
when applying them.
2022-12-13 11:55:14 -03:00
Roberto Ierusalimschy
d738c8d18b New function 'luaL_openselectedlibs'
Makes it easier to start Lua with only some standard libraries.
2022-12-07 15:12:52 -03:00
Roberto Ierusalimschy
d324a0ccf9 Simpler control for major collections 2022-11-29 10:37:08 -03:00
Roberto Ierusalimschy
ec61be9a7e 'l_mem' renamed to 'l_obj' to count objects 2022-11-23 17:29:03 -03:00
Roberto Ierusalimschy
f356d5acdd First version of GC counting objects for control
Still needs to review generational mode.
2022-11-23 17:17:20 -03:00
Roberto Ierusalimschy
8047b2d03e Tables have a 'lastfree' information only when needed
Only tables with some minimum number of entries in their hash part
have a 'lastfree' field, kept in a header before the node vector.
2022-11-01 15:42:08 -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
1e64c1391f Bug: stack overflow with nesting of coroutine.close 2022-10-25 16:44:06 -03:00
Roberto Ierusalimschy
14d2803e55 Details
Some cast operations rewritten to use respective macros.
2022-10-19 16:29:54 -03:00
Roberto Ierusalimschy
439e45a2f6 Bug: luaL_tolstring may get confused with negative index
When object has a '__name' metafield, 'luaL_tolstring' used the
received index after pushing a string on the stack.
2021-07-22 13:48:43 -03:00
Roberto Ierusalimschy
9a2de786de Stack check in warning function for tests
The warning function using for tests need to check the stack before
pushing anything. (Warning functions are not expected to access a
Lua state, therefore they have no preallocated stack space.)
2021-03-02 11:35:40 -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
cc1692515e New API function 'lua_closeslot'
Closing a to-be-closed variable with 'lua_settop' is too restrictive,
as it erases all slots above the variable. Moreover, it adds side
effects to 'lua_settop', which should be a fairly basic function.
2021-01-11 15:03:01 -03:00
Roberto Ierusalimschy
748d6d4e7a Review of asserts in 'ltests.c'
The module 'ltests.c' must work correctly with asserts off, too.
2020-12-08 11:54:21 -03:00
Roberto Ierusalimschy
58216600eb 'luaL_newstate' should not allocate extra memory
The allocation of a userdata for the state of the warn system can
cause a panic if it fails; 'luaL_ref' also can fail. This commit
re-implements the warn system so that it does not need an explicit
state. Instead, the system uses different functions to represent
the different states.
2020-11-03 16:34:36 -03:00
Roberto Ierusalimschy
9a89fb1c9d Hash always use all characters in a long string
Hashes for long strings are computed only when they are used as keys
in a table, not a too common case. And, in that case, it is to easy to
force collisions changing only the characters which are not part of the
hash.
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
f13dc59416 Better tests for gray lists
Test uses an extra bit in 'marked' to mark all elements in gray lists
and then check against elements colored gray.
2020-08-07 14:45:20 -03:00
Roberto Ierusalimschy
b9b554e0f6 Clearer handling of gray lists when entering generational mode
When entering generational mode, all objects are old. So, the only
objects that need to be in a gray list are threads, which can be
assigned without barriers. Changes in anything else (e.g., weak
tables) will trigger barriers that, if needed, will add the object
to a gray list.
2020-08-03 13:22:57 -03:00
Roberto Ierusalimschy
b4c353434f Details
The fields 'old' and 'finobjold' were renamed 'old1' and 'finobjold1',
respectively, to make clearer the main ages of their elements.
2020-07-29 11:34:08 -03:00
Roberto Ierusalimschy
8c7c9ea065 Function 'printobj' in 'ltests.c' made public
It helps to have this function available for debugging.
2020-07-27 11:24:03 -03:00
Roberto Ierusalimschy
d39ea8b3ce Make sure that main thread is non yieldable
Main thread must be non yieldable even at "level 0" (bare API), outside
the 'pcall' from 'lua.c'.
2020-07-06 13:54:01 -03:00
Roberto Ierusalimschy
bfcf06d91a Avoid memory allocation in some functions from 'ltests.c'
To allow their use in memory tests, some functions in 'ltests.c'
should never allocate memory. To avoid this allocation, the
library registers the strings used for status codes, and keeps
the variable '_WARN' always defined (with false instead of nil).
2020-07-04 16:40:18 -03:00
Roberto Ierusalimschy
993c58fde3 In 'lua_checkmemory', userdata can be gray, too
Since commit ca6fe7449a, userdata with uservalues can be gray
and can belong to gray lists ('gclist').
2020-06-15 12:01:36 -03:00
Roberto Ierusalimschy
364e569945 Avoid calling 'fprintf' with NULL
Avoid undefined behavior in calls like «fprintf("%s", NULL)».
('lua_writestringerror' is implemented as 'fprintf', and 'lua_tostring'
can return NULL if object is not a string.)
2020-06-09 16:12:01 -03:00
Roberto Ierusalimschy
513559cc47 Fixed bug in 'string.format("%p")'
The string "(null)" used for non-collectable values must be printed as a
string, not as a pointer. (Bug introduced in commit e0cbaa50fa).
2020-03-16 14:13:13 -03:00
Roberto Ierusalimschy
6eb53b7526 Details
Several details in code (e.g., moving a variable to the most inner
scope that encloses its uses), comments, parameter names, extra tests.
2020-02-27 12:59:22 -03:00
Roberto Ierusalimschy
46c3587a6f Clearer distinction between types and tags
LUA_T* represents only types; tags (types + Variants) are represented
by LUA_V* constants.
2020-01-31 11:09:53 -03:00
Roberto Ierusalimschy
bd1b87c579 Comments (mosty typos) 2019-12-30 11:45:08 -03:00
Roberto Ierusalimschy
e592f94a64 Details (mostly comments) 2019-10-22 14:08:22 -03:00
Roberto Ierusalimschy
be78aeae4c Default for warnings changed to "off"
Warnings are mostly a tool to help developers (e.g., by showing hidden
error messages); regular users usually don't need to see them.
2019-08-20 13:42:26 -03:00
Roberto Ierusalimschy
9405472565 Improvement in warn-mode '@store' (for testing)
When using warn-mode '@store', from the test library, the tests ensure
not only that the expected warnings were issued, but also that there was
no extra warnings.
2019-08-18 17:29:46 -03:00
Roberto Ierusalimschy
ca13be9af7 Supressed errors in '__close' generate warnings 2019-08-16 09:51:54 -03:00
Roberto Ierusalimschy
a1d8eb2743 Added control messages to warnings
Added the concept of control messages to the warning system, plus the
implementation of the controls "@on"/"@off" to turn warnings on/off.
Moreover, the warning system in the test library adds some other
controls to ease the test of warnings.
2019-08-15 13:44:36 -03:00
Roberto Ierusalimschy
f645d31573 To-be-closed variables must be closed on initialization
When initializing a to-be-closed variable, check whether it has a
'__close' metamethod (or is a false value) and raise an error if
if it hasn't. This produces more accurate error messages. (The
check before closing still need to be done: in the C API, the value
is not constant; and the object may lose its '__close' metamethod
during the block.)
2019-07-31 10:43:51 -03:00
Roberto Ierusalimschy
dc07719b0d Tag LUA_TUPVALTBC replaced by a flag
It is simpler to signal a to-be-closed upvalue with a boolean flag,
instead of using a different tag.
2019-07-19 11:12:31 -03:00
Roberto Ierusalimschy
1fb4d53925 OP_NEWTABLE keeps exact size of arrays
OP_NEWTABLE is followed by an OP_EXTRAARG, so that it can keep
the exact size of the array part of the table to be created.
(Functions 'luaO_int2fb'/'luaO_fb2int' were removed.)
2019-07-12 16:13:50 -03:00
Roberto Ierusalimschy
4487c28ced A few more tests for table access in the API
Added tests where the table being accessed is also the index or
value in the operation.
2019-06-25 17:38:58 -03:00
Roberto Ierusalimschy
0b63d79b36 Details
- 'luaL_setfuncs' avoids creating closures for placeholders.
- Fixed some warnings about unused values in comma expressions.
- Comments.
2019-05-13 16:20:40 -03:00
Roberto Ierusalimschy
3da34a5fa7 Revamp of 'lua_pushfstring' / 'luaO_pushvfstring'
The function 'luaO_pushvfstring' now uses an internal buffer to
concatenate small strings, instead of pushing all pieces on the
stack. This avoids the creation of several small Lua strings for each
piece of the result. (For instance, a format like "n: '%d'" used to
create three intermediate strings: "n: '", the numeral, and "'".
Now it creates none.)
2019-04-24 14:01:20 -03:00
Roberto Ierusalimschy
b56d4e570a Changes in the warning system
- The warning functions get an extra parameter that tells whether
message is to be continued (instead of using end-of-lines as a signal).

- The user data for the warning function is a regular value, instead
of a writable slot inside the Lua state.
2019-03-14 15:30:54 -03:00
Roberto Ierusalimschy
cf71a5ddc7 Details
Several small improvements (code style, warnings, comments, more tests),
in particular:

- 'lua_topointer' extended to handle strings
- raises an error in 'string.format("%10q")' ('%q' with modifiers)
- in the manual for 'string.format', the term "option" replaced by
  "conversion specifier" (the term used by the C standard)
2019-03-13 13:16:53 -03:00
Roberto Ierusalimschy
4ace93ca65 No more to-be-closed functions
To-be-closed variables must contain objects with '__toclose'
metamethods (or nil). Functions were removed for several reasons:

* Functions interact badly with sandboxes. If a sandbox raises
an error to interrupt a script, a to-be-closed function still
can hijack control and continue running arbitrary sandboxed code.

* Functions interact badly with coroutines. If a coroutine yields
and is never resumed again, its to-be-closed functions will never
run. To-be-closed objects, on the other hand, will still be closed,
provided they have appropriate finalizers.

* If you really need a function, it is easy to create a dummy
object to run that function in its '__toclose' metamethod.

This comit also adds closing of variables in case of panic.
2019-01-04 13:09:47 -02:00
Roberto Ierusalimschy
c6f7181e91 No more LUA_ERRGCMM errors
Errors in finalizers (__gc metamethods) are never propagated.
Instead, they generate a warning.
2019-01-01 12:14:56 -02:00
Roberto Ierusalimschy
437a5b07d4 Added a warning system to Lua
The warning system is just a way for Lua to emit warnings, messages
to the programmer that do not interfere with the running program.
2018-12-28 15:42:34 -02:00
Roberto Ierusalimschy
af6d9f3116 Details
A few details in the makefile and in the manual. (In particular,
it updates the dependency lists in the makefile.)
2018-12-17 13:56:22 -02:00
Roberto Ierusalimschy
fdc25a1ebf New functions 'lua_resetthread' and 'coroutine.kill'
New functions to reset/kill a thread/coroutine, mainly (only?) to
close any pending to-be-closed variable. ('lua_resetthread' also
allows a thread to be reused...)
2018-12-13 13:07:53 -02:00
Roberto Ierusalimschy
7e63d3da02 Some bugs with stack reallocation by 'luaF_close'
(Long time without testing with '-DHARDSTACKTESTS'...)
With the introduction of to-be-closed variables, calls to 'luaF_close'
can move the stack, but some call sites where keeping pointers to the
stack without correcting them.
2018-11-24 11:59:15 -02:00
Roberto Ierusalimschy
5fda30b4f9 'lua_toclose' gets the index to be closed as an argument
Sometimes it is useful to mark to-be-closed an index that is not
at the top of the stack (e.g., if the value to be closed came from
a function call returning multiple values).
2018-11-12 14:15:50 -02:00
Roberto Ierusalimschy
b8fed93215 New syntax for to-be-closed variables
The new syntax is <local *toclose x = f()>. The mark '*' allows other
attributes to be added later without the need of new keywords; it
also allows better error messages.  The API function was also renamed
('lua_tobeclosed' -> 'lua_toclose').
2018-11-07 10:03:05 -02:00