aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2020-09-09 17:28:25 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2020-09-09 17:28:25 -0300
commit613513d09f89834bea1810bc09b9296560328d92 (patch)
treeff169b6aa20c0b09f6411c98fb0ec1242dd2710b
parent8496112a18c887449d37e5aadbb6daec34ae54c0 (diff)
downloadlua-613513d09f89834bea1810bc09b9296560328d92.tar.gz
lua-613513d09f89834bea1810bc09b9296560328d92.tar.bz2
lua-613513d09f89834bea1810bc09b9296560328d92.zip
Better documentation for the GC of strings in the C API
Plus some other small changes.
-rw-r--r--manual/manual.of78
1 files changed, 58 insertions, 20 deletions
diff --git a/manual/manual.of b/manual/manual.of
index 9c275d15..c37f3061 100644
--- a/manual/manual.of
+++ b/manual/manual.of
@@ -2450,7 +2450,7 @@ When you call a Lua function
2450without a fixed number of results @seeF{lua_call}, 2450without a fixed number of results @seeF{lua_call},
2451Lua ensures that the stack has enough space for all results, 2451Lua ensures that the stack has enough space for all results,
2452but it does not ensure any extra space. 2452but it does not ensure any extra space.
2453So, before pushing anything in the stack after such a call 2453So, before pushing anything on the stack after such a call
2454you should use @Lid{lua_checkstack}. 2454you should use @Lid{lua_checkstack}.
2455 2455
2456} 2456}
@@ -2497,6 +2497,39 @@ which behaves like a nil value.
2497 2497
2498} 2498}
2499 2499
2500@sect3{constchar|@title{Pointers to strings}
2501
2502Several functions in the API return pointers (@T{const char*})
2503to Lua strings in the stack.
2504(See @Lid{lua_pushfstring}, @Lid{lua_pushlstring},
2505@Lid{lua_pushstring}, and @Lid{lua_tolstring}.
2506See also @Lid{luaL_checklstring}, @Lid{luaL_checkstring},
2507and @Lid{luaL_tolstring} in the auxiliary library.)
2508
2509In general,
2510Lua's garbage collection can free or move internal memory
2511and then invalidate pointers to internal strings.
2512To allow a safe use of these pointers,
2513The API guarantees that any pointer to a string in a stack index
2514is valid while the value at that index is neither modified nor popped.
2515When the index is a pseudo-index (referring to an upvalue),
2516the pointer is valid while the corresponding call is active and
2517the corresponding upvalue is not modified.
2518
2519Some functions in the debug interface
2520also return pointers to strings,
2521namely @Lid{lua_getlocal}, @Lid{lua_getupvalue},
2522@Lid{lua_setlocal}, and @Lid{lua_setupvalue}.
2523For these functions, the pointer is guaranteed to
2524be valid while the caller function is active and
2525the given closure (if one was given) is in the stack.
2526
2527Except for these guarantees,
2528the garbage collector is free to invalidate
2529any pointer to internal strings.
2530
2531}
2532
2500} 2533}
2501 2534
2502@sect2{c-closure| @title{C Closures} 2535@sect2{c-closure| @title{C Closures}
@@ -2791,7 +2824,7 @@ depending on the situation;
2791an interrogation mark @Char{?} means that 2824an interrogation mark @Char{?} means that
2792we cannot know how many elements the function pops/pushes 2825we cannot know how many elements the function pops/pushes
2793by looking only at its arguments. 2826by looking only at its arguments.
2794(For instance, they may depend on what is on the stack.) 2827(For instance, they may depend on what is in the stack.)
2795The third field, @T{x}, 2828The third field, @T{x},
2796tells whether the function may raise errors: 2829tells whether the function may raise errors:
2797@Char{-} means the function never raises any error; 2830@Char{-} means the function never raises any error;
@@ -3584,6 +3617,10 @@ plus an associated block of raw memory with @id{size} bytes.
3584@Lid{lua_setiuservalue} and @Lid{lua_getiuservalue}.) 3617@Lid{lua_setiuservalue} and @Lid{lua_getiuservalue}.)
3585 3618
3586The function returns the address of the block of memory. 3619The function returns the address of the block of memory.
3620Lua ensures that this address is valid as long as
3621the corresponding userdata is alive @see{GC}.
3622Moreover, if the userdata is marked for finalization @see{finalizers},
3623its address is valid at least until the call to its finalizer.
3587 3624
3588} 3625}
3589 3626
@@ -3764,7 +3801,7 @@ This function is equivalent to @Lid{lua_pushcclosure} with no upvalues.
3764@apii{0,1,v} 3801@apii{0,1,v}
3765 3802
3766Pushes onto the stack a formatted string 3803Pushes onto the stack a formatted string
3767and returns a pointer to this string. 3804and returns a pointer to this string @see{constchar}.
3768It is similar to the @ANSI{sprintf}, 3805It is similar to the @ANSI{sprintf},
3769but has two important differences. 3806but has two important differences.
3770First, 3807First,
@@ -3838,7 +3875,7 @@ the function returns.
3838The string can contain any binary data, 3875The string can contain any binary data,
3839including @x{embedded zeros}. 3876including @x{embedded zeros}.
3840 3877
3841Returns a pointer to the internal copy of the string. 3878Returns a pointer to the internal copy of the string @see{constchar}.
3842 3879
3843} 3880}
3844 3881
@@ -3865,7 +3902,7 @@ Lua will make or reuse an internal copy of the given string,
3865so the memory at @id{s} can be freed or reused immediately after 3902so the memory at @id{s} can be freed or reused immediately after
3866the function returns. 3903the function returns.
3867 3904
3868Returns a pointer to the internal copy of the string. 3905Returns a pointer to the internal copy of the string @see{constchar}.
3869 3906
3870If @id{s} is @id{NULL}, pushes @nil and returns @id{NULL}. 3907If @id{s} is @id{NULL}, pushes @nil and returns @id{NULL}.
3871 3908
@@ -4277,7 +4314,7 @@ otherwise, returns @id{NULL}.
4277} 4314}
4278 4315
4279@APIEntry{void lua_toclose (lua_State *L, int index);| 4316@APIEntry{void lua_toclose (lua_State *L, int index);|
4280@apii{0,0,v} 4317@apii{0,0,m}
4281 4318
4282Marks the given index in the stack as a 4319Marks the given index in the stack as a
4283to-be-closed @Q{variable} @see{to-be-closed}. 4320to-be-closed @Q{variable} @see{to-be-closed}.
@@ -4295,10 +4332,16 @@ by any other function in the API except @Lid{lua_settop} or @Lid{lua_pop}.
4295This function should not be called for an index 4332This function should not be called for an index
4296that is equal to or below an active to-be-closed index. 4333that is equal to or below an active to-be-closed index.
4297 4334
4298This function can raise an out-of-memory error. 4335In the case of an out-of-memory error,
4299In that case, the value in the given index is immediately closed, 4336the value in the given index is immediately closed,
4300as if it was already marked. 4337as if it was already marked.
4301 4338
4339Note that, both in case of errors and of a regular return,
4340by the time the @idx{__close} metamethod runs,
4341the @N{C stack} was already unwound,
4342so that any automatic C variable declared in the calling function
4343will be out of scope.
4344
4302} 4345}
4303 4346
4304@APIEntry{lua_Integer lua_tointeger (lua_State *L, int index);| 4347@APIEntry{lua_Integer lua_tointeger (lua_State *L, int index);|
@@ -4338,15 +4381,11 @@ then @id{lua_tolstring} also
4338when @id{lua_tolstring} is applied to keys during a table traversal.) 4381when @id{lua_tolstring} is applied to keys during a table traversal.)
4339 4382
4340@id{lua_tolstring} returns a pointer 4383@id{lua_tolstring} returns a pointer
4341to a string inside the Lua state. 4384to a string inside the Lua state @see{constchar}.
4342This string always has a zero (@Char{\0}) 4385This string always has a zero (@Char{\0})
4343after its last character (as @N{in C}), 4386after its last character (as @N{in C}),
4344but can contain other zeros in its body. 4387but can contain other zeros in its body.
4345 4388
4346Because Lua has garbage collection,
4347there is no guarantee that the pointer returned by @id{lua_tolstring}
4348will be valid after the corresponding Lua value is removed from the stack.
4349
4350} 4389}
4351 4390
4352@APIEntry{lua_Number lua_tonumber (lua_State *L, int index);| 4391@APIEntry{lua_Number lua_tonumber (lua_State *L, int index);|
@@ -4708,7 +4747,7 @@ true if the function is a vararg function
4708} 4747}
4709 4748
4710@item{@id{ftransfer}| 4749@item{@id{ftransfer}|
4711the index on the stack of the first value being @Q{transferred}, 4750the index in the stack of the first value being @Q{transferred},
4712that is, parameters in a call or return values in a return. 4751that is, parameters in a call or return values in a return.
4713(The other values are in consecutive indices.) 4752(The other values are in consecutive indices.)
4714Using this index, you can access and modify these values 4753Using this index, you can access and modify these values
@@ -4860,7 +4899,7 @@ an identification of the @emph{activation record}
4860of the function executing at a given level. 4899of the function executing at a given level.
4861@N{Level 0} is the current running function, 4900@N{Level 0} is the current running function,
4862whereas level @M{n+1} is the function that has called level @M{n} 4901whereas level @M{n+1} is the function that has called level @M{n}
4863(except for tail calls, which do not count on the stack). 4902(except for tail calls, which do not count in the stack).
4864When called with a level greater than the stack depth, 4903When called with a level greater than the stack depth,
4865@Lid{lua_getstack} returns 0; 4904@Lid{lua_getstack} returns 0;
4866otherwise it returns 1. 4905otherwise it returns 1.
@@ -4947,8 +4986,7 @@ For each event, the hook is called as explained below:
4947@description{ 4986@description{
4948 4987
4949@item{The call hook| is called when the interpreter calls a function. 4988@item{The call hook| is called when the interpreter calls a function.
4950The hook is called just after Lua enters the new function, 4989The hook is called just after Lua enters the new function.
4951before the function gets its arguments.
4952} 4990}
4953 4991
4954@item{The return hook| is called when the interpreter returns from a function. 4992@item{The return hook| is called when the interpreter returns from a function.
@@ -5038,7 +5076,7 @@ refer to the @id{n2}-th upvalue of the Lua closure at index @id{funcindex2}.
5038 5076
5039 5077
5040@C{-------------------------------------------------------------------------} 5078@C{-------------------------------------------------------------------------}
5041@sect1{@title{The Auxiliary Library} 5079@sect1{auxlib|@title{The Auxiliary Library}
5042 5080
5043@simplesect{ 5081@simplesect{
5044 5082
@@ -5925,7 +5963,7 @@ it returns @id{NULL} instead of raising an error.
5925Converts any Lua value at the given index to a @N{C string} 5963Converts any Lua value at the given index to a @N{C string}
5926in a reasonable format. 5964in a reasonable format.
5927The resulting string is pushed onto the stack and also 5965The resulting string is pushed onto the stack and also
5928returned by the function. 5966returned by the function @see{constchar}.
5929If @id{len} is not @id{NULL}, 5967If @id{len} is not @id{NULL},
5930the function also sets @T{*len} with the string length. 5968the function also sets @T{*len} with the string length.
5931 5969
@@ -8608,7 +8646,7 @@ which means the function running at level @id{f} of the call stack
8608of the given thread: 8646of the given thread:
8609@N{level 0} is the current function (@id{getinfo} itself); 8647@N{level 0} is the current function (@id{getinfo} itself);
8610@N{level 1} is the function that called @id{getinfo} 8648@N{level 1} is the function that called @id{getinfo}
8611(except for tail calls, which do not count on the stack); 8649(except for tail calls, which do not count in the stack);
8612and so on. 8650and so on.
8613If @id{f} is a number greater than the number of active functions, 8651If @id{f} is a number greater than the number of active functions,
8614then @id{getinfo} returns @fail. 8652then @id{getinfo} returns @fail.