comparison website/src/manual.html.luan @ 476:cd22e4694ea3

documentation
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 10 May 2015 22:41:00 -0600
parents 7ac0891718eb
children eb6d90fb0b5c
comparison
equal deleted inserted replaced
475:7ac0891718eb 476:cd22e4694ea3
86 <div margin-bottom="1em"> 86 <div margin-bottom="1em">
87 <a href="#libs">Standard Libraries</a> 87 <a href="#libs">Standard Libraries</a>
88 <ul> 88 <ul>
89 <li><a href="#default_lib">Default Environment</a></li> 89 <li><a href="#default_lib">Default Environment</a></li>
90 <li><a href="#luan_lib">Basic Functions</a></li> 90 <li><a href="#luan_lib">Basic Functions</a></li>
91 <li><a href="#package_lib">Modules</a></li>
91 </ul> 92 </ul>
92 </div> 93 </div>
93 94
94 <hr/> 95 <hr/>
95 96
1850 <p> 1851 <p>
1851 Could be defined as: 1852 Could be defined as:
1852 1853
1853 <p><tt><pre> 1854 <p><tt><pre>
1854 local function require(mod_name) 1855 local function require(mod_name)
1855 return Package.load(mod_name) or <a href="#Luan.error">Luan.error</a>("module '"..mod_name.."' not found") 1856 return <a href="#Package.load">Package.load</a>(mod_name) or <a href="#Luan.error">Luan.error</a>("module '"..mod_name.."' not found")
1856 end 1857 end
1857 </pre></tt></p> 1858 </pre></tt></p>
1858 1859
1859 1860
1860 <h3 margin-top="1em"><a name="luan_lib">Basic Functions</a></h3> 1861 <h3 margin-top="1em"><a name="luan_lib">Basic Functions</a></h3>
1864 1865
1865 <p><tt><pre> 1866 <p><tt><pre>
1866 local Luan = require "luan:Luan" 1867 local Luan = require "luan:Luan"
1867 </pre></tt></p> 1868 </pre></tt></p>
1868 1869
1870 <p>
1869 The basic library provides basic functions to Luan that don't depend on other libaries. 1871 The basic library provides basic functions to Luan that don't depend on other libaries.
1870 1872
1871 1873
1872 <h4 margin-top="1em"><a name="Luan.assert"><tt>Luan.assert (v [, message])</tt></a></h4> 1874 <h4 margin-top="1em"><a name="Luan.assert"><tt>Luan.assert (v [, message])</tt></a></h4>
1873 1875
2084 For complete control over the output, 2086 For complete control over the output,
2085 use <a href="#pdf-string.format"><code>string.format</code></a> and <a href="#pdf-io.write"><code>io.write</code></a>. 2087 use <a href="#pdf-string.format"><code>string.format</code></a> and <a href="#pdf-io.write"><code>io.write</code></a>.
2086 2088
2087 2089
2088 2090
2091 <h4 margin-top="1em"><a name="Luan.range"><tt>Luan.range (start, stop [, step])</tt></a></h4>
2092
2093 <p>
2094 Based on <a href="https://docs.python.org/2/library/functions.html#range">the Python range() function</a>, this lets one iterate through a sequence of numbers.
2095
2096 <p>
2097 Example use:
2098
2099 <p><tt><pre>
2100 for i in range(1,10) do
2101 Io.print("count up:",i)
2102 end
2103 for i in range(10,0,-1) do
2104 Io.print("count down:",i)
2105 end
2106 </pre></tt></p>
2107
2108 <p>
2109 Could be defined as:
2110
2111 <p><tt><pre>
2112 function Luan.range(start, stop, step)
2113 step = step or 1
2114 step == 0 and Luan.error "bad argument #3 (step may not be zero)"
2115 local i = start
2116 return function()
2117 if step > 0 and i <= stop or step < 0 and i >= stop then
2118 local rtn = i
2119 i = i + step
2120 return rtn
2121 end
2122 end
2123 end
2124 </pre></tt></p>
2125
2126
2089 2127
2090 <h4 margin-top="1em"><a name="Luan.raw_equal"><tt>Luan.raw_equal (v1, v2)</tt></a></h4> 2128 <h4 margin-top="1em"><a name="Luan.raw_equal"><tt>Luan.raw_equal (v1, v2)</tt></a></h4>
2091 2129
2092 <p> 2130 <p>
2093 Checks whether <tt>v1</tt> is equal to <tt>v2</tt>, 2131 Checks whether <tt>v1</tt> is equal to <tt>v2</tt>,
2123 without invoking any metamethod. 2161 without invoking any metamethod.
2124 <tt>table</tt> must be a table, 2162 <tt>table</tt> must be a table,
2125 <tt>index</tt> any value different from <b>nil</b>, 2163 <tt>index</tt> any value different from <b>nil</b>,
2126 and <tt>value</tt> any Lua value. 2164 and <tt>value</tt> any Lua value.
2127 2165
2128
2129 <p> 2166 <p>
2130 This function returns <tt>table</tt>. 2167 This function returns <tt>table</tt>.
2131
2132
2133
2134
2135 <p>
2136 <hr><h3><a name="pdf-select"><code>select (index, &middot;&middot;&middot;)</code></a></h3>
2137
2138
2139 <p>
2140 If <code>index</code> is a number,
2141 returns all arguments after argument number <code>index</code>;
2142 a negative number indexes from the end (-1 is the last argument).
2143 Otherwise, <code>index</code> must be the string <code>"#"</code>,
2144 and <code>select</code> returns the total number of extra arguments it received.
2145
2146
2147 2168
2148 2169
2149 <h4 margin-top="1em"><a name="Luan.set_metatable"><tt>Luan.set_metatable (table, metatable)</tt></a></h4> 2170 <h4 margin-top="1em"><a name="Luan.set_metatable"><tt>Luan.set_metatable (table, metatable)</tt></a></h4>
2150 2171
2151 <p> 2172 <p>
2204 <p> 2225 <p>
2205 If the metatable of <tt>v</tt> has a <tt>"__to_string"</tt> field, 2226 If the metatable of <tt>v</tt> has a <tt>"__to_string"</tt> field,
2206 then <tt>to_string</tt> calls the corresponding value 2227 then <tt>to_string</tt> calls the corresponding value
2207 with <tt>v</tt> as argument, 2228 with <tt>v</tt> as argument,
2208 and uses the result of the call as its result. 2229 and uses the result of the call as its result.
2230
2231
2232
2233 <h4 margin-top="1em"><a name="Luan.try"><tt>Luan.try (t)</tt></a></h4>
2234
2235 <p>
2236 Implements try-catch as found in other languages where each block is in table <tt>t</tt>.
2237
2238 <p>
2239 Example use:
2240
2241 <p><tt><pre>
2242 try {
2243 function()
2244 a_dangerous_fn()
2245 end;
2246 catch = function(e)
2247 -- handle error
2248 end;
2249 finally = function()
2250 -- clean up
2251 end;
2252 }
2253 </pre></tt></p>
2209 2254
2210 2255
2211 2256
2212 <h4 margin-top="1em"><a name="Luan.type"><tt>Luan.type (v)</tt></a></h4> 2257 <h4 margin-top="1em"><a name="Luan.type"><tt>Luan.type (v)</tt></a></h4>
2213 2258
2222 "<tt>table</tt>", 2267 "<tt>table</tt>",
2223 "<tt>function</tt>", 2268 "<tt>function</tt>",
2224 and "<tt>userdata</tt>". 2269 and "<tt>userdata</tt>".
2225 2270
2226 2271
2272 <h4 margin-top="1em"><a name="Luan.values"><tt>Luan.values (&middot;&middot;&middot;)</tt></a></h4>
2273
2274 <p>
2275 Returns a function so that the construction
2276
2277 <p><tt><pre>
2278 for v in Luan.values(&middot;&middot;&middot;) do <i>body</i> end
2279 </pre></tt></p>
2280
2281 <p>
2282 will iterate over all values of <tt>&middot;&middot;&middot;</tt>.
2283
2284
2227 2285
2228 <h4 margin-top="1em"><a name="Luan.VERSION"><tt>Luan.VERSION</tt></a></h4> 2286 <h4 margin-top="1em"><a name="Luan.VERSION"><tt>Luan.VERSION</tt></a></h4>
2229 2287
2230 <p> 2288 <p>
2231 A global variable (not a function) that 2289 A global variable (not a function) that
2235 2293
2236 2294
2237 2295
2238 2296
2239 2297
2240 2298 <h3 margin-top="1em"><a name="package_lib">Modules</a></h3>
2241 <h2>6.3 &ndash; <a name="6.3">Modules</a></h2> 2299
2300 <p>
2301 Include this library by:
2302
2303 <p><tt><pre>
2304 local Package = require "luan:Package"
2305 </pre></tt></p>
2242 2306
2243 <p> 2307 <p>
2244 The package library provides basic 2308 The package library provides basic
2245 facilities for loading modules in Lua. 2309 facilities for loading modules in Luan.
2246 It exports one function directly in the global environment: 2310
2247 <a href="#pdf-require"><code>require</code></a>. 2311
2248 Everything else is exported in a table <a name="pdf-package"><code>package</code></a>. 2312 <h4 margin-top="1em"><a name="Package.load"><tt>Package.load (mod_uri)</tt></a></h4>
2249
2250
2251 <p>
2252 <hr><h3><a name="pdf-require"><code>require (modname)</code></a></h3>
2253
2254 2313
2255 <p> 2314 <p>
2256 Loads the given module. 2315 Loads the given module.
2257 The function starts by looking into the <a href="#pdf-package.loaded"><code>package.loaded</code></a> table 2316 The function starts by looking into the <a href="#Package.loaded"><tt>Package.loaded</tt></a> table
2258 to determine whether <code>modname</code> is already loaded. 2317 to determine whether <tt>mod_uri</tt> is already loaded.
2259 If it is, then <code>require</code> returns the value stored 2318 If it is, then <tt>Package.load</tt> returns the value stored
2260 at <code>package.loaded[modname]</code>. 2319 at <tt>Package.loaded[mod_uri]</tt>.
2261 Otherwise, it tries to find a <em>loader</em> for the module. 2320 Otherwise, it tries to load a new value for the module.
2262 2321
2263 2322 <p>
2264 <p> 2323 To load a new value, <tt>Package.load</tt> first checks if <tt>mod_uri</tt> starts with "<b>java:</b>". If yes, then this is a Java class which is loaded by special Java code.
2265 To find a loader, 2324
2266 <code>require</code> is guided by the <a href="#pdf-package.searchers"><code>package.searchers</code></a> sequence. 2325 <p>
2267 By changing this sequence, 2326 If <tt>mod_uri</tt> is not a Java class, then <tt>Package.load</tt> tries to read the text of the file referred to by <tt>mod_uri</tt> (using <tt>add_extension</tt>=true). If the file doesn't exist, then <tt>Package.load</tt> returns <b>nil</b>. If the file exists, then its content is compiled into a chunk calling <a href="#Luan.load"><tt>Luan.load</tt></a> and passing in an empty table as the <tt>env</tt> value. This chunk is run passing in <tt>mod_uri</tt> as an argument. If the chunk returns a value other than <b>nil</b>, then that value is the value of this module. Otherwise the <tt>env</tt> that was passed in is the value of this module.
2268 we can change how <code>require</code> looks for a module. 2327
2269 The following explanation is based on the default configuration 2328 <p>
2270 for <a href="#pdf-package.searchers"><code>package.searchers</code></a>. 2329 If a new value for the module successful loaded, then it is stored in <tt>Package.loaded[mod_uri]</tt>. The value is returned.
2271 2330
2272 2331
2273 <p> 2332
2274 First <code>require</code> queries <code>package.preload[modname]</code>. 2333
2275 If it has a value, 2334 <h4 margin-top="1em"><a name="Package.loaded"><tt>Package.loaded</tt></a></h4>
2276 this value (which must be a function) is the loader. 2335
2277 Otherwise <code>require</code> searches for a Lua loader using the 2336
2278 path stored in <a href="#pdf-package.path"><code>package.path</code></a>. 2337 <p>
2279 If that also fails, it searches for a C&nbsp;loader using the 2338 A table used by <a href="#Package.load"><tt>Package.load</tt></a> to control which
2280 path stored in <a href="#pdf-package.cpath"><code>package.cpath</code></a>.
2281 If that also fails,
2282 it tries an <em>all-in-one</em> loader (see <a href="#pdf-package.searchers"><code>package.searchers</code></a>).
2283
2284
2285 <p>
2286 Once a loader is found,
2287 <code>require</code> calls the loader with two arguments:
2288 <code>modname</code> and an extra value dependent on how it got the loader.
2289 (If the loader came from a file,
2290 this extra value is the file name.)
2291 If the loader returns any non-nil value,
2292 <code>require</code> assigns the returned value to <code>package.loaded[modname]</code>.
2293 If the loader does not return a non-nil value and
2294 has not assigned any value to <code>package.loaded[modname]</code>,
2295 then <code>require</code> assigns <b>true</b> to this entry.
2296 In any case, <code>require</code> returns the
2297 final value of <code>package.loaded[modname]</code>.
2298
2299
2300 <p>
2301 If there is any error loading or running the module,
2302 or if it cannot find any loader for the module,
2303 then <code>require</code> raises an error.
2304
2305
2306
2307
2308 <p>
2309 <hr><h3><a name="pdf-package.config"><code>package.config</code></a></h3>
2310
2311
2312 <p>
2313 A string describing some compile-time configurations for packages.
2314 This string is a sequence of lines:
2315
2316 <ul>
2317
2318 <li>The first line is the directory separator string.
2319 Default is '<code>\</code>' for Windows and '<code>/</code>' for all other systems.</li>
2320
2321 <li>The second line is the character that separates templates in a path.
2322 Default is '<code>;</code>'.</li>
2323
2324 <li>The third line is the string that marks the
2325 substitution points in a template.
2326 Default is '<code>?</code>'.</li>
2327
2328 <li>The fourth line is a string that, in a path in Windows,
2329 is replaced by the executable's directory.
2330 Default is '<code>!</code>'.</li>
2331
2332 <li>The fifth line is a mark to ignore all text after it
2333 when building the <code>luaopen_</code> function name.
2334 Default is '<code>-</code>'.</li>
2335
2336 </ul>
2337
2338
2339
2340 <p>
2341 <hr><h3><a name="pdf-package.cpath"><code>package.cpath</code></a></h3>
2342
2343
2344 <p>
2345 The path used by <a href="#pdf-require"><code>require</code></a> to search for a C&nbsp;loader.
2346
2347
2348 <p>
2349 Lua initializes the C&nbsp;path <a href="#pdf-package.cpath"><code>package.cpath</code></a> in the same way
2350 it initializes the Lua path <a href="#pdf-package.path"><code>package.path</code></a>,
2351 using the environment variable <a name="pdf-LUA_CPATH_5_3"><code>LUA_CPATH_5_3</code></a>
2352 or the environment variable <a name="pdf-LUA_CPATH"><code>LUA_CPATH</code></a>
2353 or a default path defined in <code>luaconf.h</code>.
2354
2355
2356
2357
2358 <p>
2359 <hr><h3><a name="pdf-package.loaded"><code>package.loaded</code></a></h3>
2360
2361
2362 <p>
2363 A table used by <a href="#pdf-require"><code>require</code></a> to control which
2364 modules are already loaded. 2339 modules are already loaded.
2365 When you require a module <code>modname</code> and 2340 When you load a module <tt>mod_uri</tt> and
2366 <code>package.loaded[modname]</code> is not false, 2341 <tt>Package.loaded[modname]</tt> is not <b>nil</b>,
2367 <a href="#pdf-require"><code>require</code></a> simply returns the value stored there. 2342 <a href="#Package.load"><tt>Package.load</tt></a> simply returns the value stored there.
2368 2343
2369 2344
2370 <p> 2345 <p>
2371 This variable is only a reference to the real table; 2346 This variable is only a reference to the real table;
2372 assignments to this variable do not change the 2347 assignments to this variable do not change the
2373 table used by <a href="#pdf-require"><code>require</code></a>. 2348 table used by <a href="#Package.load"><tt>Package.load</tt></a>.
2374
2375
2376
2377
2378 <p>
2379 <hr><h3><a name="pdf-package.loadlib"><code>package.loadlib (libname, funcname)</code></a></h3>
2380
2381
2382 <p>
2383 Dynamically links the host program with the C&nbsp;library <code>libname</code>.
2384
2385
2386 <p>
2387 If <code>funcname</code> is "<code>*</code>",
2388 then it only links with the library,
2389 making the symbols exported by the library
2390 available to other dynamically linked libraries.
2391 Otherwise,
2392 it looks for a function <code>funcname</code> inside the library
2393 and returns this function as a C&nbsp;function.
2394 So, <code>funcname</code> must follow the <a href="#lua_CFunction"><code>lua_CFunction</code></a> prototype
2395 (see <a href="#lua_CFunction"><code>lua_CFunction</code></a>).
2396
2397
2398 <p>
2399 This is a low-level function.
2400 It completely bypasses the package and module system.
2401 Unlike <a href="#pdf-require"><code>require</code></a>,
2402 it does not perform any path searching and
2403 does not automatically adds extensions.
2404 <code>libname</code> must be the complete file name of the C&nbsp;library,
2405 including if necessary a path and an extension.
2406 <code>funcname</code> must be the exact name exported by the C&nbsp;library
2407 (which may depend on the C&nbsp;compiler and linker used).
2408
2409
2410 <p>
2411 This function is not supported by Standard&nbsp;C.
2412 As such, it is only available on some platforms
2413 (Windows, Linux, Mac OS X, Solaris, BSD,
2414 plus other Unix systems that support the <code>dlfcn</code> standard).
2415
2416
2417
2418
2419 <p>
2420 <hr><h3><a name="pdf-package.path"><code>package.path</code></a></h3>
2421
2422
2423 <p>
2424 The path used by <a href="#pdf-require"><code>require</code></a> to search for a Lua loader.
2425
2426
2427 <p>
2428 At start-up, Lua initializes this variable with
2429 the value of the environment variable <a name="pdf-LUA_PATH_5_3"><code>LUA_PATH_5_3</code></a> or
2430 the environment variable <a name="pdf-LUA_PATH"><code>LUA_PATH</code></a> or
2431 with a default path defined in <code>luaconf.h</code>,
2432 if those environment variables are not defined.
2433 Any "<code>;;</code>" in the value of the environment variable
2434 is replaced by the default path.
2435
2436
2437
2438
2439 <p>
2440 <hr><h3><a name="pdf-package.preload"><code>package.preload</code></a></h3>
2441
2442
2443 <p>
2444 A table to store loaders for specific modules
2445 (see <a href="#pdf-require"><code>require</code></a>).
2446
2447
2448 <p>
2449 This variable is only a reference to the real table;
2450 assignments to this variable do not change the
2451 table used by <a href="#pdf-require"><code>require</code></a>.
2452
2453
2454
2455
2456 <p>
2457 <hr><h3><a name="pdf-package.searchers"><code>package.searchers</code></a></h3>
2458
2459
2460 <p>
2461 A table used by <a href="#pdf-require"><code>require</code></a> to control how to load modules.
2462
2463
2464 <p>
2465 Each entry in this table is a <em>searcher function</em>.
2466 When looking for a module,
2467 <a href="#pdf-require"><code>require</code></a> calls each of these searchers in ascending order,
2468 with the module name (the argument given to <a href="#pdf-require"><code>require</code></a>) as its
2469 sole parameter.
2470 The function can return another function (the module <em>loader</em>)
2471 plus an extra value that will be passed to that loader,
2472 or a string explaining why it did not find that module
2473 (or <b>nil</b> if it has nothing to say).
2474
2475
2476 <p>
2477 Lua initializes this table with four searcher functions.
2478
2479
2480 <p>
2481 The first searcher simply looks for a loader in the
2482 <a href="#pdf-package.preload"><code>package.preload</code></a> table.
2483
2484
2485 <p>
2486 The second searcher looks for a loader as a Lua library,
2487 using the path stored at <a href="#pdf-package.path"><code>package.path</code></a>.
2488 The search is done as described in function <a href="#pdf-package.searchpath"><code>package.searchpath</code></a>.
2489
2490
2491 <p>
2492 The third searcher looks for a loader as a C&nbsp;library,
2493 using the path given by the variable <a href="#pdf-package.cpath"><code>package.cpath</code></a>.
2494 Again,
2495 the search is done as described in function <a href="#pdf-package.searchpath"><code>package.searchpath</code></a>.
2496 For instance,
2497 if the C&nbsp;path is the string
2498
2499 <pre>
2500 "./?.so;./?.dll;/usr/local/?/init.so"
2501 </pre><p>
2502 the searcher for module <code>foo</code>
2503 will try to open the files <code>./foo.so</code>, <code>./foo.dll</code>,
2504 and <code>/usr/local/foo/init.so</code>, in that order.
2505 Once it finds a C&nbsp;library,
2506 this searcher first uses a dynamic link facility to link the
2507 application with the library.
2508 Then it tries to find a C&nbsp;function inside the library to
2509 be used as the loader.
2510 The name of this C&nbsp;function is the string "<code>luaopen_</code>"
2511 concatenated with a copy of the module name where each dot
2512 is replaced by an underscore.
2513 Moreover, if the module name has a hyphen,
2514 its suffix after (and including) the first hyphen is removed.
2515 For instance, if the module name is <code>a.b.c-v2.1</code>,
2516 the function name will be <code>luaopen_a_b_c</code>.
2517
2518
2519 <p>
2520 The fourth searcher tries an <em>all-in-one loader</em>.
2521 It searches the C&nbsp;path for a library for
2522 the root name of the given module.
2523 For instance, when requiring <code>a.b.c</code>,
2524 it will search for a C&nbsp;library for <code>a</code>.
2525 If found, it looks into it for an open function for
2526 the submodule;
2527 in our example, that would be <code>luaopen_a_b_c</code>.
2528 With this facility, a package can pack several C&nbsp;submodules
2529 into one single library,
2530 with each submodule keeping its original open function.
2531
2532
2533 <p>
2534 All searchers except the first one (preload) return as the extra value
2535 the file name where the module was found,
2536 as returned by <a href="#pdf-package.searchpath"><code>package.searchpath</code></a>.
2537 The first searcher returns no extra value.
2538
2539
2540
2541
2542 <p>
2543 <hr><h3><a name="pdf-package.searchpath"><code>package.searchpath (name, path [, sep [, rep]])</code></a></h3>
2544
2545
2546 <p>
2547 Searches for the given <code>name</code> in the given <code>path</code>.
2548
2549
2550 <p>
2551 A path is a string containing a sequence of
2552 <em>templates</em> separated by semicolons.
2553 For each template,
2554 the function replaces each interrogation mark (if any)
2555 in the template with a copy of <code>name</code>
2556 wherein all occurrences of <code>sep</code>
2557 (a dot, by default)
2558 were replaced by <code>rep</code>
2559 (the system's directory separator, by default),
2560 and then tries to open the resulting file name.
2561
2562
2563 <p>
2564 For instance, if the path is the string
2565
2566 <pre>
2567 "./?.lua;./?.lc;/usr/local/?/init.lua"
2568 </pre><p>
2569 the search for the name <code>foo.a</code>
2570 will try to open the files
2571 <code>./foo/a.lua</code>, <code>./foo/a.lc</code>, and
2572 <code>/usr/local/foo/a/init.lua</code>, in that order.
2573
2574
2575 <p>
2576 Returns the resulting name of the first file that it can
2577 open in read mode (after closing the file),
2578 or <b>nil</b> plus an error message if none succeeds.
2579 (This error message lists all file names it tried to open.)
2580
2581 2349
2582 2350
2583 2351
2584 2352
2585 2353