comparison website/src/manual.html.luan @ 418:455784e2227d

remove C documentation from manual
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 30 Apr 2015 21:52:20 -0600
parents a40e99cf0b0b
children df95199ca4c0
comparison
equal deleted inserted replaced
417:a40e99cf0b0b 418:455784e2227d
1754 <p> 1754 <p>
1755 The loop creates ten closures 1755 The loop creates ten closures
1756 (that is, ten instances of the anonymous function). 1756 (that is, ten instances of the anonymous function).
1757 Each of these closures uses a different <tt>y</tt> variable, 1757 Each of these closures uses a different <tt>y</tt> variable,
1758 while all of them share the same <tt>x</tt>. 1758 while all of them share the same <tt>x</tt>.
1759
1760
1761
1762
1763
1764 <h1>4 &ndash; <a name="4">The Application Program Interface</a></h1>
1765
1766 <p>
1767
1768 This section describes the C&nbsp;API for Lua, that is,
1769 the set of C&nbsp;functions available to the host program to communicate
1770 with Lua.
1771 All API functions and related types and constants
1772 are declared in the header file <a name="pdf-lua.h"><code>lua.h</code></a>.
1773
1774
1775 <p>
1776 Even when we use the term "function",
1777 any facility in the API may be provided as a macro instead.
1778 Except where stated otherwise,
1779 all such macros use each of their arguments exactly once
1780 (except for the first argument, which is always a Lua state),
1781 and so do not generate any hidden side-effects.
1782
1783
1784 <p>
1785 As in most C&nbsp;libraries,
1786 the Lua API functions do not check their arguments for validity or consistency.
1787 However, you can change this behavior by compiling Lua
1788 with the macro <a name="pdf-LUA_USE_APICHECK"><code>LUA_USE_APICHECK</code></a> defined.
1789
1790
1791
1792 <h2>4.1 &ndash; <a name="4.1">The Stack</a></h2>
1793
1794 <p>
1795 Lua uses a <em>virtual stack</em> to pass values to and from C.
1796 Each element in this stack represents a Lua value
1797 (<b>nil</b>, number, string, etc.).
1798
1799
1800 <p>
1801 Whenever Lua calls C, the called function gets a new stack,
1802 which is independent of previous stacks and of stacks of
1803 C&nbsp;functions that are still active.
1804 This stack initially contains any arguments to the C&nbsp;function
1805 and it is where the C&nbsp;function pushes its results
1806 to be returned to the caller (see <a href="#lua_CFunction"><code>lua_CFunction</code></a>).
1807
1808
1809 <p>
1810 For convenience,
1811 most query operations in the API do not follow a strict stack discipline.
1812 Instead, they can refer to any element in the stack
1813 by using an <em>index</em>:
1814 A positive index represents an absolute stack position
1815 (starting at&nbsp;1);
1816 a negative index represents an offset relative to the top of the stack.
1817 More specifically, if the stack has <em>n</em> elements,
1818 then index&nbsp;1 represents the first element
1819 (that is, the element that was pushed onto the stack first)
1820 and
1821 index&nbsp;<em>n</em> represents the last element;
1822 index&nbsp;-1 also represents the last element
1823 (that is, the element at the&nbsp;top)
1824 and index <em>-n</em> represents the first element.
1825
1826
1827
1828
1829
1830 <h2>4.2 &ndash; <a name="4.2">Stack Size</a></h2>
1831
1832 <p>
1833 When you interact with the Lua API,
1834 you are responsible for ensuring consistency.
1835 In particular,
1836 <em>you are responsible for controlling stack overflow</em>.
1837 You can use the function <a href="#lua_checkstack"><code>lua_checkstack</code></a>
1838 to ensure that the stack has enough space for pushing new elements.
1839
1840
1841 <p>
1842 Whenever Lua calls C,
1843 it ensures that the stack has space for
1844 at least <a name="pdf-LUA_MINSTACK"><code>LUA_MINSTACK</code></a> extra slots.
1845 <code>LUA_MINSTACK</code> is defined as 20,
1846 so that usually you do not have to worry about stack space
1847 unless your code has loops pushing elements onto the stack.
1848
1849
1850 <p>
1851 When you call a Lua function
1852 without a fixed number of results (see <a href="#lua_call"><code>lua_call</code></a>),
1853 Lua ensures that the stack has enough space for all results,
1854 but it does not ensure any extra space.
1855 So, before pushing anything in the stack after such a call
1856 you should use <a href="#lua_checkstack"><code>lua_checkstack</code></a>.
1857
1858
1859
1860
1861
1862 <h2>4.3 &ndash; <a name="4.3">Valid and Acceptable Indices</a></h2>
1863
1864 <p>
1865 Any function in the API that receives stack indices
1866 works only with <em>valid indices</em> or <em>acceptable indices</em>.
1867
1868
1869 <p>
1870 A <em>valid index</em> is an index that refers to a
1871 real position within the stack, that is,
1872 its position lies between&nbsp;1 and the stack top
1873 (<code>1 &le; abs(index) &le; top</code>).
1874
1875 Usually, functions that can modify the value at an index
1876 require valid indices.
1877
1878
1879 <p>
1880 Unless otherwise noted,
1881 any function that accepts valid indices also accepts <em>pseudo-indices</em>,
1882 which represent some Lua values that are accessible to C&nbsp;code
1883 but which are not in the stack.
1884 Pseudo-indices are used to access the registry
1885 and the upvalues of a C&nbsp;function (see <a href="#4.4">&sect;4.4</a>).
1886
1887
1888 <p>
1889 Functions that do not need a specific stack position,
1890 but only a value in the stack (e.g., query functions),
1891 can be called with acceptable indices.
1892 An <em>acceptable index</em> can be any valid index,
1893 including the pseudo-indices,
1894 but it also can be any positive index after the stack top
1895 within the space allocated for the stack,
1896 that is, indices up to the stack size.
1897 (Note that 0 is never an acceptable index.)
1898 Except when noted otherwise,
1899 functions in the API work with acceptable indices.
1900
1901
1902 <p>
1903 Acceptable indices serve to avoid extra tests
1904 against the stack top when querying the stack.
1905 For instance, a C&nbsp;function can query its third argument
1906 without the need to first check whether there is a third argument,
1907 that is, without the need to check whether 3 is a valid index.
1908
1909
1910 <p>
1911 For functions that can be called with acceptable indices,
1912 any non-valid index is treated as if it
1913 contains a value of a virtual type <a name="pdf-LUA_TNONE"><code>LUA_TNONE</code></a>,
1914 which behaves like a nil value.
1915
1916
1917
1918
1919
1920 <h2>4.4 &ndash; <a name="4.4">C Closures</a></h2>
1921
1922 <p>
1923 When a C&nbsp;function is created,
1924 it is possible to associate some values with it,
1925 thus creating a <em>C&nbsp;closure</em>
1926 (see <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a>);
1927 these values are called <em>upvalues</em> and are
1928 accessible to the function whenever it is called.
1929
1930
1931 <p>
1932 Whenever a C&nbsp;function is called,
1933 its upvalues are located at specific pseudo-indices.
1934 These pseudo-indices are produced by the macro
1935 <a href="#lua_upvalueindex"><code>lua_upvalueindex</code></a>.
1936 The first value associated with a function is at position
1937 <code>lua_upvalueindex(1)</code>, and so on.
1938 Any access to <code>lua_upvalueindex(<em>n</em>)</code>,
1939 where <em>n</em> is greater than the number of upvalues of the
1940 current function (but not greater than 256),
1941 produces an acceptable but invalid index.
1942
1943
1944
1945
1946
1947 <h2>4.5 &ndash; <a name="4.5">Registry</a></h2>
1948
1949 <p>
1950 Lua provides a <em>registry</em>,
1951 a predefined table that can be used by any C&nbsp;code to
1952 store whatever Lua values it needs to store.
1953 The registry table is always located at pseudo-index
1954 <a name="pdf-LUA_REGISTRYINDEX"><code>LUA_REGISTRYINDEX</code></a>,
1955 which is a valid index.
1956 Any C&nbsp;library can store data into this table,
1957 but it must take care to choose keys
1958 that are different from those used
1959 by other libraries, to avoid collisions.
1960 Typically, you should use as key a string containing your library name,
1961 or a light userdata with the address of a C&nbsp;object in your code,
1962 or any Lua object created by your code.
1963 As with variable names,
1964 string keys starting with an underscore followed by
1965 uppercase letters are reserved for Lua.
1966
1967
1968 <p>
1969 The integer keys in the registry are used
1970 by the reference mechanism (see <a href="#luaL_ref"><code>luaL_ref</code></a>)
1971 and by some predefined values.
1972 Therefore, integer keys must not be used for other purposes.
1973
1974
1975 <p>
1976 When you create a new Lua state,
1977 its registry comes with some predefined values.
1978 These predefined values are indexed with integer keys
1979 defined as constants in <code>lua.h</code>.
1980 The following constants are defined:
1981
1982 <ul>
1983 <li><b><a name="pdf-LUA_RIDX_MAINTHREAD"><code>LUA_RIDX_MAINTHREAD</code></a>: </b> At this index the registry has
1984 the main thread of the state.
1985 (The main thread is the one created together with the state.)
1986 </li>
1987
1988 <li><b><a name="pdf-LUA_RIDX_GLOBALS"><code>LUA_RIDX_GLOBALS</code></a>: </b> At this index the registry has
1989 the global environment.
1990 </li>
1991 </ul>
1992
1993
1994
1995
1996 <h2>4.6 &ndash; <a name="4.6">Error Handling in C</a></h2>
1997
1998 <p>
1999 Internally, Lua uses the C <code>longjmp</code> facility to handle errors.
2000 (Lua will use exceptions if you compile it as C++;
2001 search for <code>LUAI_THROW</code> in the source code for details.)
2002 When Lua faces any error
2003 (such as a memory allocation error, type errors, syntax errors,
2004 and runtime errors)
2005 it <em>raises</em> an error;
2006 that is, it does a long jump.
2007 A <em>protected environment</em> uses <code>setjmp</code>
2008 to set a recovery point;
2009 any error jumps to the most recent active recovery point.
2010
2011
2012 <p>
2013 If an error happens outside any protected environment,
2014 Lua calls a <em>panic function</em> (see <a href="#lua_atpanic"><code>lua_atpanic</code></a>)
2015 and then calls <code>abort</code>,
2016 thus exiting the host application.
2017 Your panic function can avoid this exit by
2018 never returning
2019 (e.g., doing a long jump to your own recovery point outside Lua).
2020
2021
2022 <p>
2023 The panic function runs as if it were a message handler (see <a href="#2.3">&sect;2.3</a>);
2024 in particular, the error message is at the top of the stack.
2025 However, there is no guarantee about stack space.
2026 To push anything on the stack,
2027 the panic function must first check the available space (see <a href="#4.2">&sect;4.2</a>).
2028
2029
2030 <p>
2031 Most functions in the API can raise an error,
2032 for instance due to a memory allocation error.
2033 The documentation for each function indicates whether
2034 it can raise errors.
2035
2036
2037 <p>
2038 Inside a C&nbsp;function you can raise an error by calling <a href="#lua_error"><code>lua_error</code></a>.
2039
2040
2041
2042
2043
2044 <h2>4.7 &ndash; <a name="4.7">Handling Yields in C</a></h2>
2045
2046 <p>
2047 Internally, Lua uses the C <code>longjmp</code> facility to yield a coroutine.
2048 Therefore, if a C function <code>foo</code> calls an API function
2049 and this API function yields
2050 (directly or indirectly by calling another function that yields),
2051 Lua cannot return to <code>foo</code> any more,
2052 because the <code>longjmp</code> removes its frame from the C stack.
2053
2054
2055 <p>
2056 To avoid this kind of problem,
2057 Lua raises an error whenever it tries to yield across an API call,
2058 except for three functions:
2059 <a href="#lua_yieldk"><code>lua_yieldk</code></a>, <a href="#lua_callk"><code>lua_callk</code></a>, and <a href="#lua_pcallk"><code>lua_pcallk</code></a>.
2060 All those functions receive a <em>continuation function</em>
2061 (as a parameter named <code>k</code>) to continue execution after a yield.
2062
2063
2064 <p>
2065 We need to set some terminology to explain continuations.
2066 We have a C function called from Lua which we will call
2067 the <em>original function</em>.
2068 This original function then calls one of those three functions in the C API,
2069 which we will call the <em>callee function</em>,
2070 that then yields the current thread.
2071 (This can happen when the callee function is <a href="#lua_yieldk"><code>lua_yieldk</code></a>,
2072 or when the callee function is either <a href="#lua_callk"><code>lua_callk</code></a> or <a href="#lua_pcallk"><code>lua_pcallk</code></a>
2073 and the function called by them yields.)
2074
2075
2076 <p>
2077 Suppose the running thread yields while executing the callee function.
2078 After the thread resumes,
2079 it eventually will finish running the callee function.
2080 However,
2081 the callee function cannot return to the original function,
2082 because its frame in the C stack was destroyed by the yield.
2083 Instead, Lua calls a <em>continuation function</em>,
2084 which was given as an argument to the callee function.
2085 As the name implies,
2086 the continuation function should continue the task
2087 of the original function.
2088
2089
2090 <p>
2091 As an illustration, consider the following function:
2092
2093 <pre>
2094 int original_function (lua_State *L) {
2095 ... /* code 1 */
2096 status = lua_pcall(L, n, m, h); /* calls Lua */
2097 ... /* code 2 */
2098 }
2099 </pre><p>
2100 Now we want to allow
2101 the Lua code being run by <a href="#lua_pcall"><code>lua_pcall</code></a> to yield.
2102 First, we can rewrite our function like here:
2103
2104 <pre>
2105 int k (lua_State *L, int status, lua_KContext ctx) {
2106 ... /* code 2 */
2107 }
2108
2109 int original_function (lua_State *L) {
2110 ... /* code 1 */
2111 return k(L, lua_pcall(L, n, m, h), ctx);
2112 }
2113 </pre><p>
2114 In the above code,
2115 the new function <code>k</code> is a
2116 <em>continuation function</em> (with type <a href="#lua_KFunction"><code>lua_KFunction</code></a>),
2117 which should do all the work that the original function
2118 was doing after calling <a href="#lua_pcall"><code>lua_pcall</code></a>.
2119 Now, we must inform Lua that it must call <code>k</code> if the Lua code
2120 being executed by <a href="#lua_pcall"><code>lua_pcall</code></a> gets interrupted in some way
2121 (errors or yielding),
2122 so we rewrite the code as here,
2123 replacing <a href="#lua_pcall"><code>lua_pcall</code></a> by <a href="#lua_pcallk"><code>lua_pcallk</code></a>:
2124
2125 <pre>
2126 int original_function (lua_State *L) {
2127 ... /* code 1 */
2128 return k(L, lua_pcallk(L, n, m, h, ctx2, k), ctx1);
2129 }
2130 </pre><p>
2131 Note the external, explicit call to the continuation:
2132 Lua will call the continuation only if needed, that is,
2133 in case of errors or resuming after a yield.
2134 If the called function returns normally without ever yielding,
2135 <a href="#lua_pcallk"><code>lua_pcallk</code></a> (and <a href="#lua_callk"><code>lua_callk</code></a>) will also return normally.
2136 (Of course, instead of calling the continuation in that case,
2137 you can do the equivalent work directly inside the original function.)
2138
2139
2140 <p>
2141 Besides the Lua state,
2142 the continuation function has two other parameters:
2143 the final status of the call plus the context value (<code>ctx</code>) that
2144 was passed originally to <a href="#lua_pcallk"><code>lua_pcallk</code></a>.
2145 (Lua does not use this context value;
2146 it only passes this value from the original function to the
2147 continuation function.)
2148 For <a href="#lua_pcallk"><code>lua_pcallk</code></a>,
2149 the status is the same value that would be returned by <a href="#lua_pcallk"><code>lua_pcallk</code></a>,
2150 except that it is <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> when being executed after a yield
2151 (instead of <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>).
2152 For <a href="#lua_yieldk"><code>lua_yieldk</code></a> and <a href="#lua_callk"><code>lua_callk</code></a>,
2153 the status is always <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> when Lua calls the continuation.
2154 (For these two functions,
2155 Lua will not call the continuation in case of errors,
2156 because they do not handle errors.)
2157 Similarly, when using <a href="#lua_callk"><code>lua_callk</code></a>,
2158 you should call the continuation function
2159 with <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> as the status.
2160 (For <a href="#lua_yieldk"><code>lua_yieldk</code></a>, there is not much point in calling
2161 directly the continuation function,
2162 because <a href="#lua_yieldk"><code>lua_yieldk</code></a> usually does not return.)
2163
2164
2165 <p>
2166 Lua treats the continuation function as if it were the original function.
2167 The continuation function receives the same Lua stack
2168 from the original function,
2169 in the same state it would be if the callee function had returned.
2170 (For instance,
2171 after a <a href="#lua_callk"><code>lua_callk</code></a> the function and its arguments are
2172 removed from the stack and replaced by the results from the call.)
2173 It also has the same upvalues.
2174 Whatever it returns is handled by Lua as if it were the return
2175 of the original function.
2176
2177
2178
2179
2180
2181 <h2>4.8 &ndash; <a name="4.8">Functions and Types</a></h2>
2182
2183 <p>
2184 Here we list all functions and types from the C&nbsp;API in
2185 alphabetical order.
2186 Each function has an indicator like this:
2187 <span class="apii">[-o, +p, <em>x</em>]</span>
2188
2189
2190 <p>
2191 The first field, <code>o</code>,
2192 is how many elements the function pops from the stack.
2193 The second field, <code>p</code>,
2194 is how many elements the function pushes onto the stack.
2195 (Any function always pushes its results after popping its arguments.)
2196 A field in the form <code>x|y</code> means the function can push (or pop)
2197 <code>x</code> or <code>y</code> elements,
2198 depending on the situation;
2199 an interrogation mark '<code>?</code>' means that
2200 we cannot know how many elements the function pops/pushes
2201 by looking only at its arguments
2202 (e.g., they may depend on what is on the stack).
2203 The third field, <code>x</code>,
2204 tells whether the function may raise errors:
2205 '<code>-</code>' means the function never raises any error;
2206 '<code>e</code>' means the function may raise errors;
2207 '<code>v</code>' means the function may raise an error on purpose.
2208
2209
2210
2211 <hr><h3><a name="lua_absindex"><code>lua_absindex</code></a></h3><p>
2212 <span class="apii">[-0, +0, &ndash;]</span>
2213 <pre>int lua_absindex (lua_State *L, int idx);</pre>
2214
2215 <p>
2216 Converts the acceptable index <code>idx</code> into an absolute index
2217 (that is, one that does not depend on the stack top).
2218
2219
2220
2221
2222
2223 <hr><h3><a name="lua_Alloc"><code>lua_Alloc</code></a></h3>
2224 <pre>typedef void * (*lua_Alloc) (void *ud,
2225 void *ptr,
2226 size_t osize,
2227 size_t nsize);</pre>
2228
2229 <p>
2230 The type of the memory-allocation function used by Lua states.
2231 The allocator function must provide a
2232 functionality similar to <code>realloc</code>,
2233 but not exactly the same.
2234 Its arguments are
2235 <code>ud</code>, an opaque pointer passed to <a href="#lua_newstate"><code>lua_newstate</code></a>;
2236 <code>ptr</code>, a pointer to the block being allocated/reallocated/freed;
2237 <code>osize</code>, the original size of the block or some code about what
2238 is being allocated;
2239 and <code>nsize</code>, the new size of the block.
2240
2241
2242 <p>
2243 When <code>ptr</code> is not <code>NULL</code>,
2244 <code>osize</code> is the size of the block pointed by <code>ptr</code>,
2245 that is, the size given when it was allocated or reallocated.
2246
2247
2248 <p>
2249 When <code>ptr</code> is <code>NULL</code>,
2250 <code>osize</code> encodes the kind of object that Lua is allocating.
2251 <code>osize</code> is any of
2252 <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>,
2253 <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)
2254 Lua is creating a new object of that type.
2255 When <code>osize</code> is some other value,
2256 Lua is allocating memory for something else.
2257
2258
2259 <p>
2260 Lua assumes the following behavior from the allocator function:
2261
2262
2263 <p>
2264 When <code>nsize</code> is zero,
2265 the allocator must behave like <code>free</code>
2266 and return <code>NULL</code>.
2267
2268
2269 <p>
2270 When <code>nsize</code> is not zero,
2271 the allocator must behave like <code>realloc</code>.
2272 The allocator returns <code>NULL</code>
2273 if and only if it cannot fulfill the request.
2274 Lua assumes that the allocator never fails when
2275 <code>osize &gt;= nsize</code>.
2276
2277
2278 <p>
2279 Here is a simple implementation for the allocator function.
2280 It is used in the auxiliary library by <a href="#luaL_newstate"><code>luaL_newstate</code></a>.
2281
2282 <pre>
2283 static void *l_alloc (void *ud, void *ptr, size_t osize,
2284 size_t nsize) {
2285 (void)ud; (void)osize; /* not used */
2286 if (nsize == 0) {
2287 free(ptr);
2288 return NULL;
2289 }
2290 else
2291 return realloc(ptr, nsize);
2292 }
2293 </pre><p>
2294 Note that Standard&nbsp;C ensures
2295 that <code>free(NULL)</code> has no effect and that
2296 <code>realloc(NULL,size)</code> is equivalent to <code>malloc(size)</code>.
2297 This code assumes that <code>realloc</code> does not fail when shrinking a block.
2298 (Although Standard&nbsp;C does not ensure this behavior,
2299 it seems to be a safe assumption.)
2300
2301
2302
2303
2304
2305 <hr><h3><a name="lua_arith"><code>lua_arith</code></a></h3><p>
2306 <span class="apii">[-(2|1), +1, <em>e</em>]</span>
2307 <pre>void lua_arith (lua_State *L, int op);</pre>
2308
2309 <p>
2310 Performs an arithmetic or bitwise operation over the two values
2311 (or one, in the case of negations)
2312 at the top of the stack,
2313 with the value at the top being the second operand,
2314 pops these values, and pushes the result of the operation.
2315 The function follows the semantics of the corresponding Lua operator
2316 (that is, it may call metamethods).
2317
2318
2319 <p>
2320 The value of <code>op</code> must be one of the following constants:
2321
2322 <ul>
2323
2324 <li><b><a name="pdf-LUA_OPADD"><code>LUA_OPADD</code></a>: </b> performs addition (<code>+</code>)</li>
2325 <li><b><a name="pdf-LUA_OPSUB"><code>LUA_OPSUB</code></a>: </b> performs subtraction (<code>-</code>)</li>
2326 <li><b><a name="pdf-LUA_OPMUL"><code>LUA_OPMUL</code></a>: </b> performs multiplication (<code>*</code>)</li>
2327 <li><b><a name="pdf-LUA_OPDIV"><code>LUA_OPDIV</code></a>: </b> performs float division (<code>/</code>)</li>
2328 <li><b><a name="pdf-LUA_OPIDIV"><code>LUA_OPIDIV</code></a>: </b> performs floor division (<code>//</code>)</li>
2329 <li><b><a name="pdf-LUA_OPMOD"><code>LUA_OPMOD</code></a>: </b> performs modulo (<code>%</code>)</li>
2330 <li><b><a name="pdf-LUA_OPPOW"><code>LUA_OPPOW</code></a>: </b> performs exponentiation (<code>^</code>)</li>
2331 <li><b><a name="pdf-LUA_OPUNM"><code>LUA_OPUNM</code></a>: </b> performs mathematical negation (unary <code>-</code>)</li>
2332 <li><b><a name="pdf-LUA_OPBNOT"><code>LUA_OPBNOT</code></a>: </b> performs bitwise negation (<code>~</code>)</li>
2333 <li><b><a name="pdf-LUA_OPBAND"><code>LUA_OPBAND</code></a>: </b> performs bitwise and (<code>&amp;</code>)</li>
2334 <li><b><a name="pdf-LUA_OPBOR"><code>LUA_OPBOR</code></a>: </b> performs bitwise or (<code>|</code>)</li>
2335 <li><b><a name="pdf-LUA_OPBXOR"><code>LUA_OPBXOR</code></a>: </b> performs bitwise exclusive or (<code>~</code>)</li>
2336 <li><b><a name="pdf-LUA_OPSHL"><code>LUA_OPSHL</code></a>: </b> performs left shift (<code>&lt;&lt;</code>)</li>
2337 <li><b><a name="pdf-LUA_OPSHR"><code>LUA_OPSHR</code></a>: </b> performs right shift (<code>&gt;&gt;</code>)</li>
2338
2339 </ul>
2340
2341
2342
2343
2344 <hr><h3><a name="lua_atpanic"><code>lua_atpanic</code></a></h3><p>
2345 <span class="apii">[-0, +0, &ndash;]</span>
2346 <pre>lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf);</pre>
2347
2348 <p>
2349 Sets a new panic function and returns the old one (see <a href="#4.6">&sect;4.6</a>).
2350
2351
2352
2353
2354
2355 <hr><h3><a name="lua_call"><code>lua_call</code></a></h3><p>
2356 <span class="apii">[-(nargs+1), +nresults, <em>e</em>]</span>
2357 <pre>void lua_call (lua_State *L, int nargs, int nresults);</pre>
2358
2359 <p>
2360 Calls a function.
2361
2362
2363 <p>
2364 To call a function you must use the following protocol:
2365 first, the function to be called is pushed onto the stack;
2366 then, the arguments to the function are pushed
2367 in direct order;
2368 that is, the first argument is pushed first.
2369 Finally you call <a href="#lua_call"><code>lua_call</code></a>;
2370 <code>nargs</code> is the number of arguments that you pushed onto the stack.
2371 All arguments and the function value are popped from the stack
2372 when the function is called.
2373 The function results are pushed onto the stack when the function returns.
2374 The number of results is adjusted to <code>nresults</code>,
2375 unless <code>nresults</code> is <a name="pdf-LUA_MULTRET"><code>LUA_MULTRET</code></a>.
2376 In this case, all results from the function are pushed.
2377 Lua takes care that the returned values fit into the stack space.
2378 The function results are pushed onto the stack in direct order
2379 (the first result is pushed first),
2380 so that after the call the last result is on the top of the stack.
2381
2382
2383 <p>
2384 Any error inside the called function is propagated upwards
2385 (with a <code>longjmp</code>).
2386
2387
2388 <p>
2389 The following example shows how the host program can do the
2390 equivalent to this Lua code:
2391
2392 <pre>
2393 a = f("how", t.x, 14)
2394 </pre><p>
2395 Here it is in&nbsp;C:
2396
2397 <pre>
2398 lua_getglobal(L, "f"); /* function to be called */
2399 lua_pushliteral(L, "how"); /* 1st argument */
2400 lua_getglobal(L, "t"); /* table to be indexed */
2401 lua_getfield(L, -1, "x"); /* push result of t.x (2nd arg) */
2402 lua_remove(L, -2); /* remove 't' from the stack */
2403 lua_pushinteger(L, 14); /* 3rd argument */
2404 lua_call(L, 3, 1); /* call 'f' with 3 arguments and 1 result */
2405 lua_setglobal(L, "a"); /* set global 'a' */
2406 </pre><p>
2407 Note that the code above is <em>balanced</em>:
2408 at its end, the stack is back to its original configuration.
2409 This is considered good programming practice.
2410
2411
2412
2413
2414
2415 <hr><h3><a name="lua_callk"><code>lua_callk</code></a></h3><p>
2416 <span class="apii">[-(nargs + 1), +nresults, <em>e</em>]</span>
2417 <pre>void lua_callk (lua_State *L,
2418 int nargs,
2419 int nresults,
2420 lua_KContext ctx,
2421 lua_KFunction k);</pre>
2422
2423 <p>
2424 This function behaves exactly like <a href="#lua_call"><code>lua_call</code></a>,
2425 but allows the called function to yield (see <a href="#4.7">&sect;4.7</a>).
2426
2427
2428
2429
2430
2431 <hr><h3><a name="lua_CFunction"><code>lua_CFunction</code></a></h3>
2432 <pre>typedef int (*lua_CFunction) (lua_State *L);</pre>
2433
2434 <p>
2435 Type for C&nbsp;functions.
2436
2437
2438 <p>
2439 In order to communicate properly with Lua,
2440 a C&nbsp;function must use the following protocol,
2441 which defines the way parameters and results are passed:
2442 a C&nbsp;function receives its arguments from Lua in its stack
2443 in direct order (the first argument is pushed first).
2444 So, when the function starts,
2445 <code>lua_gettop(L)</code> returns the number of arguments received by the function.
2446 The first argument (if any) is at index 1
2447 and its last argument is at index <code>lua_gettop(L)</code>.
2448 To return values to Lua, a C&nbsp;function just pushes them onto the stack,
2449 in direct order (the first result is pushed first),
2450 and returns the number of results.
2451 Any other value in the stack below the results will be properly
2452 discarded by Lua.
2453 Like a Lua function, a C&nbsp;function called by Lua can also return
2454 many results.
2455
2456
2457 <p>
2458 As an example, the following function receives a variable number
2459 of numerical arguments and returns their average and their sum:
2460
2461 <pre>
2462 static int foo (lua_State *L) {
2463 int n = lua_gettop(L); /* number of arguments */
2464 lua_Number sum = 0.0;
2465 int i;
2466 for (i = 1; i &lt;= n; i++) {
2467 if (!lua_isnumber(L, i)) {
2468 lua_pushliteral(L, "incorrect argument");
2469 lua_error(L);
2470 }
2471 sum += lua_tonumber(L, i);
2472 }
2473 lua_pushnumber(L, sum/n); /* first result */
2474 lua_pushnumber(L, sum); /* second result */
2475 return 2; /* number of results */
2476 }
2477 </pre>
2478
2479
2480
2481
2482 <hr><h3><a name="lua_checkstack"><code>lua_checkstack</code></a></h3><p>
2483 <span class="apii">[-0, +0, &ndash;]</span>
2484 <pre>int lua_checkstack (lua_State *L, int n);</pre>
2485
2486 <p>
2487 Ensures that the stack has space for at least <code>n</code> extra slots.
2488 It returns false if it cannot fulfill the request,
2489 either because it would cause the stack
2490 to be larger than a fixed maximum size
2491 (typically at least several thousand elements) or
2492 because it cannot allocate memory for the extra space.
2493 This function never shrinks the stack;
2494 if the stack is already larger than the new size,
2495 it is left unchanged.
2496
2497
2498
2499
2500
2501 <hr><h3><a name="lua_close"><code>lua_close</code></a></h3><p>
2502 <span class="apii">[-0, +0, &ndash;]</span>
2503 <pre>void lua_close (lua_State *L);</pre>
2504
2505 <p>
2506 Destroys all objects in the given Lua state
2507 (calling the corresponding garbage-collection metamethods, if any)
2508 and frees all dynamic memory used by this state.
2509 On several platforms, you may not need to call this function,
2510 because all resources are naturally released when the host program ends.
2511 On the other hand, long-running programs that create multiple states,
2512 such as daemons or web servers,
2513 will probably need to close states as soon as they are not needed.
2514
2515
2516
2517
2518
2519 <hr><h3><a name="lua_compare"><code>lua_compare</code></a></h3><p>
2520 <span class="apii">[-0, +0, <em>e</em>]</span>
2521 <pre>int lua_compare (lua_State *L, int index1, int index2, int op);</pre>
2522
2523 <p>
2524 Compares two Lua values.
2525 Returns 1 if the value at index <code>index1</code> satisfies <code>op</code>
2526 when compared with the value at index <code>index2</code>,
2527 following the semantics of the corresponding Lua operator
2528 (that is, it may call metamethods).
2529 Otherwise returns&nbsp;0.
2530 Also returns&nbsp;0 if any of the indices is not valid.
2531
2532
2533 <p>
2534 The value of <code>op</code> must be one of the following constants:
2535
2536 <ul>
2537
2538 <li><b><a name="pdf-LUA_OPEQ"><code>LUA_OPEQ</code></a>: </b> compares for equality (<code>==</code>)</li>
2539 <li><b><a name="pdf-LUA_OPLT"><code>LUA_OPLT</code></a>: </b> compares for less than (<code>&lt;</code>)</li>
2540 <li><b><a name="pdf-LUA_OPLE"><code>LUA_OPLE</code></a>: </b> compares for less or equal (<code>&lt;=</code>)</li>
2541
2542 </ul>
2543
2544
2545
2546
2547 <hr><h3><a name="lua_concat"><code>lua_concat</code></a></h3><p>
2548 <span class="apii">[-n, +1, <em>e</em>]</span>
2549 <pre>void lua_concat (lua_State *L, int n);</pre>
2550
2551 <p>
2552 Concatenates the <code>n</code> values at the top of the stack,
2553 pops them, and leaves the result at the top.
2554 If <code>n</code>&nbsp;is&nbsp;1, the result is the single value on the stack
2555 (that is, the function does nothing);
2556 if <code>n</code> is 0, the result is the empty string.
2557 Concatenation is performed following the usual semantics of Lua
2558 (see <a href="#3.4.6">&sect;3.4.6</a>).
2559
2560
2561
2562
2563
2564 <hr><h3><a name="lua_copy"><code>lua_copy</code></a></h3><p>
2565 <span class="apii">[-0, +0, &ndash;]</span>
2566 <pre>void lua_copy (lua_State *L, int fromidx, int toidx);</pre>
2567
2568 <p>
2569 Copies the element at index <code>fromidx</code>
2570 into the valid index <code>toidx</code>,
2571 replacing the value at that position.
2572 Values at other positions are not affected.
2573
2574
2575
2576
2577
2578 <hr><h3><a name="lua_createtable"><code>lua_createtable</code></a></h3><p>
2579 <span class="apii">[-0, +1, <em>e</em>]</span>
2580 <pre>void lua_createtable (lua_State *L, int narr, int nrec);</pre>
2581
2582 <p>
2583 Creates a new empty table and pushes it onto the stack.
2584 Parameter <code>narr</code> is a hint for how many elements the table
2585 will have as a sequence;
2586 parameter <code>nrec</code> is a hint for how many other elements
2587 the table will have.
2588 Lua may use these hints to preallocate memory for the new table.
2589 This pre-allocation is useful for performance when you know in advance
2590 how many elements the table will have.
2591 Otherwise you can use the function <a href="#lua_newtable"><code>lua_newtable</code></a>.
2592
2593
2594
2595
2596
2597 <hr><h3><a name="lua_dump"><code>lua_dump</code></a></h3><p>
2598 <span class="apii">[-0, +0, <em>e</em>]</span>
2599 <pre>int lua_dump (lua_State *L,
2600 lua_Writer writer,
2601 void *data,
2602 int strip);</pre>
2603
2604 <p>
2605 Dumps a function as a binary chunk.
2606 Receives a Lua function on the top of the stack
2607 and produces a binary chunk that,
2608 if loaded again,
2609 results in a function equivalent to the one dumped.
2610 As it produces parts of the chunk,
2611 <a href="#lua_dump"><code>lua_dump</code></a> calls function <code>writer</code> (see <a href="#lua_Writer"><code>lua_Writer</code></a>)
2612 with the given <code>data</code>
2613 to write them.
2614
2615
2616 <p>
2617 If <code>strip</code> is true,
2618 the binary representation is created without debug information
2619 about the function.
2620
2621
2622 <p>
2623 The value returned is the error code returned by the last
2624 call to the writer;
2625 0&nbsp;means no errors.
2626
2627
2628 <p>
2629 This function does not pop the Lua function from the stack.
2630
2631
2632
2633
2634
2635 <hr><h3><a name="lua_error"><code>lua_error</code></a></h3><p>
2636 <span class="apii">[-1, +0, <em>v</em>]</span>
2637 <pre>int lua_error (lua_State *L);</pre>
2638
2639 <p>
2640 Generates a Lua error,
2641 using the value at the top of the stack as the error object.
2642 This function does a long jump,
2643 and therefore never returns
2644 (see <a href="#luaL_error"><code>luaL_error</code></a>).
2645
2646
2647
2648
2649
2650 <hr><h3><a name="lua_gc"><code>lua_gc</code></a></h3><p>
2651 <span class="apii">[-0, +0, <em>e</em>]</span>
2652 <pre>int lua_gc (lua_State *L, int what, int data);</pre>
2653
2654 <p>
2655 Controls the garbage collector.
2656
2657
2658 <p>
2659 This function performs several tasks,
2660 according to the value of the parameter <code>what</code>:
2661
2662 <ul>
2663
2664 <li><b><code>LUA_GCSTOP</code>: </b>
2665 stops the garbage collector.
2666 </li>
2667
2668 <li><b><code>LUA_GCRESTART</code>: </b>
2669 restarts the garbage collector.
2670 </li>
2671
2672 <li><b><code>LUA_GCCOLLECT</code>: </b>
2673 performs a full garbage-collection cycle.
2674 </li>
2675
2676 <li><b><code>LUA_GCCOUNT</code>: </b>
2677 returns the current amount of memory (in Kbytes) in use by Lua.
2678 </li>
2679
2680 <li><b><code>LUA_GCCOUNTB</code>: </b>
2681 returns the remainder of dividing the current amount of bytes of
2682 memory in use by Lua by 1024.
2683 </li>
2684
2685 <li><b><code>LUA_GCSTEP</code>: </b>
2686 performs an incremental step of garbage collection.
2687 </li>
2688
2689 <li><b><code>LUA_GCSETPAUSE</code>: </b>
2690 sets <code>data</code> as the new value
2691 for the <em>pause</em> of the collector (see <a href="#2.5">&sect;2.5</a>)
2692 and returns the previous value of the pause.
2693 </li>
2694
2695 <li><b><code>LUA_GCSETSTEPMUL</code>: </b>
2696 sets <code>data</code> as the new value for the <em>step multiplier</em> of
2697 the collector (see <a href="#2.5">&sect;2.5</a>)
2698 and returns the previous value of the step multiplier.
2699 </li>
2700
2701 <li><b><code>LUA_GCISRUNNING</code>: </b>
2702 returns a boolean that tells whether the collector is running
2703 (i.e., not stopped).
2704 </li>
2705
2706 </ul>
2707
2708 <p>
2709 For more details about these options,
2710 see <a href="#pdf-collectgarbage"><code>collectgarbage</code></a>.
2711
2712
2713
2714
2715
2716 <hr><h3><a name="lua_getallocf"><code>lua_getallocf</code></a></h3><p>
2717 <span class="apii">[-0, +0, &ndash;]</span>
2718 <pre>lua_Alloc lua_getallocf (lua_State *L, void **ud);</pre>
2719
2720 <p>
2721 Returns the memory-allocation function of a given state.
2722 If <code>ud</code> is not <code>NULL</code>, Lua stores in <code>*ud</code> the
2723 opaque pointer given when the memory-allocator function was set.
2724
2725
2726
2727
2728
2729 <hr><h3><a name="lua_getfield"><code>lua_getfield</code></a></h3><p>
2730 <span class="apii">[-0, +1, <em>e</em>]</span>
2731 <pre>int lua_getfield (lua_State *L, int index, const char *k);</pre>
2732
2733 <p>
2734 Pushes onto the stack the value <code>t[k]</code>,
2735 where <code>t</code> is the value at the given index.
2736 As in Lua, this function may trigger a metamethod
2737 for the "index" event (see <a href="#2.4">&sect;2.4</a>).
2738
2739
2740 <p>
2741 Returns the type of the pushed value.
2742
2743
2744
2745
2746
2747 <hr><h3><a name="lua_getextraspace"><code>lua_getextraspace</code></a></h3><p>
2748 <span class="apii">[-0, +0, &ndash;]</span>
2749 <pre>void *lua_getextraspace (lua_State *L);</pre>
2750
2751 <p>
2752 Returns a pointer to a raw memory area associated with the
2753 given Lua state.
2754 The application can use this area for any purpose;
2755 Lua does not use it for anything.
2756
2757
2758 <p>
2759 Each new thread has this area initialized with a copy
2760 of the area of the main thread.
2761
2762
2763 <p>
2764 By default, this area has the size of a pointer to void,
2765 but you can recompile Lua with a different size for this area.
2766 (See <code>LUA_EXTRASPACE</code> in <code>luaconf.h</code>.)
2767
2768
2769
2770
2771
2772 <hr><h3><a name="lua_getglobal"><code>lua_getglobal</code></a></h3><p>
2773 <span class="apii">[-0, +1, <em>e</em>]</span>
2774 <pre>int lua_getglobal (lua_State *L, const char *name);</pre>
2775
2776 <p>
2777 Pushes onto the stack the value of the global <code>name</code>.
2778 Returns the type of that value.
2779
2780
2781
2782
2783
2784 <hr><h3><a name="lua_geti"><code>lua_geti</code></a></h3><p>
2785 <span class="apii">[-0, +1, <em>e</em>]</span>
2786 <pre>int lua_geti (lua_State *L, int index, lua_Integer i);</pre>
2787
2788 <p>
2789 Pushes onto the stack the value <code>t[i]</code>,
2790 where <code>t</code> is the value at the given index.
2791 As in Lua, this function may trigger a metamethod
2792 for the "index" event (see <a href="#2.4">&sect;2.4</a>).
2793
2794
2795 <p>
2796 Returns the type of the pushed value.
2797
2798
2799
2800
2801
2802 <hr><h3><a name="lua_getmetatable"><code>lua_getmetatable</code></a></h3><p>
2803 <span class="apii">[-0, +(0|1), &ndash;]</span>
2804 <pre>int lua_getmetatable (lua_State *L, int index);</pre>
2805
2806 <p>
2807 If the value at the given index has a metatable,
2808 the function pushes that metatable onto the stack and returns&nbsp;1.
2809 Otherwise,
2810 the function returns&nbsp;0 and pushes nothing on the stack.
2811
2812
2813
2814
2815
2816 <hr><h3><a name="lua_gettable"><code>lua_gettable</code></a></h3><p>
2817 <span class="apii">[-1, +1, <em>e</em>]</span>
2818 <pre>int lua_gettable (lua_State *L, int index);</pre>
2819
2820 <p>
2821 Pushes onto the stack the value <code>t[k]</code>,
2822 where <code>t</code> is the value at the given index
2823 and <code>k</code> is the value at the top of the stack.
2824
2825
2826 <p>
2827 This function pops the key from the stack,
2828 pushing the resulting value in its place.
2829 As in Lua, this function may trigger a metamethod
2830 for the "index" event (see <a href="#2.4">&sect;2.4</a>).
2831
2832
2833 <p>
2834 Returns the type of the pushed value.
2835
2836
2837
2838
2839
2840 <hr><h3><a name="lua_gettop"><code>lua_gettop</code></a></h3><p>
2841 <span class="apii">[-0, +0, &ndash;]</span>
2842 <pre>int lua_gettop (lua_State *L);</pre>
2843
2844 <p>
2845 Returns the index of the top element in the stack.
2846 Because indices start at&nbsp;1,
2847 this result is equal to the number of elements in the stack;
2848 in particular, 0&nbsp;means an empty stack.
2849
2850
2851
2852
2853
2854 <hr><h3><a name="lua_getuservalue"><code>lua_getuservalue</code></a></h3><p>
2855 <span class="apii">[-0, +1, &ndash;]</span>
2856 <pre>int lua_getuservalue (lua_State *L, int index);</pre>
2857
2858 <p>
2859 Pushes onto the stack the Lua value associated with the userdata
2860 at the given index.
2861
2862
2863 <p>
2864 Returns the type of the pushed value.
2865
2866
2867
2868
2869
2870 <hr><h3><a name="lua_insert"><code>lua_insert</code></a></h3><p>
2871 <span class="apii">[-1, +1, &ndash;]</span>
2872 <pre>void lua_insert (lua_State *L, int index);</pre>
2873
2874 <p>
2875 Moves the top element into the given valid index,
2876 shifting up the elements above this index to open space.
2877 This function cannot be called with a pseudo-index,
2878 because a pseudo-index is not an actual stack position.
2879
2880
2881
2882
2883
2884 <hr><h3><a name="lua_Integer"><code>lua_Integer</code></a></h3>
2885 <pre>typedef ... lua_Integer;</pre>
2886
2887 <p>
2888 The type of integers in Lua.
2889
2890
2891 <p>
2892 By default this type is <code>long long</code>,
2893 (usually a 64-bit two-complement integer),
2894 but that can be changed to <code>long</code> or <code>int</code>
2895 (usually a 32-bit two-complement integer).
2896 (See <code>LUA_INT</code> in <code>luaconf.h</code>.)
2897
2898
2899 <p>
2900 Lua also defines the constants
2901 <a name="pdf-LUA_MININTEGER"><code>LUA_MININTEGER</code></a> and <a name="pdf-LUA_MAXINTEGER"><code>LUA_MAXINTEGER</code></a>,
2902 with the minimum and the maximum values that fit in this type.
2903
2904
2905
2906
2907
2908 <hr><h3><a name="lua_isboolean"><code>lua_isboolean</code></a></h3><p>
2909 <span class="apii">[-0, +0, &ndash;]</span>
2910 <pre>int lua_isboolean (lua_State *L, int index);</pre>
2911
2912 <p>
2913 Returns 1 if the value at the given index is a boolean,
2914 and 0&nbsp;otherwise.
2915
2916
2917
2918
2919
2920 <hr><h3><a name="lua_iscfunction"><code>lua_iscfunction</code></a></h3><p>
2921 <span class="apii">[-0, +0, &ndash;]</span>
2922 <pre>int lua_iscfunction (lua_State *L, int index);</pre>
2923
2924 <p>
2925 Returns 1 if the value at the given index is a C&nbsp;function,
2926 and 0&nbsp;otherwise.
2927
2928
2929
2930
2931
2932 <hr><h3><a name="lua_isfunction"><code>lua_isfunction</code></a></h3><p>
2933 <span class="apii">[-0, +0, &ndash;]</span>
2934 <pre>int lua_isfunction (lua_State *L, int index);</pre>
2935
2936 <p>
2937 Returns 1 if the value at the given index is a function
2938 (either C or Lua), and 0&nbsp;otherwise.
2939
2940
2941
2942
2943
2944 <hr><h3><a name="lua_isinteger"><code>lua_isinteger</code></a></h3><p>
2945 <span class="apii">[-0, +0, &ndash;]</span>
2946 <pre>int lua_isinteger (lua_State *L, int index);</pre>
2947
2948 <p>
2949 Returns 1 if the value at the given index is an integer
2950 (that is, the value is a number and is represented as an integer),
2951 and 0&nbsp;otherwise.
2952
2953
2954
2955
2956
2957 <hr><h3><a name="lua_islightuserdata"><code>lua_islightuserdata</code></a></h3><p>
2958 <span class="apii">[-0, +0, &ndash;]</span>
2959 <pre>int lua_islightuserdata (lua_State *L, int index);</pre>
2960
2961 <p>
2962 Returns 1 if the value at the given index is a light userdata,
2963 and 0&nbsp;otherwise.
2964
2965
2966
2967
2968
2969 <hr><h3><a name="lua_isnil"><code>lua_isnil</code></a></h3><p>
2970 <span class="apii">[-0, +0, &ndash;]</span>
2971 <pre>int lua_isnil (lua_State *L, int index);</pre>
2972
2973 <p>
2974 Returns 1 if the value at the given index is <b>nil</b>,
2975 and 0&nbsp;otherwise.
2976
2977
2978
2979
2980
2981 <hr><h3><a name="lua_isnone"><code>lua_isnone</code></a></h3><p>
2982 <span class="apii">[-0, +0, &ndash;]</span>
2983 <pre>int lua_isnone (lua_State *L, int index);</pre>
2984
2985 <p>
2986 Returns 1 if the given index is not valid,
2987 and 0&nbsp;otherwise.
2988
2989
2990
2991
2992
2993 <hr><h3><a name="lua_isnoneornil"><code>lua_isnoneornil</code></a></h3><p>
2994 <span class="apii">[-0, +0, &ndash;]</span>
2995 <pre>int lua_isnoneornil (lua_State *L, int index);</pre>
2996
2997 <p>
2998 Returns 1 if the given index is not valid
2999 or if the value at this index is <b>nil</b>,
3000 and 0&nbsp;otherwise.
3001
3002
3003
3004
3005
3006 <hr><h3><a name="lua_isnumber"><code>lua_isnumber</code></a></h3><p>
3007 <span class="apii">[-0, +0, &ndash;]</span>
3008 <pre>int lua_isnumber (lua_State *L, int index);</pre>
3009
3010 <p>
3011 Returns 1 if the value at the given index is a number
3012 or a string convertible to a number,
3013 and 0&nbsp;otherwise.
3014
3015
3016
3017
3018
3019 <hr><h3><a name="lua_isstring"><code>lua_isstring</code></a></h3><p>
3020 <span class="apii">[-0, +0, &ndash;]</span>
3021 <pre>int lua_isstring (lua_State *L, int index);</pre>
3022
3023 <p>
3024 Returns 1 if the value at the given index is a string
3025 or a number (which is always convertible to a string),
3026 and 0&nbsp;otherwise.
3027
3028
3029
3030
3031
3032 <hr><h3><a name="lua_istable"><code>lua_istable</code></a></h3><p>
3033 <span class="apii">[-0, +0, &ndash;]</span>
3034 <pre>int lua_istable (lua_State *L, int index);</pre>
3035
3036 <p>
3037 Returns 1 if the value at the given index is a table,
3038 and 0&nbsp;otherwise.
3039
3040
3041
3042
3043
3044 <hr><h3><a name="lua_isthread"><code>lua_isthread</code></a></h3><p>
3045 <span class="apii">[-0, +0, &ndash;]</span>
3046 <pre>int lua_isthread (lua_State *L, int index);</pre>
3047
3048 <p>
3049 Returns 1 if the value at the given index is a thread,
3050 and 0&nbsp;otherwise.
3051
3052
3053
3054
3055
3056 <hr><h3><a name="lua_isuserdata"><code>lua_isuserdata</code></a></h3><p>
3057 <span class="apii">[-0, +0, &ndash;]</span>
3058 <pre>int lua_isuserdata (lua_State *L, int index);</pre>
3059
3060 <p>
3061 Returns 1 if the value at the given index is a userdata
3062 (either full or light), and 0&nbsp;otherwise.
3063
3064
3065
3066
3067
3068 <hr><h3><a name="lua_isyieldable"><code>lua_isyieldable</code></a></h3><p>
3069 <span class="apii">[-0, +0, &ndash;]</span>
3070 <pre>int lua_isyieldable (lua_State *L);</pre>
3071
3072 <p>
3073 Returns 1 if the given coroutine can yield,
3074 and 0&nbsp;otherwise.
3075
3076
3077
3078
3079
3080 <hr><h3><a name="lua_KContext"><code>lua_KContext</code></a></h3>
3081 <pre>typedef ... lua_KContext;</pre>
3082
3083 <p>
3084 The type for continuation-function contexts.
3085 It must be a numerical type.
3086 This type is defined as <code>intptr_t</code>
3087 when <code>intptr_t</code> is available,
3088 so that it can store pointers too.
3089 Otherwise, it is defined as <code>ptrdiff_t</code>.
3090
3091
3092
3093
3094
3095 <hr><h3><a name="lua_KFunction"><code>lua_KFunction</code></a></h3>
3096 <pre>typedef int (*lua_KFunction) (lua_State *L, int status, lua_KContext ctx);</pre>
3097
3098 <p>
3099 Type for continuation functions (see <a href="#4.7">&sect;4.7</a>).
3100
3101
3102
3103
3104
3105 <hr><h3><a name="lua_len"><code>lua_len</code></a></h3><p>
3106 <span class="apii">[-0, +1, <em>e</em>]</span>
3107 <pre>void lua_len (lua_State *L, int index);</pre>
3108
3109 <p>
3110 Returns the length of the value at the given index.
3111 It is equivalent to the '<code>#</code>' operator in Lua (see <a href="#3.4.7">&sect;3.4.7</a>) and
3112 may trigger a metamethod for the "length" event (see <a href="#2.4">&sect;2.4</a>).
3113 The result is pushed on the stack.
3114
3115
3116
3117
3118
3119 <hr><h3><a name="lua_load"><code>lua_load</code></a></h3><p>
3120 <span class="apii">[-0, +1, &ndash;]</span>
3121 <pre>int lua_load (lua_State *L,
3122 lua_Reader reader,
3123 void *data,
3124 const char *chunkname,
3125 const char *mode);</pre>
3126
3127 <p>
3128 Loads a Lua chunk without running it.
3129 If there are no errors,
3130 <code>lua_load</code> pushes the compiled chunk as a Lua
3131 function on top of the stack.
3132 Otherwise, it pushes an error message.
3133
3134
3135 <p>
3136 The return values of <code>lua_load</code> are:
3137
3138 <ul>
3139
3140 <li><b><a href="#pdf-LUA_OK"><code>LUA_OK</code></a>: </b> no errors;</li>
3141
3142 <li><b><a name="pdf-LUA_ERRSYNTAX"><code>LUA_ERRSYNTAX</code></a>: </b>
3143 syntax error during precompilation;</li>
3144
3145 <li><b><a href="#pdf-LUA_ERRMEM"><code>LUA_ERRMEM</code></a>: </b>
3146 memory allocation error;</li>
3147
3148 <li><b><a href="#pdf-LUA_ERRGCMM"><code>LUA_ERRGCMM</code></a>: </b>
3149 error while running a <code>__gc</code> metamethod.
3150 (This error has no relation with the chunk being loaded.
3151 It is generated by the garbage collector.)
3152 </li>
3153
3154 </ul>
3155
3156 <p>
3157 The <code>lua_load</code> function uses a user-supplied <code>reader</code> function
3158 to read the chunk (see <a href="#lua_Reader"><code>lua_Reader</code></a>).
3159 The <code>data</code> argument is an opaque value passed to the reader function.
3160
3161
3162 <p>
3163 The <code>chunkname</code> argument gives a name to the chunk,
3164 which is used for error messages and in debug information (see <a href="#4.9">&sect;4.9</a>).
3165
3166
3167 <p>
3168 <code>lua_load</code> automatically detects whether the chunk is text or binary
3169 and loads it accordingly (see program <code>luac</code>).
3170 The string <code>mode</code> works as in function <a href="#pdf-load"><code>load</code></a>,
3171 with the addition that
3172 a <code>NULL</code> value is equivalent to the string "<code>bt</code>".
3173
3174
3175 <p>
3176 <code>lua_load</code> uses the stack internally,
3177 so the reader function must always leave the stack
3178 unmodified when returning.
3179
3180
3181 <p>
3182 If the resulting function has upvalues,
3183 its first upvalue is set to the value of the global environment
3184 stored at index <code>LUA_RIDX_GLOBALS</code> in the registry (see <a href="#4.5">&sect;4.5</a>).
3185 When loading main chunks,
3186 this upvalue will be the <code>_ENV</code> variable (see <a href="#2.2">&sect;2.2</a>).
3187 Other upvalues are initialized with <b>nil</b>.
3188
3189
3190
3191
3192
3193 <hr><h3><a name="lua_newstate"><code>lua_newstate</code></a></h3><p>
3194 <span class="apii">[-0, +0, &ndash;]</span>
3195 <pre>lua_State *lua_newstate (lua_Alloc f, void *ud);</pre>
3196
3197 <p>
3198 Creates a new thread running in a new, independent state.
3199 Returns <code>NULL</code> if it cannot create the thread or the state
3200 (due to lack of memory).
3201 The argument <code>f</code> is the allocator function;
3202 Lua does all memory allocation for this state through this function.
3203 The second argument, <code>ud</code>, is an opaque pointer that Lua
3204 passes to the allocator in every call.
3205
3206
3207
3208
3209
3210 <hr><h3><a name="lua_newtable"><code>lua_newtable</code></a></h3><p>
3211 <span class="apii">[-0, +1, <em>e</em>]</span>
3212 <pre>void lua_newtable (lua_State *L);</pre>
3213
3214 <p>
3215 Creates a new empty table and pushes it onto the stack.
3216 It is equivalent to <code>lua_createtable(L, 0, 0)</code>.
3217
3218
3219
3220
3221
3222 <hr><h3><a name="lua_newthread"><code>lua_newthread</code></a></h3><p>
3223 <span class="apii">[-0, +1, <em>e</em>]</span>
3224 <pre>lua_State *lua_newthread (lua_State *L);</pre>
3225
3226 <p>
3227 Creates a new thread, pushes it on the stack,
3228 and returns a pointer to a <a href="#lua_State"><code>lua_State</code></a> that represents this new thread.
3229 The new thread returned by this function shares with the original thread
3230 its global environment,
3231 but has an independent execution stack.
3232
3233
3234 <p>
3235 There is no explicit function to close or to destroy a thread.
3236 Threads are subject to garbage collection,
3237 like any Lua object.
3238
3239
3240
3241
3242
3243 <hr><h3><a name="lua_newuserdata"><code>lua_newuserdata</code></a></h3><p>
3244 <span class="apii">[-0, +1, <em>e</em>]</span>
3245 <pre>void *lua_newuserdata (lua_State *L, size_t size);</pre>
3246
3247 <p>
3248 This function allocates a new block of memory with the given size,
3249 pushes onto the stack a new full userdata with the block address,
3250 and returns this address.
3251 The host program can freely use this memory.
3252
3253
3254
3255
3256
3257 <hr><h3><a name="lua_next"><code>lua_next</code></a></h3><p>
3258 <span class="apii">[-1, +(2|0), <em>e</em>]</span>
3259 <pre>int lua_next (lua_State *L, int index);</pre>
3260
3261 <p>
3262 Pops a key from the stack,
3263 and pushes a key&ndash;value pair from the table at the given index
3264 (the "next" pair after the given key).
3265 If there are no more elements in the table,
3266 then <a href="#lua_next"><code>lua_next</code></a> returns 0 (and pushes nothing).
3267
3268
3269 <p>
3270 A typical traversal looks like this:
3271
3272 <pre>
3273 /* table is in the stack at index 't' */
3274 lua_pushnil(L); /* first key */
3275 while (lua_next(L, t) != 0) {
3276 /* uses 'key' (at index -2) and 'value' (at index -1) */
3277 printf("%s - %s\n",
3278 lua_typename(L, lua_type(L, -2)),
3279 lua_typename(L, lua_type(L, -1)));
3280 /* removes 'value'; keeps 'key' for next iteration */
3281 lua_pop(L, 1);
3282 }
3283 </pre>
3284
3285 <p>
3286 While traversing a table,
3287 do not call <a href="#lua_tolstring"><code>lua_tolstring</code></a> directly on a key,
3288 unless you know that the key is actually a string.
3289 Recall that <a href="#lua_tolstring"><code>lua_tolstring</code></a> may change
3290 the value at the given index;
3291 this confuses the next call to <a href="#lua_next"><code>lua_next</code></a>.
3292
3293
3294 <p>
3295 See function <a href="#pdf-next"><code>next</code></a> for the caveats of modifying
3296 the table during its traversal.
3297
3298
3299
3300
3301
3302 <hr><h3><a name="lua_Number"><code>lua_Number</code></a></h3>
3303 <pre>typedef double lua_Number;</pre>
3304
3305 <p>
3306 The type of floats in Lua.
3307
3308
3309 <p>
3310 By default this type is double,
3311 but that can be changed to a single float.
3312 (See <code>LUA_REAL</code> in <code>luaconf.h</code>.)
3313
3314
3315
3316
3317
3318 <hr><h3><a name="lua_numbertointeger"><code>lua_numbertointeger</code></a></h3>
3319 <pre>int lua_numbertointeger (lua_Number n, lua_Integer *p);</pre>
3320
3321 <p>
3322 Converts a Lua float to a Lua integer.
3323 This macro assumes that <code>n</code> has an integral value.
3324 If that value is within the range of Lua integers,
3325 it is converted to an integer and assigned to <code>*p</code>.
3326 The macro results in a boolean indicating whether the
3327 conversion was successful.
3328 (Note that this range test can be tricky to do
3329 correctly without this macro,
3330 due to roundings.)
3331
3332
3333 <p>
3334 This macro may evaluate its arguments more than once.
3335
3336
3337
3338
3339
3340 <hr><h3><a name="lua_pcall"><code>lua_pcall</code></a></h3><p>
3341 <span class="apii">[-(nargs + 1), +(nresults|1), &ndash;]</span>
3342 <pre>int lua_pcall (lua_State *L, int nargs, int nresults, int msgh);</pre>
3343
3344 <p>
3345 Calls a function in protected mode.
3346
3347
3348 <p>
3349 Both <code>nargs</code> and <code>nresults</code> have the same meaning as
3350 in <a href="#lua_call"><code>lua_call</code></a>.
3351 If there are no errors during the call,
3352 <a href="#lua_pcall"><code>lua_pcall</code></a> behaves exactly like <a href="#lua_call"><code>lua_call</code></a>.
3353 However, if there is any error,
3354 <a href="#lua_pcall"><code>lua_pcall</code></a> catches it,
3355 pushes a single value on the stack (the error message),
3356 and returns an error code.
3357 Like <a href="#lua_call"><code>lua_call</code></a>,
3358 <a href="#lua_pcall"><code>lua_pcall</code></a> always removes the function
3359 and its arguments from the stack.
3360
3361
3362 <p>
3363 If <code>msgh</code> is 0,
3364 then the error message returned on the stack
3365 is exactly the original error message.
3366 Otherwise, <code>msgh</code> is the stack index of a
3367 <em>message handler</em>.
3368 (In the current implementation, this index cannot be a pseudo-index.)
3369 In case of runtime errors,
3370 this function will be called with the error message
3371 and its return value will be the message
3372 returned on the stack by <a href="#lua_pcall"><code>lua_pcall</code></a>.
3373
3374
3375 <p>
3376 Typically, the message handler is used to add more debug
3377 information to the error message, such as a stack traceback.
3378 Such information cannot be gathered after the return of <a href="#lua_pcall"><code>lua_pcall</code></a>,
3379 since by then the stack has unwound.
3380
3381
3382 <p>
3383 The <a href="#lua_pcall"><code>lua_pcall</code></a> function returns one of the following constants
3384 (defined in <code>lua.h</code>):
3385
3386 <ul>
3387
3388 <li><b><a name="pdf-LUA_OK"><code>LUA_OK</code></a> (0): </b>
3389 success.</li>
3390
3391 <li><b><a name="pdf-LUA_ERRRUN"><code>LUA_ERRRUN</code></a>: </b>
3392 a runtime error.
3393 </li>
3394
3395 <li><b><a name="pdf-LUA_ERRMEM"><code>LUA_ERRMEM</code></a>: </b>
3396 memory allocation error.
3397 For such errors, Lua does not call the message handler.
3398 </li>
3399
3400 <li><b><a name="pdf-LUA_ERRERR"><code>LUA_ERRERR</code></a>: </b>
3401 error while running the message handler.
3402 </li>
3403
3404 <li><b><a name="pdf-LUA_ERRGCMM"><code>LUA_ERRGCMM</code></a>: </b>
3405 error while running a <code>__gc</code> metamethod.
3406 (This error typically has no relation with the function being called.)
3407 </li>
3408
3409 </ul>
3410
3411
3412
3413
3414 <hr><h3><a name="lua_pcallk"><code>lua_pcallk</code></a></h3><p>
3415 <span class="apii">[-(nargs + 1), +(nresults|1), &ndash;]</span>
3416 <pre>int lua_pcallk (lua_State *L,
3417 int nargs,
3418 int nresults,
3419 int msgh,
3420 lua_KContext ctx,
3421 lua_KFunction k);</pre>
3422
3423 <p>
3424 This function behaves exactly like <a href="#lua_pcall"><code>lua_pcall</code></a>,
3425 but allows the called function to yield (see <a href="#4.7">&sect;4.7</a>).
3426
3427
3428
3429
3430
3431 <hr><h3><a name="lua_pop"><code>lua_pop</code></a></h3><p>
3432 <span class="apii">[-n, +0, &ndash;]</span>
3433 <pre>void lua_pop (lua_State *L, int n);</pre>
3434
3435 <p>
3436 Pops <code>n</code> elements from the stack.
3437
3438
3439
3440
3441
3442 <hr><h3><a name="lua_pushboolean"><code>lua_pushboolean</code></a></h3><p>
3443 <span class="apii">[-0, +1, &ndash;]</span>
3444 <pre>void lua_pushboolean (lua_State *L, int b);</pre>
3445
3446 <p>
3447 Pushes a boolean value with value <code>b</code> onto the stack.
3448
3449
3450
3451
3452
3453 <hr><h3><a name="lua_pushcclosure"><code>lua_pushcclosure</code></a></h3><p>
3454 <span class="apii">[-n, +1, <em>e</em>]</span>
3455 <pre>void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);</pre>
3456
3457 <p>
3458 Pushes a new C&nbsp;closure onto the stack.
3459
3460
3461 <p>
3462 When a C&nbsp;function is created,
3463 it is possible to associate some values with it,
3464 thus creating a C&nbsp;closure (see <a href="#4.4">&sect;4.4</a>);
3465 these values are then accessible to the function whenever it is called.
3466 To associate values with a C&nbsp;function,
3467 first these values must be pushed onto the stack
3468 (when there are multiple values, the first value is pushed first).
3469 Then <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a>
3470 is called to create and push the C&nbsp;function onto the stack,
3471 with the argument <code>n</code> telling how many values will be
3472 associated with the function.
3473 <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a> also pops these values from the stack.
3474
3475
3476 <p>
3477 The maximum value for <code>n</code> is 255.
3478
3479
3480 <p>
3481 When <code>n</code> is zero,
3482 this function creates a <em>light C function</em>,
3483 which is just a pointer to the C&nbsp;function.
3484 In that case, it never raises a memory error.
3485
3486
3487
3488
3489
3490 <hr><h3><a name="lua_pushcfunction"><code>lua_pushcfunction</code></a></h3><p>
3491 <span class="apii">[-0, +1, &ndash;]</span>
3492 <pre>void lua_pushcfunction (lua_State *L, lua_CFunction f);</pre>
3493
3494 <p>
3495 Pushes a C&nbsp;function onto the stack.
3496 This function receives a pointer to a C function
3497 and pushes onto the stack a Lua value of type <code>function</code> that,
3498 when called, invokes the corresponding C&nbsp;function.
3499
3500
3501 <p>
3502 Any function to be registered in Lua must
3503 follow the correct protocol to receive its parameters
3504 and return its results (see <a href="#lua_CFunction"><code>lua_CFunction</code></a>).
3505
3506
3507 <p>
3508 <code>lua_pushcfunction</code> is defined as a macro:
3509
3510 <pre>
3511 #define lua_pushcfunction(L,f) lua_pushcclosure(L,f,0)
3512 </pre><p>
3513 Note that <code>f</code> is used twice.
3514
3515
3516
3517
3518
3519 <hr><h3><a name="lua_pushfstring"><code>lua_pushfstring</code></a></h3><p>
3520 <span class="apii">[-0, +1, <em>e</em>]</span>
3521 <pre>const char *lua_pushfstring (lua_State *L, const char *fmt, ...);</pre>
3522
3523 <p>
3524 Pushes onto the stack a formatted string
3525 and returns a pointer to this string.
3526 It is similar to the ISO&nbsp;C function <code>sprintf</code>,
3527 but has some important differences:
3528
3529 <ul>
3530
3531 <li>
3532 You do not have to allocate space for the result:
3533 the result is a Lua string and Lua takes care of memory allocation
3534 (and deallocation, through garbage collection).
3535 </li>
3536
3537 <li>
3538 The conversion specifiers are quite restricted.
3539 There are no flags, widths, or precisions.
3540 The conversion specifiers can only be
3541 '<code>%%</code>' (inserts the character '<code>%</code>'),
3542 '<code>%s</code>' (inserts a zero-terminated string, with no size restrictions),
3543 '<code>%f</code>' (inserts a <a href="#lua_Number"><code>lua_Number</code></a>),
3544 '<code>%L</code>' (inserts a <a href="#lua_Integer"><code>lua_Integer</code></a>),
3545 '<code>%p</code>' (inserts a pointer as a hexadecimal numeral),
3546 '<code>%d</code>' (inserts an <code>int</code>),
3547 '<code>%c</code>' (inserts an <code>int</code> as a one-byte character), and
3548 '<code>%U</code>' (inserts a <code>long int</code> as a UTF-8 byte sequence).
3549 </li>
3550
3551 </ul>
3552
3553
3554
3555
3556 <hr><h3><a name="lua_pushglobaltable"><code>lua_pushglobaltable</code></a></h3><p>
3557 <span class="apii">[-0, +1, &ndash;]</span>
3558 <pre>void lua_pushglobaltable (lua_State *L);</pre>
3559
3560 <p>
3561 Pushes the global environment onto the stack.
3562
3563
3564
3565
3566
3567 <hr><h3><a name="lua_pushinteger"><code>lua_pushinteger</code></a></h3><p>
3568 <span class="apii">[-0, +1, &ndash;]</span>
3569 <pre>void lua_pushinteger (lua_State *L, lua_Integer n);</pre>
3570
3571 <p>
3572 Pushes an integer with value <code>n</code> onto the stack.
3573
3574
3575
3576
3577
3578 <hr><h3><a name="lua_pushlightuserdata"><code>lua_pushlightuserdata</code></a></h3><p>
3579 <span class="apii">[-0, +1, &ndash;]</span>
3580 <pre>void lua_pushlightuserdata (lua_State *L, void *p);</pre>
3581
3582 <p>
3583 Pushes a light userdata onto the stack.
3584
3585
3586 <p>
3587 Userdata represent C&nbsp;values in Lua.
3588 A <em>light userdata</em> represents a pointer, a <code>void*</code>.
3589 It is a value (like a number):
3590 you do not create it, it has no individual metatable,
3591 and it is not collected (as it was never created).
3592 A light userdata is equal to "any"
3593 light userdata with the same C&nbsp;address.
3594
3595
3596
3597
3598
3599 <hr><h3><a name="lua_pushliteral"><code>lua_pushliteral</code></a></h3><p>
3600 <span class="apii">[-0, +1, <em>e</em>]</span>
3601 <pre>const char *lua_pushliteral (lua_State *L, const char *s);</pre>
3602
3603 <p>
3604 This macro is equivalent to <a href="#lua_pushlstring"><code>lua_pushlstring</code></a>,
3605 but can be used only when <code>s</code> is a literal string.
3606 It automatically provides the string length.
3607
3608
3609
3610
3611
3612 <hr><h3><a name="lua_pushlstring"><code>lua_pushlstring</code></a></h3><p>
3613 <span class="apii">[-0, +1, <em>e</em>]</span>
3614 <pre>const char *lua_pushlstring (lua_State *L, const char *s, size_t len);</pre>
3615
3616 <p>
3617 Pushes the string pointed to by <code>s</code> with size <code>len</code>
3618 onto the stack.
3619 Lua makes (or reuses) an internal copy of the given string,
3620 so the memory at <code>s</code> can be freed or reused immediately after
3621 the function returns.
3622 The string can contain any binary data,
3623 including embedded zeros.
3624
3625
3626 <p>
3627 Returns a pointer to the internal copy of the string.
3628
3629
3630
3631
3632
3633 <hr><h3><a name="lua_pushnil"><code>lua_pushnil</code></a></h3><p>
3634 <span class="apii">[-0, +1, &ndash;]</span>
3635 <pre>void lua_pushnil (lua_State *L);</pre>
3636
3637 <p>
3638 Pushes a nil value onto the stack.
3639
3640
3641
3642
3643
3644 <hr><h3><a name="lua_pushnumber"><code>lua_pushnumber</code></a></h3><p>
3645 <span class="apii">[-0, +1, &ndash;]</span>
3646 <pre>void lua_pushnumber (lua_State *L, lua_Number n);</pre>
3647
3648 <p>
3649 Pushes a float with value <code>n</code> onto the stack.
3650
3651
3652
3653
3654
3655 <hr><h3><a name="lua_pushstring"><code>lua_pushstring</code></a></h3><p>
3656 <span class="apii">[-0, +1, <em>e</em>]</span>
3657 <pre>const char *lua_pushstring (lua_State *L, const char *s);</pre>
3658
3659 <p>
3660 Pushes the zero-terminated string pointed to by <code>s</code>
3661 onto the stack.
3662 Lua makes (or reuses) an internal copy of the given string,
3663 so the memory at <code>s</code> can be freed or reused immediately after
3664 the function returns.
3665
3666
3667 <p>
3668 Returns a pointer to the internal copy of the string.
3669
3670
3671 <p>
3672 If <code>s</code> is <code>NULL</code>, pushes <b>nil</b> and returns <code>NULL</code>.
3673
3674
3675
3676
3677
3678 <hr><h3><a name="lua_pushthread"><code>lua_pushthread</code></a></h3><p>
3679 <span class="apii">[-0, +1, &ndash;]</span>
3680 <pre>int lua_pushthread (lua_State *L);</pre>
3681
3682 <p>
3683 Pushes the thread represented by <code>L</code> onto the stack.
3684 Returns 1 if this thread is the main thread of its state.
3685
3686
3687
3688
3689
3690 <hr><h3><a name="lua_pushvalue"><code>lua_pushvalue</code></a></h3><p>
3691 <span class="apii">[-0, +1, &ndash;]</span>
3692 <pre>void lua_pushvalue (lua_State *L, int index);</pre>
3693
3694 <p>
3695 Pushes a copy of the element at the given index
3696 onto the stack.
3697
3698
3699
3700
3701
3702 <hr><h3><a name="lua_pushvfstring"><code>lua_pushvfstring</code></a></h3><p>
3703 <span class="apii">[-0, +1, <em>e</em>]</span>
3704 <pre>const char *lua_pushvfstring (lua_State *L,
3705 const char *fmt,
3706 va_list argp);</pre>
3707
3708 <p>
3709 Equivalent to <a href="#lua_pushfstring"><code>lua_pushfstring</code></a>, except that it receives a <code>va_list</code>
3710 instead of a variable number of arguments.
3711
3712
3713
3714
3715
3716 <hr><h3><a name="lua_rawequal"><code>lua_rawequal</code></a></h3><p>
3717 <span class="apii">[-0, +0, &ndash;]</span>
3718 <pre>int lua_rawequal (lua_State *L, int index1, int index2);</pre>
3719
3720 <p>
3721 Returns 1 if the two values in indices <code>index1</code> and
3722 <code>index2</code> are primitively equal
3723 (that is, without calling metamethods).
3724 Otherwise returns&nbsp;0.
3725 Also returns&nbsp;0 if any of the indices are not valid.
3726
3727
3728
3729
3730
3731 <hr><h3><a name="lua_rawget"><code>lua_rawget</code></a></h3><p>
3732 <span class="apii">[-1, +1, &ndash;]</span>
3733 <pre>int lua_rawget (lua_State *L, int index);</pre>
3734
3735 <p>
3736 Similar to <a href="#lua_gettable"><code>lua_gettable</code></a>, but does a raw access
3737 (i.e., without metamethods).
3738
3739
3740
3741
3742
3743 <hr><h3><a name="lua_rawgeti"><code>lua_rawgeti</code></a></h3><p>
3744 <span class="apii">[-0, +1, &ndash;]</span>
3745 <pre>int lua_rawgeti (lua_State *L, int index, lua_Integer n);</pre>
3746
3747 <p>
3748 Pushes onto the stack the value <code>t[n]</code>,
3749 where <code>t</code> is the table at the given index.
3750 The access is raw;
3751 that is, it does not invoke metamethods.
3752
3753
3754 <p>
3755 Returns the type of the pushed value.
3756
3757
3758
3759
3760
3761 <hr><h3><a name="lua_rawgetp"><code>lua_rawgetp</code></a></h3><p>
3762 <span class="apii">[-0, +1, &ndash;]</span>
3763 <pre>int lua_rawgetp (lua_State *L, int index, const void *p);</pre>
3764
3765 <p>
3766 Pushes onto the stack the value <code>t[k]</code>,
3767 where <code>t</code> is the table at the given index and
3768 <code>k</code> is the pointer <code>p</code> represented as a light userdata.
3769 The access is raw;
3770 that is, it does not invoke metamethods.
3771
3772
3773 <p>
3774 Returns the type of the pushed value.
3775
3776
3777
3778
3779
3780 <hr><h3><a name="lua_rawlen"><code>lua_rawlen</code></a></h3><p>
3781 <span class="apii">[-0, +0, &ndash;]</span>
3782 <pre>size_t lua_rawlen (lua_State *L, int index);</pre>
3783
3784 <p>
3785 Returns the raw "length" of the value at the given index:
3786 for strings, this is the string length;
3787 for tables, this is the result of the length operator ('<code>#</code>')
3788 with no metamethods;
3789 for userdata, this is the size of the block of memory allocated
3790 for the userdata;
3791 for other values, it is&nbsp;0.
3792
3793
3794
3795
3796
3797 <hr><h3><a name="lua_rawset"><code>lua_rawset</code></a></h3><p>
3798 <span class="apii">[-2, +0, <em>e</em>]</span>
3799 <pre>void lua_rawset (lua_State *L, int index);</pre>
3800
3801 <p>
3802 Similar to <a href="#lua_settable"><code>lua_settable</code></a>, but does a raw assignment
3803 (i.e., without metamethods).
3804
3805
3806
3807
3808
3809 <hr><h3><a name="lua_rawseti"><code>lua_rawseti</code></a></h3><p>
3810 <span class="apii">[-1, +0, <em>e</em>]</span>
3811 <pre>void lua_rawseti (lua_State *L, int index, lua_Integer i);</pre>
3812
3813 <p>
3814 Does the equivalent of <code>t[i] = v</code>,
3815 where <code>t</code> is the table at the given index
3816 and <code>v</code> is the value at the top of the stack.
3817
3818
3819 <p>
3820 This function pops the value from the stack.
3821 The assignment is raw;
3822 that is, it does not invoke metamethods.
3823
3824
3825
3826
3827
3828 <hr><h3><a name="lua_rawsetp"><code>lua_rawsetp</code></a></h3><p>
3829 <span class="apii">[-1, +0, <em>e</em>]</span>
3830 <pre>void lua_rawsetp (lua_State *L, int index, const void *p);</pre>
3831
3832 <p>
3833 Does the equivalent of <code>t[k] = v</code>,
3834 where <code>t</code> is the table at the given index,
3835 <code>k</code> is the pointer <code>p</code> represented as a light userdata,
3836 and <code>v</code> is the value at the top of the stack.
3837
3838
3839 <p>
3840 This function pops the value from the stack.
3841 The assignment is raw;
3842 that is, it does not invoke metamethods.
3843
3844
3845
3846
3847
3848 <hr><h3><a name="lua_Reader"><code>lua_Reader</code></a></h3>
3849 <pre>typedef const char * (*lua_Reader) (lua_State *L,
3850 void *data,
3851 size_t *size);</pre>
3852
3853 <p>
3854 The reader function used by <a href="#lua_load"><code>lua_load</code></a>.
3855 Every time it needs another piece of the chunk,
3856 <a href="#lua_load"><code>lua_load</code></a> calls the reader,
3857 passing along its <code>data</code> parameter.
3858 The reader must return a pointer to a block of memory
3859 with a new piece of the chunk
3860 and set <code>size</code> to the block size.
3861 The block must exist until the reader function is called again.
3862 To signal the end of the chunk,
3863 the reader must return <code>NULL</code> or set <code>size</code> to zero.
3864 The reader function may return pieces of any size greater than zero.
3865
3866
3867
3868
3869
3870 <hr><h3><a name="lua_register"><code>lua_register</code></a></h3><p>
3871 <span class="apii">[-0, +0, <em>e</em>]</span>
3872 <pre>void lua_register (lua_State *L, const char *name, lua_CFunction f);</pre>
3873
3874 <p>
3875 Sets the C function <code>f</code> as the new value of global <code>name</code>.
3876 It is defined as a macro:
3877
3878 <pre>
3879 #define lua_register(L,n,f) \
3880 (lua_pushcfunction(L, f), lua_setglobal(L, n))
3881 </pre>
3882
3883
3884
3885
3886 <hr><h3><a name="lua_remove"><code>lua_remove</code></a></h3><p>
3887 <span class="apii">[-1, +0, &ndash;]</span>
3888 <pre>void lua_remove (lua_State *L, int index);</pre>
3889
3890 <p>
3891 Removes the element at the given valid index,
3892 shifting down the elements above this index to fill the gap.
3893 This function cannot be called with a pseudo-index,
3894 because a pseudo-index is not an actual stack position.
3895
3896
3897
3898
3899
3900 <hr><h3><a name="lua_replace"><code>lua_replace</code></a></h3><p>
3901 <span class="apii">[-1, +0, &ndash;]</span>
3902 <pre>void lua_replace (lua_State *L, int index);</pre>
3903
3904 <p>
3905 Moves the top element into the given valid index
3906 without shifting any element
3907 (therefore replacing the value at the given index),
3908 and then pops the top element.
3909
3910
3911
3912
3913
3914 <hr><h3><a name="lua_resume"><code>lua_resume</code></a></h3><p>
3915 <span class="apii">[-?, +?, &ndash;]</span>
3916 <pre>int lua_resume (lua_State *L, lua_State *from, int nargs);</pre>
3917
3918 <p>
3919 Starts and resumes a coroutine in a given thread.
3920
3921
3922 <p>
3923 To start a coroutine,
3924 you push onto the thread stack the main function plus any arguments;
3925 then you call <a href="#lua_resume"><code>lua_resume</code></a>,
3926 with <code>nargs</code> being the number of arguments.
3927 This call returns when the coroutine suspends or finishes its execution.
3928 When it returns, the stack contains all values passed to <a href="#lua_yield"><code>lua_yield</code></a>,
3929 or all values returned by the body function.
3930 <a href="#lua_resume"><code>lua_resume</code></a> returns
3931 <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> if the coroutine yields,
3932 <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> if the coroutine finishes its execution
3933 without errors,
3934 or an error code in case of errors (see <a href="#lua_pcall"><code>lua_pcall</code></a>).
3935
3936
3937 <p>
3938 In case of errors,
3939 the stack is not unwound,
3940 so you can use the debug API over it.
3941 The error message is on the top of the stack.
3942
3943
3944 <p>
3945 To resume a coroutine,
3946 you remove any results from the last <a href="#lua_yield"><code>lua_yield</code></a>,
3947 put on its stack only the values to
3948 be passed as results from <code>yield</code>,
3949 and then call <a href="#lua_resume"><code>lua_resume</code></a>.
3950
3951
3952 <p>
3953 The parameter <code>from</code> represents the coroutine that is resuming <code>L</code>.
3954 If there is no such coroutine,
3955 this parameter can be <code>NULL</code>.
3956
3957
3958
3959
3960
3961 <hr><h3><a name="lua_rotate"><code>lua_rotate</code></a></h3><p>
3962 <span class="apii">[-0, +0, &ndash;]</span>
3963 <pre>void lua_rotate (lua_State *L, int idx, int n);</pre>
3964
3965 <p>
3966 Rotates the stack elements from <code>idx</code> to the top <code>n</code> positions
3967 in the direction of the top, for a positive <code>n</code>,
3968 or <code>-n</code> positions in the direction of the bottom,
3969 for a negative <code>n</code>.
3970 The absolute value of <code>n</code> must not be greater than the size
3971 of the slice being rotated.
3972
3973
3974
3975
3976
3977 <hr><h3><a name="lua_setallocf"><code>lua_setallocf</code></a></h3><p>
3978 <span class="apii">[-0, +0, &ndash;]</span>
3979 <pre>void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);</pre>
3980
3981 <p>
3982 Changes the allocator function of a given state to <code>f</code>
3983 with user data <code>ud</code>.
3984
3985
3986
3987
3988
3989 <hr><h3><a name="lua_setfield"><code>lua_setfield</code></a></h3><p>
3990 <span class="apii">[-1, +0, <em>e</em>]</span>
3991 <pre>void lua_setfield (lua_State *L, int index, const char *k);</pre>
3992
3993 <p>
3994 Does the equivalent to <code>t[k] = v</code>,
3995 where <code>t</code> is the value at the given index
3996 and <code>v</code> is the value at the top of the stack.
3997
3998
3999 <p>
4000 This function pops the value from the stack.
4001 As in Lua, this function may trigger a metamethod
4002 for the "newindex" event (see <a href="#2.4">&sect;2.4</a>).
4003
4004
4005
4006
4007
4008 <hr><h3><a name="lua_setglobal"><code>lua_setglobal</code></a></h3><p>
4009 <span class="apii">[-1, +0, <em>e</em>]</span>
4010 <pre>void lua_setglobal (lua_State *L, const char *name);</pre>
4011
4012 <p>
4013 Pops a value from the stack and
4014 sets it as the new value of global <code>name</code>.
4015
4016
4017
4018
4019
4020 <hr><h3><a name="lua_seti"><code>lua_seti</code></a></h3><p>
4021 <span class="apii">[-1, +0, <em>e</em>]</span>
4022 <pre>void lua_seti (lua_State *L, int index, lua_Integer n);</pre>
4023
4024 <p>
4025 Does the equivalent to <code>t[n] = v</code>,
4026 where <code>t</code> is the value at the given index
4027 and <code>v</code> is the value at the top of the stack.
4028
4029
4030 <p>
4031 This function pops the value from the stack.
4032 As in Lua, this function may trigger a metamethod
4033 for the "newindex" event (see <a href="#2.4">&sect;2.4</a>).
4034
4035
4036
4037
4038
4039 <hr><h3><a name="lua_setmetatable"><code>lua_setmetatable</code></a></h3><p>
4040 <span class="apii">[-1, +0, &ndash;]</span>
4041 <pre>void lua_setmetatable (lua_State *L, int index);</pre>
4042
4043 <p>
4044 Pops a table from the stack and
4045 sets it as the new metatable for the value at the given index.
4046
4047
4048
4049
4050
4051 <hr><h3><a name="lua_settable"><code>lua_settable</code></a></h3><p>
4052 <span class="apii">[-2, +0, <em>e</em>]</span>
4053 <pre>void lua_settable (lua_State *L, int index);</pre>
4054
4055 <p>
4056 Does the equivalent to <code>t[k] = v</code>,
4057 where <code>t</code> is the value at the given index,
4058 <code>v</code> is the value at the top of the stack,
4059 and <code>k</code> is the value just below the top.
4060
4061
4062 <p>
4063 This function pops both the key and the value from the stack.
4064 As in Lua, this function may trigger a metamethod
4065 for the "newindex" event (see <a href="#2.4">&sect;2.4</a>).
4066
4067
4068
4069
4070
4071 <hr><h3><a name="lua_settop"><code>lua_settop</code></a></h3><p>
4072 <span class="apii">[-?, +?, &ndash;]</span>
4073 <pre>void lua_settop (lua_State *L, int index);</pre>
4074
4075 <p>
4076 Accepts any index, or&nbsp;0,
4077 and sets the stack top to this index.
4078 If the new top is larger than the old one,
4079 then the new elements are filled with <b>nil</b>.
4080 If <code>index</code> is&nbsp;0, then all stack elements are removed.
4081
4082
4083
4084
4085
4086 <hr><h3><a name="lua_setuservalue"><code>lua_setuservalue</code></a></h3><p>
4087 <span class="apii">[-1, +0, &ndash;]</span>
4088 <pre>void lua_setuservalue (lua_State *L, int index);</pre>
4089
4090 <p>
4091 Pops a value from the stack and sets it as
4092 the new value associated to the userdata at the given index.
4093
4094
4095
4096
4097
4098 <hr><h3><a name="lua_State"><code>lua_State</code></a></h3>
4099 <pre>typedef struct lua_State lua_State;</pre>
4100
4101 <p>
4102 An opaque structure that points to a thread and indirectly
4103 (through the thread) to the whole state of a Lua interpreter.
4104 The Lua library is fully reentrant:
4105 it has no global variables.
4106 All information about a state is accessible through this structure.
4107
4108
4109 <p>
4110 A pointer to this structure must be passed as the first argument to
4111 every function in the library, except to <a href="#lua_newstate"><code>lua_newstate</code></a>,
4112 which creates a Lua state from scratch.
4113
4114
4115
4116
4117
4118 <hr><h3><a name="lua_status"><code>lua_status</code></a></h3><p>
4119 <span class="apii">[-0, +0, &ndash;]</span>
4120 <pre>int lua_status (lua_State *L);</pre>
4121
4122 <p>
4123 Returns the status of the thread <code>L</code>.
4124
4125
4126 <p>
4127 The status can be 0 (<a href="#pdf-LUA_OK"><code>LUA_OK</code></a>) for a normal thread,
4128 an error code if the thread finished the execution
4129 of a <a href="#lua_resume"><code>lua_resume</code></a> with an error,
4130 or <a name="pdf-LUA_YIELD"><code>LUA_YIELD</code></a> if the thread is suspended.
4131
4132
4133 <p>
4134 You can only call functions in threads with status <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>.
4135 You can resume threads with status <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>
4136 (to start a new coroutine) or <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a>
4137 (to resume a coroutine).
4138
4139
4140
4141
4142
4143 <hr><h3><a name="lua_stringtonumber"><code>lua_stringtonumber</code></a></h3><p>
4144 <span class="apii">[-0, +1, &ndash;]</span>
4145 <pre>size_t lua_stringtonumber (lua_State *L, const char *s);</pre>
4146
4147 <p>
4148 Converts the zero-terminated string <code>s</code> to a number,
4149 pushes that number into the stack,
4150 and returns the total size of the string,
4151 that is, its length plus one.
4152 The conversion can result in an integer or a float,
4153 according to the lexical conventions of Lua (see <a href="#3.1">&sect;3.1</a>).
4154 The string may have leading and trailing spaces and a sign.
4155 If the string is not a valid numeral,
4156 returns 0 and pushes nothing.
4157 (Note that the result can be used as a boolean,
4158 true if the conversion succeeds.)
4159
4160
4161
4162
4163
4164 <hr><h3><a name="lua_toboolean"><code>lua_toboolean</code></a></h3><p>
4165 <span class="apii">[-0, +0, &ndash;]</span>
4166 <pre>int lua_toboolean (lua_State *L, int index);</pre>
4167
4168 <p>
4169 Converts the Lua value at the given index to a C&nbsp;boolean
4170 value (0&nbsp;or&nbsp;1).
4171 Like all tests in Lua,
4172 <a href="#lua_toboolean"><code>lua_toboolean</code></a> returns true for any Lua value
4173 different from <b>false</b> and <b>nil</b>;
4174 otherwise it returns false.
4175 (If you want to accept only actual boolean values,
4176 use <a href="#lua_isboolean"><code>lua_isboolean</code></a> to test the value's type.)
4177
4178
4179
4180
4181
4182 <hr><h3><a name="lua_tocfunction"><code>lua_tocfunction</code></a></h3><p>
4183 <span class="apii">[-0, +0, &ndash;]</span>
4184 <pre>lua_CFunction lua_tocfunction (lua_State *L, int index);</pre>
4185
4186 <p>
4187 Converts a value at the given index to a C&nbsp;function.
4188 That value must be a C&nbsp;function;
4189 otherwise, returns <code>NULL</code>.
4190
4191
4192
4193
4194
4195 <hr><h3><a name="lua_tointeger"><code>lua_tointeger</code></a></h3><p>
4196 <span class="apii">[-0, +0, &ndash;]</span>
4197 <pre>lua_Integer lua_tointeger (lua_State *L, int index);</pre>
4198
4199 <p>
4200 Equivalent to <a href="#lua_tointegerx"><code>lua_tointegerx</code></a> with <code>isnum</code> equal to <code>NULL</code>.
4201
4202
4203
4204
4205
4206 <hr><h3><a name="lua_tointegerx"><code>lua_tointegerx</code></a></h3><p>
4207 <span class="apii">[-0, +0, &ndash;]</span>
4208 <pre>lua_Integer lua_tointegerx (lua_State *L, int index, int *isnum);</pre>
4209
4210 <p>
4211 Converts the Lua value at the given index
4212 to the signed integral type <a href="#lua_Integer"><code>lua_Integer</code></a>.
4213 The Lua value must be an integer,
4214 or a number or string convertible to an integer (see <a href="#3.4.3">&sect;3.4.3</a>);
4215 otherwise, <code>lua_tointegerx</code> returns&nbsp;0.
4216
4217
4218 <p>
4219 If <code>isnum</code> is not <code>NULL</code>,
4220 its referent is assigned a boolean value that
4221 indicates whether the operation succeeded.
4222
4223
4224
4225
4226
4227 <hr><h3><a name="lua_tolstring"><code>lua_tolstring</code></a></h3><p>
4228 <span class="apii">[-0, +0, <em>e</em>]</span>
4229 <pre>const char *lua_tolstring (lua_State *L, int index, size_t *len);</pre>
4230
4231 <p>
4232 Converts the Lua value at the given index to a C&nbsp;string.
4233 If <code>len</code> is not <code>NULL</code>,
4234 it also sets <code>*len</code> with the string length.
4235 The Lua value must be a string or a number;
4236 otherwise, the function returns <code>NULL</code>.
4237 If the value is a number,
4238 then <code>lua_tolstring</code> also
4239 <em>changes the actual value in the stack to a string</em>.
4240 (This change confuses <a href="#lua_next"><code>lua_next</code></a>
4241 when <code>lua_tolstring</code> is applied to keys during a table traversal.)
4242
4243
4244 <p>
4245 <code>lua_tolstring</code> returns a fully aligned pointer
4246 to a string inside the Lua state.
4247 This string always has a zero ('<code>\0</code>')
4248 after its last character (as in&nbsp;C),
4249 but can contain other zeros in its body.
4250
4251
4252 <p>
4253 Because Lua has garbage collection,
4254 there is no guarantee that the pointer returned by <code>lua_tolstring</code>
4255 will be valid after the corresponding Lua value is removed from the stack.
4256
4257
4258
4259
4260
4261 <hr><h3><a name="lua_tonumber"><code>lua_tonumber</code></a></h3><p>
4262 <span class="apii">[-0, +0, &ndash;]</span>
4263 <pre>lua_Number lua_tonumber (lua_State *L, int index);</pre>
4264
4265 <p>
4266 Equivalent to <a href="#lua_tonumberx"><code>lua_tonumberx</code></a> with <code>isnum</code> equal to <code>NULL</code>.
4267
4268
4269
4270
4271
4272 <hr><h3><a name="lua_tonumberx"><code>lua_tonumberx</code></a></h3><p>
4273 <span class="apii">[-0, +0, &ndash;]</span>
4274 <pre>lua_Number lua_tonumberx (lua_State *L, int index, int *isnum);</pre>
4275
4276 <p>
4277 Converts the Lua value at the given index
4278 to the C&nbsp;type <a href="#lua_Number"><code>lua_Number</code></a> (see <a href="#lua_Number"><code>lua_Number</code></a>).
4279 The Lua value must be a number or a string convertible to a number
4280 (see <a href="#3.4.3">&sect;3.4.3</a>);
4281 otherwise, <a href="#lua_tonumberx"><code>lua_tonumberx</code></a> returns&nbsp;0.
4282
4283
4284 <p>
4285 If <code>isnum</code> is not <code>NULL</code>,
4286 its referent is assigned a boolean value that
4287 indicates whether the operation succeeded.
4288
4289
4290
4291
4292
4293 <hr><h3><a name="lua_topointer"><code>lua_topointer</code></a></h3><p>
4294 <span class="apii">[-0, +0, &ndash;]</span>
4295 <pre>const void *lua_topointer (lua_State *L, int index);</pre>
4296
4297 <p>
4298 Converts the value at the given index to a generic
4299 C&nbsp;pointer (<code>void*</code>).
4300 The value can be a userdata, a table, a thread, or a function;
4301 otherwise, <code>lua_topointer</code> returns <code>NULL</code>.
4302 Different objects will give different pointers.
4303 There is no way to convert the pointer back to its original value.
4304
4305
4306 <p>
4307 Typically this function is used only for debug information.
4308
4309
4310
4311
4312
4313 <hr><h3><a name="lua_tostring"><code>lua_tostring</code></a></h3><p>
4314 <span class="apii">[-0, +0, <em>e</em>]</span>
4315 <pre>const char *lua_tostring (lua_State *L, int index);</pre>
4316
4317 <p>
4318 Equivalent to <a href="#lua_tolstring"><code>lua_tolstring</code></a> with <code>len</code> equal to <code>NULL</code>.
4319
4320
4321
4322
4323
4324 <hr><h3><a name="lua_tothread"><code>lua_tothread</code></a></h3><p>
4325 <span class="apii">[-0, +0, &ndash;]</span>
4326 <pre>lua_State *lua_tothread (lua_State *L, int index);</pre>
4327
4328 <p>
4329 Converts the value at the given index to a Lua thread
4330 (represented as <code>lua_State*</code>).
4331 This value must be a thread;
4332 otherwise, the function returns <code>NULL</code>.
4333
4334
4335
4336
4337
4338 <hr><h3><a name="lua_touserdata"><code>lua_touserdata</code></a></h3><p>
4339 <span class="apii">[-0, +0, &ndash;]</span>
4340 <pre>void *lua_touserdata (lua_State *L, int index);</pre>
4341
4342 <p>
4343 If the value at the given index is a full userdata,
4344 returns its block address.
4345 If the value is a light userdata,
4346 returns its pointer.
4347 Otherwise, returns <code>NULL</code>.
4348
4349
4350
4351
4352
4353 <hr><h3><a name="lua_type"><code>lua_type</code></a></h3><p>
4354 <span class="apii">[-0, +0, &ndash;]</span>
4355 <pre>int lua_type (lua_State *L, int index);</pre>
4356
4357 <p>
4358 Returns the type of the value in the given valid index,
4359 or <code>LUA_TNONE</code> for a non-valid (but acceptable) index.
4360 The types returned by <a href="#lua_type"><code>lua_type</code></a> are coded by the following constants
4361 defined in <code>lua.h</code>:
4362 <a name="pdf-LUA_TNIL"><code>LUA_TNIL</code></a>,
4363 <a name="pdf-LUA_TNUMBER"><code>LUA_TNUMBER</code></a>,
4364 <a name="pdf-LUA_TBOOLEAN"><code>LUA_TBOOLEAN</code></a>,
4365 <a name="pdf-LUA_TSTRING"><code>LUA_TSTRING</code></a>,
4366 <a name="pdf-LUA_TTABLE"><code>LUA_TTABLE</code></a>,
4367 <a name="pdf-LUA_TFUNCTION"><code>LUA_TFUNCTION</code></a>,
4368 <a name="pdf-LUA_TUSERDATA"><code>LUA_TUSERDATA</code></a>,
4369 <a name="pdf-LUA_TTHREAD"><code>LUA_TTHREAD</code></a>,
4370 and
4371 <a name="pdf-LUA_TLIGHTUSERDATA"><code>LUA_TLIGHTUSERDATA</code></a>.
4372
4373
4374
4375
4376
4377 <hr><h3><a name="lua_typename"><code>lua_typename</code></a></h3><p>
4378 <span class="apii">[-0, +0, &ndash;]</span>
4379 <pre>const char *lua_typename (lua_State *L, int tp);</pre>
4380
4381 <p>
4382 Returns the name of the type encoded by the value <code>tp</code>,
4383 which must be one the values returned by <a href="#lua_type"><code>lua_type</code></a>.
4384
4385
4386
4387
4388
4389 <hr><h3><a name="lua_Unsigned"><code>lua_Unsigned</code></a></h3>
4390 <pre>typedef ... lua_Unsigned;</pre>
4391
4392 <p>
4393 The unsigned version of <a href="#lua_Integer"><code>lua_Integer</code></a>.
4394
4395
4396
4397
4398
4399 <hr><h3><a name="lua_upvalueindex"><code>lua_upvalueindex</code></a></h3><p>
4400 <span class="apii">[-0, +0, &ndash;]</span>
4401 <pre>int lua_upvalueindex (int i);</pre>
4402
4403 <p>
4404 Returns the pseudo-index that represents the <code>i</code>-th upvalue of
4405 the running function (see <a href="#4.4">&sect;4.4</a>).
4406
4407
4408
4409
4410
4411 <hr><h3><a name="lua_version"><code>lua_version</code></a></h3><p>
4412 <span class="apii">[-0, +0, <em>v</em>]</span>
4413 <pre>const lua_Number *lua_version (lua_State *L);</pre>
4414
4415 <p>
4416 Returns the address of the version number stored in the Lua core.
4417 When called with a valid <a href="#lua_State"><code>lua_State</code></a>,
4418 returns the address of the version used to create that state.
4419 When called with <code>NULL</code>,
4420 returns the address of the version running the call.
4421
4422
4423
4424
4425
4426 <hr><h3><a name="lua_Writer"><code>lua_Writer</code></a></h3>
4427 <pre>typedef int (*lua_Writer) (lua_State *L,
4428 const void* p,
4429 size_t sz,
4430 void* ud);</pre>
4431
4432 <p>
4433 The type of the writer function used by <a href="#lua_dump"><code>lua_dump</code></a>.
4434 Every time it produces another piece of chunk,
4435 <a href="#lua_dump"><code>lua_dump</code></a> calls the writer,
4436 passing along the buffer to be written (<code>p</code>),
4437 its size (<code>sz</code>),
4438 and the <code>data</code> parameter supplied to <a href="#lua_dump"><code>lua_dump</code></a>.
4439
4440
4441 <p>
4442 The writer returns an error code:
4443 0&nbsp;means no errors;
4444 any other value means an error and stops <a href="#lua_dump"><code>lua_dump</code></a> from
4445 calling the writer again.
4446
4447
4448
4449
4450
4451 <hr><h3><a name="lua_xmove"><code>lua_xmove</code></a></h3><p>
4452 <span class="apii">[-?, +?, &ndash;]</span>
4453 <pre>void lua_xmove (lua_State *from, lua_State *to, int n);</pre>
4454
4455 <p>
4456 Exchange values between different threads of the same state.
4457
4458
4459 <p>
4460 This function pops <code>n</code> values from the stack <code>from</code>,
4461 and pushes them onto the stack <code>to</code>.
4462
4463
4464
4465
4466
4467 <hr><h3><a name="lua_yield"><code>lua_yield</code></a></h3><p>
4468 <span class="apii">[-?, +?, <em>e</em>]</span>
4469 <pre>int lua_yield (lua_State *L, int nresults);</pre>
4470
4471 <p>
4472 This function is equivalent to <a href="#lua_yieldk"><code>lua_yieldk</code></a>,
4473 but it has no continuation (see <a href="#4.7">&sect;4.7</a>).
4474 Therefore, when the thread resumes,
4475 it continues the function that called
4476 the function calling <code>lua_yield</code>.
4477
4478
4479
4480
4481
4482 <hr><h3><a name="lua_yieldk"><code>lua_yieldk</code></a></h3><p>
4483 <span class="apii">[-?, +?, <em>e</em>]</span>
4484 <pre>int lua_yieldk (lua_State *L,
4485 int nresults,
4486 lua_KContext ctx,
4487 lua_KFunction k);</pre>
4488
4489 <p>
4490 Yields a coroutine (thread).
4491
4492
4493 <p>
4494 When a C&nbsp;function calls <a href="#lua_yieldk"><code>lua_yieldk</code></a>,
4495 the running coroutine suspends its execution,
4496 and the call to <a href="#lua_resume"><code>lua_resume</code></a> that started this coroutine returns.
4497 The parameter <code>nresults</code> is the number of values from the stack
4498 that will be passed as results to <a href="#lua_resume"><code>lua_resume</code></a>.
4499
4500
4501 <p>
4502 When the coroutine is resumed again,
4503 Lua calls the given continuation function <code>k</code> to continue
4504 the execution of the C function that yielded (see <a href="#4.7">&sect;4.7</a>).
4505 This continuation function receives the same stack
4506 from the previous function,
4507 with the <code>n</code> results removed and
4508 replaced by the arguments passed to <a href="#lua_resume"><code>lua_resume</code></a>.
4509 Moreover,
4510 the continuation function receives the value <code>ctx</code>
4511 that was passed to <a href="#lua_yieldk"><code>lua_yieldk</code></a>.
4512
4513
4514 <p>
4515 Usually, this function does not return;
4516 when the coroutine eventually resumes,
4517 it continues executing the continuation function.
4518 However, there is one special case,
4519 which is when this function is called
4520 from inside a line hook (see <a href="#4.9">&sect;4.9</a>).
4521 In that case, <code>lua_yieldk</code> should be called with no continuation
4522 (probably in the form of <a href="#lua_yield"><code>lua_yield</code></a>),
4523 and the hook should return immediately after the call.
4524 Lua will yield and,
4525 when the coroutine resumes again,
4526 it will continue the normal execution
4527 of the (Lua) function that triggered the hook.
4528
4529
4530 <p>
4531 This function can raise an error if it is called from a thread
4532 with a pending C call with no continuation function,
4533 or it is called from a thread that is not running inside a resume
4534 (e.g., the main thread).
4535
4536
4537
4538
4539
4540
4541
4542 <h2>4.9 &ndash; <a name="4.9">The Debug Interface</a></h2>
4543
4544 <p>
4545 Lua has no built-in debugging facilities.
4546 Instead, it offers a special interface
4547 by means of functions and <em>hooks</em>.
4548 This interface allows the construction of different
4549 kinds of debuggers, profilers, and other tools
4550 that need "inside information" from the interpreter.
4551
4552
4553
4554 <hr><h3><a name="lua_Debug"><code>lua_Debug</code></a></h3>
4555 <pre>typedef struct lua_Debug {
4556 int event;
4557 const char *name; /* (n) */
4558 const char *namewhat; /* (n) */
4559 const char *what; /* (S) */
4560 const char *source; /* (S) */
4561 int currentline; /* (l) */
4562 int linedefined; /* (S) */
4563 int lastlinedefined; /* (S) */
4564 unsigned char nups; /* (u) number of upvalues */
4565 unsigned char nparams; /* (u) number of parameters */
4566 char isvararg; /* (u) */
4567 char istailcall; /* (t) */
4568 char short_src[LUA_IDSIZE]; /* (S) */
4569 /* private part */
4570 <em>other fields</em>
4571 } lua_Debug;</pre>
4572
4573 <p>
4574 A structure used to carry different pieces of
4575 information about a function or an activation record.
4576 <a href="#lua_getstack"><code>lua_getstack</code></a> fills only the private part
4577 of this structure, for later use.
4578 To fill the other fields of <a href="#lua_Debug"><code>lua_Debug</code></a> with useful information,
4579 call <a href="#lua_getinfo"><code>lua_getinfo</code></a>.
4580
4581
4582 <p>
4583 The fields of <a href="#lua_Debug"><code>lua_Debug</code></a> have the following meaning:
4584
4585 <ul>
4586
4587 <li><b><code>source</code>: </b>
4588 the name of the chunk that created the function.
4589 If <code>source</code> starts with a '<code>@</code>',
4590 it means that the function was defined in a file where
4591 the file name follows the '<code>@</code>'.
4592 If <code>source</code> starts with a '<code>=</code>',
4593 the remainder of its contents describe the source in a user-dependent manner.
4594 Otherwise,
4595 the function was defined in a string where
4596 <code>source</code> is that string.
4597 </li>
4598
4599 <li><b><code>short_src</code>: </b>
4600 a "printable" version of <code>source</code>, to be used in error messages.
4601 </li>
4602
4603 <li><b><code>linedefined</code>: </b>
4604 the line number where the definition of the function starts.
4605 </li>
4606
4607 <li><b><code>lastlinedefined</code>: </b>
4608 the line number where the definition of the function ends.
4609 </li>
4610
4611 <li><b><code>what</code>: </b>
4612 the string <code>"Lua"</code> if the function is a Lua function,
4613 <code>"C"</code> if it is a C&nbsp;function,
4614 <code>"main"</code> if it is the main part of a chunk.
4615 </li>
4616
4617 <li><b><code>currentline</code>: </b>
4618 the current line where the given function is executing.
4619 When no line information is available,
4620 <code>currentline</code> is set to -1.
4621 </li>
4622
4623 <li><b><code>name</code>: </b>
4624 a reasonable name for the given function.
4625 Because functions in Lua are first-class values,
4626 they do not have a fixed name:
4627 some functions can be the value of multiple global variables,
4628 while others can be stored only in a table field.
4629 The <code>lua_getinfo</code> function checks how the function was
4630 called to find a suitable name.
4631 If it cannot find a name,
4632 then <code>name</code> is set to <code>NULL</code>.
4633 </li>
4634
4635 <li><b><code>namewhat</code>: </b>
4636 explains the <code>name</code> field.
4637 The value of <code>namewhat</code> can be
4638 <code>"global"</code>, <code>"local"</code>, <code>"method"</code>,
4639 <code>"field"</code>, <code>"upvalue"</code>, or <code>""</code> (the empty string),
4640 according to how the function was called.
4641 (Lua uses the empty string when no other option seems to apply.)
4642 </li>
4643
4644 <li><b><code>istailcall</code>: </b>
4645 true if this function invocation was called by a tail call.
4646 In this case, the caller of this level is not in the stack.
4647 </li>
4648
4649 <li><b><code>nups</code>: </b>
4650 the number of upvalues of the function.
4651 </li>
4652
4653 <li><b><code>nparams</code>: </b>
4654 the number of fixed parameters of the function
4655 (always 0&nbsp;for C&nbsp;functions).
4656 </li>
4657
4658 <li><b><code>isvararg</code>: </b>
4659 true if the function is a vararg function
4660 (always true for C&nbsp;functions).
4661 </li>
4662
4663 </ul>
4664
4665
4666
4667
4668 <hr><h3><a name="lua_gethook"><code>lua_gethook</code></a></h3><p>
4669 <span class="apii">[-0, +0, &ndash;]</span>
4670 <pre>lua_Hook lua_gethook (lua_State *L);</pre>
4671
4672 <p>
4673 Returns the current hook function.
4674
4675
4676
4677
4678
4679 <hr><h3><a name="lua_gethookcount"><code>lua_gethookcount</code></a></h3><p>
4680 <span class="apii">[-0, +0, &ndash;]</span>
4681 <pre>int lua_gethookcount (lua_State *L);</pre>
4682
4683 <p>
4684 Returns the current hook count.
4685
4686
4687
4688
4689
4690 <hr><h3><a name="lua_gethookmask"><code>lua_gethookmask</code></a></h3><p>
4691 <span class="apii">[-0, +0, &ndash;]</span>
4692 <pre>int lua_gethookmask (lua_State *L);</pre>
4693
4694 <p>
4695 Returns the current hook mask.
4696
4697
4698
4699
4700
4701 <hr><h3><a name="lua_getinfo"><code>lua_getinfo</code></a></h3><p>
4702 <span class="apii">[-(0|1), +(0|1|2), <em>e</em>]</span>
4703 <pre>int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);</pre>
4704
4705 <p>
4706 Gets information about a specific function or function invocation.
4707
4708
4709 <p>
4710 To get information about a function invocation,
4711 the parameter <code>ar</code> must be a valid activation record that was
4712 filled by a previous call to <a href="#lua_getstack"><code>lua_getstack</code></a> or
4713 given as argument to a hook (see <a href="#lua_Hook"><code>lua_Hook</code></a>).
4714
4715
4716 <p>
4717 To get information about a function you push it onto the stack
4718 and start the <code>what</code> string with the character '<code>&gt;</code>'.
4719 (In that case,
4720 <code>lua_getinfo</code> pops the function from the top of the stack.)
4721 For instance, to know in which line a function <code>f</code> was defined,
4722 you can write the following code:
4723
4724 <pre>
4725 lua_Debug ar;
4726 lua_getglobal(L, "f"); /* get global 'f' */
4727 lua_getinfo(L, "&gt;S", &amp;ar);
4728 printf("%d\n", ar.linedefined);
4729 </pre>
4730
4731 <p>
4732 Each character in the string <code>what</code>
4733 selects some fields of the structure <code>ar</code> to be filled or
4734 a value to be pushed on the stack:
4735
4736 <ul>
4737
4738 <li><b>'<code>n</code>': </b> fills in the field <code>name</code> and <code>namewhat</code>;
4739 </li>
4740
4741 <li><b>'<code>S</code>': </b>
4742 fills in the fields <code>source</code>, <code>short_src</code>,
4743 <code>linedefined</code>, <code>lastlinedefined</code>, and <code>what</code>;
4744 </li>
4745
4746 <li><b>'<code>l</code>': </b> fills in the field <code>currentline</code>;
4747 </li>
4748
4749 <li><b>'<code>t</code>': </b> fills in the field <code>istailcall</code>;
4750 </li>
4751
4752 <li><b>'<code>u</code>': </b> fills in the fields
4753 <code>nups</code>, <code>nparams</code>, and <code>isvararg</code>;
4754 </li>
4755
4756 <li><b>'<code>f</code>': </b>
4757 pushes onto the stack the function that is
4758 running at the given level;
4759 </li>
4760
4761 <li><b>'<code>L</code>': </b>
4762 pushes onto the stack a table whose indices are the
4763 numbers of the lines that are valid on the function.
4764 (A <em>valid line</em> is a line with some associated code,
4765 that is, a line where you can put a break point.
4766 Non-valid lines include empty lines and comments.)
4767
4768
4769 <p>
4770 If this option is given together with option '<code>f</code>',
4771 its table is pushed after the function.
4772 </li>
4773
4774 </ul>
4775
4776 <p>
4777 This function returns 0 on error
4778 (for instance, an invalid option in <code>what</code>).
4779
4780
4781
4782
4783
4784 <hr><h3><a name="lua_getlocal"><code>lua_getlocal</code></a></h3><p>
4785 <span class="apii">[-0, +(0|1), &ndash;]</span>
4786 <pre>const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n);</pre>
4787
4788 <p>
4789 Gets information about a local variable of
4790 a given activation record or a given function.
4791
4792
4793 <p>
4794 In the first case,
4795 the parameter <code>ar</code> must be a valid activation record that was
4796 filled by a previous call to <a href="#lua_getstack"><code>lua_getstack</code></a> or
4797 given as argument to a hook (see <a href="#lua_Hook"><code>lua_Hook</code></a>).
4798 The index <code>n</code> selects which local variable to inspect;
4799 see <a href="#pdf-debug.getlocal"><code>debug.getlocal</code></a> for details about variable indices
4800 and names.
4801
4802
4803 <p>
4804 <a href="#lua_getlocal"><code>lua_getlocal</code></a> pushes the variable's value onto the stack
4805 and returns its name.
4806
4807
4808 <p>
4809 In the second case, <code>ar</code> must be <code>NULL</code> and the function
4810 to be inspected must be at the top of the stack.
4811 In this case, only parameters of Lua functions are visible
4812 (as there is no information about what variables are active)
4813 and no values are pushed onto the stack.
4814
4815
4816 <p>
4817 Returns <code>NULL</code> (and pushes nothing)
4818 when the index is greater than
4819 the number of active local variables.
4820
4821
4822
4823
4824
4825 <hr><h3><a name="lua_getstack"><code>lua_getstack</code></a></h3><p>
4826 <span class="apii">[-0, +0, &ndash;]</span>
4827 <pre>int lua_getstack (lua_State *L, int level, lua_Debug *ar);</pre>
4828
4829 <p>
4830 Gets information about the interpreter runtime stack.
4831
4832
4833 <p>
4834 This function fills parts of a <a href="#lua_Debug"><code>lua_Debug</code></a> structure with
4835 an identification of the <em>activation record</em>
4836 of the function executing at a given level.
4837 Level&nbsp;0 is the current running function,
4838 whereas level <em>n+1</em> is the function that has called level <em>n</em>
4839 (except for tail calls, which do not count on the stack).
4840 When there are no errors, <a href="#lua_getstack"><code>lua_getstack</code></a> returns 1;
4841 when called with a level greater than the stack depth,
4842 it returns 0.
4843
4844
4845
4846
4847
4848 <hr><h3><a name="lua_getupvalue"><code>lua_getupvalue</code></a></h3><p>
4849 <span class="apii">[-0, +(0|1), &ndash;]</span>
4850 <pre>const char *lua_getupvalue (lua_State *L, int funcindex, int n);</pre>
4851
4852 <p>
4853 Gets information about a closure's upvalue.
4854 (For Lua functions,
4855 upvalues are the external local variables that the function uses,
4856 and that are consequently included in its closure.)
4857 <a href="#lua_getupvalue"><code>lua_getupvalue</code></a> gets the index <code>n</code> of an upvalue,
4858 pushes the upvalue's value onto the stack,
4859 and returns its name.
4860 <code>funcindex</code> points to the closure in the stack.
4861 (Upvalues have no particular order,
4862 as they are active through the whole function.
4863 So, they are numbered in an arbitrary order.)
4864
4865
4866 <p>
4867 Returns <code>NULL</code> (and pushes nothing)
4868 when the index is greater than the number of upvalues.
4869 For C&nbsp;functions, this function uses the empty string <code>""</code>
4870 as a name for all upvalues.
4871
4872
4873
4874
4875
4876 <hr><h3><a name="lua_Hook"><code>lua_Hook</code></a></h3>
4877 <pre>typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);</pre>
4878
4879 <p>
4880 Type for debugging hook functions.
4881
4882
4883 <p>
4884 Whenever a hook is called, its <code>ar</code> argument has its field
4885 <code>event</code> set to the specific event that triggered the hook.
4886 Lua identifies these events with the following constants:
4887 <a name="pdf-LUA_HOOKCALL"><code>LUA_HOOKCALL</code></a>, <a name="pdf-LUA_HOOKRET"><code>LUA_HOOKRET</code></a>,
4888 <a name="pdf-LUA_HOOKTAILCALL"><code>LUA_HOOKTAILCALL</code></a>, <a name="pdf-LUA_HOOKLINE"><code>LUA_HOOKLINE</code></a>,
4889 and <a name="pdf-LUA_HOOKCOUNT"><code>LUA_HOOKCOUNT</code></a>.
4890 Moreover, for line events, the field <code>currentline</code> is also set.
4891 To get the value of any other field in <code>ar</code>,
4892 the hook must call <a href="#lua_getinfo"><code>lua_getinfo</code></a>.
4893
4894
4895 <p>
4896 For call events, <code>event</code> can be <code>LUA_HOOKCALL</code>,
4897 the normal value, or <code>LUA_HOOKTAILCALL</code>, for a tail call;
4898 in this case, there will be no corresponding return event.
4899
4900
4901 <p>
4902 While Lua is running a hook, it disables other calls to hooks.
4903 Therefore, if a hook calls back Lua to execute a function or a chunk,
4904 this execution occurs without any calls to hooks.
4905
4906
4907 <p>
4908 Hook functions cannot have continuations,
4909 that is, they cannot call <a href="#lua_yieldk"><code>lua_yieldk</code></a>,
4910 <a href="#lua_pcallk"><code>lua_pcallk</code></a>, or <a href="#lua_callk"><code>lua_callk</code></a> with a non-null <code>k</code>.
4911
4912
4913 <p>
4914 Hook functions can yield under the following conditions:
4915 Only count and line events can yield
4916 and they cannot yield any value;
4917 to yield a hook function must finish its execution
4918 calling <a href="#lua_yield"><code>lua_yield</code></a> with <code>nresults</code> equal to zero.
4919
4920
4921
4922
4923
4924 <hr><h3><a name="lua_sethook"><code>lua_sethook</code></a></h3><p>
4925 <span class="apii">[-0, +0, &ndash;]</span>
4926 <pre>void lua_sethook (lua_State *L, lua_Hook f, int mask, int count);</pre>
4927
4928 <p>
4929 Sets the debugging hook function.
4930
4931
4932 <p>
4933 Argument <code>f</code> is the hook function.
4934 <code>mask</code> specifies on which events the hook will be called:
4935 it is formed by a bitwise or of the constants
4936 <a name="pdf-LUA_MASKCALL"><code>LUA_MASKCALL</code></a>,
4937 <a name="pdf-LUA_MASKRET"><code>LUA_MASKRET</code></a>,
4938 <a name="pdf-LUA_MASKLINE"><code>LUA_MASKLINE</code></a>,
4939 and <a name="pdf-LUA_MASKCOUNT"><code>LUA_MASKCOUNT</code></a>.
4940 The <code>count</code> argument is only meaningful when the mask
4941 includes <code>LUA_MASKCOUNT</code>.
4942 For each event, the hook is called as explained below:
4943
4944 <ul>
4945
4946 <li><b>The call hook: </b> is called when the interpreter calls a function.
4947 The hook is called just after Lua enters the new function,
4948 before the function gets its arguments.
4949 </li>
4950
4951 <li><b>The return hook: </b> is called when the interpreter returns from a function.
4952 The hook is called just before Lua leaves the function.
4953 There is no standard way to access the values
4954 to be returned by the function.
4955 </li>
4956
4957 <li><b>The line hook: </b> is called when the interpreter is about to
4958 start the execution of a new line of code,
4959 or when it jumps back in the code (even to the same line).
4960 (This event only happens while Lua is executing a Lua function.)
4961 </li>
4962
4963 <li><b>The count hook: </b> is called after the interpreter executes every
4964 <code>count</code> instructions.
4965 (This event only happens while Lua is executing a Lua function.)
4966 </li>
4967
4968 </ul>
4969
4970 <p>
4971 A hook is disabled by setting <code>mask</code> to zero.
4972
4973
4974
4975
4976
4977 <hr><h3><a name="lua_setlocal"><code>lua_setlocal</code></a></h3><p>
4978 <span class="apii">[-(0|1), +0, &ndash;]</span>
4979 <pre>const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n);</pre>
4980
4981 <p>
4982 Sets the value of a local variable of a given activation record.
4983 Parameters <code>ar</code> and <code>n</code> are as in <a href="#lua_getlocal"><code>lua_getlocal</code></a>
4984 (see <a href="#lua_getlocal"><code>lua_getlocal</code></a>).
4985 <a href="#lua_setlocal"><code>lua_setlocal</code></a> assigns the value at the top of the stack
4986 to the variable and returns its name.
4987 It also pops the value from the stack.
4988
4989
4990 <p>
4991 Returns <code>NULL</code> (and pops nothing)
4992 when the index is greater than
4993 the number of active local variables.
4994
4995
4996
4997
4998
4999 <hr><h3><a name="lua_setupvalue"><code>lua_setupvalue</code></a></h3><p>
5000 <span class="apii">[-(0|1), +0, &ndash;]</span>
5001 <pre>const char *lua_setupvalue (lua_State *L, int funcindex, int n);</pre>
5002
5003 <p>
5004 Sets the value of a closure's upvalue.
5005 It assigns the value at the top of the stack
5006 to the upvalue and returns its name.
5007 It also pops the value from the stack.
5008 Parameters <code>funcindex</code> and <code>n</code> are as in the <a href="#lua_getupvalue"><code>lua_getupvalue</code></a>
5009 (see <a href="#lua_getupvalue"><code>lua_getupvalue</code></a>).
5010
5011
5012 <p>
5013 Returns <code>NULL</code> (and pops nothing)
5014 when the index is greater than the number of upvalues.
5015
5016
5017
5018
5019
5020 <hr><h3><a name="lua_upvalueid"><code>lua_upvalueid</code></a></h3><p>
5021 <span class="apii">[-0, +0, &ndash;]</span>
5022 <pre>void *lua_upvalueid (lua_State *L, int funcindex, int n);</pre>
5023
5024 <p>
5025 Returns a unique identifier for the upvalue numbered <code>n</code>
5026 from the closure at index <code>funcindex</code>.
5027 Parameters <code>funcindex</code> and <code>n</code> are as in the <a href="#lua_getupvalue"><code>lua_getupvalue</code></a>
5028 (see <a href="#lua_getupvalue"><code>lua_getupvalue</code></a>)
5029 (but <code>n</code> cannot be greater than the number of upvalues).
5030
5031
5032 <p>
5033 These unique identifiers allow a program to check whether different
5034 closures share upvalues.
5035 Lua closures that share an upvalue
5036 (that is, that access a same external local variable)
5037 will return identical ids for those upvalue indices.
5038
5039
5040
5041
5042
5043 <hr><h3><a name="lua_upvaluejoin"><code>lua_upvaluejoin</code></a></h3><p>
5044 <span class="apii">[-0, +0, &ndash;]</span>
5045 <pre>void lua_upvaluejoin (lua_State *L, int funcindex1, int n1,
5046 int funcindex2, int n2);</pre>
5047
5048 <p>
5049 Make the <code>n1</code>-th upvalue of the Lua closure at index <code>funcindex1</code>
5050 refer to the <code>n2</code>-th upvalue of the Lua closure at index <code>funcindex2</code>.
5051
5052
5053
5054
5055
5056
5057
5058 <h1>5 &ndash; <a name="5">The Auxiliary Library</a></h1>
5059
5060 <p>
5061
5062 The <em>auxiliary library</em> provides several convenient functions
5063 to interface C with Lua.
5064 While the basic API provides the primitive functions for all
5065 interactions between C and Lua,
5066 the auxiliary library provides higher-level functions for some
5067 common tasks.
5068
5069
5070 <p>
5071 All functions and types from the auxiliary library
5072 are defined in header file <code>lauxlib.h</code> and
5073 have a prefix <code>luaL_</code>.
5074
5075
5076 <p>
5077 All functions in the auxiliary library are built on
5078 top of the basic API,
5079 and so they provide nothing that cannot be done with that API.
5080 Nevertheless, the use of the auxiliary library ensures
5081 more consistency to your code.
5082
5083
5084 <p>
5085 Several functions in the auxiliary library use internally some
5086 extra stack slots.
5087 When a function in the auxiliary library uses less than five slots,
5088 it does not check the stack size;
5089 it simply assumes that there are enough slots.
5090
5091
5092 <p>
5093 Several functions in the auxiliary library are used to
5094 check C&nbsp;function arguments.
5095 Because the error message is formatted for arguments
5096 (e.g., "<code>bad argument #1</code>"),
5097 you should not use these functions for other stack values.
5098
5099
5100 <p>
5101 Functions called <code>luaL_check*</code>
5102 always raise an error if the check is not satisfied.
5103
5104
5105
5106 <h2>5.1 &ndash; <a name="5.1">Functions and Types</a></h2>
5107
5108 <p>
5109 Here we list all functions and types from the auxiliary library
5110 in alphabetical order.
5111
5112
5113
5114 <hr><h3><a name="luaL_addchar"><code>luaL_addchar</code></a></h3><p>
5115 <span class="apii">[-?, +?, <em>e</em>]</span>
5116 <pre>void luaL_addchar (luaL_Buffer *B, char c);</pre>
5117
5118 <p>
5119 Adds the byte <code>c</code> to the buffer <code>B</code>
5120 (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
5121
5122
5123
5124
5125
5126 <hr><h3><a name="luaL_addlstring"><code>luaL_addlstring</code></a></h3><p>
5127 <span class="apii">[-?, +?, <em>e</em>]</span>
5128 <pre>void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l);</pre>
5129
5130 <p>
5131 Adds the string pointed to by <code>s</code> with length <code>l</code> to
5132 the buffer <code>B</code>
5133 (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
5134 The string can contain embedded zeros.
5135
5136
5137
5138
5139
5140 <hr><h3><a name="luaL_addsize"><code>luaL_addsize</code></a></h3><p>
5141 <span class="apii">[-?, +?, <em>e</em>]</span>
5142 <pre>void luaL_addsize (luaL_Buffer *B, size_t n);</pre>
5143
5144 <p>
5145 Adds to the buffer <code>B</code> (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>)
5146 a string of length <code>n</code> previously copied to the
5147 buffer area (see <a href="#luaL_prepbuffer"><code>luaL_prepbuffer</code></a>).
5148
5149
5150
5151
5152
5153 <hr><h3><a name="luaL_addstring"><code>luaL_addstring</code></a></h3><p>
5154 <span class="apii">[-?, +?, <em>e</em>]</span>
5155 <pre>void luaL_addstring (luaL_Buffer *B, const char *s);</pre>
5156
5157 <p>
5158 Adds the zero-terminated string pointed to by <code>s</code>
5159 to the buffer <code>B</code>
5160 (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
5161
5162
5163
5164
5165
5166 <hr><h3><a name="luaL_addvalue"><code>luaL_addvalue</code></a></h3><p>
5167 <span class="apii">[-1, +?, <em>e</em>]</span>
5168 <pre>void luaL_addvalue (luaL_Buffer *B);</pre>
5169
5170 <p>
5171 Adds the value at the top of the stack
5172 to the buffer <code>B</code>
5173 (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
5174 Pops the value.
5175
5176
5177 <p>
5178 This is the only function on string buffers that can (and must)
5179 be called with an extra element on the stack,
5180 which is the value to be added to the buffer.
5181
5182
5183
5184
5185
5186 <hr><h3><a name="luaL_argcheck"><code>luaL_argcheck</code></a></h3><p>
5187 <span class="apii">[-0, +0, <em>v</em>]</span>
5188 <pre>void luaL_argcheck (lua_State *L,
5189 int cond,
5190 int arg,
5191 const char *extramsg);</pre>
5192
5193 <p>
5194 Checks whether <code>cond</code> is true.
5195 If it is not, raises an error with a standard message (see <a href="#luaL_argerror"><code>luaL_argerror</code></a>).
5196
5197
5198
5199
5200
5201 <hr><h3><a name="luaL_argerror"><code>luaL_argerror</code></a></h3><p>
5202 <span class="apii">[-0, +0, <em>v</em>]</span>
5203 <pre>int luaL_argerror (lua_State *L, int arg, const char *extramsg);</pre>
5204
5205 <p>
5206 Raises an error reporting a problem with argument <code>arg</code>
5207 of the C function that called it,
5208 using a standard message
5209 that includes <code>extramsg</code> as a comment:
5210
5211 <pre>
5212 bad argument #<em>arg</em> to '<em>funcname</em>' (<em>extramsg</em>)
5213 </pre><p>
5214 This function never returns.
5215
5216
5217
5218
5219
5220 <hr><h3><a name="luaL_Buffer"><code>luaL_Buffer</code></a></h3>
5221 <pre>typedef struct luaL_Buffer luaL_Buffer;</pre>
5222
5223 <p>
5224 Type for a <em>string buffer</em>.
5225
5226
5227 <p>
5228 A string buffer allows C&nbsp;code to build Lua strings piecemeal.
5229 Its pattern of use is as follows:
5230
5231 <ul>
5232
5233 <li>First declare a variable <code>b</code> of type <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>.</li>
5234
5235 <li>Then initialize it with a call <code>luaL_buffinit(L, &amp;b)</code>.</li>
5236
5237 <li>
5238 Then add string pieces to the buffer calling any of
5239 the <code>luaL_add*</code> functions.
5240 </li>
5241
5242 <li>
5243 Finish by calling <code>luaL_pushresult(&amp;b)</code>.
5244 This call leaves the final string on the top of the stack.
5245 </li>
5246
5247 </ul>
5248
5249 <p>
5250 If you know beforehand the total size of the resulting string,
5251 you can use the buffer like this:
5252
5253 <ul>
5254
5255 <li>First declare a variable <code>b</code> of type <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>.</li>
5256
5257 <li>Then initialize it and preallocate a space of
5258 size <code>sz</code> with a call <code>luaL_buffinitsize(L, &amp;b, sz)</code>.</li>
5259
5260 <li>Then copy the string into that space.</li>
5261
5262 <li>
5263 Finish by calling <code>luaL_pushresultsize(&amp;b, sz)</code>,
5264 where <code>sz</code> is the total size of the resulting string
5265 copied into that space.
5266 </li>
5267
5268 </ul>
5269
5270 <p>
5271 During its normal operation,
5272 a string buffer uses a variable number of stack slots.
5273 So, while using a buffer, you cannot assume that you know where
5274 the top of the stack is.
5275 You can use the stack between successive calls to buffer operations
5276 as long as that use is balanced;
5277 that is,
5278 when you call a buffer operation,
5279 the stack is at the same level
5280 it was immediately after the previous buffer operation.
5281 (The only exception to this rule is <a href="#luaL_addvalue"><code>luaL_addvalue</code></a>.)
5282 After calling <a href="#luaL_pushresult"><code>luaL_pushresult</code></a> the stack is back to its
5283 level when the buffer was initialized,
5284 plus the final string on its top.
5285
5286
5287
5288
5289
5290 <hr><h3><a name="luaL_buffinit"><code>luaL_buffinit</code></a></h3><p>
5291 <span class="apii">[-0, +0, &ndash;]</span>
5292 <pre>void luaL_buffinit (lua_State *L, luaL_Buffer *B);</pre>
5293
5294 <p>
5295 Initializes a buffer <code>B</code>.
5296 This function does not allocate any space;
5297 the buffer must be declared as a variable
5298 (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
5299
5300
5301
5302
5303
5304 <hr><h3><a name="luaL_buffinitsize"><code>luaL_buffinitsize</code></a></h3><p>
5305 <span class="apii">[-?, +?, <em>e</em>]</span>
5306 <pre>char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz);</pre>
5307
5308 <p>
5309 Equivalent to the sequence
5310 <a href="#luaL_buffinit"><code>luaL_buffinit</code></a>, <a href="#luaL_prepbuffsize"><code>luaL_prepbuffsize</code></a>.
5311
5312
5313
5314
5315
5316 <hr><h3><a name="luaL_callmeta"><code>luaL_callmeta</code></a></h3><p>
5317 <span class="apii">[-0, +(0|1), <em>e</em>]</span>
5318 <pre>int luaL_callmeta (lua_State *L, int obj, const char *e);</pre>
5319
5320 <p>
5321 Calls a metamethod.
5322
5323
5324 <p>
5325 If the object at index <code>obj</code> has a metatable and this
5326 metatable has a field <code>e</code>,
5327 this function calls this field passing the object as its only argument.
5328 In this case this function returns true and pushes onto the
5329 stack the value returned by the call.
5330 If there is no metatable or no metamethod,
5331 this function returns false (without pushing any value on the stack).
5332
5333
5334
5335
5336
5337 <hr><h3><a name="luaL_checkany"><code>luaL_checkany</code></a></h3><p>
5338 <span class="apii">[-0, +0, <em>v</em>]</span>
5339 <pre>void luaL_checkany (lua_State *L, int arg);</pre>
5340
5341 <p>
5342 Checks whether the function has an argument
5343 of any type (including <b>nil</b>) at position <code>arg</code>.
5344
5345
5346
5347
5348
5349 <hr><h3><a name="luaL_checkinteger"><code>luaL_checkinteger</code></a></h3><p>
5350 <span class="apii">[-0, +0, <em>v</em>]</span>
5351 <pre>lua_Integer luaL_checkinteger (lua_State *L, int arg);</pre>
5352
5353 <p>
5354 Checks whether the function argument <code>arg</code> is an integer
5355 (or can be converted to an integer)
5356 and returns this integer cast to a <a href="#lua_Integer"><code>lua_Integer</code></a>.
5357
5358
5359
5360
5361
5362 <hr><h3><a name="luaL_checklstring"><code>luaL_checklstring</code></a></h3><p>
5363 <span class="apii">[-0, +0, <em>v</em>]</span>
5364 <pre>const char *luaL_checklstring (lua_State *L, int arg, size_t *l);</pre>
5365
5366 <p>
5367 Checks whether the function argument <code>arg</code> is a string
5368 and returns this string;
5369 if <code>l</code> is not <code>NULL</code> fills <code>*l</code>
5370 with the string's length.
5371
5372
5373 <p>
5374 This function uses <a href="#lua_tolstring"><code>lua_tolstring</code></a> to get its result,
5375 so all conversions and caveats of that function apply here.
5376
5377
5378
5379
5380
5381 <hr><h3><a name="luaL_checknumber"><code>luaL_checknumber</code></a></h3><p>
5382 <span class="apii">[-0, +0, <em>v</em>]</span>
5383 <pre>lua_Number luaL_checknumber (lua_State *L, int arg);</pre>
5384
5385 <p>
5386 Checks whether the function argument <code>arg</code> is a number
5387 and returns this number.
5388
5389
5390
5391
5392
5393 <hr><h3><a name="luaL_checkoption"><code>luaL_checkoption</code></a></h3><p>
5394 <span class="apii">[-0, +0, <em>v</em>]</span>
5395 <pre>int luaL_checkoption (lua_State *L,
5396 int arg,
5397 const char *def,
5398 const char *const lst[]);</pre>
5399
5400 <p>
5401 Checks whether the function argument <code>arg</code> is a string and
5402 searches for this string in the array <code>lst</code>
5403 (which must be NULL-terminated).
5404 Returns the index in the array where the string was found.
5405 Raises an error if the argument is not a string or
5406 if the string cannot be found.
5407
5408
5409 <p>
5410 If <code>def</code> is not <code>NULL</code>,
5411 the function uses <code>def</code> as a default value when
5412 there is no argument <code>arg</code> or when this argument is <b>nil</b>.
5413
5414
5415 <p>
5416 This is a useful function for mapping strings to C&nbsp;enums.
5417 (The usual convention in Lua libraries is
5418 to use strings instead of numbers to select options.)
5419
5420
5421
5422
5423
5424 <hr><h3><a name="luaL_checkstack"><code>luaL_checkstack</code></a></h3><p>
5425 <span class="apii">[-0, +0, <em>v</em>]</span>
5426 <pre>void luaL_checkstack (lua_State *L, int sz, const char *msg);</pre>
5427
5428 <p>
5429 Grows the stack size to <code>top + sz</code> elements,
5430 raising an error if the stack cannot grow to that size.
5431 <code>msg</code> is an additional text to go into the error message
5432 (or <code>NULL</code> for no additional text).
5433
5434
5435
5436
5437
5438 <hr><h3><a name="luaL_checkstring"><code>luaL_checkstring</code></a></h3><p>
5439 <span class="apii">[-0, +0, <em>v</em>]</span>
5440 <pre>const char *luaL_checkstring (lua_State *L, int arg);</pre>
5441
5442 <p>
5443 Checks whether the function argument <code>arg</code> is a string
5444 and returns this string.
5445
5446
5447 <p>
5448 This function uses <a href="#lua_tolstring"><code>lua_tolstring</code></a> to get its result,
5449 so all conversions and caveats of that function apply here.
5450
5451
5452
5453
5454
5455 <hr><h3><a name="luaL_checktype"><code>luaL_checktype</code></a></h3><p>
5456 <span class="apii">[-0, +0, <em>v</em>]</span>
5457 <pre>void luaL_checktype (lua_State *L, int arg, int t);</pre>
5458
5459 <p>
5460 Checks whether the function argument <code>arg</code> has type <code>t</code>.
5461 See <a href="#lua_type"><code>lua_type</code></a> for the encoding of types for <code>t</code>.
5462
5463
5464
5465
5466
5467 <hr><h3><a name="luaL_checkudata"><code>luaL_checkudata</code></a></h3><p>
5468 <span class="apii">[-0, +0, <em>v</em>]</span>
5469 <pre>void *luaL_checkudata (lua_State *L, int arg, const char *tname);</pre>
5470
5471 <p>
5472 Checks whether the function argument <code>arg</code> is a userdata
5473 of the type <code>tname</code> (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>) and
5474 returns the userdata address (see <a href="#lua_touserdata"><code>lua_touserdata</code></a>).
5475
5476
5477
5478
5479
5480 <hr><h3><a name="luaL_checkversion"><code>luaL_checkversion</code></a></h3><p>
5481 <span class="apii">[-0, +0, &ndash;]</span>
5482 <pre>void luaL_checkversion (lua_State *L);</pre>
5483
5484 <p>
5485 Checks whether the core running the call,
5486 the core that created the Lua state,
5487 and the code making the call are all using the same version of Lua.
5488 Also checks whether the core running the call
5489 and the core that created the Lua state
5490 are using the same address space.
5491
5492
5493
5494
5495
5496 <hr><h3><a name="luaL_dofile"><code>luaL_dofile</code></a></h3><p>
5497 <span class="apii">[-0, +?, <em>e</em>]</span>
5498 <pre>int luaL_dofile (lua_State *L, const char *filename);</pre>
5499
5500 <p>
5501 Loads and runs the given file.
5502 It is defined as the following macro:
5503
5504 <pre>
5505 (luaL_loadfile(L, filename) || lua_pcall(L, 0, LUA_MULTRET, 0))
5506 </pre><p>
5507 It returns false if there are no errors
5508 or true in case of errors.
5509
5510
5511
5512
5513
5514 <hr><h3><a name="luaL_dostring"><code>luaL_dostring</code></a></h3><p>
5515 <span class="apii">[-0, +?, &ndash;]</span>
5516 <pre>int luaL_dostring (lua_State *L, const char *str);</pre>
5517
5518 <p>
5519 Loads and runs the given string.
5520 It is defined as the following macro:
5521
5522 <pre>
5523 (luaL_loadstring(L, str) || lua_pcall(L, 0, LUA_MULTRET, 0))
5524 </pre><p>
5525 It returns false if there are no errors
5526 or true in case of errors.
5527
5528
5529
5530
5531
5532 <hr><h3><a name="luaL_error"><code>luaL_error</code></a></h3><p>
5533 <span class="apii">[-0, +0, <em>v</em>]</span>
5534 <pre>int luaL_error (lua_State *L, const char *fmt, ...);</pre>
5535
5536 <p>
5537 Raises an error.
5538 The error message format is given by <code>fmt</code>
5539 plus any extra arguments,
5540 following the same rules of <a href="#lua_pushfstring"><code>lua_pushfstring</code></a>.
5541 It also adds at the beginning of the message the file name and
5542 the line number where the error occurred,
5543 if this information is available.
5544
5545
5546 <p>
5547 This function never returns,
5548 but it is an idiom to use it in C&nbsp;functions
5549 as <code>return luaL_error(<em>args</em>)</code>.
5550
5551
5552
5553
5554
5555 <hr><h3><a name="luaL_execresult"><code>luaL_execresult</code></a></h3><p>
5556 <span class="apii">[-0, +3, <em>e</em>]</span>
5557 <pre>int luaL_execresult (lua_State *L, int stat);</pre>
5558
5559 <p>
5560 This function produces the return values for
5561 process-related functions in the standard library
5562 (<a href="#pdf-os.execute"><code>os.execute</code></a> and <a href="#pdf-io.close"><code>io.close</code></a>).
5563
5564
5565
5566
5567
5568 <hr><h3><a name="luaL_fileresult"><code>luaL_fileresult</code></a></h3><p>
5569 <span class="apii">[-0, +(1|3), <em>e</em>]</span>
5570 <pre>int luaL_fileresult (lua_State *L, int stat, const char *fname);</pre>
5571
5572 <p>
5573 This function produces the return values for
5574 file-related functions in the standard library
5575 (<a href="#pdf-io.open"><code>io.open</code></a>, <a href="#pdf-os.rename"><code>os.rename</code></a>, <a href="#pdf-file:seek"><code>file:seek</code></a>, etc.).
5576
5577
5578
5579
5580
5581 <hr><h3><a name="luaL_getmetafield"><code>luaL_getmetafield</code></a></h3><p>
5582 <span class="apii">[-0, +(0|1), <em>e</em>]</span>
5583 <pre>int luaL_getmetafield (lua_State *L, int obj, const char *e);</pre>
5584
5585 <p>
5586 Pushes onto the stack the field <code>e</code> from the metatable
5587 of the object at index <code>obj</code> and returns the type of pushed value.
5588 If the object does not have a metatable,
5589 or if the metatable does not have this field,
5590 pushes nothing and returns <code>LUA_TNIL</code>.
5591
5592
5593
5594
5595
5596 <hr><h3><a name="luaL_getmetatable"><code>luaL_getmetatable</code></a></h3><p>
5597 <span class="apii">[-0, +1, &ndash;]</span>
5598 <pre>int luaL_getmetatable (lua_State *L, const char *tname);</pre>
5599
5600 <p>
5601 Pushes onto the stack the metatable associated with name <code>tname</code>
5602 in the registry (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>).
5603 If there is no metatable associated with <code>tname</code>,
5604 returns false and pushes <b>nil</b>.
5605
5606
5607
5608
5609
5610 <hr><h3><a name="luaL_getsubtable"><code>luaL_getsubtable</code></a></h3><p>
5611 <span class="apii">[-0, +1, <em>e</em>]</span>
5612 <pre>int luaL_getsubtable (lua_State *L, int idx, const char *fname);</pre>
5613
5614 <p>
5615 Ensures that the value <code>t[fname]</code>,
5616 where <code>t</code> is the value at index <code>idx</code>,
5617 is a table,
5618 and pushes that table onto the stack.
5619 Returns true if it finds a previous table there
5620 and false if it creates a new table.
5621
5622
5623
5624
5625
5626 <hr><h3><a name="luaL_gsub"><code>luaL_gsub</code></a></h3><p>
5627 <span class="apii">[-0, +1, <em>e</em>]</span>
5628 <pre>const char *luaL_gsub (lua_State *L,
5629 const char *s,
5630 const char *p,
5631 const char *r);</pre>
5632
5633 <p>
5634 Creates a copy of string <code>s</code> by replacing
5635 any occurrence of the string <code>p</code>
5636 with the string <code>r</code>.
5637 Pushes the resulting string on the stack and returns it.
5638
5639
5640
5641
5642
5643 <hr><h3><a name="luaL_len"><code>luaL_len</code></a></h3><p>
5644 <span class="apii">[-0, +0, <em>e</em>]</span>
5645 <pre>lua_Integer luaL_len (lua_State *L, int index);</pre>
5646
5647 <p>
5648 Returns the "length" of the value at the given index
5649 as a number;
5650 it is equivalent to the '<code>#</code>' operator in Lua (see <a href="#3.4.7">&sect;3.4.7</a>).
5651 Raises an error if the result of the operation is not an integer.
5652 (This case only can happen through metamethods.)
5653
5654
5655
5656
5657
5658 <hr><h3><a name="luaL_loadbuffer"><code>luaL_loadbuffer</code></a></h3><p>
5659 <span class="apii">[-0, +1, &ndash;]</span>
5660 <pre>int luaL_loadbuffer (lua_State *L,
5661 const char *buff,
5662 size_t sz,
5663 const char *name);</pre>
5664
5665 <p>
5666 Equivalent to <a href="#luaL_loadbufferx"><code>luaL_loadbufferx</code></a> with <code>mode</code> equal to <code>NULL</code>.
5667
5668
5669
5670
5671
5672 <hr><h3><a name="luaL_loadbufferx"><code>luaL_loadbufferx</code></a></h3><p>
5673 <span class="apii">[-0, +1, &ndash;]</span>
5674 <pre>int luaL_loadbufferx (lua_State *L,
5675 const char *buff,
5676 size_t sz,
5677 const char *name,
5678 const char *mode);</pre>
5679
5680 <p>
5681 Loads a buffer as a Lua chunk.
5682 This function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in the
5683 buffer pointed to by <code>buff</code> with size <code>sz</code>.
5684
5685
5686 <p>
5687 This function returns the same results as <a href="#lua_load"><code>lua_load</code></a>.
5688 <code>name</code> is the chunk name,
5689 used for debug information and error messages.
5690 The string <code>mode</code> works as in function <a href="#lua_load"><code>lua_load</code></a>.
5691
5692
5693
5694
5695
5696 <hr><h3><a name="luaL_loadfile"><code>luaL_loadfile</code></a></h3><p>
5697 <span class="apii">[-0, +1, <em>e</em>]</span>
5698 <pre>int luaL_loadfile (lua_State *L, const char *filename);</pre>
5699
5700 <p>
5701 Equivalent to <a href="#luaL_loadfilex"><code>luaL_loadfilex</code></a> with <code>mode</code> equal to <code>NULL</code>.
5702
5703
5704
5705
5706
5707 <hr><h3><a name="luaL_loadfilex"><code>luaL_loadfilex</code></a></h3><p>
5708 <span class="apii">[-0, +1, <em>e</em>]</span>
5709 <pre>int luaL_loadfilex (lua_State *L, const char *filename,
5710 const char *mode);</pre>
5711
5712 <p>
5713 Loads a file as a Lua chunk.
5714 This function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in the file
5715 named <code>filename</code>.
5716 If <code>filename</code> is <code>NULL</code>,
5717 then it loads from the standard input.
5718 The first line in the file is ignored if it starts with a <code>#</code>.
5719
5720
5721 <p>
5722 The string <code>mode</code> works as in function <a href="#lua_load"><code>lua_load</code></a>.
5723
5724
5725 <p>
5726 This function returns the same results as <a href="#lua_load"><code>lua_load</code></a>,
5727 but it has an extra error code <a name="pdf-LUA_ERRFILE"><code>LUA_ERRFILE</code></a>
5728 if it cannot open/read the file or the file has a wrong mode.
5729
5730
5731 <p>
5732 As <a href="#lua_load"><code>lua_load</code></a>, this function only loads the chunk;
5733 it does not run it.
5734
5735
5736
5737
5738
5739 <hr><h3><a name="luaL_loadstring"><code>luaL_loadstring</code></a></h3><p>
5740 <span class="apii">[-0, +1, &ndash;]</span>
5741 <pre>int luaL_loadstring (lua_State *L, const char *s);</pre>
5742
5743 <p>
5744 Loads a string as a Lua chunk.
5745 This function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in
5746 the zero-terminated string <code>s</code>.
5747
5748
5749 <p>
5750 This function returns the same results as <a href="#lua_load"><code>lua_load</code></a>.
5751
5752
5753 <p>
5754 Also as <a href="#lua_load"><code>lua_load</code></a>, this function only loads the chunk;
5755 it does not run it.
5756
5757
5758
5759
5760
5761 <hr><h3><a name="luaL_newlib"><code>luaL_newlib</code></a></h3><p>
5762 <span class="apii">[-0, +1, <em>e</em>]</span>
5763 <pre>void luaL_newlib (lua_State *L, const luaL_Reg l[]);</pre>
5764
5765 <p>
5766 Creates a new table and registers there
5767 the functions in list <code>l</code>.
5768
5769
5770 <p>
5771 It is implemented as the following macro:
5772
5773 <pre>
5774 (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))
5775 </pre><p>
5776 The array <code>l</code> must be the actual array,
5777 not a pointer to it.
5778
5779
5780
5781
5782
5783 <hr><h3><a name="luaL_newlibtable"><code>luaL_newlibtable</code></a></h3><p>
5784 <span class="apii">[-0, +1, <em>e</em>]</span>
5785 <pre>void luaL_newlibtable (lua_State *L, const luaL_Reg l[]);</pre>
5786
5787 <p>
5788 Creates a new table with a size optimized
5789 to store all entries in the array <code>l</code>
5790 (but does not actually store them).
5791 It is intended to be used in conjunction with <a href="#luaL_setfuncs"><code>luaL_setfuncs</code></a>
5792 (see <a href="#luaL_newlib"><code>luaL_newlib</code></a>).
5793
5794
5795 <p>
5796 It is implemented as a macro.
5797 The array <code>l</code> must be the actual array,
5798 not a pointer to it.
5799
5800
5801
5802
5803
5804 <hr><h3><a name="luaL_newmetatable"><code>luaL_newmetatable</code></a></h3><p>
5805 <span class="apii">[-0, +1, <em>e</em>]</span>
5806 <pre>int luaL_newmetatable (lua_State *L, const char *tname);</pre>
5807
5808 <p>
5809 If the registry already has the key <code>tname</code>,
5810 returns 0.
5811 Otherwise,
5812 creates a new table to be used as a metatable for userdata,
5813 adds to this new table the pair <code>__name = tname</code>,
5814 adds to the registry the pair <code>[tname] = new table</code>,
5815 and returns 1.
5816 (The entry <code>__name</code> is used by some error-reporting functions.)
5817
5818
5819 <p>
5820 In both cases pushes onto the stack the final value associated
5821 with <code>tname</code> in the registry.
5822
5823
5824
5825
5826
5827 <hr><h3><a name="luaL_newstate"><code>luaL_newstate</code></a></h3><p>
5828 <span class="apii">[-0, +0, &ndash;]</span>
5829 <pre>lua_State *luaL_newstate (void);</pre>
5830
5831 <p>
5832 Creates a new Lua state.
5833 It calls <a href="#lua_newstate"><code>lua_newstate</code></a> with an
5834 allocator based on the standard&nbsp;C <code>realloc</code> function
5835 and then sets a panic function (see <a href="#4.6">&sect;4.6</a>) that prints
5836 an error message to the standard error output in case of fatal
5837 errors.
5838
5839
5840 <p>
5841 Returns the new state,
5842 or <code>NULL</code> if there is a memory allocation error.
5843
5844
5845
5846
5847
5848 <hr><h3><a name="luaL_openlibs"><code>luaL_openlibs</code></a></h3><p>
5849 <span class="apii">[-0, +0, <em>e</em>]</span>
5850 <pre>void luaL_openlibs (lua_State *L);</pre>
5851
5852 <p>
5853 Opens all standard Lua libraries into the given state.
5854
5855
5856
5857
5858
5859 <hr><h3><a name="luaL_optinteger"><code>luaL_optinteger</code></a></h3><p>
5860 <span class="apii">[-0, +0, <em>v</em>]</span>
5861 <pre>lua_Integer luaL_optinteger (lua_State *L,
5862 int arg,
5863 lua_Integer d);</pre>
5864
5865 <p>
5866 If the function argument <code>arg</code> is an integer
5867 (or convertible to an integer),
5868 returns this integer.
5869 If this argument is absent or is <b>nil</b>,
5870 returns <code>d</code>.
5871 Otherwise, raises an error.
5872
5873
5874
5875
5876
5877 <hr><h3><a name="luaL_optlstring"><code>luaL_optlstring</code></a></h3><p>
5878 <span class="apii">[-0, +0, <em>v</em>]</span>
5879 <pre>const char *luaL_optlstring (lua_State *L,
5880 int arg,
5881 const char *d,
5882 size_t *l);</pre>
5883
5884 <p>
5885 If the function argument <code>arg</code> is a string,
5886 returns this string.
5887 If this argument is absent or is <b>nil</b>,
5888 returns <code>d</code>.
5889 Otherwise, raises an error.
5890
5891
5892 <p>
5893 If <code>l</code> is not <code>NULL</code>,
5894 fills the position <code>*l</code> with the result's length.
5895
5896
5897
5898
5899
5900 <hr><h3><a name="luaL_optnumber"><code>luaL_optnumber</code></a></h3><p>
5901 <span class="apii">[-0, +0, <em>v</em>]</span>
5902 <pre>lua_Number luaL_optnumber (lua_State *L, int arg, lua_Number d);</pre>
5903
5904 <p>
5905 If the function argument <code>arg</code> is a number,
5906 returns this number.
5907 If this argument is absent or is <b>nil</b>,
5908 returns <code>d</code>.
5909 Otherwise, raises an error.
5910
5911
5912
5913
5914
5915 <hr><h3><a name="luaL_optstring"><code>luaL_optstring</code></a></h3><p>
5916 <span class="apii">[-0, +0, <em>v</em>]</span>
5917 <pre>const char *luaL_optstring (lua_State *L,
5918 int arg,
5919 const char *d);</pre>
5920
5921 <p>
5922 If the function argument <code>arg</code> is a string,
5923 returns this string.
5924 If this argument is absent or is <b>nil</b>,
5925 returns <code>d</code>.
5926 Otherwise, raises an error.
5927
5928
5929
5930
5931
5932 <hr><h3><a name="luaL_prepbuffer"><code>luaL_prepbuffer</code></a></h3><p>
5933 <span class="apii">[-?, +?, <em>e</em>]</span>
5934 <pre>char *luaL_prepbuffer (luaL_Buffer *B);</pre>
5935
5936 <p>
5937 Equivalent to <a href="#luaL_prepbuffsize"><code>luaL_prepbuffsize</code></a>
5938 with the predefined size <a name="pdf-LUAL_BUFFERSIZE"><code>LUAL_BUFFERSIZE</code></a>.
5939
5940
5941
5942
5943
5944 <hr><h3><a name="luaL_prepbuffsize"><code>luaL_prepbuffsize</code></a></h3><p>
5945 <span class="apii">[-?, +?, <em>e</em>]</span>
5946 <pre>char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz);</pre>
5947
5948 <p>
5949 Returns an address to a space of size <code>sz</code>
5950 where you can copy a string to be added to buffer <code>B</code>
5951 (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
5952 After copying the string into this space you must call
5953 <a href="#luaL_addsize"><code>luaL_addsize</code></a> with the size of the string to actually add
5954 it to the buffer.
5955
5956
5957
5958
5959
5960 <hr><h3><a name="luaL_pushresult"><code>luaL_pushresult</code></a></h3><p>
5961 <span class="apii">[-?, +1, <em>e</em>]</span>
5962 <pre>void luaL_pushresult (luaL_Buffer *B);</pre>
5963
5964 <p>
5965 Finishes the use of buffer <code>B</code> leaving the final string on
5966 the top of the stack.
5967
5968
5969
5970
5971
5972 <hr><h3><a name="luaL_pushresultsize"><code>luaL_pushresultsize</code></a></h3><p>
5973 <span class="apii">[-?, +1, <em>e</em>]</span>
5974 <pre>void luaL_pushresultsize (luaL_Buffer *B, size_t sz);</pre>
5975
5976 <p>
5977 Equivalent to the sequence <a href="#luaL_addsize"><code>luaL_addsize</code></a>, <a href="#luaL_pushresult"><code>luaL_pushresult</code></a>.
5978
5979
5980
5981
5982
5983 <hr><h3><a name="luaL_ref"><code>luaL_ref</code></a></h3><p>
5984 <span class="apii">[-1, +0, <em>e</em>]</span>
5985 <pre>int luaL_ref (lua_State *L, int t);</pre>
5986
5987 <p>
5988 Creates and returns a <em>reference</em>,
5989 in the table at index <code>t</code>,
5990 for the object at the top of the stack (and pops the object).
5991
5992
5993 <p>
5994 A reference is a unique integer key.
5995 As long as you do not manually add integer keys into table <code>t</code>,
5996 <a href="#luaL_ref"><code>luaL_ref</code></a> ensures the uniqueness of the key it returns.
5997 You can retrieve an object referred by reference <code>r</code>
5998 by calling <code>lua_rawgeti(L, t, r)</code>.
5999 Function <a href="#luaL_unref"><code>luaL_unref</code></a> frees a reference and its associated object.
6000
6001
6002 <p>
6003 If the object at the top of the stack is <b>nil</b>,
6004 <a href="#luaL_ref"><code>luaL_ref</code></a> returns the constant <a name="pdf-LUA_REFNIL"><code>LUA_REFNIL</code></a>.
6005 The constant <a name="pdf-LUA_NOREF"><code>LUA_NOREF</code></a> is guaranteed to be different
6006 from any reference returned by <a href="#luaL_ref"><code>luaL_ref</code></a>.
6007
6008
6009
6010
6011
6012 <hr><h3><a name="luaL_Reg"><code>luaL_Reg</code></a></h3>
6013 <pre>typedef struct luaL_Reg {
6014 const char *name;
6015 lua_CFunction func;
6016 } luaL_Reg;</pre>
6017
6018 <p>
6019 Type for arrays of functions to be registered by
6020 <a href="#luaL_setfuncs"><code>luaL_setfuncs</code></a>.
6021 <code>name</code> is the function name and <code>func</code> is a pointer to
6022 the function.
6023 Any array of <a href="#luaL_Reg"><code>luaL_Reg</code></a> must end with a sentinel entry
6024 in which both <code>name</code> and <code>func</code> are <code>NULL</code>.
6025
6026
6027
6028
6029
6030 <hr><h3><a name="luaL_requiref"><code>luaL_requiref</code></a></h3><p>
6031 <span class="apii">[-0, +1, <em>e</em>]</span>
6032 <pre>void luaL_requiref (lua_State *L, const char *modname,
6033 lua_CFunction openf, int glb);</pre>
6034
6035 <p>
6036 If <code>modname</code> is not already present in <a href="#pdf-package.loaded"><code>package.loaded</code></a>,
6037 calls function <code>openf</code> with string <code>modname</code> as an argument
6038 and sets the call result in <code>package.loaded[modname]</code>,
6039 as if that function has been called through <a href="#pdf-require"><code>require</code></a>.
6040
6041
6042 <p>
6043 If <code>glb</code> is true,
6044 also stores the module into global <code>modname</code>.
6045
6046
6047 <p>
6048 Leaves a copy of the module on the stack.
6049
6050
6051
6052
6053
6054 <hr><h3><a name="luaL_setfuncs"><code>luaL_setfuncs</code></a></h3><p>
6055 <span class="apii">[-nup, +0, <em>e</em>]</span>
6056 <pre>void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup);</pre>
6057
6058 <p>
6059 Registers all functions in the array <code>l</code>
6060 (see <a href="#luaL_Reg"><code>luaL_Reg</code></a>) into the table on the top of the stack
6061 (below optional upvalues, see next).
6062
6063
6064 <p>
6065 When <code>nup</code> is not zero,
6066 all functions are created sharing <code>nup</code> upvalues,
6067 which must be previously pushed on the stack
6068 on top of the library table.
6069 These values are popped from the stack after the registration.
6070
6071
6072
6073
6074
6075 <hr><h3><a name="luaL_setmetatable"><code>luaL_setmetatable</code></a></h3><p>
6076 <span class="apii">[-0, +0, &ndash;]</span>
6077 <pre>void luaL_setmetatable (lua_State *L, const char *tname);</pre>
6078
6079 <p>
6080 Sets the metatable of the object at the top of the stack
6081 as the metatable associated with name <code>tname</code>
6082 in the registry (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>).
6083
6084
6085
6086
6087
6088 <hr><h3><a name="luaL_Stream"><code>luaL_Stream</code></a></h3>
6089 <pre>typedef struct luaL_Stream {
6090 FILE *f;
6091 lua_CFunction closef;
6092 } luaL_Stream;</pre>
6093
6094 <p>
6095 The standard representation for file handles,
6096 which is used by the standard I/O library.
6097
6098
6099 <p>
6100 A file handle is implemented as a full userdata,
6101 with a metatable called <code>LUA_FILEHANDLE</code>
6102 (where <code>LUA_FILEHANDLE</code> is a macro with the actual metatable's name).
6103 The metatable is created by the I/O library
6104 (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>).
6105
6106
6107 <p>
6108 This userdata must start with the structure <code>luaL_Stream</code>;
6109 it can contain other data after this initial structure.
6110 Field <code>f</code> points to the corresponding C stream
6111 (or it can be <code>NULL</code> to indicate an incompletely created handle).
6112 Field <code>closef</code> points to a Lua function
6113 that will be called to close the stream
6114 when the handle is closed or collected;
6115 this function receives the file handle as its sole argument and
6116 must return either <b>true</b> (in case of success)
6117 or <b>nil</b> plus an error message (in case of error).
6118 Once Lua calls this field,
6119 the field value is changed to <code>NULL</code>
6120 to signal that the handle is closed.
6121
6122
6123
6124
6125
6126 <hr><h3><a name="luaL_testudata"><code>luaL_testudata</code></a></h3><p>
6127 <span class="apii">[-0, +0, <em>e</em>]</span>
6128 <pre>void *luaL_testudata (lua_State *L, int arg, const char *tname);</pre>
6129
6130 <p>
6131 This function works like <a href="#luaL_checkudata"><code>luaL_checkudata</code></a>,
6132 except that, when the test fails,
6133 it returns <code>NULL</code> instead of raising an error.
6134
6135
6136
6137
6138
6139 <hr><h3><a name="luaL_tolstring"><code>luaL_tolstring</code></a></h3><p>
6140 <span class="apii">[-0, +1, <em>e</em>]</span>
6141 <pre>const char *luaL_tolstring (lua_State *L, int idx, size_t *len);</pre>
6142
6143 <p>
6144 Converts any Lua value at the given index to a C&nbsp;string
6145 in a reasonable format.
6146 The resulting string is pushed onto the stack and also
6147 returned by the function.
6148 If <code>len</code> is not <code>NULL</code>,
6149 the function also sets <code>*len</code> with the string length.
6150
6151
6152 <p>
6153 If the value has a metatable with a <code>"__tostring"</code> field,
6154 then <code>luaL_tolstring</code> calls the corresponding metamethod
6155 with the value as argument,
6156 and uses the result of the call as its result.
6157
6158
6159
6160
6161
6162 <hr><h3><a name="luaL_traceback"><code>luaL_traceback</code></a></h3><p>
6163 <span class="apii">[-0, +1, <em>e</em>]</span>
6164 <pre>void luaL_traceback (lua_State *L, lua_State *L1, const char *msg,
6165 int level);</pre>
6166
6167 <p>
6168 Creates and pushes a traceback of the stack <code>L1</code>.
6169 If <code>msg</code> is not <code>NULL</code> it is appended
6170 at the beginning of the traceback.
6171 The <code>level</code> parameter tells at which level
6172 to start the traceback.
6173
6174
6175
6176
6177
6178 <hr><h3><a name="luaL_typename"><code>luaL_typename</code></a></h3><p>
6179 <span class="apii">[-0, +0, &ndash;]</span>
6180 <pre>const char *luaL_typename (lua_State *L, int index);</pre>
6181
6182 <p>
6183 Returns the name of the type of the value at the given index.
6184
6185
6186
6187
6188
6189 <hr><h3><a name="luaL_unref"><code>luaL_unref</code></a></h3><p>
6190 <span class="apii">[-0, +0, &ndash;]</span>
6191 <pre>void luaL_unref (lua_State *L, int t, int ref);</pre>
6192
6193 <p>
6194 Releases reference <code>ref</code> from the table at index <code>t</code>
6195 (see <a href="#luaL_ref"><code>luaL_ref</code></a>).
6196 The entry is removed from the table,
6197 so that the referred object can be collected.
6198 The reference <code>ref</code> is also freed to be used again.
6199
6200
6201 <p>
6202 If <code>ref</code> is <a href="#pdf-LUA_NOREF"><code>LUA_NOREF</code></a> or <a href="#pdf-LUA_REFNIL"><code>LUA_REFNIL</code></a>,
6203 <a href="#luaL_unref"><code>luaL_unref</code></a> does nothing.
6204
6205
6206
6207
6208
6209 <hr><h3><a name="luaL_where"><code>luaL_where</code></a></h3><p>
6210 <span class="apii">[-0, +1, <em>e</em>]</span>
6211 <pre>void luaL_where (lua_State *L, int lvl);</pre>
6212
6213 <p>
6214 Pushes onto the stack a string identifying the current position
6215 of the control at level <code>lvl</code> in the call stack.
6216 Typically this string has the following format:
6217
6218 <pre>
6219 <em>chunkname</em>:<em>currentline</em>:
6220 </pre><p>
6221 Level&nbsp;0 is the running function,
6222 level&nbsp;1 is the function that called the running function,
6223 etc.
6224
6225
6226 <p>
6227 This function is used to build a prefix for error messages.
6228
6229
6230 1759
6231 1760
6232 1761
6233 1762
6234 1763