diff options
Diffstat (limited to 'manual/manual.of')
| -rw-r--r-- | manual/manual.of | 68 |
1 files changed, 38 insertions, 30 deletions
diff --git a/manual/manual.of b/manual/manual.of index 96203d7f..9b8e144d 100644 --- a/manual/manual.of +++ b/manual/manual.of | |||
| @@ -2221,7 +2221,7 @@ The form | |||
| 2221 | } | 2221 | } |
| 2222 | can be used to emulate methods. | 2222 | can be used to emulate methods. |
| 2223 | A call @T{v:name(@rep{args})} | 2223 | A call @T{v:name(@rep{args})} |
| 2224 | is syntactic sugar for @T{v.name(v,@rep{args})}, | 2224 | is syntactic sugar for @T{v.name(v, @rep{args})}, |
| 2225 | except that @id{v} is evaluated only once. | 2225 | except that @id{v} is evaluated only once. |
| 2226 | 2226 | ||
| 2227 | Arguments have the following syntax: | 2227 | Arguments have the following syntax: |
| @@ -2372,12 +2372,10 @@ which is indicated by three dots (@Char{...}) | |||
| 2372 | at the end of its parameter list. | 2372 | at the end of its parameter list. |
| 2373 | A variadic function does not adjust its argument list; | 2373 | A variadic function does not adjust its argument list; |
| 2374 | instead, it collects all extra arguments and supplies them | 2374 | instead, it collects all extra arguments and supplies them |
| 2375 | to the function through a @def{vararg expression} and, | 2375 | to the function through a @def{vararg table}. |
| 2376 | if present, a @def{vararg table}. | 2376 | In that table, |
| 2377 | 2377 | the values at indices 1, 2, etc. are the extra arguments, | |
| 2378 | A vararg expression is also written as three dots, | 2378 | and the value at index @St{n} is the number of extra arguments. |
| 2379 | and its value is a list of all actual extra arguments, | ||
| 2380 | similar to a function with multiple results @see{multires}. | ||
| 2381 | 2379 | ||
| 2382 | As an example, consider the following definitions: | 2380 | As an example, consider the following definitions: |
| 2383 | @verbatim{ | 2381 | @verbatim{ |
| @@ -2386,7 +2384,7 @@ function g(a, b, ...) end | |||
| 2386 | function r() return 1,2,3 end | 2384 | function r() return 1,2,3 end |
| 2387 | } | 2385 | } |
| 2388 | Then, we have the following mapping from arguments to parameters and | 2386 | Then, we have the following mapping from arguments to parameters and |
| 2389 | to the vararg expression: | 2387 | to the vararg table: |
| 2390 | @verbatim{ | 2388 | @verbatim{ |
| 2391 | CALL PARAMETERS | 2389 | CALL PARAMETERS |
| 2392 | 2390 | ||
| @@ -2396,33 +2394,39 @@ f(3, 4, 5) a=3, b=4 | |||
| 2396 | f(r(), 10) a=1, b=10 | 2394 | f(r(), 10) a=1, b=10 |
| 2397 | f(r()) a=1, b=2 | 2395 | f(r()) a=1, b=2 |
| 2398 | 2396 | ||
| 2399 | g(3) a=3, b=nil, ... -> (nothing) | 2397 | g(3) a=3, b=nil, va. table -> {n = 0} |
| 2400 | g(3, 4) a=3, b=4, ... -> (nothing) | 2398 | g(3, 4) a=3, b=4, va. table -> {n = 0} |
| 2401 | g(3, 4, 5, 8) a=3, b=4, ... -> 5 8 | 2399 | g(3, 4, 5, 8) a=3, b=4, va. table -> {5, 8, n = 2} |
| 2402 | g(5, r()) a=5, b=1, ... -> 2 3 | 2400 | g(5, r()) a=5, b=1, va. table -> {2, 3, n = 2} |
| 2403 | } | 2401 | } |
| 2404 | 2402 | ||
| 2405 | The presence of a vararg table in a variadic function is indicated | 2403 | A vararg table in a variadic function can have an optional name, |
| 2406 | by a name after the three dots. | 2404 | given after the three dots. |
| 2407 | When present, | 2405 | When present, |
| 2408 | a vararg table behaves like a read-only local variable | 2406 | that name denotes a read-only local variable that |
| 2409 | with the given name that is initialized with a table. | 2407 | refers to the vararg table. |
| 2410 | In that table, | 2408 | If the vararg table does not have a name, |
| 2411 | the values at indices 1, 2, etc. are the extra arguments, | 2409 | it can only be accessed through a vararg expression. |
| 2412 | and the value at index @St{n} is the number of extra arguments. | 2410 | |
| 2413 | In other words, the code behaves as if the function started with | 2411 | A vararg expression is also written as three dots, |
| 2414 | the following statement, | 2412 | and its value is a list of the values in the vararg table, |
| 2415 | assuming the standard behavior of @Lid{table.pack}: | 2413 | from 1 to the integer value at index @St{n}. |
| 2416 | @verbatim{ | 2414 | (Therefore, if the code does not modify the vararg table, |
| 2417 | local <const> name = table.pack(...) | 2415 | this list corresponds to the extra arguments in the function call.) |
| 2418 | } | 2416 | This list behaves like the results from a |
| 2417 | function with multiple results @see{multires}. | ||
| 2419 | 2418 | ||
| 2420 | As an optimization, | 2419 | As an optimization, |
| 2421 | if the vararg table is used only as the base table | 2420 | if the vararg table satisfies some conditions, |
| 2422 | in the syntactic constructions @T{t[exp]} or @T{t.id}) | ||
| 2423 | and it is not an upvalue, | ||
| 2424 | the code does not create an actual table and instead translates | 2421 | the code does not create an actual table and instead translates |
| 2425 | the indexing expressions into accesses to the internal vararg data. | 2422 | the indexing expressions and the vararg expressions |
| 2423 | into accesses to the internal vararg data. | ||
| 2424 | The conditions are as follows: | ||
| 2425 | If the vararg table has a name, | ||
| 2426 | that name is not an upvalue in a nested function | ||
| 2427 | and it is used only as the base table | ||
| 2428 | in the syntactic constructions @T{t[exp]} or @T{t.id}). | ||
| 2429 | Note that an anonymous vararg table always satisfy these conditions. | ||
| 2426 | 2430 | ||
| 2427 | } | 2431 | } |
| 2428 | 2432 | ||
| @@ -3103,7 +3107,7 @@ void *luaL_alloc (void *ud, void *ptr, size_t osize, | |||
| 3103 | } | 3107 | } |
| 3104 | Note that @N{ISO C} ensures | 3108 | Note that @N{ISO C} ensures |
| 3105 | that @T{free(NULL)} has no effect and that | 3109 | that @T{free(NULL)} has no effect and that |
| 3106 | @T{realloc(NULL,size)} is equivalent to @T{malloc(size)}. | 3110 | @T{realloc(NULL, size)} is equivalent to @T{malloc(size)}. |
| 3107 | 3111 | ||
| 3108 | } | 3112 | } |
| 3109 | 3113 | ||
| @@ -9197,6 +9201,10 @@ Compile-time constants may not appear in this listing, | |||
| 9197 | if they were optimized away by the compiler. | 9201 | if they were optimized away by the compiler. |
| 9198 | Negative indices refer to vararg arguments; | 9202 | Negative indices refer to vararg arguments; |
| 9199 | @num{-1} is the first vararg argument. | 9203 | @num{-1} is the first vararg argument. |
| 9204 | These negative indices are only available when the vararg table | ||
| 9205 | has been optimized away; | ||
| 9206 | otherwise, the vararg arguments are available in the vararg table. | ||
| 9207 | |||
| 9200 | The function returns @fail | 9208 | The function returns @fail |
| 9201 | if there is no variable with the given index, | 9209 | if there is no variable with the given index, |
| 9202 | and raises an error when called with a level out of range. | 9210 | and raises an error when called with a level out of range. |
