Mercurial Hosting > luan
comparison website/src/manual.html.luan @ 475:7ac0891718eb
documentation
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 10 May 2015 20:44:55 -0600 |
parents | 00646edc9d92 |
children | cd22e4694ea3 |
comparison
equal
deleted
inserted
replaced
474:00646edc9d92 | 475:7ac0891718eb |
---|---|
2032 end | 2032 end |
2033 </pre></tt></p> | 2033 </pre></tt></p> |
2034 | 2034 |
2035 | 2035 |
2036 | 2036 |
2037 | 2037 <h4 margin-top="1em"><a name="Luan.pairs"><tt>Luan.pairs (t)</tt></a></h4> |
2038 <p> | 2038 |
2039 <hr><h3><a name="pdf-pairs"><code>pairs (t)</code></a></h3> | 2039 <p> |
2040 | 2040 If <tt>t</tt> has a metamethod <tt>__pairs</tt>, |
2041 | 2041 calls it with <tt>t</tt> as argument and returns the |
2042 <p> | 2042 result from the call. |
2043 If <code>t</code> has a metamethod <code>__pairs</code>, | |
2044 calls it with <code>t</code> as argument and returns the first three | |
2045 results from the call. | |
2046 | 2043 |
2047 | 2044 |
2048 <p> | 2045 <p> |
2049 Otherwise, | 2046 Otherwise, |
2050 returns three values: the <a href="#pdf-next"><code>next</code></a> function, the table <code>t</code>, and <b>nil</b>, | 2047 returns a function |
2051 so that the construction | 2048 so that the construction |
2052 | 2049 |
2053 <pre> | 2050 <p><tt><pre> |
2054 for k,v in pairs(t) do <em>body</em> end | 2051 for k,v in pairs(t) do <i>body</i> end |
2055 </pre><p> | 2052 </pre></tt></p> |
2056 will iterate over all key–value pairs of table <code>t</code>. | 2053 |
2057 | 2054 <p> |
2058 | 2055 will iterate over all key–value pairs of table <tt>t</tt>. |
2059 <p> | 2056 |
2060 See function <a href="#pdf-next"><code>next</code></a> for the caveats of modifying | 2057 |
2061 the table during its traversal. | 2058 |
2062 | 2059 <h4 margin-top="1em"><a name="Luan.pcall"><tt>Luan.pcall (f [, arg1, ···])</tt></a></h4> |
2063 | 2060 |
2064 | 2061 <p> |
2065 | 2062 Calls function <tt>f</tt> with |
2066 <p> | 2063 the given arguments in <i>protected mode</i>. |
2067 <hr><h3><a name="pdf-pcall"><code>pcall (f [, arg1, ···])</code></a></h3> | 2064 This means that any error inside <tt>f</tt> is not propagated; |
2068 | 2065 instead, <tt>pcall</tt> catches the error |
2069 | |
2070 <p> | |
2071 Calls function <code>f</code> with | |
2072 the given arguments in <em>protected mode</em>. | |
2073 This means that any error inside <code>f</code> is not propagated; | |
2074 instead, <code>pcall</code> catches the error | |
2075 and returns a status code. | 2066 and returns a status code. |
2076 Its first result is the status code (a boolean), | 2067 Its first result is the status code (a boolean), |
2077 which is true if the call succeeds without errors. | 2068 which is true if the call succeeds without errors. |
2078 In such case, <code>pcall</code> also returns all results from the call, | 2069 In such case, <tt>pcall</tt> also returns all results from the call, |
2079 after this first result. | 2070 after this first result. |
2080 In case of any error, <code>pcall</code> returns <b>false</b> plus the error message. | 2071 In case of any error, <tt>pcall</tt> returns <b>false</b> plus the error. |
2081 | 2072 |
2082 | 2073 |
2083 | 2074 |
2084 | 2075 |
2085 <p> | 2076 <p> |
2094 use <a href="#pdf-string.format"><code>string.format</code></a> and <a href="#pdf-io.write"><code>io.write</code></a>. | 2085 use <a href="#pdf-string.format"><code>string.format</code></a> and <a href="#pdf-io.write"><code>io.write</code></a>. |
2095 | 2086 |
2096 | 2087 |
2097 | 2088 |
2098 | 2089 |
2099 <p> | 2090 <h4 margin-top="1em"><a name="Luan.raw_equal"><tt>Luan.raw_equal (v1, v2)</tt></a></h4> |
2100 <hr><h3><a name="pdf-rawequal"><code>rawequal (v1, v2)</code></a></h3> | 2091 |
2101 Checks whether <code>v1</code> is equal to <code>v2</code>, | 2092 <p> |
2093 Checks whether <tt>v1</tt> is equal to <tt>v2</tt>, | |
2102 without invoking any metamethod. | 2094 without invoking any metamethod. |
2103 Returns a boolean. | 2095 Returns a boolean. |
2104 | 2096 |
2105 | 2097 |
2106 | 2098 |
2107 | 2099 <h4 margin-top="1em"><a name="Luan.raw_get"><tt>Luan.raw_get (table, index)</tt></a></h4> |
2108 <p> | 2100 |
2109 <hr><h3><a name="pdf-rawget"><code>rawget (table, index)</code></a></h3> | 2101 <p> |
2110 Gets the real value of <code>table[index]</code>, | 2102 Gets the real value of <tt>table[index]</tt>, |
2111 without invoking any metamethod. | 2103 without invoking any metamethod. |
2112 <code>table</code> must be a table; | 2104 <tt>table</tt> must be a table; |
2113 <code>index</code> may be any value. | 2105 <tt>index</tt> may be any value. |
2114 | 2106 |
2115 | 2107 |
2116 | 2108 |
2117 | 2109 <h4 margin-top="1em"><a name="Luan.raw_len"><tt>Luan.raw_len (v)</tt></a></h4> |
2118 <p> | 2110 |
2119 <hr><h3><a name="pdf-rawlen"><code>rawlen (v)</code></a></h3> | 2111 <p> |
2120 Returns the length of the object <code>v</code>, | 2112 Returns the length of the object <tt>v</tt>, |
2121 which must be a table or a string, | 2113 which must be a table or a string, |
2122 without invoking any metamethod. | 2114 without invoking any metamethod. |
2123 Returns an integer. | 2115 Returns an integer. |
2124 | 2116 |
2125 | 2117 |
2126 | 2118 |
2127 | 2119 <h4 margin-top="1em"><a name="Luan.raw_set"><tt>Luan.raw_set (table, index, value)</tt></a></h4> |
2128 <p> | 2120 |
2129 <hr><h3><a name="pdf-rawset"><code>rawset (table, index, value)</code></a></h3> | 2121 <p> |
2130 Sets the real value of <code>table[index]</code> to <code>value</code>, | 2122 Sets the real value of <tt>table[index]</tt> to <tt>value</tt>, |
2131 without invoking any metamethod. | 2123 without invoking any metamethod. |
2132 <code>table</code> must be a table, | 2124 <tt>table</tt> must be a table, |
2133 <code>index</code> any value different from <b>nil</b> and NaN, | 2125 <tt>index</tt> any value different from <b>nil</b>, |
2134 and <code>value</code> any Lua value. | 2126 and <tt>value</tt> any Lua value. |
2135 | 2127 |
2136 | 2128 |
2137 <p> | 2129 <p> |
2138 This function returns <code>table</code>. | 2130 This function returns <tt>table</tt>. |
2139 | 2131 |
2140 | 2132 |
2141 | 2133 |
2142 | 2134 |
2143 <p> | 2135 <p> |
2152 and <code>select</code> returns the total number of extra arguments it received. | 2144 and <code>select</code> returns the total number of extra arguments it received. |
2153 | 2145 |
2154 | 2146 |
2155 | 2147 |
2156 | 2148 |
2157 <p> | 2149 <h4 margin-top="1em"><a name="Luan.set_metatable"><tt>Luan.set_metatable (table, metatable)</tt></a></h4> |
2158 <hr><h3><a name="pdf-setmetatable"><code>setmetatable (table, metatable)</code></a></h3> | |
2159 | |
2160 | 2150 |
2161 <p> | 2151 <p> |
2162 Sets the metatable for the given table. | 2152 Sets the metatable for the given table. |
2163 (You cannot change the metatable of other types from Lua, only from C.) | 2153 If <tt>metatable</tt> is <b>nil</b>, |
2164 If <code>metatable</code> is <b>nil</b>, | |
2165 removes the metatable of the given table. | 2154 removes the metatable of the given table. |
2166 If the original metatable has a <code>"__metatable"</code> field, | 2155 If the original metatable has a <tt>"__metatable"</tt> field, |
2167 raises an error. | 2156 raises an error. |
2168 | 2157 |
2169 | 2158 |
2170 <p> | 2159 <p> |
2171 This function returns <code>table</code>. | 2160 This function returns <tt>table</tt>. |
2172 | 2161 |
2173 | 2162 |
2174 | 2163 |
2175 | 2164 |
2176 <p> | 2165 <p> |
2204 the function returns <b>nil</b>. | 2193 the function returns <b>nil</b>. |
2205 | 2194 |
2206 | 2195 |
2207 | 2196 |
2208 | 2197 |
2209 <p> | 2198 <h4 margin-top="1em"><a name="Luan.to_string"><tt>Luan.to_string (v)</tt></a></h4> |
2210 <hr><h3><a name="pdf-tostring"><code>tostring (v)</code></a></h3> | 2199 |
2200 <p> | |
2211 Receives a value of any type and | 2201 Receives a value of any type and |
2212 converts it to a string in a human-readable format. | 2202 converts it to a string in a human-readable format. |
2213 Floats always produce strings with some | 2203 |
2214 floating-point indication (either a decimal dot or an exponent). | 2204 <p> |
2215 (For complete control of how numbers are converted, | 2205 If the metatable of <tt>v</tt> has a <tt>"__to_string"</tt> field, |
2216 use <a href="#pdf-string.format"><code>string.format</code></a>.) | 2206 then <tt>to_string</tt> calls the corresponding value |
2217 | 2207 with <tt>v</tt> as argument, |
2218 | |
2219 <p> | |
2220 If the metatable of <code>v</code> has a <code>"__to_string"</code> field, | |
2221 then <code>tostring</code> calls the corresponding value | |
2222 with <code>v</code> as argument, | |
2223 and uses the result of the call as its result. | 2208 and uses the result of the call as its result. |
2224 | 2209 |
2225 | 2210 |
2226 | 2211 |
2227 | 2212 <h4 margin-top="1em"><a name="Luan.type"><tt>Luan.type (v)</tt></a></h4> |
2228 <p> | 2213 |
2229 <hr><h3><a name="pdf-type"><code>type (v)</code></a></h3> | 2214 <p> |
2230 Returns the type of its only argument, coded as a string. | 2215 Returns the type of its only argument, coded as a string. |
2231 The possible results of this function are | 2216 The possible results of this function are |
2232 "<code>nil</code>" (a string, not the value <b>nil</b>), | 2217 "<tt>nil</tt>" (a string, not the value <b>nil</b>), |
2233 "<code>number</code>", | 2218 "<tt>number</tt>", |
2234 "<code>string</code>", | 2219 "<tt>string</tt>", |
2235 "<code>boolean</code>", | 2220 "<tt>binary</tt>", |
2236 "<code>table</code>", | 2221 "<tt>boolean</tt>", |
2237 "<code>function</code>", | 2222 "<tt>table</tt>", |
2238 "<code>thread</code>", | 2223 "<tt>function</tt>", |
2239 and "<code>userdata</code>". | 2224 and "<tt>userdata</tt>". |
2240 | 2225 |
2241 | 2226 |
2242 | 2227 |
2243 | 2228 <h4 margin-top="1em"><a name="Luan.VERSION"><tt>Luan.VERSION</tt></a></h4> |
2244 <p> | 2229 |
2245 <hr><h3><a name="pdf-_VERSION"><code>_VERSION</code></a></h3> | 2230 <p> |
2246 A global variable (not a function) that | 2231 A global variable (not a function) that |
2247 holds a string containing the current interpreter version. | 2232 holds a string containing the current interpreter version. |
2248 The current value of this variable is "<code>Lua 5.3</code>". | 2233 The current value of this variable is "<tt>Luan 0.8</tt>". |
2249 | |
2250 | |
2251 | |
2252 | |
2253 <p> | |
2254 <hr><h3><a name="pdf-xpcall"><code>xpcall (f, msgh [, arg1, ···])</code></a></h3> | |
2255 | |
2256 | |
2257 <p> | |
2258 This function is similar to <a href="#pdf-pcall"><code>pcall</code></a>, | |
2259 except that it sets a new message handler <code>msgh</code>. | |
2260 | |
2261 | |
2262 | |
2263 | |
2264 | |
2265 | |
2266 | |
2267 <h2>6.2 – <a name="6.2">Coroutine Manipulation</a></h2> | |
2268 | |
2269 <p> | |
2270 The operations related to coroutines comprise a sub-library of | |
2271 the basic library and come inside the table <a name="pdf-coroutine"><code>coroutine</code></a>. | |
2272 See <a href="#2.6">§2.6</a> for a general description of coroutines. | |
2273 | |
2274 | |
2275 <p> | |
2276 <hr><h3><a name="pdf-coroutine.create"><code>coroutine.create (f)</code></a></h3> | |
2277 | |
2278 | |
2279 <p> | |
2280 Creates a new coroutine, with body <code>f</code>. | |
2281 <code>f</code> must be a Lua function. | |
2282 Returns this new coroutine, | |
2283 an object with type <code>"thread"</code>. | |
2284 | |
2285 | |
2286 | |
2287 | |
2288 <p> | |
2289 <hr><h3><a name="pdf-coroutine.isyieldable"><code>coroutine.isyieldable ()</code></a></h3> | |
2290 | |
2291 | |
2292 <p> | |
2293 Returns true when the running coroutine can yield. | |
2294 | |
2295 | |
2296 <p> | |
2297 A running coroutine is yieldable if it is not the main thread and | |
2298 it is not inside a non-yieldable C function. | |
2299 | |
2300 | |
2301 | |
2302 | |
2303 <p> | |
2304 <hr><h3><a name="pdf-coroutine.resume"><code>coroutine.resume (co [, val1, ···])</code></a></h3> | |
2305 | |
2306 | |
2307 <p> | |
2308 Starts or continues the execution of coroutine <code>co</code>. | |
2309 The first time you resume a coroutine, | |
2310 it starts running its body. | |
2311 The values <code>val1</code>, ... are passed | |
2312 as the arguments to the body function. | |
2313 If the coroutine has yielded, | |
2314 <code>resume</code> restarts it; | |
2315 the values <code>val1</code>, ... are passed | |
2316 as the results from the yield. | |
2317 | |
2318 | |
2319 <p> | |
2320 If the coroutine runs without any errors, | |
2321 <code>resume</code> returns <b>true</b> plus any values passed to <code>yield</code> | |
2322 (when the coroutine yields) or any values returned by the body function | |
2323 (when the coroutine terminates). | |
2324 If there is any error, | |
2325 <code>resume</code> returns <b>false</b> plus the error message. | |
2326 | |
2327 | |
2328 | |
2329 | |
2330 <p> | |
2331 <hr><h3><a name="pdf-coroutine.running"><code>coroutine.running ()</code></a></h3> | |
2332 | |
2333 | |
2334 <p> | |
2335 Returns the running coroutine plus a boolean, | |
2336 true when the running coroutine is the main one. | |
2337 | |
2338 | |
2339 | |
2340 | |
2341 <p> | |
2342 <hr><h3><a name="pdf-coroutine.status"><code>coroutine.status (co)</code></a></h3> | |
2343 | |
2344 | |
2345 <p> | |
2346 Returns the status of coroutine <code>co</code>, as a string: | |
2347 <code>"running"</code>, | |
2348 if the coroutine is running (that is, it called <code>status</code>); | |
2349 <code>"suspended"</code>, if the coroutine is suspended in a call to <code>yield</code>, | |
2350 or if it has not started running yet; | |
2351 <code>"normal"</code> if the coroutine is active but not running | |
2352 (that is, it has resumed another coroutine); | |
2353 and <code>"dead"</code> if the coroutine has finished its body function, | |
2354 or if it has stopped with an error. | |
2355 | |
2356 | |
2357 | |
2358 | |
2359 <p> | |
2360 <hr><h3><a name="pdf-coroutine.wrap"><code>coroutine.wrap (f)</code></a></h3> | |
2361 | |
2362 | |
2363 <p> | |
2364 Creates a new coroutine, with body <code>f</code>. | |
2365 <code>f</code> must be a Lua function. | |
2366 Returns a function that resumes the coroutine each time it is called. | |
2367 Any arguments passed to the function behave as the | |
2368 extra arguments to <code>resume</code>. | |
2369 Returns the same values returned by <code>resume</code>, | |
2370 except the first boolean. | |
2371 In case of error, propagates the error. | |
2372 | |
2373 | |
2374 | |
2375 | |
2376 <p> | |
2377 <hr><h3><a name="pdf-coroutine.yield"><code>coroutine.yield (···)</code></a></h3> | |
2378 | |
2379 | |
2380 <p> | |
2381 Suspends the execution of the calling coroutine. | |
2382 Any arguments to <code>yield</code> are passed as extra results to <code>resume</code>. | |
2383 | 2234 |
2384 | 2235 |
2385 | 2236 |
2386 | 2237 |
2387 | 2238 |