52
47
It also offers good support for object-oriented programming,
53
48
functional programming, and data-driven programming.
54
Lua is intended to be used as a powerful, light-weight
55
scripting language for any program that needs one.
49
Lua is intended to be used as a powerful, lightweight,
50
embeddable scripting language for any program that needs one.
56
51
Lua is implemented as a library, written in <em>clean C</em>,
57
52
the common subset of Standard C and C++.
86
81
For a discussion of the decisions behind the design of Lua,
87
82
see the technical papers available at Lua's web site.
88
83
For a detailed introduction to programming in Lua,
89
see Roberto's book, <em>Programming in Lua</em> (second edition).
84
see Roberto's book, <em>Programming in Lua</em>.
93
88
<h1>2 – <a name="2">Basic Concepts</a></h1>
96
This section describes some basic concepts of the language.
91
This section describes the basic concepts of the language.
149
144
The type <em>userdata</em> is provided to allow arbitrary C data to
150
145
be stored in Lua variables.
151
This type corresponds to a block of raw memory
152
and has no pre-defined operations in Lua,
146
A userdata value is a pointer to a block of raw memory.
147
There are two kinds of userdata:
148
full userdata, where the block of memory is managed by Lua,
149
and light userdata, where the block of memory is managed by the host.
150
Userdata has no predefined operations in Lua,
153
151
except assignment and identity test.
154
However, by using <em>metatables</em>,
155
the programmer can define operations for userdata values
152
By using <em>metatables</em>,
153
the programmer can define operations for full userdata values
156
154
(see <a href="#2.4">§2.4</a>).
157
155
Userdata values cannot be created or modified in Lua,
158
156
only through the C API.
239
237
As will be discussed in <a href="#3.2">§3.2</a> and <a href="#3.3.3">§3.3.3</a>,
240
238
any reference to a global name <code>var</code> is syntactically translated
241
239
to <code>_ENV.var</code>.
242
Moreover, every chunk is compiled in the scope of an external
243
variable called <code>_ENV</code> (see <a href="#3.3.2">§3.3.2</a>),
240
Moreover, every chunk is compiled in the scope of
241
an external local variable called <code>_ENV</code> (see <a href="#3.3.2">§3.3.2</a>),
244
242
so <code>_ENV</code> itself is never a global name in a chunk.
252
250
you can define new variables and parameters with that name.
253
251
Each reference to a global name uses the <code>_ENV</code> that is
254
252
visible at that point in the program,
255
following the usual visibility rules of Lua.
253
following the usual visibility rules of Lua (see <a href="#3.5">§3.5</a>).
259
Any table used as the value of <code>_ENV</code> is usually called
260
an <em>environment</em>.
257
Any table used as the value of <code>_ENV</code> is called an <em>environment</em>.
270
267
When Lua compiles a chunk,
271
it initializes the value of its <code>_ENV</code> variable
268
it initializes the value of its <code>_ENV</code> upvalue
272
269
with the global environment (see <a href="#pdf-load"><code>load</code></a>).
273
270
Therefore, by default,
274
271
global variables in Lua code refer to entries in the global environment.
275
Moreover, all standard libraries are loaded in the global environment,
272
Moreover, all standard libraries are loaded in the global environment
276
273
and several functions there operate on that environment.
277
You can use <a href="#pdf-load"><code>load</code></a> to load a chunk with a different environment.
274
You can use <a href="#pdf-load"><code>load</code></a> (or <a href="#pdf-loadfile"><code>loadfile</code></a>)
275
to load a chunk with a different environment.
278
276
(In C, you have to load the chunk and then change the value
279
277
of its first upvalue.)
390
388
A metatable controls how an object behaves in arithmetic operations,
391
389
order comparisons, concatenation, length operation, and indexing.
392
A metatable also can define a function to be called when a userdata
393
is garbage collected.
390
A metatable also can define a function to be called
391
when a userdata or a table is garbage collected.
394
392
When Lua performs one of these operations over a value,
395
393
it checks whether this value has a metatable with the corresponding event.
396
394
If so, the value associated with that key (the metamethod)
427
425
rawget(getmetatable(obj) or {}, event)
430
That is, the access to a metamethod does not invoke other metamethods,
431
and the access to objects with no metatables does not fail
428
This means that the access to a metamethod does not invoke other metamethods,
429
and access to objects with no metatables does not fail
432
430
(it simply results in <b>nil</b>).
591
589
A metamethod is selected only when both values
592
590
being compared have the same type
593
and the same metamethod for the selected operation.
591
and the same metamethod for the selected operation,
592
and the values are either tables or full userdata.
596
function getequalhandler (op1, op2, event)
597
if type(op1) ~= type(op2) then return nil end
598
local mm1 = metatable(op1)[event]
599
local mm2 = metatable(op2)[event]
595
function getequalhandler (op1, op2)
596
if type(op1) ~= type(op2) or
597
(type(op1) ~= "table" and type(op1) ~= "userdata") then
598
return nil -- different values
600
local mm1 = metatable(op1).__eq
601
local mm2 = metatable(op2).__eq
600
602
if mm1 == mm2 then return mm1 else return nil end
606
608
function eq_event (op1, op2)
607
if type(op1) ~= type(op2) then -- different types?
608
return false -- different values
610
609
if op1 == op2 then -- primitive equal?
611
610
return true -- values are equal
613
612
-- try metamethod
614
local h = getequalhandler(op1, op2, "__eq")
613
local h = getequalhandler(op1, op2)
615
return not not h(op1, op2)
621
Note that the result is always a boolean.
624
624
<li><b>"lt": </b>
770
776
a <em>garbage collector</em> to collect all <em>dead objects</em>
771
777
(that is, objects that are no longer accessible from Lua).
772
778
All memory used by Lua is subject to automatic management:
773
tables, userdata, functions, threads, strings, etc.
779
strings, tables, userdata, functions, threads, internal structures, etc.
820
826
You can change these numbers by calling <a href="#lua_gc"><code>lua_gc</code></a> in C
821
827
or <a href="#pdf-collectgarbage"><code>collectgarbage</code></a> in Lua.
822
With these functions you can also control
828
You can also use these functions to control
823
829
the collector directly (e.g., stop and restart it).
833
As an experimental feature in Lua 5.2,
834
you can change the collector's operation mode
835
from incremental to <em>generational</em>.
836
A <em>generational collector</em> assumes that most objects die young,
837
and therefore it traverses only young (recently created) objects.
838
This behavior can reduce the time used by the collector,
839
but also increases memory usage (as old dead objects may accumulate).
840
To mitigate this second problem,
841
from time to time the generational collector performs a full collection.
842
Remember that this is an experimental feature;
843
you are welcome to try it,
844
but check your gains.
827
848
<h3>2.5.1 – <a name="2.5.1">Garbage-Collection Metamethods</a></h3>
882
Because the object being collected must still be used by the finalizer
883
and even <em>resurrected</em>
884
(e.g., stored by the finalizer in a global variable),
885
the object memory is freed only when it becomes completely inaccessible
886
(that is, in the next garbage-collection cycle unless it was resurrected).
903
Because the object being collected must still be used by the finalizer,
904
it (and other objects accessible only through it)
905
must be <em>resurrected</em> by Lua.
906
Usually, this resurrection is transient,
907
and the object memory is freed in the next garbage-collection cycle.
908
However, if the finalizer stores the object in some global place
909
(e.g., a global variable),
910
then there is a permanent resurrection.
912
the object memory is freed only when it becomes completely inaccessible;
913
its finalizer will never be called twice.
930
After you use a table as a metatable,
931
you should not change the value of its <code>__mode</code> field.
932
Otherwise, the weak behavior of the tables controlled by this
933
metatable is undefined.
957
Any change in the weakness of a table may take effect only
958
at the next collect cycle.
959
In particular, if you change the weakness to a stronger mode,
960
Lua may still collect some items from that table
961
before the change takes effect.
937
965
Only objects that have an explicit construction
938
can be removed from weak tables.
939
Values, such as numbers and booleans,
966
are removed from weak tables.
967
Values, such as numbers and light C functions,
940
968
are not subject to garbage collection,
941
969
and therefore are not removed from weak tables
942
970
(unless its associated value is collected).
943
Lua treats strings and light C functions as non-object values.
971
Although strings are subject to garbage collection,
972
they do not have an explicit construction,
973
and therefore are not removed from weak tables.
947
Objects marked for finalization have a special behavior in weak tables.
948
When a marked object is a value in a weak table,
949
it is removed from the table before running its finalizer.
950
However, when it is a key,
951
it is removed from the table only after running its finalizer.
978
(that is, objects being finalized
979
and objects accessible only through objects being finalized)
980
have a special behavior in weak tables.
981
They are removed from weak values before running their finalizers,
982
but are removed from weak keys only in the next collection
983
after running their finalizers, when such objects are actually freed.
952
984
This behavior allows the finalizer to access properties
953
985
associated with the object through weak tables.
989
If a weak table is among the resurrected objects in a collection cycle,
990
it may not be properly cleared until the next cycle.
1408
1444
A chunk can be stored in a file or in a string inside the host program.
1409
1445
To execute a chunk,
1410
Lua first pre-compiles the chunk into instructions for a virtual machine,
1446
Lua first precompiles the chunk into instructions for a virtual machine,
1411
1447
and then it executes the compiled code
1412
1448
with an interpreter for the virtual machine.
1416
Chunks can also be pre-compiled into binary form;
1452
Chunks can also be precompiled into binary form;
1417
1453
see program <code>luac</code> for details.
1418
1454
Programs in source and compiled forms are interchangeable;
1419
1455
Lua automatically detects the file type and acts accordingly.
1808
1844
Both function calls and vararg expressions can result in multiple values.
1809
If an expression is used as a statement
1810
(only possible for function calls (see <a href="#3.3.6">§3.3.6</a>)),
1845
If a function call is used as a statement (see <a href="#3.3.6">§3.3.6</a>),
1811
1846
then its return list is adjusted to zero elements,
1812
1847
thus discarding all returned values.
1813
1848
If an expression is used as the last (or the only) element
1814
1849
of a list of expressions,
1815
1850
then no adjustment is made
1816
(unless the call is enclosed in parentheses).
1851
(unless the expression is enclosed in parentheses).
1817
1852
In all other contexts,
1818
1853
Lua adjusts the result list to one element,
1819
1854
discarding all values except the first one.
1881
1916
string and number values at run time.
1882
1917
Any arithmetic operation applied to a string tries to convert
1883
1918
this string to a number, following the rules of the Lua lexer.
1884
(The string may have leading and trailing spaces,
1885
plus an optional sign.)
1919
(The string may have leading and trailing spaces and a sign.)
1886
1920
Conversely, whenever a number is used where a string is expected,
1887
1921
the number is converted to a string, in a reasonable format.
1888
1922
For complete control over how numbers are converted to strings,
2255
2289
A function definition is an executable expression,
2256
2290
whose value has type <em>function</em>.
2257
When Lua pre-compiles a chunk,
2258
all its function bodies are pre-compiled too.
2291
When Lua precompiles a chunk,
2292
all its function bodies are precompiled too.
2259
2293
Then, whenever Lua executes the function definition,
2260
2294
the function is <em>instantiated</em> (or <em>closed</em>).
2261
2295
This function instance (or <em>closure</em>)
2272
2306
When a function is called,
2273
2307
the list of arguments is adjusted to
2274
2308
the length of the list of parameters,
2275
unless the function is a variadic or <em>vararg function</em>,
2277
indicated by three dots ('<code>...</code>') at the end of its parameter list.
2309
unless the function is a <em>vararg function</em>,
2310
which is indicated by three dots ('<code>...</code>')
2311
at the end of its parameter list.
2278
2312
A vararg function does not adjust its argument list;
2279
2313
instead, it collects all extra arguments and supplies them
2280
2314
to the function through a <em>vararg expression</em>,
2426
2460
Even when we use the term "function",
2427
2461
any facility in the API may be provided as a macro instead.
2428
All such macros use each of their arguments exactly once
2462
Except where stated otherwise,
2463
all such macros use each of their arguments exactly once
2429
2464
(except for the first argument, which is always a Lua state),
2430
2465
and so do not generate any hidden side-effects.
2434
2469
As in most C libraries,
2435
2470
the Lua API functions do not check their arguments for validity or consistency.
2436
2471
However, you can change this behavior by compiling Lua
2437
with a proper definition for the macro <a name="pdf-luai_apicheck"><code>luai_apicheck</code></a>,
2438
in file <code>luaconf.h</code>.
2472
with the macro <a name="pdf-LUA_USE_APICHECK"><code>LUA_USE_APICHECK</code></a> defined.
2509
2539
you should use <a href="#lua_checkstack"><code>lua_checkstack</code></a>.
2513
Most query functions accept as indices any value inside the
2514
available stack space, that is, indices up to the maximum stack size
2515
that you have set through <a href="#lua_checkstack"><code>lua_checkstack</code></a>.
2516
Such indices are called <em>acceptable indices</em>.
2517
More formally, we define an <em>acceptable index</em>
2545
<h2>4.3 – <a name="4.3">Valid and Acceptable Indices</a></h2>
2548
Any function in the API that receives stack indices
2549
works only with <em>valid indices</em> or <em>acceptable indices</em>.
2553
A <em>valid index</em> is an index that refers to a
2554
valid position within the stack, that is,
2555
it lies between 1 and the stack top
2556
(<code>1 ≤ abs(index) ≤ top</code>).
2558
Usually, functions that need a specific stack position
2559
(e.g., <a href="#lua_remove"><code>lua_remove</code></a>) require valid indices.
2563
Functions that do not need a specific stack position,
2564
but only a value in the stack (e.g., query functions),
2565
can be called with acceptable indices.
2566
An <em>acceptable index</em> refers to a position within
2567
the space allocated for the stack,
2568
that is, indices up to the stack size.
2569
More formally, we define an acceptable index
2521
2573
(index < 0 && abs(index) <= top) ||
2522
(index > 0 && index <= stackspace)
2574
(index > 0 && index <= stack size)
2524
Note that 0 is never an acceptable index.
2530
<h2>4.3 – <a name="4.3">Pseudo-Indices</a></h2>
2576
(Note that 0 is never an acceptable index.)
2577
When a function is called,
2578
its stack size is <code>top + LUA_MINSTACK</code>.
2579
You can change its stack size through function <a href="#lua_checkstack"><code>lua_checkstack</code></a>.
2583
Acceptable indices serve to avoid extra tests
2584
against the stack top when querying the stack.
2585
For instance, a C function can query its third argument
2586
without the need to first check whether there is a third argument,
2587
that is, without the need to check whether 3 is a valid index.
2591
For functions that can be called with acceptable indices,
2592
any non-valid index is treated as if it
2593
contains a value of a virtual type <a name="pdf-LUA_TNONE"><code>LUA_TNONE</code></a>.
2533
2597
Unless otherwise noted,
2534
2598
any function that accepts valid indices also accepts <em>pseudo-indices</em>,
2535
2599
which represent some Lua values that are accessible to C code
2536
2600
but which are not in the stack.
2537
Pseudo-indices are used to access
2601
Pseudo-indices are used to access the registry
2539
2602
and the upvalues of a C function (see <a href="#4.4">§4.4</a>).
2548
2611
When a C function is created,
2549
2612
it is possible to associate some values with it,
2550
thus creating a <em>C closure</em>;
2613
thus creating a <em>C closure</em>
2614
(see <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a>);
2551
2615
these values are called <em>upvalues</em> and are
2552
accessible to the function whenever it is called
2553
(see <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a>).
2616
accessible to the function whenever it is called.
2575
2638
Lua provides a <em>registry</em>,
2576
a pre-defined table that can be used by any C code to
2577
store whatever Lua value it needs to store.
2578
This table is always located at pseudo-index
2639
a predefined table that can be used by any C code to
2640
store whatever Lua values it needs to store.
2641
The registry table is always located at pseudo-index
2579
2642
<a name="pdf-LUA_REGISTRYINDEX"><code>LUA_REGISTRYINDEX</code></a>.
2580
2643
Any C library can store data into this table,
2581
but it should take care to choose keys different from those used
2644
but it should take care to choose keys
2645
that are different from those used
2582
2646
by other libraries, to avoid collisions.
2583
2647
Typically, you should use as key a string containing your library name,
2584
2648
or a light userdata with the address of a C object in your code,
2749
2812
The third field, <code>x</code>,
2750
2813
tells whether the function may throw errors:
2751
2814
'<code>-</code>' means the function never throws any error;
2752
'<code>m</code>' means the function may throw an error
2753
only due to not enough memory;
2815
'<code>m</code>' means the function may throw only memory allocation errors;
2754
2816
'<code>e</code>' means the function may throw other kinds of errors;
2755
2817
'<code>v</code>' means the function may throw an error on purpose.
2759
2821
<hr><h3><a name="lua_absindex"><code>lua_absindex</code></a></h3><p>
2760
<span class="apii">[-0, +0, <em>-</em>]</span>
2822
<span class="apii">[-0, +0, –]</span>
2761
2823
<pre>int lua_absindex (lua_State *L, int idx);</pre>
2797
2859
When <code>ptr</code> is <code>NULL</code>,
2798
<code>osize</code> codes the kind of object that Lua is allocating.
2860
<code>osize</code> encodes the kind of object that Lua is allocating.
2799
2861
<code>osize</code> is any of
2800
2862
<a href="#pdf-LUA_TSTRING"><code>LUA_TSTRING</code></a>, <a href="#pdf-LUA_TTABLE"><code>LUA_TTABLE</code></a>, <a href="#pdf-LUA_TFUNCTION"><code>LUA_TFUNCTION</code></a>,
2801
2863
<a href="#pdf-LUA_TUSERDATA"><code>LUA_TUSERDATA</code></a>, or <a href="#pdf-LUA_TTHREAD"><code>LUA_TTHREAD</code></a> when (and only when)
2843
2905
that <code>free(NULL)</code> has no effect and that
2844
2906
<code>realloc(NULL, size)</code> is equivalent to <code>malloc(size)</code>.
2845
2907
This code assumes that <code>realloc</code> does not fail when shrinking a block.
2846
Standard C does not ensure this behavior,
2847
but it seems a safe assumption.
2908
(Although Standard C does not ensure this behavior,
2909
it seems to be a safe assumption.)
3027
3089
<hr><h3><a name="lua_checkstack"><code>lua_checkstack</code></a></h3><p>
3028
<span class="apii">[-0, +0, <em>-</em>]</span>
3090
<span class="apii">[-0, +0, –]</span>
3029
3091
<pre>int lua_checkstack (lua_State *L, int extra);</pre>
3032
3094
Ensures that there are at least <code>extra</code> free stack slots in the stack.
3033
It returns false if it cannot grant the request,
3095
It returns false if it cannot fulfill the request,
3034
3096
because it would cause the stack to be larger than a fixed maximum size
3035
3097
(typically at least a few thousand elements) or
3036
3098
because it cannot allocate memory for the new stack size.
3052
3114
and frees all dynamic memory used by this state.
3053
3115
On several platforms, you may not need to call this function,
3054
3116
because all resources are naturally released when the host program ends.
3055
On the other hand, long-running programs,
3056
such as a daemon or a web server,
3057
might need to release states as soon as they are not needed,
3058
to avoid growing too large.
3117
On the other hand, long-running programs that create multiple states,
3118
such as daemons or web servers,
3119
might need to close states as soon as they are not needed.
3245
3307
(i.e., not stopped).
3310
<li><b><code>LUA_GCGEN</code>: </b>
3311
changes the collector to generational mode
3312
(see <a href="#2.5">§2.5</a>).
3315
<li><b><code>LUA_GCINC</code>: </b>
3316
changes the collector to incremental mode.
3317
This is the default mode.
3251
For more details about some options,
3323
For more details about these options,
3252
3324
see <a href="#pdf-collectgarbage"><code>collectgarbage</code></a>.
3293
3365
Lua may also call its continuation function
3294
3366
to handle errors during the call.
3295
3367
That is, upon an error in the function called by <a href="#lua_pcallk"><code>lua_pcallk</code></a>,
3296
Lua may not return <a href="#lua_pcallk"><code>lua_pcallk</code></a>
3368
Lua may not return to the original function
3297
3369
but instead may call the continuation function.
3298
3370
In that case, a call to <a href="#lua_getctx"><code>lua_getctx</code></a> will return the error code
3299
3371
(the value that would be returned by <a href="#lua_pcallk"><code>lua_pcallk</code></a>);
3326
3398
Pushes onto the stack the value of the global <code>name</code>.
3327
It is defined as a macro.
3333
3404
<hr><h3><a name="lua_getmetatable"><code>lua_getmetatable</code></a></h3><p>
3334
<span class="apii">[-0, +(0|1), <em>-</em>]</span>
3405
<span class="apii">[-0, +(0|1), –]</span>
3335
3406
<pre>int lua_getmetatable (lua_State *L, int index);</pre>
3338
3409
Pushes onto the stack the metatable of the value at the given
3339
3410
acceptable index.
3340
If the index is not valid,
3341
or if the value does not have a metatable,
3411
If the value does not have a metatable,
3342
3412
the function returns 0 and pushes nothing on the stack.
3425
3495
<hr><h3><a name="lua_isboolean"><code>lua_isboolean</code></a></h3><p>
3426
<span class="apii">[-0, +0, <em>-</em>]</span>
3496
<span class="apii">[-0, +0, –]</span>
3427
3497
<pre>int lua_isboolean (lua_State *L, int index);</pre>
3430
Returns 1 if the value at the given acceptable index has type boolean,
3500
Returns 1 if the value at the given acceptable index is a boolean,
3431
3501
and 0 otherwise.
3587
3657
<hr><h3><a name="lua_load"><code>lua_load</code></a></h3><p>
3588
<span class="apii">[-0, +1, <em>-</em>]</span>
3658
<span class="apii">[-0, +1, –]</span>
3589
3659
<pre>int lua_load (lua_State *L,
3590
3660
lua_Reader reader,
3592
const char *source);</pre>
3663
const char *mode);</pre>
3666
Loads a Lua chunk (without running it).
3596
3667
If there are no errors,
3597
<a href="#lua_load"><code>lua_load</code></a> pushes the compiled chunk as a Lua
3668
<code>lua_load</code> pushes the compiled chunk as a Lua
3598
3669
function on top of the stack.
3599
3670
Otherwise, it pushes an error message.
3600
The return values of <a href="#lua_load"><code>lua_load</code></a> are:
3674
The return values of <code>lua_load</code> are:
3604
<li><b><a href="#pdf-LUA_OK"><code>LUA_OK</code></a> (0): </b> no errors;</li>
3678
<li><b><a href="#pdf-LUA_OK"><code>LUA_OK</code></a>: </b> no errors;</li>
3606
3680
<li><b><a name="pdf-LUA_ERRSYNTAX"><code>LUA_ERRSYNTAX</code></a>: </b>
3607
syntax error during pre-compilation;</li>
3681
syntax error during precompilation;</li>
3609
3683
<li><b><a href="#pdf-LUA_ERRMEM"><code>LUA_ERRMEM</code></a>: </b>
3610
memory allocation error.</li>
3684
memory allocation error;</li>
3612
3686
<li><b><a href="#pdf-LUA_ERRGCMM"><code>LUA_ERRGCMM</code></a>: </b>
3613
3687
error while running a <code>__gc</code> metamethod.
3621
This function only loads a chunk;
3626
<a href="#lua_load"><code>lua_load</code></a> automatically detects whether the chunk is text or binary,
3627
and loads it accordingly (see program <code>luac</code>).
3631
The <a href="#lua_load"><code>lua_load</code></a> function uses a user-supplied <code>reader</code> function
3695
The <code>lua_load</code> function uses a user-supplied <code>reader</code> function
3632
3696
to read the chunk (see <a href="#lua_Reader"><code>lua_Reader</code></a>).
3633
3697
The <code>data</code> argument is an opaque value passed to the reader function.
3706
<code>lua_load</code> automatically detects whether the chunk is text or binary
3707
and loads it accordingly (see program <code>luac</code>).
3708
The string <code>mode</code> works as in function <a href="#pdf-load"><code>load</code></a>,
3709
with the addition that
3710
a <code>NULL</code> value is equivalent to the string "<code>bt</code>".
3642
3714
If the resulting function has one upvalue,
3643
3715
this upvalue is set to the value of the global environment
3644
3716
stored at index <code>LUA_RIDX_GLOBALS</code> in the registry (see <a href="#4.5">§4.5</a>).
3645
(When loading main chunks,
3646
this upvalue will be the <code>_ENV</code> variable (see <a href="#2.2">§2.2</a>).)
3717
When loading main chunks,
3718
this upvalue will be the <code>_ENV</code> variable (see <a href="#2.2">§2.2</a>).
3652
3724
<hr><h3><a name="lua_newstate"><code>lua_newstate</code></a></h3><p>
3653
<span class="apii">[-0, +0, <em>-</em>]</span>
3725
<span class="apii">[-0, +0, –]</span>
3654
3726
<pre>lua_State *lua_newstate (lua_Alloc f, void *ud);</pre>
3657
3729
Creates a new thread running in a new, independent state.
3658
Returns <code>NULL</code> if cannot create the thread/state
3730
Returns <code>NULL</code> if cannot create the thread or the state
3659
3731
(due to lack of memory).
3660
3732
The argument <code>f</code> is the allocator function;
3661
3733
Lua does all memory allocation for this state through this function.
3662
3734
The second argument, <code>ud</code>, is an opaque pointer that Lua
3663
simply passes to the allocator in every call.
3735
passes to the allocator in every call.
3707
3779
This function allocates a new block of memory with the given size,
3708
3780
pushes onto the stack a new full userdata with the block address,
3709
3781
and returns this address.
3713
Userdata represent C values in Lua.
3714
A <em>full userdata</em> represents a block of memory.
3715
It is an object (like a table):
3716
you must create it, it can have its own metatable,
3717
and you can detect when it is being collected.
3718
A full userdata is only equal to itself (under raw equality).
3782
The host program can freely use this memory.
3857
3921
<hr><h3><a name="lua_pcallk"><code>lua_pcallk</code></a></h3><p>
3858
<span class="apii">[-(nargs + 1), +(nresults|1), <em>-</em>]</span>
3859
<pre>int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
3860
int ctx, lua_CFunction k);</pre>
3922
<span class="apii">[-(nargs + 1), +(nresults|1), –]</span>
3923
<pre>int lua_pcallk (lua_State *L,
3928
lua_CFunction k);</pre>
3863
3931
This function behaves exactly like <a href="#lua_pcall"><code>lua_pcall</code></a>,
3920
3988
When <code>n</code> is zero,
3921
3989
this function creates a <em>light C function</em>,
3922
3990
which is just a pointer to the C function.
3923
In that case, it cannot throw a memory error.
3991
In that case, it never throws a memory error.
3929
3997
<hr><h3><a name="lua_pushcfunction"><code>lua_pushcfunction</code></a></h3><p>
3930
<span class="apii">[-0, +1, <em>-</em>]</span>
3998
<span class="apii">[-0, +1, –]</span>
3931
3999
<pre>void lua_pushcfunction (lua_State *L, lua_CFunction f);</pre>
4251
<hr><h3><a name="lua_rawgetp"><code>lua_rawgetp</code></a></h3><p>
4252
<span class="apii">[-0, +1, –]</span>
4253
<pre>void lua_rawgetp (lua_State *L, int index, const void *p);</pre>
4256
Pushes onto the stack the value <code>t[k]</code>,
4257
where <code>t</code> is the table at the given valid index and
4258
<code>k</code> is the pointer <code>p</code> represented as a light userdata.
4260
that is, it does not invoke metamethods.
4181
4266
<hr><h3><a name="lua_rawlen"><code>lua_rawlen</code></a></h3><p>
4182
<span class="apii">[-0, +0, <em>-</em>]</span>
4267
<span class="apii">[-0, +0, –]</span>
4183
4268
<pre>size_t lua_rawlen (lua_State *L, int index);</pre>
4314
<hr><h3><a name="lua_rawsetp"><code>lua_rawsetp</code></a></h3><p>
4315
<span class="apii">[-1, +0, <em>m</em>]</span>
4316
<pre>void lua_rawsetp (lua_State *L, int index, const void *p);</pre>
4319
Does the equivalent of <code>t[k] = v</code>,
4320
where <code>t</code> is the table at the given valid index,
4321
<code>k</code> is the pointer <code>p</code> represented as a light userdata,
4322
and <code>v</code> is the value at the top of the stack.
4326
This function pops the value from the stack.
4327
The assignment is raw;
4328
that is, it does not invoke metamethods.
4229
4334
<hr><h3><a name="lua_Reader"><code>lua_Reader</code></a></h3>
4230
4335
<pre>typedef const char * (*lua_Reader) (lua_State *L,
4297
4400
<hr><h3><a name="lua_resume"><code>lua_resume</code></a></h3><p>
4298
<span class="apii">[-?, +?, <em>-</em>]</span>
4299
<pre>int lua_resume (lua_State *L, int narg);</pre>
4401
<span class="apii">[-?, +?, –]</span>
4402
<pre>int lua_resume (lua_State *L, lua_State *from, int narg);</pre>
4302
4405
Starts and resumes a coroutine in a given thread.
4330
4433
and then call <a href="#lua_resume"><code>lua_resume</code></a>.
4437
The parameter <code>from</code> represents the coroutine that is resuming <code>L</code>.
4438
If there is no such coroutine,
4439
this parameter can be <code>NULL</code>.
4336
4445
<hr><h3><a name="lua_setallocf"><code>lua_setallocf</code></a></h3><p>
4337
<span class="apii">[-0, +0, <em>-</em>]</span>
4446
<span class="apii">[-0, +0, –]</span>
4338
4447
<pre>void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);</pre>
4372
4481
Pops a value from the stack and
4373
4482
sets it as the new value of global <code>name</code>.
4374
It is defined as a macro.
4380
4488
<hr><h3><a name="lua_setmetatable"><code>lua_setmetatable</code></a></h3><p>
4381
<span class="apii">[-1, +0, <em>-</em>]</span>
4489
<span class="apii">[-1, +0, –]</span>
4382
4490
<pre>void lua_setmetatable (lua_State *L, int index);</pre>
4516
4624
<hr><h3><a name="lua_tointeger"><code>lua_tointeger</code></a></h3><p>
4517
<span class="apii">[-0, +0, <em>-</em>]</span>
4625
<span class="apii">[-0, +0, –]</span>
4518
4626
<pre>lua_Integer lua_tointeger (lua_State *L, int index);</pre>
4521
A macro equivalent to <code>lua_tointegerx(L, index, NULL)</code>.
4629
Equivalent to <a href="#lua_tointegerx"><code>lua_tointegerx</code></a> with <code>isnum</code> equal to <code>NULL</code>.
4527
4635
<hr><h3><a name="lua_tointegerx"><code>lua_tointegerx</code></a></h3><p>
4528
<span class="apii">[-0, +0, <em>-</em>]</span>
4636
<span class="apii">[-0, +0, –]</span>
4529
4637
<pre>lua_Integer lua_tointegerx (lua_State *L, int index, int *isnum);</pre>
4561
4669
The Lua value must be a string or a number;
4562
4670
otherwise, the function returns <code>NULL</code>.
4563
4671
If the value is a number,
4564
then <a href="#lua_tolstring"><code>lua_tolstring</code></a> also
4672
then <code>lua_tolstring</code> also
4565
4673
<em>changes the actual value in the stack to a string</em>.
4566
4674
(This change confuses <a href="#lua_next"><code>lua_next</code></a>
4567
when <a href="#lua_tolstring"><code>lua_tolstring</code></a> is applied to keys during a table traversal.)
4675
when <code>lua_tolstring</code> is applied to keys during a table traversal.)
4571
<a href="#lua_tolstring"><code>lua_tolstring</code></a> returns a fully aligned pointer
4679
<code>lua_tolstring</code> returns a fully aligned pointer
4572
4680
to a string inside the Lua state.
4573
4681
This string always has a zero ('<code>\0</code>')
4574
4682
after its last character (as in C),
4575
4683
but can contain other zeros in its body.
4576
4684
Because Lua has garbage collection,
4577
there is no guarantee that the pointer returned by <a href="#lua_tolstring"><code>lua_tolstring</code></a>
4685
there is no guarantee that the pointer returned by <code>lua_tolstring</code>
4578
4686
will be valid after the corresponding value is removed from the stack.
4584
4692
<hr><h3><a name="lua_tonumber"><code>lua_tonumber</code></a></h3><p>
4585
<span class="apii">[-0, +0, <em>-</em>]</span>
4693
<span class="apii">[-0, +0, –]</span>
4586
4694
<pre>lua_Number lua_tonumber (lua_State *L, int index);</pre>
4589
A macro equivalent to <code>lua_tonumberx(L, index, NULL)</code>.
4697
Equivalent to <a href="#lua_tonumberx"><code>lua_tonumberx</code></a> with <code>isnum</code> equal to <code>NULL</code>.
4595
4703
<hr><h3><a name="lua_tonumberx"><code>lua_tonumberx</code></a></h3><p>
4596
<span class="apii">[-0, +0, <em>-</em>]</span>
4704
<span class="apii">[-0, +0, –]</span>
4597
4705
<pre>lua_Number lua_tonumberx (lua_State *L, int index, int *isnum);</pre>
4616
4724
<hr><h3><a name="lua_topointer"><code>lua_topointer</code></a></h3><p>
4617
<span class="apii">[-0, +0, <em>-</em>]</span>
4725
<span class="apii">[-0, +0, –]</span>
4618
4726
<pre>const void *lua_topointer (lua_State *L, int index);</pre>
4621
4729
Converts the value at the given acceptable index to a generic
4622
4730
C pointer (<code>void*</code>).
4623
4731
The value can be a userdata, a table, a thread, or a function;
4624
otherwise, <a href="#lua_topointer"><code>lua_topointer</code></a> returns <code>NULL</code>.
4732
otherwise, <code>lua_topointer</code> returns <code>NULL</code>.
4625
4733
Different objects will give different pointers.
4626
4734
There is no way to convert the pointer back to its original value.
4661
4769
<hr><h3><a name="lua_tounsigned"><code>lua_tounsigned</code></a></h3><p>
4662
<span class="apii">[-0, +0, <em>-</em>]</span>
4770
<span class="apii">[-0, +0, –]</span>
4663
4771
<pre>lua_Unsigned lua_tounsigned (lua_State *L, int index);</pre>
4666
A macro equivalent to <code>lua_tounsignedx(L, index, NULL)</code>.
4774
Equivalent to <a href="#lua_tounsignedx"><code>lua_tounsignedx</code></a> with <code>isnum</code> equal to <code>NULL</code>.
4672
4780
<hr><h3><a name="lua_tounsignedx"><code>lua_tounsignedx</code></a></h3><p>
4673
<span class="apii">[-0, +0, <em>-</em>]</span>
4781
<span class="apii">[-0, +0, –]</span>
4674
4782
<pre>lua_Unsigned lua_tounsignedx (lua_State *L, int index, int *isnum);</pre>
4716
4824
<hr><h3><a name="lua_type"><code>lua_type</code></a></h3><p>
4717
<span class="apii">[-0, +0, <em>-</em>]</span>
4825
<span class="apii">[-0, +0, –]</span>
4718
4826
<pre>int lua_type (lua_State *L, int index);</pre>
4721
4829
Returns the type of the value in the given acceptable index,
4722
or <code>LUA_TNONE</code> for a non-valid index
4723
(that is, an index to an "empty" stack position).
4830
or <code>LUA_TNONE</code> for a non-valid index.
4724
4831
The types returned by <a href="#lua_type"><code>lua_type</code></a> are coded by the following constants
4725
4832
defined in <code>lua.h</code>:
4726
4833
<a name="pdf-LUA_TNIL"><code>LUA_TNIL</code></a>,
4809
4916
<hr><h3><a name="lua_xmove"><code>lua_xmove</code></a></h3><p>
4810
<span class="apii">[-?, +?, <em>-</em>]</span>
4917
<span class="apii">[-?, +?, –]</span>
4811
4918
<pre>void lua_xmove (lua_State *from, lua_State *to, int n);</pre>
4814
Exchange values between different threads of the same global state.
4921
Exchange values between different threads of the same state.
4850
4957
return expression of a C function, as follows:
4853
return lua_yield (L, nresults, i, k);
4960
return lua_yieldk (L, n, i, k);
4855
4962
When a C function calls <a href="#lua_yieldk"><code>lua_yieldk</code></a> in that way,
4856
4963
the running coroutine suspends its execution,
4928
5035
it means that the function was defined in a file where
4929
5036
the file name follows the '<code>@</code>'.
4930
5037
If <code>source</code> starts with a '<code>=</code>',
4931
the rest of it should describe the source in a user-dependent manner.
5038
the remainder of its contents describe the source in a user-dependent manner.
4933
5040
the function was defined in a string where
4934
5041
<code>source</code> is that string.
5524
5629
<pre>int luaL_argerror (lua_State *L, int narg, const char *extramsg);</pre>
5527
Raises an error with the following message,
5528
where <code>func</code> is retrieved from the call stack:
5632
Raises an error with a standard message
5633
that includes <code>extramsg</code> as a comment.
5531
bad argument #<narg> to <func> (<extramsg>)
5535
5637
This function never returns,
5556
<li>First you declare a variable <code>b</code> of type <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>.</li>
5658
<li>First declare a variable <code>b</code> of type <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>.</li>
5558
<li>Then you initialize it with a call <code>luaL_buffinit(L, &b)</code>.</li>
5660
<li>Then initialize it with a call <code>luaL_buffinit(L, &b)</code>.</li>
5561
Then you add string pieces to the buffer calling any of
5663
Then add string pieces to the buffer calling any of
5562
5664
the <code>luaL_add*</code> functions.
5566
You finish by calling <code>luaL_pushresult(&b)</code>.
5668
Finish by calling <code>luaL_pushresult(&b)</code>.
5567
5669
This call leaves the final string on the top of the stack.
5578
<li>First you declare a variable <code>b</code> of type <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>.</li>
5680
<li>First declare a variable <code>b</code> of type <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>.</li>
5580
<li>Then you initialize it and preallocate a space of
5682
<li>Then initialize it and preallocate a space of
5581
5683
size <code>sz</code> with a call <code>luaL_buffinitsize(L, &b, sz)</code>.</li>
5583
<li>Then you copy the string into that space.</li>
5685
<li>Then copy the string into that space.</li>
5586
You finish by calling <code>luaL_pushresult(&b, sz)</code>,
5688
Finish by calling <code>luaL_pushresult(&b, sz)</code>,
5587
5689
where <code>sz</code> is the total size of the resulting string
5588
5690
copied into that space.
5648
5750
If the object at index <code>obj</code> has a metatable and this
5649
5751
metatable has a field <code>e</code>,
5650
this function calls this field and passes the object as its only argument.
5752
this function calls this field passing the object as its only argument.
5651
5753
In this case this function returns true and pushes onto the
5652
5754
stack the value returned by the call.
5653
5755
If there is no metatable or no metamethod,
6014
6116
<hr><h3><a name="luaL_loadbuffer"><code>luaL_loadbuffer</code></a></h3><p>
6015
<span class="apii">[-0, +1, <em>-</em>]</span>
6117
<span class="apii">[-0, +1, –]</span>
6016
6118
<pre>int luaL_loadbuffer (lua_State *L,
6017
6119
const char *buff,
6019
6121
const char *name);</pre>
6124
Equivalent to <a href="#luaL_loadbufferx"><code>luaL_loadbufferx</code></a> with <code>mode</code> equal to <code>NULL</code>.
6130
<hr><h3><a name="luaL_loadbufferx"><code>luaL_loadbufferx</code></a></h3><p>
6131
<span class="apii">[-0, +1, –]</span>
6132
<pre>int luaL_loadbufferx (lua_State *L,
6136
const char *mode);</pre>
6022
6139
Loads a buffer as a Lua chunk.
6023
6140
This function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in the
6024
6141
buffer pointed to by <code>buff</code> with size <code>sz</code>.
6038
6156
<pre>int luaL_loadfile (lua_State *L, const char *filename);</pre>
6159
Equivalent to <a href="#luaL_loadfilex"><code>luaL_loadfilex</code></a> with <code>mode</code> equal to <code>NULL</code>.
6165
<hr><h3><a name="luaL_loadfilex"><code>luaL_loadfilex</code></a></h3><p>
6166
<span class="apii">[-0, +1, <em>m</em>]</span>
6167
<pre>int luaL_loadfilex (lua_State *L, const char *filename,
6168
const char *mode);</pre>
6041
6171
Loads a file as a Lua chunk.
6042
6172
This function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in the file
6043
6173
named <code>filename</code>.
6578
<li>basic library;</li>
6580
<li>package library;</li>
6582
<li>coroutine library;</li>
6584
<li>string manipulation;</li>
6586
<li>table manipulation;</li>
6588
<li>mathematical functions (sin, log, etc.);</li>
6590
<li>bitwise operations;</li>
6592
<li>input and output;</li>
6594
<li>operating system facilities;</li>
6596
<li>debug facilities.</li>
6712
<li>basic library (<a href="#6.1">§6.1</a>);</li>
6714
<li>coroutine library (<a href="#6.2">§6.2</a>);</li>
6716
<li>package library (<a href="#6.3">§6.3</a>);</li>
6718
<li>string manipulation (<a href="#6.4">§6.4</a>);</li>
6720
<li>table manipulation (<a href="#6.5">§6.5</a>);</li>
6722
<li>mathematical functions (<a href="#6.6">§6.6</a>) (sin, log, etc.);</li>
6724
<li>bitwise operations (<a href="#6.7">§6.7</a>);</li>
6726
<li>input and output (<a href="#6.8">§6.8</a>);</li>
6728
<li>operating system facilities (<a href="#6.9">§6.9</a>);</li>
6730
<li>debug facilities (<a href="#6.10">§6.10</a>).</li>
6599
Except for the basic and package libraries,
6733
Except for the basic and the package libraries,
6600
6734
each library provides all its functions as fields of a global table
6601
6735
or as methods of its objects.
6662
6796
<li><b>"<code>stop</code>": </b>
6663
stops automatic invocation of the garbage collector.
6664
The collector will run only when explcitly invoked,
6797
stops automatic execution of the garbage collector.
6798
The collector will run only when explicitly invoked,
6665
6799
until a call to restart it.
6668
6802
<li><b>"<code>restart</code>": </b>
6669
restarts automatic invocation the garbage collector.
6803
restarts automatic execution of the garbage collector.
6672
6806
<li><b>"<code>count</code>": </b>
6790
6934
for i,v in ipairs(t) do <em>body</em> end
6792
will iterate over the pairs (<code>1,t[1]</code>), (<code>2,t[2]</code>), ···,
6936
will iterate over the pairs (<code>1,t[1]</code>), (<code>2,t[2]</code>), ...,
6793
6937
up to the first integer key absent from the table.
6823
6967
the first upvalue is set to the value of the
6824
6968
global environment or to <code>env</code>,
6825
6969
if that parameter is given.
6826
(When loading main chunks,
6827
the first upvalue will be the <code>_ENV</code> variable (see <a href="#2.2">§2.2</a>).)
6970
When loading main chunks,
6971
the first upvalue will be the <code>_ENV</code> variable (see <a href="#2.2">§2.2</a>).
6832
6976
and debug information (see <a href="#4.9">§4.9</a>).
6834
6978
it defaults to <code>ld</code>, if <code>ld</code> is a string,
6835
or to "<code>=(load)</code>".
6979
or to "<code>=(load)</code>" otherwise.
6839
6983
The string <code>mode</code> controls whether the chunk can be text or binary
6840
6984
(that is, a precompiled chunk).
6841
A letter '<code>t</code>' in <code>mode</code> allows a text chunk;
6842
a letter '<code>b</code>' allows a binary chunk.
6985
It may be the string "<code>b</code>" (only binary chunks),
6986
"<code>t</code>" (only text chunks),
6987
or "<code>bt</code>" (both binary and text).
6843
6988
The default is "<code>bt</code>".
6849
<hr><h3><a name="pdf-loadfile"><code>loadfile ([filename])</code></a></h3>
6994
<hr><h3><a name="pdf-loadfile"><code>loadfile ([filename [, mode [, env]]])</code></a></h3>
6947
7092
<hr><h3><a name="pdf-print"><code>print (···)</code></a></h3>
6948
Receives any number of arguments,
7093
Receives any number of arguments
6949
7094
and prints their values to <code>stdout</code>,
6950
using the <a href="#pdf-tostring"><code>tostring</code></a> function to convert them to strings.
7095
using the <a href="#pdf-tostring"><code>tostring</code></a> function to convert each argument to a string.
6951
7096
<code>print</code> is not intended for formatted output,
6952
7097
but only as a quick way to show a value,
6953
typically for debugging.
6954
For formatted output, use <a href="#pdf-string.format"><code>string.format</code></a>.
7098
for instance for debugging.
7099
For complete control over the output,
7100
use <a href="#pdf-string.format"><code>string.format</code></a> and <a href="#pdf-io.write"><code>io.write</code></a>.
6990
7136
Sets the real value of <code>table[index]</code> to <code>value</code>,
6991
7137
without invoking any metamethod.
6992
7138
<code>table</code> must be a table,
6993
<code>index</code> any value different from <b>nil</b>,
7139
<code>index</code> any value different from <b>nil</b> and NaN,
6994
7140
and <code>value</code> any Lua value.
7037
7183
<hr><h3><a name="pdf-tonumber"><code>tonumber (e [, base])</code></a></h3>
7038
Tries to convert its argument to a number.
7039
If the argument is already a number or a string convertible
7040
to a number, then <code>tonumber</code> returns this number;
7187
When called with no <code>base</code>,
7188
<code>tonumber</code> tries to convert its argument to a number.
7189
If the argument is already a number or
7190
a string convertible to a number (see <a href="#3.4.2">§3.4.2</a>),
7191
then <code>tonumber</code> returns this number;
7041
7192
otherwise, it returns <b>nil</b>.
7045
An optional argument specifies the base to interpret the numeral.
7196
When called with <code>base</code>,
7197
then <code>e</code> should be a string to be interpreted as
7198
an integer numeral in that base.
7046
7199
The base may be any integer between 2 and 36, inclusive.
7047
7200
In bases above 10, the letter '<code>A</code>' (in either upper or lower case)
7048
7201
represents 10, '<code>B</code>' represents 11, and so forth,
7049
7202
with '<code>Z</code>' representing 35.
7050
In base 10 (the default),
7051
the numeral is converted following the coercion rules (see <a href="#3.4.2">§3.4.2</a>).
7052
In other bases, only integers are accepted.
7203
If the string <code>e</code> is not a valid numeral in the given base,
7204
the function returns <b>nil</b>.
7058
<hr><h3><a name="pdf-tostring"><code>tostring (e)</code></a></h3>
7059
Receives an argument of any type and
7210
<hr><h3><a name="pdf-tostring"><code>tostring (v)</code></a></h3>
7211
Receives a value of any type and
7060
7212
converts it to a string in a reasonable format.
7061
For complete control of how numbers are converted,
7062
use <a href="#pdf-string.format"><code>string.format</code></a>.
7213
(For complete control of how numbers are converted,
7214
use <a href="#pdf-string.format"><code>string.format</code></a>.)
7066
If the metatable of <code>e</code> has a <code>"__tostring"</code> field,
7218
If the metatable of <code>v</code> has a <code>"__tostring"</code> field,
7067
7219
then <code>tostring</code> calls the corresponding value
7068
with <code>e</code> as argument,
7220
with <code>v</code> as argument,
7069
7221
and uses the result of the call as its result.
7139
7291
Starts or continues the execution of coroutine <code>co</code>.
7140
7292
The first time you resume a coroutine,
7141
7293
it starts running its body.
7142
The values <code>val1</code>, ··· are passed
7294
The values <code>val1</code>, ... are passed
7143
7295
as the arguments to the body function.
7144
7296
If the coroutine has yielded,
7145
7297
<code>resume</code> restarts it;
7146
the values <code>val1</code>, ··· are passed
7298
the values <code>val1</code>, ... are passed
7147
7299
as the results from the yield.
7409
7561
the value of the environment variable <a name="pdf-LUA_PATH_5_2"><code>LUA_PATH_5_2</code></a> or
7410
7562
the environment variable <a name="pdf-LUA_PATH"><code>LUA_PATH</code></a> or
7411
7563
with a default path defined in <code>luaconf.h</code>,
7412
if the environment variable is not defined.
7564
if those environment variables are not defined.
7413
7565
Any "<code>;;</code>" in the value of the environment variable
7414
7566
is replaced by the default path.
7531
A path is string containing a sequence of
7683
A path is a string containing a sequence of
7532
7684
<em>templates</em> separated by semicolons.
7533
7685
For each template,
7534
the function changes each interrogation
7535
mark in the template by a copy of <code>name</code>
7686
the function replaces each interrogation mark (if any)
7687
in the template with a copy of <code>name</code>
7536
7688
wherein all occurrences of <code>sep</code>
7537
7689
(a dot, by default)
7538
were replaced by the system's directory separator,
7690
were replaced by <code>rep</code>
7691
(the system's directory separator, by default),
7539
7692
and then tries to open the resulting file name.
7540
If <code>sep</code> is the empty string,
7541
the replacement is not done.
7583
7734
It also sets a metatable for strings
7584
7735
where the <code>__index</code> field points to the <code>string</code> table.
7585
7736
Therefore, you can use the string functions in object-oriented style.
7586
For instance, <code>string.byte(s, i)</code>
7737
For instance, <code>string.byte(s,i)</code>
7587
7738
can be written as <code>s:byte(i)</code>.
7595
7746
<hr><h3><a name="pdf-string.byte"><code>string.byte (s [, i [, j]])</code></a></h3>
7596
7747
Returns the internal numerical codes of the characters <code>s[i]</code>,
7597
<code>s[i+1]</code>, ···, <code>s[j]</code>.
7748
<code>s[i+1]</code>, ..., <code>s[j]</code>.
7598
7749
The default value for <code>i</code> is 1;
7599
7750
the default value for <code>j</code> is <code>i</code>.
7751
These indices are corrected
7752
following the same rules of function <a href="#pdf-string.sub"><code>string.sub</code></a>.
7603
Note that numerical codes are not necessarily portable across platforms.
7756
Numerical codes are not necessarily portable across platforms.
7694
7847
<code>G</code>, and <code>g</code> all expect a number as argument.
7695
7848
Options <code>c</code>, <code>d</code>,
7696
7849
<code>i</code>, <code>o</code>, <code>u</code>, <code>X</code>, and <code>x</code>
7697
expect an integer as argument;
7698
the range of that integer may be limited by
7850
also expect a number,
7851
but the range of that number may be limited by
7699
7852
the underlying C implementation.
7853
For options <code>o</code>, <code>u</code>, <code>X</code>, and <code>x</code>,
7854
the number cannot be negative.
7700
7855
Option <code>q</code> expects a string;
7701
7856
option <code>s</code> expects a string without embedded zeros.
7702
7857
If the argument to option <code>s</code> is not a string,
7709
7864
<hr><h3><a name="pdf-string.gmatch"><code>string.gmatch (s, pattern)</code></a></h3>
7710
7865
Returns an iterator function that,
7711
7866
each time it is called,
7712
returns the next captures from <code>pattern</code> over string <code>s</code>.
7867
returns the next captures from <code>pattern</code> over the string <code>s</code>.
7713
7868
If <code>pattern</code> specifies no captures,
7714
7869
then the whole match is produced in each call.
7884
8039
with length <code>i</code>.
8043
If, after the translation of negative indices,
8044
<code>i</code> is less than 1,
8045
it is corrected to 1.
8046
If <code>j</code> is greater than the string length,
8047
it is corrected to that length.
8048
If, after these corrections,
8049
<code>i</code> is greater than <code>j</code>,
8050
the function returns the empty string.
9007
9177
reads the file according to the given formats.
9008
9178
When no format is given,
9009
9179
uses "*l" as a default.
9010
Therefore, the construction
9180
As an example, the construction
9013
9183
for c in file:lines(1) do <em>body</em> end
9015
9185
will iterate over all characters of the file,
9016
9186
starting at the current position.
9017
(Unlike <a href="#pdf-io.lines"><code>io.lines</code></a>, this function does not close the file
9018
when the loop ends.)
9187
Unlike <a href="#pdf-io.lines"><code>io.lines</code></a>, this function does not close the file
9192
In case of errors this function raises the error,
9193
instead of returning an error code.
9088
9263
<li><b>"<code>cur</code>": </b> base is current position;</li>
9089
9264
<li><b>"<code>end</code>": </b> base is end of file;</li>
9091
In case of success, function <code>seek</code> returns the final file position,
9266
In case of success, <code>seek</code> returns the final file position,
9092
9267
measured in bytes from the beginning of the file.
9093
If this function fails, it returns <b>nil</b>,
9268
If <code>seek</code> fails, it returns <b>nil</b>,
9094
9269
plus a string describing the error.
9202
9374
After this optional character,
9203
9375
if <code>format</code> is the string "<code>*t</code>",
9204
9376
then <code>date</code> returns a table with the following fields:
9205
<code>year</code> (four digits), <code>month</code> (1--12), <code>day</code> (1--31),
9206
<code>hour</code> (0--23), <code>min</code> (0--59), <code>sec</code> (0--61),
9377
<code>year</code> (four digits), <code>month</code> (1–12), <code>day</code> (1–31),
9378
<code>hour</code> (0–23), <code>min</code> (0–59), <code>sec</code> (0–61),
9207
9379
<code>wday</code> (weekday, Sunday is 1),
9208
9380
<code>yday</code> (day of the year),
9209
9381
and <code>isdst</code> (daylight saving flag, a boolean).
9339
9516
Sets the current locale of the program.
9340
<code>locale</code> is a string specifying a locale;
9517
<code>locale</code> is a system-dependent string specifying a locale;
9341
9518
<code>category</code> is an optional string describing which category to change:
9342
9519
<code>"all"</code>, <code>"collate"</code>, <code>"ctype"</code>,
9343
9520
<code>"monetary"</code>, <code>"numeric"</code>, or <code>"time"</code>;
9369
9546
Returns the current time when called without arguments,
9370
9547
or a time representing the date and time specified by the given table.
9371
9548
This table must have fields <code>year</code>, <code>month</code>, and <code>day</code>,
9372
and may have fields <code>hour</code>, <code>min</code>, <code>sec</code>, and <code>isdst</code>
9373
(for a description of these fields, see the <a href="#pdf-os.date"><code>os.date</code></a> function).
9550
<code>hour</code> (default is 12),
9551
<code>min</code> (default is 0),
9552
<code>sec</code> (default is 0),
9553
and <code>isdst</code> (default is <b>nil</b>).
9554
For a description of these fields, see the <a href="#pdf-os.date"><code>os.date</code></a> function.
9480
<hr><h3><a name="pdf-debug.getinfo"><code>debug.getinfo ([thread,] function [, what])</code></a></h3>
9661
<hr><h3><a name="pdf-debug.getinfo"><code>debug.getinfo ([thread,] f [, what])</code></a></h3>
9484
9665
Returns a table with information about a function.
9485
You can give the function directly,
9486
or you can give a number as the value of <code>function</code>,
9487
which means the function running at level <code>function</code> of the call stack
9666
You can give the function directly
9667
or you can give a number as the value of <code>f</code>,
9668
which means the function running at level <code>f</code> of the call stack
9488
9669
of the given thread:
9489
9670
level 0 is the current function (<code>getinfo</code> itself);
9490
9671
level 1 is the function that called <code>getinfo</code>
9491
9672
(except for tail calls, which do not count on the stack);
9493
If <code>function</code> is a number larger than the number of active functions,
9674
If <code>f</code> is a number larger than the number of active functions,
9494
9675
then <code>getinfo</code> returns <b>nil</b>.
9665
<hr><h3><a name="pdf-debug.setmetatable"><code>debug.setmetatable (object, table)</code></a></h3>
9846
<hr><h3><a name="pdf-debug.setmetatable"><code>debug.setmetatable (value, table)</code></a></h3>
9669
Sets the metatable for the given <code>object</code> to the given <code>table</code>
9850
Sets the metatable for the given <code>value</code> to the given <code>table</code>
9670
9851
(which can be <b>nil</b>).
9671
Returns <code>object</code>.
9852
Returns <code>value</code>.
9677
<hr><h3><a name="pdf-debug.setupvalue"><code>debug.setupvalue (func, up, value)</code></a></h3>
9858
<hr><h3><a name="pdf-debug.setupvalue"><code>debug.setupvalue (f, up, value)</code></a></h3>
9681
9862
This function assigns the value <code>value</code> to the upvalue
9682
with index <code>up</code> of the function <code>func</code>.
9863
with index <code>up</code> of the function <code>f</code>.
9683
9864
The function returns <b>nil</b> if there is no upvalue
9684
9865
with the given index.
9685
9866
Otherwise, it returns the name of the upvalue.
9746
<hr><h3><a name="pdf-debug.upvaluejoin"><code>debug.upvaluejoin (func1, n1, func2, n2)</code></a></h3>
9927
<hr><h3><a name="pdf-debug.upvaluejoin"><code>debug.upvaluejoin (f1, n1, f2, n2)</code></a></h3>
9750
Make the <code>n1</code>-th upvalue of the Lua closure <code>func1</code>
9751
refer to the <code>n2</code>-th upvalue of the Lua closure <code>func2</code>.
9931
Make the <code>n1</code>-th upvalue of the Lua closure <code>f1</code>
9932
refer to the <code>n2</code>-th upvalue of the Lua closure <code>f2</code>.
9794
Before running any argument,
9976
When called without option <code>-E</code>,
9795
9977
the interpreter checks for an environment variable <a name="pdf-LUA_INIT_5_2"><code>LUA_INIT_5_2</code></a>
9796
(or <a name="pdf-LUA_INIT"><code>LUA_INIT</code></a> if it is not defined).
9797
If its format is <code>@<em>filename</em></code>,
9978
(or <a name="pdf-LUA_INIT"><code>LUA_INIT</code></a> if it is not defined)
9979
before running any argument.
9980
If the variable content has the format <code>@<em>filename</em></code>,
9798
9981
then <code>lua</code> executes the file.
9799
9982
Otherwise, <code>lua</code> executes the string itself.
9803
All options are handled in order, except <code>-i</code>.
9986
When called with option <code>-E</code>,
9987
besides ignoring <code>LUA_INIT</code>,
9988
the interpreter also resets the values of
9989
<a href="#pdf-package.path"><code>package.path</code></a> and <a href="#pdf-package.cpath"><code>package.cpath</code></a>
9990
with the default paths defined in <code>luaconf.h</code>,
9991
in effect ignoring the values of <code>LUA_PATH</code> and <code>LUA_CPATH</code>.
9995
All options are handled in order, except <code>-i</code> and <code>-E</code>.
9804
9996
For instance, an invocation like
9807
9999
$ lua -e'a=1' -e 'print(a)' script.lua
9809
will first set <code>a</code> to 1, then print the value of <code>a</code> (which is '<code>1</code>'),
10001
will first set <code>a</code> to 1, then print the value of <code>a</code>,
9810
10002
and finally run the file <code>script.lua</code> with no arguments.
9811
(Here <code>$</code> is the shell prompt. Your prompt can be different.)
10003
(Here <code>$</code> is the shell prompt. Your prompt may be different.)
9862
10054
When finishing normally,
9863
10055
the interpreter closes its main Lua state
9864
10056
(see <a href="#lua_close"><code>lua_close</code></a>).
9865
The script can avoid this step by terminating
9866
through <a href="#pdf-os.exit"><code>os.exit</code></a>.
9870
If the global variable <a name="pdf-_PROMPT"><code>_PROMPT</code></a> contains a string,
9871
then its value is used as the prompt.
9872
Similarly, if the global variable <a name="pdf-_PROMPT2"><code>_PROMPT2</code></a> contains a string,
9873
its value is used as the secondary prompt
9874
(issued during incomplete statements).
9875
Therefore, both prompts can be changed directly on the command line
9876
or in any Lua programs by assigning to <code>_PROMPT</code>.
9877
See the next example:
9880
$ lua -e"_PROMPT='myprompt> '" -i
9882
(The outer pair of quotes is for the shell,
9883
the inner pair is for Lua.)
9884
Note the use of <code>-i</code> to enter interactive mode;
9886
the program would just end silently
9887
right after the assignment to <code>_PROMPT</code>.
10057
The script can avoid this step by
10058
calling <a href="#pdf-os.exit"><code>os.exit</code></a> to terminate.
9914
10085
<h1>8 – <a name="8">Incompatibilities with the Previous Version</a></h1>
9917
Here we list the incompatibilities that you can find when moving a program
10088
Here we list the incompatibilities that you may find when moving a program
9918
10089
from Lua 5.1 to Lua 5.2.
9919
You can avoid some incompatibilities compiling Lua with
10090
You can avoid some incompatibilities by compiling Lua with
9920
10091
appropriate options (see file <code>luaconf.h</code>).
9922
10093
all these compatibility options will be removed in the next version of Lua.
9965
10136
The event <em>tail return</em> in debug hooks was removed.
9966
10137
Instead, tail calls generate a special new event,
9967
<em>tail call</em>, so that the debugger can know there will
9968
not be a corresponding return event.
10138
<em>tail call</em>, so that the debugger can know that
10139
there will not be a corresponding return event.
10036
10207
(<a href="#pdf-load"><code>load</code></a> and <a href="#pdf-loadfile"><code>loadfile</code></a>)
10037
10208
are potentially insecure when loading untrusted binary data.
10038
10209
(Actually, those functions were already insecure because
10039
of bugs in the verification algorithm.)
10210
of flaws in the verification algorithm.)
10040
10211
When in doubt,
10041
use the <code>mode</code> argument in function <a href="#pdf-load"><code>load</code></a>
10042
to restrict it to loading textual chunks.
10212
use the <code>mode</code> argument of those functions
10213
to restrict them to loading textual chunks.
10217
The standard paths in the official distribution may
10218
change between versions.
10060
10236
Pseudoindex <code>LUA_ENVIRONINDEX</code>
10061
10237
and functions <code>lua_getfenv</code>/<code>lua_setfenv</code>
10062
10238
were removed,
10063
as C functions do not have environments any more.
10239
as C functions no longer have environments.
10067
10243
Function <code>luaL_register</code> is deprecated.
10068
Use <a href="#luaL_setfuncs"><code>luaL_setfuncs</code></a> so that your module does not
10069
create globals anymore.
10244
Use <a href="#luaL_setfuncs"><code>luaL_setfuncs</code></a> so that your module does not create globals.
10070
10245
(Modules are not expected to set global variables anymore.)