aboutsummaryrefslogtreecommitdiff
path: root/manual/manual.of
diff options
context:
space:
mode:
Diffstat (limited to 'manual/manual.of')
-rw-r--r--manual/manual.of537
1 files changed, 287 insertions, 250 deletions
diff --git a/manual/manual.of b/manual/manual.of
index cef3e22a..e3cbddb3 100644
--- a/manual/manual.of
+++ b/manual/manual.of
@@ -621,7 +621,8 @@ that is inaccessible from Lua.
621another live object refer to the object.) 621another live object refer to the object.)
622Because Lua has no knowledge about @N{C code}, 622Because Lua has no knowledge about @N{C code},
623it never collects objects accessible through the registry @see{registry}, 623it never collects objects accessible through the registry @see{registry},
624which includes the global environment @see{globalenv}. 624which includes the global environment @see{globalenv} and
625the main thread.
625 626
626 627
627The garbage collector (GC) in Lua can work in two modes: 628The garbage collector (GC) in Lua can work in two modes:
@@ -638,8 +639,8 @@ therefore, optimal settings are also non-portable.
638You can change the GC mode and parameters by calling 639You can change the GC mode and parameters by calling
639@Lid{lua_gc} @N{in C} 640@Lid{lua_gc} @N{in C}
640or @Lid{collectgarbage} in Lua. 641or @Lid{collectgarbage} in Lua.
641You can also use these functions to control 642You can also use these functions to control the collector directly,
642the collector directly (e.g., to stop and restart it). 643for instance to stop or restart it.
643 644
644} 645}
645 646
@@ -656,39 +657,33 @@ and the @def{garbage-collector step size}.
656 657
657The garbage-collector pause 658The garbage-collector pause
658controls how long the collector waits before starting a new cycle. 659controls how long the collector waits before starting a new cycle.
659The collector starts a new cycle when the use of memory 660The collector starts a new cycle when the number of objects
660hits @M{n%} of the use after the previous collection. 661hits @M{n%} of the total after the previous collection.
661Larger values make the collector less aggressive. 662Larger values make the collector less aggressive.
662Values equal to or less than 100 mean the collector will not wait to 663Values equal to or less than 100 mean the collector will not wait to
663start a new cycle. 664start a new cycle.
664A value of 200 means that the collector waits for the total memory in use 665A value of 200 means that the collector waits for
665to double before starting a new cycle. 666the total number of objects to double before starting a new cycle.
666The default value is 200; the maximum value is 1000. 667The default value is 200.
667
668The garbage-collector step multiplier
669controls the speed of the collector relative to
670memory allocation,
671that is,
672how many elements it marks or sweeps for each
673kilobyte of memory allocated.
674Larger values make the collector more aggressive but also increase
675the size of each incremental step.
676You should not use values less than 100,
677because they make the collector too slow and
678can result in the collector never finishing a cycle.
679The default value is 100; the maximum value is 1000.
680 668
681The garbage-collector step size controls the 669The garbage-collector step size controls the
682size of each incremental step, 670size of each incremental step,
683specifically how many bytes the interpreter allocates 671specifically how many objects the interpreter creates
684before performing a step. 672before performing a step:
685This parameter is logarithmic: 673A value of @M{n} means the interpreter will create
686A value of @M{n} means the interpreter will allocate @M{2@sp{n}} 674approximately @M{n} objects between steps.
687bytes between steps and perform equivalent work during the step. 675The default value is 250.
688A large value (e.g., 60) makes the collector a stop-the-world 676
689(non-incremental) collector. 677The garbage-collector step multiplier
690The default value is 13, 678controls the size of each GC step.
691which means steps of approximately @N{8 Kbytes}. 679A value of @M{n} means the interpreter will mark or sweep,
680in each step, @M{n%} objects for each created object.
681Larger values make the collector more aggressive.
682Beware that values too small can
683make the collector too slow to ever finish a cycle.
684The default value is 200.
685As a special case, a zero value means unlimited work,
686effectively producing a non-incremental, stop-the-world collector.
692 687
693} 688}
694 689
@@ -697,31 +692,46 @@ which means steps of approximately @N{8 Kbytes}.
697In generational mode, 692In generational mode,
698the collector does frequent @emph{minor} collections, 693the collector does frequent @emph{minor} collections,
699which traverses only objects recently created. 694which traverses only objects recently created.
700If after a minor collection the use of memory is still above a limit, 695If after a minor collection the number of objects is above a limit,
701the collector does a stop-the-world @emph{major} collection, 696the collector shifts to a @emph{major} collection,
702which traverses all objects. 697which traverses all objects.
703The generational mode uses two parameters: 698The collector will then stay doing major collections until
704the @def{minor multiplier} and the @def{the major multiplier}. 699it detects that the program is generating enough garbage to justify
700going back to minor collections.
701
702The generational mode uses three parameters:
703the @def{minor multiplier}, the @def{minor-major multiplier},
704and the @def{major-minor multiplier}.
705 705
706The minor multiplier controls the frequency of minor collections. 706The minor multiplier controls the frequency of minor collections.
707For a minor multiplier @M{x}, 707For a minor multiplier @M{x},
708a new minor collection will be done when memory 708a new minor collection will be done when the number of objects
709grows @M{x%} larger than the memory in use after the previous major 709grows @M{x%} larger than the number in use just
710collection. 710after the last major collection.
711For instance, for a multiplier of 20, 711For instance, for a multiplier of 20,
712the collector will do a minor collection when the use of memory 712the collector will do a minor collection when the number of objects
713gets 20% larger than the use after the previous major collection. 713gets 20% larger than the total after the last major collection.
714The default value is 20; the maximum value is 200. 714The default value is 25.
715 715
716The major multiplier controls the frequency of major collections. 716The minor-major multiplier controls the shift to major collections.
717For a major multiplier @M{x}, 717For a multiplier @M{x},
718a new major collection will be done when memory 718the collector will shift to a major collection
719grows @M{x%} larger than the memory in use after the previous major 719when the number of old objects grows @M{x%} larger
720collection. 720than the total after the previous major collection.
721For instance, for a multiplier of 100, 721For instance, for a multiplier of 100,
722the collector will do a major collection when the use of memory 722the collector will do a major collection when the number of old objects
723gets larger than twice the use after the previous collection. 723gets larger than twice the total after the previous major collection.
724The default value is 100; the maximum value is 1000. 724The default value is 100.
725
726The major-minor multiplier controls the shift back to minor collections.
727For a multiplier @M{x},
728the collector will shift back to minor collections
729after a major collection collects at least @M{x%}
730of the objects allocated during the last cycle.
731In particular, for a multiplier of 0,
732the collector will immediately shift back to minor collections
733after doing one cycle of major collections.
734The default value is 50.
725 735
726} 736}
727 737
@@ -1467,7 +1477,7 @@ It has the following syntax:
1467 exp @bnfter{,} exp @bnfopt{@bnfter{,} exp} @Rw{do} block @Rw{end}} 1477 exp @bnfter{,} exp @bnfopt{@bnfter{,} exp} @Rw{do} block @Rw{end}}
1468} 1478}
1469The given identifier (@bnfNter{Name}) defines the control variable, 1479The given identifier (@bnfNter{Name}) defines the control variable,
1470which is a new variable local to the loop body (@emph{block}). 1480which is a new read-only variable local to the loop body (@emph{block}).
1471 1481
1472The loop starts by evaluating once the three control expressions. 1482The loop starts by evaluating once the three control expressions.
1473Their values are called respectively 1483Their values are called respectively
@@ -1499,11 +1509,6 @@ For integer loops,
1499the control variable never wraps around; 1509the control variable never wraps around;
1500instead, the loop ends in case of an overflow. 1510instead, the loop ends in case of an overflow.
1501 1511
1502You should not change the value of the control variable
1503during the loop.
1504If you need its value after the loop,
1505assign it to another variable before exiting the loop.
1506
1507} 1512}
1508 1513
1509@sect4{@title{The generic @Rw{for} loop} 1514@sect4{@title{The generic @Rw{for} loop}
@@ -1526,7 +1531,8 @@ for @rep{var_1}, @Cdots, @rep{var_n} in @rep{explist} do @rep{body} end
1526works as follows. 1531works as follows.
1527 1532
1528The names @rep{var_i} declare loop variables local to the loop body. 1533The names @rep{var_i} declare loop variables local to the loop body.
1529The first of these variables is the @emph{control variable}. 1534The first of these variables is the @emph{control variable},
1535which is a read-only variable.
1530 1536
1531The loop starts by evaluating @rep{explist} 1537The loop starts by evaluating @rep{explist}
1532to produce four values: 1538to produce four values:
@@ -1550,9 +1556,6 @@ to-be-closed variable @see{to-be-closed},
1550which can be used to release resources when the loop ends. 1556which can be used to release resources when the loop ends.
1551Otherwise, it does not interfere with the loop. 1557Otherwise, it does not interfere with the loop.
1552 1558
1553You should not change the value of the control variable
1554during the loop.
1555
1556} 1559}
1557 1560
1558} 1561}
@@ -1586,7 +1589,8 @@ Each variable name may be postfixed by an attribute
1586@producname{attrib}@producbody{@bnfopt{@bnfter{<} @bnfNter{Name} @bnfter{>}}} 1589@producname{attrib}@producbody{@bnfopt{@bnfter{<} @bnfNter{Name} @bnfter{>}}}
1587} 1590}
1588There are two possible attributes: 1591There are two possible attributes:
1589@id{const}, which declares a @x{constant variable}, 1592@id{const}, which declares a @emph{constant} or @emph{read-only} variable,
1593@index{constant variable}
1590that is, a variable that cannot be assigned to 1594that is, a variable that cannot be assigned to
1591after its initialization; 1595after its initialization;
1592and @id{close}, which declares a to-be-closed variable @see{to-be-closed}. 1596and @id{close}, which declares a to-be-closed variable @see{to-be-closed}.
@@ -2569,8 +2573,8 @@ See also @Lid{luaL_checklstring}, @Lid{luaL_checkstring},
2569and @Lid{luaL_tolstring} in the auxiliary library.) 2573and @Lid{luaL_tolstring} in the auxiliary library.)
2570 2574
2571In general, 2575In general,
2572Lua's garbage collection can free or move internal memory 2576Lua's garbage collection can free or move memory
2573and then invalidate pointers to internal strings. 2577and then invalidate pointers to strings handled by a Lua state.
2574To allow a safe use of these pointers, 2578To allow a safe use of these pointers,
2575the API guarantees that any pointer to a string in a stack index 2579the API guarantees that any pointer to a string in a stack index
2576is valid while the string value at that index is not removed from the stack. 2580is valid while the string value at that index is not removed from the stack.
@@ -2641,8 +2645,8 @@ string keys starting with an underscore followed by
2641uppercase letters are reserved for Lua. 2645uppercase letters are reserved for Lua.
2642 2646
2643The integer keys in the registry are used 2647The integer keys in the registry are used
2644by the reference mechanism @seeC{luaL_ref} 2648by the reference mechanism @seeC{luaL_ref},
2645and by some predefined values. 2649with some predefined values.
2646Therefore, integer keys in the registry 2650Therefore, integer keys in the registry
2647must not be used for other purposes. 2651must not be used for other purposes.
2648 2652
@@ -2736,7 +2740,8 @@ For such errors, Lua does not call the @x{message handler}.
2736 2740
2737@item{@defid{LUA_ERRERR}| error while running the @x{message handler}.} 2741@item{@defid{LUA_ERRERR}| error while running the @x{message handler}.}
2738 2742
2739@item{@defid{LUA_ERRSYNTAX}| syntax error during precompilation.} 2743@item{@defid{LUA_ERRSYNTAX}| syntax error during precompilation
2744or format error in a binary chunk.}
2740 2745
2741@item{@defid{LUA_YIELD}| the thread (coroutine) yields.} 2746@item{@defid{LUA_YIELD}| the thread (coroutine) yields.}
2742 2747
@@ -3163,8 +3168,6 @@ The index must be the last index previously marked to be closed
3163A @idx{__close} metamethod cannot yield 3168A @idx{__close} metamethod cannot yield
3164when called through this function. 3169when called through this function.
3165 3170
3166(This function was introduced in @N{release 5.4.3}.)
3167
3168} 3171}
3169 3172
3170@APIEntry{int lua_closethread (lua_State *L, lua_State *from);| 3173@APIEntry{int lua_closethread (lua_State *L, lua_State *from);|
@@ -3184,8 +3187,6 @@ The parameter @id{from} represents the coroutine that is resetting @id{L}.
3184If there is no such coroutine, 3187If there is no such coroutine,
3185this parameter can be @id{NULL}. 3188this parameter can be @id{NULL}.
3186 3189
3187(This function was introduced in @N{release 5.4.6}.)
3188
3189} 3190}
3190 3191
3191@APIEntry{int lua_compare (lua_State *L, int index1, int index2, int op);| 3192@APIEntry{int lua_compare (lua_State *L, int index1, int index2, int op);|
@@ -3233,11 +3234,11 @@ Values at other positions are not affected.
3233 3234
3234} 3235}
3235 3236
3236@APIEntry{void lua_createtable (lua_State *L, int narr, int nrec);| 3237@APIEntry{void lua_createtable (lua_State *L, int nseq, int nrec);|
3237@apii{0,1,m} 3238@apii{0,1,m}
3238 3239
3239Creates a new empty table and pushes it onto the stack. 3240Creates a new empty table and pushes it onto the stack.
3240Parameter @id{narr} is a hint for how many elements the table 3241Parameter @id{nseq} is a hint for how many elements the table
3241will have as a sequence; 3242will have as a sequence;
3242parameter @id{nrec} is a hint for how many other elements 3243parameter @id{nrec} is a hint for how many other elements
3243the table will have. 3244the table will have.
@@ -3264,6 +3265,13 @@ As it produces parts of the chunk,
3264with the given @id{data} 3265with the given @id{data}
3265to write them. 3266to write them.
3266 3267
3268The function @Lid{lua_dump} fully preserves the Lua stack
3269through the calls to the writer function,
3270except that it may push some values for internal use
3271before the first call,
3272and it restores the stack size to its original size
3273after the last call.
3274
3267If @id{strip} is true, 3275If @id{strip} is true,
3268the binary representation may not include all debug information 3276the binary representation may not include all debug information
3269about the function, 3277about the function,
@@ -3273,8 +3281,6 @@ The value returned is the error code returned by the last
3273call to the writer; 3281call to the writer;
3274@N{0 means} no errors. 3282@N{0 means} no errors.
3275 3283
3276This function does not pop the Lua function from the stack.
3277
3278} 3284}
3279 3285
3280@APIEntry{int lua_error (lua_State *L);| 3286@APIEntry{int lua_error (lua_State *L);|
@@ -3299,50 +3305,62 @@ For options that need extra arguments,
3299they are listed after the option. 3305they are listed after the option.
3300@description{ 3306@description{
3301 3307
3302@item{@id{LUA_GCCOLLECT}| 3308@item{@defid{LUA_GCCOLLECT}|
3303Performs a full garbage-collection cycle. 3309Performs a full garbage-collection cycle.
3304} 3310}
3305 3311
3306@item{@id{LUA_GCSTOP}| 3312@item{@defid{LUA_GCSTOP}|
3307Stops the garbage collector. 3313Stops the garbage collector.
3308} 3314}
3309 3315
3310@item{@id{LUA_GCRESTART}| 3316@item{@defid{LUA_GCRESTART}|
3311Restarts the garbage collector. 3317Restarts the garbage collector.
3312} 3318}
3313 3319
3314@item{@id{LUA_GCCOUNT}| 3320@item{@defid{LUA_GCCOUNT}|
3315Returns the current amount of memory (in Kbytes) in use by Lua. 3321Returns the current amount of memory (in Kbytes) in use by Lua.
3316} 3322}
3317 3323
3318@item{@id{LUA_GCCOUNTB}| 3324@item{@defid{LUA_GCCOUNTB}|
3319Returns the remainder of dividing the current amount of bytes of 3325Returns the remainder of dividing the current amount of bytes of
3320memory in use by Lua by 1024. 3326memory in use by Lua by 1024.
3321} 3327}
3322 3328
3323@item{@id{LUA_GCSTEP} @T{(int stepsize)}| 3329@item{@defid{LUA_GCSTEP} (int n)|
3324Performs an incremental step of garbage collection, 3330Performs a step of garbage collection.
3325corresponding to the allocation of @id{stepsize} Kbytes.
3326} 3331}
3327 3332
3328@item{@id{LUA_GCISRUNNING}| 3333@item{@defid{LUA_GCISRUNNING}|
3329Returns a boolean that tells whether the collector is running 3334Returns a boolean that tells whether the collector is running
3330(i.e., not stopped). 3335(i.e., not stopped).
3331} 3336}
3332 3337
3333@item{@id{LUA_GCINC} (int pause, int stepmul, stepsize)| 3338@item{@defid{LUA_GCINC}|
3334Changes the collector to incremental mode 3339Changes the collector to incremental mode.
3335with the given parameters @see{incmode}.
3336Returns the previous mode (@id{LUA_GCGEN} or @id{LUA_GCINC}). 3340Returns the previous mode (@id{LUA_GCGEN} or @id{LUA_GCINC}).
3337} 3341}
3338 3342
3339@item{@id{LUA_GCGEN} (int minormul, int majormul)| 3343@item{@defid{LUA_GCGEN}|
3340Changes the collector to generational mode 3344Changes the collector to generational mode.
3341with the given parameters @see{genmode}.
3342Returns the previous mode (@id{LUA_GCGEN} or @id{LUA_GCINC}). 3345Returns the previous mode (@id{LUA_GCGEN} or @id{LUA_GCINC}).
3343} 3346}
3344 3347
3348@item{@defid{LUA_GCPARAM} (int param, int val)|
3349Changes and/or returns the value of a parameter of the collector.
3350If @id{val} is negative, the call only returns the current value.
3351The argument @id{param} must have one of the following values:
3352@description{
3353@item{@defid{LUA_GCPMINORMUL}| The minor multiplier. }
3354@item{@defid{LUA_GCPMAJORMINOR}| The major-minor multiplier. }
3355@item{@defid{LUA_GCPMINORMAJOR}| The minor-major multiplier. }
3356@item{@defid{LUA_GCPPAUSE}| The garbage-collector pause. }
3357@item{@defid{LUA_GCPSTEPMUL}| The step multiplier. }
3358@item{@defid{LUA_GCPSTEPSIZE}| The step size. }
3359}
3360}
3361
3345} 3362}
3363
3346For more details about these options, 3364For more details about these options,
3347see @Lid{collectgarbage}. 3365see @Lid{collectgarbage}.
3348 3366
@@ -3652,10 +3670,27 @@ and loads it accordingly (see program @idx{luac}).
3652The string @id{mode} works as in function @Lid{load}, 3670The string @id{mode} works as in function @Lid{load},
3653with the addition that 3671with the addition that
3654a @id{NULL} value is equivalent to the string @St{bt}. 3672a @id{NULL} value is equivalent to the string @St{bt}.
3655 3673Moreover, it may have a @Char{B} instead of a @Char{b},
3656@id{lua_load} uses the stack internally, 3674meaning a @emphx{fixed buffer} with the binary dump.
3657so the reader function must always leave the stack 3675
3658unmodified when returning. 3676A fixed buffer means that the address returned by the reader function
3677will contain the chunk until everything created by the chunk has
3678been collected;
3679therefore, Lua can avoid copying to internal structures
3680some parts of the chunk.
3681(In general, a fixed buffer would keep the chunk
3682as its contents until the end of the program,
3683for instance with the chunk in ROM.)
3684Moreover, for a fixed buffer,
3685the reader function should return the entire chunk in the first read.
3686(As an example, @Lid{luaL_loadbufferx} does that.)
3687
3688The function @Lid{lua_load} fully preserves the Lua stack
3689through the calls to the reader function,
3690except that it may push some values for internal use
3691before the first call,
3692and it restores the stack size to its original size plus one
3693(for the pushed result) after the last call.
3659 3694
3660@id{lua_load} can return 3695@id{lua_load} can return
3661@Lid{LUA_OK}, @Lid{LUA_ERRSYNTAX}, or @Lid{LUA_ERRMEM}. 3696@Lid{LUA_OK}, @Lid{LUA_ERRSYNTAX}, or @Lid{LUA_ERRMEM}.
@@ -3671,7 +3706,8 @@ Other upvalues are initialized with @nil.
3671 3706
3672} 3707}
3673 3708
3674@APIEntry{lua_State *lua_newstate (lua_Alloc f, void *ud);| 3709@APIEntry{lua_State *lua_newstate (lua_Alloc f, void *ud,
3710 unsigned int seed);|
3675@apii{0,0,-} 3711@apii{0,0,-}
3676 3712
3677Creates a new independent state and returns its main thread. 3713Creates a new independent state and returns its main thread.
@@ -3682,6 +3718,8 @@ Lua will do all memory allocation for this state
3682through this function @seeF{lua_Alloc}. 3718through this function @seeF{lua_Alloc}.
3683The second argument, @id{ud}, is an opaque pointer that Lua 3719The second argument, @id{ud}, is an opaque pointer that Lua
3684passes to the allocator in every call. 3720passes to the allocator in every call.
3721The third argument, @id{seed}, is a seed for the hashing of
3722strings when they are used as table keys.
3685 3723
3686} 3724}
3687 3725
@@ -3898,6 +3936,40 @@ This function is equivalent to @Lid{lua_pushcclosure} with no upvalues.
3898 3936
3899} 3937}
3900 3938
3939@APIEntry{const char *(lua_pushextlstring) (lua_State *L,
3940 const char *s, size_t len, lua_Alloc falloc, void *ud);|
3941@apii{0,1,m}
3942
3943Creates an @emphx{external string},
3944that is, a string that uses memory not managed by Lua.
3945The pointer @id{s} points to the exernal buffer
3946holding the string content,
3947and @id{len} is the length of the string.
3948The string should have a zero at its end,
3949that is, the condition @T{s[len] == '\0'} should hold.
3950
3951If @id{falloc} is different from @id{NULL},
3952that function will be called by Lua
3953when the external buffer is no longer needed.
3954The contents of the buffer should not change before this call.
3955The function will be called with the given @id{ud},
3956the string @id{s} as the block,
3957the length plus one (to account for the ending zero) as the old size,
3958and 0 as the new size.
3959
3960Lua always @x{internalizes} strings with lengths up to 40 characters.
3961So, for strings in that range,
3962this function will immediately internalize the string
3963and call @id{falloc} to free the buffer.
3964
3965Even when using an external buffer,
3966Lua still has to allocate a header for the string.
3967In case of a memory-allocation error,
3968Lua will call @id{falloc} before raising the error.
3969
3970}
3971
3972
3901@APIEntry{const char *lua_pushfstring (lua_State *L, const char *fmt, ...);| 3973@APIEntry{const char *lua_pushfstring (lua_State *L, const char *fmt, ...);|
3902@apii{0,1,v} 3974@apii{0,1,v}
3903 3975
@@ -4181,14 +4253,6 @@ and then pops the top element.
4181 4253
4182} 4254}
4183 4255
4184@APIEntry{int lua_resetthread (lua_State *L);|
4185@apii{0,?,-}
4186
4187This function is deprecated;
4188it is equivalent to @Lid{lua_closethread} with
4189@id{from} being @id{NULL}.
4190
4191}
4192 4256
4193@APIEntry{int lua_resume (lua_State *L, lua_State *from, int nargs, 4257@APIEntry{int lua_resume (lua_State *L, lua_State *from, int nargs,
4194 int *nresults);| 4258 int *nresults);|
@@ -4470,8 +4534,6 @@ indicates whether the operation succeeded.
4470@apii{0,0,m} 4534@apii{0,0,m}
4471 4535
4472Converts the Lua value at the given index to a @N{C string}. 4536Converts the Lua value at the given index to a @N{C string}.
4473If @id{len} is not @id{NULL},
4474it sets @T{*len} with the string length.
4475The Lua value must be a string or a number; 4537The Lua value must be a string or a number;
4476otherwise, the function returns @id{NULL}. 4538otherwise, the function returns @id{NULL}.
4477If the value is a number, 4539If the value is a number,
@@ -4480,15 +4542,19 @@ then @id{lua_tolstring} also
4480(This change confuses @Lid{lua_next} 4542(This change confuses @Lid{lua_next}
4481when @id{lua_tolstring} is applied to keys during a table traversal.) 4543when @id{lua_tolstring} is applied to keys during a table traversal.)
4482 4544
4483@id{lua_tolstring} returns a pointer 4545If @id{len} is not @id{NULL},
4484to a string inside the Lua state @see{constchar}. 4546the function sets @T{*len} with the string length.
4485This string always has a zero (@Char{\0}) 4547The returned @N{C string} always has a zero (@Char{\0})
4486after its last character (as @N{in C}), 4548after its last character,
4487but can contain other zeros in its body. 4549but can contain other zeros in its body.
4488 4550
4551The pointer returned by @id{lua_tolstring}
4552may be invalidated by the garbage collector if the
4553corresponding Lua value is removed from the stack @see{constchar}.
4554
4489This function can raise memory errors only 4555This function can raise memory errors only
4490when converting a number to a string 4556when converting a number to a string
4491(as then it has to create a new string). 4557(as then it may have to create a new string).
4492 4558
4493} 4559}
4494 4560
@@ -4646,6 +4712,10 @@ passing along the buffer to be written (@id{p}),
4646its size (@id{sz}), 4712its size (@id{sz}),
4647and the @id{ud} parameter supplied to @Lid{lua_dump}. 4713and the @id{ud} parameter supplied to @Lid{lua_dump}.
4648 4714
4715After @Lid{lua_dump} writes its last piece,
4716it will signal that by calling the writer function one more time,
4717with a @id{NULL} buffer (and size 0).
4718
4649The writer returns an error code: 4719The writer returns an error code:
4650@N{0 means} no errors; 4720@N{0 means} no errors;
4651any other value means an error and stops @Lid{lua_dump} from 4721any other value means an error and stops @Lid{lua_dump} from
@@ -5695,6 +5765,8 @@ This function returns the same results as @Lid{lua_load}.
5695@id{name} is the chunk name, 5765@id{name} is the chunk name,
5696used for debug information and error messages. 5766used for debug information and error messages.
5697The string @id{mode} works as in the function @Lid{lua_load}. 5767The string @id{mode} works as in the function @Lid{lua_load}.
5768In particular, this function supports mode @Char{B} for
5769fixed buffers.
5698 5770
5699} 5771}
5700 5772
@@ -5741,6 +5813,16 @@ it does not run it.
5741 5813
5742} 5814}
5743 5815
5816@APIEntry{unsigned int luaL_makeseed (lua_State *L);|
5817@apii{0,0,-}
5818
5819Returns a value with a weak attempt for randomness.
5820(It produces that value based on the current date and time
5821and the address of an internal variable,
5822in case the machine has Address Space Layout Randomization.)
5823
5824}
5825
5744 5826
5745@APIEntry{void luaL_newlib (lua_State *L, const luaL_Reg l[]);| 5827@APIEntry{void luaL_newlib (lua_State *L, const luaL_Reg l[]);|
5746@apii{0,1,m} 5828@apii{0,1,m}
@@ -5940,11 +6022,21 @@ Creates and returns a @def{reference},
5940in the table at index @id{t}, 6022in the table at index @id{t},
5941for the object on the top of the stack (and pops the object). 6023for the object on the top of the stack (and pops the object).
5942 6024
5943A reference is a unique integer key. 6025The reference system uses the integer keys of the table.
5944As long as you do not manually add integer keys into the table @id{t}, 6026A reference is a unique integer key;
5945@Lid{luaL_ref} ensures the uniqueness of the key it returns. 6027@Lid{luaL_ref} ensures the uniqueness of the keys it returns.
6028The entry 1 is reserved for internal use.
6029Before the first use of @Lid{luaL_ref},
6030the integer keys of the table
6031should form a proper sequence (no holes),
6032and the value at entry 1 should be false:
6033@nil if the sequence is empty,
6034@false otherwise.
6035You should not manually set integer keys in the table
6036after the first use of @Lid{luaL_ref}.
6037
5946You can retrieve an object referred by the reference @id{r} 6038You can retrieve an object referred by the reference @id{r}
5947by calling @T{lua_rawgeti(L, t, r)}. 6039by calling @T{lua_rawgeti(L, t, r)} or @T{lua_geti(L, t, r)}.
5948The function @Lid{luaL_unref} frees a reference. 6040The function @Lid{luaL_unref} frees a reference.
5949 6041
5950If the object on the top of the stack is @nil, 6042If the object on the top of the stack is @nil,
@@ -6110,8 +6202,8 @@ Returns the name of the type of the value at the given index.
6110Releases the reference @id{ref} from the table at index @id{t} 6202Releases the reference @id{ref} from the table at index @id{t}
6111@seeC{luaL_ref}. 6203@seeC{luaL_ref}.
6112The entry is removed from the table, 6204The entry is removed from the table,
6113so that the referred object can be collected. 6205so that the referred object can be collected and
6114The reference @id{ref} is also freed to be used again. 6206the reference @id{ref} can be used again by @Lid{luaL_ref}.
6115 6207
6116If @id{ref} is @Lid{LUA_NOREF} or @Lid{LUA_REFNIL}, 6208If @id{ref} is @Lid{LUA_NOREF} or @Lid{LUA_REFNIL},
6117@Lid{luaL_unref} does nothing. 6209@Lid{luaL_unref} does nothing.
@@ -6268,13 +6360,25 @@ gives the exact number of bytes in use by Lua.
6268 6360
6269@item{@St{step}| 6361@item{@St{step}|
6270Performs a garbage-collection step. 6362Performs a garbage-collection step.
6271The step @Q{size} is controlled by @id{arg}. 6363This option may be followed by an extra argument,
6272With a zero value, 6364an integer with the step size.
6273the collector will perform one basic (indivisible) step. 6365The default for this argument is zero.
6274For non-zero values, 6366
6275the collector will perform as if that amount of memory 6367If the size is a positive @id{n},
6276(in Kbytes) had been allocated by Lua. 6368the collector acts as if @id{n} new objects have been created.
6277Returns @true if the step finished a collection cycle. 6369If the size is zero,
6370the collector performs a basic step.
6371In incremental mode,
6372a basic step corresponds to the current step size.
6373In generational mode,
6374a basic step performs a full minor collection or
6375a major collection,
6376if the collector has scheduled one.
6377
6378In incremental mode,
6379the function returns @true if the step finished a collection cycle.
6380In generational mode,
6381the function returns @true if the step performed a major collection.
6278} 6382}
6279 6383
6280@item{@St{isrunning}| 6384@item{@St{isrunning}|
@@ -6283,20 +6387,34 @@ Returns a boolean that tells whether the collector is running
6283} 6387}
6284 6388
6285@item{@St{incremental}| 6389@item{@St{incremental}|
6286Change the collector mode to incremental. 6390Changes the collector mode to incremental and returns the previous mode.
6287This option can be followed by three numbers:
6288the garbage-collector pause,
6289the step multiplier,
6290and the step size @see{incmode}.
6291A zero means to not change that value.
6292} 6391}
6293 6392
6294@item{@St{generational}| 6393@item{@St{generational}|
6295Change the collector mode to generational. 6394Changes the collector mode to generational and returns the previous mode.
6296This option can be followed by two numbers: 6395}
6297the garbage-collector minor multiplier 6396
6298and the major multiplier @see{genmode}. 6397@item{@St{param}|
6299A zero means to not change that value. 6398Changes and/or retrieves the values of a parameter of the collector.
6399This option must be followed by one or two extra arguments:
6400The name of the parameter being changed or retrieved (a string)
6401and an optional new value for that parameter (an integer).
6402The first argument must have one of the following values:
6403@description{
6404@item{@St{minormul}| The minor multiplier. }
6405@item{@St{majorminor}| The major-minor multiplier. }
6406@item{@St{minormajor}| The minor-major multiplier. }
6407@item{@St{pause}| The garbage-collector pause. }
6408@item{@St{stepmul}| The step multiplier. }
6409@item{@St{stepsize}| The step size. }
6410}
6411The call always returns the previous value of the parameter.
6412If the call does not give a new value,
6413the value is left unchanged.
6414
6415Lua rounds these values before storing them;
6416so, the value returned as the previous value may not be
6417exactly the last value set.
6300} 6418}
6301 6419
6302} 6420}
@@ -6913,9 +7031,9 @@ including if necessary a path and an extension.
6913(which may depend on the @N{C compiler} and linker used). 7031(which may depend on the @N{C compiler} and linker used).
6914 7032
6915This functionality is not supported by @N{ISO C}. 7033This functionality is not supported by @N{ISO C}.
6916As such, it is only available on some platforms 7034As such, @id{loadlib} is only available on some platforms:
6917(Windows, Linux, Mac OS X, Solaris, BSD, 7035Linux, Windows, Mac OS X, Solaris, BSD,
6918plus other Unix systems that support the @id{dlfcn} standard). 7036plus other Unix systems that support the @id{dlfcn} standard.
6919 7037
6920This function is inherently insecure, 7038This function is inherently insecure,
6921as it allows Lua to call any function in any readable dynamic 7039as it allows Lua to call any function in any readable dynamic
@@ -7855,6 +7973,19 @@ If @id{i} is greater than @id{j}, returns the empty string.
7855 7973
7856} 7974}
7857 7975
7976@LibEntry{table.create (nseq [, nrec])|
7977
7978Creates a new empty table, preallocating memory.
7979This preallocation may help performance and save memory
7980when you know in advance how many elements the table will have.
7981
7982Parameter @id{nseq} is a hint for how many elements the table
7983will have as a sequence.
7984Optional parameter @id{nrec} is a hint for how many other elements
7985the table will have; its default is zero.
7986
7987}
7988
7858@LibEntry{table.insert (list, [pos,] value)| 7989@LibEntry{table.insert (list, [pos,] value)|
7859 7990
7860Inserts element @id{value} at position @id{pos} in @id{list}, 7991Inserts element @id{value} at position @id{pos} in @id{list},
@@ -8113,7 +8244,7 @@ different sequences of results each time the program runs.
8113 8244
8114When called with at least one argument, 8245When called with at least one argument,
8115the integer parameters @id{x} and @id{y} are 8246the integer parameters @id{x} and @id{y} are
8116joined into a 128-bit @emphx{seed} that 8247joined into a @emphx{seed} that
8117is used to reinitialize the pseudo-random generator; 8248is used to reinitialize the pseudo-random generator;
8118equal seeds produce equal sequences of numbers. 8249equal seeds produce equal sequences of numbers.
8119The default for @id{y} is zero. 8250The default for @id{y} is zero.
@@ -9009,7 +9140,6 @@ The options are:
9009@item{@T{--}| stop handling options;} 9140@item{@T{--}| stop handling options;}
9010@item{@T{-}| execute @id{stdin} as a file and stop handling options.} 9141@item{@T{-}| execute @id{stdin} as a file and stop handling options.}
9011} 9142}
9012(The form @T{-l @rep{g=mod}} was introduced in @N{release 5.4.4}.)
9013 9143
9014After handling its options, @id{lua} runs the given @emph{script}. 9144After handling its options, @id{lua} runs the given @emph{script}.
9015When called without arguments, 9145When called without arguments,
@@ -9136,7 +9266,7 @@ is a more portable solution.
9136@simplesect{ 9266@simplesect{
9137 9267
9138Here we list the incompatibilities that you may find when moving a program 9268Here we list the incompatibilities that you may find when moving a program
9139from @N{Lua 5.3} to @N{Lua 5.4}. 9269from @N{Lua 5.4} to @N{Lua 5.5}.
9140 9270
9141You can avoid some incompatibilities by compiling Lua with 9271You can avoid some incompatibilities by compiling Lua with
9142appropriate options (see file @id{luaconf.h}). 9272appropriate options (see file @id{luaconf.h}).
@@ -9173,51 +9303,9 @@ change between versions.
9173@itemize{ 9303@itemize{
9174 9304
9175@item{ 9305@item{
9176The coercion of strings to numbers in 9306The control variable in @Rw{for} loops are read only.
9177arithmetic and bitwise operations 9307If you need to change it,
9178has been removed from the core language. 9308declare a local variable with the same name in the loop body.
9179The string library does a similar job
9180for arithmetic (but not for bitwise) operations
9181using the string metamethods.
9182However, unlike in previous versions,
9183the new implementation preserves the implicit type of the numeral
9184in the string.
9185For instance, the result of @T{"1" + "2"} now is an integer,
9186not a float.
9187}
9188
9189@item{
9190Literal decimal integer constants that overflow are read as floats,
9191instead of wrapping around.
9192You can use hexadecimal notation for such constants if you
9193want the old behavior
9194(reading them as integers with wrap around).
9195}
9196
9197@item{
9198The use of the @idx{__lt} metamethod to emulate @idx{__le}
9199has been removed.
9200When needed, this metamethod must be explicitly defined.
9201}
9202
9203@item{
9204The semantics of the numerical @Rw{for} loop
9205over integers changed in some details.
9206In particular, the control variable never wraps around.
9207}
9208
9209@item{
9210A label for a @Rw{goto} cannot be declared where a label with the same
9211name is visible, even if this other label is declared in an enclosing
9212block.
9213}
9214
9215@item{
9216When finalizing an object,
9217Lua does not ignore @idx{__gc} metamethods that are not functions.
9218Any value will be called, if present.
9219(Non-callable values will generate a warning,
9220like any other error when calling a finalizer.)
9221} 9309}
9222 9310
9223} 9311}
@@ -9228,39 +9316,10 @@ like any other error when calling a finalizer.)
9228@itemize{ 9316@itemize{
9229 9317
9230@item{ 9318@item{
9231The function @Lid{print} does not call @Lid{tostring} 9319Parameters for the garbage collection are not set
9232to format its arguments; 9320with the options @St{incremental} and @St{generational};
9233instead, it has this functionality hardwired. 9321instead, there is a new option @St{param} to that end.
9234You should use @idx{__tostring} to modify how values are printed. 9322Moreover, there were some changes in the parameters themselves.
9235}
9236
9237@item{
9238The pseudo-random number generator used by the function @Lid{math.random}
9239now starts with a somewhat random seed.
9240Moreover, it uses a different algorithm.
9241}
9242
9243@item{
9244By default, the decoding functions in the @Lid{utf8} library
9245do not accept surrogates as valid code points.
9246An extra parameter in these functions makes them more permissive.
9247}
9248
9249@item{
9250The options @St{setpause} and @St{setstepmul}
9251of the function @Lid{collectgarbage} are deprecated.
9252You should use the new option @St{incremental} to set them.
9253}
9254
9255@item{
9256The function @Lid{io.lines} now returns four values,
9257instead of just one.
9258That can be a problem when it is used as the sole
9259argument to another function that has optional parameters,
9260such as in @T{load(io.lines(filename, "L"))}.
9261To fix that issue,
9262you can wrap the call into parentheses,
9263to adjust its number of results to one.
9264} 9323}
9265 9324
9266} 9325}
@@ -9272,46 +9331,24 @@ to adjust its number of results to one.
9272@itemize{ 9331@itemize{
9273 9332
9274@item{ 9333@item{
9275Full userdata now has an arbitrary number of associated user values. 9334The function @id{lua_resetthread} is deprecated;
9276Therefore, the functions @id{lua_newuserdata}, 9335it is equivalent to @Lid{lua_closethread} with
9277@id{lua_setuservalue}, and @id{lua_getuservalue} were 9336@id{from} being @id{NULL}.
9278replaced by @Lid{lua_newuserdatauv},
9279@Lid{lua_setiuservalue}, and @Lid{lua_getiuservalue},
9280which have an extra argument.
9281
9282For compatibility, the old names still work as macros assuming
9283one single user value.
9284Note, however, that userdata with zero user values
9285are more efficient memory-wise.
9286}
9287
9288@item{
9289The function @Lid{lua_resume} has an extra parameter.
9290This out parameter returns the number of values on
9291the top of the stack that were yielded or returned by the coroutine.
9292(In previous versions,
9293those values were the entire stack.)
9294}
9295
9296@item{
9297The function @Lid{lua_version} returns the version number,
9298instead of an address of the version number.
9299The Lua core should work correctly with libraries using their
9300own static copies of the same core,
9301so there is no need to check whether they are using the same
9302address space.
9303} 9337}
9304 9338
9305@item{ 9339@item{
9306The constant @id{LUA_ERRGCMM} was removed. 9340The function @Lid{lua_dump} changed the way it keeps the stack
9307Errors in finalizers are never propagated; 9341through the calls to the writer function.
9308instead, they generate a warning. 9342(That was not specified in previous versions.)
9343Also, it calls the writer function one extra time,
9344to signal the end of the dump.
9309} 9345}
9310 9346
9311@item{ 9347@item{
9312The options @idx{LUA_GCSETPAUSE} and @idx{LUA_GCSETSTEPMUL} 9348Parameters for the garbage collection are not set
9313of the function @Lid{lua_gc} are deprecated. 9349with the options @Lid{LUA_GCINC} and @Lid{LUA_GCGEN};
9314You should use the new option @id{LUA_GCINC} to set them. 9350instead, there is a new option @Lid{LUA_GCPARAM} to that end.
9351Moreover, there were some changes in the parameters themselves.
9315} 9352}
9316 9353
9317} 9354}