diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2020-09-09 17:28:25 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2020-09-09 17:28:25 -0300 |
commit | 613513d09f89834bea1810bc09b9296560328d92 (patch) | |
tree | ff169b6aa20c0b09f6411c98fb0ec1242dd2710b | |
parent | 8496112a18c887449d37e5aadbb6daec34ae54c0 (diff) | |
download | lua-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.of | 78 |
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 | |||
2450 | without a fixed number of results @seeF{lua_call}, | 2450 | without a fixed number of results @seeF{lua_call}, |
2451 | Lua ensures that the stack has enough space for all results, | 2451 | Lua ensures that the stack has enough space for all results, |
2452 | but it does not ensure any extra space. | 2452 | but it does not ensure any extra space. |
2453 | So, before pushing anything in the stack after such a call | 2453 | So, before pushing anything on the stack after such a call |
2454 | you should use @Lid{lua_checkstack}. | 2454 | you 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 | |||
2502 | Several functions in the API return pointers (@T{const char*}) | ||
2503 | to Lua strings in the stack. | ||
2504 | (See @Lid{lua_pushfstring}, @Lid{lua_pushlstring}, | ||
2505 | @Lid{lua_pushstring}, and @Lid{lua_tolstring}. | ||
2506 | See also @Lid{luaL_checklstring}, @Lid{luaL_checkstring}, | ||
2507 | and @Lid{luaL_tolstring} in the auxiliary library.) | ||
2508 | |||
2509 | In general, | ||
2510 | Lua's garbage collection can free or move internal memory | ||
2511 | and then invalidate pointers to internal strings. | ||
2512 | To allow a safe use of these pointers, | ||
2513 | The API guarantees that any pointer to a string in a stack index | ||
2514 | is valid while the value at that index is neither modified nor popped. | ||
2515 | When the index is a pseudo-index (referring to an upvalue), | ||
2516 | the pointer is valid while the corresponding call is active and | ||
2517 | the corresponding upvalue is not modified. | ||
2518 | |||
2519 | Some functions in the debug interface | ||
2520 | also return pointers to strings, | ||
2521 | namely @Lid{lua_getlocal}, @Lid{lua_getupvalue}, | ||
2522 | @Lid{lua_setlocal}, and @Lid{lua_setupvalue}. | ||
2523 | For these functions, the pointer is guaranteed to | ||
2524 | be valid while the caller function is active and | ||
2525 | the given closure (if one was given) is in the stack. | ||
2526 | |||
2527 | Except for these guarantees, | ||
2528 | the garbage collector is free to invalidate | ||
2529 | any 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; | |||
2791 | an interrogation mark @Char{?} means that | 2824 | an interrogation mark @Char{?} means that |
2792 | we cannot know how many elements the function pops/pushes | 2825 | we cannot know how many elements the function pops/pushes |
2793 | by looking only at its arguments. | 2826 | by 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.) |
2795 | The third field, @T{x}, | 2828 | The third field, @T{x}, |
2796 | tells whether the function may raise errors: | 2829 | tells 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 | ||
3586 | The function returns the address of the block of memory. | 3619 | The function returns the address of the block of memory. |
3620 | Lua ensures that this address is valid as long as | ||
3621 | the corresponding userdata is alive @see{GC}. | ||
3622 | Moreover, if the userdata is marked for finalization @see{finalizers}, | ||
3623 | its 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 | ||
3766 | Pushes onto the stack a formatted string | 3803 | Pushes onto the stack a formatted string |
3767 | and returns a pointer to this string. | 3804 | and returns a pointer to this string @see{constchar}. |
3768 | It is similar to the @ANSI{sprintf}, | 3805 | It is similar to the @ANSI{sprintf}, |
3769 | but has two important differences. | 3806 | but has two important differences. |
3770 | First, | 3807 | First, |
@@ -3838,7 +3875,7 @@ the function returns. | |||
3838 | The string can contain any binary data, | 3875 | The string can contain any binary data, |
3839 | including @x{embedded zeros}. | 3876 | including @x{embedded zeros}. |
3840 | 3877 | ||
3841 | Returns a pointer to the internal copy of the string. | 3878 | Returns 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, | |||
3865 | so the memory at @id{s} can be freed or reused immediately after | 3902 | so the memory at @id{s} can be freed or reused immediately after |
3866 | the function returns. | 3903 | the function returns. |
3867 | 3904 | ||
3868 | Returns a pointer to the internal copy of the string. | 3905 | Returns a pointer to the internal copy of the string @see{constchar}. |
3869 | 3906 | ||
3870 | If @id{s} is @id{NULL}, pushes @nil and returns @id{NULL}. | 3907 | If @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 | ||
4282 | Marks the given index in the stack as a | 4319 | Marks the given index in the stack as a |
4283 | to-be-closed @Q{variable} @see{to-be-closed}. | 4320 | to-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}. | |||
4295 | This function should not be called for an index | 4332 | This function should not be called for an index |
4296 | that is equal to or below an active to-be-closed index. | 4333 | that is equal to or below an active to-be-closed index. |
4297 | 4334 | ||
4298 | This function can raise an out-of-memory error. | 4335 | In the case of an out-of-memory error, |
4299 | In that case, the value in the given index is immediately closed, | 4336 | the value in the given index is immediately closed, |
4300 | as if it was already marked. | 4337 | as if it was already marked. |
4301 | 4338 | ||
4339 | Note that, both in case of errors and of a regular return, | ||
4340 | by the time the @idx{__close} metamethod runs, | ||
4341 | the @N{C stack} was already unwound, | ||
4342 | so that any automatic C variable declared in the calling function | ||
4343 | will 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 | |||
4338 | when @id{lua_tolstring} is applied to keys during a table traversal.) | 4381 | when @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 |
4341 | to a string inside the Lua state. | 4384 | to a string inside the Lua state @see{constchar}. |
4342 | This string always has a zero (@Char{\0}) | 4385 | This string always has a zero (@Char{\0}) |
4343 | after its last character (as @N{in C}), | 4386 | after its last character (as @N{in C}), |
4344 | but can contain other zeros in its body. | 4387 | but can contain other zeros in its body. |
4345 | 4388 | ||
4346 | Because Lua has garbage collection, | ||
4347 | there is no guarantee that the pointer returned by @id{lua_tolstring} | ||
4348 | will 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}| |
4711 | the index on the stack of the first value being @Q{transferred}, | 4750 | the index in the stack of the first value being @Q{transferred}, |
4712 | that is, parameters in a call or return values in a return. | 4751 | that 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.) |
4714 | Using this index, you can access and modify these values | 4753 | Using this index, you can access and modify these values |
@@ -4860,7 +4899,7 @@ an identification of the @emph{activation record} | |||
4860 | of the function executing at a given level. | 4899 | of 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, |
4862 | whereas level @M{n+1} is the function that has called level @M{n} | 4901 | whereas 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). |
4864 | When called with a level greater than the stack depth, | 4903 | When called with a level greater than the stack depth, |
4865 | @Lid{lua_getstack} returns 0; | 4904 | @Lid{lua_getstack} returns 0; |
4866 | otherwise it returns 1. | 4905 | otherwise 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. |
4950 | The hook is called just after Lua enters the new function, | 4989 | The hook is called just after Lua enters the new function. |
4951 | before 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. | |||
5925 | Converts any Lua value at the given index to a @N{C string} | 5963 | Converts any Lua value at the given index to a @N{C string} |
5926 | in a reasonable format. | 5964 | in a reasonable format. |
5927 | The resulting string is pushed onto the stack and also | 5965 | The resulting string is pushed onto the stack and also |
5928 | returned by the function. | 5966 | returned by the function @see{constchar}. |
5929 | If @id{len} is not @id{NULL}, | 5967 | If @id{len} is not @id{NULL}, |
5930 | the function also sets @T{*len} with the string length. | 5968 | the 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 | |||
8608 | of the given thread: | 8646 | of 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); |
8612 | and so on. | 8650 | and so on. |
8613 | If @id{f} is a number greater than the number of active functions, | 8651 | If @id{f} is a number greater than the number of active functions, |
8614 | then @id{getinfo} returns @fail. | 8652 | then @id{getinfo} returns @fail. |