diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-01-04 13:09:47 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-01-04 13:09:47 -0200 |
commit | 4ace93ca6502dd1da38d5c06fa099d229e791ba8 (patch) | |
tree | 34f95ef56aecb56ae1db5e8b843b6e9bd2cbae51 /manual | |
parent | c6f7181e910b6b2ff1346b5486a31be87b1da5af (diff) | |
download | lua-4ace93ca6502dd1da38d5c06fa099d229e791ba8.tar.gz lua-4ace93ca6502dd1da38d5c06fa099d229e791ba8.tar.bz2 lua-4ace93ca6502dd1da38d5c06fa099d229e791ba8.zip |
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.
Diffstat (limited to 'manual')
-rw-r--r-- | manual/manual.of | 34 |
1 files changed, 14 insertions, 20 deletions
diff --git a/manual/manual.of b/manual/manual.of index d64f0f1a..b9ab1ebe 100644 --- a/manual/manual.of +++ b/manual/manual.of | |||
@@ -1536,33 +1536,29 @@ goes out of scope, including normal block termination, | |||
1536 | exiting its block by @Rw{break}/@Rw{goto}/@Rw{return}, | 1536 | exiting its block by @Rw{break}/@Rw{goto}/@Rw{return}, |
1537 | or exiting by an error. | 1537 | or exiting by an error. |
1538 | 1538 | ||
1539 | To \emph{close} a value has the following meaning here: | 1539 | Here, to \emph{close} a value means |
1540 | If the value of the variable when it goes out of scope is a function, | 1540 | to call its @idx{__close} metamethod. |
1541 | that function is called; | 1541 | If the value is @nil, it is ignored; |
1542 | otherwise, if the value has a @idx{__close} metamethod, | 1542 | otherwise, |
1543 | that metamethod is called; | 1543 | if it does not have a @idx{__close} metamethod, |
1544 | otherwise, if the value is @nil, nothing is done; | 1544 | an error is raised. |
1545 | otherwise, an error is raised. | 1545 | When calling the metamethod, |
1546 | In the function case, | 1546 | the value itself is passed as the first argument |
1547 | if the scope is being closed by an error, | 1547 | and the error object (if any) is passed as a second argument; |
1548 | the error object is passed as an argument to the function; | 1548 | if there was no error, the second argument is @nil. |
1549 | if there is no error, the function gets @nil. | ||
1550 | In the metamethod case, | ||
1551 | the value itself always is passed as an argument to the metamethod. | ||
1552 | 1549 | ||
1553 | If several to-be-closed variables go out of scope at the same event, | 1550 | If several to-be-closed variables go out of scope at the same event, |
1554 | they are closed in the reverse order that they were declared. | 1551 | they are closed in the reverse order that they were declared. |
1555 | If there is any error while running a closing function, | 1552 | If there is any error while running a closing method, |
1556 | that error is handled like an error in the regular code | 1553 | that error is handled like an error in the regular code |
1557 | where the variable was defined; | 1554 | where the variable was defined; |
1558 | in particular, | 1555 | in particular, |
1559 | the other pending closing functions will still be called. | 1556 | the other pending closing methods will still be called. |
1560 | 1557 | ||
1561 | If a coroutine yields inside a block and is never resumed again, | 1558 | If a coroutine yields inside a block and is never resumed again, |
1562 | the variables visible at that block will never go out of scope, | 1559 | the variables visible at that block will never go out of scope, |
1563 | and therefore they will not be closed. | 1560 | and therefore they will not be closed. |
1564 | Similarly, if a script is interrupted by an unprotected error, | 1561 | (You should use finalizers to handle this case.) |
1565 | its to-be-closed variables will not be closed. | ||
1566 | 1562 | ||
1567 | } | 1563 | } |
1568 | 1564 | ||
@@ -3002,7 +2998,7 @@ and therefore never returns | |||
3002 | } | 2998 | } |
3003 | 2999 | ||
3004 | @APIEntry{int lua_gc (lua_State *L, int what, int data);| | 3000 | @APIEntry{int lua_gc (lua_State *L, int what, int data);| |
3005 | @apii{0,0,v} | 3001 | @apii{0,0,-} |
3006 | 3002 | ||
3007 | Controls the garbage collector. | 3003 | Controls the garbage collector. |
3008 | 3004 | ||
@@ -3056,8 +3052,6 @@ returns a boolean that tells whether the collector is running | |||
3056 | For more details about these options, | 3052 | For more details about these options, |
3057 | see @Lid{collectgarbage}. | 3053 | see @Lid{collectgarbage}. |
3058 | 3054 | ||
3059 | This function may raise errors when calling finalizers. | ||
3060 | |||
3061 | } | 3055 | } |
3062 | 3056 | ||
3063 | @APIEntry{lua_Alloc lua_getallocf (lua_State *L, void **ud);| | 3057 | @APIEntry{lua_Alloc lua_getallocf (lua_State *L, void **ud);| |