aboutsummaryrefslogtreecommitdiff
path: root/manual/manual.of
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--manual/manual.of91
1 files changed, 61 insertions, 30 deletions
diff --git a/manual/manual.of b/manual/manual.of
index 3c704118..beea41f9 100644
--- a/manual/manual.of
+++ b/manual/manual.of
@@ -2262,7 +2262,7 @@ return x or f(x) -- results adjusted to 1
2262 2262
2263@sect3{func-def| @title{Function Definitions} 2263@sect3{func-def| @title{Function Definitions}
2264 2264
2265The syntax for function definition is 2265The syntax for a function definition is
2266@Produc{ 2266@Produc{
2267@producname{functiondef}@producbody{@Rw{function} funcbody} 2267@producname{functiondef}@producbody{@Rw{function} funcbody}
2268@producname{funcbody}@producbody{@bnfter{(} @bnfopt{parlist} @bnfter{)} block @Rw{end}} 2268@producname{funcbody}@producbody{@bnfter{(} @bnfopt{parlist} @bnfter{)} block @Rw{end}}
@@ -2315,6 +2315,18 @@ translates to
2315global f; f = function () @rep{body} end 2315global f; f = function () @rep{body} end
2316} 2316}
2317 2317
2318The @emphx{colon} syntax
2319is used to emulate @def{methods},
2320adding an implicit extra parameter @idx{self} to the function.
2321Thus, the statement
2322@verbatim{
2323function t.a.b.c:f (@rep{params}) @rep{body} end
2324}
2325is syntactic sugar for
2326@verbatim{
2327t.a.b.c.f = function (self, @rep{params}) @rep{body} end
2328}
2329
2318A function definition is an executable expression, 2330A function definition is an executable expression,
2319whose value has type @emph{function}. 2331whose value has type @emph{function}.
2320When Lua precompiles a chunk, 2332When Lua precompiles a chunk,
@@ -2325,11 +2337,25 @@ the function is @emph{instantiated} (or @emph{closed}).
2325This function instance, or @emphx{closure}, 2337This function instance, or @emphx{closure},
2326is the final value of the expression. 2338is the final value of the expression.
2327 2339
2340Results are returned using the @Rw{return} statement @see{control}.
2341If control reaches the end of a function
2342without encountering a @Rw{return} statement,
2343then the function returns with no results.
2344
2345@index{multiple return}
2346There is a system-dependent limit on the number of values
2347that a function may return.
2348This limit is guaranteed to be at least 1000.
2349
2350@sect4{@title{Parameters}
2351
2328Parameters act as local variables that are 2352Parameters act as local variables that are
2329initialized with the argument values: 2353initialized with the argument values:
2330@Produc{ 2354@Produc{
2331@producname{parlist}@producbody{namelist @bnfopt{@bnfter{,} @bnfter{...}} @Or 2355@producname{parlist}@producbody{namelist @bnfopt{@bnfter{,} varargparam} @Or
2332 @bnfter{...}} 2356 varargparam}
2357@producname{varargparam}@producbody{@bnfter{...}
2358 @bnfopt{@bnfter{|} @bnfNter{Name}}}
2333} 2359}
2334When a Lua function is called, 2360When a Lua function is called,
2335it adjusts its list of @x{arguments} to 2361it adjusts its list of @x{arguments} to
@@ -2339,11 +2365,12 @@ which is indicated by three dots (@Char{...})
2339at the end of its parameter list. 2365at the end of its parameter list.
2340A variadic function does not adjust its argument list; 2366A variadic function does not adjust its argument list;
2341instead, it collects all extra arguments and supplies them 2367instead, it collects all extra arguments and supplies them
2342to the function through a @def{vararg expression}, 2368to the function through a @def{vararg expression} and,
2343which is also written as three dots. 2369if present, a @def{vararg table}.
2344The value of this expression is a list of all actual extra arguments,
2345similar to a function with multiple results @see{multires}.
2346 2370
2371A vararg expression is also written as three dots,
2372and its value is a list of all actual extra arguments,
2373similar to a function with multiple results @see{multires}.
2347 2374
2348As an example, consider the following definitions: 2375As an example, consider the following definitions:
2349@verbatim{ 2376@verbatim{
@@ -2368,26 +2395,27 @@ g(3, 4, 5, 8) a=3, b=4, ... -> 5 8
2368g(5, r()) a=5, b=1, ... -> 2 3 2395g(5, r()) a=5, b=1, ... -> 2 3
2369} 2396}
2370 2397
2371Results are returned using the @Rw{return} statement @see{control}. 2398The presence of a varag table in a variadic function is indicated
2372If control reaches the end of a function 2399by the @T{|name} syntax after the three dots.
2373without encountering a @Rw{return} statement, 2400When present,
2374then the function returns with no results. 2401a vararg table behaves like a read-only local variable
2375 2402with the given name that is initialized with a table.
2376@index{multiple return} 2403In that table,
2377There is a system-dependent limit on the number of values 2404the values at indices 1, 2, etc. are the extra arguments,
2378that a function may return. 2405and the value at index @St{n} is the number of extra arguments.
2379This limit is guaranteed to be at least 1000. 2406In other words, the code behaves as if the function started with
2380 2407the following statement,
2381The @emphx{colon} syntax 2408assuming the standard behavior of @Lid{table.pack}:
2382is used to emulate @def{methods},
2383adding an implicit extra parameter @idx{self} to the function.
2384Thus, the statement
2385@verbatim{ 2409@verbatim{
2386function t.a.b.c:f (@rep{params}) @rep{body} end 2410local <const> name = table.pack(...)
2387} 2411}
2388is syntactic sugar for 2412
2389@verbatim{ 2413As an optimization,
2390t.a.b.c.f = function (self, @rep{params}) @rep{body} end 2414if the vararg table is used only as a base in indexing expressions
2415(the @T{t} in @T{t[exp]} or @T{t.id}) and it is not an upvalue,
2416the code does not create an actual table and instead translates
2417the indexing expressions into accesses to the internal vararg data.
2418
2391} 2419}
2392 2420
2393} 2421}
@@ -2422,7 +2450,7 @@ for instance @T{foo(e1, e2, e3)} @see{functioncall}.}
2422for instance @T{a , b, c = e1, e2, e3} @see{assignment}.} 2450for instance @T{a , b, c = e1, e2, e3} @see{assignment}.}
2423 2451
2424@item{A local or global declaration, 2452@item{A local or global declaration,
2425which is a special case of multiple assignment.} 2453which is similar to a multiple assignment.}
2426 2454
2427@item{The initial values in a generic @rw{for} loop, 2455@item{The initial values in a generic @rw{for} loop,
2428for instance @T{for k in e1, e2, e3 do ... end} @see{for}.} 2456for instance @T{for k in e1, e2, e3 do ... end} @see{for}.}
@@ -3016,7 +3044,7 @@ typedef void * (*lua_Alloc) (void *ud,
3016 size_t osize, 3044 size_t osize,
3017 size_t nsize);| 3045 size_t nsize);|
3018 3046
3019The type of the @x{memory-allocation function} used by Lua states. 3047The type of the @x{memory-allocator function} used by Lua states.
3020The allocator function must provide a 3048The allocator function must provide a
3021functionality similar to @id{realloc}, 3049functionality similar to @id{realloc},
3022but not exactly the same. 3050but not exactly the same.
@@ -3482,7 +3510,7 @@ This function should not be called by a finalizer.
3482@APIEntry{lua_Alloc lua_getallocf (lua_State *L, void **ud);| 3510@APIEntry{lua_Alloc lua_getallocf (lua_State *L, void **ud);|
3483@apii{0,0,-} 3511@apii{0,0,-}
3484 3512
3485Returns the @x{memory-allocation function} of a given state. 3513Returns the @x{memory-allocator function} of a given state.
3486If @id{ud} is not @id{NULL}, Lua stores in @T{*ud} the 3514If @id{ud} is not @id{NULL}, Lua stores in @T{*ud} the
3487opaque pointer given when the memory-allocator function was set. 3515opaque pointer given when the memory-allocator function was set.
3488 3516
@@ -9732,8 +9760,11 @@ and @bnfNter{LiteralString}, see @See{lexical}.)
9732 9760
9733@producname{funcbody}@producbody{@bnfter{(} @bnfopt{parlist} @bnfter{)} block @Rw{end}} 9761@producname{funcbody}@producbody{@bnfter{(} @bnfopt{parlist} @bnfter{)} block @Rw{end}}
9734 9762
9735@producname{parlist}@producbody{namelist @bnfopt{@bnfter{,} @bnfter{...}} 9763@producname{parlist}@producbody{namelist @bnfopt{@bnfter{,} varargparam} @Or
9736 @Or @bnfter{...}} 9764 varargparam}
9765
9766@producname{varargparam}@producbody{@bnfter{...}
9767 @bnfopt{@bnfter{|} @bnfNter{Name}}}
9737 9768
9738@producname{tableconstructor}@producbody{@bnfter{@Open} @bnfopt{fieldlist} @bnfter{@Close}} 9769@producname{tableconstructor}@producbody{@bnfter{@Open} @bnfopt{fieldlist} @bnfter{@Close}}
9739 9770