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