diff options
Diffstat (limited to 'vendor/lua-5.4.8')
| -rw-r--r-- | vendor/lua-5.4.8/README | 6 | ||||
| -rw-r--r-- | vendor/lua-5.4.8/doc/OSIApproved_100X125.png | bin | 0 -> 12127 bytes | |||
| -rw-r--r-- | vendor/lua-5.4.8/doc/contents.html | 678 | ||||
| -rw-r--r-- | vendor/lua-5.4.8/doc/index.css | 21 | ||||
| -rw-r--r-- | vendor/lua-5.4.8/doc/logo.gif | bin | 0 -> 9893 bytes | |||
| -rw-r--r-- | vendor/lua-5.4.8/doc/lua.1 | 155 | ||||
| -rw-r--r-- | vendor/lua-5.4.8/doc/lua.css | 162 | ||||
| -rw-r--r-- | vendor/lua-5.4.8/doc/luac.1 | 118 | ||||
| -rw-r--r-- | vendor/lua-5.4.8/doc/manual.css | 21 | ||||
| -rw-r--r-- | vendor/lua-5.4.8/doc/manual.html | 12060 | ||||
| -rw-r--r-- | vendor/lua-5.4.8/doc/readme.html | 339 |
11 files changed, 13560 insertions, 0 deletions
diff --git a/vendor/lua-5.4.8/README b/vendor/lua-5.4.8/README new file mode 100644 index 0000000..b251d29 --- /dev/null +++ b/vendor/lua-5.4.8/README | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | |||
| 2 | This is Lua 5.4.8, released on 21 May 2025. | ||
| 3 | |||
| 4 | For installation instructions, license details, and | ||
| 5 | further information about Lua, see doc/readme.html. | ||
| 6 | |||
diff --git a/vendor/lua-5.4.8/doc/OSIApproved_100X125.png b/vendor/lua-5.4.8/doc/OSIApproved_100X125.png new file mode 100644 index 0000000..795f7a0 --- /dev/null +++ b/vendor/lua-5.4.8/doc/OSIApproved_100X125.png | |||
| Binary files differ | |||
diff --git a/vendor/lua-5.4.8/doc/contents.html b/vendor/lua-5.4.8/doc/contents.html new file mode 100644 index 0000000..18b677d --- /dev/null +++ b/vendor/lua-5.4.8/doc/contents.html | |||
| @@ -0,0 +1,678 @@ | |||
| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> | ||
| 2 | <HTML> | ||
| 3 | <HEAD> | ||
| 4 | <TITLE>Lua 5.4 Reference Manual - contents</TITLE> | ||
| 5 | <LINK REL="stylesheet" TYPE="text/css" HREF="lua.css"> | ||
| 6 | <LINK REL="stylesheet" TYPE="text/css" HREF="index.css"> | ||
| 7 | <META HTTP-EQUIV="content-type" CONTENT="text/html; charset=iso-8859-1"> | ||
| 8 | </HEAD> | ||
| 9 | |||
| 10 | <BODY> | ||
| 11 | |||
| 12 | <H1> | ||
| 13 | <A HREF="https://www.lua.org/"><IMG SRC="logo.gif" ALT="Lua"></A> | ||
| 14 | Lua 5.4 Reference Manual | ||
| 15 | </H1> | ||
| 16 | |||
| 17 | <P> | ||
| 18 | The reference manual is the official definition of the Lua language. | ||
| 19 | <BR> | ||
| 20 | For a complete introduction to Lua programming, see the book | ||
| 21 | <A HREF="https://www.lua.org/pil/">Programming in Lua</A>. | ||
| 22 | |||
| 23 | <DIV CLASS="menubar"> | ||
| 24 | <A HREF="manual.html">start</A> | ||
| 25 | · | ||
| 26 | <A HREF="#contents">contents</A> | ||
| 27 | · | ||
| 28 | <A HREF="#index">index</A> | ||
| 29 | · | ||
| 30 | <A HREF="https://www.lua.org/manual/">other versions</A> | ||
| 31 | </DIV> | ||
| 32 | |||
| 33 | <P> | ||
| 34 | <SMALL> | ||
| 35 | Copyright © 2020–2025 Lua.org, PUC-Rio. | ||
| 36 | Freely available under the terms of the | ||
| 37 | <A HREF="https://www.lua.org/license.html">Lua license</A>. | ||
| 38 | </SMALL> | ||
| 39 | |||
| 40 | <H2><A NAME="contents">Contents</A></H2> | ||
| 41 | <UL CLASS="contents menubar"> | ||
| 42 | <LI><A HREF="manual.html">1 – Introduction</A> | ||
| 43 | <P> | ||
| 44 | <LI><A HREF="manual.html#2">2 – Basic Concepts</A> | ||
| 45 | <UL> | ||
| 46 | <LI><A HREF="manual.html#2.1">2.1 – Values and Types</A> | ||
| 47 | <LI><A HREF="manual.html#2.2">2.2 – Environments and the Global Environment</A> | ||
| 48 | <LI><A HREF="manual.html#2.3">2.3 – Error Handling</A> | ||
| 49 | <LI><A HREF="manual.html#2.4">2.4 – Metatables and Metamethods</A> | ||
| 50 | <LI><A HREF="manual.html#2.5">2.5 – Garbage Collection</A> | ||
| 51 | <UL> | ||
| 52 | <LI><A HREF="manual.html#2.5.1">2.5.1 – Incremental Garbage Collection</A> | ||
| 53 | <LI><A HREF="manual.html#2.5.2">2.5.2 – Generational Garbage Collection</A> | ||
| 54 | <LI><A HREF="manual.html#2.5.3">2.5.3 – Garbage-Collection Metamethods</A> | ||
| 55 | <LI><A HREF="manual.html#2.5.4">2.5.4 – Weak Tables</A> | ||
| 56 | </UL> | ||
| 57 | <LI><A HREF="manual.html#2.6">2.6 – Coroutines</A> | ||
| 58 | </UL> | ||
| 59 | <P> | ||
| 60 | <LI><A HREF="manual.html#3">3 – The Language</A> | ||
| 61 | <UL> | ||
| 62 | <LI><A HREF="manual.html#3.1">3.1 – Lexical Conventions</A> | ||
| 63 | <LI><A HREF="manual.html#3.2">3.2 – Variables</A> | ||
| 64 | <LI><A HREF="manual.html#3.3">3.3 – Statements</A> | ||
| 65 | <UL> | ||
| 66 | <LI><A HREF="manual.html#3.3.1">3.3.1 – Blocks</A> | ||
| 67 | <LI><A HREF="manual.html#3.3.2">3.3.2 – Chunks</A> | ||
| 68 | <LI><A HREF="manual.html#3.3.3">3.3.3 – Assignment</A> | ||
| 69 | <LI><A HREF="manual.html#3.3.4">3.3.4 – Control Structures</A> | ||
| 70 | <LI><A HREF="manual.html#3.3.5">3.3.5 – For Statement</A> | ||
| 71 | <LI><A HREF="manual.html#3.3.6">3.3.6 – Function Calls as Statements</A> | ||
| 72 | <LI><A HREF="manual.html#3.3.7">3.3.7 – Local Declarations</A> | ||
| 73 | <LI><A HREF="manual.html#3.3.8">3.3.8 – To-be-closed Variables</A> | ||
| 74 | </UL> | ||
| 75 | <LI><A HREF="manual.html#3.4">3.4 – Expressions</A> | ||
| 76 | <UL> | ||
| 77 | <LI><A HREF="manual.html#3.4.1">3.4.1 – Arithmetic Operators</A> | ||
| 78 | <LI><A HREF="manual.html#3.4.2">3.4.2 – Bitwise Operators</A> | ||
| 79 | <LI><A HREF="manual.html#3.4.3">3.4.3 – Coercions and Conversions</A> | ||
| 80 | <LI><A HREF="manual.html#3.4.4">3.4.4 – Relational Operators</A> | ||
| 81 | <LI><A HREF="manual.html#3.4.5">3.4.5 – Logical Operators</A> | ||
| 82 | <LI><A HREF="manual.html#3.4.6">3.4.6 – Concatenation</A> | ||
| 83 | <LI><A HREF="manual.html#3.4.7">3.4.7 – The Length Operator</A> | ||
| 84 | <LI><A HREF="manual.html#3.4.8">3.4.8 – Precedence</A> | ||
| 85 | <LI><A HREF="manual.html#3.4.9">3.4.9 – Table Constructors</A> | ||
| 86 | <LI><A HREF="manual.html#3.4.10">3.4.10 – Function Calls</A> | ||
| 87 | <LI><A HREF="manual.html#3.4.11">3.4.11 – Function Definitions</A> | ||
| 88 | <LI><A HREF="manual.html#3.4.12">3.4.12 – Lists of expressions, multiple results, and adjustment<A> | ||
| 89 | |||
| 90 | </UL> | ||
| 91 | <LI><A HREF="manual.html#3.5">3.5 – Visibility Rules</A> | ||
| 92 | </UL> | ||
| 93 | <P> | ||
| 94 | <LI><A HREF="manual.html#4">4 – The Application Program Interface</A> | ||
| 95 | <UL> | ||
| 96 | <LI><A HREF="manual.html#4.1">4.1 – The Stack</A> | ||
| 97 | <UL> | ||
| 98 | <LI><A HREF="manual.html#4.1.1">4.1.1 – Stack Size</A> | ||
| 99 | <LI><A HREF="manual.html#4.1.2">4.1.2 – Valid and Acceptable Indices</A> | ||
| 100 | <LI><A HREF="manual.html#4.1.3">4.1.3 – Pointers to strings</A> | ||
| 101 | </UL> | ||
| 102 | <LI><A HREF="manual.html#4.2">4.2 – C Closures</A> | ||
| 103 | <LI><A HREF="manual.html#4.3">4.3 – Registry</A> | ||
| 104 | <LI><A HREF="manual.html#4.4">4.4 – Error Handling in C</A> | ||
| 105 | <UL> | ||
| 106 | <LI><A HREF="manual.html#4.4.1">4.4.1 – Status Codes</A> | ||
| 107 | </UL> | ||
| 108 | <LI><A HREF="manual.html#4.5">4.5 – Handling Yields in C</A> | ||
| 109 | <LI><A HREF="manual.html#4.6">4.6 – Functions and Types</A> | ||
| 110 | <LI><A HREF="manual.html#4.7">4.7 – The Debug Interface</A> | ||
| 111 | </UL> | ||
| 112 | <P> | ||
| 113 | <LI><A HREF="manual.html#5">5 – The Auxiliary Library</A> | ||
| 114 | <UL> | ||
| 115 | <LI><A HREF="manual.html#5.1">5.1 – Functions and Types</A> | ||
| 116 | </UL> | ||
| 117 | <P> | ||
| 118 | <LI><A HREF="manual.html#6">6 – The Standard Libraries</A> | ||
| 119 | <UL> | ||
| 120 | <LI><A HREF="manual.html#6.1">6.1 – Basic Functions</A> | ||
| 121 | <LI><A HREF="manual.html#6.2">6.2 – Coroutine Manipulation</A> | ||
| 122 | <LI><A HREF="manual.html#6.3">6.3 – Modules</A> | ||
| 123 | <LI><A HREF="manual.html#6.4">6.4 – String Manipulation</A> | ||
| 124 | <UL> | ||
| 125 | <LI><A HREF="manual.html#6.4.1">6.4.1 – Patterns</A> | ||
| 126 | <LI><A HREF="manual.html#6.4.2">6.4.2 – Format Strings for Pack and Unpack</A> | ||
| 127 | </UL> | ||
| 128 | <LI><A HREF="manual.html#6.5">6.5 – UTF-8 Support</A> | ||
| 129 | <LI><A HREF="manual.html#6.6">6.6 – Table Manipulation</A> | ||
| 130 | <LI><A HREF="manual.html#6.7">6.7 – Mathematical Functions</A> | ||
| 131 | <LI><A HREF="manual.html#6.8">6.8 – Input and Output Facilities</A> | ||
| 132 | <LI><A HREF="manual.html#6.9">6.9 – Operating System Facilities</A> | ||
| 133 | <LI><A HREF="manual.html#6.10">6.10 – The Debug Library</A> | ||
| 134 | </UL> | ||
| 135 | <P> | ||
| 136 | <LI><A HREF="manual.html#7">7 – Lua Standalone</A> | ||
| 137 | <P> | ||
| 138 | <LI><A HREF="manual.html#8">8 – Incompatibilities with the Previous Version</A> | ||
| 139 | <UL> | ||
| 140 | <LI><A HREF="manual.html#8.1">8.1 – Incompatibilities in the Language</A> | ||
| 141 | <LI><A HREF="manual.html#8.2">8.2 – Incompatibilities in the Libraries</A> | ||
| 142 | <LI><A HREF="manual.html#8.3">8.3 – Incompatibilities in the API</A> | ||
| 143 | </UL> | ||
| 144 | <P> | ||
| 145 | <LI><A HREF="manual.html#9">9 – The Complete Syntax of Lua</A> | ||
| 146 | </UL> | ||
| 147 | |||
| 148 | <H2><A NAME="index">Index</A></H2> | ||
| 149 | <TABLE CLASS="menubar" WIDTH="100%"> | ||
| 150 | <TR> | ||
| 151 | <TD> | ||
| 152 | <H3><A NAME="functions">Lua functions</A></H3> | ||
| 153 | <P> | ||
| 154 | <A HREF="manual.html#6.1">basic</A><BR> | ||
| 155 | <A HREF="manual.html#pdf-_G">_G</A><BR> | ||
| 156 | <A HREF="manual.html#pdf-_VERSION">_VERSION</A><BR> | ||
| 157 | <A HREF="manual.html#pdf-assert">assert</A><BR> | ||
| 158 | <A HREF="manual.html#pdf-collectgarbage">collectgarbage</A><BR> | ||
| 159 | <A HREF="manual.html#pdf-dofile">dofile</A><BR> | ||
| 160 | <A HREF="manual.html#pdf-error">error</A><BR> | ||
| 161 | <A HREF="manual.html#pdf-getmetatable">getmetatable</A><BR> | ||
| 162 | <A HREF="manual.html#pdf-ipairs">ipairs</A><BR> | ||
| 163 | <A HREF="manual.html#pdf-load">load</A><BR> | ||
| 164 | <A HREF="manual.html#pdf-loadfile">loadfile</A><BR> | ||
| 165 | <A HREF="manual.html#pdf-next">next</A><BR> | ||
| 166 | <A HREF="manual.html#pdf-pairs">pairs</A><BR> | ||
| 167 | <A HREF="manual.html#pdf-pcall">pcall</A><BR> | ||
| 168 | <A HREF="manual.html#pdf-print">print</A><BR> | ||
| 169 | <A HREF="manual.html#pdf-rawequal">rawequal</A><BR> | ||
| 170 | <A HREF="manual.html#pdf-rawget">rawget</A><BR> | ||
| 171 | <A HREF="manual.html#pdf-rawlen">rawlen</A><BR> | ||
| 172 | <A HREF="manual.html#pdf-rawset">rawset</A><BR> | ||
| 173 | <A HREF="manual.html#pdf-require">require</A><BR> | ||
| 174 | <A HREF="manual.html#pdf-select">select</A><BR> | ||
| 175 | <A HREF="manual.html#pdf-setmetatable">setmetatable</A><BR> | ||
| 176 | <A HREF="manual.html#pdf-tonumber">tonumber</A><BR> | ||
| 177 | <A HREF="manual.html#pdf-tostring">tostring</A><BR> | ||
| 178 | <A HREF="manual.html#pdf-type">type</A><BR> | ||
| 179 | <A HREF="manual.html#pdf-warn">warn</A><BR> | ||
| 180 | <A HREF="manual.html#pdf-xpcall">xpcall</A><BR> | ||
| 181 | |||
| 182 | <P> | ||
| 183 | <A HREF="manual.html#6.2">coroutine</A><BR> | ||
| 184 | <A HREF="manual.html#pdf-coroutine.close">coroutine.close</A><BR> | ||
| 185 | <A HREF="manual.html#pdf-coroutine.create">coroutine.create</A><BR> | ||
| 186 | <A HREF="manual.html#pdf-coroutine.isyieldable">coroutine.isyieldable</A><BR> | ||
| 187 | <A HREF="manual.html#pdf-coroutine.resume">coroutine.resume</A><BR> | ||
| 188 | <A HREF="manual.html#pdf-coroutine.running">coroutine.running</A><BR> | ||
| 189 | <A HREF="manual.html#pdf-coroutine.status">coroutine.status</A><BR> | ||
| 190 | <A HREF="manual.html#pdf-coroutine.wrap">coroutine.wrap</A><BR> | ||
| 191 | <A HREF="manual.html#pdf-coroutine.yield">coroutine.yield</A><BR> | ||
| 192 | |||
| 193 | <P> | ||
| 194 | <A HREF="manual.html#6.10">debug</A><BR> | ||
| 195 | <A HREF="manual.html#pdf-debug.debug">debug.debug</A><BR> | ||
| 196 | <A HREF="manual.html#pdf-debug.gethook">debug.gethook</A><BR> | ||
| 197 | <A HREF="manual.html#pdf-debug.getinfo">debug.getinfo</A><BR> | ||
| 198 | <A HREF="manual.html#pdf-debug.getlocal">debug.getlocal</A><BR> | ||
| 199 | <A HREF="manual.html#pdf-debug.getmetatable">debug.getmetatable</A><BR> | ||
| 200 | <A HREF="manual.html#pdf-debug.getregistry">debug.getregistry</A><BR> | ||
| 201 | <A HREF="manual.html#pdf-debug.getupvalue">debug.getupvalue</A><BR> | ||
| 202 | <A HREF="manual.html#pdf-debug.getuservalue">debug.getuservalue</A><BR> | ||
| 203 | <A HREF="manual.html#pdf-debug.sethook">debug.sethook</A><BR> | ||
| 204 | <A HREF="manual.html#pdf-debug.setlocal">debug.setlocal</A><BR> | ||
| 205 | <A HREF="manual.html#pdf-debug.setmetatable">debug.setmetatable</A><BR> | ||
| 206 | <A HREF="manual.html#pdf-debug.setupvalue">debug.setupvalue</A><BR> | ||
| 207 | <A HREF="manual.html#pdf-debug.setuservalue">debug.setuservalue</A><BR> | ||
| 208 | <A HREF="manual.html#pdf-debug.traceback">debug.traceback</A><BR> | ||
| 209 | <A HREF="manual.html#pdf-debug.upvalueid">debug.upvalueid</A><BR> | ||
| 210 | <A HREF="manual.html#pdf-debug.upvaluejoin">debug.upvaluejoin</A><BR> | ||
| 211 | |||
| 212 | <P> | ||
| 213 | <A HREF="manual.html#6.8">io</A><BR> | ||
| 214 | <A HREF="manual.html#pdf-io.close">io.close</A><BR> | ||
| 215 | <A HREF="manual.html#pdf-io.flush">io.flush</A><BR> | ||
| 216 | <A HREF="manual.html#pdf-io.input">io.input</A><BR> | ||
| 217 | <A HREF="manual.html#pdf-io.lines">io.lines</A><BR> | ||
| 218 | <A HREF="manual.html#pdf-io.open">io.open</A><BR> | ||
| 219 | <A HREF="manual.html#pdf-io.output">io.output</A><BR> | ||
| 220 | <A HREF="manual.html#pdf-io.popen">io.popen</A><BR> | ||
| 221 | <A HREF="manual.html#pdf-io.read">io.read</A><BR> | ||
| 222 | <A HREF="manual.html#pdf-io.stderr">io.stderr</A><BR> | ||
| 223 | <A HREF="manual.html#pdf-io.stdin">io.stdin</A><BR> | ||
| 224 | <A HREF="manual.html#pdf-io.stdout">io.stdout</A><BR> | ||
| 225 | <A HREF="manual.html#pdf-io.tmpfile">io.tmpfile</A><BR> | ||
| 226 | <A HREF="manual.html#pdf-io.type">io.type</A><BR> | ||
| 227 | <A HREF="manual.html#pdf-io.write">io.write</A><BR> | ||
| 228 | |||
| 229 | <A HREF="manual.html#pdf-file:close">file:close</A><BR> | ||
| 230 | <A HREF="manual.html#pdf-file:flush">file:flush</A><BR> | ||
| 231 | <A HREF="manual.html#pdf-file:lines">file:lines</A><BR> | ||
| 232 | <A HREF="manual.html#pdf-file:read">file:read</A><BR> | ||
| 233 | <A HREF="manual.html#pdf-file:seek">file:seek</A><BR> | ||
| 234 | <A HREF="manual.html#pdf-file:setvbuf">file:setvbuf</A><BR> | ||
| 235 | <A HREF="manual.html#pdf-file:write">file:write</A><BR> | ||
| 236 | |||
| 237 | </TD> | ||
| 238 | <TD> | ||
| 239 | <H3> </H3> | ||
| 240 | <P> | ||
| 241 | <A HREF="manual.html#6.7">math</A><BR> | ||
| 242 | <A HREF="manual.html#pdf-math.abs">math.abs</A><BR> | ||
| 243 | <A HREF="manual.html#pdf-math.acos">math.acos</A><BR> | ||
| 244 | <A HREF="manual.html#pdf-math.asin">math.asin</A><BR> | ||
| 245 | <A HREF="manual.html#pdf-math.atan">math.atan</A><BR> | ||
| 246 | <A HREF="manual.html#pdf-math.ceil">math.ceil</A><BR> | ||
| 247 | <A HREF="manual.html#pdf-math.cos">math.cos</A><BR> | ||
| 248 | <A HREF="manual.html#pdf-math.deg">math.deg</A><BR> | ||
| 249 | <A HREF="manual.html#pdf-math.exp">math.exp</A><BR> | ||
| 250 | <A HREF="manual.html#pdf-math.floor">math.floor</A><BR> | ||
| 251 | <A HREF="manual.html#pdf-math.fmod">math.fmod</A><BR> | ||
| 252 | <A HREF="manual.html#pdf-math.huge">math.huge</A><BR> | ||
| 253 | <A HREF="manual.html#pdf-math.log">math.log</A><BR> | ||
| 254 | <A HREF="manual.html#pdf-math.max">math.max</A><BR> | ||
| 255 | <A HREF="manual.html#pdf-math.maxinteger">math.maxinteger</A><BR> | ||
| 256 | <A HREF="manual.html#pdf-math.min">math.min</A><BR> | ||
| 257 | <A HREF="manual.html#pdf-math.mininteger">math.mininteger</A><BR> | ||
| 258 | <A HREF="manual.html#pdf-math.modf">math.modf</A><BR> | ||
| 259 | <A HREF="manual.html#pdf-math.pi">math.pi</A><BR> | ||
| 260 | <A HREF="manual.html#pdf-math.rad">math.rad</A><BR> | ||
| 261 | <A HREF="manual.html#pdf-math.random">math.random</A><BR> | ||
| 262 | <A HREF="manual.html#pdf-math.randomseed">math.randomseed</A><BR> | ||
| 263 | <A HREF="manual.html#pdf-math.sin">math.sin</A><BR> | ||
| 264 | <A HREF="manual.html#pdf-math.sqrt">math.sqrt</A><BR> | ||
| 265 | <A HREF="manual.html#pdf-math.tan">math.tan</A><BR> | ||
| 266 | <A HREF="manual.html#pdf-math.tointeger">math.tointeger</A><BR> | ||
| 267 | <A HREF="manual.html#pdf-math.type">math.type</A><BR> | ||
| 268 | <A HREF="manual.html#pdf-math.ult">math.ult</A><BR> | ||
| 269 | |||
| 270 | <P> | ||
| 271 | <A HREF="manual.html#6.9">os</A><BR> | ||
| 272 | <A HREF="manual.html#pdf-os.clock">os.clock</A><BR> | ||
| 273 | <A HREF="manual.html#pdf-os.date">os.date</A><BR> | ||
| 274 | <A HREF="manual.html#pdf-os.difftime">os.difftime</A><BR> | ||
| 275 | <A HREF="manual.html#pdf-os.execute">os.execute</A><BR> | ||
| 276 | <A HREF="manual.html#pdf-os.exit">os.exit</A><BR> | ||
| 277 | <A HREF="manual.html#pdf-os.getenv">os.getenv</A><BR> | ||
| 278 | <A HREF="manual.html#pdf-os.remove">os.remove</A><BR> | ||
| 279 | <A HREF="manual.html#pdf-os.rename">os.rename</A><BR> | ||
| 280 | <A HREF="manual.html#pdf-os.setlocale">os.setlocale</A><BR> | ||
| 281 | <A HREF="manual.html#pdf-os.time">os.time</A><BR> | ||
| 282 | <A HREF="manual.html#pdf-os.tmpname">os.tmpname</A><BR> | ||
| 283 | |||
| 284 | <P> | ||
| 285 | <A HREF="manual.html#6.3">package</A><BR> | ||
| 286 | <A HREF="manual.html#pdf-package.config">package.config</A><BR> | ||
| 287 | <A HREF="manual.html#pdf-package.cpath">package.cpath</A><BR> | ||
| 288 | <A HREF="manual.html#pdf-package.loaded">package.loaded</A><BR> | ||
| 289 | <A HREF="manual.html#pdf-package.loadlib">package.loadlib</A><BR> | ||
| 290 | <A HREF="manual.html#pdf-package.path">package.path</A><BR> | ||
| 291 | <A HREF="manual.html#pdf-package.preload">package.preload</A><BR> | ||
| 292 | <A HREF="manual.html#pdf-package.searchers">package.searchers</A><BR> | ||
| 293 | <A HREF="manual.html#pdf-package.searchpath">package.searchpath</A><BR> | ||
| 294 | |||
| 295 | <P> | ||
| 296 | <A HREF="manual.html#6.4">string</A><BR> | ||
| 297 | <A HREF="manual.html#pdf-string.byte">string.byte</A><BR> | ||
| 298 | <A HREF="manual.html#pdf-string.char">string.char</A><BR> | ||
| 299 | <A HREF="manual.html#pdf-string.dump">string.dump</A><BR> | ||
| 300 | <A HREF="manual.html#pdf-string.find">string.find</A><BR> | ||
| 301 | <A HREF="manual.html#pdf-string.format">string.format</A><BR> | ||
| 302 | <A HREF="manual.html#pdf-string.gmatch">string.gmatch</A><BR> | ||
| 303 | <A HREF="manual.html#pdf-string.gsub">string.gsub</A><BR> | ||
| 304 | <A HREF="manual.html#pdf-string.len">string.len</A><BR> | ||
| 305 | <A HREF="manual.html#pdf-string.lower">string.lower</A><BR> | ||
| 306 | <A HREF="manual.html#pdf-string.match">string.match</A><BR> | ||
| 307 | <A HREF="manual.html#pdf-string.pack">string.pack</A><BR> | ||
| 308 | <A HREF="manual.html#pdf-string.packsize">string.packsize</A><BR> | ||
| 309 | <A HREF="manual.html#pdf-string.rep">string.rep</A><BR> | ||
| 310 | <A HREF="manual.html#pdf-string.reverse">string.reverse</A><BR> | ||
| 311 | <A HREF="manual.html#pdf-string.sub">string.sub</A><BR> | ||
| 312 | <A HREF="manual.html#pdf-string.unpack">string.unpack</A><BR> | ||
| 313 | <A HREF="manual.html#pdf-string.upper">string.upper</A><BR> | ||
| 314 | |||
| 315 | <P> | ||
| 316 | <A HREF="manual.html#6.6">table</A><BR> | ||
| 317 | <A HREF="manual.html#pdf-table.concat">table.concat</A><BR> | ||
| 318 | <A HREF="manual.html#pdf-table.insert">table.insert</A><BR> | ||
| 319 | <A HREF="manual.html#pdf-table.move">table.move</A><BR> | ||
| 320 | <A HREF="manual.html#pdf-table.pack">table.pack</A><BR> | ||
| 321 | <A HREF="manual.html#pdf-table.remove">table.remove</A><BR> | ||
| 322 | <A HREF="manual.html#pdf-table.sort">table.sort</A><BR> | ||
| 323 | <A HREF="manual.html#pdf-table.unpack">table.unpack</A><BR> | ||
| 324 | |||
| 325 | <P> | ||
| 326 | <A HREF="manual.html#6.5">utf8</A><BR> | ||
| 327 | <A HREF="manual.html#pdf-utf8.char">utf8.char</A><BR> | ||
| 328 | <A HREF="manual.html#pdf-utf8.charpattern">utf8.charpattern</A><BR> | ||
| 329 | <A HREF="manual.html#pdf-utf8.codepoint">utf8.codepoint</A><BR> | ||
| 330 | <A HREF="manual.html#pdf-utf8.codes">utf8.codes</A><BR> | ||
| 331 | <A HREF="manual.html#pdf-utf8.len">utf8.len</A><BR> | ||
| 332 | <A HREF="manual.html#pdf-utf8.offset">utf8.offset</A><BR> | ||
| 333 | |||
| 334 | <H3><A NAME="metamethods">metamethods</A></H3> | ||
| 335 | <P> | ||
| 336 | <A HREF="manual.html#2.4">__add</A><BR> | ||
| 337 | <A HREF="manual.html#2.4">__band</A><BR> | ||
| 338 | <A HREF="manual.html#2.4">__bnot</A><BR> | ||
| 339 | <A HREF="manual.html#2.4">__bor</A><BR> | ||
| 340 | <A HREF="manual.html#2.4">__bxor</A><BR> | ||
| 341 | <A HREF="manual.html#2.4">__call</A><BR> | ||
| 342 | <A HREF="manual.html#3.3.8">__close</A><BR> | ||
| 343 | <A HREF="manual.html#2.4">__concat</A><BR> | ||
| 344 | <A HREF="manual.html#2.4">__div</A><BR> | ||
| 345 | <A HREF="manual.html#2.4">__eq</A><BR> | ||
| 346 | <A HREF="manual.html#2.5.3">__gc</A><BR> | ||
| 347 | <A HREF="manual.html#2.4">__idiv</A><BR> | ||
| 348 | <A HREF="manual.html#2.4">__index</A><BR> | ||
| 349 | <A HREF="manual.html#2.4">__le</A><BR> | ||
| 350 | <A HREF="manual.html#2.4">__len</A><BR> | ||
| 351 | <A HREF="manual.html#2.4">__lt</A><BR> | ||
| 352 | <A HREF="manual.html#pdf-getmetatable">__metatable</A><BR> | ||
| 353 | <A HREF="manual.html#2.4">__mod</A><BR> | ||
| 354 | <A HREF="manual.html#2.5.4">__mode</A><BR> | ||
| 355 | <A HREF="manual.html#2.4">__mul</A><BR> | ||
| 356 | <A HREF="manual.html#luaL_newmetatable">__name</A><BR> | ||
| 357 | <A HREF="manual.html#2.4">__newindex</A><BR> | ||
| 358 | <A HREF="manual.html#pdf-pairs">__pairs</A><BR> | ||
| 359 | <A HREF="manual.html#2.4">__pow</A><BR> | ||
| 360 | <A HREF="manual.html#2.4">__shl</A><BR> | ||
| 361 | <A HREF="manual.html#2.4">__shr</A><BR> | ||
| 362 | <A HREF="manual.html#2.4">__sub</A><BR> | ||
| 363 | <A HREF="manual.html#pdf-tostring">__tostring</A><BR> | ||
| 364 | <A HREF="manual.html#2.4">__unm</A><BR> | ||
| 365 | |||
| 366 | <H3><A NAME="env">environment<BR>variables</A></H3> | ||
| 367 | <P> | ||
| 368 | <A HREF="manual.html#pdf-LUA_CPATH">LUA_CPATH</A><BR> | ||
| 369 | <A HREF="manual.html#pdf-LUA_CPATH_5_4">LUA_CPATH_5_4</A><BR> | ||
| 370 | <A HREF="manual.html#pdf-LUA_INIT">LUA_INIT</A><BR> | ||
| 371 | <A HREF="manual.html#pdf-LUA_INIT_5_4">LUA_INIT_5_4</A><BR> | ||
| 372 | <A HREF="manual.html#pdf-LUA_PATH">LUA_PATH</A><BR> | ||
| 373 | <A HREF="manual.html#pdf-LUA_PATH_5_4">LUA_PATH_5_4</A><BR> | ||
| 374 | |||
| 375 | </TD> | ||
| 376 | <TD> | ||
| 377 | <H3><A NAME="api">C API</A></H3> | ||
| 378 | <P> | ||
| 379 | <A HREF="manual.html#lua_Alloc">lua_Alloc</A><BR> | ||
| 380 | <A HREF="manual.html#lua_CFunction">lua_CFunction</A><BR> | ||
| 381 | <A HREF="manual.html#lua_Debug">lua_Debug</A><BR> | ||
| 382 | <A HREF="manual.html#lua_Hook">lua_Hook</A><BR> | ||
| 383 | <A HREF="manual.html#lua_Integer">lua_Integer</A><BR> | ||
| 384 | <A HREF="manual.html#lua_KContext">lua_KContext</A><BR> | ||
| 385 | <A HREF="manual.html#lua_KFunction">lua_KFunction</A><BR> | ||
| 386 | <A HREF="manual.html#lua_Number">lua_Number</A><BR> | ||
| 387 | <A HREF="manual.html#lua_Reader">lua_Reader</A><BR> | ||
| 388 | <A HREF="manual.html#lua_State">lua_State</A><BR> | ||
| 389 | <A HREF="manual.html#lua_Unsigned">lua_Unsigned</A><BR> | ||
| 390 | <A HREF="manual.html#lua_WarnFunction">lua_WarnFunction</A><BR> | ||
| 391 | <A HREF="manual.html#lua_Writer">lua_Writer</A><BR> | ||
| 392 | |||
| 393 | <P> | ||
| 394 | <A HREF="manual.html#lua_absindex">lua_absindex</A><BR> | ||
| 395 | <A HREF="manual.html#lua_arith">lua_arith</A><BR> | ||
| 396 | <A HREF="manual.html#lua_atpanic">lua_atpanic</A><BR> | ||
| 397 | <A HREF="manual.html#lua_call">lua_call</A><BR> | ||
| 398 | <A HREF="manual.html#lua_callk">lua_callk</A><BR> | ||
| 399 | <A HREF="manual.html#lua_checkstack">lua_checkstack</A><BR> | ||
| 400 | <A HREF="manual.html#lua_close">lua_close</A><BR> | ||
| 401 | <A HREF="manual.html#lua_closeslot">lua_closeslot</A><BR> | ||
| 402 | <A HREF="manual.html#lua_compare">lua_compare</A><BR> | ||
| 403 | <A HREF="manual.html#lua_concat">lua_concat</A><BR> | ||
| 404 | <A HREF="manual.html#lua_copy">lua_copy</A><BR> | ||
| 405 | <A HREF="manual.html#lua_createtable">lua_createtable</A><BR> | ||
| 406 | <A HREF="manual.html#lua_dump">lua_dump</A><BR> | ||
| 407 | <A HREF="manual.html#lua_error">lua_error</A><BR> | ||
| 408 | <A HREF="manual.html#lua_gc">lua_gc</A><BR> | ||
| 409 | <A HREF="manual.html#lua_getallocf">lua_getallocf</A><BR> | ||
| 410 | <A HREF="manual.html#lua_getextraspace">lua_getextraspace</A><BR> | ||
| 411 | <A HREF="manual.html#lua_getfield">lua_getfield</A><BR> | ||
| 412 | <A HREF="manual.html#lua_getglobal">lua_getglobal</A><BR> | ||
| 413 | <A HREF="manual.html#lua_gethook">lua_gethook</A><BR> | ||
| 414 | <A HREF="manual.html#lua_gethookcount">lua_gethookcount</A><BR> | ||
| 415 | <A HREF="manual.html#lua_gethookmask">lua_gethookmask</A><BR> | ||
| 416 | <A HREF="manual.html#lua_geti">lua_geti</A><BR> | ||
| 417 | <A HREF="manual.html#lua_getinfo">lua_getinfo</A><BR> | ||
| 418 | <A HREF="manual.html#lua_getiuservalue">lua_getiuservalue</A><BR> | ||
| 419 | <A HREF="manual.html#lua_getlocal">lua_getlocal</A><BR> | ||
| 420 | <A HREF="manual.html#lua_getmetatable">lua_getmetatable</A><BR> | ||
| 421 | <A HREF="manual.html#lua_getstack">lua_getstack</A><BR> | ||
| 422 | <A HREF="manual.html#lua_gettable">lua_gettable</A><BR> | ||
| 423 | <A HREF="manual.html#lua_gettop">lua_gettop</A><BR> | ||
| 424 | <A HREF="manual.html#lua_getupvalue">lua_getupvalue</A><BR> | ||
| 425 | <A HREF="manual.html#lua_insert">lua_insert</A><BR> | ||
| 426 | <A HREF="manual.html#lua_isboolean">lua_isboolean</A><BR> | ||
| 427 | <A HREF="manual.html#lua_iscfunction">lua_iscfunction</A><BR> | ||
| 428 | <A HREF="manual.html#lua_isfunction">lua_isfunction</A><BR> | ||
| 429 | <A HREF="manual.html#lua_isinteger">lua_isinteger</A><BR> | ||
| 430 | <A HREF="manual.html#lua_islightuserdata">lua_islightuserdata</A><BR> | ||
| 431 | <A HREF="manual.html#lua_isnil">lua_isnil</A><BR> | ||
| 432 | <A HREF="manual.html#lua_isnone">lua_isnone</A><BR> | ||
| 433 | <A HREF="manual.html#lua_isnoneornil">lua_isnoneornil</A><BR> | ||
| 434 | <A HREF="manual.html#lua_isnumber">lua_isnumber</A><BR> | ||
| 435 | <A HREF="manual.html#lua_isstring">lua_isstring</A><BR> | ||
| 436 | <A HREF="manual.html#lua_istable">lua_istable</A><BR> | ||
| 437 | <A HREF="manual.html#lua_isthread">lua_isthread</A><BR> | ||
| 438 | <A HREF="manual.html#lua_isuserdata">lua_isuserdata</A><BR> | ||
| 439 | <A HREF="manual.html#lua_isyieldable">lua_isyieldable</A><BR> | ||
| 440 | <A HREF="manual.html#lua_len">lua_len</A><BR> | ||
| 441 | <A HREF="manual.html#lua_load">lua_load</A><BR> | ||
| 442 | <A HREF="manual.html#lua_newstate">lua_newstate</A><BR> | ||
| 443 | <A HREF="manual.html#lua_newtable">lua_newtable</A><BR> | ||
| 444 | <A HREF="manual.html#lua_newthread">lua_newthread</A><BR> | ||
| 445 | <A HREF="manual.html#lua_newuserdatauv">lua_newuserdatauv</A><BR> | ||
| 446 | <A HREF="manual.html#lua_next">lua_next</A><BR> | ||
| 447 | <A HREF="manual.html#lua_numbertointeger">lua_numbertointeger</A><BR> | ||
| 448 | <A HREF="manual.html#lua_pcall">lua_pcall</A><BR> | ||
| 449 | <A HREF="manual.html#lua_pcallk">lua_pcallk</A><BR> | ||
| 450 | <A HREF="manual.html#lua_pop">lua_pop</A><BR> | ||
| 451 | <A HREF="manual.html#lua_pushboolean">lua_pushboolean</A><BR> | ||
| 452 | <A HREF="manual.html#lua_pushcclosure">lua_pushcclosure</A><BR> | ||
| 453 | <A HREF="manual.html#lua_pushcfunction">lua_pushcfunction</A><BR> | ||
| 454 | <A HREF="manual.html#lua_pushfstring">lua_pushfstring</A><BR> | ||
| 455 | <A HREF="manual.html#lua_pushglobaltable">lua_pushglobaltable</A><BR> | ||
| 456 | <A HREF="manual.html#lua_pushinteger">lua_pushinteger</A><BR> | ||
| 457 | <A HREF="manual.html#lua_pushlightuserdata">lua_pushlightuserdata</A><BR> | ||
| 458 | <A HREF="manual.html#lua_pushliteral">lua_pushliteral</A><BR> | ||
| 459 | <A HREF="manual.html#lua_pushlstring">lua_pushlstring</A><BR> | ||
| 460 | <A HREF="manual.html#lua_pushnil">lua_pushnil</A><BR> | ||
| 461 | <A HREF="manual.html#lua_pushnumber">lua_pushnumber</A><BR> | ||
| 462 | <A HREF="manual.html#lua_pushstring">lua_pushstring</A><BR> | ||
| 463 | <A HREF="manual.html#lua_pushthread">lua_pushthread</A><BR> | ||
| 464 | <A HREF="manual.html#lua_pushvalue">lua_pushvalue</A><BR> | ||
| 465 | <A HREF="manual.html#lua_pushvfstring">lua_pushvfstring</A><BR> | ||
| 466 | <A HREF="manual.html#lua_rawequal">lua_rawequal</A><BR> | ||
| 467 | <A HREF="manual.html#lua_rawget">lua_rawget</A><BR> | ||
| 468 | <A HREF="manual.html#lua_rawgeti">lua_rawgeti</A><BR> | ||
| 469 | <A HREF="manual.html#lua_rawgetp">lua_rawgetp</A><BR> | ||
| 470 | <A HREF="manual.html#lua_rawlen">lua_rawlen</A><BR> | ||
| 471 | <A HREF="manual.html#lua_rawset">lua_rawset</A><BR> | ||
| 472 | <A HREF="manual.html#lua_rawseti">lua_rawseti</A><BR> | ||
| 473 | <A HREF="manual.html#lua_rawsetp">lua_rawsetp</A><BR> | ||
| 474 | <A HREF="manual.html#lua_register">lua_register</A><BR> | ||
| 475 | <A HREF="manual.html#lua_remove">lua_remove</A><BR> | ||
| 476 | <A HREF="manual.html#lua_replace">lua_replace</A><BR> | ||
| 477 | <A HREF="manual.html#lua_resetthread">lua_resetthread</A><BR> | ||
| 478 | <A HREF="manual.html#lua_resume">lua_resume</A><BR> | ||
| 479 | <A HREF="manual.html#lua_rotate">lua_rotate</A><BR> | ||
| 480 | <A HREF="manual.html#lua_setallocf">lua_setallocf</A><BR> | ||
| 481 | <A HREF="manual.html#lua_setfield">lua_setfield</A><BR> | ||
| 482 | <A HREF="manual.html#lua_setglobal">lua_setglobal</A><BR> | ||
| 483 | <A HREF="manual.html#lua_sethook">lua_sethook</A><BR> | ||
| 484 | <A HREF="manual.html#lua_seti">lua_seti</A><BR> | ||
| 485 | <A HREF="manual.html#lua_setiuservalue">lua_setiuservalue</A><BR> | ||
| 486 | <A HREF="manual.html#lua_setlocal">lua_setlocal</A><BR> | ||
| 487 | <A HREF="manual.html#lua_setmetatable">lua_setmetatable</A><BR> | ||
| 488 | <A HREF="manual.html#lua_settable">lua_settable</A><BR> | ||
| 489 | <A HREF="manual.html#lua_settop">lua_settop</A><BR> | ||
| 490 | <A HREF="manual.html#lua_setupvalue">lua_setupvalue</A><BR> | ||
| 491 | <A HREF="manual.html#lua_setwarnf">lua_setwarnf</A><BR> | ||
| 492 | <A HREF="manual.html#lua_status">lua_status</A><BR> | ||
| 493 | <A HREF="manual.html#lua_stringtonumber">lua_stringtonumber</A><BR> | ||
| 494 | <A HREF="manual.html#lua_toboolean">lua_toboolean</A><BR> | ||
| 495 | <A HREF="manual.html#lua_tocfunction">lua_tocfunction</A><BR> | ||
| 496 | <A HREF="manual.html#lua_toclose">lua_toclose</A><BR> | ||
| 497 | <A HREF="manual.html#lua_tointeger">lua_tointeger</A><BR> | ||
| 498 | <A HREF="manual.html#lua_tointegerx">lua_tointegerx</A><BR> | ||
| 499 | <A HREF="manual.html#lua_tolstring">lua_tolstring</A><BR> | ||
| 500 | <A HREF="manual.html#lua_tonumber">lua_tonumber</A><BR> | ||
| 501 | <A HREF="manual.html#lua_tonumberx">lua_tonumberx</A><BR> | ||
| 502 | <A HREF="manual.html#lua_topointer">lua_topointer</A><BR> | ||
| 503 | <A HREF="manual.html#lua_tostring">lua_tostring</A><BR> | ||
| 504 | <A HREF="manual.html#lua_tothread">lua_tothread</A><BR> | ||
| 505 | <A HREF="manual.html#lua_touserdata">lua_touserdata</A><BR> | ||
| 506 | <A HREF="manual.html#lua_type">lua_type</A><BR> | ||
| 507 | <A HREF="manual.html#lua_typename">lua_typename</A><BR> | ||
| 508 | <A HREF="manual.html#lua_upvalueid">lua_upvalueid</A><BR> | ||
| 509 | <A HREF="manual.html#lua_upvalueindex">lua_upvalueindex</A><BR> | ||
| 510 | <A HREF="manual.html#lua_upvaluejoin">lua_upvaluejoin</A><BR> | ||
| 511 | <A HREF="manual.html#lua_version">lua_version</A><BR> | ||
| 512 | <A HREF="manual.html#lua_warning">lua_warning</A><BR> | ||
| 513 | <A HREF="manual.html#lua_xmove">lua_xmove</A><BR> | ||
| 514 | <A HREF="manual.html#lua_yield">lua_yield</A><BR> | ||
| 515 | <A HREF="manual.html#lua_yieldk">lua_yieldk</A><BR> | ||
| 516 | |||
| 517 | </TD> | ||
| 518 | <TD> | ||
| 519 | <H3><A NAME="auxlib">auxiliary library</A></H3> | ||
| 520 | <P> | ||
| 521 | <A HREF="manual.html#luaL_Buffer">luaL_Buffer</A><BR> | ||
| 522 | <A HREF="manual.html#luaL_Reg">luaL_Reg</A><BR> | ||
| 523 | <A HREF="manual.html#luaL_Stream">luaL_Stream</A><BR> | ||
| 524 | |||
| 525 | <P> | ||
| 526 | <A HREF="manual.html#luaL_addchar">luaL_addchar</A><BR> | ||
| 527 | <A HREF="manual.html#luaL_addgsub">luaL_addgsub</A><BR> | ||
| 528 | <A HREF="manual.html#luaL_addlstring">luaL_addlstring</A><BR> | ||
| 529 | <A HREF="manual.html#luaL_addsize">luaL_addsize</A><BR> | ||
| 530 | <A HREF="manual.html#luaL_addstring">luaL_addstring</A><BR> | ||
| 531 | <A HREF="manual.html#luaL_addvalue">luaL_addvalue</A><BR> | ||
| 532 | <A HREF="manual.html#luaL_argcheck">luaL_argcheck</A><BR> | ||
| 533 | <A HREF="manual.html#luaL_argerror">luaL_argerror</A><BR> | ||
| 534 | <A HREF="manual.html#luaL_argexpected">luaL_argexpected</A><BR> | ||
| 535 | <A HREF="manual.html#luaL_buffaddr">luaL_buffaddr</A><BR> | ||
| 536 | <A HREF="manual.html#luaL_buffinit">luaL_buffinit</A><BR> | ||
| 537 | <A HREF="manual.html#luaL_buffinitsize">luaL_buffinitsize</A><BR> | ||
| 538 | <A HREF="manual.html#luaL_bufflen">luaL_bufflen</A><BR> | ||
| 539 | <A HREF="manual.html#luaL_buffsub">luaL_buffsub</A><BR> | ||
| 540 | <A HREF="manual.html#luaL_callmeta">luaL_callmeta</A><BR> | ||
| 541 | <A HREF="manual.html#luaL_checkany">luaL_checkany</A><BR> | ||
| 542 | <A HREF="manual.html#luaL_checkinteger">luaL_checkinteger</A><BR> | ||
| 543 | <A HREF="manual.html#luaL_checklstring">luaL_checklstring</A><BR> | ||
| 544 | <A HREF="manual.html#luaL_checknumber">luaL_checknumber</A><BR> | ||
| 545 | <A HREF="manual.html#luaL_checkoption">luaL_checkoption</A><BR> | ||
| 546 | <A HREF="manual.html#luaL_checkstack">luaL_checkstack</A><BR> | ||
| 547 | <A HREF="manual.html#luaL_checkstring">luaL_checkstring</A><BR> | ||
| 548 | <A HREF="manual.html#luaL_checktype">luaL_checktype</A><BR> | ||
| 549 | <A HREF="manual.html#luaL_checkudata">luaL_checkudata</A><BR> | ||
| 550 | <A HREF="manual.html#luaL_checkversion">luaL_checkversion</A><BR> | ||
| 551 | <A HREF="manual.html#luaL_dofile">luaL_dofile</A><BR> | ||
| 552 | <A HREF="manual.html#luaL_dostring">luaL_dostring</A><BR> | ||
| 553 | <A HREF="manual.html#luaL_error">luaL_error</A><BR> | ||
| 554 | <A HREF="manual.html#luaL_execresult">luaL_execresult</A><BR> | ||
| 555 | <A HREF="manual.html#luaL_fileresult">luaL_fileresult</A><BR> | ||
| 556 | <A HREF="manual.html#luaL_getmetafield">luaL_getmetafield</A><BR> | ||
| 557 | <A HREF="manual.html#luaL_getmetatable">luaL_getmetatable</A><BR> | ||
| 558 | <A HREF="manual.html#luaL_getsubtable">luaL_getsubtable</A><BR> | ||
| 559 | <A HREF="manual.html#luaL_gsub">luaL_gsub</A><BR> | ||
| 560 | <A HREF="manual.html#luaL_len">luaL_len</A><BR> | ||
| 561 | <A HREF="manual.html#luaL_loadbuffer">luaL_loadbuffer</A><BR> | ||
| 562 | <A HREF="manual.html#luaL_loadbufferx">luaL_loadbufferx</A><BR> | ||
| 563 | <A HREF="manual.html#luaL_loadfile">luaL_loadfile</A><BR> | ||
| 564 | <A HREF="manual.html#luaL_loadfilex">luaL_loadfilex</A><BR> | ||
| 565 | <A HREF="manual.html#luaL_loadstring">luaL_loadstring</A><BR> | ||
| 566 | <A HREF="manual.html#luaL_newlib">luaL_newlib</A><BR> | ||
| 567 | <A HREF="manual.html#luaL_newlibtable">luaL_newlibtable</A><BR> | ||
| 568 | <A HREF="manual.html#luaL_newmetatable">luaL_newmetatable</A><BR> | ||
| 569 | <A HREF="manual.html#luaL_newstate">luaL_newstate</A><BR> | ||
| 570 | <A HREF="manual.html#luaL_openlibs">luaL_openlibs</A><BR> | ||
| 571 | <A HREF="manual.html#luaL_opt">luaL_opt</A><BR> | ||
| 572 | <A HREF="manual.html#luaL_optinteger">luaL_optinteger</A><BR> | ||
| 573 | <A HREF="manual.html#luaL_optlstring">luaL_optlstring</A><BR> | ||
| 574 | <A HREF="manual.html#luaL_optnumber">luaL_optnumber</A><BR> | ||
| 575 | <A HREF="manual.html#luaL_optstring">luaL_optstring</A><BR> | ||
| 576 | <A HREF="manual.html#luaL_prepbuffer">luaL_prepbuffer</A><BR> | ||
| 577 | <A HREF="manual.html#luaL_prepbuffsize">luaL_prepbuffsize</A><BR> | ||
| 578 | <A HREF="manual.html#luaL_pushfail">luaL_pushfail</A><BR> | ||
| 579 | <A HREF="manual.html#luaL_pushresult">luaL_pushresult</A><BR> | ||
| 580 | <A HREF="manual.html#luaL_pushresultsize">luaL_pushresultsize</A><BR> | ||
| 581 | <A HREF="manual.html#luaL_ref">luaL_ref</A><BR> | ||
| 582 | <A HREF="manual.html#luaL_requiref">luaL_requiref</A><BR> | ||
| 583 | <A HREF="manual.html#luaL_setfuncs">luaL_setfuncs</A><BR> | ||
| 584 | <A HREF="manual.html#luaL_setmetatable">luaL_setmetatable</A><BR> | ||
| 585 | <A HREF="manual.html#luaL_testudata">luaL_testudata</A><BR> | ||
| 586 | <A HREF="manual.html#luaL_tolstring">luaL_tolstring</A><BR> | ||
| 587 | <A HREF="manual.html#luaL_traceback">luaL_traceback</A><BR> | ||
| 588 | <A HREF="manual.html#luaL_typeerror">luaL_typeerror</A><BR> | ||
| 589 | <A HREF="manual.html#luaL_typename">luaL_typename</A><BR> | ||
| 590 | <A HREF="manual.html#luaL_unref">luaL_unref</A><BR> | ||
| 591 | <A HREF="manual.html#luaL_where">luaL_where</A><BR> | ||
| 592 | |||
| 593 | <H3><A NAME="library">standard library</A></H3> | ||
| 594 | <P> | ||
| 595 | <A HREF="manual.html#pdf-luaopen_base">luaopen_base</A><BR> | ||
| 596 | <A HREF="manual.html#pdf-luaopen_coroutine">luaopen_coroutine</A><BR> | ||
| 597 | <A HREF="manual.html#pdf-luaopen_debug">luaopen_debug</A><BR> | ||
| 598 | <A HREF="manual.html#pdf-luaopen_io">luaopen_io</A><BR> | ||
| 599 | <A HREF="manual.html#pdf-luaopen_math">luaopen_math</A><BR> | ||
| 600 | <A HREF="manual.html#pdf-luaopen_os">luaopen_os</A><BR> | ||
| 601 | <A HREF="manual.html#pdf-luaopen_package">luaopen_package</A><BR> | ||
| 602 | <A HREF="manual.html#pdf-luaopen_string">luaopen_string</A><BR> | ||
| 603 | <A HREF="manual.html#pdf-luaopen_table">luaopen_table</A><BR> | ||
| 604 | <A HREF="manual.html#pdf-luaopen_utf8">luaopen_utf8</A><BR> | ||
| 605 | |||
| 606 | <H3><A NAME="constants">constants</A></H3> | ||
| 607 | <P> | ||
| 608 | <A HREF="manual.html#pdf-LUA_ERRERR">LUA_ERRERR</A><BR> | ||
| 609 | <A HREF="manual.html#pdf-LUA_ERRFILE">LUA_ERRFILE</A><BR> | ||
| 610 | <A HREF="manual.html#pdf-LUA_ERRMEM">LUA_ERRMEM</A><BR> | ||
| 611 | <A HREF="manual.html#pdf-LUA_ERRRUN">LUA_ERRRUN</A><BR> | ||
| 612 | <A HREF="manual.html#pdf-LUA_ERRSYNTAX">LUA_ERRSYNTAX</A><BR> | ||
| 613 | <A HREF="manual.html#pdf-LUA_HOOKCALL">LUA_HOOKCALL</A><BR> | ||
| 614 | <A HREF="manual.html#pdf-LUA_HOOKCOUNT">LUA_HOOKCOUNT</A><BR> | ||
| 615 | <A HREF="manual.html#pdf-LUA_HOOKLINE">LUA_HOOKLINE</A><BR> | ||
| 616 | <A HREF="manual.html#pdf-LUA_HOOKRET">LUA_HOOKRET</A><BR> | ||
| 617 | <A HREF="manual.html#pdf-LUA_HOOKTAILCALL">LUA_HOOKTAILCALL</A><BR> | ||
| 618 | <A HREF="manual.html#pdf-LUA_LOADED_TABLE">LUA_LOADED_TABLE</A><BR> | ||
| 619 | <A HREF="manual.html#pdf-LUA_MASKCALL">LUA_MASKCALL</A><BR> | ||
| 620 | <A HREF="manual.html#pdf-LUA_MASKCOUNT">LUA_MASKCOUNT</A><BR> | ||
| 621 | <A HREF="manual.html#pdf-LUA_MASKLINE">LUA_MASKLINE</A><BR> | ||
| 622 | <A HREF="manual.html#pdf-LUA_MASKRET">LUA_MASKRET</A><BR> | ||
| 623 | <A HREF="manual.html#pdf-LUA_MAXINTEGER">LUA_MAXINTEGER</A><BR> | ||
| 624 | <A HREF="manual.html#pdf-LUA_MININTEGER">LUA_MININTEGER</A><BR> | ||
| 625 | <A HREF="manual.html#pdf-LUA_MINSTACK">LUA_MINSTACK</A><BR> | ||
| 626 | <A HREF="manual.html#pdf-LUA_MULTRET">LUA_MULTRET</A><BR> | ||
| 627 | <A HREF="manual.html#pdf-LUA_NOREF">LUA_NOREF</A><BR> | ||
| 628 | <A HREF="manual.html#pdf-LUA_OK">LUA_OK</A><BR> | ||
| 629 | <A HREF="manual.html#pdf-LUA_OPADD">LUA_OPADD</A><BR> | ||
| 630 | <A HREF="manual.html#pdf-LUA_OPBAND">LUA_OPBAND</A><BR> | ||
| 631 | <A HREF="manual.html#pdf-LUA_OPBNOT">LUA_OPBNOT</A><BR> | ||
| 632 | <A HREF="manual.html#pdf-LUA_OPBOR">LUA_OPBOR</A><BR> | ||
| 633 | <A HREF="manual.html#pdf-LUA_OPBXOR">LUA_OPBXOR</A><BR> | ||
| 634 | <A HREF="manual.html#pdf-LUA_OPDIV">LUA_OPDIV</A><BR> | ||
| 635 | <A HREF="manual.html#pdf-LUA_OPEQ">LUA_OPEQ</A><BR> | ||
| 636 | <A HREF="manual.html#pdf-LUA_OPIDIV">LUA_OPIDIV</A><BR> | ||
| 637 | <A HREF="manual.html#pdf-LUA_OPLE">LUA_OPLE</A><BR> | ||
| 638 | <A HREF="manual.html#pdf-LUA_OPLT">LUA_OPLT</A><BR> | ||
| 639 | <A HREF="manual.html#pdf-LUA_OPMOD">LUA_OPMOD</A><BR> | ||
| 640 | <A HREF="manual.html#pdf-LUA_OPMUL">LUA_OPMUL</A><BR> | ||
| 641 | <A HREF="manual.html#pdf-LUA_OPPOW">LUA_OPPOW</A><BR> | ||
| 642 | <A HREF="manual.html#pdf-LUA_OPSHL">LUA_OPSHL</A><BR> | ||
| 643 | <A HREF="manual.html#pdf-LUA_OPSHR">LUA_OPSHR</A><BR> | ||
| 644 | <A HREF="manual.html#pdf-LUA_OPSUB">LUA_OPSUB</A><BR> | ||
| 645 | <A HREF="manual.html#pdf-LUA_OPUNM">LUA_OPUNM</A><BR> | ||
| 646 | <A HREF="manual.html#pdf-LUA_PRELOAD_TABLE">LUA_PRELOAD_TABLE</A><BR> | ||
| 647 | <A HREF="manual.html#pdf-LUA_REFNIL">LUA_REFNIL</A><BR> | ||
| 648 | <A HREF="manual.html#pdf-LUA_REGISTRYINDEX">LUA_REGISTRYINDEX</A><BR> | ||
| 649 | <A HREF="manual.html#pdf-LUA_RIDX_GLOBALS">LUA_RIDX_GLOBALS</A><BR> | ||
| 650 | <A HREF="manual.html#pdf-LUA_RIDX_MAINTHREAD">LUA_RIDX_MAINTHREAD</A><BR> | ||
| 651 | <A HREF="manual.html#pdf-LUA_TBOOLEAN">LUA_TBOOLEAN</A><BR> | ||
| 652 | <A HREF="manual.html#pdf-LUA_TFUNCTION">LUA_TFUNCTION</A><BR> | ||
| 653 | <A HREF="manual.html#pdf-LUA_TLIGHTUSERDATA">LUA_TLIGHTUSERDATA</A><BR> | ||
| 654 | <A HREF="manual.html#pdf-LUA_TNIL">LUA_TNIL</A><BR> | ||
| 655 | <A HREF="manual.html#pdf-LUA_TNONE">LUA_TNONE</A><BR> | ||
| 656 | <A HREF="manual.html#pdf-LUA_TNUMBER">LUA_TNUMBER</A><BR> | ||
| 657 | <A HREF="manual.html#pdf-LUA_TSTRING">LUA_TSTRING</A><BR> | ||
| 658 | <A HREF="manual.html#pdf-LUA_TTABLE">LUA_TTABLE</A><BR> | ||
| 659 | <A HREF="manual.html#pdf-LUA_TTHREAD">LUA_TTHREAD</A><BR> | ||
| 660 | <A HREF="manual.html#pdf-LUA_TUSERDATA">LUA_TUSERDATA</A><BR> | ||
| 661 | <A HREF="manual.html#pdf-LUA_USE_APICHECK">LUA_USE_APICHECK</A><BR> | ||
| 662 | <A HREF="manual.html#pdf-LUA_YIELD">LUA_YIELD</A><BR> | ||
| 663 | <A HREF="manual.html#pdf-LUAL_BUFFERSIZE">LUAL_BUFFERSIZE</A><BR> | ||
| 664 | |||
| 665 | </TD> | ||
| 666 | </TR> | ||
| 667 | </TABLE> | ||
| 668 | |||
| 669 | <P CLASS="footer"> | ||
| 670 | Last update: | ||
| 671 | Wed May 21 21:11:33 UTC 2025 | ||
| 672 | </P> | ||
| 673 | <!-- | ||
| 674 | Last change: revised for Lua 5.4.8 | ||
| 675 | --> | ||
| 676 | |||
| 677 | </BODY> | ||
| 678 | </HTML> | ||
diff --git a/vendor/lua-5.4.8/doc/index.css b/vendor/lua-5.4.8/doc/index.css new file mode 100644 index 0000000..c961835 --- /dev/null +++ b/vendor/lua-5.4.8/doc/index.css | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | ul { | ||
| 2 | list-style-type: none ; | ||
| 3 | } | ||
| 4 | |||
| 5 | ul.contents { | ||
| 6 | padding: 0 ; | ||
| 7 | } | ||
| 8 | |||
| 9 | table { | ||
| 10 | border: none ; | ||
| 11 | border-spacing: 0 ; | ||
| 12 | border-collapse: collapse ; | ||
| 13 | } | ||
| 14 | |||
| 15 | td { | ||
| 16 | vertical-align: top ; | ||
| 17 | padding: 0 ; | ||
| 18 | text-align: left ; | ||
| 19 | line-height: 1.25 ; | ||
| 20 | width: 15% ; | ||
| 21 | } | ||
diff --git a/vendor/lua-5.4.8/doc/logo.gif b/vendor/lua-5.4.8/doc/logo.gif new file mode 100644 index 0000000..5c77eac --- /dev/null +++ b/vendor/lua-5.4.8/doc/logo.gif | |||
| Binary files differ | |||
diff --git a/vendor/lua-5.4.8/doc/lua.1 b/vendor/lua-5.4.8/doc/lua.1 new file mode 100644 index 0000000..3c9e000 --- /dev/null +++ b/vendor/lua-5.4.8/doc/lua.1 | |||
| @@ -0,0 +1,155 @@ | |||
| 1 | .\" $Id: lua.man,v 1.14 2024/05/08 18:48:27 lhf Exp $ | ||
| 2 | .TH LUA 1 "$Date: 2024/05/08 18:48:27 $" | ||
| 3 | .SH NAME | ||
| 4 | lua \- Lua interpreter | ||
| 5 | .SH SYNOPSIS | ||
| 6 | .B lua | ||
| 7 | [ | ||
| 8 | .I options | ||
| 9 | ] | ||
| 10 | [ | ||
| 11 | .I script | ||
| 12 | [ | ||
| 13 | .I args | ||
| 14 | ] | ||
| 15 | ] | ||
| 16 | .SH DESCRIPTION | ||
| 17 | .B lua | ||
| 18 | is the standalone Lua interpreter. | ||
| 19 | It loads and executes Lua programs, | ||
| 20 | either in textual source form or | ||
| 21 | in precompiled binary form. | ||
| 22 | (Precompiled binaries are output by | ||
| 23 | .BR luac , | ||
| 24 | the Lua compiler.) | ||
| 25 | .B lua | ||
| 26 | can be used as a batch interpreter and also interactively. | ||
| 27 | .LP | ||
| 28 | After handling the | ||
| 29 | .IR options , | ||
| 30 | the Lua program in file | ||
| 31 | .I script | ||
| 32 | is loaded and executed. | ||
| 33 | The | ||
| 34 | .I args | ||
| 35 | are available to | ||
| 36 | .I script | ||
| 37 | as strings in a global table named | ||
| 38 | .B arg | ||
| 39 | and also as arguments to its main function. | ||
| 40 | When called without arguments, | ||
| 41 | .B lua | ||
| 42 | behaves as | ||
| 43 | .B "lua \-v \-i" | ||
| 44 | if the standard input is a terminal, | ||
| 45 | and as | ||
| 46 | .B "lua \-" | ||
| 47 | otherwise. | ||
| 48 | .LP | ||
| 49 | In interactive mode, | ||
| 50 | .B lua | ||
| 51 | prompts the user, | ||
| 52 | reads lines from the standard input, | ||
| 53 | and executes them as they are read. | ||
| 54 | If the line contains an expression, | ||
| 55 | then the line is evaluated and the result is printed. | ||
| 56 | If a line does not contain a complete statement, | ||
| 57 | then a secondary prompt is displayed and | ||
| 58 | lines are read until a complete statement is formed or | ||
| 59 | a syntax error is found. | ||
| 60 | .LP | ||
| 61 | Before handling command line options and scripts, | ||
| 62 | .B lua | ||
| 63 | checks the contents of the environment variables | ||
| 64 | .B LUA_INIT_5_4 | ||
| 65 | and | ||
| 66 | .BR LUA_INIT , | ||
| 67 | in that order. | ||
| 68 | If the contents are of the form | ||
| 69 | .RI '@ filename ', | ||
| 70 | then | ||
| 71 | .I filename | ||
| 72 | is executed. | ||
| 73 | Otherwise, the contents are assumed to be a Lua statement and is executed. | ||
| 74 | When | ||
| 75 | .B LUA_INIT_5_4 | ||
| 76 | is defined, | ||
| 77 | .B LUA_INIT | ||
| 78 | is ignored. | ||
| 79 | .SH OPTIONS | ||
| 80 | .TP | ||
| 81 | .BI \-e " stat" | ||
| 82 | execute statement | ||
| 83 | .IR stat . | ||
| 84 | .TP | ||
| 85 | .B \-i | ||
| 86 | enter interactive mode after executing | ||
| 87 | .IR script . | ||
| 88 | .TP | ||
| 89 | .BI \-l " mod" | ||
| 90 | require library | ||
| 91 | .I mod | ||
| 92 | into global | ||
| 93 | .IR mod . | ||
| 94 | .TP | ||
| 95 | .BI \-l " g=mod" | ||
| 96 | require library | ||
| 97 | .I mod | ||
| 98 | into global | ||
| 99 | .IR g . | ||
| 100 | .TP | ||
| 101 | .B \-v | ||
| 102 | show version information. | ||
| 103 | .TP | ||
| 104 | .B \-E | ||
| 105 | ignore environment variables. | ||
| 106 | .TP | ||
| 107 | .B \-W | ||
| 108 | turn warnings on. | ||
| 109 | .TP | ||
| 110 | .B \-\- | ||
| 111 | stop handling options. | ||
| 112 | .TP | ||
| 113 | .B \- | ||
| 114 | stop handling options and execute the standard input as a file. | ||
| 115 | .SH ENVIRONMENT VARIABLES | ||
| 116 | The following environment variables affect the execution of | ||
| 117 | .BR lua . | ||
| 118 | When defined, | ||
| 119 | the version-specific variants take priority | ||
| 120 | and the version-neutral variants are ignored. | ||
| 121 | .TP | ||
| 122 | .B LUA_INIT, LUA_INIT_5_4 | ||
| 123 | Code to be executed before command line options and scripts. | ||
| 124 | .TP | ||
| 125 | .B LUA_PATH, LUA_PATH_5_4 | ||
| 126 | Initial value of package.path, | ||
| 127 | the path used by require to search for Lua loaders. | ||
| 128 | .TP | ||
| 129 | .B LUA_CPATH, LUA_CPATH_5_4 | ||
| 130 | Initial value of package.cpath, | ||
| 131 | the path used by require to search for C loaders. | ||
| 132 | .SH EXIT STATUS | ||
| 133 | If a script calls os.exit, | ||
| 134 | then | ||
| 135 | .B lua | ||
| 136 | exits with the given exit status. | ||
| 137 | Otherwise, | ||
| 138 | .B lua | ||
| 139 | exits | ||
| 140 | with EXIT_SUCCESS (0 on POSIX systems) if there were no errors | ||
| 141 | and | ||
| 142 | with EXIT_FAILURE (1 on POSIX systems) if there were errors. | ||
| 143 | Errors raised in interactive mode do not cause exits. | ||
| 144 | .SH DIAGNOSTICS | ||
| 145 | Error messages should be self explanatory. | ||
| 146 | .SH "SEE ALSO" | ||
| 147 | .BR luac (1) | ||
| 148 | .br | ||
| 149 | The documentation at lua.org, | ||
| 150 | especially section 7 of the reference manual. | ||
| 151 | .SH AUTHORS | ||
| 152 | R. Ierusalimschy, | ||
| 153 | L. H. de Figueiredo, | ||
| 154 | W. Celes | ||
| 155 | .\" EOF | ||
diff --git a/vendor/lua-5.4.8/doc/lua.css b/vendor/lua-5.4.8/doc/lua.css new file mode 100644 index 0000000..9013b44 --- /dev/null +++ b/vendor/lua-5.4.8/doc/lua.css | |||
| @@ -0,0 +1,162 @@ | |||
| 1 | html { | ||
| 2 | background-color: #F8F8F8 ; | ||
| 3 | } | ||
| 4 | |||
| 5 | body { | ||
| 6 | background-color: #FFFFFF ; | ||
| 7 | color: #000000 ; | ||
| 8 | font-family: Helvetica, Arial, sans-serif ; | ||
| 9 | text-align: justify ; | ||
| 10 | line-height: 1.25 ; | ||
| 11 | margin: 16px auto ; | ||
| 12 | padding: 32px ; | ||
| 13 | border: solid #ccc 1px ; | ||
| 14 | border-radius: 20px ; | ||
| 15 | max-width: 70em ; | ||
| 16 | width: 90% ; | ||
| 17 | } | ||
| 18 | |||
| 19 | h1, h2, h3, h4 { | ||
| 20 | color: #000080 ; | ||
| 21 | font-family: Verdana, Geneva, sans-serif ; | ||
| 22 | font-weight: normal ; | ||
| 23 | font-style: normal ; | ||
| 24 | text-align: left ; | ||
| 25 | } | ||
| 26 | |||
| 27 | h1 { | ||
| 28 | font-size: 28pt ; | ||
| 29 | } | ||
| 30 | |||
| 31 | h1 img { | ||
| 32 | vertical-align: text-bottom ; | ||
| 33 | } | ||
| 34 | |||
| 35 | h2:before { | ||
| 36 | content: "\2756" ; | ||
| 37 | padding-right: 0.5em ; | ||
| 38 | } | ||
| 39 | |||
| 40 | a { | ||
| 41 | text-decoration: none ; | ||
| 42 | } | ||
| 43 | |||
| 44 | a:link { | ||
| 45 | color: #000080 ; | ||
| 46 | } | ||
| 47 | |||
| 48 | a:link:hover, a:visited:hover { | ||
| 49 | background-color: #D0D0FF ; | ||
| 50 | color: #000080 ; | ||
| 51 | border-radius: 4px ; | ||
| 52 | } | ||
| 53 | |||
| 54 | a:link:active, a:visited:active { | ||
| 55 | color: #FF0000 ; | ||
| 56 | } | ||
| 57 | |||
| 58 | div.menubar { | ||
| 59 | padding-bottom: 0.5em ; | ||
| 60 | } | ||
| 61 | |||
| 62 | p.menubar { | ||
| 63 | margin-left: 2.5em ; | ||
| 64 | } | ||
| 65 | |||
| 66 | .menubar a:hover { | ||
| 67 | margin: -3px -3px -3px -3px ; | ||
| 68 | padding: 3px 3px 3px 3px ; | ||
| 69 | border-radius: 4px ; | ||
| 70 | } | ||
| 71 | |||
| 72 | :target { | ||
| 73 | background-color: #F0F0F0 ; | ||
| 74 | margin: -8px ; | ||
| 75 | padding: 8px ; | ||
| 76 | border-radius: 8px ; | ||
| 77 | outline: none ; | ||
| 78 | } | ||
| 79 | |||
| 80 | hr { | ||
| 81 | display: none ; | ||
| 82 | } | ||
| 83 | |||
| 84 | table hr { | ||
| 85 | background-color: #a0a0a0 ; | ||
| 86 | color: #a0a0a0 ; | ||
| 87 | border: 0 ; | ||
| 88 | height: 1px ; | ||
| 89 | display: block ; | ||
| 90 | } | ||
| 91 | |||
| 92 | .footer { | ||
| 93 | color: gray ; | ||
| 94 | font-size: x-small ; | ||
| 95 | text-transform: lowercase ; | ||
| 96 | } | ||
| 97 | |||
| 98 | input[type=text] { | ||
| 99 | border: solid #a0a0a0 2px ; | ||
| 100 | border-radius: 2em ; | ||
| 101 | background-image: url('images/search.png') ; | ||
| 102 | background-repeat: no-repeat ; | ||
| 103 | background-position: 4px center ; | ||
| 104 | padding-left: 20px ; | ||
| 105 | height: 2em ; | ||
| 106 | } | ||
| 107 | |||
| 108 | pre.session { | ||
| 109 | background-color: #F8F8F8 ; | ||
| 110 | padding: 1em ; | ||
| 111 | border-radius: 8px ; | ||
| 112 | } | ||
| 113 | |||
| 114 | table { | ||
| 115 | border: none ; | ||
| 116 | border-spacing: 0 ; | ||
| 117 | border-collapse: collapse ; | ||
| 118 | } | ||
| 119 | |||
| 120 | td { | ||
| 121 | padding: 0 ; | ||
| 122 | margin: 0 ; | ||
| 123 | } | ||
| 124 | |||
| 125 | td.gutter { | ||
| 126 | width: 4% ; | ||
| 127 | } | ||
| 128 | |||
| 129 | table.columns td { | ||
| 130 | vertical-align: top ; | ||
| 131 | padding-bottom: 1em ; | ||
| 132 | text-align: justify ; | ||
| 133 | line-height: 1.25 ; | ||
| 134 | } | ||
| 135 | |||
| 136 | table.book td { | ||
| 137 | vertical-align: top ; | ||
| 138 | } | ||
| 139 | |||
| 140 | table.book td.cover { | ||
| 141 | padding-right: 1em ; | ||
| 142 | } | ||
| 143 | |||
| 144 | table.book img { | ||
| 145 | border: solid #000080 1px ; | ||
| 146 | border-radius: 2px ; | ||
| 147 | } | ||
| 148 | |||
| 149 | table.book span { | ||
| 150 | font-size: small ; | ||
| 151 | text-align: left ; | ||
| 152 | display: block ; | ||
| 153 | margin-top: 0.25em ; | ||
| 154 | } | ||
| 155 | |||
| 156 | p.logos a:link:hover, p.logos a:visited:hover { | ||
| 157 | background-color: inherit ; | ||
| 158 | } | ||
| 159 | |||
| 160 | img { | ||
| 161 | background-color: white ; | ||
| 162 | } | ||
diff --git a/vendor/lua-5.4.8/doc/luac.1 b/vendor/lua-5.4.8/doc/luac.1 new file mode 100644 index 0000000..33a4ed0 --- /dev/null +++ b/vendor/lua-5.4.8/doc/luac.1 | |||
| @@ -0,0 +1,118 @@ | |||
| 1 | .\" $Id: luac.man,v 1.29 2011/11/16 13:53:40 lhf Exp $ | ||
| 2 | .TH LUAC 1 "$Date: 2011/11/16 13:53:40 $" | ||
| 3 | .SH NAME | ||
| 4 | luac \- Lua compiler | ||
| 5 | .SH SYNOPSIS | ||
| 6 | .B luac | ||
| 7 | [ | ||
| 8 | .I options | ||
| 9 | ] [ | ||
| 10 | .I filenames | ||
| 11 | ] | ||
| 12 | .SH DESCRIPTION | ||
| 13 | .B luac | ||
| 14 | is the Lua compiler. | ||
| 15 | It translates programs written in the Lua programming language | ||
| 16 | into binary files containing precompiled chunks | ||
| 17 | that can be later loaded and executed. | ||
| 18 | .LP | ||
| 19 | The main advantages of precompiling chunks are: | ||
| 20 | faster loading, | ||
| 21 | protecting source code from accidental user changes, | ||
| 22 | and | ||
| 23 | off-line syntax checking. | ||
| 24 | Precompiling does not imply faster execution | ||
| 25 | because in Lua chunks are always compiled into bytecodes before being executed. | ||
| 26 | .B luac | ||
| 27 | simply allows those bytecodes to be saved in a file for later execution. | ||
| 28 | Precompiled chunks are not necessarily smaller than the corresponding source. | ||
| 29 | The main goal in precompiling is faster loading. | ||
| 30 | .LP | ||
| 31 | In the command line, | ||
| 32 | you can mix | ||
| 33 | text files containing Lua source and | ||
| 34 | binary files containing precompiled chunks. | ||
| 35 | .B luac | ||
| 36 | produces a single output file containing the combined bytecodes | ||
| 37 | for all files given. | ||
| 38 | Executing the combined file is equivalent to executing the given files. | ||
| 39 | By default, | ||
| 40 | the output file is named | ||
| 41 | .BR luac.out , | ||
| 42 | but you can change this with the | ||
| 43 | .B \-o | ||
| 44 | option. | ||
| 45 | .LP | ||
| 46 | Precompiled chunks are | ||
| 47 | .I not | ||
| 48 | portable across different architectures. | ||
| 49 | Moreover, | ||
| 50 | the internal format of precompiled chunks | ||
| 51 | is likely to change when a new version of Lua is released. | ||
| 52 | Make sure you save the source files of all Lua programs that you precompile. | ||
| 53 | .LP | ||
| 54 | .SH OPTIONS | ||
| 55 | .TP | ||
| 56 | .B \-l | ||
| 57 | produce a listing of the compiled bytecode for Lua's virtual machine. | ||
| 58 | Listing bytecodes is useful to learn about Lua's virtual machine. | ||
| 59 | If no files are given, then | ||
| 60 | .B luac | ||
| 61 | loads | ||
| 62 | .B luac.out | ||
| 63 | and lists its contents. | ||
| 64 | Use | ||
| 65 | .B \-l \-l | ||
| 66 | for a full listing. | ||
| 67 | .TP | ||
| 68 | .BI \-o " file" | ||
| 69 | output to | ||
| 70 | .IR file , | ||
| 71 | instead of the default | ||
| 72 | .BR luac.out . | ||
| 73 | (You can use | ||
| 74 | .B "'\-'" | ||
| 75 | for standard output, | ||
| 76 | but not on platforms that open standard output in text mode.) | ||
| 77 | The output file may be one of the given files because | ||
| 78 | all files are loaded before the output file is written. | ||
| 79 | Be careful not to overwrite precious files. | ||
| 80 | .TP | ||
| 81 | .B \-p | ||
| 82 | load files but do not generate any output file. | ||
| 83 | Used mainly for syntax checking and for testing precompiled chunks: | ||
| 84 | corrupted files will probably generate errors when loaded. | ||
| 85 | If no files are given, then | ||
| 86 | .B luac | ||
| 87 | loads | ||
| 88 | .B luac.out | ||
| 89 | and tests its contents. | ||
| 90 | No messages are displayed if the file loads without errors. | ||
| 91 | .TP | ||
| 92 | .B \-s | ||
| 93 | strip debug information before writing the output file. | ||
| 94 | This saves some space in very large chunks, | ||
| 95 | but if errors occur when running a stripped chunk, | ||
| 96 | then the error messages may not contain the full information they usually do. | ||
| 97 | In particular, | ||
| 98 | line numbers and names of local variables are lost. | ||
| 99 | .TP | ||
| 100 | .B \-v | ||
| 101 | show version information. | ||
| 102 | .TP | ||
| 103 | .B \-\- | ||
| 104 | stop handling options. | ||
| 105 | .TP | ||
| 106 | .B \- | ||
| 107 | stop handling options and process standard input. | ||
| 108 | .SH "SEE ALSO" | ||
| 109 | .BR lua (1) | ||
| 110 | .br | ||
| 111 | The documentation at lua.org. | ||
| 112 | .SH DIAGNOSTICS | ||
| 113 | Error messages should be self explanatory. | ||
| 114 | .SH AUTHORS | ||
| 115 | R. Ierusalimschy, | ||
| 116 | L. H. de Figueiredo, | ||
| 117 | W. Celes | ||
| 118 | .\" EOF | ||
diff --git a/vendor/lua-5.4.8/doc/manual.css b/vendor/lua-5.4.8/doc/manual.css new file mode 100644 index 0000000..aa0e677 --- /dev/null +++ b/vendor/lua-5.4.8/doc/manual.css | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | h3 code { | ||
| 2 | font-family: inherit ; | ||
| 3 | font-size: inherit ; | ||
| 4 | } | ||
| 5 | |||
| 6 | pre, code { | ||
| 7 | font-size: 12pt ; | ||
| 8 | } | ||
| 9 | |||
| 10 | span.apii { | ||
| 11 | color: gray ; | ||
| 12 | float: right ; | ||
| 13 | font-family: inherit ; | ||
| 14 | font-style: normal ; | ||
| 15 | font-size: small ; | ||
| 16 | } | ||
| 17 | |||
| 18 | h2:before { | ||
| 19 | content: "" ; | ||
| 20 | padding-right: 0em ; | ||
| 21 | } | ||
diff --git a/vendor/lua-5.4.8/doc/manual.html b/vendor/lua-5.4.8/doc/manual.html new file mode 100644 index 0000000..8239bc2 --- /dev/null +++ b/vendor/lua-5.4.8/doc/manual.html | |||
| @@ -0,0 +1,12060 @@ | |||
| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> | ||
| 2 | <HTML> | ||
| 3 | <HEAD> | ||
| 4 | <TITLE>Lua 5.4 Reference Manual</TITLE> | ||
| 5 | <LINK REL="stylesheet" TYPE="text/css" HREF="lua.css"> | ||
| 6 | <LINK REL="stylesheet" TYPE="text/css" HREF="manual.css"> | ||
| 7 | <META HTTP-EQUIV="content-type" CONTENT="text/html; charset=iso-8859-1"> | ||
| 8 | </HEAD> | ||
| 9 | |||
| 10 | <BODY> | ||
| 11 | |||
| 12 | <H1> | ||
| 13 | <A HREF="https://www.lua.org/"><IMG SRC="logo.gif" ALT="Lua"></A> | ||
| 14 | Lua 5.4 Reference Manual | ||
| 15 | </H1> | ||
| 16 | |||
| 17 | <P> | ||
| 18 | by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes | ||
| 19 | |||
| 20 | <P> | ||
| 21 | <SMALL> | ||
| 22 | Copyright © 2020–2025 Lua.org, PUC-Rio. | ||
| 23 | Freely available under the terms of the | ||
| 24 | <a href="https://www.lua.org/license.html">Lua license</a>. | ||
| 25 | </SMALL> | ||
| 26 | |||
| 27 | <DIV CLASS="menubar"> | ||
| 28 | <A HREF="contents.html#contents">contents</A> | ||
| 29 | · | ||
| 30 | <A HREF="contents.html#index">index</A> | ||
| 31 | · | ||
| 32 | <A HREF="https://www.lua.org/manual/">other versions</A> | ||
| 33 | </DIV> | ||
| 34 | |||
| 35 | <!-- ====================================================================== --> | ||
| 36 | <p> | ||
| 37 | |||
| 38 | <!-- $Id: manual.of $ --> | ||
| 39 | |||
| 40 | |||
| 41 | |||
| 42 | |||
| 43 | <h1>1 – <a name="1">Introduction</a></h1> | ||
| 44 | |||
| 45 | <p> | ||
| 46 | Lua is a powerful, efficient, lightweight, embeddable scripting language. | ||
| 47 | It supports procedural programming, | ||
| 48 | object-oriented programming, functional programming, | ||
| 49 | data-driven programming, and data description. | ||
| 50 | |||
| 51 | |||
| 52 | <p> | ||
| 53 | Lua combines simple procedural syntax with powerful data description | ||
| 54 | constructs based on associative arrays and extensible semantics. | ||
| 55 | Lua is dynamically typed, | ||
| 56 | runs by interpreting bytecode with a register-based | ||
| 57 | virtual machine, | ||
| 58 | and has automatic memory management with | ||
| 59 | a generational garbage collection, | ||
| 60 | making it ideal for configuration, scripting, | ||
| 61 | and rapid prototyping. | ||
| 62 | |||
| 63 | |||
| 64 | <p> | ||
| 65 | Lua is implemented as a library, written in <em>clean C</em>, | ||
| 66 | the common subset of standard C and C++. | ||
| 67 | The Lua distribution includes a host program called <code>lua</code>, | ||
| 68 | which uses the Lua library to offer a complete, | ||
| 69 | standalone Lua interpreter, | ||
| 70 | for interactive or batch use. | ||
| 71 | Lua is intended to be used both as a powerful, lightweight, | ||
| 72 | embeddable scripting language for any program that needs one, | ||
| 73 | and as a powerful but lightweight and efficient stand-alone language. | ||
| 74 | |||
| 75 | |||
| 76 | <p> | ||
| 77 | As an extension language, Lua has no notion of a "main" program: | ||
| 78 | it works <em>embedded</em> in a host client, | ||
| 79 | called the <em>embedding program</em> or simply the <em>host</em>. | ||
| 80 | (Frequently, this host is the stand-alone <code>lua</code> program.) | ||
| 81 | The host program can invoke functions to execute a piece of Lua code, | ||
| 82 | can write and read Lua variables, | ||
| 83 | and can register C functions to be called by Lua code. | ||
| 84 | Through the use of C functions, Lua can be augmented to cope with | ||
| 85 | a wide range of different domains, | ||
| 86 | thus creating customized programming languages sharing a syntactical framework. | ||
| 87 | |||
| 88 | |||
| 89 | <p> | ||
| 90 | Lua is free software, | ||
| 91 | and is provided as usual with no guarantees, | ||
| 92 | as stated in its license. | ||
| 93 | The implementation described in this manual is available | ||
| 94 | at Lua's official web site, <code>www.lua.org</code>. | ||
| 95 | |||
| 96 | |||
| 97 | <p> | ||
| 98 | Like any other reference manual, | ||
| 99 | this document is dry in places. | ||
| 100 | For a discussion of the decisions behind the design of Lua, | ||
| 101 | see the technical papers available at Lua's web site. | ||
| 102 | For a detailed introduction to programming in Lua, | ||
| 103 | see Roberto's book, <em>Programming in Lua</em>. | ||
| 104 | |||
| 105 | |||
| 106 | |||
| 107 | <h1>2 – <a name="2">Basic Concepts</a></h1> | ||
| 108 | |||
| 109 | |||
| 110 | |||
| 111 | <p> | ||
| 112 | This section describes the basic concepts of the language. | ||
| 113 | |||
| 114 | |||
| 115 | |||
| 116 | |||
| 117 | |||
| 118 | <h2>2.1 – <a name="2.1">Values and Types</a></h2> | ||
| 119 | |||
| 120 | <p> | ||
| 121 | Lua is a dynamically typed language. | ||
| 122 | This means that | ||
| 123 | variables do not have types; only values do. | ||
| 124 | There are no type definitions in the language. | ||
| 125 | All values carry their own type. | ||
| 126 | |||
| 127 | |||
| 128 | <p> | ||
| 129 | All values in Lua are first-class values. | ||
| 130 | This means that all values can be stored in variables, | ||
| 131 | passed as arguments to other functions, and returned as results. | ||
| 132 | |||
| 133 | |||
| 134 | <p> | ||
| 135 | There are eight basic types in Lua: | ||
| 136 | <em>nil</em>, <em>boolean</em>, <em>number</em>, | ||
| 137 | <em>string</em>, <em>function</em>, <em>userdata</em>, | ||
| 138 | <em>thread</em>, and <em>table</em>. | ||
| 139 | The type <em>nil</em> has one single value, <b>nil</b>, | ||
| 140 | whose main property is to be different from any other value; | ||
| 141 | it often represents the absence of a useful value. | ||
| 142 | The type <em>boolean</em> has two values, <b>false</b> and <b>true</b>. | ||
| 143 | Both <b>nil</b> and <b>false</b> make a condition false; | ||
| 144 | they are collectively called <em>false values</em>. | ||
| 145 | Any other value makes a condition true. | ||
| 146 | Despite its name, | ||
| 147 | <b>false</b> is frequently used as an alternative to <b>nil</b>, | ||
| 148 | with the key difference that <b>false</b> behaves | ||
| 149 | like a regular value in a table, | ||
| 150 | while a <b>nil</b> in a table represents an absent key. | ||
| 151 | |||
| 152 | |||
| 153 | <p> | ||
| 154 | The type <em>number</em> represents both | ||
| 155 | integer numbers and real (floating-point) numbers, | ||
| 156 | using two subtypes: <em>integer</em> and <em>float</em>. | ||
| 157 | Standard Lua uses 64-bit integers and double-precision (64-bit) floats, | ||
| 158 | but you can also compile Lua so that it | ||
| 159 | uses 32-bit integers and/or single-precision (32-bit) floats. | ||
| 160 | The option with 32 bits for both integers and floats | ||
| 161 | is particularly attractive | ||
| 162 | for small machines and embedded systems. | ||
| 163 | (See macro <code>LUA_32BITS</code> in file <code>luaconf.h</code>.) | ||
| 164 | |||
| 165 | |||
| 166 | <p> | ||
| 167 | Unless stated otherwise, | ||
| 168 | any overflow when manipulating integer values <em>wrap around</em>, | ||
| 169 | according to the usual rules of two-complement arithmetic. | ||
| 170 | (In other words, | ||
| 171 | the actual result is the unique representable integer | ||
| 172 | that is equal modulo <em>2<sup>n</sup></em> to the mathematical result, | ||
| 173 | where <em>n</em> is the number of bits of the integer type.) | ||
| 174 | |||
| 175 | |||
| 176 | <p> | ||
| 177 | Lua has explicit rules about when each subtype is used, | ||
| 178 | but it also converts between them automatically as needed (see <a href="#3.4.3">§3.4.3</a>). | ||
| 179 | Therefore, | ||
| 180 | the programmer may choose to mostly ignore the difference | ||
| 181 | between integers and floats | ||
| 182 | or to assume complete control over the representation of each number. | ||
| 183 | |||
| 184 | |||
| 185 | <p> | ||
| 186 | The type <em>string</em> represents immutable sequences of bytes. | ||
| 187 | |||
| 188 | Lua is 8-bit clean: | ||
| 189 | strings can contain any 8-bit value, | ||
| 190 | including embedded zeros ('<code>\0</code>'). | ||
| 191 | Lua is also encoding-agnostic; | ||
| 192 | it makes no assumptions about the contents of a string. | ||
| 193 | The length of any string in Lua must fit in a Lua integer. | ||
| 194 | |||
| 195 | |||
| 196 | <p> | ||
| 197 | Lua can call (and manipulate) functions written in Lua and | ||
| 198 | functions written in C (see <a href="#3.4.10">§3.4.10</a>). | ||
| 199 | Both are represented by the type <em>function</em>. | ||
| 200 | |||
| 201 | |||
| 202 | <p> | ||
| 203 | The type <em>userdata</em> is provided to allow arbitrary C data to | ||
| 204 | be stored in Lua variables. | ||
| 205 | A userdata value represents a block of raw memory. | ||
| 206 | There are two kinds of userdata: | ||
| 207 | <em>full userdata</em>, | ||
| 208 | which is an object with a block of memory managed by Lua, | ||
| 209 | and <em>light userdata</em>, | ||
| 210 | which is simply a C pointer value. | ||
| 211 | Userdata has no predefined operations in Lua, | ||
| 212 | except assignment and identity test. | ||
| 213 | By using <em>metatables</em>, | ||
| 214 | the programmer can define operations for full userdata values | ||
| 215 | (see <a href="#2.4">§2.4</a>). | ||
| 216 | Userdata values cannot be created or modified in Lua, | ||
| 217 | only through the C API. | ||
| 218 | This guarantees the integrity of data owned by | ||
| 219 | the host program and C libraries. | ||
| 220 | |||
| 221 | |||
| 222 | <p> | ||
| 223 | The type <em>thread</em> represents independent threads of execution | ||
| 224 | and it is used to implement coroutines (see <a href="#2.6">§2.6</a>). | ||
| 225 | Lua threads are not related to operating-system threads. | ||
| 226 | Lua supports coroutines on all systems, | ||
| 227 | even those that do not support threads natively. | ||
| 228 | |||
| 229 | |||
| 230 | <p> | ||
| 231 | The type <em>table</em> implements associative arrays, | ||
| 232 | that is, arrays that can have as indices not only numbers, | ||
| 233 | but any Lua value except <b>nil</b> and NaN. | ||
| 234 | (<em>Not a Number</em> is a special floating-point value | ||
| 235 | used by the IEEE 754 standard to represent | ||
| 236 | undefined numerical results, such as <code>0/0</code>.) | ||
| 237 | Tables can be <em>heterogeneous</em>; | ||
| 238 | that is, they can contain values of all types (except <b>nil</b>). | ||
| 239 | Any key associated to the value <b>nil</b> is not considered part of the table. | ||
| 240 | Conversely, any key that is not part of a table has | ||
| 241 | an associated value <b>nil</b>. | ||
| 242 | |||
| 243 | |||
| 244 | <p> | ||
| 245 | Tables are the sole data-structuring mechanism in Lua; | ||
| 246 | they can be used to represent ordinary arrays, lists, | ||
| 247 | symbol tables, sets, records, graphs, trees, etc. | ||
| 248 | To represent records, Lua uses the field name as an index. | ||
| 249 | The language supports this representation by | ||
| 250 | providing <code>a.name</code> as syntactic sugar for <code>a["name"]</code>. | ||
| 251 | There are several convenient ways to create tables in Lua | ||
| 252 | (see <a href="#3.4.9">§3.4.9</a>). | ||
| 253 | |||
| 254 | |||
| 255 | <p> | ||
| 256 | Like indices, | ||
| 257 | the values of table fields can be of any type. | ||
| 258 | In particular, | ||
| 259 | because functions are first-class values, | ||
| 260 | table fields can contain functions. | ||
| 261 | Thus tables can also carry <em>methods</em> (see <a href="#3.4.11">§3.4.11</a>). | ||
| 262 | |||
| 263 | |||
| 264 | <p> | ||
| 265 | The indexing of tables follows | ||
| 266 | the definition of raw equality in the language. | ||
| 267 | The expressions <code>a[i]</code> and <code>a[j]</code> | ||
| 268 | denote the same table element | ||
| 269 | if and only if <code>i</code> and <code>j</code> are raw equal | ||
| 270 | (that is, equal without metamethods). | ||
| 271 | In particular, floats with integral values | ||
| 272 | are equal to their respective integers | ||
| 273 | (e.g., <code>1.0 == 1</code>). | ||
| 274 | To avoid ambiguities, | ||
| 275 | any float used as a key that is equal to an integer | ||
| 276 | is converted to that integer. | ||
| 277 | For instance, if you write <code>a[2.0] = true</code>, | ||
| 278 | the actual key inserted into the table will be the integer <code>2</code>. | ||
| 279 | |||
| 280 | |||
| 281 | <p> | ||
| 282 | Tables, functions, threads, and (full) userdata values are <em>objects</em>: | ||
| 283 | variables do not actually <em>contain</em> these values, | ||
| 284 | only <em>references</em> to them. | ||
| 285 | Assignment, parameter passing, and function returns | ||
| 286 | always manipulate references to such values; | ||
| 287 | these operations do not imply any kind of copy. | ||
| 288 | |||
| 289 | |||
| 290 | <p> | ||
| 291 | The library function <a href="#pdf-type"><code>type</code></a> returns a string describing the type | ||
| 292 | of a given value (see <a href="#pdf-type"><code>type</code></a>). | ||
| 293 | |||
| 294 | |||
| 295 | |||
| 296 | |||
| 297 | |||
| 298 | <h2>2.2 – <a name="2.2">Environments and the Global Environment</a></h2> | ||
| 299 | |||
| 300 | <p> | ||
| 301 | As we will discuss further in <a href="#3.2">§3.2</a> and <a href="#3.3.3">§3.3.3</a>, | ||
| 302 | any reference to a free name | ||
| 303 | (that is, a name not bound to any declaration) <code>var</code> | ||
| 304 | is syntactically translated to <code>_ENV.var</code>. | ||
| 305 | Moreover, every chunk is compiled in the scope of | ||
| 306 | an external local variable named <code>_ENV</code> (see <a href="#3.3.2">§3.3.2</a>), | ||
| 307 | so <code>_ENV</code> itself is never a free name in a chunk. | ||
| 308 | |||
| 309 | |||
| 310 | <p> | ||
| 311 | Despite the existence of this external <code>_ENV</code> variable and | ||
| 312 | the translation of free names, | ||
| 313 | <code>_ENV</code> is a completely regular name. | ||
| 314 | In particular, | ||
| 315 | you can define new variables and parameters with that name. | ||
| 316 | Each reference to a free name uses the <code>_ENV</code> that is | ||
| 317 | visible at that point in the program, | ||
| 318 | following the usual visibility rules of Lua (see <a href="#3.5">§3.5</a>). | ||
| 319 | |||
| 320 | |||
| 321 | <p> | ||
| 322 | Any table used as the value of <code>_ENV</code> is called an <em>environment</em>. | ||
| 323 | |||
| 324 | |||
| 325 | <p> | ||
| 326 | Lua keeps a distinguished environment called the <em>global environment</em>. | ||
| 327 | This value is kept at a special index in the C registry (see <a href="#4.3">§4.3</a>). | ||
| 328 | In Lua, the global variable <a href="#pdf-_G"><code>_G</code></a> is initialized with this same value. | ||
| 329 | (<a href="#pdf-_G"><code>_G</code></a> is never used internally, | ||
| 330 | so changing its value will affect only your own code.) | ||
| 331 | |||
| 332 | |||
| 333 | <p> | ||
| 334 | When Lua loads a chunk, | ||
| 335 | the default value for its <code>_ENV</code> variable | ||
| 336 | is the global environment (see <a href="#pdf-load"><code>load</code></a>). | ||
| 337 | Therefore, by default, | ||
| 338 | free names in Lua code refer to entries in the global environment | ||
| 339 | and, therefore, they are also called <em>global variables</em>. | ||
| 340 | Moreover, all standard libraries are loaded in the global environment | ||
| 341 | and some functions there operate on that environment. | ||
| 342 | You can use <a href="#pdf-load"><code>load</code></a> (or <a href="#pdf-loadfile"><code>loadfile</code></a>) | ||
| 343 | to load a chunk with a different environment. | ||
| 344 | (In C, you have to load the chunk and then change the value | ||
| 345 | of its first upvalue; see <a href="#lua_setupvalue"><code>lua_setupvalue</code></a>.) | ||
| 346 | |||
| 347 | |||
| 348 | |||
| 349 | |||
| 350 | |||
| 351 | <h2>2.3 – <a name="2.3">Error Handling</a></h2> | ||
| 352 | |||
| 353 | <p> | ||
| 354 | Several operations in Lua can <em>raise</em> an error. | ||
| 355 | An error interrupts the normal flow of the program, | ||
| 356 | which can continue by <em>catching</em> the error. | ||
| 357 | |||
| 358 | |||
| 359 | <p> | ||
| 360 | Lua code can explicitly raise an error by calling the | ||
| 361 | <a href="#pdf-error"><code>error</code></a> function. | ||
| 362 | (This function never returns.) | ||
| 363 | |||
| 364 | |||
| 365 | <p> | ||
| 366 | To catch errors in Lua, | ||
| 367 | you can do a <em>protected call</em>, | ||
| 368 | using <a href="#pdf-pcall"><code>pcall</code></a> (or <a href="#pdf-xpcall"><code>xpcall</code></a>). | ||
| 369 | The function <a href="#pdf-pcall"><code>pcall</code></a> calls a given function in <em>protected mode</em>. | ||
| 370 | Any error while running the function stops its execution, | ||
| 371 | and control returns immediately to <code>pcall</code>, | ||
| 372 | which returns a status code. | ||
| 373 | |||
| 374 | |||
| 375 | <p> | ||
| 376 | Because Lua is an embedded extension language, | ||
| 377 | Lua code starts running by a call | ||
| 378 | from C code in the host program. | ||
| 379 | (When you use Lua standalone, | ||
| 380 | the <code>lua</code> application is the host program.) | ||
| 381 | Usually, this call is protected; | ||
| 382 | so, when an otherwise unprotected error occurs during | ||
| 383 | the compilation or execution of a Lua chunk, | ||
| 384 | control returns to the host, | ||
| 385 | which can take appropriate measures, | ||
| 386 | such as printing an error message. | ||
| 387 | |||
| 388 | |||
| 389 | <p> | ||
| 390 | Whenever there is an error, | ||
| 391 | an <em>error object</em> | ||
| 392 | is propagated with information about the error. | ||
| 393 | Lua itself only generates errors whose error object is a string, | ||
| 394 | but programs can generate errors with | ||
| 395 | any value as the error object. | ||
| 396 | It is up to the Lua program or its host to handle such error objects. | ||
| 397 | For historical reasons, | ||
| 398 | an error object is often called an <em>error message</em>, | ||
| 399 | even though it does not have to be a string. | ||
| 400 | |||
| 401 | |||
| 402 | <p> | ||
| 403 | When you use <a href="#pdf-xpcall"><code>xpcall</code></a> (or <a href="#lua_pcall"><code>lua_pcall</code></a>, in C) | ||
| 404 | you can give a <em>message handler</em> | ||
| 405 | to be called in case of errors. | ||
| 406 | This function is called with the original error object | ||
| 407 | and returns a new error object. | ||
| 408 | It is called before the error unwinds the stack, | ||
| 409 | so that it can gather more information about the error, | ||
| 410 | for instance by inspecting the stack and creating a stack traceback. | ||
| 411 | This message handler is still protected by the protected call; | ||
| 412 | so, an error inside the message handler | ||
| 413 | will call the message handler again. | ||
| 414 | If this loop goes on for too long, | ||
| 415 | Lua breaks it and returns an appropriate message. | ||
| 416 | The message handler is called only for regular runtime errors. | ||
| 417 | It is not called for memory-allocation errors | ||
| 418 | nor for errors while running finalizers or other message handlers. | ||
| 419 | |||
| 420 | |||
| 421 | <p> | ||
| 422 | Lua also offers a system of <em>warnings</em> (see <a href="#pdf-warn"><code>warn</code></a>). | ||
| 423 | Unlike errors, warnings do not interfere | ||
| 424 | in any way with program execution. | ||
| 425 | They typically only generate a message to the user, | ||
| 426 | although this behavior can be adapted from C (see <a href="#lua_setwarnf"><code>lua_setwarnf</code></a>). | ||
| 427 | |||
| 428 | |||
| 429 | |||
| 430 | |||
| 431 | |||
| 432 | <h2>2.4 – <a name="2.4">Metatables and Metamethods</a></h2> | ||
| 433 | |||
| 434 | <p> | ||
| 435 | Every value in Lua can have a <em>metatable</em>. | ||
| 436 | This <em>metatable</em> is an ordinary Lua table | ||
| 437 | that defines the behavior of the original value | ||
| 438 | under certain events. | ||
| 439 | You can change several aspects of the behavior | ||
| 440 | of a value by setting specific fields in its metatable. | ||
| 441 | For instance, when a non-numeric value is the operand of an addition, | ||
| 442 | Lua checks for a function in the field <code>__add</code> of the value's metatable. | ||
| 443 | If it finds one, | ||
| 444 | Lua calls this function to perform the addition. | ||
| 445 | |||
| 446 | |||
| 447 | <p> | ||
| 448 | The key for each event in a metatable is a string | ||
| 449 | with the event name prefixed by two underscores; | ||
| 450 | the corresponding value is called a <em>metavalue</em>. | ||
| 451 | For most events, the metavalue must be a function, | ||
| 452 | which is then called a <em>metamethod</em>. | ||
| 453 | In the previous example, the key is the string "<code>__add</code>" | ||
| 454 | and the metamethod is the function that performs the addition. | ||
| 455 | Unless stated otherwise, | ||
| 456 | a metamethod can in fact be any callable value, | ||
| 457 | which is either a function or a value with a <code>__call</code> metamethod. | ||
| 458 | |||
| 459 | |||
| 460 | <p> | ||
| 461 | You can query the metatable of any value | ||
| 462 | using the <a href="#pdf-getmetatable"><code>getmetatable</code></a> function. | ||
| 463 | Lua queries metamethods in metatables using a raw access (see <a href="#pdf-rawget"><code>rawget</code></a>). | ||
| 464 | |||
| 465 | |||
| 466 | <p> | ||
| 467 | You can replace the metatable of tables | ||
| 468 | using the <a href="#pdf-setmetatable"><code>setmetatable</code></a> function. | ||
| 469 | You cannot change the metatable of other types from Lua code, | ||
| 470 | except by using the debug library (<a href="#6.10">§6.10</a>). | ||
| 471 | |||
| 472 | |||
| 473 | <p> | ||
| 474 | Tables and full userdata have individual metatables, | ||
| 475 | although multiple tables and userdata can share their metatables. | ||
| 476 | Values of all other types share one single metatable per type; | ||
| 477 | that is, there is one single metatable for all numbers, | ||
| 478 | one for all strings, etc. | ||
| 479 | By default, a value has no metatable, | ||
| 480 | but the string library sets a metatable for the string type (see <a href="#6.4">§6.4</a>). | ||
| 481 | |||
| 482 | |||
| 483 | <p> | ||
| 484 | A detailed list of operations controlled by metatables is given next. | ||
| 485 | Each event is identified by its corresponding key. | ||
| 486 | By convention, all metatable keys used by Lua are composed by | ||
| 487 | two underscores followed by lowercase Latin letters. | ||
| 488 | |||
| 489 | |||
| 490 | |||
| 491 | <ul> | ||
| 492 | |||
| 493 | <li><b><code>__add</code>: </b> | ||
| 494 | the addition (<code>+</code>) operation. | ||
| 495 | If any operand for an addition is not a number, | ||
| 496 | Lua will try to call a metamethod. | ||
| 497 | It starts by checking the first operand (even if it is a number); | ||
| 498 | if that operand does not define a metamethod for <code>__add</code>, | ||
| 499 | then Lua will check the second operand. | ||
| 500 | If Lua can find a metamethod, | ||
| 501 | it calls the metamethod with the two operands as arguments, | ||
| 502 | and the result of the call | ||
| 503 | (adjusted to one value) | ||
| 504 | is the result of the operation. | ||
| 505 | Otherwise, if no metamethod is found, | ||
| 506 | Lua raises an error. | ||
| 507 | </li> | ||
| 508 | |||
| 509 | <li><b><code>__sub</code>: </b> | ||
| 510 | the subtraction (<code>-</code>) operation. | ||
| 511 | Behavior similar to the addition operation. | ||
| 512 | </li> | ||
| 513 | |||
| 514 | <li><b><code>__mul</code>: </b> | ||
| 515 | the multiplication (<code>*</code>) operation. | ||
| 516 | Behavior similar to the addition operation. | ||
| 517 | </li> | ||
| 518 | |||
| 519 | <li><b><code>__div</code>: </b> | ||
| 520 | the division (<code>/</code>) operation. | ||
| 521 | Behavior similar to the addition operation. | ||
| 522 | </li> | ||
| 523 | |||
| 524 | <li><b><code>__mod</code>: </b> | ||
| 525 | the modulo (<code>%</code>) operation. | ||
| 526 | Behavior similar to the addition operation. | ||
| 527 | </li> | ||
| 528 | |||
| 529 | <li><b><code>__pow</code>: </b> | ||
| 530 | the exponentiation (<code>^</code>) operation. | ||
| 531 | Behavior similar to the addition operation. | ||
| 532 | </li> | ||
| 533 | |||
| 534 | <li><b><code>__unm</code>: </b> | ||
| 535 | the negation (unary <code>-</code>) operation. | ||
| 536 | Behavior similar to the addition operation. | ||
| 537 | </li> | ||
| 538 | |||
| 539 | <li><b><code>__idiv</code>: </b> | ||
| 540 | the floor division (<code>//</code>) operation. | ||
| 541 | Behavior similar to the addition operation. | ||
| 542 | </li> | ||
| 543 | |||
| 544 | <li><b><code>__band</code>: </b> | ||
| 545 | the bitwise AND (<code>&</code>) operation. | ||
| 546 | Behavior similar to the addition operation, | ||
| 547 | except that Lua will try a metamethod | ||
| 548 | if any operand is neither an integer | ||
| 549 | nor a float coercible to an integer (see <a href="#3.4.3">§3.4.3</a>). | ||
| 550 | </li> | ||
| 551 | |||
| 552 | <li><b><code>__bor</code>: </b> | ||
| 553 | the bitwise OR (<code>|</code>) operation. | ||
| 554 | Behavior similar to the bitwise AND operation. | ||
| 555 | </li> | ||
| 556 | |||
| 557 | <li><b><code>__bxor</code>: </b> | ||
| 558 | the bitwise exclusive OR (binary <code>~</code>) operation. | ||
| 559 | Behavior similar to the bitwise AND operation. | ||
| 560 | </li> | ||
| 561 | |||
| 562 | <li><b><code>__bnot</code>: </b> | ||
| 563 | the bitwise NOT (unary <code>~</code>) operation. | ||
| 564 | Behavior similar to the bitwise AND operation. | ||
| 565 | </li> | ||
| 566 | |||
| 567 | <li><b><code>__shl</code>: </b> | ||
| 568 | the bitwise left shift (<code><<</code>) operation. | ||
| 569 | Behavior similar to the bitwise AND operation. | ||
| 570 | </li> | ||
| 571 | |||
| 572 | <li><b><code>__shr</code>: </b> | ||
| 573 | the bitwise right shift (<code>>></code>) operation. | ||
| 574 | Behavior similar to the bitwise AND operation. | ||
| 575 | </li> | ||
| 576 | |||
| 577 | <li><b><code>__concat</code>: </b> | ||
| 578 | the concatenation (<code>..</code>) operation. | ||
| 579 | Behavior similar to the addition operation, | ||
| 580 | except that Lua will try a metamethod | ||
| 581 | if any operand is neither a string nor a number | ||
| 582 | (which is always coercible to a string). | ||
| 583 | </li> | ||
| 584 | |||
| 585 | <li><b><code>__len</code>: </b> | ||
| 586 | the length (<code>#</code>) operation. | ||
| 587 | If the object is not a string, | ||
| 588 | Lua will try its metamethod. | ||
| 589 | If there is a metamethod, | ||
| 590 | Lua calls it with the object as argument, | ||
| 591 | and the result of the call | ||
| 592 | (always adjusted to one value) | ||
| 593 | is the result of the operation. | ||
| 594 | If there is no metamethod but the object is a table, | ||
| 595 | then Lua uses the table length operation (see <a href="#3.4.7">§3.4.7</a>). | ||
| 596 | Otherwise, Lua raises an error. | ||
| 597 | </li> | ||
| 598 | |||
| 599 | <li><b><code>__eq</code>: </b> | ||
| 600 | the equal (<code>==</code>) operation. | ||
| 601 | Behavior similar to the addition operation, | ||
| 602 | except that Lua will try a metamethod only when the values | ||
| 603 | being compared are either both tables or both full userdata | ||
| 604 | and they are not primitively equal. | ||
| 605 | The result of the call is always converted to a boolean. | ||
| 606 | </li> | ||
| 607 | |||
| 608 | <li><b><code>__lt</code>: </b> | ||
| 609 | the less than (<code><</code>) operation. | ||
| 610 | Behavior similar to the addition operation, | ||
| 611 | except that Lua will try a metamethod only when the values | ||
| 612 | being compared are neither both numbers nor both strings. | ||
| 613 | Moreover, the result of the call is always converted to a boolean. | ||
| 614 | </li> | ||
| 615 | |||
| 616 | <li><b><code>__le</code>: </b> | ||
| 617 | the less equal (<code><=</code>) operation. | ||
| 618 | Behavior similar to the less than operation. | ||
| 619 | </li> | ||
| 620 | |||
| 621 | <li><b><code>__index</code>: </b> | ||
| 622 | The indexing access operation <code>table[key]</code>. | ||
| 623 | This event happens when <code>table</code> is not a table or | ||
| 624 | when <code>key</code> is not present in <code>table</code>. | ||
| 625 | The metavalue is looked up in the metatable of <code>table</code>. | ||
| 626 | |||
| 627 | |||
| 628 | <p> | ||
| 629 | The metavalue for this event can be either a function, a table, | ||
| 630 | or any value with an <code>__index</code> metavalue. | ||
| 631 | If it is a function, | ||
| 632 | it is called with <code>table</code> and <code>key</code> as arguments, | ||
| 633 | and the result of the call | ||
| 634 | (adjusted to one value) | ||
| 635 | is the result of the operation. | ||
| 636 | Otherwise, | ||
| 637 | the final result is the result of indexing this metavalue with <code>key</code>. | ||
| 638 | This indexing is regular, not raw, | ||
| 639 | and therefore can trigger another <code>__index</code> metavalue. | ||
| 640 | </li> | ||
| 641 | |||
| 642 | <li><b><code>__newindex</code>: </b> | ||
| 643 | The indexing assignment <code>table[key] = value</code>. | ||
| 644 | Like the index event, | ||
| 645 | this event happens when <code>table</code> is not a table or | ||
| 646 | when <code>key</code> is not present in <code>table</code>. | ||
| 647 | The metavalue is looked up in the metatable of <code>table</code>. | ||
| 648 | |||
| 649 | |||
| 650 | <p> | ||
| 651 | Like with indexing, | ||
| 652 | the metavalue for this event can be either a function, a table, | ||
| 653 | or any value with an <code>__newindex</code> metavalue. | ||
| 654 | If it is a function, | ||
| 655 | it is called with <code>table</code>, <code>key</code>, and <code>value</code> as arguments. | ||
| 656 | Otherwise, | ||
| 657 | Lua repeats the indexing assignment over this metavalue | ||
| 658 | with the same key and value. | ||
| 659 | This assignment is regular, not raw, | ||
| 660 | and therefore can trigger another <code>__newindex</code> metavalue. | ||
| 661 | |||
| 662 | |||
| 663 | <p> | ||
| 664 | Whenever a <code>__newindex</code> metavalue is invoked, | ||
| 665 | Lua does not perform the primitive assignment. | ||
| 666 | If needed, | ||
| 667 | the metamethod itself can call <a href="#pdf-rawset"><code>rawset</code></a> | ||
| 668 | to do the assignment. | ||
| 669 | </li> | ||
| 670 | |||
| 671 | <li><b><code>__call</code>: </b> | ||
| 672 | The call operation <code>func(args)</code>. | ||
| 673 | This event happens when Lua tries to call a non-function value | ||
| 674 | (that is, <code>func</code> is not a function). | ||
| 675 | The metamethod is looked up in <code>func</code>. | ||
| 676 | If present, | ||
| 677 | the metamethod is called with <code>func</code> as its first argument, | ||
| 678 | followed by the arguments of the original call (<code>args</code>). | ||
| 679 | All results of the call | ||
| 680 | are the results of the operation. | ||
| 681 | This is the only metamethod that allows multiple results. | ||
| 682 | </li> | ||
| 683 | |||
| 684 | </ul> | ||
| 685 | |||
| 686 | <p> | ||
| 687 | In addition to the previous list, | ||
| 688 | the interpreter also respects the following keys in metatables: | ||
| 689 | <code>__gc</code> (see <a href="#2.5.3">§2.5.3</a>), | ||
| 690 | <code>__close</code> (see <a href="#3.3.8">§3.3.8</a>), | ||
| 691 | <code>__mode</code> (see <a href="#2.5.4">§2.5.4</a>), | ||
| 692 | and <code>__name</code>. | ||
| 693 | (The entry <code>__name</code>, | ||
| 694 | when it contains a string, | ||
| 695 | may be used by <a href="#pdf-tostring"><code>tostring</code></a> and in error messages.) | ||
| 696 | |||
| 697 | |||
| 698 | <p> | ||
| 699 | For the unary operators (negation, length, and bitwise NOT), | ||
| 700 | the metamethod is computed and called with a dummy second operand, | ||
| 701 | equal to the first one. | ||
| 702 | This extra operand is only to simplify Lua's internals | ||
| 703 | (by making these operators behave like a binary operation) | ||
| 704 | and may be removed in future versions. | ||
| 705 | For most uses this extra operand is irrelevant. | ||
| 706 | |||
| 707 | |||
| 708 | <p> | ||
| 709 | Because metatables are regular tables, | ||
| 710 | they can contain arbitrary fields, | ||
| 711 | not only the event names defined above. | ||
| 712 | Some functions in the standard library | ||
| 713 | (e.g., <a href="#pdf-tostring"><code>tostring</code></a>) | ||
| 714 | use other fields in metatables for their own purposes. | ||
| 715 | |||
| 716 | |||
| 717 | <p> | ||
| 718 | It is a good practice to add all needed metamethods to a table | ||
| 719 | before setting it as a metatable of some object. | ||
| 720 | In particular, the <code>__gc</code> metamethod works only when this order | ||
| 721 | is followed (see <a href="#2.5.3">§2.5.3</a>). | ||
| 722 | It is also a good practice to set the metatable of an object | ||
| 723 | right after its creation. | ||
| 724 | |||
| 725 | |||
| 726 | |||
| 727 | |||
| 728 | |||
| 729 | <h2>2.5 – <a name="2.5">Garbage Collection</a></h2> | ||
| 730 | |||
| 731 | |||
| 732 | |||
| 733 | <p> | ||
| 734 | Lua performs automatic memory management. | ||
| 735 | This means that | ||
| 736 | you do not have to worry about allocating memory for new objects | ||
| 737 | or freeing it when the objects are no longer needed. | ||
| 738 | Lua manages memory automatically by running | ||
| 739 | a <em>garbage collector</em> to collect all <em>dead</em> objects. | ||
| 740 | All memory used by Lua is subject to automatic management: | ||
| 741 | strings, tables, userdata, functions, threads, internal structures, etc. | ||
| 742 | |||
| 743 | |||
| 744 | <p> | ||
| 745 | An object is considered <em>dead</em> | ||
| 746 | as soon as the collector can be sure the object | ||
| 747 | will not be accessed again in the normal execution of the program. | ||
| 748 | ("Normal execution" here excludes finalizers, | ||
| 749 | which can resurrect dead objects (see <a href="#2.5.3">§2.5.3</a>), | ||
| 750 | and excludes also operations using the debug library.) | ||
| 751 | Note that the time when the collector can be sure that an object | ||
| 752 | is dead may not coincide with the programmer's expectations. | ||
| 753 | The only guarantees are that Lua will not collect an object | ||
| 754 | that may still be accessed in the normal execution of the program, | ||
| 755 | and it will eventually collect an object | ||
| 756 | that is inaccessible from Lua. | ||
| 757 | (Here, | ||
| 758 | <em>inaccessible from Lua</em> means that neither a variable nor | ||
| 759 | another live object refer to the object.) | ||
| 760 | Because Lua has no knowledge about C code, | ||
| 761 | it never collects objects accessible through the registry (see <a href="#4.3">§4.3</a>), | ||
| 762 | which includes the global environment (see <a href="#2.2">§2.2</a>). | ||
| 763 | |||
| 764 | |||
| 765 | <p> | ||
| 766 | The garbage collector (GC) in Lua can work in two modes: | ||
| 767 | incremental and generational. | ||
| 768 | |||
| 769 | |||
| 770 | <p> | ||
| 771 | The default GC mode with the default parameters | ||
| 772 | are adequate for most uses. | ||
| 773 | However, programs that waste a large proportion of their time | ||
| 774 | allocating and freeing memory can benefit from other settings. | ||
| 775 | Keep in mind that the GC behavior is non-portable | ||
| 776 | both across platforms and across different Lua releases; | ||
| 777 | therefore, optimal settings are also non-portable. | ||
| 778 | |||
| 779 | |||
| 780 | <p> | ||
| 781 | You can change the GC mode and parameters by calling | ||
| 782 | <a href="#lua_gc"><code>lua_gc</code></a> in C | ||
| 783 | or <a href="#pdf-collectgarbage"><code>collectgarbage</code></a> in Lua. | ||
| 784 | You can also use these functions to control | ||
| 785 | the collector directly (e.g., to stop and restart it). | ||
| 786 | |||
| 787 | |||
| 788 | |||
| 789 | |||
| 790 | |||
| 791 | <h3>2.5.1 – <a name="2.5.1">Incremental Garbage Collection</a></h3> | ||
| 792 | |||
| 793 | <p> | ||
| 794 | In incremental mode, | ||
| 795 | each GC cycle performs a mark-and-sweep collection in small steps | ||
| 796 | interleaved with the program's execution. | ||
| 797 | In this mode, | ||
| 798 | the collector uses three numbers to control its garbage-collection cycles: | ||
| 799 | the <em>garbage-collector pause</em>, | ||
| 800 | the <em>garbage-collector step multiplier</em>, | ||
| 801 | and the <em>garbage-collector step size</em>. | ||
| 802 | |||
| 803 | |||
| 804 | <p> | ||
| 805 | The garbage-collector pause | ||
| 806 | controls how long the collector waits before starting a new cycle. | ||
| 807 | The collector starts a new cycle when the use of memory | ||
| 808 | hits <em>n%</em> of the use after the previous collection. | ||
| 809 | Larger values make the collector less aggressive. | ||
| 810 | Values equal to or less than 100 mean the collector will not wait to | ||
| 811 | start a new cycle. | ||
| 812 | A value of 200 means that the collector waits for the total memory in use | ||
| 813 | to double before starting a new cycle. | ||
| 814 | The default value is 200; the maximum value is 1000. | ||
| 815 | |||
| 816 | |||
| 817 | <p> | ||
| 818 | The garbage-collector step multiplier | ||
| 819 | controls the speed of the collector relative to | ||
| 820 | memory allocation, | ||
| 821 | that is, | ||
| 822 | how many elements it marks or sweeps for each | ||
| 823 | kilobyte of memory allocated. | ||
| 824 | Larger values make the collector more aggressive but also increase | ||
| 825 | the size of each incremental step. | ||
| 826 | You should not use values less than 100, | ||
| 827 | because they make the collector too slow and | ||
| 828 | can result in the collector never finishing a cycle. | ||
| 829 | The default value is 100; the maximum value is 1000. | ||
| 830 | |||
| 831 | |||
| 832 | <p> | ||
| 833 | The garbage-collector step size controls the | ||
| 834 | size of each incremental step, | ||
| 835 | specifically how many bytes the interpreter allocates | ||
| 836 | before performing a step. | ||
| 837 | This parameter is logarithmic: | ||
| 838 | A value of <em>n</em> means the interpreter will allocate <em>2<sup>n</sup></em> | ||
| 839 | bytes between steps and perform equivalent work during the step. | ||
| 840 | A large value (e.g., 60) makes the collector a stop-the-world | ||
| 841 | (non-incremental) collector. | ||
| 842 | The default value is 13, | ||
| 843 | which means steps of approximately 8 Kbytes. | ||
| 844 | |||
| 845 | |||
| 846 | |||
| 847 | |||
| 848 | |||
| 849 | <h3>2.5.2 – <a name="2.5.2">Generational Garbage Collection</a></h3> | ||
| 850 | |||
| 851 | <p> | ||
| 852 | In generational mode, | ||
| 853 | the collector does frequent <em>minor</em> collections, | ||
| 854 | which traverses only objects recently created. | ||
| 855 | If after a minor collection the use of memory is still above a limit, | ||
| 856 | the collector does a stop-the-world <em>major</em> collection, | ||
| 857 | which traverses all objects. | ||
| 858 | The generational mode uses two parameters: | ||
| 859 | the <em>minor multiplier</em> and the <em>the major multiplier</em>. | ||
| 860 | |||
| 861 | |||
| 862 | <p> | ||
| 863 | The minor multiplier controls the frequency of minor collections. | ||
| 864 | For a minor multiplier <em>x</em>, | ||
| 865 | a new minor collection will be done when memory | ||
| 866 | grows <em>x%</em> larger than the memory in use after the previous major | ||
| 867 | collection. | ||
| 868 | For instance, for a multiplier of 20, | ||
| 869 | the collector will do a minor collection when the use of memory | ||
| 870 | gets 20% larger than the use after the previous major collection. | ||
| 871 | The default value is 20; the maximum value is 200. | ||
| 872 | |||
| 873 | |||
| 874 | <p> | ||
| 875 | The major multiplier controls the frequency of major collections. | ||
| 876 | For a major multiplier <em>x</em>, | ||
| 877 | a new major collection will be done when memory | ||
| 878 | grows <em>x%</em> larger than the memory in use after the previous major | ||
| 879 | collection. | ||
| 880 | For instance, for a multiplier of 100, | ||
| 881 | the collector will do a major collection when the use of memory | ||
| 882 | gets larger than twice the use after the previous collection. | ||
| 883 | The default value is 100; the maximum value is 1000. | ||
| 884 | |||
| 885 | |||
| 886 | |||
| 887 | |||
| 888 | |||
| 889 | <h3>2.5.3 – <a name="2.5.3">Garbage-Collection Metamethods</a></h3> | ||
| 890 | |||
| 891 | <p> | ||
| 892 | You can set garbage-collector metamethods for tables | ||
| 893 | and, using the C API, | ||
| 894 | for full userdata (see <a href="#2.4">§2.4</a>). | ||
| 895 | These metamethods, called <em>finalizers</em>, | ||
| 896 | are called when the garbage collector detects that the | ||
| 897 | corresponding table or userdata is dead. | ||
| 898 | Finalizers allow you to coordinate Lua's garbage collection | ||
| 899 | with external resource management such as closing files, | ||
| 900 | network or database connections, | ||
| 901 | or freeing your own memory. | ||
| 902 | |||
| 903 | |||
| 904 | <p> | ||
| 905 | For an object (table or userdata) to be finalized when collected, | ||
| 906 | you must <em>mark</em> it for finalization. | ||
| 907 | |||
| 908 | You mark an object for finalization when you set its metatable | ||
| 909 | and the metatable has a <code>__gc</code> metamethod. | ||
| 910 | Note that if you set a metatable without a <code>__gc</code> field | ||
| 911 | and later create that field in the metatable, | ||
| 912 | the object will not be marked for finalization. | ||
| 913 | |||
| 914 | |||
| 915 | <p> | ||
| 916 | When a marked object becomes dead, | ||
| 917 | it is not collected immediately by the garbage collector. | ||
| 918 | Instead, Lua puts it in a list. | ||
| 919 | After the collection, | ||
| 920 | Lua goes through that list. | ||
| 921 | For each object in the list, | ||
| 922 | it checks the object's <code>__gc</code> metamethod: | ||
| 923 | If it is present, | ||
| 924 | Lua calls it with the object as its single argument. | ||
| 925 | |||
| 926 | |||
| 927 | <p> | ||
| 928 | At the end of each garbage-collection cycle, | ||
| 929 | the finalizers are called in | ||
| 930 | the reverse order that the objects were marked for finalization, | ||
| 931 | among those collected in that cycle; | ||
| 932 | that is, the first finalizer to be called is the one associated | ||
| 933 | with the object marked last in the program. | ||
| 934 | The execution of each finalizer may occur at any point during | ||
| 935 | the execution of the regular code. | ||
| 936 | |||
| 937 | |||
| 938 | <p> | ||
| 939 | Because the object being collected must still be used by the finalizer, | ||
| 940 | that object (and other objects accessible only through it) | ||
| 941 | must be <em>resurrected</em> by Lua. | ||
| 942 | Usually, this resurrection is transient, | ||
| 943 | and the object memory is freed in the next garbage-collection cycle. | ||
| 944 | However, if the finalizer stores the object in some global place | ||
| 945 | (e.g., a global variable), | ||
| 946 | then the resurrection is permanent. | ||
| 947 | Moreover, if the finalizer marks a finalizing object for finalization again, | ||
| 948 | its finalizer will be called again in the next cycle where the | ||
| 949 | object is dead. | ||
| 950 | In any case, | ||
| 951 | the object memory is freed only in a GC cycle where | ||
| 952 | the object is dead and not marked for finalization. | ||
| 953 | |||
| 954 | |||
| 955 | <p> | ||
| 956 | When you close a state (see <a href="#lua_close"><code>lua_close</code></a>), | ||
| 957 | Lua calls the finalizers of all objects marked for finalization, | ||
| 958 | following the reverse order that they were marked. | ||
| 959 | If any finalizer marks objects for collection during that phase, | ||
| 960 | these marks have no effect. | ||
| 961 | |||
| 962 | |||
| 963 | <p> | ||
| 964 | Finalizers cannot yield nor run the garbage collector. | ||
| 965 | Because they can run in unpredictable times, | ||
| 966 | it is good practice to restrict each finalizer | ||
| 967 | to the minimum necessary to properly release | ||
| 968 | its associated resource. | ||
| 969 | |||
| 970 | |||
| 971 | <p> | ||
| 972 | Any error while running a finalizer generates a warning; | ||
| 973 | the error is not propagated. | ||
| 974 | |||
| 975 | |||
| 976 | |||
| 977 | |||
| 978 | |||
| 979 | <h3>2.5.4 – <a name="2.5.4">Weak Tables</a></h3> | ||
| 980 | |||
| 981 | <p> | ||
| 982 | A <em>weak table</em> is a table whose elements are | ||
| 983 | <em>weak references</em>. | ||
| 984 | A weak reference is ignored by the garbage collector. | ||
| 985 | In other words, | ||
| 986 | if the only references to an object are weak references, | ||
| 987 | then the garbage collector will collect that object. | ||
| 988 | |||
| 989 | |||
| 990 | <p> | ||
| 991 | A weak table can have weak keys, weak values, or both. | ||
| 992 | A table with weak values allows the collection of its values, | ||
| 993 | but prevents the collection of its keys. | ||
| 994 | A table with both weak keys and weak values allows the collection of | ||
| 995 | both keys and values. | ||
| 996 | In any case, if either the key or the value is collected, | ||
| 997 | the whole pair is removed from the table. | ||
| 998 | The weakness of a table is controlled by the | ||
| 999 | <code>__mode</code> field of its metatable. | ||
| 1000 | This metavalue, if present, must be one of the following strings: | ||
| 1001 | "<code>k</code>", for a table with weak keys; | ||
| 1002 | "<code>v</code>", for a table with weak values; | ||
| 1003 | or "<code>kv</code>", for a table with both weak keys and values. | ||
| 1004 | |||
| 1005 | |||
| 1006 | <p> | ||
| 1007 | A table with weak keys and strong values | ||
| 1008 | is also called an <em>ephemeron table</em>. | ||
| 1009 | In an ephemeron table, | ||
| 1010 | a value is considered reachable only if its key is reachable. | ||
| 1011 | In particular, | ||
| 1012 | if the only reference to a key comes through its value, | ||
| 1013 | the pair is removed. | ||
| 1014 | |||
| 1015 | |||
| 1016 | <p> | ||
| 1017 | Any change in the weakness of a table may take effect only | ||
| 1018 | at the next collect cycle. | ||
| 1019 | In particular, if you change the weakness to a stronger mode, | ||
| 1020 | Lua may still collect some items from that table | ||
| 1021 | before the change takes effect. | ||
| 1022 | |||
| 1023 | |||
| 1024 | <p> | ||
| 1025 | Only objects that have an explicit construction | ||
| 1026 | are removed from weak tables. | ||
| 1027 | Values, such as numbers and light C functions, | ||
| 1028 | are not subject to garbage collection, | ||
| 1029 | and therefore are not removed from weak tables | ||
| 1030 | (unless their associated values are collected). | ||
| 1031 | Although strings are subject to garbage collection, | ||
| 1032 | they do not have an explicit construction and | ||
| 1033 | their equality is by value; | ||
| 1034 | they behave more like values than like objects. | ||
| 1035 | Therefore, they are not removed from weak tables. | ||
| 1036 | |||
| 1037 | |||
| 1038 | <p> | ||
| 1039 | Resurrected objects | ||
| 1040 | (that is, objects being finalized | ||
| 1041 | and objects accessible only through objects being finalized) | ||
| 1042 | have a special behavior in weak tables. | ||
| 1043 | They are removed from weak values before running their finalizers, | ||
| 1044 | but are removed from weak keys only in the next collection | ||
| 1045 | after running their finalizers, when such objects are actually freed. | ||
| 1046 | This behavior allows the finalizer to access properties | ||
| 1047 | associated with the object through weak tables. | ||
| 1048 | |||
| 1049 | |||
| 1050 | <p> | ||
| 1051 | If a weak table is among the resurrected objects in a collection cycle, | ||
| 1052 | it may not be properly cleared until the next cycle. | ||
| 1053 | |||
| 1054 | |||
| 1055 | |||
| 1056 | |||
| 1057 | |||
| 1058 | |||
| 1059 | |||
| 1060 | <h2>2.6 – <a name="2.6">Coroutines</a></h2> | ||
| 1061 | |||
| 1062 | <p> | ||
| 1063 | Lua supports coroutines, | ||
| 1064 | also called <em>collaborative multithreading</em>. | ||
| 1065 | A coroutine in Lua represents an independent thread of execution. | ||
| 1066 | Unlike threads in multithread systems, however, | ||
| 1067 | a coroutine only suspends its execution by explicitly calling | ||
| 1068 | a yield function. | ||
| 1069 | |||
| 1070 | |||
| 1071 | <p> | ||
| 1072 | You create a coroutine by calling <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>. | ||
| 1073 | Its sole argument is a function | ||
| 1074 | that is the main function of the coroutine. | ||
| 1075 | The <code>create</code> function only creates a new coroutine and | ||
| 1076 | returns a handle to it (an object of type <em>thread</em>); | ||
| 1077 | it does not start the coroutine. | ||
| 1078 | |||
| 1079 | |||
| 1080 | <p> | ||
| 1081 | You execute a coroutine by calling <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>. | ||
| 1082 | When you first call <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>, | ||
| 1083 | passing as its first argument | ||
| 1084 | a thread returned by <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>, | ||
| 1085 | the coroutine starts its execution by | ||
| 1086 | calling its main function. | ||
| 1087 | Extra arguments passed to <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> are passed | ||
| 1088 | as arguments to that function. | ||
| 1089 | After the coroutine starts running, | ||
| 1090 | it runs until it terminates or <em>yields</em>. | ||
| 1091 | |||
| 1092 | |||
| 1093 | <p> | ||
| 1094 | A coroutine can terminate its execution in two ways: | ||
| 1095 | normally, when its main function returns | ||
| 1096 | (explicitly or implicitly, after the last instruction); | ||
| 1097 | and abnormally, if there is an unprotected error. | ||
| 1098 | In case of normal termination, | ||
| 1099 | <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> returns <b>true</b>, | ||
| 1100 | plus any values returned by the coroutine main function. | ||
| 1101 | In case of errors, <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> returns <b>false</b> | ||
| 1102 | plus the error object. | ||
| 1103 | In this case, the coroutine does not unwind its stack, | ||
| 1104 | so that it is possible to inspect it after the error | ||
| 1105 | with the debug API. | ||
| 1106 | |||
| 1107 | |||
| 1108 | <p> | ||
| 1109 | A coroutine yields by calling <a href="#pdf-coroutine.yield"><code>coroutine.yield</code></a>. | ||
| 1110 | When a coroutine yields, | ||
| 1111 | the corresponding <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> returns immediately, | ||
| 1112 | even if the yield happens inside nested function calls | ||
| 1113 | (that is, not in the main function, | ||
| 1114 | but in a function directly or indirectly called by the main function). | ||
| 1115 | In the case of a yield, <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> also returns <b>true</b>, | ||
| 1116 | plus any values passed to <a href="#pdf-coroutine.yield"><code>coroutine.yield</code></a>. | ||
| 1117 | The next time you resume the same coroutine, | ||
| 1118 | it continues its execution from the point where it yielded, | ||
| 1119 | with the call to <a href="#pdf-coroutine.yield"><code>coroutine.yield</code></a> returning any extra | ||
| 1120 | arguments passed to <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>. | ||
| 1121 | |||
| 1122 | |||
| 1123 | <p> | ||
| 1124 | Like <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>, | ||
| 1125 | the <a href="#pdf-coroutine.wrap"><code>coroutine.wrap</code></a> function also creates a coroutine, | ||
| 1126 | but instead of returning the coroutine itself, | ||
| 1127 | it returns a function that, when called, resumes the coroutine. | ||
| 1128 | Any arguments passed to this function | ||
| 1129 | go as extra arguments to <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>. | ||
| 1130 | <a href="#pdf-coroutine.wrap"><code>coroutine.wrap</code></a> returns all the values returned by <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>, | ||
| 1131 | except the first one (the boolean error code). | ||
| 1132 | Unlike <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>, | ||
| 1133 | the function created by <a href="#pdf-coroutine.wrap"><code>coroutine.wrap</code></a> | ||
| 1134 | propagates any error to the caller. | ||
| 1135 | In this case, | ||
| 1136 | the function also closes the coroutine (see <a href="#pdf-coroutine.close"><code>coroutine.close</code></a>). | ||
| 1137 | |||
| 1138 | |||
| 1139 | <p> | ||
| 1140 | As an example of how coroutines work, | ||
| 1141 | consider the following code: | ||
| 1142 | |||
| 1143 | <pre> | ||
| 1144 | function foo (a) | ||
| 1145 | print("foo", a) | ||
| 1146 | return coroutine.yield(2*a) | ||
| 1147 | end | ||
| 1148 | |||
| 1149 | co = coroutine.create(function (a,b) | ||
| 1150 | print("co-body", a, b) | ||
| 1151 | local r = foo(a+1) | ||
| 1152 | print("co-body", r) | ||
| 1153 | local r, s = coroutine.yield(a+b, a-b) | ||
| 1154 | print("co-body", r, s) | ||
| 1155 | return b, "end" | ||
| 1156 | end) | ||
| 1157 | |||
| 1158 | print("main", coroutine.resume(co, 1, 10)) | ||
| 1159 | print("main", coroutine.resume(co, "r")) | ||
| 1160 | print("main", coroutine.resume(co, "x", "y")) | ||
| 1161 | print("main", coroutine.resume(co, "x", "y")) | ||
| 1162 | </pre><p> | ||
| 1163 | When you run it, it produces the following output: | ||
| 1164 | |||
| 1165 | <pre> | ||
| 1166 | co-body 1 10 | ||
| 1167 | foo 2 | ||
| 1168 | main true 4 | ||
| 1169 | co-body r | ||
| 1170 | main true 11 -9 | ||
| 1171 | co-body x y | ||
| 1172 | main true 10 end | ||
| 1173 | main false cannot resume dead coroutine | ||
| 1174 | </pre> | ||
| 1175 | |||
| 1176 | <p> | ||
| 1177 | You can also create and manipulate coroutines through the C API: | ||
| 1178 | see functions <a href="#lua_newthread"><code>lua_newthread</code></a>, <a href="#lua_resume"><code>lua_resume</code></a>, | ||
| 1179 | and <a href="#lua_yield"><code>lua_yield</code></a>. | ||
| 1180 | |||
| 1181 | |||
| 1182 | |||
| 1183 | |||
| 1184 | |||
| 1185 | <h1>3 – <a name="3">The Language</a></h1> | ||
| 1186 | |||
| 1187 | |||
| 1188 | |||
| 1189 | <p> | ||
| 1190 | This section describes the lexis, the syntax, and the semantics of Lua. | ||
| 1191 | In other words, | ||
| 1192 | this section describes | ||
| 1193 | which tokens are valid, | ||
| 1194 | how they can be combined, | ||
| 1195 | and what their combinations mean. | ||
| 1196 | |||
| 1197 | |||
| 1198 | <p> | ||
| 1199 | Language constructs will be explained using the usual extended BNF notation, | ||
| 1200 | in which | ||
| 1201 | {<em>a</em>} means 0 or more <em>a</em>'s, and | ||
| 1202 | [<em>a</em>] means an optional <em>a</em>. | ||
| 1203 | Non-terminals are shown like non-terminal, | ||
| 1204 | keywords are shown like <b>kword</b>, | ||
| 1205 | and other terminal symbols are shown like ‘<b>=</b>’. | ||
| 1206 | The complete syntax of Lua can be found in <a href="#9">§9</a> | ||
| 1207 | at the end of this manual. | ||
| 1208 | |||
| 1209 | |||
| 1210 | |||
| 1211 | |||
| 1212 | |||
| 1213 | <h2>3.1 – <a name="3.1">Lexical Conventions</a></h2> | ||
| 1214 | |||
| 1215 | <p> | ||
| 1216 | Lua is a free-form language. | ||
| 1217 | It ignores spaces and comments between lexical elements (tokens), | ||
| 1218 | except as delimiters between two tokens. | ||
| 1219 | In source code, | ||
| 1220 | Lua recognizes as spaces the standard ASCII whitespace | ||
| 1221 | characters space, form feed, newline, | ||
| 1222 | carriage return, horizontal tab, and vertical tab. | ||
| 1223 | |||
| 1224 | |||
| 1225 | <p> | ||
| 1226 | <em>Names</em> | ||
| 1227 | (also called <em>identifiers</em>) | ||
| 1228 | in Lua can be any string of Latin letters, | ||
| 1229 | Arabic-Indic digits, and underscores, | ||
| 1230 | not beginning with a digit and | ||
| 1231 | not being a reserved word. | ||
| 1232 | Identifiers are used to name variables, table fields, and labels. | ||
| 1233 | |||
| 1234 | |||
| 1235 | <p> | ||
| 1236 | The following <em>keywords</em> are reserved | ||
| 1237 | and cannot be used as names: | ||
| 1238 | |||
| 1239 | |||
| 1240 | <pre> | ||
| 1241 | and break do else elseif end | ||
| 1242 | false for function goto if in | ||
| 1243 | local nil not or repeat return | ||
| 1244 | then true until while | ||
| 1245 | </pre> | ||
| 1246 | |||
| 1247 | <p> | ||
| 1248 | Lua is a case-sensitive language: | ||
| 1249 | <code>and</code> is a reserved word, but <code>And</code> and <code>AND</code> | ||
| 1250 | are two different, valid names. | ||
| 1251 | As a convention, | ||
| 1252 | programs should avoid creating | ||
| 1253 | names that start with an underscore followed by | ||
| 1254 | one or more uppercase letters (such as <a href="#pdf-_VERSION"><code>_VERSION</code></a>). | ||
| 1255 | |||
| 1256 | |||
| 1257 | <p> | ||
| 1258 | The following strings denote other tokens: | ||
| 1259 | |||
| 1260 | <pre> | ||
| 1261 | + - * / % ^ # | ||
| 1262 | & ~ | << >> // | ||
| 1263 | == ~= <= >= < > = | ||
| 1264 | ( ) { } [ ] :: | ||
| 1265 | ; : , . .. ... | ||
| 1266 | </pre> | ||
| 1267 | |||
| 1268 | <p> | ||
| 1269 | A <em>short literal string</em> | ||
| 1270 | can be delimited by matching single or double quotes, | ||
| 1271 | and can contain the following C-like escape sequences: | ||
| 1272 | '<code>\a</code>' (bell), | ||
| 1273 | '<code>\b</code>' (backspace), | ||
| 1274 | '<code>\f</code>' (form feed), | ||
| 1275 | '<code>\n</code>' (newline), | ||
| 1276 | '<code>\r</code>' (carriage return), | ||
| 1277 | '<code>\t</code>' (horizontal tab), | ||
| 1278 | '<code>\v</code>' (vertical tab), | ||
| 1279 | '<code>\\</code>' (backslash), | ||
| 1280 | '<code>\"</code>' (quotation mark [double quote]), | ||
| 1281 | and '<code>\'</code>' (apostrophe [single quote]). | ||
| 1282 | A backslash followed by a line break | ||
| 1283 | results in a newline in the string. | ||
| 1284 | The escape sequence '<code>\z</code>' skips the following span | ||
| 1285 | of whitespace characters, | ||
| 1286 | including line breaks; | ||
| 1287 | it is particularly useful to break and indent a long literal string | ||
| 1288 | into multiple lines without adding the newlines and spaces | ||
| 1289 | into the string contents. | ||
| 1290 | A short literal string cannot contain unescaped line breaks | ||
| 1291 | nor escapes not forming a valid escape sequence. | ||
| 1292 | |||
| 1293 | |||
| 1294 | <p> | ||
| 1295 | We can specify any byte in a short literal string, | ||
| 1296 | including embedded zeros, | ||
| 1297 | by its numeric value. | ||
| 1298 | This can be done | ||
| 1299 | with the escape sequence <code>\x<em>XX</em></code>, | ||
| 1300 | where <em>XX</em> is a sequence of exactly two hexadecimal digits, | ||
| 1301 | or with the escape sequence <code>\<em>ddd</em></code>, | ||
| 1302 | where <em>ddd</em> is a sequence of up to three decimal digits. | ||
| 1303 | (Note that if a decimal escape sequence is to be followed by a digit, | ||
| 1304 | it must be expressed using exactly three digits.) | ||
| 1305 | |||
| 1306 | |||
| 1307 | <p> | ||
| 1308 | The UTF-8 encoding of a Unicode character | ||
| 1309 | can be inserted in a literal string with | ||
| 1310 | the escape sequence <code>\u{<em>XXX</em>}</code> | ||
| 1311 | (with mandatory enclosing braces), | ||
| 1312 | where <em>XXX</em> is a sequence of one or more hexadecimal digits | ||
| 1313 | representing the character code point. | ||
| 1314 | This code point can be any value less than <em>2<sup>31</sup></em>. | ||
| 1315 | (Lua uses the original UTF-8 specification here, | ||
| 1316 | which is not restricted to valid Unicode code points.) | ||
| 1317 | |||
| 1318 | |||
| 1319 | <p> | ||
| 1320 | Literal strings can also be defined using a long format | ||
| 1321 | enclosed by <em>long brackets</em>. | ||
| 1322 | We define an <em>opening long bracket of level <em>n</em></em> as an opening | ||
| 1323 | square bracket followed by <em>n</em> equal signs followed by another | ||
| 1324 | opening square bracket. | ||
| 1325 | So, an opening long bracket of level 0 is written as <code>[[</code>, | ||
| 1326 | an opening long bracket of level 1 is written as <code>[=[</code>, | ||
| 1327 | and so on. | ||
| 1328 | A <em>closing long bracket</em> is defined similarly; | ||
| 1329 | for instance, | ||
| 1330 | a closing long bracket of level 4 is written as <code>]====]</code>. | ||
| 1331 | A <em>long literal</em> starts with an opening long bracket of any level and | ||
| 1332 | ends at the first closing long bracket of the same level. | ||
| 1333 | It can contain any text except a closing bracket of the same level. | ||
| 1334 | Literals in this bracketed form can run for several lines, | ||
| 1335 | do not interpret any escape sequences, | ||
| 1336 | and ignore long brackets of any other level. | ||
| 1337 | Any kind of end-of-line sequence | ||
| 1338 | (carriage return, newline, carriage return followed by newline, | ||
| 1339 | or newline followed by carriage return) | ||
| 1340 | is converted to a simple newline. | ||
| 1341 | When the opening long bracket is immediately followed by a newline, | ||
| 1342 | the newline is not included in the string. | ||
| 1343 | |||
| 1344 | |||
| 1345 | <p> | ||
| 1346 | As an example, in a system using ASCII | ||
| 1347 | (in which '<code>a</code>' is coded as 97, | ||
| 1348 | newline is coded as 10, and '<code>1</code>' is coded as 49), | ||
| 1349 | the five literal strings below denote the same string: | ||
| 1350 | |||
| 1351 | <pre> | ||
| 1352 | a = 'alo\n123"' | ||
| 1353 | a = "alo\n123\"" | ||
| 1354 | a = '\97lo\10\04923"' | ||
| 1355 | a = [[alo | ||
| 1356 | 123"]] | ||
| 1357 | a = [==[ | ||
| 1358 | alo | ||
| 1359 | 123"]==] | ||
| 1360 | </pre> | ||
| 1361 | |||
| 1362 | <p> | ||
| 1363 | Any byte in a literal string not | ||
| 1364 | explicitly affected by the previous rules represents itself. | ||
| 1365 | However, Lua opens files for parsing in text mode, | ||
| 1366 | and the system's file functions may have problems with | ||
| 1367 | some control characters. | ||
| 1368 | So, it is safer to represent | ||
| 1369 | binary data as a quoted literal with | ||
| 1370 | explicit escape sequences for the non-text characters. | ||
| 1371 | |||
| 1372 | |||
| 1373 | <p> | ||
| 1374 | A <em>numeric constant</em> (or <em>numeral</em>) | ||
| 1375 | can be written with an optional fractional part | ||
| 1376 | and an optional decimal exponent, | ||
| 1377 | marked by a letter '<code>e</code>' or '<code>E</code>'. | ||
| 1378 | Lua also accepts hexadecimal constants, | ||
| 1379 | which start with <code>0x</code> or <code>0X</code>. | ||
| 1380 | Hexadecimal constants also accept an optional fractional part | ||
| 1381 | plus an optional binary exponent, | ||
| 1382 | marked by a letter '<code>p</code>' or '<code>P</code>' and written in decimal. | ||
| 1383 | (For instance, <code>0x1.fp10</code> denotes 1984, | ||
| 1384 | which is <em>0x1f / 16</em> multiplied by <em>2<sup>10</sup></em>.) | ||
| 1385 | |||
| 1386 | |||
| 1387 | <p> | ||
| 1388 | A numeric constant with a radix point or an exponent | ||
| 1389 | denotes a float; | ||
| 1390 | otherwise, | ||
| 1391 | if its value fits in an integer or it is a hexadecimal constant, | ||
| 1392 | it denotes an integer; | ||
| 1393 | otherwise (that is, a decimal integer numeral that overflows), | ||
| 1394 | it denotes a float. | ||
| 1395 | Hexadecimal numerals with neither a radix point nor an exponent | ||
| 1396 | always denote an integer value; | ||
| 1397 | if the value overflows, it <em>wraps around</em> | ||
| 1398 | to fit into a valid integer. | ||
| 1399 | |||
| 1400 | |||
| 1401 | <p> | ||
| 1402 | Examples of valid integer constants are | ||
| 1403 | |||
| 1404 | <pre> | ||
| 1405 | 3 345 0xff 0xBEBADA | ||
| 1406 | </pre><p> | ||
| 1407 | Examples of valid float constants are | ||
| 1408 | |||
| 1409 | <pre> | ||
| 1410 | 3.0 3.1416 314.16e-2 0.31416E1 34e1 | ||
| 1411 | 0x0.1E 0xA23p-4 0X1.921FB54442D18P+1 | ||
| 1412 | </pre> | ||
| 1413 | |||
| 1414 | <p> | ||
| 1415 | A <em>comment</em> starts with a double hyphen (<code>--</code>) | ||
| 1416 | anywhere outside a string. | ||
| 1417 | If the text immediately after <code>--</code> is not an opening long bracket, | ||
| 1418 | the comment is a <em>short comment</em>, | ||
| 1419 | which runs until the end of the line. | ||
| 1420 | Otherwise, it is a <em>long comment</em>, | ||
| 1421 | which runs until the corresponding closing long bracket. | ||
| 1422 | |||
| 1423 | |||
| 1424 | |||
| 1425 | |||
| 1426 | |||
| 1427 | <h2>3.2 – <a name="3.2">Variables</a></h2> | ||
| 1428 | |||
| 1429 | <p> | ||
| 1430 | Variables are places that store values. | ||
| 1431 | There are three kinds of variables in Lua: | ||
| 1432 | global variables, local variables, and table fields. | ||
| 1433 | |||
| 1434 | |||
| 1435 | <p> | ||
| 1436 | A single name can denote a global variable or a local variable | ||
| 1437 | (or a function's formal parameter, | ||
| 1438 | which is a particular kind of local variable): | ||
| 1439 | |||
| 1440 | <pre> | ||
| 1441 | var ::= Name | ||
| 1442 | </pre><p> | ||
| 1443 | Name denotes identifiers (see <a href="#3.1">§3.1</a>). | ||
| 1444 | |||
| 1445 | |||
| 1446 | <p> | ||
| 1447 | Any variable name is assumed to be global unless explicitly declared | ||
| 1448 | as a local (see <a href="#3.3.7">§3.3.7</a>). | ||
| 1449 | Local variables are <em>lexically scoped</em>: | ||
| 1450 | local variables can be freely accessed by functions | ||
| 1451 | defined inside their scope (see <a href="#3.5">§3.5</a>). | ||
| 1452 | |||
| 1453 | |||
| 1454 | <p> | ||
| 1455 | Before the first assignment to a variable, its value is <b>nil</b>. | ||
| 1456 | |||
| 1457 | |||
| 1458 | <p> | ||
| 1459 | Square brackets are used to index a table: | ||
| 1460 | |||
| 1461 | <pre> | ||
| 1462 | var ::= prefixexp ‘<b>[</b>’ exp ‘<b>]</b>’ | ||
| 1463 | </pre><p> | ||
| 1464 | The meaning of accesses to table fields can be changed via metatables | ||
| 1465 | (see <a href="#2.4">§2.4</a>). | ||
| 1466 | |||
| 1467 | |||
| 1468 | <p> | ||
| 1469 | The syntax <code>var.Name</code> is just syntactic sugar for | ||
| 1470 | <code>var["Name"]</code>: | ||
| 1471 | |||
| 1472 | <pre> | ||
| 1473 | var ::= prefixexp ‘<b>.</b>’ Name | ||
| 1474 | </pre> | ||
| 1475 | |||
| 1476 | <p> | ||
| 1477 | An access to a global variable <code>x</code> | ||
| 1478 | is equivalent to <code>_ENV.x</code>. | ||
| 1479 | Due to the way that chunks are compiled, | ||
| 1480 | the variable <code>_ENV</code> itself is never global (see <a href="#2.2">§2.2</a>). | ||
| 1481 | |||
| 1482 | |||
| 1483 | |||
| 1484 | |||
| 1485 | |||
| 1486 | <h2>3.3 – <a name="3.3">Statements</a></h2> | ||
| 1487 | |||
| 1488 | |||
| 1489 | |||
| 1490 | <p> | ||
| 1491 | Lua supports an almost conventional set of statements, | ||
| 1492 | similar to those in other conventional languages. | ||
| 1493 | This set includes | ||
| 1494 | blocks, assignments, control structures, function calls, | ||
| 1495 | and variable declarations. | ||
| 1496 | |||
| 1497 | |||
| 1498 | |||
| 1499 | |||
| 1500 | |||
| 1501 | <h3>3.3.1 – <a name="3.3.1">Blocks</a></h3> | ||
| 1502 | |||
| 1503 | <p> | ||
| 1504 | A block is a list of statements, | ||
| 1505 | which are executed sequentially: | ||
| 1506 | |||
| 1507 | <pre> | ||
| 1508 | block ::= {stat} | ||
| 1509 | </pre><p> | ||
| 1510 | Lua has <em>empty statements</em> | ||
| 1511 | that allow you to separate statements with semicolons, | ||
| 1512 | start a block with a semicolon | ||
| 1513 | or write two semicolons in sequence: | ||
| 1514 | |||
| 1515 | <pre> | ||
| 1516 | stat ::= ‘<b>;</b>’ | ||
| 1517 | </pre> | ||
| 1518 | |||
| 1519 | <p> | ||
| 1520 | Both function calls and assignments | ||
| 1521 | can start with an open parenthesis. | ||
| 1522 | This possibility leads to an ambiguity in Lua's grammar. | ||
| 1523 | Consider the following fragment: | ||
| 1524 | |||
| 1525 | <pre> | ||
| 1526 | a = b + c | ||
| 1527 | (print or io.write)('done') | ||
| 1528 | </pre><p> | ||
| 1529 | The grammar could see this fragment in two ways: | ||
| 1530 | |||
| 1531 | <pre> | ||
| 1532 | a = b + c(print or io.write)('done') | ||
| 1533 | |||
| 1534 | a = b + c; (print or io.write)('done') | ||
| 1535 | </pre><p> | ||
| 1536 | The current parser always sees such constructions | ||
| 1537 | in the first way, | ||
| 1538 | interpreting the open parenthesis | ||
| 1539 | as the start of the arguments to a call. | ||
| 1540 | To avoid this ambiguity, | ||
| 1541 | it is a good practice to always precede with a semicolon | ||
| 1542 | statements that start with a parenthesis: | ||
| 1543 | |||
| 1544 | <pre> | ||
| 1545 | ;(print or io.write)('done') | ||
| 1546 | </pre> | ||
| 1547 | |||
| 1548 | <p> | ||
| 1549 | A block can be explicitly delimited to produce a single statement: | ||
| 1550 | |||
| 1551 | <pre> | ||
| 1552 | stat ::= <b>do</b> block <b>end</b> | ||
| 1553 | </pre><p> | ||
| 1554 | Explicit blocks are useful | ||
| 1555 | to control the scope of variable declarations. | ||
| 1556 | Explicit blocks are also sometimes used to | ||
| 1557 | add a <b>return</b> statement in the middle | ||
| 1558 | of another block (see <a href="#3.3.4">§3.3.4</a>). | ||
| 1559 | |||
| 1560 | |||
| 1561 | |||
| 1562 | |||
| 1563 | |||
| 1564 | <h3>3.3.2 – <a name="3.3.2">Chunks</a></h3> | ||
| 1565 | |||
| 1566 | <p> | ||
| 1567 | The unit of compilation of Lua is called a <em>chunk</em>. | ||
| 1568 | Syntactically, | ||
| 1569 | a chunk is simply a block: | ||
| 1570 | |||
| 1571 | <pre> | ||
| 1572 | chunk ::= block | ||
| 1573 | </pre> | ||
| 1574 | |||
| 1575 | <p> | ||
| 1576 | Lua handles a chunk as the body of an anonymous function | ||
| 1577 | with a variable number of arguments | ||
| 1578 | (see <a href="#3.4.11">§3.4.11</a>). | ||
| 1579 | As such, chunks can define local variables, | ||
| 1580 | receive arguments, and return values. | ||
| 1581 | Moreover, such anonymous function is compiled as in the | ||
| 1582 | scope of an external local variable called <code>_ENV</code> (see <a href="#2.2">§2.2</a>). | ||
| 1583 | The resulting function always has <code>_ENV</code> as its only external variable, | ||
| 1584 | even if it does not use that variable. | ||
| 1585 | |||
| 1586 | |||
| 1587 | <p> | ||
| 1588 | A chunk can be stored in a file or in a string inside the host program. | ||
| 1589 | To execute a chunk, | ||
| 1590 | Lua first <em>loads</em> it, | ||
| 1591 | precompiling the chunk's code into instructions for a virtual machine, | ||
| 1592 | and then Lua executes the compiled code | ||
| 1593 | with an interpreter for the virtual machine. | ||
| 1594 | |||
| 1595 | |||
| 1596 | <p> | ||
| 1597 | Chunks can also be precompiled into binary form; | ||
| 1598 | see the program <code>luac</code> and the function <a href="#pdf-string.dump"><code>string.dump</code></a> for details. | ||
| 1599 | Programs in source and compiled forms are interchangeable; | ||
| 1600 | Lua automatically detects the file type and acts accordingly (see <a href="#pdf-load"><code>load</code></a>). | ||
| 1601 | |||
| 1602 | |||
| 1603 | |||
| 1604 | |||
| 1605 | |||
| 1606 | <h3>3.3.3 – <a name="3.3.3">Assignment</a></h3> | ||
| 1607 | |||
| 1608 | <p> | ||
| 1609 | Lua allows multiple assignments. | ||
| 1610 | Therefore, the syntax for assignment | ||
| 1611 | defines a list of variables on the left side | ||
| 1612 | and a list of expressions on the right side. | ||
| 1613 | The elements in both lists are separated by commas: | ||
| 1614 | |||
| 1615 | <pre> | ||
| 1616 | stat ::= varlist ‘<b>=</b>’ explist | ||
| 1617 | varlist ::= var {‘<b>,</b>’ var} | ||
| 1618 | explist ::= exp {‘<b>,</b>’ exp} | ||
| 1619 | </pre><p> | ||
| 1620 | Expressions are discussed in <a href="#3.4">§3.4</a>. | ||
| 1621 | |||
| 1622 | |||
| 1623 | <p> | ||
| 1624 | Before the assignment, | ||
| 1625 | the list of values is <em>adjusted</em> to the length of | ||
| 1626 | the list of variables (see <a href="#3.4.12">§3.4.12</a>). | ||
| 1627 | |||
| 1628 | |||
| 1629 | <p> | ||
| 1630 | If a variable is both assigned and read | ||
| 1631 | inside a multiple assignment, | ||
| 1632 | Lua ensures that all reads get the value of the variable | ||
| 1633 | before the assignment. | ||
| 1634 | Thus the code | ||
| 1635 | |||
| 1636 | <pre> | ||
| 1637 | i = 3 | ||
| 1638 | i, a[i] = i+1, 20 | ||
| 1639 | </pre><p> | ||
| 1640 | sets <code>a[3]</code> to 20, without affecting <code>a[4]</code> | ||
| 1641 | because the <code>i</code> in <code>a[i]</code> is evaluated (to 3) | ||
| 1642 | before it is assigned 4. | ||
| 1643 | Similarly, the line | ||
| 1644 | |||
| 1645 | <pre> | ||
| 1646 | x, y = y, x | ||
| 1647 | </pre><p> | ||
| 1648 | exchanges the values of <code>x</code> and <code>y</code>, | ||
| 1649 | and | ||
| 1650 | |||
| 1651 | <pre> | ||
| 1652 | x, y, z = y, z, x | ||
| 1653 | </pre><p> | ||
| 1654 | cyclically permutes the values of <code>x</code>, <code>y</code>, and <code>z</code>. | ||
| 1655 | |||
| 1656 | |||
| 1657 | <p> | ||
| 1658 | Note that this guarantee covers only accesses | ||
| 1659 | syntactically inside the assignment statement. | ||
| 1660 | If a function or a metamethod called during the assignment | ||
| 1661 | changes the value of a variable, | ||
| 1662 | Lua gives no guarantees about the order of that access. | ||
| 1663 | |||
| 1664 | |||
| 1665 | <p> | ||
| 1666 | An assignment to a global name <code>x = val</code> | ||
| 1667 | is equivalent to the assignment | ||
| 1668 | <code>_ENV.x = val</code> (see <a href="#2.2">§2.2</a>). | ||
| 1669 | |||
| 1670 | |||
| 1671 | <p> | ||
| 1672 | The meaning of assignments to table fields and | ||
| 1673 | global variables (which are actually table fields, too) | ||
| 1674 | can be changed via metatables (see <a href="#2.4">§2.4</a>). | ||
| 1675 | |||
| 1676 | |||
| 1677 | |||
| 1678 | |||
| 1679 | |||
| 1680 | <h3>3.3.4 – <a name="3.3.4">Control Structures</a></h3><p> | ||
| 1681 | The control structures | ||
| 1682 | <b>if</b>, <b>while</b>, and <b>repeat</b> have the usual meaning and | ||
| 1683 | familiar syntax: | ||
| 1684 | |||
| 1685 | |||
| 1686 | |||
| 1687 | |||
| 1688 | <pre> | ||
| 1689 | stat ::= <b>while</b> exp <b>do</b> block <b>end</b> | ||
| 1690 | stat ::= <b>repeat</b> block <b>until</b> exp | ||
| 1691 | stat ::= <b>if</b> exp <b>then</b> block {<b>elseif</b> exp <b>then</b> block} [<b>else</b> block] <b>end</b> | ||
| 1692 | </pre><p> | ||
| 1693 | Lua also has a <b>for</b> statement, in two flavors (see <a href="#3.3.5">§3.3.5</a>). | ||
| 1694 | |||
| 1695 | |||
| 1696 | <p> | ||
| 1697 | The condition expression of a | ||
| 1698 | control structure can return any value. | ||
| 1699 | Both <b>false</b> and <b>nil</b> test false. | ||
| 1700 | All values different from <b>nil</b> and <b>false</b> test true. | ||
| 1701 | In particular, the number 0 and the empty string also test true. | ||
| 1702 | |||
| 1703 | |||
| 1704 | <p> | ||
| 1705 | In the <b>repeat</b>–<b>until</b> loop, | ||
| 1706 | the inner block does not end at the <b>until</b> keyword, | ||
| 1707 | but only after the condition. | ||
| 1708 | So, the condition can refer to local variables | ||
| 1709 | declared inside the loop block. | ||
| 1710 | |||
| 1711 | |||
| 1712 | <p> | ||
| 1713 | The <b>goto</b> statement transfers the program control to a label. | ||
| 1714 | For syntactical reasons, | ||
| 1715 | labels in Lua are considered statements too: | ||
| 1716 | |||
| 1717 | |||
| 1718 | |||
| 1719 | <pre> | ||
| 1720 | stat ::= <b>goto</b> Name | ||
| 1721 | stat ::= label | ||
| 1722 | label ::= ‘<b>::</b>’ Name ‘<b>::</b>’ | ||
| 1723 | </pre> | ||
| 1724 | |||
| 1725 | <p> | ||
| 1726 | A label is visible in the entire block where it is defined, | ||
| 1727 | except inside nested functions. | ||
| 1728 | A goto can jump to any visible label as long as it does not | ||
| 1729 | enter into the scope of a local variable. | ||
| 1730 | A label should not be declared | ||
| 1731 | where a label with the same name is visible, | ||
| 1732 | even if this other label has been declared in an enclosing block. | ||
| 1733 | |||
| 1734 | |||
| 1735 | <p> | ||
| 1736 | The <b>break</b> statement terminates the execution of a | ||
| 1737 | <b>while</b>, <b>repeat</b>, or <b>for</b> loop, | ||
| 1738 | skipping to the next statement after the loop: | ||
| 1739 | |||
| 1740 | |||
| 1741 | <pre> | ||
| 1742 | stat ::= <b>break</b> | ||
| 1743 | </pre><p> | ||
| 1744 | A <b>break</b> ends the innermost enclosing loop. | ||
| 1745 | |||
| 1746 | |||
| 1747 | <p> | ||
| 1748 | The <b>return</b> statement is used to return values | ||
| 1749 | from a function or a chunk | ||
| 1750 | (which is handled as an anonymous function). | ||
| 1751 | |||
| 1752 | Functions can return more than one value, | ||
| 1753 | so the syntax for the <b>return</b> statement is | ||
| 1754 | |||
| 1755 | <pre> | ||
| 1756 | stat ::= <b>return</b> [explist] [‘<b>;</b>’] | ||
| 1757 | </pre> | ||
| 1758 | |||
| 1759 | <p> | ||
| 1760 | The <b>return</b> statement can only be written | ||
| 1761 | as the last statement of a block. | ||
| 1762 | If it is necessary to <b>return</b> in the middle of a block, | ||
| 1763 | then an explicit inner block can be used, | ||
| 1764 | as in the idiom <code>do return end</code>, | ||
| 1765 | because now <b>return</b> is the last statement in its (inner) block. | ||
| 1766 | |||
| 1767 | |||
| 1768 | |||
| 1769 | |||
| 1770 | |||
| 1771 | <h3>3.3.5 – <a name="3.3.5">For Statement</a></h3> | ||
| 1772 | |||
| 1773 | <p> | ||
| 1774 | |||
| 1775 | The <b>for</b> statement has two forms: | ||
| 1776 | one numerical and one generic. | ||
| 1777 | |||
| 1778 | |||
| 1779 | |||
| 1780 | <h4>The numerical <b>for</b> loop</h4> | ||
| 1781 | |||
| 1782 | <p> | ||
| 1783 | The numerical <b>for</b> loop repeats a block of code while a | ||
| 1784 | control variable goes through an arithmetic progression. | ||
| 1785 | It has the following syntax: | ||
| 1786 | |||
| 1787 | <pre> | ||
| 1788 | stat ::= <b>for</b> Name ‘<b>=</b>’ exp ‘<b>,</b>’ exp [‘<b>,</b>’ exp] <b>do</b> block <b>end</b> | ||
| 1789 | </pre><p> | ||
| 1790 | The given identifier (Name) defines the control variable, | ||
| 1791 | which is a new variable local to the loop body (<em>block</em>). | ||
| 1792 | |||
| 1793 | |||
| 1794 | <p> | ||
| 1795 | The loop starts by evaluating once the three control expressions. | ||
| 1796 | Their values are called respectively | ||
| 1797 | the <em>initial value</em>, the <em>limit</em>, and the <em>step</em>. | ||
| 1798 | If the step is absent, it defaults to 1. | ||
| 1799 | |||
| 1800 | |||
| 1801 | <p> | ||
| 1802 | If both the initial value and the step are integers, | ||
| 1803 | the loop is done with integers; | ||
| 1804 | note that the limit may not be an integer. | ||
| 1805 | Otherwise, the three values are converted to | ||
| 1806 | floats and the loop is done with floats. | ||
| 1807 | Beware of floating-point accuracy in this case. | ||
| 1808 | |||
| 1809 | |||
| 1810 | <p> | ||
| 1811 | After that initialization, | ||
| 1812 | the loop body is repeated with the value of the control variable | ||
| 1813 | going through an arithmetic progression, | ||
| 1814 | starting at the initial value, | ||
| 1815 | with a common difference given by the step. | ||
| 1816 | A negative step makes a decreasing sequence; | ||
| 1817 | a step equal to zero raises an error. | ||
| 1818 | The loop continues while the value is less than | ||
| 1819 | or equal to the limit | ||
| 1820 | (greater than or equal to for a negative step). | ||
| 1821 | If the initial value is already greater than the limit | ||
| 1822 | (or less than, if the step is negative), | ||
| 1823 | the body is not executed. | ||
| 1824 | |||
| 1825 | |||
| 1826 | <p> | ||
| 1827 | For integer loops, | ||
| 1828 | the control variable never wraps around; | ||
| 1829 | instead, the loop ends in case of an overflow. | ||
| 1830 | |||
| 1831 | |||
| 1832 | <p> | ||
| 1833 | You should not change the value of the control variable | ||
| 1834 | during the loop. | ||
| 1835 | If you need its value after the loop, | ||
| 1836 | assign it to another variable before exiting the loop. | ||
| 1837 | |||
| 1838 | |||
| 1839 | |||
| 1840 | |||
| 1841 | |||
| 1842 | <h4>The generic <b>for</b> loop</h4> | ||
| 1843 | |||
| 1844 | <p> | ||
| 1845 | The generic <b>for</b> statement works over functions, | ||
| 1846 | called <em>iterators</em>. | ||
| 1847 | On each iteration, the iterator function is called to produce a new value, | ||
| 1848 | stopping when this new value is <b>nil</b>. | ||
| 1849 | The generic <b>for</b> loop has the following syntax: | ||
| 1850 | |||
| 1851 | <pre> | ||
| 1852 | stat ::= <b>for</b> namelist <b>in</b> explist <b>do</b> block <b>end</b> | ||
| 1853 | namelist ::= Name {‘<b>,</b>’ Name} | ||
| 1854 | </pre><p> | ||
| 1855 | A <b>for</b> statement like | ||
| 1856 | |||
| 1857 | <pre> | ||
| 1858 | for <em>var_1</em>, ···, <em>var_n</em> in <em>explist</em> do <em>body</em> end | ||
| 1859 | </pre><p> | ||
| 1860 | works as follows. | ||
| 1861 | |||
| 1862 | |||
| 1863 | <p> | ||
| 1864 | The names <em>var_i</em> declare loop variables local to the loop body. | ||
| 1865 | The first of these variables is the <em>control variable</em>. | ||
| 1866 | |||
| 1867 | |||
| 1868 | <p> | ||
| 1869 | The loop starts by evaluating <em>explist</em> | ||
| 1870 | to produce four values: | ||
| 1871 | an <em>iterator function</em>, | ||
| 1872 | a <em>state</em>, | ||
| 1873 | an initial value for the control variable, | ||
| 1874 | and a <em>closing value</em>. | ||
| 1875 | |||
| 1876 | |||
| 1877 | <p> | ||
| 1878 | Then, at each iteration, | ||
| 1879 | Lua calls the iterator function with two arguments: | ||
| 1880 | the state and the control variable. | ||
| 1881 | The results from this call are then assigned to the loop variables, | ||
| 1882 | following the rules of multiple assignments (see <a href="#3.3.3">§3.3.3</a>). | ||
| 1883 | If the control variable becomes <b>nil</b>, | ||
| 1884 | the loop terminates. | ||
| 1885 | Otherwise, the body is executed and the loop goes | ||
| 1886 | to the next iteration. | ||
| 1887 | |||
| 1888 | |||
| 1889 | <p> | ||
| 1890 | The closing value behaves like a | ||
| 1891 | to-be-closed variable (see <a href="#3.3.8">§3.3.8</a>), | ||
| 1892 | which can be used to release resources when the loop ends. | ||
| 1893 | Otherwise, it does not interfere with the loop. | ||
| 1894 | |||
| 1895 | |||
| 1896 | <p> | ||
| 1897 | You should not change the value of the control variable | ||
| 1898 | during the loop. | ||
| 1899 | |||
| 1900 | |||
| 1901 | |||
| 1902 | |||
| 1903 | |||
| 1904 | |||
| 1905 | |||
| 1906 | <h3>3.3.6 – <a name="3.3.6">Function Calls as Statements</a></h3><p> | ||
| 1907 | To allow possible side-effects, | ||
| 1908 | function calls can be executed as statements: | ||
| 1909 | |||
| 1910 | <pre> | ||
| 1911 | stat ::= functioncall | ||
| 1912 | </pre><p> | ||
| 1913 | In this case, all returned values are thrown away. | ||
| 1914 | Function calls are explained in <a href="#3.4.10">§3.4.10</a>. | ||
| 1915 | |||
| 1916 | |||
| 1917 | |||
| 1918 | |||
| 1919 | |||
| 1920 | <h3>3.3.7 – <a name="3.3.7">Local Declarations</a></h3><p> | ||
| 1921 | Local variables can be declared anywhere inside a block. | ||
| 1922 | The declaration can include an initialization: | ||
| 1923 | |||
| 1924 | <pre> | ||
| 1925 | stat ::= <b>local</b> attnamelist [‘<b>=</b>’ explist] | ||
| 1926 | attnamelist ::= Name attrib {‘<b>,</b>’ Name attrib} | ||
| 1927 | </pre><p> | ||
| 1928 | If present, an initial assignment has the same semantics | ||
| 1929 | of a multiple assignment (see <a href="#3.3.3">§3.3.3</a>). | ||
| 1930 | Otherwise, all variables are initialized with <b>nil</b>. | ||
| 1931 | |||
| 1932 | |||
| 1933 | <p> | ||
| 1934 | Each variable name may be postfixed by an attribute | ||
| 1935 | (a name between angle brackets): | ||
| 1936 | |||
| 1937 | <pre> | ||
| 1938 | attrib ::= [‘<b><</b>’ Name ‘<b>></b>’] | ||
| 1939 | </pre><p> | ||
| 1940 | There are two possible attributes: | ||
| 1941 | <code>const</code>, which declares a constant variable, | ||
| 1942 | that is, a variable that cannot be assigned to | ||
| 1943 | after its initialization; | ||
| 1944 | and <code>close</code>, which declares a to-be-closed variable (see <a href="#3.3.8">§3.3.8</a>). | ||
| 1945 | A list of variables can contain at most one to-be-closed variable. | ||
| 1946 | |||
| 1947 | |||
| 1948 | <p> | ||
| 1949 | A chunk is also a block (see <a href="#3.3.2">§3.3.2</a>), | ||
| 1950 | and so local variables can be declared in a chunk outside any explicit block. | ||
| 1951 | |||
| 1952 | |||
| 1953 | <p> | ||
| 1954 | The visibility rules for local variables are explained in <a href="#3.5">§3.5</a>. | ||
| 1955 | |||
| 1956 | |||
| 1957 | |||
| 1958 | |||
| 1959 | |||
| 1960 | <h3>3.3.8 – <a name="3.3.8">To-be-closed Variables</a></h3> | ||
| 1961 | |||
| 1962 | <p> | ||
| 1963 | A to-be-closed variable behaves like a constant local variable, | ||
| 1964 | except that its value is <em>closed</em> whenever the variable | ||
| 1965 | goes out of scope, including normal block termination, | ||
| 1966 | exiting its block by <b>break</b>/<b>goto</b>/<b>return</b>, | ||
| 1967 | or exiting by an error. | ||
| 1968 | |||
| 1969 | |||
| 1970 | <p> | ||
| 1971 | Here, to <em>close</em> a value means | ||
| 1972 | to call its <code>__close</code> metamethod. | ||
| 1973 | When calling the metamethod, | ||
| 1974 | the value itself is passed as the first argument | ||
| 1975 | and the error object that caused the exit (if any) | ||
| 1976 | is passed as a second argument; | ||
| 1977 | if there was no error, the second argument is <b>nil</b>. | ||
| 1978 | |||
| 1979 | |||
| 1980 | <p> | ||
| 1981 | The value assigned to a to-be-closed variable | ||
| 1982 | must have a <code>__close</code> metamethod | ||
| 1983 | or be a false value. | ||
| 1984 | (<b>nil</b> and <b>false</b> are ignored as to-be-closed values.) | ||
| 1985 | |||
| 1986 | |||
| 1987 | <p> | ||
| 1988 | If several to-be-closed variables go out of scope at the same event, | ||
| 1989 | they are closed in the reverse order that they were declared. | ||
| 1990 | |||
| 1991 | |||
| 1992 | <p> | ||
| 1993 | If there is any error while running a closing method, | ||
| 1994 | that error is handled like an error in the regular code | ||
| 1995 | where the variable was defined. | ||
| 1996 | After an error, | ||
| 1997 | the other pending closing methods will still be called. | ||
| 1998 | |||
| 1999 | |||
| 2000 | <p> | ||
| 2001 | If a coroutine yields and is never resumed again, | ||
| 2002 | some variables may never go out of scope, | ||
| 2003 | and therefore they will never be closed. | ||
| 2004 | (These variables are the ones created inside the coroutine | ||
| 2005 | and in scope at the point where the coroutine yielded.) | ||
| 2006 | Similarly, if a coroutine ends with an error, | ||
| 2007 | it does not unwind its stack, | ||
| 2008 | so it does not close any variable. | ||
| 2009 | In both cases, | ||
| 2010 | you can either use finalizers | ||
| 2011 | or call <a href="#pdf-coroutine.close"><code>coroutine.close</code></a> to close the variables. | ||
| 2012 | However, if the coroutine was created | ||
| 2013 | through <a href="#pdf-coroutine.wrap"><code>coroutine.wrap</code></a>, | ||
| 2014 | then its corresponding function will close the coroutine | ||
| 2015 | in case of errors. | ||
| 2016 | |||
| 2017 | |||
| 2018 | |||
| 2019 | |||
| 2020 | |||
| 2021 | |||
| 2022 | |||
| 2023 | <h2>3.4 – <a name="3.4">Expressions</a></h2> | ||
| 2024 | |||
| 2025 | |||
| 2026 | |||
| 2027 | <p> | ||
| 2028 | The basic expressions in Lua are the following: | ||
| 2029 | |||
| 2030 | <pre> | ||
| 2031 | exp ::= prefixexp | ||
| 2032 | exp ::= <b>nil</b> | <b>false</b> | <b>true</b> | ||
| 2033 | exp ::= Numeral | ||
| 2034 | exp ::= LiteralString | ||
| 2035 | exp ::= functiondef | ||
| 2036 | exp ::= tableconstructor | ||
| 2037 | exp ::= ‘<b>...</b>’ | ||
| 2038 | exp ::= exp binop exp | ||
| 2039 | exp ::= unop exp | ||
| 2040 | prefixexp ::= var | functioncall | ‘<b>(</b>’ exp ‘<b>)</b>’ | ||
| 2041 | </pre> | ||
| 2042 | |||
| 2043 | <p> | ||
| 2044 | Numerals and literal strings are explained in <a href="#3.1">§3.1</a>; | ||
| 2045 | variables are explained in <a href="#3.2">§3.2</a>; | ||
| 2046 | function definitions are explained in <a href="#3.4.11">§3.4.11</a>; | ||
| 2047 | function calls are explained in <a href="#3.4.10">§3.4.10</a>; | ||
| 2048 | table constructors are explained in <a href="#3.4.9">§3.4.9</a>. | ||
| 2049 | Vararg expressions, | ||
| 2050 | denoted by three dots ('<code>...</code>'), can only be used when | ||
| 2051 | directly inside a variadic function; | ||
| 2052 | they are explained in <a href="#3.4.11">§3.4.11</a>. | ||
| 2053 | |||
| 2054 | |||
| 2055 | <p> | ||
| 2056 | Binary operators comprise arithmetic operators (see <a href="#3.4.1">§3.4.1</a>), | ||
| 2057 | bitwise operators (see <a href="#3.4.2">§3.4.2</a>), | ||
| 2058 | relational operators (see <a href="#3.4.4">§3.4.4</a>), logical operators (see <a href="#3.4.5">§3.4.5</a>), | ||
| 2059 | and the concatenation operator (see <a href="#3.4.6">§3.4.6</a>). | ||
| 2060 | Unary operators comprise the unary minus (see <a href="#3.4.1">§3.4.1</a>), | ||
| 2061 | the unary bitwise NOT (see <a href="#3.4.2">§3.4.2</a>), | ||
| 2062 | the unary logical <b>not</b> (see <a href="#3.4.5">§3.4.5</a>), | ||
| 2063 | and the unary <em>length operator</em> (see <a href="#3.4.7">§3.4.7</a>). | ||
| 2064 | |||
| 2065 | |||
| 2066 | |||
| 2067 | |||
| 2068 | |||
| 2069 | <h3>3.4.1 – <a name="3.4.1">Arithmetic Operators</a></h3><p> | ||
| 2070 | Lua supports the following arithmetic operators: | ||
| 2071 | |||
| 2072 | <ul> | ||
| 2073 | <li><b><code>+</code>: </b>addition</li> | ||
| 2074 | <li><b><code>-</code>: </b>subtraction</li> | ||
| 2075 | <li><b><code>*</code>: </b>multiplication</li> | ||
| 2076 | <li><b><code>/</code>: </b>float division</li> | ||
| 2077 | <li><b><code>//</code>: </b>floor division</li> | ||
| 2078 | <li><b><code>%</code>: </b>modulo</li> | ||
| 2079 | <li><b><code>^</code>: </b>exponentiation</li> | ||
| 2080 | <li><b><code>-</code>: </b>unary minus</li> | ||
| 2081 | </ul> | ||
| 2082 | |||
| 2083 | <p> | ||
| 2084 | With the exception of exponentiation and float division, | ||
| 2085 | the arithmetic operators work as follows: | ||
| 2086 | If both operands are integers, | ||
| 2087 | the operation is performed over integers and the result is an integer. | ||
| 2088 | Otherwise, if both operands are numbers, | ||
| 2089 | then they are converted to floats, | ||
| 2090 | the operation is performed following the machine's rules | ||
| 2091 | for floating-point arithmetic | ||
| 2092 | (usually the IEEE 754 standard), | ||
| 2093 | and the result is a float. | ||
| 2094 | (The string library coerces strings to numbers in | ||
| 2095 | arithmetic operations; see <a href="#3.4.3">§3.4.3</a> for details.) | ||
| 2096 | |||
| 2097 | |||
| 2098 | <p> | ||
| 2099 | Exponentiation and float division (<code>/</code>) | ||
| 2100 | always convert their operands to floats | ||
| 2101 | and the result is always a float. | ||
| 2102 | Exponentiation uses the ISO C function <code>pow</code>, | ||
| 2103 | so that it works for non-integer exponents too. | ||
| 2104 | |||
| 2105 | |||
| 2106 | <p> | ||
| 2107 | Floor division (<code>//</code>) is a division | ||
| 2108 | that rounds the quotient towards minus infinity, | ||
| 2109 | resulting in the floor of the division of its operands. | ||
| 2110 | |||
| 2111 | |||
| 2112 | <p> | ||
| 2113 | Modulo is defined as the remainder of a division | ||
| 2114 | that rounds the quotient towards minus infinity (floor division). | ||
| 2115 | |||
| 2116 | |||
| 2117 | <p> | ||
| 2118 | In case of overflows in integer arithmetic, | ||
| 2119 | all operations <em>wrap around</em>. | ||
| 2120 | |||
| 2121 | |||
| 2122 | |||
| 2123 | <h3>3.4.2 – <a name="3.4.2">Bitwise Operators</a></h3><p> | ||
| 2124 | Lua supports the following bitwise operators: | ||
| 2125 | |||
| 2126 | <ul> | ||
| 2127 | <li><b><code>&</code>: </b>bitwise AND</li> | ||
| 2128 | <li><b><code>|</code>: </b>bitwise OR</li> | ||
| 2129 | <li><b><code>~</code>: </b>bitwise exclusive OR</li> | ||
| 2130 | <li><b><code>>></code>: </b>right shift</li> | ||
| 2131 | <li><b><code><<</code>: </b>left shift</li> | ||
| 2132 | <li><b><code>~</code>: </b>unary bitwise NOT</li> | ||
| 2133 | </ul> | ||
| 2134 | |||
| 2135 | <p> | ||
| 2136 | All bitwise operations convert its operands to integers | ||
| 2137 | (see <a href="#3.4.3">§3.4.3</a>), | ||
| 2138 | operate on all bits of those integers, | ||
| 2139 | and result in an integer. | ||
| 2140 | |||
| 2141 | |||
| 2142 | <p> | ||
| 2143 | Both right and left shifts fill the vacant bits with zeros. | ||
| 2144 | Negative displacements shift to the other direction; | ||
| 2145 | displacements with absolute values equal to or higher than | ||
| 2146 | the number of bits in an integer | ||
| 2147 | result in zero (as all bits are shifted out). | ||
| 2148 | |||
| 2149 | |||
| 2150 | |||
| 2151 | |||
| 2152 | |||
| 2153 | <h3>3.4.3 – <a name="3.4.3">Coercions and Conversions</a></h3><p> | ||
| 2154 | Lua provides some automatic conversions between some | ||
| 2155 | types and representations at run time. | ||
| 2156 | Bitwise operators always convert float operands to integers. | ||
| 2157 | Exponentiation and float division | ||
| 2158 | always convert integer operands to floats. | ||
| 2159 | All other arithmetic operations applied to mixed numbers | ||
| 2160 | (integers and floats) convert the integer operand to a float. | ||
| 2161 | The C API also converts both integers to floats and | ||
| 2162 | floats to integers, as needed. | ||
| 2163 | Moreover, string concatenation accepts numbers as arguments, | ||
| 2164 | besides strings. | ||
| 2165 | |||
| 2166 | |||
| 2167 | <p> | ||
| 2168 | In a conversion from integer to float, | ||
| 2169 | if the integer value has an exact representation as a float, | ||
| 2170 | that is the result. | ||
| 2171 | Otherwise, | ||
| 2172 | the conversion gets the nearest higher or | ||
| 2173 | the nearest lower representable value. | ||
| 2174 | This kind of conversion never fails. | ||
| 2175 | |||
| 2176 | |||
| 2177 | <p> | ||
| 2178 | The conversion from float to integer | ||
| 2179 | checks whether the float has an exact representation as an integer | ||
| 2180 | (that is, the float has an integral value and | ||
| 2181 | it is in the range of integer representation). | ||
| 2182 | If it does, that representation is the result. | ||
| 2183 | Otherwise, the conversion fails. | ||
| 2184 | |||
| 2185 | |||
| 2186 | <p> | ||
| 2187 | Several places in Lua coerce strings to numbers when necessary. | ||
| 2188 | In particular, | ||
| 2189 | the string library sets metamethods that try to coerce | ||
| 2190 | strings to numbers in all arithmetic operations. | ||
| 2191 | If the conversion fails, | ||
| 2192 | the library calls the metamethod of the other operand | ||
| 2193 | (if present) or it raises an error. | ||
| 2194 | Note that bitwise operators do not do this coercion. | ||
| 2195 | |||
| 2196 | |||
| 2197 | <p> | ||
| 2198 | It is always a good practice not to rely on the | ||
| 2199 | implicit coercions from strings to numbers, | ||
| 2200 | as they are not always applied; | ||
| 2201 | in particular, <code>"1"==1</code> is false and <code>"1"<1</code> raises an error | ||
| 2202 | (see <a href="#3.4.4">§3.4.4</a>). | ||
| 2203 | These coercions exist mainly for compatibility and may be removed | ||
| 2204 | in future versions of the language. | ||
| 2205 | |||
| 2206 | |||
| 2207 | <p> | ||
| 2208 | A string is converted to an integer or a float | ||
| 2209 | following its syntax and the rules of the Lua lexer. | ||
| 2210 | The string may have also leading and trailing whitespaces and a sign. | ||
| 2211 | All conversions from strings to numbers | ||
| 2212 | accept both a dot and the current locale mark | ||
| 2213 | as the radix character. | ||
| 2214 | (The Lua lexer, however, accepts only a dot.) | ||
| 2215 | If the string is not a valid numeral, | ||
| 2216 | the conversion fails. | ||
| 2217 | If necessary, the result of this first step is then converted | ||
| 2218 | to a specific number subtype following the previous rules | ||
| 2219 | for conversions between floats and integers. | ||
| 2220 | |||
| 2221 | |||
| 2222 | <p> | ||
| 2223 | The conversion from numbers to strings uses a | ||
| 2224 | non-specified human-readable format. | ||
| 2225 | To convert numbers to strings in any specific way, | ||
| 2226 | use the function <a href="#pdf-string.format"><code>string.format</code></a>. | ||
| 2227 | |||
| 2228 | |||
| 2229 | |||
| 2230 | |||
| 2231 | |||
| 2232 | <h3>3.4.4 – <a name="3.4.4">Relational Operators</a></h3><p> | ||
| 2233 | Lua supports the following relational operators: | ||
| 2234 | |||
| 2235 | <ul> | ||
| 2236 | <li><b><code>==</code>: </b>equality</li> | ||
| 2237 | <li><b><code>~=</code>: </b>inequality</li> | ||
| 2238 | <li><b><code><</code>: </b>less than</li> | ||
| 2239 | <li><b><code>></code>: </b>greater than</li> | ||
| 2240 | <li><b><code><=</code>: </b>less or equal</li> | ||
| 2241 | <li><b><code>>=</code>: </b>greater or equal</li> | ||
| 2242 | </ul><p> | ||
| 2243 | These operators always result in <b>false</b> or <b>true</b>. | ||
| 2244 | |||
| 2245 | |||
| 2246 | <p> | ||
| 2247 | Equality (<code>==</code>) first compares the type of its operands. | ||
| 2248 | If the types are different, then the result is <b>false</b>. | ||
| 2249 | Otherwise, the values of the operands are compared. | ||
| 2250 | Strings are equal if they have the same byte content. | ||
| 2251 | Numbers are equal if they denote the same mathematical value. | ||
| 2252 | |||
| 2253 | |||
| 2254 | <p> | ||
| 2255 | Tables, userdata, and threads | ||
| 2256 | are compared by reference: | ||
| 2257 | two objects are considered equal only if they are the same object. | ||
| 2258 | Every time you create a new object | ||
| 2259 | (a table, a userdata, or a thread), | ||
| 2260 | this new object is different from any previously existing object. | ||
| 2261 | A function is always equal to itself. | ||
| 2262 | Functions with any detectable difference | ||
| 2263 | (different behavior, different definition) are always different. | ||
| 2264 | Functions created at different times but with no detectable differences | ||
| 2265 | may be classified as equal or not | ||
| 2266 | (depending on internal caching details). | ||
| 2267 | |||
| 2268 | |||
| 2269 | <p> | ||
| 2270 | You can change the way that Lua compares tables and userdata | ||
| 2271 | by using the <code>__eq</code> metamethod (see <a href="#2.4">§2.4</a>). | ||
| 2272 | |||
| 2273 | |||
| 2274 | <p> | ||
| 2275 | Equality comparisons do not convert strings to numbers | ||
| 2276 | or vice versa. | ||
| 2277 | Thus, <code>"0"==0</code> evaluates to <b>false</b>, | ||
| 2278 | and <code>t[0]</code> and <code>t["0"]</code> denote different | ||
| 2279 | entries in a table. | ||
| 2280 | |||
| 2281 | |||
| 2282 | <p> | ||
| 2283 | The operator <code>~=</code> is exactly the negation of equality (<code>==</code>). | ||
| 2284 | |||
| 2285 | |||
| 2286 | <p> | ||
| 2287 | The order operators work as follows. | ||
| 2288 | If both arguments are numbers, | ||
| 2289 | then they are compared according to their mathematical values, | ||
| 2290 | regardless of their subtypes. | ||
| 2291 | Otherwise, if both arguments are strings, | ||
| 2292 | then their values are compared according to the current locale. | ||
| 2293 | Otherwise, Lua tries to call the <code>__lt</code> or the <code>__le</code> | ||
| 2294 | metamethod (see <a href="#2.4">§2.4</a>). | ||
| 2295 | A comparison <code>a > b</code> is translated to <code>b < a</code> | ||
| 2296 | and <code>a >= b</code> is translated to <code>b <= a</code>. | ||
| 2297 | |||
| 2298 | |||
| 2299 | <p> | ||
| 2300 | Following the IEEE 754 standard, | ||
| 2301 | the special value NaN is considered neither less than, | ||
| 2302 | nor equal to, nor greater than any value, including itself. | ||
| 2303 | |||
| 2304 | |||
| 2305 | |||
| 2306 | |||
| 2307 | |||
| 2308 | <h3>3.4.5 – <a name="3.4.5">Logical Operators</a></h3><p> | ||
| 2309 | The logical operators in Lua are | ||
| 2310 | <b>and</b>, <b>or</b>, and <b>not</b>. | ||
| 2311 | Like the control structures (see <a href="#3.3.4">§3.3.4</a>), | ||
| 2312 | all logical operators consider both <b>false</b> and <b>nil</b> as false | ||
| 2313 | and anything else as true. | ||
| 2314 | |||
| 2315 | |||
| 2316 | <p> | ||
| 2317 | The negation operator <b>not</b> always returns <b>false</b> or <b>true</b>. | ||
| 2318 | The conjunction operator <b>and</b> returns its first argument | ||
| 2319 | if this value is <b>false</b> or <b>nil</b>; | ||
| 2320 | otherwise, <b>and</b> returns its second argument. | ||
| 2321 | The disjunction operator <b>or</b> returns its first argument | ||
| 2322 | if this value is different from <b>nil</b> and <b>false</b>; | ||
| 2323 | otherwise, <b>or</b> returns its second argument. | ||
| 2324 | Both <b>and</b> and <b>or</b> use short-circuit evaluation; | ||
| 2325 | that is, | ||
| 2326 | the second operand is evaluated only if necessary. | ||
| 2327 | Here are some examples: | ||
| 2328 | |||
| 2329 | <pre> | ||
| 2330 | 10 or 20 --> 10 | ||
| 2331 | 10 or error() --> 10 | ||
| 2332 | nil or "a" --> "a" | ||
| 2333 | nil and 10 --> nil | ||
| 2334 | false and error() --> false | ||
| 2335 | false and nil --> false | ||
| 2336 | false or nil --> nil | ||
| 2337 | 10 and 20 --> 20 | ||
| 2338 | </pre> | ||
| 2339 | |||
| 2340 | |||
| 2341 | |||
| 2342 | |||
| 2343 | <h3>3.4.6 – <a name="3.4.6">Concatenation</a></h3><p> | ||
| 2344 | The string concatenation operator in Lua is | ||
| 2345 | denoted by two dots ('<code>..</code>'). | ||
| 2346 | If both operands are strings or numbers, | ||
| 2347 | then the numbers are converted to strings | ||
| 2348 | in a non-specified format (see <a href="#3.4.3">§3.4.3</a>). | ||
| 2349 | Otherwise, the <code>__concat</code> metamethod is called (see <a href="#2.4">§2.4</a>). | ||
| 2350 | |||
| 2351 | |||
| 2352 | |||
| 2353 | |||
| 2354 | |||
| 2355 | <h3>3.4.7 – <a name="3.4.7">The Length Operator</a></h3> | ||
| 2356 | |||
| 2357 | <p> | ||
| 2358 | The length operator is denoted by the unary prefix operator <code>#</code>. | ||
| 2359 | |||
| 2360 | |||
| 2361 | <p> | ||
| 2362 | The length of a string is its number of bytes. | ||
| 2363 | (That is the usual meaning of string length when each | ||
| 2364 | character is one byte.) | ||
| 2365 | |||
| 2366 | |||
| 2367 | <p> | ||
| 2368 | The length operator applied on a table | ||
| 2369 | returns a border in that table. | ||
| 2370 | A <em>border</em> in a table <code>t</code> is any non-negative integer | ||
| 2371 | that satisfies the following condition: | ||
| 2372 | |||
| 2373 | <pre> | ||
| 2374 | (border == 0 or t[border] ~= nil) and | ||
| 2375 | (t[border + 1] == nil or border == math.maxinteger) | ||
| 2376 | </pre><p> | ||
| 2377 | In words, | ||
| 2378 | a border is any positive integer index present in the table | ||
| 2379 | that is followed by an absent index, | ||
| 2380 | plus two limit cases: | ||
| 2381 | zero, when index 1 is absent; | ||
| 2382 | and the maximum value for an integer, when that index is present. | ||
| 2383 | Note that keys that are not positive integers | ||
| 2384 | do not interfere with borders. | ||
| 2385 | |||
| 2386 | |||
| 2387 | <p> | ||
| 2388 | A table with exactly one border is called a <em>sequence</em>. | ||
| 2389 | For instance, the table <code>{10, 20, 30, 40, 50}</code> is a sequence, | ||
| 2390 | as it has only one border (5). | ||
| 2391 | The table <code>{10, 20, 30, nil, 50}</code> has two borders (3 and 5), | ||
| 2392 | and therefore it is not a sequence. | ||
| 2393 | (The <b>nil</b> at index 4 is called a <em>hole</em>.) | ||
| 2394 | The table <code>{nil, 20, 30, nil, nil, 60, nil}</code> | ||
| 2395 | has three borders (0, 3, and 6), | ||
| 2396 | so it is not a sequence, too. | ||
| 2397 | The table <code>{}</code> is a sequence with border 0. | ||
| 2398 | |||
| 2399 | |||
| 2400 | <p> | ||
| 2401 | When <code>t</code> is a sequence, | ||
| 2402 | <code>#t</code> returns its only border, | ||
| 2403 | which corresponds to the intuitive notion of the length of the sequence. | ||
| 2404 | When <code>t</code> is not a sequence, | ||
| 2405 | <code>#t</code> can return any of its borders. | ||
| 2406 | (The exact one depends on details of | ||
| 2407 | the internal representation of the table, | ||
| 2408 | which in turn can depend on how the table was populated and | ||
| 2409 | the memory addresses of its non-numeric keys.) | ||
| 2410 | |||
| 2411 | |||
| 2412 | <p> | ||
| 2413 | The computation of the length of a table | ||
| 2414 | has a guaranteed worst time of <em>O(log n)</em>, | ||
| 2415 | where <em>n</em> is the largest integer key in the table. | ||
| 2416 | |||
| 2417 | |||
| 2418 | <p> | ||
| 2419 | A program can modify the behavior of the length operator for | ||
| 2420 | any value but strings through the <code>__len</code> metamethod (see <a href="#2.4">§2.4</a>). | ||
| 2421 | |||
| 2422 | |||
| 2423 | |||
| 2424 | |||
| 2425 | |||
| 2426 | <h3>3.4.8 – <a name="3.4.8">Precedence</a></h3><p> | ||
| 2427 | Operator precedence in Lua follows the table below, | ||
| 2428 | from lower to higher priority: | ||
| 2429 | |||
| 2430 | <pre> | ||
| 2431 | or | ||
| 2432 | and | ||
| 2433 | < > <= >= ~= == | ||
| 2434 | | | ||
| 2435 | ~ | ||
| 2436 | & | ||
| 2437 | << >> | ||
| 2438 | .. | ||
| 2439 | + - | ||
| 2440 | * / // % | ||
| 2441 | unary operators (not # - ~) | ||
| 2442 | ^ | ||
| 2443 | </pre><p> | ||
| 2444 | As usual, | ||
| 2445 | you can use parentheses to change the precedences of an expression. | ||
| 2446 | The concatenation ('<code>..</code>') and exponentiation ('<code>^</code>') | ||
| 2447 | operators are right associative. | ||
| 2448 | All other binary operators are left associative. | ||
| 2449 | |||
| 2450 | |||
| 2451 | |||
| 2452 | |||
| 2453 | |||
| 2454 | <h3>3.4.9 – <a name="3.4.9">Table Constructors</a></h3><p> | ||
| 2455 | Table constructors are expressions that create tables. | ||
| 2456 | Every time a constructor is evaluated, a new table is created. | ||
| 2457 | A constructor can be used to create an empty table | ||
| 2458 | or to create a table and initialize some of its fields. | ||
| 2459 | The general syntax for constructors is | ||
| 2460 | |||
| 2461 | <pre> | ||
| 2462 | tableconstructor ::= ‘<b>{</b>’ [fieldlist] ‘<b>}</b>’ | ||
| 2463 | fieldlist ::= field {fieldsep field} [fieldsep] | ||
| 2464 | field ::= ‘<b>[</b>’ exp ‘<b>]</b>’ ‘<b>=</b>’ exp | Name ‘<b>=</b>’ exp | exp | ||
| 2465 | fieldsep ::= ‘<b>,</b>’ | ‘<b>;</b>’ | ||
| 2466 | </pre> | ||
| 2467 | |||
| 2468 | <p> | ||
| 2469 | Each field of the form <code>[exp1] = exp2</code> adds to the new table an entry | ||
| 2470 | with key <code>exp1</code> and value <code>exp2</code>. | ||
| 2471 | A field of the form <code>name = exp</code> is equivalent to | ||
| 2472 | <code>["name"] = exp</code>. | ||
| 2473 | Fields of the form <code>exp</code> are equivalent to | ||
| 2474 | <code>[i] = exp</code>, where <code>i</code> are consecutive integers | ||
| 2475 | starting with 1; | ||
| 2476 | fields in the other formats do not affect this counting. | ||
| 2477 | For example, | ||
| 2478 | |||
| 2479 | <pre> | ||
| 2480 | a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 } | ||
| 2481 | </pre><p> | ||
| 2482 | is equivalent to | ||
| 2483 | |||
| 2484 | <pre> | ||
| 2485 | do | ||
| 2486 | local t = {} | ||
| 2487 | t[f(1)] = g | ||
| 2488 | t[1] = "x" -- 1st exp | ||
| 2489 | t[2] = "y" -- 2nd exp | ||
| 2490 | t.x = 1 -- t["x"] = 1 | ||
| 2491 | t[3] = f(x) -- 3rd exp | ||
| 2492 | t[30] = 23 | ||
| 2493 | t[4] = 45 -- 4th exp | ||
| 2494 | a = t | ||
| 2495 | end | ||
| 2496 | </pre> | ||
| 2497 | |||
| 2498 | <p> | ||
| 2499 | The order of the assignments in a constructor is undefined. | ||
| 2500 | (This order would be relevant only when there are repeated keys.) | ||
| 2501 | |||
| 2502 | |||
| 2503 | <p> | ||
| 2504 | If the last field in the list has the form <code>exp</code> | ||
| 2505 | and the expression is a multires expression, | ||
| 2506 | then all values returned by this expression enter the list consecutively | ||
| 2507 | (see <a href="#3.4.12">§3.4.12</a>). | ||
| 2508 | |||
| 2509 | |||
| 2510 | <p> | ||
| 2511 | The field list can have an optional trailing separator, | ||
| 2512 | as a convenience for machine-generated code. | ||
| 2513 | |||
| 2514 | |||
| 2515 | |||
| 2516 | |||
| 2517 | |||
| 2518 | <h3>3.4.10 – <a name="3.4.10">Function Calls</a></h3><p> | ||
| 2519 | A function call in Lua has the following syntax: | ||
| 2520 | |||
| 2521 | <pre> | ||
| 2522 | functioncall ::= prefixexp args | ||
| 2523 | </pre><p> | ||
| 2524 | In a function call, | ||
| 2525 | first prefixexp and args are evaluated. | ||
| 2526 | If the value of prefixexp has type <em>function</em>, | ||
| 2527 | then this function is called | ||
| 2528 | with the given arguments. | ||
| 2529 | Otherwise, if present, | ||
| 2530 | the prefixexp <code>__call</code> metamethod is called: | ||
| 2531 | its first argument is the value of prefixexp, | ||
| 2532 | followed by the original call arguments | ||
| 2533 | (see <a href="#2.4">§2.4</a>). | ||
| 2534 | |||
| 2535 | |||
| 2536 | <p> | ||
| 2537 | The form | ||
| 2538 | |||
| 2539 | <pre> | ||
| 2540 | functioncall ::= prefixexp ‘<b>:</b>’ Name args | ||
| 2541 | </pre><p> | ||
| 2542 | can be used to emulate methods. | ||
| 2543 | A call <code>v:name(<em>args</em>)</code> | ||
| 2544 | is syntactic sugar for <code>v.name(v,<em>args</em>)</code>, | ||
| 2545 | except that <code>v</code> is evaluated only once. | ||
| 2546 | |||
| 2547 | |||
| 2548 | <p> | ||
| 2549 | Arguments have the following syntax: | ||
| 2550 | |||
| 2551 | <pre> | ||
| 2552 | args ::= ‘<b>(</b>’ [explist] ‘<b>)</b>’ | ||
| 2553 | args ::= tableconstructor | ||
| 2554 | args ::= LiteralString | ||
| 2555 | </pre><p> | ||
| 2556 | All argument expressions are evaluated before the call. | ||
| 2557 | A call of the form <code>f{<em>fields</em>}</code> is | ||
| 2558 | syntactic sugar for <code>f({<em>fields</em>})</code>; | ||
| 2559 | that is, the argument list is a single new table. | ||
| 2560 | A call of the form <code>f'<em>string</em>'</code> | ||
| 2561 | (or <code>f"<em>string</em>"</code> or <code>f[[<em>string</em>]]</code>) | ||
| 2562 | is syntactic sugar for <code>f('<em>string</em>')</code>; | ||
| 2563 | that is, the argument list is a single literal string. | ||
| 2564 | |||
| 2565 | |||
| 2566 | <p> | ||
| 2567 | A call of the form <code>return <em>functioncall</em></code> not in the | ||
| 2568 | scope of a to-be-closed variable is called a <em>tail call</em>. | ||
| 2569 | Lua implements <em>proper tail calls</em> | ||
| 2570 | (or <em>proper tail recursion</em>): | ||
| 2571 | In a tail call, | ||
| 2572 | the called function reuses the stack entry of the calling function. | ||
| 2573 | Therefore, there is no limit on the number of nested tail calls that | ||
| 2574 | a program can execute. | ||
| 2575 | However, a tail call erases any debug information about the | ||
| 2576 | calling function. | ||
| 2577 | Note that a tail call only happens with a particular syntax, | ||
| 2578 | where the <b>return</b> has one single function call as argument, | ||
| 2579 | and it is outside the scope of any to-be-closed variable. | ||
| 2580 | This syntax makes the calling function return exactly | ||
| 2581 | the returns of the called function, | ||
| 2582 | without any intervening action. | ||
| 2583 | So, none of the following examples are tail calls: | ||
| 2584 | |||
| 2585 | <pre> | ||
| 2586 | return (f(x)) -- results adjusted to 1 | ||
| 2587 | return 2 * f(x) -- result multiplied by 2 | ||
| 2588 | return x, f(x) -- additional results | ||
| 2589 | f(x); return -- results discarded | ||
| 2590 | return x or f(x) -- results adjusted to 1 | ||
| 2591 | </pre> | ||
| 2592 | |||
| 2593 | |||
| 2594 | |||
| 2595 | |||
| 2596 | <h3>3.4.11 – <a name="3.4.11">Function Definitions</a></h3> | ||
| 2597 | |||
| 2598 | <p> | ||
| 2599 | The syntax for function definition is | ||
| 2600 | |||
| 2601 | <pre> | ||
| 2602 | functiondef ::= <b>function</b> funcbody | ||
| 2603 | funcbody ::= ‘<b>(</b>’ [parlist] ‘<b>)</b>’ block <b>end</b> | ||
| 2604 | </pre> | ||
| 2605 | |||
| 2606 | <p> | ||
| 2607 | The following syntactic sugar simplifies function definitions: | ||
| 2608 | |||
| 2609 | <pre> | ||
| 2610 | stat ::= <b>function</b> funcname funcbody | ||
| 2611 | stat ::= <b>local</b> <b>function</b> Name funcbody | ||
| 2612 | funcname ::= Name {‘<b>.</b>’ Name} [‘<b>:</b>’ Name] | ||
| 2613 | </pre><p> | ||
| 2614 | The statement | ||
| 2615 | |||
| 2616 | <pre> | ||
| 2617 | function f () <em>body</em> end | ||
| 2618 | </pre><p> | ||
| 2619 | translates to | ||
| 2620 | |||
| 2621 | <pre> | ||
| 2622 | f = function () <em>body</em> end | ||
| 2623 | </pre><p> | ||
| 2624 | The statement | ||
| 2625 | |||
| 2626 | <pre> | ||
| 2627 | function t.a.b.c.f () <em>body</em> end | ||
| 2628 | </pre><p> | ||
| 2629 | translates to | ||
| 2630 | |||
| 2631 | <pre> | ||
| 2632 | t.a.b.c.f = function () <em>body</em> end | ||
| 2633 | </pre><p> | ||
| 2634 | The statement | ||
| 2635 | |||
| 2636 | <pre> | ||
| 2637 | local function f () <em>body</em> end | ||
| 2638 | </pre><p> | ||
| 2639 | translates to | ||
| 2640 | |||
| 2641 | <pre> | ||
| 2642 | local f; f = function () <em>body</em> end | ||
| 2643 | </pre><p> | ||
| 2644 | not to | ||
| 2645 | |||
| 2646 | <pre> | ||
| 2647 | local f = function () <em>body</em> end | ||
| 2648 | </pre><p> | ||
| 2649 | (This only makes a difference when the body of the function | ||
| 2650 | contains references to <code>f</code>.) | ||
| 2651 | |||
| 2652 | |||
| 2653 | <p> | ||
| 2654 | A function definition is an executable expression, | ||
| 2655 | whose value has type <em>function</em>. | ||
| 2656 | When Lua precompiles a chunk, | ||
| 2657 | all its function bodies are precompiled too, | ||
| 2658 | but they are not created yet. | ||
| 2659 | Then, whenever Lua executes the function definition, | ||
| 2660 | the function is <em>instantiated</em> (or <em>closed</em>). | ||
| 2661 | This function instance, or <em>closure</em>, | ||
| 2662 | is the final value of the expression. | ||
| 2663 | |||
| 2664 | |||
| 2665 | <p> | ||
| 2666 | Parameters act as local variables that are | ||
| 2667 | initialized with the argument values: | ||
| 2668 | |||
| 2669 | <pre> | ||
| 2670 | parlist ::= namelist [‘<b>,</b>’ ‘<b>...</b>’] | ‘<b>...</b>’ | ||
| 2671 | </pre><p> | ||
| 2672 | When a Lua function is called, | ||
| 2673 | it adjusts its list of arguments to | ||
| 2674 | the length of its list of parameters (see <a href="#3.4.12">§3.4.12</a>), | ||
| 2675 | unless the function is a <em>variadic function</em>, | ||
| 2676 | which is indicated by three dots ('<code>...</code>') | ||
| 2677 | at the end of its parameter list. | ||
| 2678 | A variadic function does not adjust its argument list; | ||
| 2679 | instead, it collects all extra arguments and supplies them | ||
| 2680 | to the function through a <em>vararg expression</em>, | ||
| 2681 | which is also written as three dots. | ||
| 2682 | The value of this expression is a list of all actual extra arguments, | ||
| 2683 | similar to a function with multiple results (see <a href="#3.4.12">§3.4.12</a>). | ||
| 2684 | |||
| 2685 | |||
| 2686 | <p> | ||
| 2687 | As an example, consider the following definitions: | ||
| 2688 | |||
| 2689 | <pre> | ||
| 2690 | function f(a, b) end | ||
| 2691 | function g(a, b, ...) end | ||
| 2692 | function r() return 1,2,3 end | ||
| 2693 | </pre><p> | ||
| 2694 | Then, we have the following mapping from arguments to parameters and | ||
| 2695 | to the vararg expression: | ||
| 2696 | |||
| 2697 | <pre> | ||
| 2698 | CALL PARAMETERS | ||
| 2699 | |||
| 2700 | f(3) a=3, b=nil | ||
| 2701 | f(3, 4) a=3, b=4 | ||
| 2702 | f(3, 4, 5) a=3, b=4 | ||
| 2703 | f(r(), 10) a=1, b=10 | ||
| 2704 | f(r()) a=1, b=2 | ||
| 2705 | |||
| 2706 | g(3) a=3, b=nil, ... --> (nothing) | ||
| 2707 | g(3, 4) a=3, b=4, ... --> (nothing) | ||
| 2708 | g(3, 4, 5, 8) a=3, b=4, ... --> 5 8 | ||
| 2709 | g(5, r()) a=5, b=1, ... --> 2 3 | ||
| 2710 | </pre> | ||
| 2711 | |||
| 2712 | <p> | ||
| 2713 | Results are returned using the <b>return</b> statement (see <a href="#3.3.4">§3.3.4</a>). | ||
| 2714 | If control reaches the end of a function | ||
| 2715 | without encountering a <b>return</b> statement, | ||
| 2716 | then the function returns with no results. | ||
| 2717 | |||
| 2718 | |||
| 2719 | <p> | ||
| 2720 | |||
| 2721 | There is a system-dependent limit on the number of values | ||
| 2722 | that a function may return. | ||
| 2723 | This limit is guaranteed to be greater than 1000. | ||
| 2724 | |||
| 2725 | |||
| 2726 | <p> | ||
| 2727 | The <em>colon</em> syntax | ||
| 2728 | is used to emulate <em>methods</em>, | ||
| 2729 | adding an implicit extra parameter <code>self</code> to the function. | ||
| 2730 | Thus, the statement | ||
| 2731 | |||
| 2732 | <pre> | ||
| 2733 | function t.a.b.c:f (<em>params</em>) <em>body</em> end | ||
| 2734 | </pre><p> | ||
| 2735 | is syntactic sugar for | ||
| 2736 | |||
| 2737 | <pre> | ||
| 2738 | t.a.b.c.f = function (self, <em>params</em>) <em>body</em> end | ||
| 2739 | </pre> | ||
| 2740 | |||
| 2741 | |||
| 2742 | |||
| 2743 | |||
| 2744 | <h3>3.4.12 – <a name="3.4.12">Lists of expressions, multiple results, | ||
| 2745 | and adjustment</a></h3> | ||
| 2746 | |||
| 2747 | <p> | ||
| 2748 | Both function calls and vararg expressions can result in multiple values. | ||
| 2749 | These expressions are called <em>multires expressions</em>. | ||
| 2750 | |||
| 2751 | |||
| 2752 | <p> | ||
| 2753 | When a multires expression is used as the last element | ||
| 2754 | of a list of expressions, | ||
| 2755 | all results from the expression are added to the | ||
| 2756 | list of values produced by the list of expressions. | ||
| 2757 | Note that a single expression | ||
| 2758 | in a place that expects a list of expressions | ||
| 2759 | is the last expression in that (singleton) list. | ||
| 2760 | |||
| 2761 | |||
| 2762 | <p> | ||
| 2763 | These are the places where Lua expects a list of expressions: | ||
| 2764 | |||
| 2765 | <ul> | ||
| 2766 | |||
| 2767 | <li>A <b>return</b> statement, | ||
| 2768 | for instance <code>return e1, e2, e3</code> (see <a href="#3.3.4">§3.3.4</a>).</li> | ||
| 2769 | |||
| 2770 | <li>A table constructor, | ||
| 2771 | for instance <code>{e1, e2, e3}</code> (see <a href="#3.4.9">§3.4.9</a>).</li> | ||
| 2772 | |||
| 2773 | <li>The arguments of a function call, | ||
| 2774 | for instance <code>foo(e1, e2, e3)</code> (see <a href="#3.4.10">§3.4.10</a>).</li> | ||
| 2775 | |||
| 2776 | <li>A multiple assignment, | ||
| 2777 | for instance <code>a , b, c = e1, e2, e3</code> (see <a href="#3.3.3">§3.3.3</a>).</li> | ||
| 2778 | |||
| 2779 | <li>A local declaration, | ||
| 2780 | for instance <code>local a , b, c = e1, e2, e3</code> (see <a href="#3.3.7">§3.3.7</a>).</li> | ||
| 2781 | |||
| 2782 | <li>The initial values in a generic <b>for</b> loop, | ||
| 2783 | for instance <code>for k in e1, e2, e3 do ... end</code> (see <a href="#3.3.5">§3.3.5</a>).</li> | ||
| 2784 | |||
| 2785 | </ul><p> | ||
| 2786 | In the last four cases, | ||
| 2787 | the list of values from the list of expressions | ||
| 2788 | must be <em>adjusted</em> to a specific length: | ||
| 2789 | the number of parameters in a call to a non-variadic function | ||
| 2790 | (see <a href="#3.4.11">§3.4.11</a>), | ||
| 2791 | the number of variables in a multiple assignment or | ||
| 2792 | a local declaration, | ||
| 2793 | and exactly four values for a generic <b>for</b> loop. | ||
| 2794 | The <em>adjustment</em> follows these rules: | ||
| 2795 | If there are more values than needed, | ||
| 2796 | the extra values are thrown away; | ||
| 2797 | if there are fewer values than needed, | ||
| 2798 | the list is extended with <b>nil</b>'s. | ||
| 2799 | When the list of expressions ends with a multires expression, | ||
| 2800 | all results from that expression enter the list of values | ||
| 2801 | before the adjustment. | ||
| 2802 | |||
| 2803 | |||
| 2804 | <p> | ||
| 2805 | When a multires expression is used | ||
| 2806 | in a list of expressions without being the last element, | ||
| 2807 | or in a place where the syntax expects a single expression, | ||
| 2808 | Lua adjusts the result list of that expression to one element. | ||
| 2809 | As a particular case, | ||
| 2810 | the syntax expects a single expression inside a parenthesized expression; | ||
| 2811 | therefore, adding parentheses around a multires expression | ||
| 2812 | forces it to produce exactly one result. | ||
| 2813 | |||
| 2814 | |||
| 2815 | <p> | ||
| 2816 | We seldom need to use a vararg expression in a place | ||
| 2817 | where the syntax expects a single expression. | ||
| 2818 | (Usually it is simpler to add a regular parameter before | ||
| 2819 | the variadic part and use that parameter.) | ||
| 2820 | When there is such a need, | ||
| 2821 | we recommend assigning the vararg expression | ||
| 2822 | to a single variable and using that variable | ||
| 2823 | in its place. | ||
| 2824 | |||
| 2825 | |||
| 2826 | <p> | ||
| 2827 | Here are some examples of uses of mutlres expressions. | ||
| 2828 | In all cases, when the construction needs | ||
| 2829 | "the n-th result" and there is no such result, | ||
| 2830 | it uses a <b>nil</b>. | ||
| 2831 | |||
| 2832 | <pre> | ||
| 2833 | print(x, f()) -- prints x and all results from f(). | ||
| 2834 | print(x, (f())) -- prints x and the first result from f(). | ||
| 2835 | print(f(), x) -- prints the first result from f() and x. | ||
| 2836 | print(1 + f()) -- prints 1 added to the first result from f(). | ||
| 2837 | local x = ... -- x gets the first vararg argument. | ||
| 2838 | x,y = ... -- x gets the first vararg argument, | ||
| 2839 | -- y gets the second vararg argument. | ||
| 2840 | x,y,z = w, f() -- x gets w, y gets the first result from f(), | ||
| 2841 | -- z gets the second result from f(). | ||
| 2842 | x,y,z = f() -- x gets the first result from f(), | ||
| 2843 | -- y gets the second result from f(), | ||
| 2844 | -- z gets the third result from f(). | ||
| 2845 | x,y,z = f(), g() -- x gets the first result from f(), | ||
| 2846 | -- y gets the first result from g(), | ||
| 2847 | -- z gets the second result from g(). | ||
| 2848 | x,y,z = (f()) -- x gets the first result from f(), y and z get nil. | ||
| 2849 | return f() -- returns all results from f(). | ||
| 2850 | return x, ... -- returns x and all received vararg arguments. | ||
| 2851 | return x,y,f() -- returns x, y, and all results from f(). | ||
| 2852 | {f()} -- creates a list with all results from f(). | ||
| 2853 | {...} -- creates a list with all vararg arguments. | ||
| 2854 | {f(), 5} -- creates a list with the first result from f() and 5. | ||
| 2855 | </pre> | ||
| 2856 | |||
| 2857 | |||
| 2858 | |||
| 2859 | |||
| 2860 | |||
| 2861 | |||
| 2862 | <h2>3.5 – <a name="3.5">Visibility Rules</a></h2> | ||
| 2863 | |||
| 2864 | <p> | ||
| 2865 | |||
| 2866 | Lua is a lexically scoped language. | ||
| 2867 | The scope of a local variable begins at the first statement after | ||
| 2868 | its declaration and lasts until the last non-void statement | ||
| 2869 | of the innermost block that includes the declaration. | ||
| 2870 | (<em>Void statements</em> are labels and empty statements.) | ||
| 2871 | Consider the following example: | ||
| 2872 | |||
| 2873 | <pre> | ||
| 2874 | x = 10 -- global variable | ||
| 2875 | do -- new block | ||
| 2876 | local x = x -- new 'x', with value 10 | ||
| 2877 | print(x) --> 10 | ||
| 2878 | x = x+1 | ||
| 2879 | do -- another block | ||
| 2880 | local x = x+1 -- another 'x' | ||
| 2881 | print(x) --> 12 | ||
| 2882 | end | ||
| 2883 | print(x) --> 11 | ||
| 2884 | end | ||
| 2885 | print(x) --> 10 (the global one) | ||
| 2886 | </pre> | ||
| 2887 | |||
| 2888 | <p> | ||
| 2889 | Notice that, in a declaration like <code>local x = x</code>, | ||
| 2890 | the new <code>x</code> being declared is not in scope yet, | ||
| 2891 | and so the second <code>x</code> refers to the outside variable. | ||
| 2892 | |||
| 2893 | |||
| 2894 | <p> | ||
| 2895 | Because of the lexical scoping rules, | ||
| 2896 | local variables can be freely accessed by functions | ||
| 2897 | defined inside their scope. | ||
| 2898 | A local variable used by an inner function is called an <em>upvalue</em> | ||
| 2899 | (or <em>external local variable</em>, or simply <em>external variable</em>) | ||
| 2900 | inside the inner function. | ||
| 2901 | |||
| 2902 | |||
| 2903 | <p> | ||
| 2904 | Notice that each execution of a <b>local</b> statement | ||
| 2905 | defines new local variables. | ||
| 2906 | Consider the following example: | ||
| 2907 | |||
| 2908 | <pre> | ||
| 2909 | a = {} | ||
| 2910 | local x = 20 | ||
| 2911 | for i = 1, 10 do | ||
| 2912 | local y = 0 | ||
| 2913 | a[i] = function () y = y + 1; return x + y end | ||
| 2914 | end | ||
| 2915 | </pre><p> | ||
| 2916 | The loop creates ten closures | ||
| 2917 | (that is, ten instances of the anonymous function). | ||
| 2918 | Each of these closures uses a different <code>y</code> variable, | ||
| 2919 | while all of them share the same <code>x</code>. | ||
| 2920 | |||
| 2921 | |||
| 2922 | |||
| 2923 | |||
| 2924 | |||
| 2925 | <h1>4 – <a name="4">The Application Program Interface</a></h1> | ||
| 2926 | |||
| 2927 | |||
| 2928 | |||
| 2929 | <p> | ||
| 2930 | |||
| 2931 | This section describes the C API for Lua, that is, | ||
| 2932 | the set of C functions available to the host program to communicate | ||
| 2933 | with Lua. | ||
| 2934 | All API functions and related types and constants | ||
| 2935 | are declared in the header file <a name="pdf-lua.h"><code>lua.h</code></a>. | ||
| 2936 | |||
| 2937 | |||
| 2938 | <p> | ||
| 2939 | Even when we use the term "function", | ||
| 2940 | any facility in the API may be provided as a macro instead. | ||
| 2941 | Except where stated otherwise, | ||
| 2942 | all such macros use each of their arguments exactly once | ||
| 2943 | (except for the first argument, which is always a Lua state), | ||
| 2944 | and so do not generate any hidden side-effects. | ||
| 2945 | |||
| 2946 | |||
| 2947 | <p> | ||
| 2948 | As in most C libraries, | ||
| 2949 | the Lua API functions do not check their arguments | ||
| 2950 | for validity or consistency. | ||
| 2951 | However, you can change this behavior by compiling Lua | ||
| 2952 | with the macro <a name="pdf-LUA_USE_APICHECK"><code>LUA_USE_APICHECK</code></a> defined. | ||
| 2953 | |||
| 2954 | |||
| 2955 | <p> | ||
| 2956 | The Lua library is fully reentrant: | ||
| 2957 | it has no global variables. | ||
| 2958 | It keeps all information it needs in a dynamic structure, | ||
| 2959 | called the <em>Lua state</em>. | ||
| 2960 | |||
| 2961 | |||
| 2962 | <p> | ||
| 2963 | Each Lua state has one or more threads, | ||
| 2964 | which correspond to independent, cooperative lines of execution. | ||
| 2965 | The type <a href="#lua_State"><code>lua_State</code></a> (despite its name) refers to a thread. | ||
| 2966 | (Indirectly, through the thread, it also refers to the | ||
| 2967 | Lua state associated to the thread.) | ||
| 2968 | |||
| 2969 | |||
| 2970 | <p> | ||
| 2971 | A pointer to a thread must be passed as the first argument to | ||
| 2972 | every function in the library, except to <a href="#lua_newstate"><code>lua_newstate</code></a>, | ||
| 2973 | which creates a Lua state from scratch and returns a pointer | ||
| 2974 | to the <em>main thread</em> in the new state. | ||
| 2975 | |||
| 2976 | |||
| 2977 | |||
| 2978 | |||
| 2979 | |||
| 2980 | <h2>4.1 – <a name="4.1">The Stack</a></h2> | ||
| 2981 | |||
| 2982 | |||
| 2983 | |||
| 2984 | <p> | ||
| 2985 | Lua uses a <em>virtual stack</em> to pass values to and from C. | ||
| 2986 | Each element in this stack represents a Lua value | ||
| 2987 | (<b>nil</b>, number, string, etc.). | ||
| 2988 | Functions in the API can access this stack through the | ||
| 2989 | Lua state parameter that they receive. | ||
| 2990 | |||
| 2991 | |||
| 2992 | <p> | ||
| 2993 | Whenever Lua calls C, the called function gets a new stack, | ||
| 2994 | which is independent of previous stacks and of stacks of | ||
| 2995 | C functions that are still active. | ||
| 2996 | This stack initially contains any arguments to the C function | ||
| 2997 | and it is where the C function can store temporary | ||
| 2998 | Lua values and must push its results | ||
| 2999 | to be returned to the caller (see <a href="#lua_CFunction"><code>lua_CFunction</code></a>). | ||
| 3000 | |||
| 3001 | |||
| 3002 | <p> | ||
| 3003 | For convenience, | ||
| 3004 | most query operations in the API do not follow a strict stack discipline. | ||
| 3005 | Instead, they can refer to any element in the stack | ||
| 3006 | by using an <em>index</em>: | ||
| 3007 | A positive index represents an absolute stack position, | ||
| 3008 | starting at 1 as the bottom of the stack; | ||
| 3009 | a negative index represents an offset relative to the top of the stack. | ||
| 3010 | More specifically, if the stack has <em>n</em> elements, | ||
| 3011 | then index 1 represents the first element | ||
| 3012 | (that is, the element that was pushed onto the stack first) | ||
| 3013 | and | ||
| 3014 | index <em>n</em> represents the last element; | ||
| 3015 | index -1 also represents the last element | ||
| 3016 | (that is, the element at the top) | ||
| 3017 | and index <em>-n</em> represents the first element. | ||
| 3018 | |||
| 3019 | |||
| 3020 | |||
| 3021 | |||
| 3022 | |||
| 3023 | <h3>4.1.1 – <a name="4.1.1">Stack Size</a></h3> | ||
| 3024 | |||
| 3025 | <p> | ||
| 3026 | When you interact with the Lua API, | ||
| 3027 | you are responsible for ensuring consistency. | ||
| 3028 | In particular, | ||
| 3029 | <em>you are responsible for controlling stack overflow</em>. | ||
| 3030 | When you call any API function, | ||
| 3031 | you must ensure the stack has enough room to accommodate the results. | ||
| 3032 | |||
| 3033 | |||
| 3034 | <p> | ||
| 3035 | There is one exception to the above rule: | ||
| 3036 | When you call a Lua function | ||
| 3037 | without a fixed number of results (see <a href="#lua_call"><code>lua_call</code></a>), | ||
| 3038 | Lua ensures that the stack has enough space for all results. | ||
| 3039 | However, it does not ensure any extra space. | ||
| 3040 | So, before pushing anything on the stack after such a call | ||
| 3041 | you should use <a href="#lua_checkstack"><code>lua_checkstack</code></a>. | ||
| 3042 | |||
| 3043 | |||
| 3044 | <p> | ||
| 3045 | Whenever Lua calls C, | ||
| 3046 | it ensures that the stack has space for | ||
| 3047 | at least <a name="pdf-LUA_MINSTACK"><code>LUA_MINSTACK</code></a> extra elements; | ||
| 3048 | that is, you can safely push up to <code>LUA_MINSTACK</code> values into it. | ||
| 3049 | <code>LUA_MINSTACK</code> is defined as 20, | ||
| 3050 | so that usually you do not have to worry about stack space | ||
| 3051 | unless your code has loops pushing elements onto the stack. | ||
| 3052 | Whenever necessary, | ||
| 3053 | you can use the function <a href="#lua_checkstack"><code>lua_checkstack</code></a> | ||
| 3054 | to ensure that the stack has enough space for pushing new elements. | ||
| 3055 | |||
| 3056 | |||
| 3057 | |||
| 3058 | |||
| 3059 | |||
| 3060 | <h3>4.1.2 – <a name="4.1.2">Valid and Acceptable Indices</a></h3> | ||
| 3061 | |||
| 3062 | <p> | ||
| 3063 | Any function in the API that receives stack indices | ||
| 3064 | works only with <em>valid indices</em> or <em>acceptable indices</em>. | ||
| 3065 | |||
| 3066 | |||
| 3067 | <p> | ||
| 3068 | A <em>valid index</em> is an index that refers to a | ||
| 3069 | position that stores a modifiable Lua value. | ||
| 3070 | It comprises stack indices between 1 and the stack top | ||
| 3071 | (<code>1 ≤ abs(index) ≤ top</code>) | ||
| 3072 | |||
| 3073 | plus <em>pseudo-indices</em>, | ||
| 3074 | which represent some positions that are accessible to C code | ||
| 3075 | but that are not in the stack. | ||
| 3076 | Pseudo-indices are used to access the registry (see <a href="#4.3">§4.3</a>) | ||
| 3077 | and the upvalues of a C function (see <a href="#4.2">§4.2</a>). | ||
| 3078 | |||
| 3079 | |||
| 3080 | <p> | ||
| 3081 | Functions that do not need a specific mutable position, | ||
| 3082 | but only a value (e.g., query functions), | ||
| 3083 | can be called with acceptable indices. | ||
| 3084 | An <em>acceptable index</em> can be any valid index, | ||
| 3085 | but it also can be any positive index after the stack top | ||
| 3086 | within the space allocated for the stack, | ||
| 3087 | that is, indices up to the stack size. | ||
| 3088 | (Note that 0 is never an acceptable index.) | ||
| 3089 | Indices to upvalues (see <a href="#4.2">§4.2</a>) greater than the real number | ||
| 3090 | of upvalues in the current C function are also acceptable (but invalid). | ||
| 3091 | Except when noted otherwise, | ||
| 3092 | functions in the API work with acceptable indices. | ||
| 3093 | |||
| 3094 | |||
| 3095 | <p> | ||
| 3096 | Acceptable indices serve to avoid extra tests | ||
| 3097 | against the stack top when querying the stack. | ||
| 3098 | For instance, a C function can query its third argument | ||
| 3099 | without the need to check whether there is a third argument, | ||
| 3100 | that is, without the need to check whether 3 is a valid index. | ||
| 3101 | |||
| 3102 | |||
| 3103 | <p> | ||
| 3104 | For functions that can be called with acceptable indices, | ||
| 3105 | any non-valid index is treated as if it | ||
| 3106 | contains a value of a virtual type <a name="pdf-LUA_TNONE"><code>LUA_TNONE</code></a>, | ||
| 3107 | which behaves like a nil value. | ||
| 3108 | |||
| 3109 | |||
| 3110 | |||
| 3111 | |||
| 3112 | |||
| 3113 | <h3>4.1.3 – <a name="4.1.3">Pointers to strings</a></h3> | ||
| 3114 | |||
| 3115 | <p> | ||
| 3116 | Several functions in the API return pointers (<code>const char*</code>) | ||
| 3117 | to Lua strings in the stack. | ||
| 3118 | (See <a href="#lua_pushfstring"><code>lua_pushfstring</code></a>, <a href="#lua_pushlstring"><code>lua_pushlstring</code></a>, | ||
| 3119 | <a href="#lua_pushstring"><code>lua_pushstring</code></a>, and <a href="#lua_tolstring"><code>lua_tolstring</code></a>. | ||
| 3120 | See also <a href="#luaL_checklstring"><code>luaL_checklstring</code></a>, <a href="#luaL_checkstring"><code>luaL_checkstring</code></a>, | ||
| 3121 | and <a href="#luaL_tolstring"><code>luaL_tolstring</code></a> in the auxiliary library.) | ||
| 3122 | |||
| 3123 | |||
| 3124 | <p> | ||
| 3125 | In general, | ||
| 3126 | Lua's garbage collection can free or move internal memory | ||
| 3127 | and then invalidate pointers to internal strings. | ||
| 3128 | To allow a safe use of these pointers, | ||
| 3129 | the API guarantees that any pointer to a string in a stack index | ||
| 3130 | is valid while the string value at that index is not removed from the stack. | ||
| 3131 | (It can be moved to another index, though.) | ||
| 3132 | When the index is a pseudo-index (referring to an upvalue), | ||
| 3133 | the pointer is valid while the corresponding call is active and | ||
| 3134 | the corresponding upvalue is not modified. | ||
| 3135 | |||
| 3136 | |||
| 3137 | <p> | ||
| 3138 | Some functions in the debug interface | ||
| 3139 | also return pointers to strings, | ||
| 3140 | namely <a href="#lua_getlocal"><code>lua_getlocal</code></a>, <a href="#lua_getupvalue"><code>lua_getupvalue</code></a>, | ||
| 3141 | <a href="#lua_setlocal"><code>lua_setlocal</code></a>, and <a href="#lua_setupvalue"><code>lua_setupvalue</code></a>. | ||
| 3142 | For these functions, the pointer is guaranteed to | ||
| 3143 | be valid while the caller function is active and | ||
| 3144 | the given closure (if one was given) is in the stack. | ||
| 3145 | |||
| 3146 | |||
| 3147 | <p> | ||
| 3148 | Except for these guarantees, | ||
| 3149 | the garbage collector is free to invalidate | ||
| 3150 | any pointer to internal strings. | ||
| 3151 | |||
| 3152 | |||
| 3153 | |||
| 3154 | |||
| 3155 | |||
| 3156 | |||
| 3157 | |||
| 3158 | <h2>4.2 – <a name="4.2">C Closures</a></h2> | ||
| 3159 | |||
| 3160 | <p> | ||
| 3161 | When a C function is created, | ||
| 3162 | it is possible to associate some values with it, | ||
| 3163 | thus creating a <em>C closure</em> | ||
| 3164 | (see <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a>); | ||
| 3165 | these values are called <em>upvalues</em> and are | ||
| 3166 | accessible to the function whenever it is called. | ||
| 3167 | |||
| 3168 | |||
| 3169 | <p> | ||
| 3170 | Whenever a C function is called, | ||
| 3171 | its upvalues are located at specific pseudo-indices. | ||
| 3172 | These pseudo-indices are produced by the macro | ||
| 3173 | <a href="#lua_upvalueindex"><code>lua_upvalueindex</code></a>. | ||
| 3174 | The first upvalue associated with a function is at index | ||
| 3175 | <code>lua_upvalueindex(1)</code>, and so on. | ||
| 3176 | Any access to <code>lua_upvalueindex(<em>n</em>)</code>, | ||
| 3177 | where <em>n</em> is greater than the number of upvalues of the | ||
| 3178 | current function | ||
| 3179 | (but not greater than 256, | ||
| 3180 | which is one plus the maximum number of upvalues in a closure), | ||
| 3181 | produces an acceptable but invalid index. | ||
| 3182 | |||
| 3183 | |||
| 3184 | <p> | ||
| 3185 | A C closure can also change the values | ||
| 3186 | of its corresponding upvalues. | ||
| 3187 | |||
| 3188 | |||
| 3189 | |||
| 3190 | |||
| 3191 | |||
| 3192 | <h2>4.3 – <a name="4.3">Registry</a></h2> | ||
| 3193 | |||
| 3194 | <p> | ||
| 3195 | Lua provides a <em>registry</em>, | ||
| 3196 | a predefined table that can be used by any C code to | ||
| 3197 | store whatever Lua values it needs to store. | ||
| 3198 | The registry table is always accessible at pseudo-index | ||
| 3199 | <a name="pdf-LUA_REGISTRYINDEX"><code>LUA_REGISTRYINDEX</code></a>. | ||
| 3200 | Any C library can store data into this table, | ||
| 3201 | but it must take care to choose keys | ||
| 3202 | that are different from those used | ||
| 3203 | by other libraries, to avoid collisions. | ||
| 3204 | Typically, you should use as key a string containing your library name, | ||
| 3205 | or a light userdata with the address of a C object in your code, | ||
| 3206 | or any Lua object created by your code. | ||
| 3207 | As with variable names, | ||
| 3208 | string keys starting with an underscore followed by | ||
| 3209 | uppercase letters are reserved for Lua. | ||
| 3210 | |||
| 3211 | |||
| 3212 | <p> | ||
| 3213 | The integer keys in the registry are used | ||
| 3214 | by the reference mechanism (see <a href="#luaL_ref"><code>luaL_ref</code></a>) | ||
| 3215 | and by some predefined values. | ||
| 3216 | Therefore, integer keys in the registry | ||
| 3217 | must not be used for other purposes. | ||
| 3218 | |||
| 3219 | |||
| 3220 | <p> | ||
| 3221 | When you create a new Lua state, | ||
| 3222 | its registry comes with some predefined values. | ||
| 3223 | These predefined values are indexed with integer keys | ||
| 3224 | defined as constants in <code>lua.h</code>. | ||
| 3225 | The following constants are defined: | ||
| 3226 | |||
| 3227 | <ul> | ||
| 3228 | <li><b><a name="pdf-LUA_RIDX_MAINTHREAD"><code>LUA_RIDX_MAINTHREAD</code></a>: </b> At this index the registry has | ||
| 3229 | the main thread of the state. | ||
| 3230 | (The main thread is the one created together with the state.) | ||
| 3231 | </li> | ||
| 3232 | |||
| 3233 | <li><b><a name="pdf-LUA_RIDX_GLOBALS"><code>LUA_RIDX_GLOBALS</code></a>: </b> At this index the registry has | ||
| 3234 | the global environment. | ||
| 3235 | </li> | ||
| 3236 | </ul> | ||
| 3237 | |||
| 3238 | |||
| 3239 | |||
| 3240 | |||
| 3241 | <h2>4.4 – <a name="4.4">Error Handling in C</a></h2> | ||
| 3242 | |||
| 3243 | |||
| 3244 | |||
| 3245 | <p> | ||
| 3246 | Internally, Lua uses the C <code>longjmp</code> facility to handle errors. | ||
| 3247 | (Lua will use exceptions if you compile it as C++; | ||
| 3248 | search for <code>LUAI_THROW</code> in the source code for details.) | ||
| 3249 | When Lua faces any error, | ||
| 3250 | such as a memory allocation error or a type error, | ||
| 3251 | it <em>raises</em> an error; | ||
| 3252 | that is, it does a long jump. | ||
| 3253 | A <em>protected environment</em> uses <code>setjmp</code> | ||
| 3254 | to set a recovery point; | ||
| 3255 | any error jumps to the most recent active recovery point. | ||
| 3256 | |||
| 3257 | |||
| 3258 | <p> | ||
| 3259 | Inside a C function you can raise an error explicitly | ||
| 3260 | by calling <a href="#lua_error"><code>lua_error</code></a>. | ||
| 3261 | |||
| 3262 | |||
| 3263 | <p> | ||
| 3264 | Most functions in the API can raise an error, | ||
| 3265 | for instance due to a memory allocation error. | ||
| 3266 | The documentation for each function indicates whether | ||
| 3267 | it can raise errors. | ||
| 3268 | |||
| 3269 | |||
| 3270 | <p> | ||
| 3271 | If an error happens outside any protected environment, | ||
| 3272 | Lua calls a <em>panic function</em> (see <a href="#lua_atpanic"><code>lua_atpanic</code></a>) | ||
| 3273 | and then calls <code>abort</code>, | ||
| 3274 | thus exiting the host application. | ||
| 3275 | Your panic function can avoid this exit by | ||
| 3276 | never returning | ||
| 3277 | (e.g., doing a long jump to your own recovery point outside Lua). | ||
| 3278 | |||
| 3279 | |||
| 3280 | <p> | ||
| 3281 | The panic function, | ||
| 3282 | as its name implies, | ||
| 3283 | is a mechanism of last resort. | ||
| 3284 | Programs should avoid it. | ||
| 3285 | As a general rule, | ||
| 3286 | when a C function is called by Lua with a Lua state, | ||
| 3287 | it can do whatever it wants on that Lua state, | ||
| 3288 | as it should be already protected. | ||
| 3289 | However, | ||
| 3290 | when C code operates on other Lua states | ||
| 3291 | (e.g., a Lua-state argument to the function, | ||
| 3292 | a Lua state stored in the registry, or | ||
| 3293 | the result of <a href="#lua_newthread"><code>lua_newthread</code></a>), | ||
| 3294 | it should use them only in API calls that cannot raise errors. | ||
| 3295 | |||
| 3296 | |||
| 3297 | <p> | ||
| 3298 | The panic function runs as if it were a message handler (see <a href="#2.3">§2.3</a>); | ||
| 3299 | in particular, the error object is on the top of the stack. | ||
| 3300 | However, there is no guarantee about stack space. | ||
| 3301 | To push anything on the stack, | ||
| 3302 | the panic function must first check the available space (see <a href="#4.1.1">§4.1.1</a>). | ||
| 3303 | |||
| 3304 | |||
| 3305 | |||
| 3306 | |||
| 3307 | |||
| 3308 | <h3>4.4.1 – <a name="4.4.1">Status Codes</a></h3> | ||
| 3309 | |||
| 3310 | <p> | ||
| 3311 | Several functions that report errors in the API use the following | ||
| 3312 | status codes to indicate different kinds of errors or other conditions: | ||
| 3313 | |||
| 3314 | <ul> | ||
| 3315 | |||
| 3316 | <li><b><a name="pdf-LUA_OK"><code>LUA_OK</code></a> (0): </b> no errors.</li> | ||
| 3317 | |||
| 3318 | <li><b><a name="pdf-LUA_ERRRUN"><code>LUA_ERRRUN</code></a>: </b> a runtime error.</li> | ||
| 3319 | |||
| 3320 | <li><b><a name="pdf-LUA_ERRMEM"><code>LUA_ERRMEM</code></a>: </b> | ||
| 3321 | memory allocation error. | ||
| 3322 | For such errors, Lua does not call the message handler. | ||
| 3323 | </li> | ||
| 3324 | |||
| 3325 | <li><b><a name="pdf-LUA_ERRERR"><code>LUA_ERRERR</code></a>: </b> error while running the message handler.</li> | ||
| 3326 | |||
| 3327 | <li><b><a name="pdf-LUA_ERRSYNTAX"><code>LUA_ERRSYNTAX</code></a>: </b> syntax error during precompilation.</li> | ||
| 3328 | |||
| 3329 | <li><b><a name="pdf-LUA_YIELD"><code>LUA_YIELD</code></a>: </b> the thread (coroutine) yields.</li> | ||
| 3330 | |||
| 3331 | <li><b><a name="pdf-LUA_ERRFILE"><code>LUA_ERRFILE</code></a>: </b> a file-related error; | ||
| 3332 | e.g., it cannot open or read the file.</li> | ||
| 3333 | |||
| 3334 | </ul><p> | ||
| 3335 | These constants are defined in the header file <code>lua.h</code>. | ||
| 3336 | |||
| 3337 | |||
| 3338 | |||
| 3339 | |||
| 3340 | |||
| 3341 | |||
| 3342 | |||
| 3343 | <h2>4.5 – <a name="4.5">Handling Yields in C</a></h2> | ||
| 3344 | |||
| 3345 | <p> | ||
| 3346 | Internally, Lua uses the C <code>longjmp</code> facility to yield a coroutine. | ||
| 3347 | Therefore, if a C function <code>foo</code> calls an API function | ||
| 3348 | and this API function yields | ||
| 3349 | (directly or indirectly by calling another function that yields), | ||
| 3350 | Lua cannot return to <code>foo</code> any more, | ||
| 3351 | because the <code>longjmp</code> removes its frame from the C stack. | ||
| 3352 | |||
| 3353 | |||
| 3354 | <p> | ||
| 3355 | To avoid this kind of problem, | ||
| 3356 | Lua raises an error whenever it tries to yield across an API call, | ||
| 3357 | except for three functions: | ||
| 3358 | <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>. | ||
| 3359 | All those functions receive a <em>continuation function</em> | ||
| 3360 | (as a parameter named <code>k</code>) to continue execution after a yield. | ||
| 3361 | |||
| 3362 | |||
| 3363 | <p> | ||
| 3364 | We need to set some terminology to explain continuations. | ||
| 3365 | We have a C function called from Lua which we will call | ||
| 3366 | the <em>original function</em>. | ||
| 3367 | This original function then calls one of those three functions in the C API, | ||
| 3368 | which we will call the <em>callee function</em>, | ||
| 3369 | that then yields the current thread. | ||
| 3370 | This can happen when the callee function is <a href="#lua_yieldk"><code>lua_yieldk</code></a>, | ||
| 3371 | 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> | ||
| 3372 | and the function called by them yields. | ||
| 3373 | |||
| 3374 | |||
| 3375 | <p> | ||
| 3376 | Suppose the running thread yields while executing the callee function. | ||
| 3377 | After the thread resumes, | ||
| 3378 | it eventually will finish running the callee function. | ||
| 3379 | However, | ||
| 3380 | the callee function cannot return to the original function, | ||
| 3381 | because its frame in the C stack was destroyed by the yield. | ||
| 3382 | Instead, Lua calls a <em>continuation function</em>, | ||
| 3383 | which was given as an argument to the callee function. | ||
| 3384 | As the name implies, | ||
| 3385 | the continuation function should continue the task | ||
| 3386 | of the original function. | ||
| 3387 | |||
| 3388 | |||
| 3389 | <p> | ||
| 3390 | As an illustration, consider the following function: | ||
| 3391 | |||
| 3392 | <pre> | ||
| 3393 | int original_function (lua_State *L) { | ||
| 3394 | ... /* code 1 */ | ||
| 3395 | status = lua_pcall(L, n, m, h); /* calls Lua */ | ||
| 3396 | ... /* code 2 */ | ||
| 3397 | } | ||
| 3398 | </pre><p> | ||
| 3399 | Now we want to allow | ||
| 3400 | the Lua code being run by <a href="#lua_pcall"><code>lua_pcall</code></a> to yield. | ||
| 3401 | First, we can rewrite our function like here: | ||
| 3402 | |||
| 3403 | <pre> | ||
| 3404 | int k (lua_State *L, int status, lua_KContext ctx) { | ||
| 3405 | ... /* code 2 */ | ||
| 3406 | } | ||
| 3407 | |||
| 3408 | int original_function (lua_State *L) { | ||
| 3409 | ... /* code 1 */ | ||
| 3410 | return k(L, lua_pcall(L, n, m, h), ctx); | ||
| 3411 | } | ||
| 3412 | </pre><p> | ||
| 3413 | In the above code, | ||
| 3414 | the new function <code>k</code> is a | ||
| 3415 | <em>continuation function</em> (with type <a href="#lua_KFunction"><code>lua_KFunction</code></a>), | ||
| 3416 | which should do all the work that the original function | ||
| 3417 | was doing after calling <a href="#lua_pcall"><code>lua_pcall</code></a>. | ||
| 3418 | Now, we must inform Lua that it must call <code>k</code> if the Lua code | ||
| 3419 | being executed by <a href="#lua_pcall"><code>lua_pcall</code></a> gets interrupted in some way | ||
| 3420 | (errors or yielding), | ||
| 3421 | so we rewrite the code as here, | ||
| 3422 | replacing <a href="#lua_pcall"><code>lua_pcall</code></a> by <a href="#lua_pcallk"><code>lua_pcallk</code></a>: | ||
| 3423 | |||
| 3424 | <pre> | ||
| 3425 | int original_function (lua_State *L) { | ||
| 3426 | ... /* code 1 */ | ||
| 3427 | return k(L, lua_pcallk(L, n, m, h, ctx2, k), ctx1); | ||
| 3428 | } | ||
| 3429 | </pre><p> | ||
| 3430 | Note the external, explicit call to the continuation: | ||
| 3431 | Lua will call the continuation only if needed, that is, | ||
| 3432 | in case of errors or resuming after a yield. | ||
| 3433 | If the called function returns normally without ever yielding, | ||
| 3434 | <a href="#lua_pcallk"><code>lua_pcallk</code></a> (and <a href="#lua_callk"><code>lua_callk</code></a>) will also return normally. | ||
| 3435 | (Of course, instead of calling the continuation in that case, | ||
| 3436 | you can do the equivalent work directly inside the original function.) | ||
| 3437 | |||
| 3438 | |||
| 3439 | <p> | ||
| 3440 | Besides the Lua state, | ||
| 3441 | the continuation function has two other parameters: | ||
| 3442 | the final status of the call and the context value (<code>ctx</code>) that | ||
| 3443 | was passed originally to <a href="#lua_pcallk"><code>lua_pcallk</code></a>. | ||
| 3444 | Lua does not use this context value; | ||
| 3445 | it only passes this value from the original function to the | ||
| 3446 | continuation function. | ||
| 3447 | For <a href="#lua_pcallk"><code>lua_pcallk</code></a>, | ||
| 3448 | the status is the same value that would be returned by <a href="#lua_pcallk"><code>lua_pcallk</code></a>, | ||
| 3449 | except that it is <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> when being executed after a yield | ||
| 3450 | (instead of <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>). | ||
| 3451 | For <a href="#lua_yieldk"><code>lua_yieldk</code></a> and <a href="#lua_callk"><code>lua_callk</code></a>, | ||
| 3452 | the status is always <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> when Lua calls the continuation. | ||
| 3453 | (For these two functions, | ||
| 3454 | Lua will not call the continuation in case of errors, | ||
| 3455 | because they do not handle errors.) | ||
| 3456 | Similarly, when using <a href="#lua_callk"><code>lua_callk</code></a>, | ||
| 3457 | you should call the continuation function | ||
| 3458 | with <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> as the status. | ||
| 3459 | (For <a href="#lua_yieldk"><code>lua_yieldk</code></a>, there is not much point in calling | ||
| 3460 | directly the continuation function, | ||
| 3461 | because <a href="#lua_yieldk"><code>lua_yieldk</code></a> usually does not return.) | ||
| 3462 | |||
| 3463 | |||
| 3464 | <p> | ||
| 3465 | Lua treats the continuation function as if it were the original function. | ||
| 3466 | The continuation function receives the same Lua stack | ||
| 3467 | from the original function, | ||
| 3468 | in the same state it would be if the callee function had returned. | ||
| 3469 | (For instance, | ||
| 3470 | after a <a href="#lua_callk"><code>lua_callk</code></a> the function and its arguments are | ||
| 3471 | removed from the stack and replaced by the results from the call.) | ||
| 3472 | It also has the same upvalues. | ||
| 3473 | Whatever it returns is handled by Lua as if it were the return | ||
| 3474 | of the original function. | ||
| 3475 | |||
| 3476 | |||
| 3477 | |||
| 3478 | |||
| 3479 | |||
| 3480 | <h2>4.6 – <a name="4.6">Functions and Types</a></h2> | ||
| 3481 | |||
| 3482 | <p> | ||
| 3483 | Here we list all functions and types from the C API in | ||
| 3484 | alphabetical order. | ||
| 3485 | Each function has an indicator like this: | ||
| 3486 | <span class="apii">[-o, +p, <em>x</em>]</span> | ||
| 3487 | |||
| 3488 | |||
| 3489 | <p> | ||
| 3490 | The first field, <code>o</code>, | ||
| 3491 | is how many elements the function pops from the stack. | ||
| 3492 | The second field, <code>p</code>, | ||
| 3493 | is how many elements the function pushes onto the stack. | ||
| 3494 | (Any function always pushes its results after popping its arguments.) | ||
| 3495 | A field in the form <code>x|y</code> means the function can push (or pop) | ||
| 3496 | <code>x</code> or <code>y</code> elements, | ||
| 3497 | depending on the situation; | ||
| 3498 | an interrogation mark '<code>?</code>' means that | ||
| 3499 | we cannot know how many elements the function pops/pushes | ||
| 3500 | by looking only at its arguments. | ||
| 3501 | (For instance, they may depend on what is in the stack.) | ||
| 3502 | The third field, <code>x</code>, | ||
| 3503 | tells whether the function may raise errors: | ||
| 3504 | '<code>-</code>' means the function never raises any error; | ||
| 3505 | '<code>m</code>' means the function may raise only out-of-memory errors; | ||
| 3506 | '<code>v</code>' means the function may raise the errors explained in the text; | ||
| 3507 | '<code>e</code>' means the function can run arbitrary Lua code, | ||
| 3508 | either directly or through metamethods, | ||
| 3509 | and therefore may raise any errors. | ||
| 3510 | |||
| 3511 | |||
| 3512 | |||
| 3513 | <hr><h3><a name="lua_absindex"><code>lua_absindex</code></a></h3><p> | ||
| 3514 | <span class="apii">[-0, +0, –]</span> | ||
| 3515 | <pre>int lua_absindex (lua_State *L, int idx);</pre> | ||
| 3516 | |||
| 3517 | <p> | ||
| 3518 | Converts the acceptable index <code>idx</code> | ||
| 3519 | into an equivalent absolute index | ||
| 3520 | (that is, one that does not depend on the stack size). | ||
| 3521 | |||
| 3522 | |||
| 3523 | |||
| 3524 | |||
| 3525 | |||
| 3526 | <hr><h3><a name="lua_Alloc"><code>lua_Alloc</code></a></h3> | ||
| 3527 | <pre>typedef void * (*lua_Alloc) (void *ud, | ||
| 3528 | void *ptr, | ||
| 3529 | size_t osize, | ||
| 3530 | size_t nsize);</pre> | ||
| 3531 | |||
| 3532 | <p> | ||
| 3533 | The type of the memory-allocation function used by Lua states. | ||
| 3534 | The allocator function must provide a | ||
| 3535 | functionality similar to <code>realloc</code>, | ||
| 3536 | but not exactly the same. | ||
| 3537 | Its arguments are | ||
| 3538 | <code>ud</code>, an opaque pointer passed to <a href="#lua_newstate"><code>lua_newstate</code></a>; | ||
| 3539 | <code>ptr</code>, a pointer to the block being allocated/reallocated/freed; | ||
| 3540 | <code>osize</code>, the original size of the block or some code about what | ||
| 3541 | is being allocated; | ||
| 3542 | and <code>nsize</code>, the new size of the block. | ||
| 3543 | |||
| 3544 | |||
| 3545 | <p> | ||
| 3546 | When <code>ptr</code> is not <code>NULL</code>, | ||
| 3547 | <code>osize</code> is the size of the block pointed by <code>ptr</code>, | ||
| 3548 | that is, the size given when it was allocated or reallocated. | ||
| 3549 | |||
| 3550 | |||
| 3551 | <p> | ||
| 3552 | When <code>ptr</code> is <code>NULL</code>, | ||
| 3553 | <code>osize</code> encodes the kind of object that Lua is allocating. | ||
| 3554 | <code>osize</code> is any of | ||
| 3555 | <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>, | ||
| 3556 | <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) | ||
| 3557 | Lua is creating a new object of that type. | ||
| 3558 | When <code>osize</code> is some other value, | ||
| 3559 | Lua is allocating memory for something else. | ||
| 3560 | |||
| 3561 | |||
| 3562 | <p> | ||
| 3563 | Lua assumes the following behavior from the allocator function: | ||
| 3564 | |||
| 3565 | |||
| 3566 | <p> | ||
| 3567 | When <code>nsize</code> is zero, | ||
| 3568 | the allocator must behave like <code>free</code> | ||
| 3569 | and then return <code>NULL</code>. | ||
| 3570 | |||
| 3571 | |||
| 3572 | <p> | ||
| 3573 | When <code>nsize</code> is not zero, | ||
| 3574 | the allocator must behave like <code>realloc</code>. | ||
| 3575 | In particular, the allocator returns <code>NULL</code> | ||
| 3576 | if and only if it cannot fulfill the request. | ||
| 3577 | |||
| 3578 | |||
| 3579 | <p> | ||
| 3580 | Here is a simple implementation for the allocator function. | ||
| 3581 | It is used in the auxiliary library by <a href="#luaL_newstate"><code>luaL_newstate</code></a>. | ||
| 3582 | |||
| 3583 | <pre> | ||
| 3584 | static void *l_alloc (void *ud, void *ptr, size_t osize, | ||
| 3585 | size_t nsize) { | ||
| 3586 | (void)ud; (void)osize; /* not used */ | ||
| 3587 | if (nsize == 0) { | ||
| 3588 | free(ptr); | ||
| 3589 | return NULL; | ||
| 3590 | } | ||
| 3591 | else | ||
| 3592 | return realloc(ptr, nsize); | ||
| 3593 | } | ||
| 3594 | </pre><p> | ||
| 3595 | Note that ISO C ensures | ||
| 3596 | that <code>free(NULL)</code> has no effect and that | ||
| 3597 | <code>realloc(NULL,size)</code> is equivalent to <code>malloc(size)</code>. | ||
| 3598 | |||
| 3599 | |||
| 3600 | |||
| 3601 | |||
| 3602 | |||
| 3603 | <hr><h3><a name="lua_arith"><code>lua_arith</code></a></h3><p> | ||
| 3604 | <span class="apii">[-(2|1), +1, <em>e</em>]</span> | ||
| 3605 | <pre>void lua_arith (lua_State *L, int op);</pre> | ||
| 3606 | |||
| 3607 | <p> | ||
| 3608 | Performs an arithmetic or bitwise operation over the two values | ||
| 3609 | (or one, in the case of negations) | ||
| 3610 | at the top of the stack, | ||
| 3611 | with the value on the top being the second operand, | ||
| 3612 | pops these values, and pushes the result of the operation. | ||
| 3613 | The function follows the semantics of the corresponding Lua operator | ||
| 3614 | (that is, it may call metamethods). | ||
| 3615 | |||
| 3616 | |||
| 3617 | <p> | ||
| 3618 | The value of <code>op</code> must be one of the following constants: | ||
| 3619 | |||
| 3620 | <ul> | ||
| 3621 | |||
| 3622 | <li><b><a name="pdf-LUA_OPADD"><code>LUA_OPADD</code></a>: </b> performs addition (<code>+</code>)</li> | ||
| 3623 | <li><b><a name="pdf-LUA_OPSUB"><code>LUA_OPSUB</code></a>: </b> performs subtraction (<code>-</code>)</li> | ||
| 3624 | <li><b><a name="pdf-LUA_OPMUL"><code>LUA_OPMUL</code></a>: </b> performs multiplication (<code>*</code>)</li> | ||
| 3625 | <li><b><a name="pdf-LUA_OPDIV"><code>LUA_OPDIV</code></a>: </b> performs float division (<code>/</code>)</li> | ||
| 3626 | <li><b><a name="pdf-LUA_OPIDIV"><code>LUA_OPIDIV</code></a>: </b> performs floor division (<code>//</code>)</li> | ||
| 3627 | <li><b><a name="pdf-LUA_OPMOD"><code>LUA_OPMOD</code></a>: </b> performs modulo (<code>%</code>)</li> | ||
| 3628 | <li><b><a name="pdf-LUA_OPPOW"><code>LUA_OPPOW</code></a>: </b> performs exponentiation (<code>^</code>)</li> | ||
| 3629 | <li><b><a name="pdf-LUA_OPUNM"><code>LUA_OPUNM</code></a>: </b> performs mathematical negation (unary <code>-</code>)</li> | ||
| 3630 | <li><b><a name="pdf-LUA_OPBNOT"><code>LUA_OPBNOT</code></a>: </b> performs bitwise NOT (<code>~</code>)</li> | ||
| 3631 | <li><b><a name="pdf-LUA_OPBAND"><code>LUA_OPBAND</code></a>: </b> performs bitwise AND (<code>&</code>)</li> | ||
| 3632 | <li><b><a name="pdf-LUA_OPBOR"><code>LUA_OPBOR</code></a>: </b> performs bitwise OR (<code>|</code>)</li> | ||
| 3633 | <li><b><a name="pdf-LUA_OPBXOR"><code>LUA_OPBXOR</code></a>: </b> performs bitwise exclusive OR (<code>~</code>)</li> | ||
| 3634 | <li><b><a name="pdf-LUA_OPSHL"><code>LUA_OPSHL</code></a>: </b> performs left shift (<code><<</code>)</li> | ||
| 3635 | <li><b><a name="pdf-LUA_OPSHR"><code>LUA_OPSHR</code></a>: </b> performs right shift (<code>>></code>)</li> | ||
| 3636 | |||
| 3637 | </ul> | ||
| 3638 | |||
| 3639 | |||
| 3640 | |||
| 3641 | |||
| 3642 | <hr><h3><a name="lua_atpanic"><code>lua_atpanic</code></a></h3><p> | ||
| 3643 | <span class="apii">[-0, +0, –]</span> | ||
| 3644 | <pre>lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf);</pre> | ||
| 3645 | |||
| 3646 | <p> | ||
| 3647 | Sets a new panic function and returns the old one (see <a href="#4.4">§4.4</a>). | ||
| 3648 | |||
| 3649 | |||
| 3650 | |||
| 3651 | |||
| 3652 | |||
| 3653 | <hr><h3><a name="lua_call"><code>lua_call</code></a></h3><p> | ||
| 3654 | <span class="apii">[-(nargs+1), +nresults, <em>e</em>]</span> | ||
| 3655 | <pre>void lua_call (lua_State *L, int nargs, int nresults);</pre> | ||
| 3656 | |||
| 3657 | <p> | ||
| 3658 | Calls a function. | ||
| 3659 | Like regular Lua calls, | ||
| 3660 | <code>lua_call</code> respects the <code>__call</code> metamethod. | ||
| 3661 | So, here the word "function" | ||
| 3662 | means any callable value. | ||
| 3663 | |||
| 3664 | |||
| 3665 | <p> | ||
| 3666 | To do a call you must use the following protocol: | ||
| 3667 | first, the function to be called is pushed onto the stack; | ||
| 3668 | then, the arguments to the call are pushed | ||
| 3669 | in direct order; | ||
| 3670 | that is, the first argument is pushed first. | ||
| 3671 | Finally you call <a href="#lua_call"><code>lua_call</code></a>; | ||
| 3672 | <code>nargs</code> is the number of arguments that you pushed onto the stack. | ||
| 3673 | When the function returns, | ||
| 3674 | all arguments and the function value are popped | ||
| 3675 | and the call results are pushed onto the stack. | ||
| 3676 | The number of results is adjusted to <code>nresults</code>, | ||
| 3677 | unless <code>nresults</code> is <a name="pdf-LUA_MULTRET"><code>LUA_MULTRET</code></a>. | ||
| 3678 | In this case, all results from the function are pushed; | ||
| 3679 | Lua takes care that the returned values fit into the stack space, | ||
| 3680 | but it does not ensure any extra space in the stack. | ||
| 3681 | The function results are pushed onto the stack in direct order | ||
| 3682 | (the first result is pushed first), | ||
| 3683 | so that after the call the last result is on the top of the stack. | ||
| 3684 | |||
| 3685 | |||
| 3686 | <p> | ||
| 3687 | Any error while calling and running the function is propagated upwards | ||
| 3688 | (with a <code>longjmp</code>). | ||
| 3689 | |||
| 3690 | |||
| 3691 | <p> | ||
| 3692 | The following example shows how the host program can do the | ||
| 3693 | equivalent to this Lua code: | ||
| 3694 | |||
| 3695 | <pre> | ||
| 3696 | a = f("how", t.x, 14) | ||
| 3697 | </pre><p> | ||
| 3698 | Here it is in C: | ||
| 3699 | |||
| 3700 | <pre> | ||
| 3701 | lua_getglobal(L, "f"); /* function to be called */ | ||
| 3702 | lua_pushliteral(L, "how"); /* 1st argument */ | ||
| 3703 | lua_getglobal(L, "t"); /* table to be indexed */ | ||
| 3704 | lua_getfield(L, -1, "x"); /* push result of t.x (2nd arg) */ | ||
| 3705 | lua_remove(L, -2); /* remove 't' from the stack */ | ||
| 3706 | lua_pushinteger(L, 14); /* 3rd argument */ | ||
| 3707 | lua_call(L, 3, 1); /* call 'f' with 3 arguments and 1 result */ | ||
| 3708 | lua_setglobal(L, "a"); /* set global 'a' */ | ||
| 3709 | </pre><p> | ||
| 3710 | Note that the code above is <em>balanced</em>: | ||
| 3711 | at its end, the stack is back to its original configuration. | ||
| 3712 | This is considered good programming practice. | ||
| 3713 | |||
| 3714 | |||
| 3715 | |||
| 3716 | |||
| 3717 | |||
| 3718 | <hr><h3><a name="lua_callk"><code>lua_callk</code></a></h3><p> | ||
| 3719 | <span class="apii">[-(nargs + 1), +nresults, <em>e</em>]</span> | ||
| 3720 | <pre>void lua_callk (lua_State *L, | ||
| 3721 | int nargs, | ||
| 3722 | int nresults, | ||
| 3723 | lua_KContext ctx, | ||
| 3724 | lua_KFunction k);</pre> | ||
| 3725 | |||
| 3726 | <p> | ||
| 3727 | This function behaves exactly like <a href="#lua_call"><code>lua_call</code></a>, | ||
| 3728 | but allows the called function to yield (see <a href="#4.5">§4.5</a>). | ||
| 3729 | |||
| 3730 | |||
| 3731 | |||
| 3732 | |||
| 3733 | |||
| 3734 | <hr><h3><a name="lua_CFunction"><code>lua_CFunction</code></a></h3> | ||
| 3735 | <pre>typedef int (*lua_CFunction) (lua_State *L);</pre> | ||
| 3736 | |||
| 3737 | <p> | ||
| 3738 | Type for C functions. | ||
| 3739 | |||
| 3740 | |||
| 3741 | <p> | ||
| 3742 | In order to communicate properly with Lua, | ||
| 3743 | a C function must use the following protocol, | ||
| 3744 | which defines the way parameters and results are passed: | ||
| 3745 | a C function receives its arguments from Lua in its stack | ||
| 3746 | in direct order (the first argument is pushed first). | ||
| 3747 | So, when the function starts, | ||
| 3748 | <code>lua_gettop(L)</code> returns the number of arguments received by the function. | ||
| 3749 | The first argument (if any) is at index 1 | ||
| 3750 | and its last argument is at index <code>lua_gettop(L)</code>. | ||
| 3751 | To return values to Lua, a C function just pushes them onto the stack, | ||
| 3752 | in direct order (the first result is pushed first), | ||
| 3753 | and returns in C the number of results. | ||
| 3754 | Any other value in the stack below the results will be properly | ||
| 3755 | discarded by Lua. | ||
| 3756 | Like a Lua function, a C function called by Lua can also return | ||
| 3757 | many results. | ||
| 3758 | |||
| 3759 | |||
| 3760 | <p> | ||
| 3761 | As an example, the following function receives a variable number | ||
| 3762 | of numeric arguments and returns their average and their sum: | ||
| 3763 | |||
| 3764 | <pre> | ||
| 3765 | static int foo (lua_State *L) { | ||
| 3766 | int n = lua_gettop(L); /* number of arguments */ | ||
| 3767 | lua_Number sum = 0.0; | ||
| 3768 | int i; | ||
| 3769 | for (i = 1; i <= n; i++) { | ||
| 3770 | if (!lua_isnumber(L, i)) { | ||
| 3771 | lua_pushliteral(L, "incorrect argument"); | ||
| 3772 | lua_error(L); | ||
| 3773 | } | ||
| 3774 | sum += lua_tonumber(L, i); | ||
| 3775 | } | ||
| 3776 | lua_pushnumber(L, sum/n); /* first result */ | ||
| 3777 | lua_pushnumber(L, sum); /* second result */ | ||
| 3778 | return 2; /* number of results */ | ||
| 3779 | } | ||
| 3780 | </pre> | ||
| 3781 | |||
| 3782 | |||
| 3783 | |||
| 3784 | |||
| 3785 | <hr><h3><a name="lua_checkstack"><code>lua_checkstack</code></a></h3><p> | ||
| 3786 | <span class="apii">[-0, +0, –]</span> | ||
| 3787 | <pre>int lua_checkstack (lua_State *L, int n);</pre> | ||
| 3788 | |||
| 3789 | <p> | ||
| 3790 | Ensures that the stack has space for at least <code>n</code> extra elements, | ||
| 3791 | that is, that you can safely push up to <code>n</code> values into it. | ||
| 3792 | It returns false if it cannot fulfill the request, | ||
| 3793 | either because it would cause the stack | ||
| 3794 | to be greater than a fixed maximum size | ||
| 3795 | (typically at least several thousand elements) or | ||
| 3796 | because it cannot allocate memory for the extra space. | ||
| 3797 | This function never shrinks the stack; | ||
| 3798 | if the stack already has space for the extra elements, | ||
| 3799 | it is left unchanged. | ||
| 3800 | |||
| 3801 | |||
| 3802 | |||
| 3803 | |||
| 3804 | |||
| 3805 | <hr><h3><a name="lua_close"><code>lua_close</code></a></h3><p> | ||
| 3806 | <span class="apii">[-0, +0, –]</span> | ||
| 3807 | <pre>void lua_close (lua_State *L);</pre> | ||
| 3808 | |||
| 3809 | <p> | ||
| 3810 | Close all active to-be-closed variables in the main thread, | ||
| 3811 | release all objects in the given Lua state | ||
| 3812 | (calling the corresponding garbage-collection metamethods, if any), | ||
| 3813 | and frees all dynamic memory used by this state. | ||
| 3814 | |||
| 3815 | |||
| 3816 | <p> | ||
| 3817 | On several platforms, you may not need to call this function, | ||
| 3818 | because all resources are naturally released when the host program ends. | ||
| 3819 | On the other hand, long-running programs that create multiple states, | ||
| 3820 | such as daemons or web servers, | ||
| 3821 | will probably need to close states as soon as they are not needed. | ||
| 3822 | |||
| 3823 | |||
| 3824 | |||
| 3825 | |||
| 3826 | |||
| 3827 | <hr><h3><a name="lua_closeslot"><code>lua_closeslot</code></a></h3><p> | ||
| 3828 | <span class="apii">[-0, +0, <em>e</em>]</span> | ||
| 3829 | <pre>void lua_closeslot (lua_State *L, int index);</pre> | ||
| 3830 | |||
| 3831 | <p> | ||
| 3832 | Close the to-be-closed slot at the given index and set its value to <b>nil</b>. | ||
| 3833 | The index must be the last index previously marked to be closed | ||
| 3834 | (see <a href="#lua_toclose"><code>lua_toclose</code></a>) that is still active (that is, not closed yet). | ||
| 3835 | |||
| 3836 | |||
| 3837 | <p> | ||
| 3838 | A <code>__close</code> metamethod cannot yield | ||
| 3839 | when called through this function. | ||
| 3840 | |||
| 3841 | |||
| 3842 | <p> | ||
| 3843 | (This function was introduced in release 5.4.3.) | ||
| 3844 | |||
| 3845 | |||
| 3846 | |||
| 3847 | |||
| 3848 | |||
| 3849 | <hr><h3><a name="lua_closethread"><code>lua_closethread</code></a></h3><p> | ||
| 3850 | <span class="apii">[-0, +?, –]</span> | ||
| 3851 | <pre>int lua_closethread (lua_State *L, lua_State *from);</pre> | ||
| 3852 | |||
| 3853 | <p> | ||
| 3854 | Resets a thread, cleaning its call stack and closing all pending | ||
| 3855 | to-be-closed variables. | ||
| 3856 | Returns a status code: | ||
| 3857 | <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> for no errors in the thread | ||
| 3858 | (either the original error that stopped the thread or | ||
| 3859 | errors in closing methods), | ||
| 3860 | or an error status otherwise. | ||
| 3861 | In case of error, | ||
| 3862 | leaves the error object on the top of the stack. | ||
| 3863 | |||
| 3864 | |||
| 3865 | <p> | ||
| 3866 | The parameter <code>from</code> represents the coroutine that is resetting <code>L</code>. | ||
| 3867 | If there is no such coroutine, | ||
| 3868 | this parameter can be <code>NULL</code>. | ||
| 3869 | |||
| 3870 | |||
| 3871 | <p> | ||
| 3872 | (This function was introduced in release 5.4.6.) | ||
| 3873 | |||
| 3874 | |||
| 3875 | |||
| 3876 | |||
| 3877 | |||
| 3878 | <hr><h3><a name="lua_compare"><code>lua_compare</code></a></h3><p> | ||
| 3879 | <span class="apii">[-0, +0, <em>e</em>]</span> | ||
| 3880 | <pre>int lua_compare (lua_State *L, int index1, int index2, int op);</pre> | ||
| 3881 | |||
| 3882 | <p> | ||
| 3883 | Compares two Lua values. | ||
| 3884 | Returns 1 if the value at index <code>index1</code> satisfies <code>op</code> | ||
| 3885 | when compared with the value at index <code>index2</code>, | ||
| 3886 | following the semantics of the corresponding Lua operator | ||
| 3887 | (that is, it may call metamethods). | ||
| 3888 | Otherwise returns 0. | ||
| 3889 | Also returns 0 if any of the indices is not valid. | ||
| 3890 | |||
| 3891 | |||
| 3892 | <p> | ||
| 3893 | The value of <code>op</code> must be one of the following constants: | ||
| 3894 | |||
| 3895 | <ul> | ||
| 3896 | |||
| 3897 | <li><b><a name="pdf-LUA_OPEQ"><code>LUA_OPEQ</code></a>: </b> compares for equality (<code>==</code>)</li> | ||
| 3898 | <li><b><a name="pdf-LUA_OPLT"><code>LUA_OPLT</code></a>: </b> compares for less than (<code><</code>)</li> | ||
| 3899 | <li><b><a name="pdf-LUA_OPLE"><code>LUA_OPLE</code></a>: </b> compares for less or equal (<code><=</code>)</li> | ||
| 3900 | |||
| 3901 | </ul> | ||
| 3902 | |||
| 3903 | |||
| 3904 | |||
| 3905 | |||
| 3906 | <hr><h3><a name="lua_concat"><code>lua_concat</code></a></h3><p> | ||
| 3907 | <span class="apii">[-n, +1, <em>e</em>]</span> | ||
| 3908 | <pre>void lua_concat (lua_State *L, int n);</pre> | ||
| 3909 | |||
| 3910 | <p> | ||
| 3911 | Concatenates the <code>n</code> values at the top of the stack, | ||
| 3912 | pops them, and leaves the result on the top. | ||
| 3913 | If <code>n</code> is 1, the result is the single value on the stack | ||
| 3914 | (that is, the function does nothing); | ||
| 3915 | if <code>n</code> is 0, the result is the empty string. | ||
| 3916 | Concatenation is performed following the usual semantics of Lua | ||
| 3917 | (see <a href="#3.4.6">§3.4.6</a>). | ||
| 3918 | |||
| 3919 | |||
| 3920 | |||
| 3921 | |||
| 3922 | |||
| 3923 | <hr><h3><a name="lua_copy"><code>lua_copy</code></a></h3><p> | ||
| 3924 | <span class="apii">[-0, +0, –]</span> | ||
| 3925 | <pre>void lua_copy (lua_State *L, int fromidx, int toidx);</pre> | ||
| 3926 | |||
| 3927 | <p> | ||
| 3928 | Copies the element at index <code>fromidx</code> | ||
| 3929 | into the valid index <code>toidx</code>, | ||
| 3930 | replacing the value at that position. | ||
| 3931 | Values at other positions are not affected. | ||
| 3932 | |||
| 3933 | |||
| 3934 | |||
| 3935 | |||
| 3936 | |||
| 3937 | <hr><h3><a name="lua_createtable"><code>lua_createtable</code></a></h3><p> | ||
| 3938 | <span class="apii">[-0, +1, <em>m</em>]</span> | ||
| 3939 | <pre>void lua_createtable (lua_State *L, int narr, int nrec);</pre> | ||
| 3940 | |||
| 3941 | <p> | ||
| 3942 | Creates a new empty table and pushes it onto the stack. | ||
| 3943 | Parameter <code>narr</code> is a hint for how many elements the table | ||
| 3944 | will have as a sequence; | ||
| 3945 | parameter <code>nrec</code> is a hint for how many other elements | ||
| 3946 | the table will have. | ||
| 3947 | Lua may use these hints to preallocate memory for the new table. | ||
| 3948 | This preallocation may help performance when you know in advance | ||
| 3949 | how many elements the table will have. | ||
| 3950 | Otherwise you can use the function <a href="#lua_newtable"><code>lua_newtable</code></a>. | ||
| 3951 | |||
| 3952 | |||
| 3953 | |||
| 3954 | |||
| 3955 | |||
| 3956 | <hr><h3><a name="lua_dump"><code>lua_dump</code></a></h3><p> | ||
| 3957 | <span class="apii">[-0, +0, –]</span> | ||
| 3958 | <pre>int lua_dump (lua_State *L, | ||
| 3959 | lua_Writer writer, | ||
| 3960 | void *data, | ||
| 3961 | int strip);</pre> | ||
| 3962 | |||
| 3963 | <p> | ||
| 3964 | Dumps a function as a binary chunk. | ||
| 3965 | Receives a Lua function on the top of the stack | ||
| 3966 | and produces a binary chunk that, | ||
| 3967 | if loaded again, | ||
| 3968 | results in a function equivalent to the one dumped. | ||
| 3969 | As it produces parts of the chunk, | ||
| 3970 | <a href="#lua_dump"><code>lua_dump</code></a> calls function <code>writer</code> (see <a href="#lua_Writer"><code>lua_Writer</code></a>) | ||
| 3971 | with the given <code>data</code> | ||
| 3972 | to write them. | ||
| 3973 | |||
| 3974 | |||
| 3975 | <p> | ||
| 3976 | If <code>strip</code> is true, | ||
| 3977 | the binary representation may not include all debug information | ||
| 3978 | about the function, | ||
| 3979 | to save space. | ||
| 3980 | |||
| 3981 | |||
| 3982 | <p> | ||
| 3983 | The value returned is the error code returned by the last | ||
| 3984 | call to the writer; | ||
| 3985 | 0 means no errors. | ||
| 3986 | |||
| 3987 | |||
| 3988 | <p> | ||
| 3989 | This function does not pop the Lua function from the stack. | ||
| 3990 | |||
| 3991 | |||
| 3992 | |||
| 3993 | |||
| 3994 | |||
| 3995 | <hr><h3><a name="lua_error"><code>lua_error</code></a></h3><p> | ||
| 3996 | <span class="apii">[-1, +0, <em>v</em>]</span> | ||
| 3997 | <pre>int lua_error (lua_State *L);</pre> | ||
| 3998 | |||
| 3999 | <p> | ||
| 4000 | Raises a Lua error, | ||
| 4001 | using the value on the top of the stack as the error object. | ||
| 4002 | This function does a long jump, | ||
| 4003 | and therefore never returns | ||
| 4004 | (see <a href="#luaL_error"><code>luaL_error</code></a>). | ||
| 4005 | |||
| 4006 | |||
| 4007 | |||
| 4008 | |||
| 4009 | |||
| 4010 | <hr><h3><a name="lua_gc"><code>lua_gc</code></a></h3><p> | ||
| 4011 | <span class="apii">[-0, +0, –]</span> | ||
| 4012 | <pre>int lua_gc (lua_State *L, int what, ...);</pre> | ||
| 4013 | |||
| 4014 | <p> | ||
| 4015 | Controls the garbage collector. | ||
| 4016 | |||
| 4017 | |||
| 4018 | <p> | ||
| 4019 | This function performs several tasks, | ||
| 4020 | according to the value of the parameter <code>what</code>. | ||
| 4021 | For options that need extra arguments, | ||
| 4022 | they are listed after the option. | ||
| 4023 | |||
| 4024 | <ul> | ||
| 4025 | |||
| 4026 | <li><b><code>LUA_GCCOLLECT</code>: </b> | ||
| 4027 | Performs a full garbage-collection cycle. | ||
| 4028 | </li> | ||
| 4029 | |||
| 4030 | <li><b><code>LUA_GCSTOP</code>: </b> | ||
| 4031 | Stops the garbage collector. | ||
| 4032 | </li> | ||
| 4033 | |||
| 4034 | <li><b><code>LUA_GCRESTART</code>: </b> | ||
| 4035 | Restarts the garbage collector. | ||
| 4036 | </li> | ||
| 4037 | |||
| 4038 | <li><b><code>LUA_GCCOUNT</code>: </b> | ||
| 4039 | Returns the current amount of memory (in Kbytes) in use by Lua. | ||
| 4040 | </li> | ||
| 4041 | |||
| 4042 | <li><b><code>LUA_GCCOUNTB</code>: </b> | ||
| 4043 | Returns the remainder of dividing the current amount of bytes of | ||
| 4044 | memory in use by Lua by 1024. | ||
| 4045 | </li> | ||
| 4046 | |||
| 4047 | <li><b><code>LUA_GCSTEP</code> <code>(int stepsize)</code>: </b> | ||
| 4048 | Performs an incremental step of garbage collection, | ||
| 4049 | corresponding to the allocation of <code>stepsize</code> Kbytes. | ||
| 4050 | </li> | ||
| 4051 | |||
| 4052 | <li><b><code>LUA_GCISRUNNING</code>: </b> | ||
| 4053 | Returns a boolean that tells whether the collector is running | ||
| 4054 | (i.e., not stopped). | ||
| 4055 | </li> | ||
| 4056 | |||
| 4057 | <li><b><code>LUA_GCINC</code> (int pause, int stepmul, stepsize): </b> | ||
| 4058 | Changes the collector to incremental mode | ||
| 4059 | with the given parameters (see <a href="#2.5.1">§2.5.1</a>). | ||
| 4060 | Returns the previous mode (<code>LUA_GCGEN</code> or <code>LUA_GCINC</code>). | ||
| 4061 | </li> | ||
| 4062 | |||
| 4063 | <li><b><code>LUA_GCGEN</code> (int minormul, int majormul): </b> | ||
| 4064 | Changes the collector to generational mode | ||
| 4065 | with the given parameters (see <a href="#2.5.2">§2.5.2</a>). | ||
| 4066 | Returns the previous mode (<code>LUA_GCGEN</code> or <code>LUA_GCINC</code>). | ||
| 4067 | </li> | ||
| 4068 | |||
| 4069 | </ul><p> | ||
| 4070 | For more details about these options, | ||
| 4071 | see <a href="#pdf-collectgarbage"><code>collectgarbage</code></a>. | ||
| 4072 | |||
| 4073 | |||
| 4074 | <p> | ||
| 4075 | This function should not be called by a finalizer. | ||
| 4076 | |||
| 4077 | |||
| 4078 | |||
| 4079 | |||
| 4080 | |||
| 4081 | <hr><h3><a name="lua_getallocf"><code>lua_getallocf</code></a></h3><p> | ||
| 4082 | <span class="apii">[-0, +0, –]</span> | ||
| 4083 | <pre>lua_Alloc lua_getallocf (lua_State *L, void **ud);</pre> | ||
| 4084 | |||
| 4085 | <p> | ||
| 4086 | Returns the memory-allocation function of a given state. | ||
| 4087 | If <code>ud</code> is not <code>NULL</code>, Lua stores in <code>*ud</code> the | ||
| 4088 | opaque pointer given when the memory-allocator function was set. | ||
| 4089 | |||
| 4090 | |||
| 4091 | |||
| 4092 | |||
| 4093 | |||
| 4094 | <hr><h3><a name="lua_getfield"><code>lua_getfield</code></a></h3><p> | ||
| 4095 | <span class="apii">[-0, +1, <em>e</em>]</span> | ||
| 4096 | <pre>int lua_getfield (lua_State *L, int index, const char *k);</pre> | ||
| 4097 | |||
| 4098 | <p> | ||
| 4099 | Pushes onto the stack the value <code>t[k]</code>, | ||
| 4100 | where <code>t</code> is the value at the given index. | ||
| 4101 | As in Lua, this function may trigger a metamethod | ||
| 4102 | for the "index" event (see <a href="#2.4">§2.4</a>). | ||
| 4103 | |||
| 4104 | |||
| 4105 | <p> | ||
| 4106 | Returns the type of the pushed value. | ||
| 4107 | |||
| 4108 | |||
| 4109 | |||
| 4110 | |||
| 4111 | |||
| 4112 | <hr><h3><a name="lua_getextraspace"><code>lua_getextraspace</code></a></h3><p> | ||
| 4113 | <span class="apii">[-0, +0, –]</span> | ||
| 4114 | <pre>void *lua_getextraspace (lua_State *L);</pre> | ||
| 4115 | |||
| 4116 | <p> | ||
| 4117 | Returns a pointer to a raw memory area associated with the | ||
| 4118 | given Lua state. | ||
| 4119 | The application can use this area for any purpose; | ||
| 4120 | Lua does not use it for anything. | ||
| 4121 | |||
| 4122 | |||
| 4123 | <p> | ||
| 4124 | Each new thread has this area initialized with a copy | ||
| 4125 | of the area of the main thread. | ||
| 4126 | |||
| 4127 | |||
| 4128 | <p> | ||
| 4129 | By default, this area has the size of a pointer to void, | ||
| 4130 | but you can recompile Lua with a different size for this area. | ||
| 4131 | (See <code>LUA_EXTRASPACE</code> in <code>luaconf.h</code>.) | ||
| 4132 | |||
| 4133 | |||
| 4134 | |||
| 4135 | |||
| 4136 | |||
| 4137 | <hr><h3><a name="lua_getglobal"><code>lua_getglobal</code></a></h3><p> | ||
| 4138 | <span class="apii">[-0, +1, <em>e</em>]</span> | ||
| 4139 | <pre>int lua_getglobal (lua_State *L, const char *name);</pre> | ||
| 4140 | |||
| 4141 | <p> | ||
| 4142 | Pushes onto the stack the value of the global <code>name</code>. | ||
| 4143 | Returns the type of that value. | ||
| 4144 | |||
| 4145 | |||
| 4146 | |||
| 4147 | |||
| 4148 | |||
| 4149 | <hr><h3><a name="lua_geti"><code>lua_geti</code></a></h3><p> | ||
| 4150 | <span class="apii">[-0, +1, <em>e</em>]</span> | ||
| 4151 | <pre>int lua_geti (lua_State *L, int index, lua_Integer i);</pre> | ||
| 4152 | |||
| 4153 | <p> | ||
| 4154 | Pushes onto the stack the value <code>t[i]</code>, | ||
| 4155 | where <code>t</code> is the value at the given index. | ||
| 4156 | As in Lua, this function may trigger a metamethod | ||
| 4157 | for the "index" event (see <a href="#2.4">§2.4</a>). | ||
| 4158 | |||
| 4159 | |||
| 4160 | <p> | ||
| 4161 | Returns the type of the pushed value. | ||
| 4162 | |||
| 4163 | |||
| 4164 | |||
| 4165 | |||
| 4166 | |||
| 4167 | <hr><h3><a name="lua_getmetatable"><code>lua_getmetatable</code></a></h3><p> | ||
| 4168 | <span class="apii">[-0, +(0|1), –]</span> | ||
| 4169 | <pre>int lua_getmetatable (lua_State *L, int index);</pre> | ||
| 4170 | |||
| 4171 | <p> | ||
| 4172 | If the value at the given index has a metatable, | ||
| 4173 | the function pushes that metatable onto the stack and returns 1. | ||
| 4174 | Otherwise, | ||
| 4175 | the function returns 0 and pushes nothing on the stack. | ||
| 4176 | |||
| 4177 | |||
| 4178 | |||
| 4179 | |||
| 4180 | |||
| 4181 | <hr><h3><a name="lua_gettable"><code>lua_gettable</code></a></h3><p> | ||
| 4182 | <span class="apii">[-1, +1, <em>e</em>]</span> | ||
| 4183 | <pre>int lua_gettable (lua_State *L, int index);</pre> | ||
| 4184 | |||
| 4185 | <p> | ||
| 4186 | Pushes onto the stack the value <code>t[k]</code>, | ||
| 4187 | where <code>t</code> is the value at the given index | ||
| 4188 | and <code>k</code> is the value on the top of the stack. | ||
| 4189 | |||
| 4190 | |||
| 4191 | <p> | ||
| 4192 | This function pops the key from the stack, | ||
| 4193 | pushing the resulting value in its place. | ||
| 4194 | As in Lua, this function may trigger a metamethod | ||
| 4195 | for the "index" event (see <a href="#2.4">§2.4</a>). | ||
| 4196 | |||
| 4197 | |||
| 4198 | <p> | ||
| 4199 | Returns the type of the pushed value. | ||
| 4200 | |||
| 4201 | |||
| 4202 | |||
| 4203 | |||
| 4204 | |||
| 4205 | <hr><h3><a name="lua_gettop"><code>lua_gettop</code></a></h3><p> | ||
| 4206 | <span class="apii">[-0, +0, –]</span> | ||
| 4207 | <pre>int lua_gettop (lua_State *L);</pre> | ||
| 4208 | |||
| 4209 | <p> | ||
| 4210 | Returns the index of the top element in the stack. | ||
| 4211 | Because indices start at 1, | ||
| 4212 | this result is equal to the number of elements in the stack; | ||
| 4213 | in particular, 0 means an empty stack. | ||
| 4214 | |||
| 4215 | |||
| 4216 | |||
| 4217 | |||
| 4218 | |||
| 4219 | <hr><h3><a name="lua_getiuservalue"><code>lua_getiuservalue</code></a></h3><p> | ||
| 4220 | <span class="apii">[-0, +1, –]</span> | ||
| 4221 | <pre>int lua_getiuservalue (lua_State *L, int index, int n);</pre> | ||
| 4222 | |||
| 4223 | <p> | ||
| 4224 | Pushes onto the stack the <code>n</code>-th user value associated with the | ||
| 4225 | full userdata at the given index and | ||
| 4226 | returns the type of the pushed value. | ||
| 4227 | |||
| 4228 | |||
| 4229 | <p> | ||
| 4230 | If the userdata does not have that value, | ||
| 4231 | pushes <b>nil</b> and returns <a href="#pdf-LUA_TNONE"><code>LUA_TNONE</code></a>. | ||
| 4232 | |||
| 4233 | |||
| 4234 | |||
| 4235 | |||
| 4236 | |||
| 4237 | <hr><h3><a name="lua_insert"><code>lua_insert</code></a></h3><p> | ||
| 4238 | <span class="apii">[-1, +1, –]</span> | ||
| 4239 | <pre>void lua_insert (lua_State *L, int index);</pre> | ||
| 4240 | |||
| 4241 | <p> | ||
| 4242 | Moves the top element into the given valid index, | ||
| 4243 | shifting up the elements above this index to open space. | ||
| 4244 | This function cannot be called with a pseudo-index, | ||
| 4245 | because a pseudo-index is not an actual stack position. | ||
| 4246 | |||
| 4247 | |||
| 4248 | |||
| 4249 | |||
| 4250 | |||
| 4251 | <hr><h3><a name="lua_Integer"><code>lua_Integer</code></a></h3> | ||
| 4252 | <pre>typedef ... lua_Integer;</pre> | ||
| 4253 | |||
| 4254 | <p> | ||
| 4255 | The type of integers in Lua. | ||
| 4256 | |||
| 4257 | |||
| 4258 | <p> | ||
| 4259 | By default this type is <code>long long</code>, | ||
| 4260 | (usually a 64-bit two-complement integer), | ||
| 4261 | but that can be changed to <code>long</code> or <code>int</code> | ||
| 4262 | (usually a 32-bit two-complement integer). | ||
| 4263 | (See <code>LUA_INT_TYPE</code> in <code>luaconf.h</code>.) | ||
| 4264 | |||
| 4265 | |||
| 4266 | <p> | ||
| 4267 | Lua also defines the constants | ||
| 4268 | <a name="pdf-LUA_MININTEGER"><code>LUA_MININTEGER</code></a> and <a name="pdf-LUA_MAXINTEGER"><code>LUA_MAXINTEGER</code></a>, | ||
| 4269 | with the minimum and the maximum values that fit in this type. | ||
| 4270 | |||
| 4271 | |||
| 4272 | |||
| 4273 | |||
| 4274 | |||
| 4275 | <hr><h3><a name="lua_isboolean"><code>lua_isboolean</code></a></h3><p> | ||
| 4276 | <span class="apii">[-0, +0, –]</span> | ||
| 4277 | <pre>int lua_isboolean (lua_State *L, int index);</pre> | ||
| 4278 | |||
| 4279 | <p> | ||
| 4280 | Returns 1 if the value at the given index is a boolean, | ||
| 4281 | and 0 otherwise. | ||
| 4282 | |||
| 4283 | |||
| 4284 | |||
| 4285 | |||
| 4286 | |||
| 4287 | <hr><h3><a name="lua_iscfunction"><code>lua_iscfunction</code></a></h3><p> | ||
| 4288 | <span class="apii">[-0, +0, –]</span> | ||
| 4289 | <pre>int lua_iscfunction (lua_State *L, int index);</pre> | ||
| 4290 | |||
| 4291 | <p> | ||
| 4292 | Returns 1 if the value at the given index is a C function, | ||
| 4293 | and 0 otherwise. | ||
| 4294 | |||
| 4295 | |||
| 4296 | |||
| 4297 | |||
| 4298 | |||
| 4299 | <hr><h3><a name="lua_isfunction"><code>lua_isfunction</code></a></h3><p> | ||
| 4300 | <span class="apii">[-0, +0, –]</span> | ||
| 4301 | <pre>int lua_isfunction (lua_State *L, int index);</pre> | ||
| 4302 | |||
| 4303 | <p> | ||
| 4304 | Returns 1 if the value at the given index is a function | ||
| 4305 | (either C or Lua), and 0 otherwise. | ||
| 4306 | |||
| 4307 | |||
| 4308 | |||
| 4309 | |||
| 4310 | |||
| 4311 | <hr><h3><a name="lua_isinteger"><code>lua_isinteger</code></a></h3><p> | ||
| 4312 | <span class="apii">[-0, +0, –]</span> | ||
| 4313 | <pre>int lua_isinteger (lua_State *L, int index);</pre> | ||
| 4314 | |||
| 4315 | <p> | ||
| 4316 | Returns 1 if the value at the given index is an integer | ||
| 4317 | (that is, the value is a number and is represented as an integer), | ||
| 4318 | and 0 otherwise. | ||
| 4319 | |||
| 4320 | |||
| 4321 | |||
| 4322 | |||
| 4323 | |||
| 4324 | <hr><h3><a name="lua_islightuserdata"><code>lua_islightuserdata</code></a></h3><p> | ||
| 4325 | <span class="apii">[-0, +0, –]</span> | ||
| 4326 | <pre>int lua_islightuserdata (lua_State *L, int index);</pre> | ||
| 4327 | |||
| 4328 | <p> | ||
| 4329 | Returns 1 if the value at the given index is a light userdata, | ||
| 4330 | and 0 otherwise. | ||
| 4331 | |||
| 4332 | |||
| 4333 | |||
| 4334 | |||
| 4335 | |||
| 4336 | <hr><h3><a name="lua_isnil"><code>lua_isnil</code></a></h3><p> | ||
| 4337 | <span class="apii">[-0, +0, –]</span> | ||
| 4338 | <pre>int lua_isnil (lua_State *L, int index);</pre> | ||
| 4339 | |||
| 4340 | <p> | ||
| 4341 | Returns 1 if the value at the given index is <b>nil</b>, | ||
| 4342 | and 0 otherwise. | ||
| 4343 | |||
| 4344 | |||
| 4345 | |||
| 4346 | |||
| 4347 | |||
| 4348 | <hr><h3><a name="lua_isnone"><code>lua_isnone</code></a></h3><p> | ||
| 4349 | <span class="apii">[-0, +0, –]</span> | ||
| 4350 | <pre>int lua_isnone (lua_State *L, int index);</pre> | ||
| 4351 | |||
| 4352 | <p> | ||
| 4353 | Returns 1 if the given index is not valid, | ||
| 4354 | and 0 otherwise. | ||
| 4355 | |||
| 4356 | |||
| 4357 | |||
| 4358 | |||
| 4359 | |||
| 4360 | <hr><h3><a name="lua_isnoneornil"><code>lua_isnoneornil</code></a></h3><p> | ||
| 4361 | <span class="apii">[-0, +0, –]</span> | ||
| 4362 | <pre>int lua_isnoneornil (lua_State *L, int index);</pre> | ||
| 4363 | |||
| 4364 | <p> | ||
| 4365 | Returns 1 if the given index is not valid | ||
| 4366 | or if the value at this index is <b>nil</b>, | ||
| 4367 | and 0 otherwise. | ||
| 4368 | |||
| 4369 | |||
| 4370 | |||
| 4371 | |||
| 4372 | |||
| 4373 | <hr><h3><a name="lua_isnumber"><code>lua_isnumber</code></a></h3><p> | ||
| 4374 | <span class="apii">[-0, +0, –]</span> | ||
| 4375 | <pre>int lua_isnumber (lua_State *L, int index);</pre> | ||
| 4376 | |||
| 4377 | <p> | ||
| 4378 | Returns 1 if the value at the given index is a number | ||
| 4379 | or a string convertible to a number, | ||
| 4380 | and 0 otherwise. | ||
| 4381 | |||
| 4382 | |||
| 4383 | |||
| 4384 | |||
| 4385 | |||
| 4386 | <hr><h3><a name="lua_isstring"><code>lua_isstring</code></a></h3><p> | ||
| 4387 | <span class="apii">[-0, +0, –]</span> | ||
| 4388 | <pre>int lua_isstring (lua_State *L, int index);</pre> | ||
| 4389 | |||
| 4390 | <p> | ||
| 4391 | Returns 1 if the value at the given index is a string | ||
| 4392 | or a number (which is always convertible to a string), | ||
| 4393 | and 0 otherwise. | ||
| 4394 | |||
| 4395 | |||
| 4396 | |||
| 4397 | |||
| 4398 | |||
| 4399 | <hr><h3><a name="lua_istable"><code>lua_istable</code></a></h3><p> | ||
| 4400 | <span class="apii">[-0, +0, –]</span> | ||
| 4401 | <pre>int lua_istable (lua_State *L, int index);</pre> | ||
| 4402 | |||
| 4403 | <p> | ||
| 4404 | Returns 1 if the value at the given index is a table, | ||
| 4405 | and 0 otherwise. | ||
| 4406 | |||
| 4407 | |||
| 4408 | |||
| 4409 | |||
| 4410 | |||
| 4411 | <hr><h3><a name="lua_isthread"><code>lua_isthread</code></a></h3><p> | ||
| 4412 | <span class="apii">[-0, +0, –]</span> | ||
| 4413 | <pre>int lua_isthread (lua_State *L, int index);</pre> | ||
| 4414 | |||
| 4415 | <p> | ||
| 4416 | Returns 1 if the value at the given index is a thread, | ||
| 4417 | and 0 otherwise. | ||
| 4418 | |||
| 4419 | |||
| 4420 | |||
| 4421 | |||
| 4422 | |||
| 4423 | <hr><h3><a name="lua_isuserdata"><code>lua_isuserdata</code></a></h3><p> | ||
| 4424 | <span class="apii">[-0, +0, –]</span> | ||
| 4425 | <pre>int lua_isuserdata (lua_State *L, int index);</pre> | ||
| 4426 | |||
| 4427 | <p> | ||
| 4428 | Returns 1 if the value at the given index is a userdata | ||
| 4429 | (either full or light), and 0 otherwise. | ||
| 4430 | |||
| 4431 | |||
| 4432 | |||
| 4433 | |||
| 4434 | |||
| 4435 | <hr><h3><a name="lua_isyieldable"><code>lua_isyieldable</code></a></h3><p> | ||
| 4436 | <span class="apii">[-0, +0, –]</span> | ||
| 4437 | <pre>int lua_isyieldable (lua_State *L);</pre> | ||
| 4438 | |||
| 4439 | <p> | ||
| 4440 | Returns 1 if the given coroutine can yield, | ||
| 4441 | and 0 otherwise. | ||
| 4442 | |||
| 4443 | |||
| 4444 | |||
| 4445 | |||
| 4446 | |||
| 4447 | <hr><h3><a name="lua_KContext"><code>lua_KContext</code></a></h3> | ||
| 4448 | <pre>typedef ... lua_KContext;</pre> | ||
| 4449 | |||
| 4450 | <p> | ||
| 4451 | The type for continuation-function contexts. | ||
| 4452 | It must be a numeric type. | ||
| 4453 | This type is defined as <code>intptr_t</code> | ||
| 4454 | when <code>intptr_t</code> is available, | ||
| 4455 | so that it can store pointers too. | ||
| 4456 | Otherwise, it is defined as <code>ptrdiff_t</code>. | ||
| 4457 | |||
| 4458 | |||
| 4459 | |||
| 4460 | |||
| 4461 | |||
| 4462 | <hr><h3><a name="lua_KFunction"><code>lua_KFunction</code></a></h3> | ||
| 4463 | <pre>typedef int (*lua_KFunction) (lua_State *L, int status, lua_KContext ctx);</pre> | ||
| 4464 | |||
| 4465 | <p> | ||
| 4466 | Type for continuation functions (see <a href="#4.5">§4.5</a>). | ||
| 4467 | |||
| 4468 | |||
| 4469 | |||
| 4470 | |||
| 4471 | |||
| 4472 | <hr><h3><a name="lua_len"><code>lua_len</code></a></h3><p> | ||
| 4473 | <span class="apii">[-0, +1, <em>e</em>]</span> | ||
| 4474 | <pre>void lua_len (lua_State *L, int index);</pre> | ||
| 4475 | |||
| 4476 | <p> | ||
| 4477 | Returns the length of the value at the given index. | ||
| 4478 | It is equivalent to the '<code>#</code>' operator in Lua (see <a href="#3.4.7">§3.4.7</a>) and | ||
| 4479 | may trigger a metamethod for the "length" event (see <a href="#2.4">§2.4</a>). | ||
| 4480 | The result is pushed on the stack. | ||
| 4481 | |||
| 4482 | |||
| 4483 | |||
| 4484 | |||
| 4485 | |||
| 4486 | <hr><h3><a name="lua_load"><code>lua_load</code></a></h3><p> | ||
| 4487 | <span class="apii">[-0, +1, –]</span> | ||
| 4488 | <pre>int lua_load (lua_State *L, | ||
| 4489 | lua_Reader reader, | ||
| 4490 | void *data, | ||
| 4491 | const char *chunkname, | ||
| 4492 | const char *mode);</pre> | ||
| 4493 | |||
| 4494 | <p> | ||
| 4495 | Loads a Lua chunk without running it. | ||
| 4496 | If there are no errors, | ||
| 4497 | <code>lua_load</code> pushes the compiled chunk as a Lua | ||
| 4498 | function on top of the stack. | ||
| 4499 | Otherwise, it pushes an error message. | ||
| 4500 | |||
| 4501 | |||
| 4502 | <p> | ||
| 4503 | The <code>lua_load</code> function uses a user-supplied <code>reader</code> function | ||
| 4504 | to read the chunk (see <a href="#lua_Reader"><code>lua_Reader</code></a>). | ||
| 4505 | The <code>data</code> argument is an opaque value passed to the reader function. | ||
| 4506 | |||
| 4507 | |||
| 4508 | <p> | ||
| 4509 | The <code>chunkname</code> argument gives a name to the chunk, | ||
| 4510 | which is used for error messages and in debug information (see <a href="#4.7">§4.7</a>). | ||
| 4511 | |||
| 4512 | |||
| 4513 | <p> | ||
| 4514 | <code>lua_load</code> automatically detects whether the chunk is text or binary | ||
| 4515 | and loads it accordingly (see program <code>luac</code>). | ||
| 4516 | The string <code>mode</code> works as in function <a href="#pdf-load"><code>load</code></a>, | ||
| 4517 | with the addition that | ||
| 4518 | a <code>NULL</code> value is equivalent to the string "<code>bt</code>". | ||
| 4519 | |||
| 4520 | |||
| 4521 | <p> | ||
| 4522 | <code>lua_load</code> uses the stack internally, | ||
| 4523 | so the reader function must always leave the stack | ||
| 4524 | unmodified when returning. | ||
| 4525 | |||
| 4526 | |||
| 4527 | <p> | ||
| 4528 | <code>lua_load</code> can return | ||
| 4529 | <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>, <a href="#pdf-LUA_ERRSYNTAX"><code>LUA_ERRSYNTAX</code></a>, or <a href="#pdf-LUA_ERRMEM"><code>LUA_ERRMEM</code></a>. | ||
| 4530 | The function may also return other values corresponding to | ||
| 4531 | errors raised by the read function (see <a href="#4.4.1">§4.4.1</a>). | ||
| 4532 | |||
| 4533 | |||
| 4534 | <p> | ||
| 4535 | If the resulting function has upvalues, | ||
| 4536 | its first upvalue is set to the value of the global environment | ||
| 4537 | stored at index <code>LUA_RIDX_GLOBALS</code> in the registry (see <a href="#4.3">§4.3</a>). | ||
| 4538 | When loading main chunks, | ||
| 4539 | this upvalue will be the <code>_ENV</code> variable (see <a href="#2.2">§2.2</a>). | ||
| 4540 | Other upvalues are initialized with <b>nil</b>. | ||
| 4541 | |||
| 4542 | |||
| 4543 | |||
| 4544 | |||
| 4545 | |||
| 4546 | <hr><h3><a name="lua_newstate"><code>lua_newstate</code></a></h3><p> | ||
| 4547 | <span class="apii">[-0, +0, –]</span> | ||
| 4548 | <pre>lua_State *lua_newstate (lua_Alloc f, void *ud);</pre> | ||
| 4549 | |||
| 4550 | <p> | ||
| 4551 | Creates a new independent state and returns its main thread. | ||
| 4552 | Returns <code>NULL</code> if it cannot create the state | ||
| 4553 | (due to lack of memory). | ||
| 4554 | The argument <code>f</code> is the allocator function; | ||
| 4555 | Lua will do all memory allocation for this state | ||
| 4556 | through this function (see <a href="#lua_Alloc"><code>lua_Alloc</code></a>). | ||
| 4557 | The second argument, <code>ud</code>, is an opaque pointer that Lua | ||
| 4558 | passes to the allocator in every call. | ||
| 4559 | |||
| 4560 | |||
| 4561 | |||
| 4562 | |||
| 4563 | |||
| 4564 | <hr><h3><a name="lua_newtable"><code>lua_newtable</code></a></h3><p> | ||
| 4565 | <span class="apii">[-0, +1, <em>m</em>]</span> | ||
| 4566 | <pre>void lua_newtable (lua_State *L);</pre> | ||
| 4567 | |||
| 4568 | <p> | ||
| 4569 | Creates a new empty table and pushes it onto the stack. | ||
| 4570 | It is equivalent to <code>lua_createtable(L, 0, 0)</code>. | ||
| 4571 | |||
| 4572 | |||
| 4573 | |||
| 4574 | |||
| 4575 | |||
| 4576 | <hr><h3><a name="lua_newthread"><code>lua_newthread</code></a></h3><p> | ||
| 4577 | <span class="apii">[-0, +1, <em>m</em>]</span> | ||
| 4578 | <pre>lua_State *lua_newthread (lua_State *L);</pre> | ||
| 4579 | |||
| 4580 | <p> | ||
| 4581 | Creates a new thread, pushes it on the stack, | ||
| 4582 | and returns a pointer to a <a href="#lua_State"><code>lua_State</code></a> that represents this new thread. | ||
| 4583 | The new thread returned by this function shares with the original thread | ||
| 4584 | its global environment, | ||
| 4585 | but has an independent execution stack. | ||
| 4586 | |||
| 4587 | |||
| 4588 | <p> | ||
| 4589 | Threads are subject to garbage collection, | ||
| 4590 | like any Lua object. | ||
| 4591 | |||
| 4592 | |||
| 4593 | |||
| 4594 | |||
| 4595 | |||
| 4596 | <hr><h3><a name="lua_newuserdatauv"><code>lua_newuserdatauv</code></a></h3><p> | ||
| 4597 | <span class="apii">[-0, +1, <em>m</em>]</span> | ||
| 4598 | <pre>void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue);</pre> | ||
| 4599 | |||
| 4600 | <p> | ||
| 4601 | This function creates and pushes on the stack a new full userdata, | ||
| 4602 | with <code>nuvalue</code> associated Lua values, called <code>user values</code>, | ||
| 4603 | plus an associated block of raw memory with <code>size</code> bytes. | ||
| 4604 | (The user values can be set and read with the functions | ||
| 4605 | <a href="#lua_setiuservalue"><code>lua_setiuservalue</code></a> and <a href="#lua_getiuservalue"><code>lua_getiuservalue</code></a>.) | ||
| 4606 | |||
| 4607 | |||
| 4608 | <p> | ||
| 4609 | The function returns the address of the block of memory. | ||
| 4610 | Lua ensures that this address is valid as long as | ||
| 4611 | the corresponding userdata is alive (see <a href="#2.5">§2.5</a>). | ||
| 4612 | Moreover, if the userdata is marked for finalization (see <a href="#2.5.3">§2.5.3</a>), | ||
| 4613 | its address is valid at least until the call to its finalizer. | ||
| 4614 | |||
| 4615 | |||
| 4616 | |||
| 4617 | |||
| 4618 | |||
| 4619 | <hr><h3><a name="lua_next"><code>lua_next</code></a></h3><p> | ||
| 4620 | <span class="apii">[-1, +(2|0), <em>v</em>]</span> | ||
| 4621 | <pre>int lua_next (lua_State *L, int index);</pre> | ||
| 4622 | |||
| 4623 | <p> | ||
| 4624 | Pops a key from the stack, | ||
| 4625 | and pushes a key–value pair from the table at the given index, | ||
| 4626 | the "next" pair after the given key. | ||
| 4627 | If there are no more elements in the table, | ||
| 4628 | then <a href="#lua_next"><code>lua_next</code></a> returns 0 and pushes nothing. | ||
| 4629 | |||
| 4630 | |||
| 4631 | <p> | ||
| 4632 | A typical table traversal looks like this: | ||
| 4633 | |||
| 4634 | <pre> | ||
| 4635 | /* table is in the stack at index 't' */ | ||
| 4636 | lua_pushnil(L); /* first key */ | ||
| 4637 | while (lua_next(L, t) != 0) { | ||
| 4638 | /* uses 'key' (at index -2) and 'value' (at index -1) */ | ||
| 4639 | printf("%s - %s\n", | ||
| 4640 | lua_typename(L, lua_type(L, -2)), | ||
| 4641 | lua_typename(L, lua_type(L, -1))); | ||
| 4642 | /* removes 'value'; keeps 'key' for next iteration */ | ||
| 4643 | lua_pop(L, 1); | ||
| 4644 | } | ||
| 4645 | </pre> | ||
| 4646 | |||
| 4647 | <p> | ||
| 4648 | While traversing a table, | ||
| 4649 | avoid calling <a href="#lua_tolstring"><code>lua_tolstring</code></a> directly on a key, | ||
| 4650 | unless you know that the key is actually a string. | ||
| 4651 | Recall that <a href="#lua_tolstring"><code>lua_tolstring</code></a> may change | ||
| 4652 | the value at the given index; | ||
| 4653 | this confuses the next call to <a href="#lua_next"><code>lua_next</code></a>. | ||
| 4654 | |||
| 4655 | |||
| 4656 | <p> | ||
| 4657 | This function may raise an error if the given key | ||
| 4658 | is neither <b>nil</b> nor present in the table. | ||
| 4659 | See function <a href="#pdf-next"><code>next</code></a> for the caveats of modifying | ||
| 4660 | the table during its traversal. | ||
| 4661 | |||
| 4662 | |||
| 4663 | |||
| 4664 | |||
| 4665 | |||
| 4666 | <hr><h3><a name="lua_Number"><code>lua_Number</code></a></h3> | ||
| 4667 | <pre>typedef ... lua_Number;</pre> | ||
| 4668 | |||
| 4669 | <p> | ||
| 4670 | The type of floats in Lua. | ||
| 4671 | |||
| 4672 | |||
| 4673 | <p> | ||
| 4674 | By default this type is double, | ||
| 4675 | but that can be changed to a single float or a long double. | ||
| 4676 | (See <code>LUA_FLOAT_TYPE</code> in <code>luaconf.h</code>.) | ||
| 4677 | |||
| 4678 | |||
| 4679 | |||
| 4680 | |||
| 4681 | |||
| 4682 | <hr><h3><a name="lua_numbertointeger"><code>lua_numbertointeger</code></a></h3> | ||
| 4683 | <pre>int lua_numbertointeger (lua_Number n, lua_Integer *p);</pre> | ||
| 4684 | |||
| 4685 | <p> | ||
| 4686 | Tries to convert a Lua float to a Lua integer; | ||
| 4687 | the float <code>n</code> must have an integral value. | ||
| 4688 | If that value is within the range of Lua integers, | ||
| 4689 | it is converted to an integer and assigned to <code>*p</code>. | ||
| 4690 | The macro results in a boolean indicating whether the | ||
| 4691 | conversion was successful. | ||
| 4692 | (Note that this range test can be tricky to do | ||
| 4693 | correctly without this macro, due to rounding.) | ||
| 4694 | |||
| 4695 | |||
| 4696 | <p> | ||
| 4697 | This macro may evaluate its arguments more than once. | ||
| 4698 | |||
| 4699 | |||
| 4700 | |||
| 4701 | |||
| 4702 | |||
| 4703 | <hr><h3><a name="lua_pcall"><code>lua_pcall</code></a></h3><p> | ||
| 4704 | <span class="apii">[-(nargs + 1), +(nresults|1), –]</span> | ||
| 4705 | <pre>int lua_pcall (lua_State *L, int nargs, int nresults, int msgh);</pre> | ||
| 4706 | |||
| 4707 | <p> | ||
| 4708 | Calls a function (or a callable object) in protected mode. | ||
| 4709 | |||
| 4710 | |||
| 4711 | <p> | ||
| 4712 | Both <code>nargs</code> and <code>nresults</code> have the same meaning as | ||
| 4713 | in <a href="#lua_call"><code>lua_call</code></a>. | ||
| 4714 | If there are no errors during the call, | ||
| 4715 | <a href="#lua_pcall"><code>lua_pcall</code></a> behaves exactly like <a href="#lua_call"><code>lua_call</code></a>. | ||
| 4716 | However, if there is any error, | ||
| 4717 | <a href="#lua_pcall"><code>lua_pcall</code></a> catches it, | ||
| 4718 | pushes a single value on the stack (the error object), | ||
| 4719 | and returns an error code. | ||
| 4720 | Like <a href="#lua_call"><code>lua_call</code></a>, | ||
| 4721 | <a href="#lua_pcall"><code>lua_pcall</code></a> always removes the function | ||
| 4722 | and its arguments from the stack. | ||
| 4723 | |||
| 4724 | |||
| 4725 | <p> | ||
| 4726 | If <code>msgh</code> is 0, | ||
| 4727 | then the error object returned on the stack | ||
| 4728 | is exactly the original error object. | ||
| 4729 | Otherwise, <code>msgh</code> is the stack index of a | ||
| 4730 | <em>message handler</em>. | ||
| 4731 | (This index cannot be a pseudo-index.) | ||
| 4732 | In case of runtime errors, | ||
| 4733 | this handler will be called with the error object | ||
| 4734 | and its return value will be the object | ||
| 4735 | returned on the stack by <a href="#lua_pcall"><code>lua_pcall</code></a>. | ||
| 4736 | |||
| 4737 | |||
| 4738 | <p> | ||
| 4739 | Typically, the message handler is used to add more debug | ||
| 4740 | information to the error object, such as a stack traceback. | ||
| 4741 | Such information cannot be gathered after the return of <a href="#lua_pcall"><code>lua_pcall</code></a>, | ||
| 4742 | since by then the stack has unwound. | ||
| 4743 | |||
| 4744 | |||
| 4745 | <p> | ||
| 4746 | The <a href="#lua_pcall"><code>lua_pcall</code></a> function returns one of the following status codes: | ||
| 4747 | <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>, <a href="#pdf-LUA_ERRRUN"><code>LUA_ERRRUN</code></a>, <a href="#pdf-LUA_ERRMEM"><code>LUA_ERRMEM</code></a>, or <a href="#pdf-LUA_ERRERR"><code>LUA_ERRERR</code></a>. | ||
| 4748 | |||
| 4749 | |||
| 4750 | |||
| 4751 | |||
| 4752 | |||
| 4753 | <hr><h3><a name="lua_pcallk"><code>lua_pcallk</code></a></h3><p> | ||
| 4754 | <span class="apii">[-(nargs + 1), +(nresults|1), –]</span> | ||
| 4755 | <pre>int lua_pcallk (lua_State *L, | ||
| 4756 | int nargs, | ||
| 4757 | int nresults, | ||
| 4758 | int msgh, | ||
| 4759 | lua_KContext ctx, | ||
| 4760 | lua_KFunction k);</pre> | ||
| 4761 | |||
| 4762 | <p> | ||
| 4763 | This function behaves exactly like <a href="#lua_pcall"><code>lua_pcall</code></a>, | ||
| 4764 | except that it allows the called function to yield (see <a href="#4.5">§4.5</a>). | ||
| 4765 | |||
| 4766 | |||
| 4767 | |||
| 4768 | |||
| 4769 | |||
| 4770 | <hr><h3><a name="lua_pop"><code>lua_pop</code></a></h3><p> | ||
| 4771 | <span class="apii">[-n, +0, <em>e</em>]</span> | ||
| 4772 | <pre>void lua_pop (lua_State *L, int n);</pre> | ||
| 4773 | |||
| 4774 | <p> | ||
| 4775 | Pops <code>n</code> elements from the stack. | ||
| 4776 | It is implemented as a macro over <a href="#lua_settop"><code>lua_settop</code></a>. | ||
| 4777 | |||
| 4778 | |||
| 4779 | |||
| 4780 | |||
| 4781 | |||
| 4782 | <hr><h3><a name="lua_pushboolean"><code>lua_pushboolean</code></a></h3><p> | ||
| 4783 | <span class="apii">[-0, +1, –]</span> | ||
| 4784 | <pre>void lua_pushboolean (lua_State *L, int b);</pre> | ||
| 4785 | |||
| 4786 | <p> | ||
| 4787 | Pushes a boolean value with value <code>b</code> onto the stack. | ||
| 4788 | |||
| 4789 | |||
| 4790 | |||
| 4791 | |||
| 4792 | |||
| 4793 | <hr><h3><a name="lua_pushcclosure"><code>lua_pushcclosure</code></a></h3><p> | ||
| 4794 | <span class="apii">[-n, +1, <em>m</em>]</span> | ||
| 4795 | <pre>void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);</pre> | ||
| 4796 | |||
| 4797 | <p> | ||
| 4798 | Pushes a new C closure onto the stack. | ||
| 4799 | This function receives a pointer to a C function | ||
| 4800 | and pushes onto the stack a Lua value of type <code>function</code> that, | ||
| 4801 | when called, invokes the corresponding C function. | ||
| 4802 | The parameter <code>n</code> tells how many upvalues this function will have | ||
| 4803 | (see <a href="#4.2">§4.2</a>). | ||
| 4804 | |||
| 4805 | |||
| 4806 | <p> | ||
| 4807 | Any function to be callable by Lua must | ||
| 4808 | follow the correct protocol to receive its parameters | ||
| 4809 | and return its results (see <a href="#lua_CFunction"><code>lua_CFunction</code></a>). | ||
| 4810 | |||
| 4811 | |||
| 4812 | <p> | ||
| 4813 | When a C function is created, | ||
| 4814 | it is possible to associate some values with it, | ||
| 4815 | the so called upvalues; | ||
| 4816 | these upvalues are then accessible to the function whenever it is called. | ||
| 4817 | This association is called a C closure (see <a href="#4.2">§4.2</a>). | ||
| 4818 | To create a C closure, | ||
| 4819 | first the initial values for its upvalues must be pushed onto the stack. | ||
| 4820 | (When there are multiple upvalues, the first value is pushed first.) | ||
| 4821 | Then <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a> | ||
| 4822 | is called to create and push the C function onto the stack, | ||
| 4823 | with the argument <code>n</code> telling how many values will be | ||
| 4824 | associated with the function. | ||
| 4825 | <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a> also pops these values from the stack. | ||
| 4826 | |||
| 4827 | |||
| 4828 | <p> | ||
| 4829 | The maximum value for <code>n</code> is 255. | ||
| 4830 | |||
| 4831 | |||
| 4832 | <p> | ||
| 4833 | When <code>n</code> is zero, | ||
| 4834 | this function creates a <em>light C function</em>, | ||
| 4835 | which is just a pointer to the C function. | ||
| 4836 | In that case, it never raises a memory error. | ||
| 4837 | |||
| 4838 | |||
| 4839 | |||
| 4840 | |||
| 4841 | |||
| 4842 | <hr><h3><a name="lua_pushcfunction"><code>lua_pushcfunction</code></a></h3><p> | ||
| 4843 | <span class="apii">[-0, +1, –]</span> | ||
| 4844 | <pre>void lua_pushcfunction (lua_State *L, lua_CFunction f);</pre> | ||
| 4845 | |||
| 4846 | <p> | ||
| 4847 | Pushes a C function onto the stack. | ||
| 4848 | This function is equivalent to <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a> with no upvalues. | ||
| 4849 | |||
| 4850 | |||
| 4851 | |||
| 4852 | |||
| 4853 | |||
| 4854 | <hr><h3><a name="lua_pushfstring"><code>lua_pushfstring</code></a></h3><p> | ||
| 4855 | <span class="apii">[-0, +1, <em>v</em>]</span> | ||
| 4856 | <pre>const char *lua_pushfstring (lua_State *L, const char *fmt, ...);</pre> | ||
| 4857 | |||
| 4858 | <p> | ||
| 4859 | Pushes onto the stack a formatted string | ||
| 4860 | and returns a pointer to this string (see <a href="#4.1.3">§4.1.3</a>). | ||
| 4861 | It is similar to the ISO C function <code>sprintf</code>, | ||
| 4862 | but has two important differences. | ||
| 4863 | First, | ||
| 4864 | you do not have to allocate space for the result; | ||
| 4865 | the result is a Lua string and Lua takes care of memory allocation | ||
| 4866 | (and deallocation, through garbage collection). | ||
| 4867 | Second, | ||
| 4868 | the conversion specifiers are quite restricted. | ||
| 4869 | There are no flags, widths, or precisions. | ||
| 4870 | The conversion specifiers can only be | ||
| 4871 | '<code>%%</code>' (inserts the character '<code>%</code>'), | ||
| 4872 | '<code>%s</code>' (inserts a zero-terminated string, with no size restrictions), | ||
| 4873 | '<code>%f</code>' (inserts a <a href="#lua_Number"><code>lua_Number</code></a>), | ||
| 4874 | '<code>%I</code>' (inserts a <a href="#lua_Integer"><code>lua_Integer</code></a>), | ||
| 4875 | '<code>%p</code>' (inserts a pointer), | ||
| 4876 | '<code>%d</code>' (inserts an <code>int</code>), | ||
| 4877 | '<code>%c</code>' (inserts an <code>int</code> as a one-byte character), and | ||
| 4878 | '<code>%U</code>' (inserts a <code>long int</code> as a UTF-8 byte sequence). | ||
| 4879 | |||
| 4880 | |||
| 4881 | <p> | ||
| 4882 | This function may raise errors due to memory overflow | ||
| 4883 | or an invalid conversion specifier. | ||
| 4884 | |||
| 4885 | |||
| 4886 | |||
| 4887 | |||
| 4888 | |||
| 4889 | <hr><h3><a name="lua_pushglobaltable"><code>lua_pushglobaltable</code></a></h3><p> | ||
| 4890 | <span class="apii">[-0, +1, –]</span> | ||
| 4891 | <pre>void lua_pushglobaltable (lua_State *L);</pre> | ||
| 4892 | |||
| 4893 | <p> | ||
| 4894 | Pushes the global environment onto the stack. | ||
| 4895 | |||
| 4896 | |||
| 4897 | |||
| 4898 | |||
| 4899 | |||
| 4900 | <hr><h3><a name="lua_pushinteger"><code>lua_pushinteger</code></a></h3><p> | ||
| 4901 | <span class="apii">[-0, +1, –]</span> | ||
| 4902 | <pre>void lua_pushinteger (lua_State *L, lua_Integer n);</pre> | ||
| 4903 | |||
| 4904 | <p> | ||
| 4905 | Pushes an integer with value <code>n</code> onto the stack. | ||
| 4906 | |||
| 4907 | |||
| 4908 | |||
| 4909 | |||
| 4910 | |||
| 4911 | <hr><h3><a name="lua_pushlightuserdata"><code>lua_pushlightuserdata</code></a></h3><p> | ||
| 4912 | <span class="apii">[-0, +1, –]</span> | ||
| 4913 | <pre>void lua_pushlightuserdata (lua_State *L, void *p);</pre> | ||
| 4914 | |||
| 4915 | <p> | ||
| 4916 | Pushes a light userdata onto the stack. | ||
| 4917 | |||
| 4918 | |||
| 4919 | <p> | ||
| 4920 | Userdata represent C values in Lua. | ||
| 4921 | A <em>light userdata</em> represents a pointer, a <code>void*</code>. | ||
| 4922 | It is a value (like a number): | ||
| 4923 | you do not create it, it has no individual metatable, | ||
| 4924 | and it is not collected (as it was never created). | ||
| 4925 | A light userdata is equal to "any" | ||
| 4926 | light userdata with the same C address. | ||
| 4927 | |||
| 4928 | |||
| 4929 | |||
| 4930 | |||
| 4931 | |||
| 4932 | <hr><h3><a name="lua_pushliteral"><code>lua_pushliteral</code></a></h3><p> | ||
| 4933 | <span class="apii">[-0, +1, <em>m</em>]</span> | ||
| 4934 | <pre>const char *lua_pushliteral (lua_State *L, const char *s);</pre> | ||
| 4935 | |||
| 4936 | <p> | ||
| 4937 | This macro is equivalent to <a href="#lua_pushstring"><code>lua_pushstring</code></a>, | ||
| 4938 | but should be used only when <code>s</code> is a literal string. | ||
| 4939 | (Lua may optimize this case.) | ||
| 4940 | |||
| 4941 | |||
| 4942 | |||
| 4943 | |||
| 4944 | |||
| 4945 | <hr><h3><a name="lua_pushlstring"><code>lua_pushlstring</code></a></h3><p> | ||
| 4946 | <span class="apii">[-0, +1, <em>m</em>]</span> | ||
| 4947 | <pre>const char *lua_pushlstring (lua_State *L, const char *s, size_t len);</pre> | ||
| 4948 | |||
| 4949 | <p> | ||
| 4950 | Pushes the string pointed to by <code>s</code> with size <code>len</code> | ||
| 4951 | onto the stack. | ||
| 4952 | Lua will make or reuse an internal copy of the given string, | ||
| 4953 | so the memory at <code>s</code> can be freed or reused immediately after | ||
| 4954 | the function returns. | ||
| 4955 | The string can contain any binary data, | ||
| 4956 | including embedded zeros. | ||
| 4957 | |||
| 4958 | |||
| 4959 | <p> | ||
| 4960 | Returns a pointer to the internal copy of the string (see <a href="#4.1.3">§4.1.3</a>). | ||
| 4961 | |||
| 4962 | |||
| 4963 | |||
| 4964 | |||
| 4965 | |||
| 4966 | <hr><h3><a name="lua_pushnil"><code>lua_pushnil</code></a></h3><p> | ||
| 4967 | <span class="apii">[-0, +1, –]</span> | ||
| 4968 | <pre>void lua_pushnil (lua_State *L);</pre> | ||
| 4969 | |||
| 4970 | <p> | ||
| 4971 | Pushes a nil value onto the stack. | ||
| 4972 | |||
| 4973 | |||
| 4974 | |||
| 4975 | |||
| 4976 | |||
| 4977 | <hr><h3><a name="lua_pushnumber"><code>lua_pushnumber</code></a></h3><p> | ||
| 4978 | <span class="apii">[-0, +1, –]</span> | ||
| 4979 | <pre>void lua_pushnumber (lua_State *L, lua_Number n);</pre> | ||
| 4980 | |||
| 4981 | <p> | ||
| 4982 | Pushes a float with value <code>n</code> onto the stack. | ||
| 4983 | |||
| 4984 | |||
| 4985 | |||
| 4986 | |||
| 4987 | |||
| 4988 | <hr><h3><a name="lua_pushstring"><code>lua_pushstring</code></a></h3><p> | ||
| 4989 | <span class="apii">[-0, +1, <em>m</em>]</span> | ||
| 4990 | <pre>const char *lua_pushstring (lua_State *L, const char *s);</pre> | ||
| 4991 | |||
| 4992 | <p> | ||
| 4993 | Pushes the zero-terminated string pointed to by <code>s</code> | ||
| 4994 | onto the stack. | ||
| 4995 | Lua will make or reuse an internal copy of the given string, | ||
| 4996 | so the memory at <code>s</code> can be freed or reused immediately after | ||
| 4997 | the function returns. | ||
| 4998 | |||
| 4999 | |||
| 5000 | <p> | ||
| 5001 | Returns a pointer to the internal copy of the string (see <a href="#4.1.3">§4.1.3</a>). | ||
| 5002 | |||
| 5003 | |||
| 5004 | <p> | ||
| 5005 | If <code>s</code> is <code>NULL</code>, pushes <b>nil</b> and returns <code>NULL</code>. | ||
| 5006 | |||
| 5007 | |||
| 5008 | |||
| 5009 | |||
| 5010 | |||
| 5011 | <hr><h3><a name="lua_pushthread"><code>lua_pushthread</code></a></h3><p> | ||
| 5012 | <span class="apii">[-0, +1, –]</span> | ||
| 5013 | <pre>int lua_pushthread (lua_State *L);</pre> | ||
| 5014 | |||
| 5015 | <p> | ||
| 5016 | Pushes the thread represented by <code>L</code> onto the stack. | ||
| 5017 | Returns 1 if this thread is the main thread of its state. | ||
| 5018 | |||
| 5019 | |||
| 5020 | |||
| 5021 | |||
| 5022 | |||
| 5023 | <hr><h3><a name="lua_pushvalue"><code>lua_pushvalue</code></a></h3><p> | ||
| 5024 | <span class="apii">[-0, +1, –]</span> | ||
| 5025 | <pre>void lua_pushvalue (lua_State *L, int index);</pre> | ||
| 5026 | |||
| 5027 | <p> | ||
| 5028 | Pushes a copy of the element at the given index | ||
| 5029 | onto the stack. | ||
| 5030 | |||
| 5031 | |||
| 5032 | |||
| 5033 | |||
| 5034 | |||
| 5035 | <hr><h3><a name="lua_pushvfstring"><code>lua_pushvfstring</code></a></h3><p> | ||
| 5036 | <span class="apii">[-0, +1, <em>v</em>]</span> | ||
| 5037 | <pre>const char *lua_pushvfstring (lua_State *L, | ||
| 5038 | const char *fmt, | ||
| 5039 | va_list argp);</pre> | ||
| 5040 | |||
| 5041 | <p> | ||
| 5042 | Equivalent to <a href="#lua_pushfstring"><code>lua_pushfstring</code></a>, except that it receives a <code>va_list</code> | ||
| 5043 | instead of a variable number of arguments. | ||
| 5044 | |||
| 5045 | |||
| 5046 | |||
| 5047 | |||
| 5048 | |||
| 5049 | <hr><h3><a name="lua_rawequal"><code>lua_rawequal</code></a></h3><p> | ||
| 5050 | <span class="apii">[-0, +0, –]</span> | ||
| 5051 | <pre>int lua_rawequal (lua_State *L, int index1, int index2);</pre> | ||
| 5052 | |||
| 5053 | <p> | ||
| 5054 | Returns 1 if the two values in indices <code>index1</code> and | ||
| 5055 | <code>index2</code> are primitively equal | ||
| 5056 | (that is, equal without calling the <code>__eq</code> metamethod). | ||
| 5057 | Otherwise returns 0. | ||
| 5058 | Also returns 0 if any of the indices are not valid. | ||
| 5059 | |||
| 5060 | |||
| 5061 | |||
| 5062 | |||
| 5063 | |||
| 5064 | <hr><h3><a name="lua_rawget"><code>lua_rawget</code></a></h3><p> | ||
| 5065 | <span class="apii">[-1, +1, –]</span> | ||
| 5066 | <pre>int lua_rawget (lua_State *L, int index);</pre> | ||
| 5067 | |||
| 5068 | <p> | ||
| 5069 | Similar to <a href="#lua_gettable"><code>lua_gettable</code></a>, but does a raw access | ||
| 5070 | (i.e., without metamethods). | ||
| 5071 | The value at <code>index</code> must be a table. | ||
| 5072 | |||
| 5073 | |||
| 5074 | |||
| 5075 | |||
| 5076 | |||
| 5077 | <hr><h3><a name="lua_rawgeti"><code>lua_rawgeti</code></a></h3><p> | ||
| 5078 | <span class="apii">[-0, +1, –]</span> | ||
| 5079 | <pre>int lua_rawgeti (lua_State *L, int index, lua_Integer n);</pre> | ||
| 5080 | |||
| 5081 | <p> | ||
| 5082 | Pushes onto the stack the value <code>t[n]</code>, | ||
| 5083 | where <code>t</code> is the table at the given index. | ||
| 5084 | The access is raw, | ||
| 5085 | that is, it does not use the <code>__index</code> metavalue. | ||
| 5086 | |||
| 5087 | |||
| 5088 | <p> | ||
| 5089 | Returns the type of the pushed value. | ||
| 5090 | |||
| 5091 | |||
| 5092 | |||
| 5093 | |||
| 5094 | |||
| 5095 | <hr><h3><a name="lua_rawgetp"><code>lua_rawgetp</code></a></h3><p> | ||
| 5096 | <span class="apii">[-0, +1, –]</span> | ||
| 5097 | <pre>int lua_rawgetp (lua_State *L, int index, const void *p);</pre> | ||
| 5098 | |||
| 5099 | <p> | ||
| 5100 | Pushes onto the stack the value <code>t[k]</code>, | ||
| 5101 | where <code>t</code> is the table at the given index and | ||
| 5102 | <code>k</code> is the pointer <code>p</code> represented as a light userdata. | ||
| 5103 | The access is raw; | ||
| 5104 | that is, it does not use the <code>__index</code> metavalue. | ||
| 5105 | |||
| 5106 | |||
| 5107 | <p> | ||
| 5108 | Returns the type of the pushed value. | ||
| 5109 | |||
| 5110 | |||
| 5111 | |||
| 5112 | |||
| 5113 | |||
| 5114 | <hr><h3><a name="lua_rawlen"><code>lua_rawlen</code></a></h3><p> | ||
| 5115 | <span class="apii">[-0, +0, –]</span> | ||
| 5116 | <pre>lua_Unsigned lua_rawlen (lua_State *L, int index);</pre> | ||
| 5117 | |||
| 5118 | <p> | ||
| 5119 | Returns the raw "length" of the value at the given index: | ||
| 5120 | for strings, this is the string length; | ||
| 5121 | for tables, this is the result of the length operator ('<code>#</code>') | ||
| 5122 | with no metamethods; | ||
| 5123 | for userdata, this is the size of the block of memory allocated | ||
| 5124 | for the userdata. | ||
| 5125 | For other values, this call returns 0. | ||
| 5126 | |||
| 5127 | |||
| 5128 | |||
| 5129 | |||
| 5130 | |||
| 5131 | <hr><h3><a name="lua_rawset"><code>lua_rawset</code></a></h3><p> | ||
| 5132 | <span class="apii">[-2, +0, <em>m</em>]</span> | ||
| 5133 | <pre>void lua_rawset (lua_State *L, int index);</pre> | ||
| 5134 | |||
| 5135 | <p> | ||
| 5136 | Similar to <a href="#lua_settable"><code>lua_settable</code></a>, but does a raw assignment | ||
| 5137 | (i.e., without metamethods). | ||
| 5138 | The value at <code>index</code> must be a table. | ||
| 5139 | |||
| 5140 | |||
| 5141 | |||
| 5142 | |||
| 5143 | |||
| 5144 | <hr><h3><a name="lua_rawseti"><code>lua_rawseti</code></a></h3><p> | ||
| 5145 | <span class="apii">[-1, +0, <em>m</em>]</span> | ||
| 5146 | <pre>void lua_rawseti (lua_State *L, int index, lua_Integer i);</pre> | ||
| 5147 | |||
| 5148 | <p> | ||
| 5149 | Does the equivalent of <code>t[i] = v</code>, | ||
| 5150 | where <code>t</code> is the table at the given index | ||
| 5151 | and <code>v</code> is the value on the top of the stack. | ||
| 5152 | |||
| 5153 | |||
| 5154 | <p> | ||
| 5155 | This function pops the value from the stack. | ||
| 5156 | The assignment is raw, | ||
| 5157 | that is, it does not use the <code>__newindex</code> metavalue. | ||
| 5158 | |||
| 5159 | |||
| 5160 | |||
| 5161 | |||
| 5162 | |||
| 5163 | <hr><h3><a name="lua_rawsetp"><code>lua_rawsetp</code></a></h3><p> | ||
| 5164 | <span class="apii">[-1, +0, <em>m</em>]</span> | ||
| 5165 | <pre>void lua_rawsetp (lua_State *L, int index, const void *p);</pre> | ||
| 5166 | |||
| 5167 | <p> | ||
| 5168 | Does the equivalent of <code>t[p] = v</code>, | ||
| 5169 | where <code>t</code> is the table at the given index, | ||
| 5170 | <code>p</code> is encoded as a light userdata, | ||
| 5171 | and <code>v</code> is the value on the top of the stack. | ||
| 5172 | |||
| 5173 | |||
| 5174 | <p> | ||
| 5175 | This function pops the value from the stack. | ||
| 5176 | The assignment is raw, | ||
| 5177 | that is, it does not use the <code>__newindex</code> metavalue. | ||
| 5178 | |||
| 5179 | |||
| 5180 | |||
| 5181 | |||
| 5182 | |||
| 5183 | <hr><h3><a name="lua_Reader"><code>lua_Reader</code></a></h3> | ||
| 5184 | <pre>typedef const char * (*lua_Reader) (lua_State *L, | ||
| 5185 | void *data, | ||
| 5186 | size_t *size);</pre> | ||
| 5187 | |||
| 5188 | <p> | ||
| 5189 | The reader function used by <a href="#lua_load"><code>lua_load</code></a>. | ||
| 5190 | Every time <a href="#lua_load"><code>lua_load</code></a> needs another piece of the chunk, | ||
| 5191 | it calls the reader, | ||
| 5192 | passing along its <code>data</code> parameter. | ||
| 5193 | The reader must return a pointer to a block of memory | ||
| 5194 | with a new piece of the chunk | ||
| 5195 | and set <code>size</code> to the block size. | ||
| 5196 | The block must exist until the reader function is called again. | ||
| 5197 | To signal the end of the chunk, | ||
| 5198 | the reader must return <code>NULL</code> or set <code>size</code> to zero. | ||
| 5199 | The reader function may return pieces of any size greater than zero. | ||
| 5200 | |||
| 5201 | |||
| 5202 | |||
| 5203 | |||
| 5204 | |||
| 5205 | <hr><h3><a name="lua_register"><code>lua_register</code></a></h3><p> | ||
| 5206 | <span class="apii">[-0, +0, <em>e</em>]</span> | ||
| 5207 | <pre>void lua_register (lua_State *L, const char *name, lua_CFunction f);</pre> | ||
| 5208 | |||
| 5209 | <p> | ||
| 5210 | Sets the C function <code>f</code> as the new value of global <code>name</code>. | ||
| 5211 | It is defined as a macro: | ||
| 5212 | |||
| 5213 | <pre> | ||
| 5214 | #define lua_register(L,n,f) \ | ||
| 5215 | (lua_pushcfunction(L, f), lua_setglobal(L, n)) | ||
| 5216 | </pre> | ||
| 5217 | |||
| 5218 | |||
| 5219 | |||
| 5220 | |||
| 5221 | <hr><h3><a name="lua_remove"><code>lua_remove</code></a></h3><p> | ||
| 5222 | <span class="apii">[-1, +0, –]</span> | ||
| 5223 | <pre>void lua_remove (lua_State *L, int index);</pre> | ||
| 5224 | |||
| 5225 | <p> | ||
| 5226 | Removes the element at the given valid index, | ||
| 5227 | shifting down the elements above this index to fill the gap. | ||
| 5228 | This function cannot be called with a pseudo-index, | ||
| 5229 | because a pseudo-index is not an actual stack position. | ||
| 5230 | |||
| 5231 | |||
| 5232 | |||
| 5233 | |||
| 5234 | |||
| 5235 | <hr><h3><a name="lua_replace"><code>lua_replace</code></a></h3><p> | ||
| 5236 | <span class="apii">[-1, +0, –]</span> | ||
| 5237 | <pre>void lua_replace (lua_State *L, int index);</pre> | ||
| 5238 | |||
| 5239 | <p> | ||
| 5240 | Moves the top element into the given valid index | ||
| 5241 | without shifting any element | ||
| 5242 | (therefore replacing the value at that given index), | ||
| 5243 | and then pops the top element. | ||
| 5244 | |||
| 5245 | |||
| 5246 | |||
| 5247 | |||
| 5248 | |||
| 5249 | <hr><h3><a name="lua_resetthread"><code>lua_resetthread</code></a></h3><p> | ||
| 5250 | <span class="apii">[-0, +?, –]</span> | ||
| 5251 | <pre>int lua_resetthread (lua_State *L);</pre> | ||
| 5252 | |||
| 5253 | <p> | ||
| 5254 | This function is deprecated; | ||
| 5255 | it is equivalent to <a href="#lua_closethread"><code>lua_closethread</code></a> with | ||
| 5256 | <code>from</code> being <code>NULL</code>. | ||
| 5257 | |||
| 5258 | |||
| 5259 | |||
| 5260 | |||
| 5261 | |||
| 5262 | <hr><h3><a name="lua_resume"><code>lua_resume</code></a></h3><p> | ||
| 5263 | <span class="apii">[-?, +?, –]</span> | ||
| 5264 | <pre>int lua_resume (lua_State *L, lua_State *from, int nargs, | ||
| 5265 | int *nresults);</pre> | ||
| 5266 | |||
| 5267 | <p> | ||
| 5268 | Starts and resumes a coroutine in the given thread <code>L</code>. | ||
| 5269 | |||
| 5270 | |||
| 5271 | <p> | ||
| 5272 | To start a coroutine, | ||
| 5273 | you push the main function plus any arguments | ||
| 5274 | onto the empty stack of the thread. | ||
| 5275 | then you call <a href="#lua_resume"><code>lua_resume</code></a>, | ||
| 5276 | with <code>nargs</code> being the number of arguments. | ||
| 5277 | This call returns when the coroutine suspends or finishes its execution. | ||
| 5278 | When it returns, | ||
| 5279 | <code>*nresults</code> is updated and | ||
| 5280 | the top of the stack contains | ||
| 5281 | the <code>*nresults</code> values passed to <a href="#lua_yield"><code>lua_yield</code></a> | ||
| 5282 | or returned by the body function. | ||
| 5283 | <a href="#lua_resume"><code>lua_resume</code></a> returns | ||
| 5284 | <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> if the coroutine yields, | ||
| 5285 | <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> if the coroutine finishes its execution | ||
| 5286 | without errors, | ||
| 5287 | or an error code in case of errors (see <a href="#4.4.1">§4.4.1</a>). | ||
| 5288 | In case of errors, | ||
| 5289 | the error object is on the top of the stack. | ||
| 5290 | |||
| 5291 | |||
| 5292 | <p> | ||
| 5293 | To resume a coroutine, | ||
| 5294 | you remove the <code>*nresults</code> yielded values from its stack, | ||
| 5295 | push the values to be passed as results from <code>yield</code>, | ||
| 5296 | and then call <a href="#lua_resume"><code>lua_resume</code></a>. | ||
| 5297 | |||
| 5298 | |||
| 5299 | <p> | ||
| 5300 | The parameter <code>from</code> represents the coroutine that is resuming <code>L</code>. | ||
| 5301 | If there is no such coroutine, | ||
| 5302 | this parameter can be <code>NULL</code>. | ||
| 5303 | |||
| 5304 | |||
| 5305 | |||
| 5306 | |||
| 5307 | |||
| 5308 | <hr><h3><a name="lua_rotate"><code>lua_rotate</code></a></h3><p> | ||
| 5309 | <span class="apii">[-0, +0, –]</span> | ||
| 5310 | <pre>void lua_rotate (lua_State *L, int idx, int n);</pre> | ||
| 5311 | |||
| 5312 | <p> | ||
| 5313 | Rotates the stack elements between the valid index <code>idx</code> | ||
| 5314 | and the top of the stack. | ||
| 5315 | The elements are rotated <code>n</code> positions in the direction of the top, | ||
| 5316 | for a positive <code>n</code>, | ||
| 5317 | or <code>-n</code> positions in the direction of the bottom, | ||
| 5318 | for a negative <code>n</code>. | ||
| 5319 | The absolute value of <code>n</code> must not be greater than the size | ||
| 5320 | of the slice being rotated. | ||
| 5321 | This function cannot be called with a pseudo-index, | ||
| 5322 | because a pseudo-index is not an actual stack position. | ||
| 5323 | |||
| 5324 | |||
| 5325 | |||
| 5326 | |||
| 5327 | |||
| 5328 | <hr><h3><a name="lua_setallocf"><code>lua_setallocf</code></a></h3><p> | ||
| 5329 | <span class="apii">[-0, +0, –]</span> | ||
| 5330 | <pre>void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);</pre> | ||
| 5331 | |||
| 5332 | <p> | ||
| 5333 | Changes the allocator function of a given state to <code>f</code> | ||
| 5334 | with user data <code>ud</code>. | ||
| 5335 | |||
| 5336 | |||
| 5337 | |||
| 5338 | |||
| 5339 | |||
| 5340 | <hr><h3><a name="lua_setfield"><code>lua_setfield</code></a></h3><p> | ||
| 5341 | <span class="apii">[-1, +0, <em>e</em>]</span> | ||
| 5342 | <pre>void lua_setfield (lua_State *L, int index, const char *k);</pre> | ||
| 5343 | |||
| 5344 | <p> | ||
| 5345 | Does the equivalent to <code>t[k] = v</code>, | ||
| 5346 | where <code>t</code> is the value at the given index | ||
| 5347 | and <code>v</code> is the value on the top of the stack. | ||
| 5348 | |||
| 5349 | |||
| 5350 | <p> | ||
| 5351 | This function pops the value from the stack. | ||
| 5352 | As in Lua, this function may trigger a metamethod | ||
| 5353 | for the "newindex" event (see <a href="#2.4">§2.4</a>). | ||
| 5354 | |||
| 5355 | |||
| 5356 | |||
| 5357 | |||
| 5358 | |||
| 5359 | <hr><h3><a name="lua_setglobal"><code>lua_setglobal</code></a></h3><p> | ||
| 5360 | <span class="apii">[-1, +0, <em>e</em>]</span> | ||
| 5361 | <pre>void lua_setglobal (lua_State *L, const char *name);</pre> | ||
| 5362 | |||
| 5363 | <p> | ||
| 5364 | Pops a value from the stack and | ||
| 5365 | sets it as the new value of global <code>name</code>. | ||
| 5366 | |||
| 5367 | |||
| 5368 | |||
| 5369 | |||
| 5370 | |||
| 5371 | <hr><h3><a name="lua_seti"><code>lua_seti</code></a></h3><p> | ||
| 5372 | <span class="apii">[-1, +0, <em>e</em>]</span> | ||
| 5373 | <pre>void lua_seti (lua_State *L, int index, lua_Integer n);</pre> | ||
| 5374 | |||
| 5375 | <p> | ||
| 5376 | Does the equivalent to <code>t[n] = v</code>, | ||
| 5377 | where <code>t</code> is the value at the given index | ||
| 5378 | and <code>v</code> is the value on the top of the stack. | ||
| 5379 | |||
| 5380 | |||
| 5381 | <p> | ||
| 5382 | This function pops the value from the stack. | ||
| 5383 | As in Lua, this function may trigger a metamethod | ||
| 5384 | for the "newindex" event (see <a href="#2.4">§2.4</a>). | ||
| 5385 | |||
| 5386 | |||
| 5387 | |||
| 5388 | |||
| 5389 | |||
| 5390 | <hr><h3><a name="lua_setiuservalue"><code>lua_setiuservalue</code></a></h3><p> | ||
| 5391 | <span class="apii">[-1, +0, –]</span> | ||
| 5392 | <pre>int lua_setiuservalue (lua_State *L, int index, int n);</pre> | ||
| 5393 | |||
| 5394 | <p> | ||
| 5395 | Pops a value from the stack and sets it as | ||
| 5396 | the new <code>n</code>-th user value associated to the | ||
| 5397 | full userdata at the given index. | ||
| 5398 | Returns 0 if the userdata does not have that value. | ||
| 5399 | |||
| 5400 | |||
| 5401 | |||
| 5402 | |||
| 5403 | |||
| 5404 | <hr><h3><a name="lua_setmetatable"><code>lua_setmetatable</code></a></h3><p> | ||
| 5405 | <span class="apii">[-1, +0, –]</span> | ||
| 5406 | <pre>int lua_setmetatable (lua_State *L, int index);</pre> | ||
| 5407 | |||
| 5408 | <p> | ||
| 5409 | Pops a table or <b>nil</b> from the stack and | ||
| 5410 | sets that value as the new metatable for the value at the given index. | ||
| 5411 | (<b>nil</b> means no metatable.) | ||
| 5412 | |||
| 5413 | |||
| 5414 | <p> | ||
| 5415 | (For historical reasons, this function returns an <code>int</code>, | ||
| 5416 | which now is always 1.) | ||
| 5417 | |||
| 5418 | |||
| 5419 | |||
| 5420 | |||
| 5421 | |||
| 5422 | <hr><h3><a name="lua_settable"><code>lua_settable</code></a></h3><p> | ||
| 5423 | <span class="apii">[-2, +0, <em>e</em>]</span> | ||
| 5424 | <pre>void lua_settable (lua_State *L, int index);</pre> | ||
| 5425 | |||
| 5426 | <p> | ||
| 5427 | Does the equivalent to <code>t[k] = v</code>, | ||
| 5428 | where <code>t</code> is the value at the given index, | ||
| 5429 | <code>v</code> is the value on the top of the stack, | ||
| 5430 | and <code>k</code> is the value just below the top. | ||
| 5431 | |||
| 5432 | |||
| 5433 | <p> | ||
| 5434 | This function pops both the key and the value from the stack. | ||
| 5435 | As in Lua, this function may trigger a metamethod | ||
| 5436 | for the "newindex" event (see <a href="#2.4">§2.4</a>). | ||
| 5437 | |||
| 5438 | |||
| 5439 | |||
| 5440 | |||
| 5441 | |||
| 5442 | <hr><h3><a name="lua_settop"><code>lua_settop</code></a></h3><p> | ||
| 5443 | <span class="apii">[-?, +?, <em>e</em>]</span> | ||
| 5444 | <pre>void lua_settop (lua_State *L, int index);</pre> | ||
| 5445 | |||
| 5446 | <p> | ||
| 5447 | Accepts any index, or 0, | ||
| 5448 | and sets the stack top to this index. | ||
| 5449 | If the new top is greater than the old one, | ||
| 5450 | then the new elements are filled with <b>nil</b>. | ||
| 5451 | If <code>index</code> is 0, then all stack elements are removed. | ||
| 5452 | |||
| 5453 | |||
| 5454 | <p> | ||
| 5455 | This function can run arbitrary code when removing an index | ||
| 5456 | marked as to-be-closed from the stack. | ||
| 5457 | |||
| 5458 | |||
| 5459 | |||
| 5460 | |||
| 5461 | |||
| 5462 | <hr><h3><a name="lua_setwarnf"><code>lua_setwarnf</code></a></h3><p> | ||
| 5463 | <span class="apii">[-0, +0, –]</span> | ||
| 5464 | <pre>void lua_setwarnf (lua_State *L, lua_WarnFunction f, void *ud);</pre> | ||
| 5465 | |||
| 5466 | <p> | ||
| 5467 | Sets the warning function to be used by Lua to emit warnings | ||
| 5468 | (see <a href="#lua_WarnFunction"><code>lua_WarnFunction</code></a>). | ||
| 5469 | The <code>ud</code> parameter sets the value <code>ud</code> passed to | ||
| 5470 | the warning function. | ||
| 5471 | |||
| 5472 | |||
| 5473 | |||
| 5474 | |||
| 5475 | |||
| 5476 | <hr><h3><a name="lua_State"><code>lua_State</code></a></h3> | ||
| 5477 | <pre>typedef struct lua_State lua_State;</pre> | ||
| 5478 | |||
| 5479 | <p> | ||
| 5480 | An opaque structure that points to a thread and indirectly | ||
| 5481 | (through the thread) to the whole state of a Lua interpreter. | ||
| 5482 | The Lua library is fully reentrant: | ||
| 5483 | it has no global variables. | ||
| 5484 | All information about a state is accessible through this structure. | ||
| 5485 | |||
| 5486 | |||
| 5487 | <p> | ||
| 5488 | A pointer to this structure must be passed as the first argument to | ||
| 5489 | every function in the library, except to <a href="#lua_newstate"><code>lua_newstate</code></a>, | ||
| 5490 | which creates a Lua state from scratch. | ||
| 5491 | |||
| 5492 | |||
| 5493 | |||
| 5494 | |||
| 5495 | |||
| 5496 | <hr><h3><a name="lua_status"><code>lua_status</code></a></h3><p> | ||
| 5497 | <span class="apii">[-0, +0, –]</span> | ||
| 5498 | <pre>int lua_status (lua_State *L);</pre> | ||
| 5499 | |||
| 5500 | <p> | ||
| 5501 | Returns the status of the thread <code>L</code>. | ||
| 5502 | |||
| 5503 | |||
| 5504 | <p> | ||
| 5505 | The status can be <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> for a normal thread, | ||
| 5506 | an error code if the thread finished the execution | ||
| 5507 | of a <a href="#lua_resume"><code>lua_resume</code></a> with an error, | ||
| 5508 | or <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> if the thread is suspended. | ||
| 5509 | |||
| 5510 | |||
| 5511 | <p> | ||
| 5512 | You can call functions only in threads with status <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>. | ||
| 5513 | You can resume threads with status <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> | ||
| 5514 | (to start a new coroutine) or <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> | ||
| 5515 | (to resume a coroutine). | ||
| 5516 | |||
| 5517 | |||
| 5518 | |||
| 5519 | |||
| 5520 | |||
| 5521 | <hr><h3><a name="lua_stringtonumber"><code>lua_stringtonumber</code></a></h3><p> | ||
| 5522 | <span class="apii">[-0, +1, –]</span> | ||
| 5523 | <pre>size_t lua_stringtonumber (lua_State *L, const char *s);</pre> | ||
| 5524 | |||
| 5525 | <p> | ||
| 5526 | Converts the zero-terminated string <code>s</code> to a number, | ||
| 5527 | pushes that number into the stack, | ||
| 5528 | and returns the total size of the string, | ||
| 5529 | that is, its length plus one. | ||
| 5530 | The conversion can result in an integer or a float, | ||
| 5531 | according to the lexical conventions of Lua (see <a href="#3.1">§3.1</a>). | ||
| 5532 | The string may have leading and trailing whitespaces and a sign. | ||
| 5533 | If the string is not a valid numeral, | ||
| 5534 | returns 0 and pushes nothing. | ||
| 5535 | (Note that the result can be used as a boolean, | ||
| 5536 | true if the conversion succeeds.) | ||
| 5537 | |||
| 5538 | |||
| 5539 | |||
| 5540 | |||
| 5541 | |||
| 5542 | <hr><h3><a name="lua_toboolean"><code>lua_toboolean</code></a></h3><p> | ||
| 5543 | <span class="apii">[-0, +0, –]</span> | ||
| 5544 | <pre>int lua_toboolean (lua_State *L, int index);</pre> | ||
| 5545 | |||
| 5546 | <p> | ||
| 5547 | Converts the Lua value at the given index to a C boolean | ||
| 5548 | value (0 or 1). | ||
| 5549 | Like all tests in Lua, | ||
| 5550 | <a href="#lua_toboolean"><code>lua_toboolean</code></a> returns true for any Lua value | ||
| 5551 | different from <b>false</b> and <b>nil</b>; | ||
| 5552 | otherwise it returns false. | ||
| 5553 | (If you want to accept only actual boolean values, | ||
| 5554 | use <a href="#lua_isboolean"><code>lua_isboolean</code></a> to test the value's type.) | ||
| 5555 | |||
| 5556 | |||
| 5557 | |||
| 5558 | |||
| 5559 | |||
| 5560 | <hr><h3><a name="lua_tocfunction"><code>lua_tocfunction</code></a></h3><p> | ||
| 5561 | <span class="apii">[-0, +0, –]</span> | ||
| 5562 | <pre>lua_CFunction lua_tocfunction (lua_State *L, int index);</pre> | ||
| 5563 | |||
| 5564 | <p> | ||
| 5565 | Converts a value at the given index to a C function. | ||
| 5566 | That value must be a C function; | ||
| 5567 | otherwise, returns <code>NULL</code>. | ||
| 5568 | |||
| 5569 | |||
| 5570 | |||
| 5571 | |||
| 5572 | |||
| 5573 | <hr><h3><a name="lua_toclose"><code>lua_toclose</code></a></h3><p> | ||
| 5574 | <span class="apii">[-0, +0, <em>v</em>]</span> | ||
| 5575 | <pre>void lua_toclose (lua_State *L, int index);</pre> | ||
| 5576 | |||
| 5577 | <p> | ||
| 5578 | Marks the given index in the stack as a | ||
| 5579 | to-be-closed slot (see <a href="#3.3.8">§3.3.8</a>). | ||
| 5580 | Like a to-be-closed variable in Lua, | ||
| 5581 | the value at that slot in the stack will be closed | ||
| 5582 | when it goes out of scope. | ||
| 5583 | Here, in the context of a C function, | ||
| 5584 | to go out of scope means that the running function returns to Lua, | ||
| 5585 | or there is an error, | ||
| 5586 | or the slot is removed from the stack through | ||
| 5587 | <a href="#lua_settop"><code>lua_settop</code></a> or <a href="#lua_pop"><code>lua_pop</code></a>, | ||
| 5588 | or there is a call to <a href="#lua_closeslot"><code>lua_closeslot</code></a>. | ||
| 5589 | A slot marked as to-be-closed should not be removed from the stack | ||
| 5590 | by any other function in the API except <a href="#lua_settop"><code>lua_settop</code></a> or <a href="#lua_pop"><code>lua_pop</code></a>, | ||
| 5591 | unless previously deactivated by <a href="#lua_closeslot"><code>lua_closeslot</code></a>. | ||
| 5592 | |||
| 5593 | |||
| 5594 | <p> | ||
| 5595 | This function raises an error if the value at the given slot | ||
| 5596 | neither has a <code>__close</code> metamethod nor is a false value. | ||
| 5597 | |||
| 5598 | |||
| 5599 | <p> | ||
| 5600 | This function should not be called for an index | ||
| 5601 | that is equal to or below an active to-be-closed slot. | ||
| 5602 | |||
| 5603 | |||
| 5604 | <p> | ||
| 5605 | Note that, both in case of errors and of a regular return, | ||
| 5606 | by the time the <code>__close</code> metamethod runs, | ||
| 5607 | the C stack was already unwound, | ||
| 5608 | so that any automatic C variable declared in the calling function | ||
| 5609 | (e.g., a buffer) will be out of scope. | ||
| 5610 | |||
| 5611 | |||
| 5612 | |||
| 5613 | |||
| 5614 | |||
| 5615 | <hr><h3><a name="lua_tointeger"><code>lua_tointeger</code></a></h3><p> | ||
| 5616 | <span class="apii">[-0, +0, –]</span> | ||
| 5617 | <pre>lua_Integer lua_tointeger (lua_State *L, int index);</pre> | ||
| 5618 | |||
| 5619 | <p> | ||
| 5620 | Equivalent to <a href="#lua_tointegerx"><code>lua_tointegerx</code></a> with <code>isnum</code> equal to <code>NULL</code>. | ||
| 5621 | |||
| 5622 | |||
| 5623 | |||
| 5624 | |||
| 5625 | |||
| 5626 | <hr><h3><a name="lua_tointegerx"><code>lua_tointegerx</code></a></h3><p> | ||
| 5627 | <span class="apii">[-0, +0, –]</span> | ||
| 5628 | <pre>lua_Integer lua_tointegerx (lua_State *L, int index, int *isnum);</pre> | ||
| 5629 | |||
| 5630 | <p> | ||
| 5631 | Converts the Lua value at the given index | ||
| 5632 | to the signed integral type <a href="#lua_Integer"><code>lua_Integer</code></a>. | ||
| 5633 | The Lua value must be an integer, | ||
| 5634 | or a number or string convertible to an integer (see <a href="#3.4.3">§3.4.3</a>); | ||
| 5635 | otherwise, <code>lua_tointegerx</code> returns 0. | ||
| 5636 | |||
| 5637 | |||
| 5638 | <p> | ||
| 5639 | If <code>isnum</code> is not <code>NULL</code>, | ||
| 5640 | its referent is assigned a boolean value that | ||
| 5641 | indicates whether the operation succeeded. | ||
| 5642 | |||
| 5643 | |||
| 5644 | |||
| 5645 | |||
| 5646 | |||
| 5647 | <hr><h3><a name="lua_tolstring"><code>lua_tolstring</code></a></h3><p> | ||
| 5648 | <span class="apii">[-0, +0, <em>m</em>]</span> | ||
| 5649 | <pre>const char *lua_tolstring (lua_State *L, int index, size_t *len);</pre> | ||
| 5650 | |||
| 5651 | <p> | ||
| 5652 | Converts the Lua value at the given index to a C string. | ||
| 5653 | If <code>len</code> is not <code>NULL</code>, | ||
| 5654 | it sets <code>*len</code> with the string length. | ||
| 5655 | The Lua value must be a string or a number; | ||
| 5656 | otherwise, the function returns <code>NULL</code>. | ||
| 5657 | If the value is a number, | ||
| 5658 | then <code>lua_tolstring</code> also | ||
| 5659 | <em>changes the actual value in the stack to a string</em>. | ||
| 5660 | (This change confuses <a href="#lua_next"><code>lua_next</code></a> | ||
| 5661 | when <code>lua_tolstring</code> is applied to keys during a table traversal.) | ||
| 5662 | |||
| 5663 | |||
| 5664 | <p> | ||
| 5665 | <code>lua_tolstring</code> returns a pointer | ||
| 5666 | to a string inside the Lua state (see <a href="#4.1.3">§4.1.3</a>). | ||
| 5667 | This string always has a zero ('<code>\0</code>') | ||
| 5668 | after its last character (as in C), | ||
| 5669 | but can contain other zeros in its body. | ||
| 5670 | |||
| 5671 | |||
| 5672 | <p> | ||
| 5673 | This function can raise memory errors only | ||
| 5674 | when converting a number to a string | ||
| 5675 | (as then it may create a new string). | ||
| 5676 | |||
| 5677 | |||
| 5678 | |||
| 5679 | |||
| 5680 | |||
| 5681 | <hr><h3><a name="lua_tonumber"><code>lua_tonumber</code></a></h3><p> | ||
| 5682 | <span class="apii">[-0, +0, –]</span> | ||
| 5683 | <pre>lua_Number lua_tonumber (lua_State *L, int index);</pre> | ||
| 5684 | |||
| 5685 | <p> | ||
| 5686 | Equivalent to <a href="#lua_tonumberx"><code>lua_tonumberx</code></a> with <code>isnum</code> equal to <code>NULL</code>. | ||
| 5687 | |||
| 5688 | |||
| 5689 | |||
| 5690 | |||
| 5691 | |||
| 5692 | <hr><h3><a name="lua_tonumberx"><code>lua_tonumberx</code></a></h3><p> | ||
| 5693 | <span class="apii">[-0, +0, –]</span> | ||
| 5694 | <pre>lua_Number lua_tonumberx (lua_State *L, int index, int *isnum);</pre> | ||
| 5695 | |||
| 5696 | <p> | ||
| 5697 | Converts the Lua value at the given index | ||
| 5698 | to the C type <a href="#lua_Number"><code>lua_Number</code></a> (see <a href="#lua_Number"><code>lua_Number</code></a>). | ||
| 5699 | The Lua value must be a number or a string convertible to a number | ||
| 5700 | (see <a href="#3.4.3">§3.4.3</a>); | ||
| 5701 | otherwise, <a href="#lua_tonumberx"><code>lua_tonumberx</code></a> returns 0. | ||
| 5702 | |||
| 5703 | |||
| 5704 | <p> | ||
| 5705 | If <code>isnum</code> is not <code>NULL</code>, | ||
| 5706 | its referent is assigned a boolean value that | ||
| 5707 | indicates whether the operation succeeded. | ||
| 5708 | |||
| 5709 | |||
| 5710 | |||
| 5711 | |||
| 5712 | |||
| 5713 | <hr><h3><a name="lua_topointer"><code>lua_topointer</code></a></h3><p> | ||
| 5714 | <span class="apii">[-0, +0, –]</span> | ||
| 5715 | <pre>const void *lua_topointer (lua_State *L, int index);</pre> | ||
| 5716 | |||
| 5717 | <p> | ||
| 5718 | Converts the value at the given index to a generic | ||
| 5719 | C pointer (<code>void*</code>). | ||
| 5720 | The value can be a userdata, a table, a thread, a string, or a function; | ||
| 5721 | otherwise, <code>lua_topointer</code> returns <code>NULL</code>. | ||
| 5722 | Different objects will give different pointers. | ||
| 5723 | There is no way to convert the pointer back to its original value. | ||
| 5724 | |||
| 5725 | |||
| 5726 | <p> | ||
| 5727 | Typically this function is used only for hashing and debug information. | ||
| 5728 | |||
| 5729 | |||
| 5730 | |||
| 5731 | |||
| 5732 | |||
| 5733 | <hr><h3><a name="lua_tostring"><code>lua_tostring</code></a></h3><p> | ||
| 5734 | <span class="apii">[-0, +0, <em>m</em>]</span> | ||
| 5735 | <pre>const char *lua_tostring (lua_State *L, int index);</pre> | ||
| 5736 | |||
| 5737 | <p> | ||
| 5738 | Equivalent to <a href="#lua_tolstring"><code>lua_tolstring</code></a> with <code>len</code> equal to <code>NULL</code>. | ||
| 5739 | |||
| 5740 | |||
| 5741 | |||
| 5742 | |||
| 5743 | |||
| 5744 | <hr><h3><a name="lua_tothread"><code>lua_tothread</code></a></h3><p> | ||
| 5745 | <span class="apii">[-0, +0, –]</span> | ||
| 5746 | <pre>lua_State *lua_tothread (lua_State *L, int index);</pre> | ||
| 5747 | |||
| 5748 | <p> | ||
| 5749 | Converts the value at the given index to a Lua thread | ||
| 5750 | (represented as <code>lua_State*</code>). | ||
| 5751 | This value must be a thread; | ||
| 5752 | otherwise, the function returns <code>NULL</code>. | ||
| 5753 | |||
| 5754 | |||
| 5755 | |||
| 5756 | |||
| 5757 | |||
| 5758 | <hr><h3><a name="lua_touserdata"><code>lua_touserdata</code></a></h3><p> | ||
| 5759 | <span class="apii">[-0, +0, –]</span> | ||
| 5760 | <pre>void *lua_touserdata (lua_State *L, int index);</pre> | ||
| 5761 | |||
| 5762 | <p> | ||
| 5763 | If the value at the given index is a full userdata, | ||
| 5764 | returns its memory-block address. | ||
| 5765 | If the value is a light userdata, | ||
| 5766 | returns its value (a pointer). | ||
| 5767 | Otherwise, returns <code>NULL</code>. | ||
| 5768 | |||
| 5769 | |||
| 5770 | |||
| 5771 | |||
| 5772 | |||
| 5773 | <hr><h3><a name="lua_type"><code>lua_type</code></a></h3><p> | ||
| 5774 | <span class="apii">[-0, +0, –]</span> | ||
| 5775 | <pre>int lua_type (lua_State *L, int index);</pre> | ||
| 5776 | |||
| 5777 | <p> | ||
| 5778 | Returns the type of the value in the given valid index, | ||
| 5779 | or <code>LUA_TNONE</code> for a non-valid but acceptable index. | ||
| 5780 | The types returned by <a href="#lua_type"><code>lua_type</code></a> are coded by the following constants | ||
| 5781 | defined in <code>lua.h</code>: | ||
| 5782 | <a name="pdf-LUA_TNIL"><code>LUA_TNIL</code></a>, | ||
| 5783 | <a name="pdf-LUA_TNUMBER"><code>LUA_TNUMBER</code></a>, | ||
| 5784 | <a name="pdf-LUA_TBOOLEAN"><code>LUA_TBOOLEAN</code></a>, | ||
| 5785 | <a name="pdf-LUA_TSTRING"><code>LUA_TSTRING</code></a>, | ||
| 5786 | <a name="pdf-LUA_TTABLE"><code>LUA_TTABLE</code></a>, | ||
| 5787 | <a name="pdf-LUA_TFUNCTION"><code>LUA_TFUNCTION</code></a>, | ||
| 5788 | <a name="pdf-LUA_TUSERDATA"><code>LUA_TUSERDATA</code></a>, | ||
| 5789 | <a name="pdf-LUA_TTHREAD"><code>LUA_TTHREAD</code></a>, | ||
| 5790 | and | ||
| 5791 | <a name="pdf-LUA_TLIGHTUSERDATA"><code>LUA_TLIGHTUSERDATA</code></a>. | ||
| 5792 | |||
| 5793 | |||
| 5794 | |||
| 5795 | |||
| 5796 | |||
| 5797 | <hr><h3><a name="lua_typename"><code>lua_typename</code></a></h3><p> | ||
| 5798 | <span class="apii">[-0, +0, –]</span> | ||
| 5799 | <pre>const char *lua_typename (lua_State *L, int tp);</pre> | ||
| 5800 | |||
| 5801 | <p> | ||
| 5802 | Returns the name of the type encoded by the value <code>tp</code>, | ||
| 5803 | which must be one the values returned by <a href="#lua_type"><code>lua_type</code></a>. | ||
| 5804 | |||
| 5805 | |||
| 5806 | |||
| 5807 | |||
| 5808 | |||
| 5809 | <hr><h3><a name="lua_Unsigned"><code>lua_Unsigned</code></a></h3> | ||
| 5810 | <pre>typedef ... lua_Unsigned;</pre> | ||
| 5811 | |||
| 5812 | <p> | ||
| 5813 | The unsigned version of <a href="#lua_Integer"><code>lua_Integer</code></a>. | ||
| 5814 | |||
| 5815 | |||
| 5816 | |||
| 5817 | |||
| 5818 | |||
| 5819 | <hr><h3><a name="lua_upvalueindex"><code>lua_upvalueindex</code></a></h3><p> | ||
| 5820 | <span class="apii">[-0, +0, –]</span> | ||
| 5821 | <pre>int lua_upvalueindex (int i);</pre> | ||
| 5822 | |||
| 5823 | <p> | ||
| 5824 | Returns the pseudo-index that represents the <code>i</code>-th upvalue of | ||
| 5825 | the running function (see <a href="#4.2">§4.2</a>). | ||
| 5826 | <code>i</code> must be in the range <em>[1,256]</em>. | ||
| 5827 | |||
| 5828 | |||
| 5829 | |||
| 5830 | |||
| 5831 | |||
| 5832 | <hr><h3><a name="lua_version"><code>lua_version</code></a></h3><p> | ||
| 5833 | <span class="apii">[-0, +0, –]</span> | ||
| 5834 | <pre>lua_Number lua_version (lua_State *L);</pre> | ||
| 5835 | |||
| 5836 | <p> | ||
| 5837 | Returns the version number of this core. | ||
| 5838 | |||
| 5839 | |||
| 5840 | |||
| 5841 | |||
| 5842 | |||
| 5843 | <hr><h3><a name="lua_WarnFunction"><code>lua_WarnFunction</code></a></h3> | ||
| 5844 | <pre>typedef void (*lua_WarnFunction) (void *ud, const char *msg, int tocont);</pre> | ||
| 5845 | |||
| 5846 | <p> | ||
| 5847 | The type of warning functions, called by Lua to emit warnings. | ||
| 5848 | The first parameter is an opaque pointer | ||
| 5849 | set by <a href="#lua_setwarnf"><code>lua_setwarnf</code></a>. | ||
| 5850 | The second parameter is the warning message. | ||
| 5851 | The third parameter is a boolean that | ||
| 5852 | indicates whether the message is | ||
| 5853 | to be continued by the message in the next call. | ||
| 5854 | |||
| 5855 | |||
| 5856 | <p> | ||
| 5857 | See <a href="#pdf-warn"><code>warn</code></a> for more details about warnings. | ||
| 5858 | |||
| 5859 | |||
| 5860 | |||
| 5861 | |||
| 5862 | |||
| 5863 | <hr><h3><a name="lua_warning"><code>lua_warning</code></a></h3><p> | ||
| 5864 | <span class="apii">[-0, +0, –]</span> | ||
| 5865 | <pre>void lua_warning (lua_State *L, const char *msg, int tocont);</pre> | ||
| 5866 | |||
| 5867 | <p> | ||
| 5868 | Emits a warning with the given message. | ||
| 5869 | A message in a call with <code>tocont</code> true should be | ||
| 5870 | continued in another call to this function. | ||
| 5871 | |||
| 5872 | |||
| 5873 | <p> | ||
| 5874 | See <a href="#pdf-warn"><code>warn</code></a> for more details about warnings. | ||
| 5875 | |||
| 5876 | |||
| 5877 | |||
| 5878 | |||
| 5879 | |||
| 5880 | <hr><h3><a name="lua_Writer"><code>lua_Writer</code></a></h3> | ||
| 5881 | <pre>typedef int (*lua_Writer) (lua_State *L, | ||
| 5882 | const void* p, | ||
| 5883 | size_t sz, | ||
| 5884 | void* ud);</pre> | ||
| 5885 | |||
| 5886 | <p> | ||
| 5887 | The type of the writer function used by <a href="#lua_dump"><code>lua_dump</code></a>. | ||
| 5888 | Every time <a href="#lua_dump"><code>lua_dump</code></a> produces another piece of chunk, | ||
| 5889 | it calls the writer, | ||
| 5890 | passing along the buffer to be written (<code>p</code>), | ||
| 5891 | its size (<code>sz</code>), | ||
| 5892 | and the <code>ud</code> parameter supplied to <a href="#lua_dump"><code>lua_dump</code></a>. | ||
| 5893 | |||
| 5894 | |||
| 5895 | <p> | ||
| 5896 | The writer returns an error code: | ||
| 5897 | 0 means no errors; | ||
| 5898 | any other value means an error and stops <a href="#lua_dump"><code>lua_dump</code></a> from | ||
| 5899 | calling the writer again. | ||
| 5900 | |||
| 5901 | |||
| 5902 | |||
| 5903 | |||
| 5904 | |||
| 5905 | <hr><h3><a name="lua_xmove"><code>lua_xmove</code></a></h3><p> | ||
| 5906 | <span class="apii">[-?, +?, –]</span> | ||
| 5907 | <pre>void lua_xmove (lua_State *from, lua_State *to, int n);</pre> | ||
| 5908 | |||
| 5909 | <p> | ||
| 5910 | Exchange values between different threads of the same state. | ||
| 5911 | |||
| 5912 | |||
| 5913 | <p> | ||
| 5914 | This function pops <code>n</code> values from the stack <code>from</code>, | ||
| 5915 | and pushes them onto the stack <code>to</code>. | ||
| 5916 | |||
| 5917 | |||
| 5918 | |||
| 5919 | |||
| 5920 | |||
| 5921 | <hr><h3><a name="lua_yield"><code>lua_yield</code></a></h3><p> | ||
| 5922 | <span class="apii">[-?, +?, <em>v</em>]</span> | ||
| 5923 | <pre>int lua_yield (lua_State *L, int nresults);</pre> | ||
| 5924 | |||
| 5925 | <p> | ||
| 5926 | This function is equivalent to <a href="#lua_yieldk"><code>lua_yieldk</code></a>, | ||
| 5927 | but it has no continuation (see <a href="#4.5">§4.5</a>). | ||
| 5928 | Therefore, when the thread resumes, | ||
| 5929 | it continues the function that called | ||
| 5930 | the function calling <code>lua_yield</code>. | ||
| 5931 | To avoid surprises, | ||
| 5932 | this function should be called only in a tail call. | ||
| 5933 | |||
| 5934 | |||
| 5935 | |||
| 5936 | |||
| 5937 | |||
| 5938 | <hr><h3><a name="lua_yieldk"><code>lua_yieldk</code></a></h3><p> | ||
| 5939 | <span class="apii">[-?, +?, <em>v</em>]</span> | ||
| 5940 | <pre>int lua_yieldk (lua_State *L, | ||
| 5941 | int nresults, | ||
| 5942 | lua_KContext ctx, | ||
| 5943 | lua_KFunction k);</pre> | ||
| 5944 | |||
| 5945 | <p> | ||
| 5946 | Yields a coroutine (thread). | ||
| 5947 | |||
| 5948 | |||
| 5949 | <p> | ||
| 5950 | When a C function calls <a href="#lua_yieldk"><code>lua_yieldk</code></a>, | ||
| 5951 | the running coroutine suspends its execution, | ||
| 5952 | and the call to <a href="#lua_resume"><code>lua_resume</code></a> that started this coroutine returns. | ||
| 5953 | The parameter <code>nresults</code> is the number of values from the stack | ||
| 5954 | that will be passed as results to <a href="#lua_resume"><code>lua_resume</code></a>. | ||
| 5955 | |||
| 5956 | |||
| 5957 | <p> | ||
| 5958 | When the coroutine is resumed again, | ||
| 5959 | Lua calls the given continuation function <code>k</code> to continue | ||
| 5960 | the execution of the C function that yielded (see <a href="#4.5">§4.5</a>). | ||
| 5961 | This continuation function receives the same stack | ||
| 5962 | from the previous function, | ||
| 5963 | with the <code>n</code> results removed and | ||
| 5964 | replaced by the arguments passed to <a href="#lua_resume"><code>lua_resume</code></a>. | ||
| 5965 | Moreover, | ||
| 5966 | the continuation function receives the value <code>ctx</code> | ||
| 5967 | that was passed to <a href="#lua_yieldk"><code>lua_yieldk</code></a>. | ||
| 5968 | |||
| 5969 | |||
| 5970 | <p> | ||
| 5971 | Usually, this function does not return; | ||
| 5972 | when the coroutine eventually resumes, | ||
| 5973 | it continues executing the continuation function. | ||
| 5974 | However, there is one special case, | ||
| 5975 | which is when this function is called | ||
| 5976 | from inside a line or a count hook (see <a href="#4.7">§4.7</a>). | ||
| 5977 | In that case, <code>lua_yieldk</code> should be called with no continuation | ||
| 5978 | (probably in the form of <a href="#lua_yield"><code>lua_yield</code></a>) and no results, | ||
| 5979 | and the hook should return immediately after the call. | ||
| 5980 | Lua will yield and, | ||
| 5981 | when the coroutine resumes again, | ||
| 5982 | it will continue the normal execution | ||
| 5983 | of the (Lua) function that triggered the hook. | ||
| 5984 | |||
| 5985 | |||
| 5986 | <p> | ||
| 5987 | This function can raise an error if it is called from a thread | ||
| 5988 | with a pending C call with no continuation function | ||
| 5989 | (what is called a <em>C-call boundary</em>), | ||
| 5990 | or it is called from a thread that is not running inside a resume | ||
| 5991 | (typically the main thread). | ||
| 5992 | |||
| 5993 | |||
| 5994 | |||
| 5995 | |||
| 5996 | |||
| 5997 | |||
| 5998 | |||
| 5999 | <h2>4.7 – <a name="4.7">The Debug Interface</a></h2> | ||
| 6000 | |||
| 6001 | <p> | ||
| 6002 | Lua has no built-in debugging facilities. | ||
| 6003 | Instead, it offers a special interface | ||
| 6004 | by means of functions and <em>hooks</em>. | ||
| 6005 | This interface allows the construction of different | ||
| 6006 | kinds of debuggers, profilers, and other tools | ||
| 6007 | that need "inside information" from the interpreter. | ||
| 6008 | |||
| 6009 | |||
| 6010 | |||
| 6011 | <hr><h3><a name="lua_Debug"><code>lua_Debug</code></a></h3> | ||
| 6012 | <pre>typedef struct lua_Debug { | ||
| 6013 | int event; | ||
| 6014 | const char *name; /* (n) */ | ||
| 6015 | const char *namewhat; /* (n) */ | ||
| 6016 | const char *what; /* (S) */ | ||
| 6017 | const char *source; /* (S) */ | ||
| 6018 | size_t srclen; /* (S) */ | ||
| 6019 | int currentline; /* (l) */ | ||
| 6020 | int linedefined; /* (S) */ | ||
| 6021 | int lastlinedefined; /* (S) */ | ||
| 6022 | unsigned char nups; /* (u) number of upvalues */ | ||
| 6023 | unsigned char nparams; /* (u) number of parameters */ | ||
| 6024 | char isvararg; /* (u) */ | ||
| 6025 | char istailcall; /* (t) */ | ||
| 6026 | unsigned short ftransfer; /* (r) index of first value transferred */ | ||
| 6027 | unsigned short ntransfer; /* (r) number of transferred values */ | ||
| 6028 | char short_src[LUA_IDSIZE]; /* (S) */ | ||
| 6029 | /* private part */ | ||
| 6030 | <em>other fields</em> | ||
| 6031 | } lua_Debug;</pre> | ||
| 6032 | |||
| 6033 | <p> | ||
| 6034 | A structure used to carry different pieces of | ||
| 6035 | information about a function or an activation record. | ||
| 6036 | <a href="#lua_getstack"><code>lua_getstack</code></a> fills only the private part | ||
| 6037 | of this structure, for later use. | ||
| 6038 | To fill the other fields of <a href="#lua_Debug"><code>lua_Debug</code></a> with useful information, | ||
| 6039 | you must call <a href="#lua_getinfo"><code>lua_getinfo</code></a> with an appropriate parameter. | ||
| 6040 | (Specifically, to get a field, | ||
| 6041 | you must add the letter between parentheses in the field's comment | ||
| 6042 | to the parameter <code>what</code> of <a href="#lua_getinfo"><code>lua_getinfo</code></a>.) | ||
| 6043 | |||
| 6044 | |||
| 6045 | <p> | ||
| 6046 | The fields of <a href="#lua_Debug"><code>lua_Debug</code></a> have the following meaning: | ||
| 6047 | |||
| 6048 | <ul> | ||
| 6049 | |||
| 6050 | <li><b><code>source</code>: </b> | ||
| 6051 | the source of the chunk that created the function. | ||
| 6052 | If <code>source</code> starts with a '<code>@</code>', | ||
| 6053 | it means that the function was defined in a file where | ||
| 6054 | the file name follows the '<code>@</code>'. | ||
| 6055 | If <code>source</code> starts with a '<code>=</code>', | ||
| 6056 | the remainder of its contents describes the source in a user-dependent manner. | ||
| 6057 | Otherwise, | ||
| 6058 | the function was defined in a string where | ||
| 6059 | <code>source</code> is that string. | ||
| 6060 | </li> | ||
| 6061 | |||
| 6062 | <li><b><code>srclen</code>: </b> | ||
| 6063 | The length of the string <code>source</code>. | ||
| 6064 | </li> | ||
| 6065 | |||
| 6066 | <li><b><code>short_src</code>: </b> | ||
| 6067 | a "printable" version of <code>source</code>, to be used in error messages. | ||
| 6068 | </li> | ||
| 6069 | |||
| 6070 | <li><b><code>linedefined</code>: </b> | ||
| 6071 | the line number where the definition of the function starts. | ||
| 6072 | </li> | ||
| 6073 | |||
| 6074 | <li><b><code>lastlinedefined</code>: </b> | ||
| 6075 | the line number where the definition of the function ends. | ||
| 6076 | </li> | ||
| 6077 | |||
| 6078 | <li><b><code>what</code>: </b> | ||
| 6079 | the string <code>"Lua"</code> if the function is a Lua function, | ||
| 6080 | <code>"C"</code> if it is a C function, | ||
| 6081 | <code>"main"</code> if it is the main part of a chunk. | ||
| 6082 | </li> | ||
| 6083 | |||
| 6084 | <li><b><code>currentline</code>: </b> | ||
| 6085 | the current line where the given function is executing. | ||
| 6086 | When no line information is available, | ||
| 6087 | <code>currentline</code> is set to -1. | ||
| 6088 | </li> | ||
| 6089 | |||
| 6090 | <li><b><code>name</code>: </b> | ||
| 6091 | a reasonable name for the given function. | ||
| 6092 | Because functions in Lua are first-class values, | ||
| 6093 | they do not have a fixed name: | ||
| 6094 | some functions can be the value of multiple global variables, | ||
| 6095 | while others can be stored only in a table field. | ||
| 6096 | The <code>lua_getinfo</code> function checks how the function was | ||
| 6097 | called to find a suitable name. | ||
| 6098 | If it cannot find a name, | ||
| 6099 | then <code>name</code> is set to <code>NULL</code>. | ||
| 6100 | </li> | ||
| 6101 | |||
| 6102 | <li><b><code>namewhat</code>: </b> | ||
| 6103 | explains the <code>name</code> field. | ||
| 6104 | The value of <code>namewhat</code> can be | ||
| 6105 | <code>"global"</code>, <code>"local"</code>, <code>"method"</code>, | ||
| 6106 | <code>"field"</code>, <code>"upvalue"</code>, or <code>""</code> (the empty string), | ||
| 6107 | according to how the function was called. | ||
| 6108 | (Lua uses the empty string when no other option seems to apply.) | ||
| 6109 | </li> | ||
| 6110 | |||
| 6111 | <li><b><code>istailcall</code>: </b> | ||
| 6112 | true if this function invocation was called by a tail call. | ||
| 6113 | In this case, the caller of this level is not in the stack. | ||
| 6114 | </li> | ||
| 6115 | |||
| 6116 | <li><b><code>nups</code>: </b> | ||
| 6117 | the number of upvalues of the function. | ||
| 6118 | </li> | ||
| 6119 | |||
| 6120 | <li><b><code>nparams</code>: </b> | ||
| 6121 | the number of parameters of the function | ||
| 6122 | (always 0 for C functions). | ||
| 6123 | </li> | ||
| 6124 | |||
| 6125 | <li><b><code>isvararg</code>: </b> | ||
| 6126 | true if the function is a variadic function | ||
| 6127 | (always true for C functions). | ||
| 6128 | </li> | ||
| 6129 | |||
| 6130 | <li><b><code>ftransfer</code>: </b> | ||
| 6131 | the index in the stack of the first value being "transferred", | ||
| 6132 | that is, parameters in a call or return values in a return. | ||
| 6133 | (The other values are in consecutive indices.) | ||
| 6134 | Using this index, you can access and modify these values | ||
| 6135 | through <a href="#lua_getlocal"><code>lua_getlocal</code></a> and <a href="#lua_setlocal"><code>lua_setlocal</code></a>. | ||
| 6136 | This field is only meaningful during a | ||
| 6137 | call hook, denoting the first parameter, | ||
| 6138 | or a return hook, denoting the first value being returned. | ||
| 6139 | (For call hooks, this value is always 1.) | ||
| 6140 | </li> | ||
| 6141 | |||
| 6142 | <li><b><code>ntransfer</code>: </b> | ||
| 6143 | The number of values being transferred (see previous item). | ||
| 6144 | (For calls of Lua functions, | ||
| 6145 | this value is always equal to <code>nparams</code>.) | ||
| 6146 | </li> | ||
| 6147 | |||
| 6148 | </ul> | ||
| 6149 | |||
| 6150 | |||
| 6151 | |||
| 6152 | |||
| 6153 | <hr><h3><a name="lua_gethook"><code>lua_gethook</code></a></h3><p> | ||
| 6154 | <span class="apii">[-0, +0, –]</span> | ||
| 6155 | <pre>lua_Hook lua_gethook (lua_State *L);</pre> | ||
| 6156 | |||
| 6157 | <p> | ||
| 6158 | Returns the current hook function. | ||
| 6159 | |||
| 6160 | |||
| 6161 | |||
| 6162 | |||
| 6163 | |||
| 6164 | <hr><h3><a name="lua_gethookcount"><code>lua_gethookcount</code></a></h3><p> | ||
| 6165 | <span class="apii">[-0, +0, –]</span> | ||
| 6166 | <pre>int lua_gethookcount (lua_State *L);</pre> | ||
| 6167 | |||
| 6168 | <p> | ||
| 6169 | Returns the current hook count. | ||
| 6170 | |||
| 6171 | |||
| 6172 | |||
| 6173 | |||
| 6174 | |||
| 6175 | <hr><h3><a name="lua_gethookmask"><code>lua_gethookmask</code></a></h3><p> | ||
| 6176 | <span class="apii">[-0, +0, –]</span> | ||
| 6177 | <pre>int lua_gethookmask (lua_State *L);</pre> | ||
| 6178 | |||
| 6179 | <p> | ||
| 6180 | Returns the current hook mask. | ||
| 6181 | |||
| 6182 | |||
| 6183 | |||
| 6184 | |||
| 6185 | |||
| 6186 | <hr><h3><a name="lua_getinfo"><code>lua_getinfo</code></a></h3><p> | ||
| 6187 | <span class="apii">[-(0|1), +(0|1|2), <em>m</em>]</span> | ||
| 6188 | <pre>int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);</pre> | ||
| 6189 | |||
| 6190 | <p> | ||
| 6191 | Gets information about a specific function or function invocation. | ||
| 6192 | |||
| 6193 | |||
| 6194 | <p> | ||
| 6195 | To get information about a function invocation, | ||
| 6196 | the parameter <code>ar</code> must be a valid activation record that was | ||
| 6197 | filled by a previous call to <a href="#lua_getstack"><code>lua_getstack</code></a> or | ||
| 6198 | given as argument to a hook (see <a href="#lua_Hook"><code>lua_Hook</code></a>). | ||
| 6199 | |||
| 6200 | |||
| 6201 | <p> | ||
| 6202 | To get information about a function, you push it onto the stack | ||
| 6203 | and start the <code>what</code> string with the character '<code>></code>'. | ||
| 6204 | (In that case, | ||
| 6205 | <code>lua_getinfo</code> pops the function from the top of the stack.) | ||
| 6206 | For instance, to know in which line a function <code>f</code> was defined, | ||
| 6207 | you can write the following code: | ||
| 6208 | |||
| 6209 | <pre> | ||
| 6210 | lua_Debug ar; | ||
| 6211 | lua_getglobal(L, "f"); /* get global 'f' */ | ||
| 6212 | lua_getinfo(L, ">S", &ar); | ||
| 6213 | printf("%d\n", ar.linedefined); | ||
| 6214 | </pre> | ||
| 6215 | |||
| 6216 | <p> | ||
| 6217 | Each character in the string <code>what</code> | ||
| 6218 | selects some fields of the structure <code>ar</code> to be filled or | ||
| 6219 | a value to be pushed on the stack. | ||
| 6220 | (These characters are also documented in the declaration of | ||
| 6221 | the structure <a href="#lua_Debug"><code>lua_Debug</code></a>, | ||
| 6222 | between parentheses in the comments following each field.) | ||
| 6223 | |||
| 6224 | <ul> | ||
| 6225 | |||
| 6226 | <li><b>'<code>f</code>': </b> | ||
| 6227 | pushes onto the stack the function that is | ||
| 6228 | running at the given level; | ||
| 6229 | </li> | ||
| 6230 | |||
| 6231 | <li><b>'<code>l</code>': </b> fills in the field <code>currentline</code>; | ||
| 6232 | </li> | ||
| 6233 | |||
| 6234 | <li><b>'<code>n</code>': </b> fills in the fields <code>name</code> and <code>namewhat</code>; | ||
| 6235 | </li> | ||
| 6236 | |||
| 6237 | <li><b>'<code>r</code>': </b> fills in the fields <code>ftransfer</code> and <code>ntransfer</code>; | ||
| 6238 | </li> | ||
| 6239 | |||
| 6240 | <li><b>'<code>S</code>': </b> | ||
| 6241 | fills in the fields <code>source</code>, <code>short_src</code>, | ||
| 6242 | <code>linedefined</code>, <code>lastlinedefined</code>, and <code>what</code>; | ||
| 6243 | </li> | ||
| 6244 | |||
| 6245 | <li><b>'<code>t</code>': </b> fills in the field <code>istailcall</code>; | ||
| 6246 | </li> | ||
| 6247 | |||
| 6248 | <li><b>'<code>u</code>': </b> fills in the fields | ||
| 6249 | <code>nups</code>, <code>nparams</code>, and <code>isvararg</code>; | ||
| 6250 | </li> | ||
| 6251 | |||
| 6252 | <li><b>'<code>L</code>': </b> | ||
| 6253 | pushes onto the stack a table whose indices are | ||
| 6254 | the lines on the function with some associated code, | ||
| 6255 | that is, the lines where you can put a break point. | ||
| 6256 | (Lines with no code include empty lines and comments.) | ||
| 6257 | If this option is given together with option '<code>f</code>', | ||
| 6258 | its table is pushed after the function. | ||
| 6259 | This is the only option that can raise a memory error. | ||
| 6260 | </li> | ||
| 6261 | |||
| 6262 | </ul> | ||
| 6263 | |||
| 6264 | <p> | ||
| 6265 | This function returns 0 to signal an invalid option in <code>what</code>; | ||
| 6266 | even then the valid options are handled correctly. | ||
| 6267 | |||
| 6268 | |||
| 6269 | |||
| 6270 | |||
| 6271 | |||
| 6272 | <hr><h3><a name="lua_getlocal"><code>lua_getlocal</code></a></h3><p> | ||
| 6273 | <span class="apii">[-0, +(0|1), –]</span> | ||
| 6274 | <pre>const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n);</pre> | ||
| 6275 | |||
| 6276 | <p> | ||
| 6277 | Gets information about a local variable or a temporary value | ||
| 6278 | of a given activation record or a given function. | ||
| 6279 | |||
| 6280 | |||
| 6281 | <p> | ||
| 6282 | In the first case, | ||
| 6283 | the parameter <code>ar</code> must be a valid activation record that was | ||
| 6284 | filled by a previous call to <a href="#lua_getstack"><code>lua_getstack</code></a> or | ||
| 6285 | given as argument to a hook (see <a href="#lua_Hook"><code>lua_Hook</code></a>). | ||
| 6286 | The index <code>n</code> selects which local variable to inspect; | ||
| 6287 | see <a href="#pdf-debug.getlocal"><code>debug.getlocal</code></a> for details about variable indices | ||
| 6288 | and names. | ||
| 6289 | |||
| 6290 | |||
| 6291 | <p> | ||
| 6292 | <a href="#lua_getlocal"><code>lua_getlocal</code></a> pushes the variable's value onto the stack | ||
| 6293 | and returns its name. | ||
| 6294 | |||
| 6295 | |||
| 6296 | <p> | ||
| 6297 | In the second case, <code>ar</code> must be <code>NULL</code> and the function | ||
| 6298 | to be inspected must be on the top of the stack. | ||
| 6299 | In this case, only parameters of Lua functions are visible | ||
| 6300 | (as there is no information about what variables are active) | ||
| 6301 | and no values are pushed onto the stack. | ||
| 6302 | |||
| 6303 | |||
| 6304 | <p> | ||
| 6305 | Returns <code>NULL</code> (and pushes nothing) | ||
| 6306 | when the index is greater than | ||
| 6307 | the number of active local variables. | ||
| 6308 | |||
| 6309 | |||
| 6310 | |||
| 6311 | |||
| 6312 | |||
| 6313 | <hr><h3><a name="lua_getstack"><code>lua_getstack</code></a></h3><p> | ||
| 6314 | <span class="apii">[-0, +0, –]</span> | ||
| 6315 | <pre>int lua_getstack (lua_State *L, int level, lua_Debug *ar);</pre> | ||
| 6316 | |||
| 6317 | <p> | ||
| 6318 | Gets information about the interpreter runtime stack. | ||
| 6319 | |||
| 6320 | |||
| 6321 | <p> | ||
| 6322 | This function fills parts of a <a href="#lua_Debug"><code>lua_Debug</code></a> structure with | ||
| 6323 | an identification of the <em>activation record</em> | ||
| 6324 | of the function executing at a given level. | ||
| 6325 | Level 0 is the current running function, | ||
| 6326 | whereas level <em>n+1</em> is the function that has called level <em>n</em> | ||
| 6327 | (except for tail calls, which do not count in the stack). | ||
| 6328 | When called with a level greater than the stack depth, | ||
| 6329 | <a href="#lua_getstack"><code>lua_getstack</code></a> returns 0; | ||
| 6330 | otherwise it returns 1. | ||
| 6331 | |||
| 6332 | |||
| 6333 | |||
| 6334 | |||
| 6335 | |||
| 6336 | <hr><h3><a name="lua_getupvalue"><code>lua_getupvalue</code></a></h3><p> | ||
| 6337 | <span class="apii">[-0, +(0|1), –]</span> | ||
| 6338 | <pre>const char *lua_getupvalue (lua_State *L, int funcindex, int n);</pre> | ||
| 6339 | |||
| 6340 | <p> | ||
| 6341 | Gets information about the <code>n</code>-th upvalue | ||
| 6342 | of the closure at index <code>funcindex</code>. | ||
| 6343 | It pushes the upvalue's value onto the stack | ||
| 6344 | and returns its name. | ||
| 6345 | Returns <code>NULL</code> (and pushes nothing) | ||
| 6346 | when the index <code>n</code> is greater than the number of upvalues. | ||
| 6347 | |||
| 6348 | |||
| 6349 | <p> | ||
| 6350 | See <a href="#pdf-debug.getupvalue"><code>debug.getupvalue</code></a> for more information about upvalues. | ||
| 6351 | |||
| 6352 | |||
| 6353 | |||
| 6354 | |||
| 6355 | |||
| 6356 | <hr><h3><a name="lua_Hook"><code>lua_Hook</code></a></h3> | ||
| 6357 | <pre>typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);</pre> | ||
| 6358 | |||
| 6359 | <p> | ||
| 6360 | Type for debugging hook functions. | ||
| 6361 | |||
| 6362 | |||
| 6363 | <p> | ||
| 6364 | Whenever a hook is called, its <code>ar</code> argument has its field | ||
| 6365 | <code>event</code> set to the specific event that triggered the hook. | ||
| 6366 | Lua identifies these events with the following constants: | ||
| 6367 | <a name="pdf-LUA_HOOKCALL"><code>LUA_HOOKCALL</code></a>, <a name="pdf-LUA_HOOKRET"><code>LUA_HOOKRET</code></a>, | ||
| 6368 | <a name="pdf-LUA_HOOKTAILCALL"><code>LUA_HOOKTAILCALL</code></a>, <a name="pdf-LUA_HOOKLINE"><code>LUA_HOOKLINE</code></a>, | ||
| 6369 | and <a name="pdf-LUA_HOOKCOUNT"><code>LUA_HOOKCOUNT</code></a>. | ||
| 6370 | Moreover, for line events, the field <code>currentline</code> is also set. | ||
| 6371 | To get the value of any other field in <code>ar</code>, | ||
| 6372 | the hook must call <a href="#lua_getinfo"><code>lua_getinfo</code></a>. | ||
| 6373 | |||
| 6374 | |||
| 6375 | <p> | ||
| 6376 | For call events, <code>event</code> can be <code>LUA_HOOKCALL</code>, | ||
| 6377 | the normal value, or <code>LUA_HOOKTAILCALL</code>, for a tail call; | ||
| 6378 | in this case, there will be no corresponding return event. | ||
| 6379 | |||
| 6380 | |||
| 6381 | <p> | ||
| 6382 | While Lua is running a hook, it disables other calls to hooks. | ||
| 6383 | Therefore, if a hook calls back Lua to execute a function or a chunk, | ||
| 6384 | this execution occurs without any calls to hooks. | ||
| 6385 | |||
| 6386 | |||
| 6387 | <p> | ||
| 6388 | Hook functions cannot have continuations, | ||
| 6389 | that is, they cannot call <a href="#lua_yieldk"><code>lua_yieldk</code></a>, | ||
| 6390 | <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>. | ||
| 6391 | |||
| 6392 | |||
| 6393 | <p> | ||
| 6394 | Hook functions can yield under the following conditions: | ||
| 6395 | Only count and line events can yield; | ||
| 6396 | to yield, a hook function must finish its execution | ||
| 6397 | calling <a href="#lua_yield"><code>lua_yield</code></a> with <code>nresults</code> equal to zero | ||
| 6398 | (that is, with no values). | ||
| 6399 | |||
| 6400 | |||
| 6401 | |||
| 6402 | |||
| 6403 | |||
| 6404 | <hr><h3><a name="lua_sethook"><code>lua_sethook</code></a></h3><p> | ||
| 6405 | <span class="apii">[-0, +0, –]</span> | ||
| 6406 | <pre>void lua_sethook (lua_State *L, lua_Hook f, int mask, int count);</pre> | ||
| 6407 | |||
| 6408 | <p> | ||
| 6409 | Sets the debugging hook function. | ||
| 6410 | |||
| 6411 | |||
| 6412 | <p> | ||
| 6413 | Argument <code>f</code> is the hook function. | ||
| 6414 | <code>mask</code> specifies on which events the hook will be called: | ||
| 6415 | it is formed by a bitwise OR of the constants | ||
| 6416 | <a name="pdf-LUA_MASKCALL"><code>LUA_MASKCALL</code></a>, | ||
| 6417 | <a name="pdf-LUA_MASKRET"><code>LUA_MASKRET</code></a>, | ||
| 6418 | <a name="pdf-LUA_MASKLINE"><code>LUA_MASKLINE</code></a>, | ||
| 6419 | and <a name="pdf-LUA_MASKCOUNT"><code>LUA_MASKCOUNT</code></a>. | ||
| 6420 | The <code>count</code> argument is only meaningful when the mask | ||
| 6421 | includes <code>LUA_MASKCOUNT</code>. | ||
| 6422 | For each event, the hook is called as explained below: | ||
| 6423 | |||
| 6424 | <ul> | ||
| 6425 | |||
| 6426 | <li><b>The call hook: </b> is called when the interpreter calls a function. | ||
| 6427 | The hook is called just after Lua enters the new function. | ||
| 6428 | </li> | ||
| 6429 | |||
| 6430 | <li><b>The return hook: </b> is called when the interpreter returns from a function. | ||
| 6431 | The hook is called just before Lua leaves the function. | ||
| 6432 | </li> | ||
| 6433 | |||
| 6434 | <li><b>The line hook: </b> is called when the interpreter is about to | ||
| 6435 | start the execution of a new line of code, | ||
| 6436 | or when it jumps back in the code (even to the same line). | ||
| 6437 | This event only happens while Lua is executing a Lua function. | ||
| 6438 | </li> | ||
| 6439 | |||
| 6440 | <li><b>The count hook: </b> is called after the interpreter executes every | ||
| 6441 | <code>count</code> instructions. | ||
| 6442 | This event only happens while Lua is executing a Lua function. | ||
| 6443 | </li> | ||
| 6444 | |||
| 6445 | </ul> | ||
| 6446 | |||
| 6447 | <p> | ||
| 6448 | Hooks are disabled by setting <code>mask</code> to zero. | ||
| 6449 | |||
| 6450 | |||
| 6451 | |||
| 6452 | |||
| 6453 | |||
| 6454 | <hr><h3><a name="lua_setlocal"><code>lua_setlocal</code></a></h3><p> | ||
| 6455 | <span class="apii">[-(0|1), +0, –]</span> | ||
| 6456 | <pre>const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n);</pre> | ||
| 6457 | |||
| 6458 | <p> | ||
| 6459 | Sets the value of a local variable of a given activation record. | ||
| 6460 | It assigns the value on the top of the stack | ||
| 6461 | to the variable and returns its name. | ||
| 6462 | It also pops the value from the stack. | ||
| 6463 | |||
| 6464 | |||
| 6465 | <p> | ||
| 6466 | Returns <code>NULL</code> (and pops nothing) | ||
| 6467 | when the index is greater than | ||
| 6468 | the number of active local variables. | ||
| 6469 | |||
| 6470 | |||
| 6471 | <p> | ||
| 6472 | Parameters <code>ar</code> and <code>n</code> are as in the function <a href="#lua_getlocal"><code>lua_getlocal</code></a>. | ||
| 6473 | |||
| 6474 | |||
| 6475 | |||
| 6476 | |||
| 6477 | |||
| 6478 | <hr><h3><a name="lua_setupvalue"><code>lua_setupvalue</code></a></h3><p> | ||
| 6479 | <span class="apii">[-(0|1), +0, –]</span> | ||
| 6480 | <pre>const char *lua_setupvalue (lua_State *L, int funcindex, int n);</pre> | ||
| 6481 | |||
| 6482 | <p> | ||
| 6483 | Sets the value of a closure's upvalue. | ||
| 6484 | It assigns the value on the top of the stack | ||
| 6485 | to the upvalue and returns its name. | ||
| 6486 | It also pops the value from the stack. | ||
| 6487 | |||
| 6488 | |||
| 6489 | <p> | ||
| 6490 | Returns <code>NULL</code> (and pops nothing) | ||
| 6491 | when the index <code>n</code> is greater than the number of upvalues. | ||
| 6492 | |||
| 6493 | |||
| 6494 | <p> | ||
| 6495 | Parameters <code>funcindex</code> and <code>n</code> are as in | ||
| 6496 | the function <a href="#lua_getupvalue"><code>lua_getupvalue</code></a>. | ||
| 6497 | |||
| 6498 | |||
| 6499 | |||
| 6500 | |||
| 6501 | |||
| 6502 | <hr><h3><a name="lua_upvalueid"><code>lua_upvalueid</code></a></h3><p> | ||
| 6503 | <span class="apii">[-0, +0, –]</span> | ||
| 6504 | <pre>void *lua_upvalueid (lua_State *L, int funcindex, int n);</pre> | ||
| 6505 | |||
| 6506 | <p> | ||
| 6507 | Returns a unique identifier for the upvalue numbered <code>n</code> | ||
| 6508 | from the closure at index <code>funcindex</code>. | ||
| 6509 | |||
| 6510 | |||
| 6511 | <p> | ||
| 6512 | These unique identifiers allow a program to check whether different | ||
| 6513 | closures share upvalues. | ||
| 6514 | Lua closures that share an upvalue | ||
| 6515 | (that is, that access a same external local variable) | ||
| 6516 | will return identical ids for those upvalue indices. | ||
| 6517 | |||
| 6518 | |||
| 6519 | <p> | ||
| 6520 | Parameters <code>funcindex</code> and <code>n</code> are as in | ||
| 6521 | the function <a href="#lua_getupvalue"><code>lua_getupvalue</code></a>, | ||
| 6522 | but <code>n</code> cannot be greater than the number of upvalues. | ||
| 6523 | |||
| 6524 | |||
| 6525 | |||
| 6526 | |||
| 6527 | |||
| 6528 | <hr><h3><a name="lua_upvaluejoin"><code>lua_upvaluejoin</code></a></h3><p> | ||
| 6529 | <span class="apii">[-0, +0, –]</span> | ||
| 6530 | <pre>void lua_upvaluejoin (lua_State *L, int funcindex1, int n1, | ||
| 6531 | int funcindex2, int n2);</pre> | ||
| 6532 | |||
| 6533 | <p> | ||
| 6534 | Make the <code>n1</code>-th upvalue of the Lua closure at index <code>funcindex1</code> | ||
| 6535 | refer to the <code>n2</code>-th upvalue of the Lua closure at index <code>funcindex2</code>. | ||
| 6536 | |||
| 6537 | |||
| 6538 | |||
| 6539 | |||
| 6540 | |||
| 6541 | |||
| 6542 | |||
| 6543 | <h1>5 – <a name="5">The Auxiliary Library</a></h1> | ||
| 6544 | |||
| 6545 | |||
| 6546 | |||
| 6547 | <p> | ||
| 6548 | |||
| 6549 | The <em>auxiliary library</em> provides several convenient functions | ||
| 6550 | to interface C with Lua. | ||
| 6551 | While the basic API provides the primitive functions for all | ||
| 6552 | interactions between C and Lua, | ||
| 6553 | the auxiliary library provides higher-level functions for some | ||
| 6554 | common tasks. | ||
| 6555 | |||
| 6556 | |||
| 6557 | <p> | ||
| 6558 | All functions and types from the auxiliary library | ||
| 6559 | are defined in header file <code>lauxlib.h</code> and | ||
| 6560 | have a prefix <code>luaL_</code>. | ||
| 6561 | |||
| 6562 | |||
| 6563 | <p> | ||
| 6564 | All functions in the auxiliary library are built on | ||
| 6565 | top of the basic API, | ||
| 6566 | and so they provide nothing that cannot be done with that API. | ||
| 6567 | Nevertheless, the use of the auxiliary library ensures | ||
| 6568 | more consistency to your code. | ||
| 6569 | |||
| 6570 | |||
| 6571 | <p> | ||
| 6572 | Several functions in the auxiliary library use internally some | ||
| 6573 | extra stack slots. | ||
| 6574 | When a function in the auxiliary library uses less than five slots, | ||
| 6575 | it does not check the stack size; | ||
| 6576 | it simply assumes that there are enough slots. | ||
| 6577 | |||
| 6578 | |||
| 6579 | <p> | ||
| 6580 | Several functions in the auxiliary library are used to | ||
| 6581 | check C function arguments. | ||
| 6582 | Because the error message is formatted for arguments | ||
| 6583 | (e.g., "<code>bad argument #1</code>"), | ||
| 6584 | you should not use these functions for other stack values. | ||
| 6585 | |||
| 6586 | |||
| 6587 | <p> | ||
| 6588 | Functions called <code>luaL_check*</code> | ||
| 6589 | always raise an error if the check is not satisfied. | ||
| 6590 | |||
| 6591 | |||
| 6592 | |||
| 6593 | |||
| 6594 | |||
| 6595 | <h2>5.1 – <a name="5.1">Functions and Types</a></h2> | ||
| 6596 | |||
| 6597 | <p> | ||
| 6598 | Here we list all functions and types from the auxiliary library | ||
| 6599 | in alphabetical order. | ||
| 6600 | |||
| 6601 | |||
| 6602 | |||
| 6603 | <hr><h3><a name="luaL_addchar"><code>luaL_addchar</code></a></h3><p> | ||
| 6604 | <span class="apii">[-?, +?, <em>m</em>]</span> | ||
| 6605 | <pre>void luaL_addchar (luaL_Buffer *B, char c);</pre> | ||
| 6606 | |||
| 6607 | <p> | ||
| 6608 | Adds the byte <code>c</code> to the buffer <code>B</code> | ||
| 6609 | (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>). | ||
| 6610 | |||
| 6611 | |||
| 6612 | |||
| 6613 | |||
| 6614 | |||
| 6615 | <hr><h3><a name="luaL_addgsub"><code>luaL_addgsub</code></a></h3><p> | ||
| 6616 | <span class="apii">[-?, +?, <em>m</em>]</span> | ||
| 6617 | <pre>const void luaL_addgsub (luaL_Buffer *B, const char *s, | ||
| 6618 | const char *p, const char *r);</pre> | ||
| 6619 | |||
| 6620 | <p> | ||
| 6621 | Adds a copy of the string <code>s</code> to the buffer <code>B</code> (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>), | ||
| 6622 | replacing any occurrence of the string <code>p</code> | ||
| 6623 | with the string <code>r</code>. | ||
| 6624 | |||
| 6625 | |||
| 6626 | |||
| 6627 | |||
| 6628 | |||
| 6629 | <hr><h3><a name="luaL_addlstring"><code>luaL_addlstring</code></a></h3><p> | ||
| 6630 | <span class="apii">[-?, +?, <em>m</em>]</span> | ||
| 6631 | <pre>void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l);</pre> | ||
| 6632 | |||
| 6633 | <p> | ||
| 6634 | Adds the string pointed to by <code>s</code> with length <code>l</code> to | ||
| 6635 | the buffer <code>B</code> | ||
| 6636 | (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>). | ||
| 6637 | The string can contain embedded zeros. | ||
| 6638 | |||
| 6639 | |||
| 6640 | |||
| 6641 | |||
| 6642 | |||
| 6643 | <hr><h3><a name="luaL_addsize"><code>luaL_addsize</code></a></h3><p> | ||
| 6644 | <span class="apii">[-?, +?, –]</span> | ||
| 6645 | <pre>void luaL_addsize (luaL_Buffer *B, size_t n);</pre> | ||
| 6646 | |||
| 6647 | <p> | ||
| 6648 | Adds to the buffer <code>B</code> | ||
| 6649 | a string of length <code>n</code> previously copied to the | ||
| 6650 | buffer area (see <a href="#luaL_prepbuffer"><code>luaL_prepbuffer</code></a>). | ||
| 6651 | |||
| 6652 | |||
| 6653 | |||
| 6654 | |||
| 6655 | |||
| 6656 | <hr><h3><a name="luaL_addstring"><code>luaL_addstring</code></a></h3><p> | ||
| 6657 | <span class="apii">[-?, +?, <em>m</em>]</span> | ||
| 6658 | <pre>void luaL_addstring (luaL_Buffer *B, const char *s);</pre> | ||
| 6659 | |||
| 6660 | <p> | ||
| 6661 | Adds the zero-terminated string pointed to by <code>s</code> | ||
| 6662 | to the buffer <code>B</code> | ||
| 6663 | (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>). | ||
| 6664 | |||
| 6665 | |||
| 6666 | |||
| 6667 | |||
| 6668 | |||
| 6669 | <hr><h3><a name="luaL_addvalue"><code>luaL_addvalue</code></a></h3><p> | ||
| 6670 | <span class="apii">[-?, +?, <em>m</em>]</span> | ||
| 6671 | <pre>void luaL_addvalue (luaL_Buffer *B);</pre> | ||
| 6672 | |||
| 6673 | <p> | ||
| 6674 | Adds the value on the top of the stack | ||
| 6675 | to the buffer <code>B</code> | ||
| 6676 | (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>). | ||
| 6677 | Pops the value. | ||
| 6678 | |||
| 6679 | |||
| 6680 | <p> | ||
| 6681 | This is the only function on string buffers that can (and must) | ||
| 6682 | be called with an extra element on the stack, | ||
| 6683 | which is the value to be added to the buffer. | ||
| 6684 | |||
| 6685 | |||
| 6686 | |||
| 6687 | |||
| 6688 | |||
| 6689 | <hr><h3><a name="luaL_argcheck"><code>luaL_argcheck</code></a></h3><p> | ||
| 6690 | <span class="apii">[-0, +0, <em>v</em>]</span> | ||
| 6691 | <pre>void luaL_argcheck (lua_State *L, | ||
| 6692 | int cond, | ||
| 6693 | int arg, | ||
| 6694 | const char *extramsg);</pre> | ||
| 6695 | |||
| 6696 | <p> | ||
| 6697 | Checks whether <code>cond</code> is true. | ||
| 6698 | If it is not, raises an error with a standard message (see <a href="#luaL_argerror"><code>luaL_argerror</code></a>). | ||
| 6699 | |||
| 6700 | |||
| 6701 | |||
| 6702 | |||
| 6703 | |||
| 6704 | <hr><h3><a name="luaL_argerror"><code>luaL_argerror</code></a></h3><p> | ||
| 6705 | <span class="apii">[-0, +0, <em>v</em>]</span> | ||
| 6706 | <pre>int luaL_argerror (lua_State *L, int arg, const char *extramsg);</pre> | ||
| 6707 | |||
| 6708 | <p> | ||
| 6709 | Raises an error reporting a problem with argument <code>arg</code> | ||
| 6710 | of the C function that called it, | ||
| 6711 | using a standard message | ||
| 6712 | that includes <code>extramsg</code> as a comment: | ||
| 6713 | |||
| 6714 | <pre> | ||
| 6715 | bad argument #<em>arg</em> to '<em>funcname</em>' (<em>extramsg</em>) | ||
| 6716 | </pre><p> | ||
| 6717 | This function never returns. | ||
| 6718 | |||
| 6719 | |||
| 6720 | |||
| 6721 | |||
| 6722 | |||
| 6723 | <hr><h3><a name="luaL_argexpected"><code>luaL_argexpected</code></a></h3><p> | ||
| 6724 | <span class="apii">[-0, +0, <em>v</em>]</span> | ||
| 6725 | <pre>void luaL_argexpected (lua_State *L, | ||
| 6726 | int cond, | ||
| 6727 | int arg, | ||
| 6728 | const char *tname);</pre> | ||
| 6729 | |||
| 6730 | <p> | ||
| 6731 | Checks whether <code>cond</code> is true. | ||
| 6732 | If it is not, raises an error about the type of the argument <code>arg</code> | ||
| 6733 | with a standard message (see <a href="#luaL_typeerror"><code>luaL_typeerror</code></a>). | ||
| 6734 | |||
| 6735 | |||
| 6736 | |||
| 6737 | |||
| 6738 | |||
| 6739 | <hr><h3><a name="luaL_Buffer"><code>luaL_Buffer</code></a></h3> | ||
| 6740 | <pre>typedef struct luaL_Buffer luaL_Buffer;</pre> | ||
| 6741 | |||
| 6742 | <p> | ||
| 6743 | Type for a <em>string buffer</em>. | ||
| 6744 | |||
| 6745 | |||
| 6746 | <p> | ||
| 6747 | A string buffer allows C code to build Lua strings piecemeal. | ||
| 6748 | Its pattern of use is as follows: | ||
| 6749 | |||
| 6750 | <ul> | ||
| 6751 | |||
| 6752 | <li>First declare a variable <code>b</code> of type <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>.</li> | ||
| 6753 | |||
| 6754 | <li>Then initialize it with a call <code>luaL_buffinit(L, &b)</code>.</li> | ||
| 6755 | |||
| 6756 | <li> | ||
| 6757 | Then add string pieces to the buffer calling any of | ||
| 6758 | the <code>luaL_add*</code> functions. | ||
| 6759 | </li> | ||
| 6760 | |||
| 6761 | <li> | ||
| 6762 | Finish by calling <code>luaL_pushresult(&b)</code>. | ||
| 6763 | This call leaves the final string on the top of the stack. | ||
| 6764 | </li> | ||
| 6765 | |||
| 6766 | </ul> | ||
| 6767 | |||
| 6768 | <p> | ||
| 6769 | If you know beforehand the maximum size of the resulting string, | ||
| 6770 | you can use the buffer like this: | ||
| 6771 | |||
| 6772 | <ul> | ||
| 6773 | |||
| 6774 | <li>First declare a variable <code>b</code> of type <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>.</li> | ||
| 6775 | |||
| 6776 | <li>Then initialize it and preallocate a space of | ||
| 6777 | size <code>sz</code> with a call <code>luaL_buffinitsize(L, &b, sz)</code>.</li> | ||
| 6778 | |||
| 6779 | <li>Then produce the string into that space.</li> | ||
| 6780 | |||
| 6781 | <li> | ||
| 6782 | Finish by calling <code>luaL_pushresultsize(&b, sz)</code>, | ||
| 6783 | where <code>sz</code> is the total size of the resulting string | ||
| 6784 | copied into that space (which may be less than or | ||
| 6785 | equal to the preallocated size). | ||
| 6786 | </li> | ||
| 6787 | |||
| 6788 | </ul> | ||
| 6789 | |||
| 6790 | <p> | ||
| 6791 | During its normal operation, | ||
| 6792 | a string buffer uses a variable number of stack slots. | ||
| 6793 | So, while using a buffer, you cannot assume that you know where | ||
| 6794 | the top of the stack is. | ||
| 6795 | You can use the stack between successive calls to buffer operations | ||
| 6796 | as long as that use is balanced; | ||
| 6797 | that is, | ||
| 6798 | when you call a buffer operation, | ||
| 6799 | the stack is at the same level | ||
| 6800 | it was immediately after the previous buffer operation. | ||
| 6801 | (The only exception to this rule is <a href="#luaL_addvalue"><code>luaL_addvalue</code></a>.) | ||
| 6802 | After calling <a href="#luaL_pushresult"><code>luaL_pushresult</code></a>, | ||
| 6803 | the stack is back to its level when the buffer was initialized, | ||
| 6804 | plus the final string on its top. | ||
| 6805 | |||
| 6806 | |||
| 6807 | |||
| 6808 | |||
| 6809 | |||
| 6810 | <hr><h3><a name="luaL_buffaddr"><code>luaL_buffaddr</code></a></h3><p> | ||
| 6811 | <span class="apii">[-0, +0, –]</span> | ||
| 6812 | <pre>char *luaL_buffaddr (luaL_Buffer *B);</pre> | ||
| 6813 | |||
| 6814 | <p> | ||
| 6815 | Returns the address of the current content of buffer <code>B</code> | ||
| 6816 | (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>). | ||
| 6817 | Note that any addition to the buffer may invalidate this address. | ||
| 6818 | |||
| 6819 | |||
| 6820 | |||
| 6821 | |||
| 6822 | |||
| 6823 | <hr><h3><a name="luaL_buffinit"><code>luaL_buffinit</code></a></h3><p> | ||
| 6824 | <span class="apii">[-0, +?, –]</span> | ||
| 6825 | <pre>void luaL_buffinit (lua_State *L, luaL_Buffer *B);</pre> | ||
| 6826 | |||
| 6827 | <p> | ||
| 6828 | Initializes a buffer <code>B</code> | ||
| 6829 | (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>). | ||
| 6830 | This function does not allocate any space; | ||
| 6831 | the buffer must be declared as a variable. | ||
| 6832 | |||
| 6833 | |||
| 6834 | |||
| 6835 | |||
| 6836 | |||
| 6837 | <hr><h3><a name="luaL_bufflen"><code>luaL_bufflen</code></a></h3><p> | ||
| 6838 | <span class="apii">[-0, +0, –]</span> | ||
| 6839 | <pre>size_t luaL_bufflen (luaL_Buffer *B);</pre> | ||
| 6840 | |||
| 6841 | <p> | ||
| 6842 | Returns the length of the current content of buffer <code>B</code> | ||
| 6843 | (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>). | ||
| 6844 | |||
| 6845 | |||
| 6846 | |||
| 6847 | |||
| 6848 | |||
| 6849 | <hr><h3><a name="luaL_buffinitsize"><code>luaL_buffinitsize</code></a></h3><p> | ||
| 6850 | <span class="apii">[-?, +?, <em>m</em>]</span> | ||
| 6851 | <pre>char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz);</pre> | ||
| 6852 | |||
| 6853 | <p> | ||
| 6854 | Equivalent to the sequence | ||
| 6855 | <a href="#luaL_buffinit"><code>luaL_buffinit</code></a>, <a href="#luaL_prepbuffsize"><code>luaL_prepbuffsize</code></a>. | ||
| 6856 | |||
| 6857 | |||
| 6858 | |||
| 6859 | |||
| 6860 | |||
| 6861 | <hr><h3><a name="luaL_buffsub"><code>luaL_buffsub</code></a></h3><p> | ||
| 6862 | <span class="apii">[-?, +?, –]</span> | ||
| 6863 | <pre>void luaL_buffsub (luaL_Buffer *B, int n);</pre> | ||
| 6864 | |||
| 6865 | <p> | ||
| 6866 | Removes <code>n</code> bytes from the buffer <code>B</code> | ||
| 6867 | (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>). | ||
| 6868 | The buffer must have at least that many bytes. | ||
| 6869 | |||
| 6870 | |||
| 6871 | |||
| 6872 | |||
| 6873 | |||
| 6874 | <hr><h3><a name="luaL_callmeta"><code>luaL_callmeta</code></a></h3><p> | ||
| 6875 | <span class="apii">[-0, +(0|1), <em>e</em>]</span> | ||
| 6876 | <pre>int luaL_callmeta (lua_State *L, int obj, const char *e);</pre> | ||
| 6877 | |||
| 6878 | <p> | ||
| 6879 | Calls a metamethod. | ||
| 6880 | |||
| 6881 | |||
| 6882 | <p> | ||
| 6883 | If the object at index <code>obj</code> has a metatable and this | ||
| 6884 | metatable has a field <code>e</code>, | ||
| 6885 | this function calls this field passing the object as its only argument. | ||
| 6886 | In this case this function returns true and pushes onto the | ||
| 6887 | stack the value returned by the call. | ||
| 6888 | If there is no metatable or no metamethod, | ||
| 6889 | this function returns false without pushing any value on the stack. | ||
| 6890 | |||
| 6891 | |||
| 6892 | |||
| 6893 | |||
| 6894 | |||
| 6895 | <hr><h3><a name="luaL_checkany"><code>luaL_checkany</code></a></h3><p> | ||
| 6896 | <span class="apii">[-0, +0, <em>v</em>]</span> | ||
| 6897 | <pre>void luaL_checkany (lua_State *L, int arg);</pre> | ||
| 6898 | |||
| 6899 | <p> | ||
| 6900 | Checks whether the function has an argument | ||
| 6901 | of any type (including <b>nil</b>) at position <code>arg</code>. | ||
| 6902 | |||
| 6903 | |||
| 6904 | |||
| 6905 | |||
| 6906 | |||
| 6907 | <hr><h3><a name="luaL_checkinteger"><code>luaL_checkinteger</code></a></h3><p> | ||
| 6908 | <span class="apii">[-0, +0, <em>v</em>]</span> | ||
| 6909 | <pre>lua_Integer luaL_checkinteger (lua_State *L, int arg);</pre> | ||
| 6910 | |||
| 6911 | <p> | ||
| 6912 | Checks whether the function argument <code>arg</code> is an integer | ||
| 6913 | (or can be converted to an integer) | ||
| 6914 | and returns this integer. | ||
| 6915 | |||
| 6916 | |||
| 6917 | |||
| 6918 | |||
| 6919 | |||
| 6920 | <hr><h3><a name="luaL_checklstring"><code>luaL_checklstring</code></a></h3><p> | ||
| 6921 | <span class="apii">[-0, +0, <em>v</em>]</span> | ||
| 6922 | <pre>const char *luaL_checklstring (lua_State *L, int arg, size_t *l);</pre> | ||
| 6923 | |||
| 6924 | <p> | ||
| 6925 | Checks whether the function argument <code>arg</code> is a string | ||
| 6926 | and returns this string; | ||
| 6927 | if <code>l</code> is not <code>NULL</code> fills its referent | ||
| 6928 | with the string's length. | ||
| 6929 | |||
| 6930 | |||
| 6931 | <p> | ||
| 6932 | This function uses <a href="#lua_tolstring"><code>lua_tolstring</code></a> to get its result, | ||
| 6933 | so all conversions and caveats of that function apply here. | ||
| 6934 | |||
| 6935 | |||
| 6936 | |||
| 6937 | |||
| 6938 | |||
| 6939 | <hr><h3><a name="luaL_checknumber"><code>luaL_checknumber</code></a></h3><p> | ||
| 6940 | <span class="apii">[-0, +0, <em>v</em>]</span> | ||
| 6941 | <pre>lua_Number luaL_checknumber (lua_State *L, int arg);</pre> | ||
| 6942 | |||
| 6943 | <p> | ||
| 6944 | Checks whether the function argument <code>arg</code> is a number | ||
| 6945 | and returns this number converted to a <code>lua_Number</code>. | ||
| 6946 | |||
| 6947 | |||
| 6948 | |||
| 6949 | |||
| 6950 | |||
| 6951 | <hr><h3><a name="luaL_checkoption"><code>luaL_checkoption</code></a></h3><p> | ||
| 6952 | <span class="apii">[-0, +0, <em>v</em>]</span> | ||
| 6953 | <pre>int luaL_checkoption (lua_State *L, | ||
| 6954 | int arg, | ||
| 6955 | const char *def, | ||
| 6956 | const char *const lst[]);</pre> | ||
| 6957 | |||
| 6958 | <p> | ||
| 6959 | Checks whether the function argument <code>arg</code> is a string and | ||
| 6960 | searches for this string in the array <code>lst</code> | ||
| 6961 | (which must be NULL-terminated). | ||
| 6962 | Returns the index in the array where the string was found. | ||
| 6963 | Raises an error if the argument is not a string or | ||
| 6964 | if the string cannot be found. | ||
| 6965 | |||
| 6966 | |||
| 6967 | <p> | ||
| 6968 | If <code>def</code> is not <code>NULL</code>, | ||
| 6969 | the function uses <code>def</code> as a default value when | ||
| 6970 | there is no argument <code>arg</code> or when this argument is <b>nil</b>. | ||
| 6971 | |||
| 6972 | |||
| 6973 | <p> | ||
| 6974 | This is a useful function for mapping strings to C enums. | ||
| 6975 | (The usual convention in Lua libraries is | ||
| 6976 | to use strings instead of numbers to select options.) | ||
| 6977 | |||
| 6978 | |||
| 6979 | |||
| 6980 | |||
| 6981 | |||
| 6982 | <hr><h3><a name="luaL_checkstack"><code>luaL_checkstack</code></a></h3><p> | ||
| 6983 | <span class="apii">[-0, +0, <em>v</em>]</span> | ||
| 6984 | <pre>void luaL_checkstack (lua_State *L, int sz, const char *msg);</pre> | ||
| 6985 | |||
| 6986 | <p> | ||
| 6987 | Grows the stack size to <code>top + sz</code> elements, | ||
| 6988 | raising an error if the stack cannot grow to that size. | ||
| 6989 | <code>msg</code> is an additional text to go into the error message | ||
| 6990 | (or <code>NULL</code> for no additional text). | ||
| 6991 | |||
| 6992 | |||
| 6993 | |||
| 6994 | |||
| 6995 | |||
| 6996 | <hr><h3><a name="luaL_checkstring"><code>luaL_checkstring</code></a></h3><p> | ||
| 6997 | <span class="apii">[-0, +0, <em>v</em>]</span> | ||
| 6998 | <pre>const char *luaL_checkstring (lua_State *L, int arg);</pre> | ||
| 6999 | |||
| 7000 | <p> | ||
| 7001 | Checks whether the function argument <code>arg</code> is a string | ||
| 7002 | and returns this string. | ||
| 7003 | |||
| 7004 | |||
| 7005 | <p> | ||
| 7006 | This function uses <a href="#lua_tolstring"><code>lua_tolstring</code></a> to get its result, | ||
| 7007 | so all conversions and caveats of that function apply here. | ||
| 7008 | |||
| 7009 | |||
| 7010 | |||
| 7011 | |||
| 7012 | |||
| 7013 | <hr><h3><a name="luaL_checktype"><code>luaL_checktype</code></a></h3><p> | ||
| 7014 | <span class="apii">[-0, +0, <em>v</em>]</span> | ||
| 7015 | <pre>void luaL_checktype (lua_State *L, int arg, int t);</pre> | ||
| 7016 | |||
| 7017 | <p> | ||
| 7018 | Checks whether the function argument <code>arg</code> has type <code>t</code>. | ||
| 7019 | See <a href="#lua_type"><code>lua_type</code></a> for the encoding of types for <code>t</code>. | ||
| 7020 | |||
| 7021 | |||
| 7022 | |||
| 7023 | |||
| 7024 | |||
| 7025 | <hr><h3><a name="luaL_checkudata"><code>luaL_checkudata</code></a></h3><p> | ||
| 7026 | <span class="apii">[-0, +0, <em>v</em>]</span> | ||
| 7027 | <pre>void *luaL_checkudata (lua_State *L, int arg, const char *tname);</pre> | ||
| 7028 | |||
| 7029 | <p> | ||
| 7030 | Checks whether the function argument <code>arg</code> is a userdata | ||
| 7031 | of the type <code>tname</code> (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>) and | ||
| 7032 | returns the userdata's memory-block address (see <a href="#lua_touserdata"><code>lua_touserdata</code></a>). | ||
| 7033 | |||
| 7034 | |||
| 7035 | |||
| 7036 | |||
| 7037 | |||
| 7038 | <hr><h3><a name="luaL_checkversion"><code>luaL_checkversion</code></a></h3><p> | ||
| 7039 | <span class="apii">[-0, +0, <em>v</em>]</span> | ||
| 7040 | <pre>void luaL_checkversion (lua_State *L);</pre> | ||
| 7041 | |||
| 7042 | <p> | ||
| 7043 | Checks whether the code making the call and the Lua library being called | ||
| 7044 | are using the same version of Lua and the same numeric types. | ||
| 7045 | |||
| 7046 | |||
| 7047 | |||
| 7048 | |||
| 7049 | |||
| 7050 | <hr><h3><a name="luaL_dofile"><code>luaL_dofile</code></a></h3><p> | ||
| 7051 | <span class="apii">[-0, +?, <em>m</em>]</span> | ||
| 7052 | <pre>int luaL_dofile (lua_State *L, const char *filename);</pre> | ||
| 7053 | |||
| 7054 | <p> | ||
| 7055 | Loads and runs the given file. | ||
| 7056 | It is defined as the following macro: | ||
| 7057 | |||
| 7058 | <pre> | ||
| 7059 | (luaL_loadfile(L, filename) || lua_pcall(L, 0, LUA_MULTRET, 0)) | ||
| 7060 | </pre><p> | ||
| 7061 | It returns 0 (<a href="#pdf-LUA_OK"><code>LUA_OK</code></a>) if there are no errors, | ||
| 7062 | or 1 in case of errors. | ||
| 7063 | |||
| 7064 | |||
| 7065 | |||
| 7066 | |||
| 7067 | |||
| 7068 | <hr><h3><a name="luaL_dostring"><code>luaL_dostring</code></a></h3><p> | ||
| 7069 | <span class="apii">[-0, +?, –]</span> | ||
| 7070 | <pre>int luaL_dostring (lua_State *L, const char *str);</pre> | ||
| 7071 | |||
| 7072 | <p> | ||
| 7073 | Loads and runs the given string. | ||
| 7074 | It is defined as the following macro: | ||
| 7075 | |||
| 7076 | <pre> | ||
| 7077 | (luaL_loadstring(L, str) || lua_pcall(L, 0, LUA_MULTRET, 0)) | ||
| 7078 | </pre><p> | ||
| 7079 | It returns 0 (<a href="#pdf-LUA_OK"><code>LUA_OK</code></a>) if there are no errors, | ||
| 7080 | or 1 in case of errors. | ||
| 7081 | |||
| 7082 | |||
| 7083 | |||
| 7084 | |||
| 7085 | |||
| 7086 | <hr><h3><a name="luaL_error"><code>luaL_error</code></a></h3><p> | ||
| 7087 | <span class="apii">[-0, +0, <em>v</em>]</span> | ||
| 7088 | <pre>int luaL_error (lua_State *L, const char *fmt, ...);</pre> | ||
| 7089 | |||
| 7090 | <p> | ||
| 7091 | Raises an error. | ||
| 7092 | The error message format is given by <code>fmt</code> | ||
| 7093 | plus any extra arguments, | ||
| 7094 | following the same rules of <a href="#lua_pushfstring"><code>lua_pushfstring</code></a>. | ||
| 7095 | It also adds at the beginning of the message the file name and | ||
| 7096 | the line number where the error occurred, | ||
| 7097 | if this information is available. | ||
| 7098 | |||
| 7099 | |||
| 7100 | <p> | ||
| 7101 | This function never returns, | ||
| 7102 | but it is an idiom to use it in C functions | ||
| 7103 | as <code>return luaL_error(<em>args</em>)</code>. | ||
| 7104 | |||
| 7105 | |||
| 7106 | |||
| 7107 | |||
| 7108 | |||
| 7109 | <hr><h3><a name="luaL_execresult"><code>luaL_execresult</code></a></h3><p> | ||
| 7110 | <span class="apii">[-0, +3, <em>m</em>]</span> | ||
| 7111 | <pre>int luaL_execresult (lua_State *L, int stat);</pre> | ||
| 7112 | |||
| 7113 | <p> | ||
| 7114 | This function produces the return values for | ||
| 7115 | process-related functions in the standard library | ||
| 7116 | (<a href="#pdf-os.execute"><code>os.execute</code></a> and <a href="#pdf-io.close"><code>io.close</code></a>). | ||
| 7117 | |||
| 7118 | |||
| 7119 | |||
| 7120 | |||
| 7121 | |||
| 7122 | <hr><h3><a name="luaL_fileresult"><code>luaL_fileresult</code></a></h3><p> | ||
| 7123 | <span class="apii">[-0, +(1|3), <em>m</em>]</span> | ||
| 7124 | <pre>int luaL_fileresult (lua_State *L, int stat, const char *fname);</pre> | ||
| 7125 | |||
| 7126 | <p> | ||
| 7127 | This function produces the return values for | ||
| 7128 | file-related functions in the standard library | ||
| 7129 | (<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.). | ||
| 7130 | |||
| 7131 | |||
| 7132 | |||
| 7133 | |||
| 7134 | |||
| 7135 | <hr><h3><a name="luaL_getmetafield"><code>luaL_getmetafield</code></a></h3><p> | ||
| 7136 | <span class="apii">[-0, +(0|1), <em>m</em>]</span> | ||
| 7137 | <pre>int luaL_getmetafield (lua_State *L, int obj, const char *e);</pre> | ||
| 7138 | |||
| 7139 | <p> | ||
| 7140 | Pushes onto the stack the field <code>e</code> from the metatable | ||
| 7141 | of the object at index <code>obj</code> and returns the type of the pushed value. | ||
| 7142 | If the object does not have a metatable, | ||
| 7143 | or if the metatable does not have this field, | ||
| 7144 | pushes nothing and returns <code>LUA_TNIL</code>. | ||
| 7145 | |||
| 7146 | |||
| 7147 | |||
| 7148 | |||
| 7149 | |||
| 7150 | <hr><h3><a name="luaL_getmetatable"><code>luaL_getmetatable</code></a></h3><p> | ||
| 7151 | <span class="apii">[-0, +1, <em>m</em>]</span> | ||
| 7152 | <pre>int luaL_getmetatable (lua_State *L, const char *tname);</pre> | ||
| 7153 | |||
| 7154 | <p> | ||
| 7155 | Pushes onto the stack the metatable associated with the name <code>tname</code> | ||
| 7156 | in the registry (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>), | ||
| 7157 | or <b>nil</b> if there is no metatable associated with that name. | ||
| 7158 | Returns the type of the pushed value. | ||
| 7159 | |||
| 7160 | |||
| 7161 | |||
| 7162 | |||
| 7163 | |||
| 7164 | <hr><h3><a name="luaL_getsubtable"><code>luaL_getsubtable</code></a></h3><p> | ||
| 7165 | <span class="apii">[-0, +1, <em>e</em>]</span> | ||
| 7166 | <pre>int luaL_getsubtable (lua_State *L, int idx, const char *fname);</pre> | ||
| 7167 | |||
| 7168 | <p> | ||
| 7169 | Ensures that the value <code>t[fname]</code>, | ||
| 7170 | where <code>t</code> is the value at index <code>idx</code>, | ||
| 7171 | is a table, | ||
| 7172 | and pushes that table onto the stack. | ||
| 7173 | Returns true if it finds a previous table there | ||
| 7174 | and false if it creates a new table. | ||
| 7175 | |||
| 7176 | |||
| 7177 | |||
| 7178 | |||
| 7179 | |||
| 7180 | <hr><h3><a name="luaL_gsub"><code>luaL_gsub</code></a></h3><p> | ||
| 7181 | <span class="apii">[-0, +1, <em>m</em>]</span> | ||
| 7182 | <pre>const char *luaL_gsub (lua_State *L, | ||
| 7183 | const char *s, | ||
| 7184 | const char *p, | ||
| 7185 | const char *r);</pre> | ||
| 7186 | |||
| 7187 | <p> | ||
| 7188 | Creates a copy of string <code>s</code>, | ||
| 7189 | replacing any occurrence of the string <code>p</code> | ||
| 7190 | with the string <code>r</code>. | ||
| 7191 | Pushes the resulting string on the stack and returns it. | ||
| 7192 | |||
| 7193 | |||
| 7194 | |||
| 7195 | |||
| 7196 | |||
| 7197 | <hr><h3><a name="luaL_len"><code>luaL_len</code></a></h3><p> | ||
| 7198 | <span class="apii">[-0, +0, <em>e</em>]</span> | ||
| 7199 | <pre>lua_Integer luaL_len (lua_State *L, int index);</pre> | ||
| 7200 | |||
| 7201 | <p> | ||
| 7202 | Returns the "length" of the value at the given index | ||
| 7203 | as a number; | ||
| 7204 | it is equivalent to the '<code>#</code>' operator in Lua (see <a href="#3.4.7">§3.4.7</a>). | ||
| 7205 | Raises an error if the result of the operation is not an integer. | ||
| 7206 | (This case can only happen through metamethods.) | ||
| 7207 | |||
| 7208 | |||
| 7209 | |||
| 7210 | |||
| 7211 | |||
| 7212 | <hr><h3><a name="luaL_loadbuffer"><code>luaL_loadbuffer</code></a></h3><p> | ||
| 7213 | <span class="apii">[-0, +1, –]</span> | ||
| 7214 | <pre>int luaL_loadbuffer (lua_State *L, | ||
| 7215 | const char *buff, | ||
| 7216 | size_t sz, | ||
| 7217 | const char *name);</pre> | ||
| 7218 | |||
| 7219 | <p> | ||
| 7220 | Equivalent to <a href="#luaL_loadbufferx"><code>luaL_loadbufferx</code></a> with <code>mode</code> equal to <code>NULL</code>. | ||
| 7221 | |||
| 7222 | |||
| 7223 | |||
| 7224 | |||
| 7225 | |||
| 7226 | <hr><h3><a name="luaL_loadbufferx"><code>luaL_loadbufferx</code></a></h3><p> | ||
| 7227 | <span class="apii">[-0, +1, –]</span> | ||
| 7228 | <pre>int luaL_loadbufferx (lua_State *L, | ||
| 7229 | const char *buff, | ||
| 7230 | size_t sz, | ||
| 7231 | const char *name, | ||
| 7232 | const char *mode);</pre> | ||
| 7233 | |||
| 7234 | <p> | ||
| 7235 | Loads a buffer as a Lua chunk. | ||
| 7236 | This function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in the | ||
| 7237 | buffer pointed to by <code>buff</code> with size <code>sz</code>. | ||
| 7238 | |||
| 7239 | |||
| 7240 | <p> | ||
| 7241 | This function returns the same results as <a href="#lua_load"><code>lua_load</code></a>. | ||
| 7242 | <code>name</code> is the chunk name, | ||
| 7243 | used for debug information and error messages. | ||
| 7244 | The string <code>mode</code> works as in the function <a href="#lua_load"><code>lua_load</code></a>. | ||
| 7245 | |||
| 7246 | |||
| 7247 | |||
| 7248 | |||
| 7249 | |||
| 7250 | <hr><h3><a name="luaL_loadfile"><code>luaL_loadfile</code></a></h3><p> | ||
| 7251 | <span class="apii">[-0, +1, <em>m</em>]</span> | ||
| 7252 | <pre>int luaL_loadfile (lua_State *L, const char *filename);</pre> | ||
| 7253 | |||
| 7254 | <p> | ||
| 7255 | Equivalent to <a href="#luaL_loadfilex"><code>luaL_loadfilex</code></a> with <code>mode</code> equal to <code>NULL</code>. | ||
| 7256 | |||
| 7257 | |||
| 7258 | |||
| 7259 | |||
| 7260 | |||
| 7261 | <hr><h3><a name="luaL_loadfilex"><code>luaL_loadfilex</code></a></h3><p> | ||
| 7262 | <span class="apii">[-0, +1, <em>m</em>]</span> | ||
| 7263 | <pre>int luaL_loadfilex (lua_State *L, const char *filename, | ||
| 7264 | const char *mode);</pre> | ||
| 7265 | |||
| 7266 | <p> | ||
| 7267 | Loads a file as a Lua chunk. | ||
| 7268 | This function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in the file | ||
| 7269 | named <code>filename</code>. | ||
| 7270 | If <code>filename</code> is <code>NULL</code>, | ||
| 7271 | then it loads from the standard input. | ||
| 7272 | The first line in the file is ignored if it starts with a <code>#</code>. | ||
| 7273 | |||
| 7274 | |||
| 7275 | <p> | ||
| 7276 | The string <code>mode</code> works as in the function <a href="#lua_load"><code>lua_load</code></a>. | ||
| 7277 | |||
| 7278 | |||
| 7279 | <p> | ||
| 7280 | This function returns the same results as <a href="#lua_load"><code>lua_load</code></a> | ||
| 7281 | or <a href="#pdf-LUA_ERRFILE"><code>LUA_ERRFILE</code></a> for file-related errors. | ||
| 7282 | |||
| 7283 | |||
| 7284 | <p> | ||
| 7285 | As <a href="#lua_load"><code>lua_load</code></a>, this function only loads the chunk; | ||
| 7286 | it does not run it. | ||
| 7287 | |||
| 7288 | |||
| 7289 | |||
| 7290 | |||
| 7291 | |||
| 7292 | <hr><h3><a name="luaL_loadstring"><code>luaL_loadstring</code></a></h3><p> | ||
| 7293 | <span class="apii">[-0, +1, –]</span> | ||
| 7294 | <pre>int luaL_loadstring (lua_State *L, const char *s);</pre> | ||
| 7295 | |||
| 7296 | <p> | ||
| 7297 | Loads a string as a Lua chunk. | ||
| 7298 | This function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in | ||
| 7299 | the zero-terminated string <code>s</code>. | ||
| 7300 | |||
| 7301 | |||
| 7302 | <p> | ||
| 7303 | This function returns the same results as <a href="#lua_load"><code>lua_load</code></a>. | ||
| 7304 | |||
| 7305 | |||
| 7306 | <p> | ||
| 7307 | Also as <a href="#lua_load"><code>lua_load</code></a>, this function only loads the chunk; | ||
| 7308 | it does not run it. | ||
| 7309 | |||
| 7310 | |||
| 7311 | |||
| 7312 | |||
| 7313 | |||
| 7314 | <hr><h3><a name="luaL_newlib"><code>luaL_newlib</code></a></h3><p> | ||
| 7315 | <span class="apii">[-0, +1, <em>m</em>]</span> | ||
| 7316 | <pre>void luaL_newlib (lua_State *L, const luaL_Reg l[]);</pre> | ||
| 7317 | |||
| 7318 | <p> | ||
| 7319 | Creates a new table and registers there | ||
| 7320 | the functions in the list <code>l</code>. | ||
| 7321 | |||
| 7322 | |||
| 7323 | <p> | ||
| 7324 | It is implemented as the following macro: | ||
| 7325 | |||
| 7326 | <pre> | ||
| 7327 | (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) | ||
| 7328 | </pre><p> | ||
| 7329 | The array <code>l</code> must be the actual array, | ||
| 7330 | not a pointer to it. | ||
| 7331 | |||
| 7332 | |||
| 7333 | |||
| 7334 | |||
| 7335 | |||
| 7336 | <hr><h3><a name="luaL_newlibtable"><code>luaL_newlibtable</code></a></h3><p> | ||
| 7337 | <span class="apii">[-0, +1, <em>m</em>]</span> | ||
| 7338 | <pre>void luaL_newlibtable (lua_State *L, const luaL_Reg l[]);</pre> | ||
| 7339 | |||
| 7340 | <p> | ||
| 7341 | Creates a new table with a size optimized | ||
| 7342 | to store all entries in the array <code>l</code> | ||
| 7343 | (but does not actually store them). | ||
| 7344 | It is intended to be used in conjunction with <a href="#luaL_setfuncs"><code>luaL_setfuncs</code></a> | ||
| 7345 | (see <a href="#luaL_newlib"><code>luaL_newlib</code></a>). | ||
| 7346 | |||
| 7347 | |||
| 7348 | <p> | ||
| 7349 | It is implemented as a macro. | ||
| 7350 | The array <code>l</code> must be the actual array, | ||
| 7351 | not a pointer to it. | ||
| 7352 | |||
| 7353 | |||
| 7354 | |||
| 7355 | |||
| 7356 | |||
| 7357 | <hr><h3><a name="luaL_newmetatable"><code>luaL_newmetatable</code></a></h3><p> | ||
| 7358 | <span class="apii">[-0, +1, <em>m</em>]</span> | ||
| 7359 | <pre>int luaL_newmetatable (lua_State *L, const char *tname);</pre> | ||
| 7360 | |||
| 7361 | <p> | ||
| 7362 | If the registry already has the key <code>tname</code>, | ||
| 7363 | returns 0. | ||
| 7364 | Otherwise, | ||
| 7365 | creates a new table to be used as a metatable for userdata, | ||
| 7366 | adds to this new table the pair <code>__name = tname</code>, | ||
| 7367 | adds to the registry the pair <code>[tname] = new table</code>, | ||
| 7368 | and returns 1. | ||
| 7369 | |||
| 7370 | |||
| 7371 | <p> | ||
| 7372 | In both cases, | ||
| 7373 | the function pushes onto the stack the final value associated | ||
| 7374 | with <code>tname</code> in the registry. | ||
| 7375 | |||
| 7376 | |||
| 7377 | |||
| 7378 | |||
| 7379 | |||
| 7380 | <hr><h3><a name="luaL_newstate"><code>luaL_newstate</code></a></h3><p> | ||
| 7381 | <span class="apii">[-0, +0, –]</span> | ||
| 7382 | <pre>lua_State *luaL_newstate (void);</pre> | ||
| 7383 | |||
| 7384 | <p> | ||
| 7385 | Creates a new Lua state. | ||
| 7386 | It calls <a href="#lua_newstate"><code>lua_newstate</code></a> with an | ||
| 7387 | allocator based on the ISO C allocation functions | ||
| 7388 | and then sets a warning function and a panic function (see <a href="#4.4">§4.4</a>) | ||
| 7389 | that print messages to the standard error output. | ||
| 7390 | |||
| 7391 | |||
| 7392 | <p> | ||
| 7393 | Returns the new state, | ||
| 7394 | or <code>NULL</code> if there is a memory allocation error. | ||
| 7395 | |||
| 7396 | |||
| 7397 | |||
| 7398 | |||
| 7399 | |||
| 7400 | <hr><h3><a name="luaL_openlibs"><code>luaL_openlibs</code></a></h3><p> | ||
| 7401 | <span class="apii">[-0, +0, <em>e</em>]</span> | ||
| 7402 | <pre>void luaL_openlibs (lua_State *L);</pre> | ||
| 7403 | |||
| 7404 | <p> | ||
| 7405 | Opens all standard Lua libraries into the given state. | ||
| 7406 | |||
| 7407 | |||
| 7408 | |||
| 7409 | |||
| 7410 | |||
| 7411 | <hr><h3><a name="luaL_opt"><code>luaL_opt</code></a></h3><p> | ||
| 7412 | <span class="apii">[-0, +0, –]</span> | ||
| 7413 | <pre>T luaL_opt (L, func, arg, dflt);</pre> | ||
| 7414 | |||
| 7415 | <p> | ||
| 7416 | This macro is defined as follows: | ||
| 7417 | |||
| 7418 | <pre> | ||
| 7419 | (lua_isnoneornil(L,(arg)) ? (dflt) : func(L,(arg))) | ||
| 7420 | </pre><p> | ||
| 7421 | In words, if the argument <code>arg</code> is nil or absent, | ||
| 7422 | the macro results in the default <code>dflt</code>. | ||
| 7423 | Otherwise, it results in the result of calling <code>func</code> | ||
| 7424 | with the state <code>L</code> and the argument index <code>arg</code> as | ||
| 7425 | arguments. | ||
| 7426 | Note that it evaluates the expression <code>dflt</code> only if needed. | ||
| 7427 | |||
| 7428 | |||
| 7429 | |||
| 7430 | |||
| 7431 | |||
| 7432 | <hr><h3><a name="luaL_optinteger"><code>luaL_optinteger</code></a></h3><p> | ||
| 7433 | <span class="apii">[-0, +0, <em>v</em>]</span> | ||
| 7434 | <pre>lua_Integer luaL_optinteger (lua_State *L, | ||
| 7435 | int arg, | ||
| 7436 | lua_Integer d);</pre> | ||
| 7437 | |||
| 7438 | <p> | ||
| 7439 | If the function argument <code>arg</code> is an integer | ||
| 7440 | (or it is convertible to an integer), | ||
| 7441 | returns this integer. | ||
| 7442 | If this argument is absent or is <b>nil</b>, | ||
| 7443 | returns <code>d</code>. | ||
| 7444 | Otherwise, raises an error. | ||
| 7445 | |||
| 7446 | |||
| 7447 | |||
| 7448 | |||
| 7449 | |||
| 7450 | <hr><h3><a name="luaL_optlstring"><code>luaL_optlstring</code></a></h3><p> | ||
| 7451 | <span class="apii">[-0, +0, <em>v</em>]</span> | ||
| 7452 | <pre>const char *luaL_optlstring (lua_State *L, | ||
| 7453 | int arg, | ||
| 7454 | const char *d, | ||
| 7455 | size_t *l);</pre> | ||
| 7456 | |||
| 7457 | <p> | ||
| 7458 | If the function argument <code>arg</code> is a string, | ||
| 7459 | returns this string. | ||
| 7460 | If this argument is absent or is <b>nil</b>, | ||
| 7461 | returns <code>d</code>. | ||
| 7462 | Otherwise, raises an error. | ||
| 7463 | |||
| 7464 | |||
| 7465 | <p> | ||
| 7466 | If <code>l</code> is not <code>NULL</code>, | ||
| 7467 | fills its referent with the result's length. | ||
| 7468 | If the result is <code>NULL</code> | ||
| 7469 | (only possible when returning <code>d</code> and <code>d == NULL</code>), | ||
| 7470 | its length is considered zero. | ||
| 7471 | |||
| 7472 | |||
| 7473 | <p> | ||
| 7474 | This function uses <a href="#lua_tolstring"><code>lua_tolstring</code></a> to get its result, | ||
| 7475 | so all conversions and caveats of that function apply here. | ||
| 7476 | |||
| 7477 | |||
| 7478 | |||
| 7479 | |||
| 7480 | |||
| 7481 | <hr><h3><a name="luaL_optnumber"><code>luaL_optnumber</code></a></h3><p> | ||
| 7482 | <span class="apii">[-0, +0, <em>v</em>]</span> | ||
| 7483 | <pre>lua_Number luaL_optnumber (lua_State *L, int arg, lua_Number d);</pre> | ||
| 7484 | |||
| 7485 | <p> | ||
| 7486 | If the function argument <code>arg</code> is a number, | ||
| 7487 | returns this number as a <code>lua_Number</code>. | ||
| 7488 | If this argument is absent or is <b>nil</b>, | ||
| 7489 | returns <code>d</code>. | ||
| 7490 | Otherwise, raises an error. | ||
| 7491 | |||
| 7492 | |||
| 7493 | |||
| 7494 | |||
| 7495 | |||
| 7496 | <hr><h3><a name="luaL_optstring"><code>luaL_optstring</code></a></h3><p> | ||
| 7497 | <span class="apii">[-0, +0, <em>v</em>]</span> | ||
| 7498 | <pre>const char *luaL_optstring (lua_State *L, | ||
| 7499 | int arg, | ||
| 7500 | const char *d);</pre> | ||
| 7501 | |||
| 7502 | <p> | ||
| 7503 | If the function argument <code>arg</code> is a string, | ||
| 7504 | returns this string. | ||
| 7505 | If this argument is absent or is <b>nil</b>, | ||
| 7506 | returns <code>d</code>. | ||
| 7507 | Otherwise, raises an error. | ||
| 7508 | |||
| 7509 | |||
| 7510 | |||
| 7511 | |||
| 7512 | |||
| 7513 | <hr><h3><a name="luaL_prepbuffer"><code>luaL_prepbuffer</code></a></h3><p> | ||
| 7514 | <span class="apii">[-?, +?, <em>m</em>]</span> | ||
| 7515 | <pre>char *luaL_prepbuffer (luaL_Buffer *B);</pre> | ||
| 7516 | |||
| 7517 | <p> | ||
| 7518 | Equivalent to <a href="#luaL_prepbuffsize"><code>luaL_prepbuffsize</code></a> | ||
| 7519 | with the predefined size <a name="pdf-LUAL_BUFFERSIZE"><code>LUAL_BUFFERSIZE</code></a>. | ||
| 7520 | |||
| 7521 | |||
| 7522 | |||
| 7523 | |||
| 7524 | |||
| 7525 | <hr><h3><a name="luaL_prepbuffsize"><code>luaL_prepbuffsize</code></a></h3><p> | ||
| 7526 | <span class="apii">[-?, +?, <em>m</em>]</span> | ||
| 7527 | <pre>char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz);</pre> | ||
| 7528 | |||
| 7529 | <p> | ||
| 7530 | Returns an address to a space of size <code>sz</code> | ||
| 7531 | where you can copy a string to be added to buffer <code>B</code> | ||
| 7532 | (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>). | ||
| 7533 | After copying the string into this space you must call | ||
| 7534 | <a href="#luaL_addsize"><code>luaL_addsize</code></a> with the size of the string to actually add | ||
| 7535 | it to the buffer. | ||
| 7536 | |||
| 7537 | |||
| 7538 | |||
| 7539 | |||
| 7540 | |||
| 7541 | <hr><h3><a name="luaL_pushfail"><code>luaL_pushfail</code></a></h3><p> | ||
| 7542 | <span class="apii">[-0, +1, –]</span> | ||
| 7543 | <pre>void luaL_pushfail (lua_State *L);</pre> | ||
| 7544 | |||
| 7545 | <p> | ||
| 7546 | Pushes the <b>fail</b> value onto the stack (see <a href="#6">§6</a>). | ||
| 7547 | |||
| 7548 | |||
| 7549 | |||
| 7550 | |||
| 7551 | |||
| 7552 | <hr><h3><a name="luaL_pushresult"><code>luaL_pushresult</code></a></h3><p> | ||
| 7553 | <span class="apii">[-?, +1, <em>m</em>]</span> | ||
| 7554 | <pre>void luaL_pushresult (luaL_Buffer *B);</pre> | ||
| 7555 | |||
| 7556 | <p> | ||
| 7557 | Finishes the use of buffer <code>B</code> leaving the final string on | ||
| 7558 | the top of the stack. | ||
| 7559 | |||
| 7560 | |||
| 7561 | |||
| 7562 | |||
| 7563 | |||
| 7564 | <hr><h3><a name="luaL_pushresultsize"><code>luaL_pushresultsize</code></a></h3><p> | ||
| 7565 | <span class="apii">[-?, +1, <em>m</em>]</span> | ||
| 7566 | <pre>void luaL_pushresultsize (luaL_Buffer *B, size_t sz);</pre> | ||
| 7567 | |||
| 7568 | <p> | ||
| 7569 | Equivalent to the sequence <a href="#luaL_addsize"><code>luaL_addsize</code></a>, <a href="#luaL_pushresult"><code>luaL_pushresult</code></a>. | ||
| 7570 | |||
| 7571 | |||
| 7572 | |||
| 7573 | |||
| 7574 | |||
| 7575 | <hr><h3><a name="luaL_ref"><code>luaL_ref</code></a></h3><p> | ||
| 7576 | <span class="apii">[-1, +0, <em>m</em>]</span> | ||
| 7577 | <pre>int luaL_ref (lua_State *L, int t);</pre> | ||
| 7578 | |||
| 7579 | <p> | ||
| 7580 | Creates and returns a <em>reference</em>, | ||
| 7581 | in the table at index <code>t</code>, | ||
| 7582 | for the object on the top of the stack (and pops the object). | ||
| 7583 | |||
| 7584 | |||
| 7585 | <p> | ||
| 7586 | A reference is a unique integer key. | ||
| 7587 | As long as you do not manually add integer keys into the table <code>t</code>, | ||
| 7588 | <a href="#luaL_ref"><code>luaL_ref</code></a> ensures the uniqueness of the key it returns. | ||
| 7589 | You can retrieve an object referred by the reference <code>r</code> | ||
| 7590 | by calling <code>lua_rawgeti(L, t, r)</code>. | ||
| 7591 | The function <a href="#luaL_unref"><code>luaL_unref</code></a> frees a reference. | ||
| 7592 | |||
| 7593 | |||
| 7594 | <p> | ||
| 7595 | If the object on the top of the stack is <b>nil</b>, | ||
| 7596 | <a href="#luaL_ref"><code>luaL_ref</code></a> returns the constant <a name="pdf-LUA_REFNIL"><code>LUA_REFNIL</code></a>. | ||
| 7597 | The constant <a name="pdf-LUA_NOREF"><code>LUA_NOREF</code></a> is guaranteed to be different | ||
| 7598 | from any reference returned by <a href="#luaL_ref"><code>luaL_ref</code></a>. | ||
| 7599 | |||
| 7600 | |||
| 7601 | |||
| 7602 | |||
| 7603 | |||
| 7604 | <hr><h3><a name="luaL_Reg"><code>luaL_Reg</code></a></h3> | ||
| 7605 | <pre>typedef struct luaL_Reg { | ||
| 7606 | const char *name; | ||
| 7607 | lua_CFunction func; | ||
| 7608 | } luaL_Reg;</pre> | ||
| 7609 | |||
| 7610 | <p> | ||
| 7611 | Type for arrays of functions to be registered by | ||
| 7612 | <a href="#luaL_setfuncs"><code>luaL_setfuncs</code></a>. | ||
| 7613 | <code>name</code> is the function name and <code>func</code> is a pointer to | ||
| 7614 | the function. | ||
| 7615 | Any array of <a href="#luaL_Reg"><code>luaL_Reg</code></a> must end with a sentinel entry | ||
| 7616 | in which both <code>name</code> and <code>func</code> are <code>NULL</code>. | ||
| 7617 | |||
| 7618 | |||
| 7619 | |||
| 7620 | |||
| 7621 | |||
| 7622 | <hr><h3><a name="luaL_requiref"><code>luaL_requiref</code></a></h3><p> | ||
| 7623 | <span class="apii">[-0, +1, <em>e</em>]</span> | ||
| 7624 | <pre>void luaL_requiref (lua_State *L, const char *modname, | ||
| 7625 | lua_CFunction openf, int glb);</pre> | ||
| 7626 | |||
| 7627 | <p> | ||
| 7628 | If <code>package.loaded[modname]</code> is not true, | ||
| 7629 | calls the function <code>openf</code> with the string <code>modname</code> as an argument | ||
| 7630 | and sets the call result to <code>package.loaded[modname]</code>, | ||
| 7631 | as if that function has been called through <a href="#pdf-require"><code>require</code></a>. | ||
| 7632 | |||
| 7633 | |||
| 7634 | <p> | ||
| 7635 | If <code>glb</code> is true, | ||
| 7636 | also stores the module into the global <code>modname</code>. | ||
| 7637 | |||
| 7638 | |||
| 7639 | <p> | ||
| 7640 | Leaves a copy of the module on the stack. | ||
| 7641 | |||
| 7642 | |||
| 7643 | |||
| 7644 | |||
| 7645 | |||
| 7646 | <hr><h3><a name="luaL_setfuncs"><code>luaL_setfuncs</code></a></h3><p> | ||
| 7647 | <span class="apii">[-nup, +0, <em>m</em>]</span> | ||
| 7648 | <pre>void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup);</pre> | ||
| 7649 | |||
| 7650 | <p> | ||
| 7651 | Registers all functions in the array <code>l</code> | ||
| 7652 | (see <a href="#luaL_Reg"><code>luaL_Reg</code></a>) into the table on the top of the stack | ||
| 7653 | (below optional upvalues, see next). | ||
| 7654 | |||
| 7655 | |||
| 7656 | <p> | ||
| 7657 | When <code>nup</code> is not zero, | ||
| 7658 | all functions are created with <code>nup</code> upvalues, | ||
| 7659 | initialized with copies of the <code>nup</code> values | ||
| 7660 | previously pushed on the stack | ||
| 7661 | on top of the library table. | ||
| 7662 | These values are popped from the stack after the registration. | ||
| 7663 | |||
| 7664 | |||
| 7665 | <p> | ||
| 7666 | A function with a <code>NULL</code> value represents a placeholder, | ||
| 7667 | which is filled with <b>false</b>. | ||
| 7668 | |||
| 7669 | |||
| 7670 | |||
| 7671 | |||
| 7672 | |||
| 7673 | <hr><h3><a name="luaL_setmetatable"><code>luaL_setmetatable</code></a></h3><p> | ||
| 7674 | <span class="apii">[-0, +0, –]</span> | ||
| 7675 | <pre>void luaL_setmetatable (lua_State *L, const char *tname);</pre> | ||
| 7676 | |||
| 7677 | <p> | ||
| 7678 | Sets the metatable of the object on the top of the stack | ||
| 7679 | as the metatable associated with name <code>tname</code> | ||
| 7680 | in the registry (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>). | ||
| 7681 | |||
| 7682 | |||
| 7683 | |||
| 7684 | |||
| 7685 | |||
| 7686 | <hr><h3><a name="luaL_Stream"><code>luaL_Stream</code></a></h3> | ||
| 7687 | <pre>typedef struct luaL_Stream { | ||
| 7688 | FILE *f; | ||
| 7689 | lua_CFunction closef; | ||
| 7690 | } luaL_Stream;</pre> | ||
| 7691 | |||
| 7692 | <p> | ||
| 7693 | The standard representation for file handles | ||
| 7694 | used by the standard I/O library. | ||
| 7695 | |||
| 7696 | |||
| 7697 | <p> | ||
| 7698 | A file handle is implemented as a full userdata, | ||
| 7699 | with a metatable called <code>LUA_FILEHANDLE</code> | ||
| 7700 | (where <code>LUA_FILEHANDLE</code> is a macro with the actual metatable's name). | ||
| 7701 | The metatable is created by the I/O library | ||
| 7702 | (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>). | ||
| 7703 | |||
| 7704 | |||
| 7705 | <p> | ||
| 7706 | This userdata must start with the structure <code>luaL_Stream</code>; | ||
| 7707 | it can contain other data after this initial structure. | ||
| 7708 | The field <code>f</code> points to the corresponding C stream | ||
| 7709 | (or it can be <code>NULL</code> to indicate an incompletely created handle). | ||
| 7710 | The field <code>closef</code> points to a Lua function | ||
| 7711 | that will be called to close the stream | ||
| 7712 | when the handle is closed or collected; | ||
| 7713 | this function receives the file handle as its sole argument and | ||
| 7714 | must return either a true value, in case of success, | ||
| 7715 | or a false value plus an error message, in case of error. | ||
| 7716 | Once Lua calls this field, | ||
| 7717 | it changes the field value to <code>NULL</code> | ||
| 7718 | to signal that the handle is closed. | ||
| 7719 | |||
| 7720 | |||
| 7721 | |||
| 7722 | |||
| 7723 | |||
| 7724 | <hr><h3><a name="luaL_testudata"><code>luaL_testudata</code></a></h3><p> | ||
| 7725 | <span class="apii">[-0, +0, <em>m</em>]</span> | ||
| 7726 | <pre>void *luaL_testudata (lua_State *L, int arg, const char *tname);</pre> | ||
| 7727 | |||
| 7728 | <p> | ||
| 7729 | This function works like <a href="#luaL_checkudata"><code>luaL_checkudata</code></a>, | ||
| 7730 | except that, when the test fails, | ||
| 7731 | it returns <code>NULL</code> instead of raising an error. | ||
| 7732 | |||
| 7733 | |||
| 7734 | |||
| 7735 | |||
| 7736 | |||
| 7737 | <hr><h3><a name="luaL_tolstring"><code>luaL_tolstring</code></a></h3><p> | ||
| 7738 | <span class="apii">[-0, +1, <em>e</em>]</span> | ||
| 7739 | <pre>const char *luaL_tolstring (lua_State *L, int idx, size_t *len);</pre> | ||
| 7740 | |||
| 7741 | <p> | ||
| 7742 | Converts any Lua value at the given index to a C string | ||
| 7743 | in a reasonable format. | ||
| 7744 | The resulting string is pushed onto the stack and also | ||
| 7745 | returned by the function (see <a href="#4.1.3">§4.1.3</a>). | ||
| 7746 | If <code>len</code> is not <code>NULL</code>, | ||
| 7747 | the function also sets <code>*len</code> with the string length. | ||
| 7748 | |||
| 7749 | |||
| 7750 | <p> | ||
| 7751 | If the value has a metatable with a <code>__tostring</code> field, | ||
| 7752 | then <code>luaL_tolstring</code> calls the corresponding metamethod | ||
| 7753 | with the value as argument, | ||
| 7754 | and uses the result of the call as its result. | ||
| 7755 | |||
| 7756 | |||
| 7757 | |||
| 7758 | |||
| 7759 | |||
| 7760 | <hr><h3><a name="luaL_traceback"><code>luaL_traceback</code></a></h3><p> | ||
| 7761 | <span class="apii">[-0, +1, <em>m</em>]</span> | ||
| 7762 | <pre>void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, | ||
| 7763 | int level);</pre> | ||
| 7764 | |||
| 7765 | <p> | ||
| 7766 | Creates and pushes a traceback of the stack <code>L1</code>. | ||
| 7767 | If <code>msg</code> is not <code>NULL</code>, it is appended | ||
| 7768 | at the beginning of the traceback. | ||
| 7769 | The <code>level</code> parameter tells at which level | ||
| 7770 | to start the traceback. | ||
| 7771 | |||
| 7772 | |||
| 7773 | |||
| 7774 | |||
| 7775 | |||
| 7776 | <hr><h3><a name="luaL_typeerror"><code>luaL_typeerror</code></a></h3><p> | ||
| 7777 | <span class="apii">[-0, +0, <em>v</em>]</span> | ||
| 7778 | <pre>int luaL_typeerror (lua_State *L, int arg, const char *tname);</pre> | ||
| 7779 | |||
| 7780 | <p> | ||
| 7781 | Raises a type error for the argument <code>arg</code> | ||
| 7782 | of the C function that called it, | ||
| 7783 | using a standard message; | ||
| 7784 | <code>tname</code> is a "name" for the expected type. | ||
| 7785 | This function never returns. | ||
| 7786 | |||
| 7787 | |||
| 7788 | |||
| 7789 | |||
| 7790 | |||
| 7791 | <hr><h3><a name="luaL_typename"><code>luaL_typename</code></a></h3><p> | ||
| 7792 | <span class="apii">[-0, +0, –]</span> | ||
| 7793 | <pre>const char *luaL_typename (lua_State *L, int index);</pre> | ||
| 7794 | |||
| 7795 | <p> | ||
| 7796 | Returns the name of the type of the value at the given index. | ||
| 7797 | |||
| 7798 | |||
| 7799 | |||
| 7800 | |||
| 7801 | |||
| 7802 | <hr><h3><a name="luaL_unref"><code>luaL_unref</code></a></h3><p> | ||
| 7803 | <span class="apii">[-0, +0, –]</span> | ||
| 7804 | <pre>void luaL_unref (lua_State *L, int t, int ref);</pre> | ||
| 7805 | |||
| 7806 | <p> | ||
| 7807 | Releases the reference <code>ref</code> from the table at index <code>t</code> | ||
| 7808 | (see <a href="#luaL_ref"><code>luaL_ref</code></a>). | ||
| 7809 | The entry is removed from the table, | ||
| 7810 | so that the referred object can be collected. | ||
| 7811 | The reference <code>ref</code> is also freed to be used again. | ||
| 7812 | |||
| 7813 | |||
| 7814 | <p> | ||
| 7815 | 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>, | ||
| 7816 | <a href="#luaL_unref"><code>luaL_unref</code></a> does nothing. | ||
| 7817 | |||
| 7818 | |||
| 7819 | |||
| 7820 | |||
| 7821 | |||
| 7822 | <hr><h3><a name="luaL_where"><code>luaL_where</code></a></h3><p> | ||
| 7823 | <span class="apii">[-0, +1, <em>m</em>]</span> | ||
| 7824 | <pre>void luaL_where (lua_State *L, int lvl);</pre> | ||
| 7825 | |||
| 7826 | <p> | ||
| 7827 | Pushes onto the stack a string identifying the current position | ||
| 7828 | of the control at level <code>lvl</code> in the call stack. | ||
| 7829 | Typically this string has the following format: | ||
| 7830 | |||
| 7831 | <pre> | ||
| 7832 | <em>chunkname</em>:<em>currentline</em>: | ||
| 7833 | </pre><p> | ||
| 7834 | Level 0 is the running function, | ||
| 7835 | level 1 is the function that called the running function, | ||
| 7836 | etc. | ||
| 7837 | |||
| 7838 | |||
| 7839 | <p> | ||
| 7840 | This function is used to build a prefix for error messages. | ||
| 7841 | |||
| 7842 | |||
| 7843 | |||
| 7844 | |||
| 7845 | |||
| 7846 | |||
| 7847 | |||
| 7848 | <h1>6 – <a name="6">The Standard Libraries</a></h1> | ||
| 7849 | |||
| 7850 | |||
| 7851 | |||
| 7852 | <p> | ||
| 7853 | The standard Lua libraries provide useful functions | ||
| 7854 | that are implemented in C through the C API. | ||
| 7855 | Some of these functions provide essential services to the language | ||
| 7856 | (e.g., <a href="#pdf-type"><code>type</code></a> and <a href="#pdf-getmetatable"><code>getmetatable</code></a>); | ||
| 7857 | others provide access to outside services (e.g., I/O); | ||
| 7858 | and others could be implemented in Lua itself, | ||
| 7859 | but that for different reasons | ||
| 7860 | deserve an implementation in C (e.g., <a href="#pdf-table.sort"><code>table.sort</code></a>). | ||
| 7861 | |||
| 7862 | |||
| 7863 | <p> | ||
| 7864 | All libraries are implemented through the official C API | ||
| 7865 | and are provided as separate C modules. | ||
| 7866 | Unless otherwise noted, | ||
| 7867 | these library functions do not adjust its number of arguments | ||
| 7868 | to its expected parameters. | ||
| 7869 | For instance, a function documented as <code>foo(arg)</code> | ||
| 7870 | should not be called without an argument. | ||
| 7871 | |||
| 7872 | |||
| 7873 | <p> | ||
| 7874 | The notation <b>fail</b> means a false value representing | ||
| 7875 | some kind of failure. | ||
| 7876 | (Currently, <b>fail</b> is equal to <b>nil</b>, | ||
| 7877 | but that may change in future versions. | ||
| 7878 | The recommendation is to always test the success of these functions | ||
| 7879 | with <code>(not status)</code>, instead of <code>(status == nil)</code>.) | ||
| 7880 | |||
| 7881 | |||
| 7882 | <p> | ||
| 7883 | Currently, Lua has the following standard libraries: | ||
| 7884 | |||
| 7885 | <ul> | ||
| 7886 | |||
| 7887 | <li>basic library (<a href="#6.1">§6.1</a>);</li> | ||
| 7888 | |||
| 7889 | <li>coroutine library (<a href="#6.2">§6.2</a>);</li> | ||
| 7890 | |||
| 7891 | <li>package library (<a href="#6.3">§6.3</a>);</li> | ||
| 7892 | |||
| 7893 | <li>string manipulation (<a href="#6.4">§6.4</a>);</li> | ||
| 7894 | |||
| 7895 | <li>basic UTF-8 support (<a href="#6.5">§6.5</a>);</li> | ||
| 7896 | |||
| 7897 | <li>table manipulation (<a href="#6.6">§6.6</a>);</li> | ||
| 7898 | |||
| 7899 | <li>mathematical functions (<a href="#6.7">§6.7</a>) (sin, log, etc.);</li> | ||
| 7900 | |||
| 7901 | <li>input and output (<a href="#6.8">§6.8</a>);</li> | ||
| 7902 | |||
| 7903 | <li>operating system facilities (<a href="#6.9">§6.9</a>);</li> | ||
| 7904 | |||
| 7905 | <li>debug facilities (<a href="#6.10">§6.10</a>).</li> | ||
| 7906 | |||
| 7907 | </ul><p> | ||
| 7908 | Except for the basic and the package libraries, | ||
| 7909 | each library provides all its functions as fields of a global table | ||
| 7910 | or as methods of its objects. | ||
| 7911 | |||
| 7912 | |||
| 7913 | <p> | ||
| 7914 | To have access to these libraries, | ||
| 7915 | the C host program should call the <a href="#luaL_openlibs"><code>luaL_openlibs</code></a> function, | ||
| 7916 | which opens all standard libraries. | ||
| 7917 | Alternatively, | ||
| 7918 | the host program can open them individually by using | ||
| 7919 | <a href="#luaL_requiref"><code>luaL_requiref</code></a> to call | ||
| 7920 | <a name="pdf-luaopen_base"><code>luaopen_base</code></a> (for the basic library), | ||
| 7921 | <a name="pdf-luaopen_package"><code>luaopen_package</code></a> (for the package library), | ||
| 7922 | <a name="pdf-luaopen_coroutine"><code>luaopen_coroutine</code></a> (for the coroutine library), | ||
| 7923 | <a name="pdf-luaopen_string"><code>luaopen_string</code></a> (for the string library), | ||
| 7924 | <a name="pdf-luaopen_utf8"><code>luaopen_utf8</code></a> (for the UTF-8 library), | ||
| 7925 | <a name="pdf-luaopen_table"><code>luaopen_table</code></a> (for the table library), | ||
| 7926 | <a name="pdf-luaopen_math"><code>luaopen_math</code></a> (for the mathematical library), | ||
| 7927 | <a name="pdf-luaopen_io"><code>luaopen_io</code></a> (for the I/O library), | ||
| 7928 | <a name="pdf-luaopen_os"><code>luaopen_os</code></a> (for the operating system library), | ||
| 7929 | and <a name="pdf-luaopen_debug"><code>luaopen_debug</code></a> (for the debug library). | ||
| 7930 | These functions are declared in <a name="pdf-lualib.h"><code>lualib.h</code></a>. | ||
| 7931 | |||
| 7932 | |||
| 7933 | |||
| 7934 | |||
| 7935 | |||
| 7936 | <h2>6.1 – <a name="6.1">Basic Functions</a></h2> | ||
| 7937 | |||
| 7938 | <p> | ||
| 7939 | The basic library provides core functions to Lua. | ||
| 7940 | If you do not include this library in your application, | ||
| 7941 | you should check carefully whether you need to provide | ||
| 7942 | implementations for some of its facilities. | ||
| 7943 | |||
| 7944 | |||
| 7945 | <p> | ||
| 7946 | <hr><h3><a name="pdf-assert"><code>assert (v [, message])</code></a></h3> | ||
| 7947 | |||
| 7948 | |||
| 7949 | <p> | ||
| 7950 | Raises an error if | ||
| 7951 | the value of its argument <code>v</code> is false (i.e., <b>nil</b> or <b>false</b>); | ||
| 7952 | otherwise, returns all its arguments. | ||
| 7953 | In case of error, | ||
| 7954 | <code>message</code> is the error object; | ||
| 7955 | when absent, it defaults to "<code>assertion failed!</code>" | ||
| 7956 | |||
| 7957 | |||
| 7958 | |||
| 7959 | |||
| 7960 | <p> | ||
| 7961 | <hr><h3><a name="pdf-collectgarbage"><code>collectgarbage ([opt [, arg]])</code></a></h3> | ||
| 7962 | |||
| 7963 | |||
| 7964 | <p> | ||
| 7965 | This function is a generic interface to the garbage collector. | ||
| 7966 | It performs different functions according to its first argument, <code>opt</code>: | ||
| 7967 | |||
| 7968 | <ul> | ||
| 7969 | |||
| 7970 | <li><b>"<code>collect</code>": </b> | ||
| 7971 | Performs a full garbage-collection cycle. | ||
| 7972 | This is the default option. | ||
| 7973 | </li> | ||
| 7974 | |||
| 7975 | <li><b>"<code>stop</code>": </b> | ||
| 7976 | Stops automatic execution of the garbage collector. | ||
| 7977 | The collector will run only when explicitly invoked, | ||
| 7978 | until a call to restart it. | ||
| 7979 | </li> | ||
| 7980 | |||
| 7981 | <li><b>"<code>restart</code>": </b> | ||
| 7982 | Restarts automatic execution of the garbage collector. | ||
| 7983 | </li> | ||
| 7984 | |||
| 7985 | <li><b>"<code>count</code>": </b> | ||
| 7986 | Returns the total memory in use by Lua in Kbytes. | ||
| 7987 | The value has a fractional part, | ||
| 7988 | so that it multiplied by 1024 | ||
| 7989 | gives the exact number of bytes in use by Lua. | ||
| 7990 | </li> | ||
| 7991 | |||
| 7992 | <li><b>"<code>step</code>": </b> | ||
| 7993 | Performs a garbage-collection step. | ||
| 7994 | The step "size" is controlled by <code>arg</code>. | ||
| 7995 | With a zero value, | ||
| 7996 | the collector will perform one basic (indivisible) step. | ||
| 7997 | For non-zero values, | ||
| 7998 | the collector will perform as if that amount of memory | ||
| 7999 | (in Kbytes) had been allocated by Lua. | ||
| 8000 | Returns <b>true</b> if the step finished a collection cycle. | ||
| 8001 | </li> | ||
| 8002 | |||
| 8003 | <li><b>"<code>isrunning</code>": </b> | ||
| 8004 | Returns a boolean that tells whether the collector is running | ||
| 8005 | (i.e., not stopped). | ||
| 8006 | </li> | ||
| 8007 | |||
| 8008 | <li><b>"<code>incremental</code>": </b> | ||
| 8009 | Change the collector mode to incremental. | ||
| 8010 | This option can be followed by three numbers: | ||
| 8011 | the garbage-collector pause, | ||
| 8012 | the step multiplier, | ||
| 8013 | and the step size (see <a href="#2.5.1">§2.5.1</a>). | ||
| 8014 | A zero means to not change that value. | ||
| 8015 | </li> | ||
| 8016 | |||
| 8017 | <li><b>"<code>generational</code>": </b> | ||
| 8018 | Change the collector mode to generational. | ||
| 8019 | This option can be followed by two numbers: | ||
| 8020 | the garbage-collector minor multiplier | ||
| 8021 | and the major multiplier (see <a href="#2.5.2">§2.5.2</a>). | ||
| 8022 | A zero means to not change that value. | ||
| 8023 | </li> | ||
| 8024 | |||
| 8025 | </ul><p> | ||
| 8026 | See <a href="#2.5">§2.5</a> for more details about garbage collection | ||
| 8027 | and some of these options. | ||
| 8028 | |||
| 8029 | |||
| 8030 | <p> | ||
| 8031 | This function should not be called by a finalizer. | ||
| 8032 | |||
| 8033 | |||
| 8034 | |||
| 8035 | |||
| 8036 | <p> | ||
| 8037 | <hr><h3><a name="pdf-dofile"><code>dofile ([filename])</code></a></h3> | ||
| 8038 | Opens the named file and executes its content as a Lua chunk. | ||
| 8039 | When called without arguments, | ||
| 8040 | <code>dofile</code> executes the content of the standard input (<code>stdin</code>). | ||
| 8041 | Returns all values returned by the chunk. | ||
| 8042 | In case of errors, <code>dofile</code> propagates the error | ||
| 8043 | to its caller. | ||
| 8044 | (That is, <code>dofile</code> does not run in protected mode.) | ||
| 8045 | |||
| 8046 | |||
| 8047 | |||
| 8048 | |||
| 8049 | <p> | ||
| 8050 | <hr><h3><a name="pdf-error"><code>error (message [, level])</code></a></h3> | ||
| 8051 | Raises an error (see <a href="#2.3">§2.3</a>) with <code>message</code> as the error object. | ||
| 8052 | This function never returns. | ||
| 8053 | |||
| 8054 | |||
| 8055 | <p> | ||
| 8056 | Usually, <code>error</code> adds some information about the error position | ||
| 8057 | at the beginning of the message, if the message is a string. | ||
| 8058 | The <code>level</code> argument specifies how to get the error position. | ||
| 8059 | With level 1 (the default), the error position is where the | ||
| 8060 | <code>error</code> function was called. | ||
| 8061 | Level 2 points the error to where the function | ||
| 8062 | that called <code>error</code> was called; and so on. | ||
| 8063 | Passing a level 0 avoids the addition of error position information | ||
| 8064 | to the message. | ||
| 8065 | |||
| 8066 | |||
| 8067 | |||
| 8068 | |||
| 8069 | <p> | ||
| 8070 | <hr><h3><a name="pdf-_G"><code>_G</code></a></h3> | ||
| 8071 | A global variable (not a function) that | ||
| 8072 | holds the global environment (see <a href="#2.2">§2.2</a>). | ||
| 8073 | Lua itself does not use this variable; | ||
| 8074 | changing its value does not affect any environment, | ||
| 8075 | nor vice versa. | ||
| 8076 | |||
| 8077 | |||
| 8078 | |||
| 8079 | |||
| 8080 | <p> | ||
| 8081 | <hr><h3><a name="pdf-getmetatable"><code>getmetatable (object)</code></a></h3> | ||
| 8082 | |||
| 8083 | |||
| 8084 | <p> | ||
| 8085 | If <code>object</code> does not have a metatable, returns <b>nil</b>. | ||
| 8086 | Otherwise, | ||
| 8087 | if the object's metatable has a <code>__metatable</code> field, | ||
| 8088 | returns the associated value. | ||
| 8089 | Otherwise, returns the metatable of the given object. | ||
| 8090 | |||
| 8091 | |||
| 8092 | |||
| 8093 | |||
| 8094 | <p> | ||
| 8095 | <hr><h3><a name="pdf-ipairs"><code>ipairs (t)</code></a></h3> | ||
| 8096 | |||
| 8097 | |||
| 8098 | <p> | ||
| 8099 | Returns three values (an iterator function, the table <code>t</code>, and 0) | ||
| 8100 | so that the construction | ||
| 8101 | |||
| 8102 | <pre> | ||
| 8103 | for i,v in ipairs(t) do <em>body</em> end | ||
| 8104 | </pre><p> | ||
| 8105 | will iterate over the key–value pairs | ||
| 8106 | (<code>1,t[1]</code>), (<code>2,t[2]</code>), ..., | ||
| 8107 | up to the first absent index. | ||
| 8108 | |||
| 8109 | |||
| 8110 | |||
| 8111 | |||
| 8112 | <p> | ||
| 8113 | <hr><h3><a name="pdf-load"><code>load (chunk [, chunkname [, mode [, env]]])</code></a></h3> | ||
| 8114 | |||
| 8115 | |||
| 8116 | <p> | ||
| 8117 | Loads a chunk. | ||
| 8118 | |||
| 8119 | |||
| 8120 | <p> | ||
| 8121 | If <code>chunk</code> is a string, the chunk is this string. | ||
| 8122 | If <code>chunk</code> is a function, | ||
| 8123 | <code>load</code> calls it repeatedly to get the chunk pieces. | ||
| 8124 | Each call to <code>chunk</code> must return a string that concatenates | ||
| 8125 | with previous results. | ||
| 8126 | A return of an empty string, <b>nil</b>, or no value signals the end of the chunk. | ||
| 8127 | |||
| 8128 | |||
| 8129 | <p> | ||
| 8130 | If there are no syntactic errors, | ||
| 8131 | <code>load</code> returns the compiled chunk as a function; | ||
| 8132 | otherwise, it returns <b>fail</b> plus the error message. | ||
| 8133 | |||
| 8134 | |||
| 8135 | <p> | ||
| 8136 | When you load a main chunk, | ||
| 8137 | the resulting function will always have exactly one upvalue, | ||
| 8138 | the <code>_ENV</code> variable (see <a href="#2.2">§2.2</a>). | ||
| 8139 | However, | ||
| 8140 | when you load a binary chunk created from a function (see <a href="#pdf-string.dump"><code>string.dump</code></a>), | ||
| 8141 | the resulting function can have an arbitrary number of upvalues, | ||
| 8142 | and there is no guarantee that its first upvalue will be | ||
| 8143 | the <code>_ENV</code> variable. | ||
| 8144 | (A non-main function may not even have an <code>_ENV</code> upvalue.) | ||
| 8145 | |||
| 8146 | |||
| 8147 | <p> | ||
| 8148 | Regardless, if the resulting function has any upvalues, | ||
| 8149 | its first upvalue is set to the value of <code>env</code>, | ||
| 8150 | if that parameter is given, | ||
| 8151 | or to the value of the global environment. | ||
| 8152 | Other upvalues are initialized with <b>nil</b>. | ||
| 8153 | All upvalues are fresh, that is, | ||
| 8154 | they are not shared with any other function. | ||
| 8155 | |||
| 8156 | |||
| 8157 | <p> | ||
| 8158 | <code>chunkname</code> is used as the name of the chunk for error messages | ||
| 8159 | and debug information (see <a href="#4.7">§4.7</a>). | ||
| 8160 | When absent, | ||
| 8161 | it defaults to <code>chunk</code>, if <code>chunk</code> is a string, | ||
| 8162 | or to "<code>=(load)</code>" otherwise. | ||
| 8163 | |||
| 8164 | |||
| 8165 | <p> | ||
| 8166 | The string <code>mode</code> controls whether the chunk can be text or binary | ||
| 8167 | (that is, a precompiled chunk). | ||
| 8168 | It may be the string "<code>b</code>" (only binary chunks), | ||
| 8169 | "<code>t</code>" (only text chunks), | ||
| 8170 | or "<code>bt</code>" (both binary and text). | ||
| 8171 | The default is "<code>bt</code>". | ||
| 8172 | |||
| 8173 | |||
| 8174 | <p> | ||
| 8175 | It is safe to load malformed binary chunks; | ||
| 8176 | <code>load</code> signals an appropriate error. | ||
| 8177 | However, | ||
| 8178 | Lua does not check the consistency of the code inside binary chunks; | ||
| 8179 | running maliciously crafted bytecode can crash the interpreter. | ||
| 8180 | |||
| 8181 | |||
| 8182 | |||
| 8183 | |||
| 8184 | <p> | ||
| 8185 | <hr><h3><a name="pdf-loadfile"><code>loadfile ([filename [, mode [, env]]])</code></a></h3> | ||
| 8186 | |||
| 8187 | |||
| 8188 | <p> | ||
| 8189 | Similar to <a href="#pdf-load"><code>load</code></a>, | ||
| 8190 | but gets the chunk from file <code>filename</code> | ||
| 8191 | or from the standard input, | ||
| 8192 | if no file name is given. | ||
| 8193 | |||
| 8194 | |||
| 8195 | |||
| 8196 | |||
| 8197 | <p> | ||
| 8198 | <hr><h3><a name="pdf-next"><code>next (table [, index])</code></a></h3> | ||
| 8199 | |||
| 8200 | |||
| 8201 | <p> | ||
| 8202 | Allows a program to traverse all fields of a table. | ||
| 8203 | Its first argument is a table and its second argument | ||
| 8204 | is an index in this table. | ||
| 8205 | A call to <code>next</code> returns the next index of the table | ||
| 8206 | and its associated value. | ||
| 8207 | When called with <b>nil</b> as its second argument, | ||
| 8208 | <code>next</code> returns an initial index | ||
| 8209 | and its associated value. | ||
| 8210 | When called with the last index, | ||
| 8211 | or with <b>nil</b> in an empty table, | ||
| 8212 | <code>next</code> returns <b>nil</b>. | ||
| 8213 | If the second argument is absent, then it is interpreted as <b>nil</b>. | ||
| 8214 | In particular, | ||
| 8215 | you can use <code>next(t)</code> to check whether a table is empty. | ||
| 8216 | |||
| 8217 | |||
| 8218 | <p> | ||
| 8219 | The order in which the indices are enumerated is not specified, | ||
| 8220 | <em>even for numeric indices</em>. | ||
| 8221 | (To traverse a table in numerical order, | ||
| 8222 | use a numerical <b>for</b>.) | ||
| 8223 | |||
| 8224 | |||
| 8225 | <p> | ||
| 8226 | You should not assign any value to a non-existent field in a table | ||
| 8227 | during its traversal. | ||
| 8228 | You may however modify existing fields. | ||
| 8229 | In particular, you may set existing fields to nil. | ||
| 8230 | |||
| 8231 | |||
| 8232 | |||
| 8233 | |||
| 8234 | <p> | ||
| 8235 | <hr><h3><a name="pdf-pairs"><code>pairs (t)</code></a></h3> | ||
| 8236 | |||
| 8237 | |||
| 8238 | <p> | ||
| 8239 | If <code>t</code> has a metamethod <code>__pairs</code>, | ||
| 8240 | calls it with <code>t</code> as argument and returns the first three | ||
| 8241 | results from the call. | ||
| 8242 | |||
| 8243 | |||
| 8244 | <p> | ||
| 8245 | Otherwise, | ||
| 8246 | returns three values: the <a href="#pdf-next"><code>next</code></a> function, the table <code>t</code>, and <b>nil</b>, | ||
| 8247 | so that the construction | ||
| 8248 | |||
| 8249 | <pre> | ||
| 8250 | for k,v in pairs(t) do <em>body</em> end | ||
| 8251 | </pre><p> | ||
| 8252 | will iterate over all key–value pairs of table <code>t</code>. | ||
| 8253 | |||
| 8254 | |||
| 8255 | <p> | ||
| 8256 | See function <a href="#pdf-next"><code>next</code></a> for the caveats of modifying | ||
| 8257 | the table during its traversal. | ||
| 8258 | |||
| 8259 | |||
| 8260 | |||
| 8261 | |||
| 8262 | <p> | ||
| 8263 | <hr><h3><a name="pdf-pcall"><code>pcall (f [, arg1, ···])</code></a></h3> | ||
| 8264 | |||
| 8265 | |||
| 8266 | <p> | ||
| 8267 | Calls the function <code>f</code> with | ||
| 8268 | the given arguments in <em>protected mode</em>. | ||
| 8269 | This means that any error inside <code>f</code> is not propagated; | ||
| 8270 | instead, <code>pcall</code> catches the error | ||
| 8271 | and returns a status code. | ||
| 8272 | Its first result is the status code (a boolean), | ||
| 8273 | which is <b>true</b> if the call succeeds without errors. | ||
| 8274 | In such case, <code>pcall</code> also returns all results from the call, | ||
| 8275 | after this first result. | ||
| 8276 | In case of any error, <code>pcall</code> returns <b>false</b> plus the error object. | ||
| 8277 | Note that errors caught by <code>pcall</code> do not call a message handler. | ||
| 8278 | |||
| 8279 | |||
| 8280 | |||
| 8281 | |||
| 8282 | <p> | ||
| 8283 | <hr><h3><a name="pdf-print"><code>print (···)</code></a></h3> | ||
| 8284 | Receives any number of arguments | ||
| 8285 | and prints their values to <code>stdout</code>, | ||
| 8286 | converting each argument to a string | ||
| 8287 | following the same rules of <a href="#pdf-tostring"><code>tostring</code></a>. | ||
| 8288 | |||
| 8289 | |||
| 8290 | <p> | ||
| 8291 | The function <code>print</code> is not intended for formatted output, | ||
| 8292 | but only as a quick way to show a value, | ||
| 8293 | for instance for debugging. | ||
| 8294 | For complete control over the output, | ||
| 8295 | use <a href="#pdf-string.format"><code>string.format</code></a> and <a href="#pdf-io.write"><code>io.write</code></a>. | ||
| 8296 | |||
| 8297 | |||
| 8298 | |||
| 8299 | |||
| 8300 | <p> | ||
| 8301 | <hr><h3><a name="pdf-rawequal"><code>rawequal (v1, v2)</code></a></h3> | ||
| 8302 | Checks whether <code>v1</code> is equal to <code>v2</code>, | ||
| 8303 | without invoking the <code>__eq</code> metamethod. | ||
| 8304 | Returns a boolean. | ||
| 8305 | |||
| 8306 | |||
| 8307 | |||
| 8308 | |||
| 8309 | <p> | ||
| 8310 | <hr><h3><a name="pdf-rawget"><code>rawget (table, index)</code></a></h3> | ||
| 8311 | Gets the real value of <code>table[index]</code>, | ||
| 8312 | without using the <code>__index</code> metavalue. | ||
| 8313 | <code>table</code> must be a table; | ||
| 8314 | <code>index</code> may be any value. | ||
| 8315 | |||
| 8316 | |||
| 8317 | |||
| 8318 | |||
| 8319 | <p> | ||
| 8320 | <hr><h3><a name="pdf-rawlen"><code>rawlen (v)</code></a></h3> | ||
| 8321 | Returns the length of the object <code>v</code>, | ||
| 8322 | which must be a table or a string, | ||
| 8323 | without invoking the <code>__len</code> metamethod. | ||
| 8324 | Returns an integer. | ||
| 8325 | |||
| 8326 | |||
| 8327 | |||
| 8328 | |||
| 8329 | <p> | ||
| 8330 | <hr><h3><a name="pdf-rawset"><code>rawset (table, index, value)</code></a></h3> | ||
| 8331 | Sets the real value of <code>table[index]</code> to <code>value</code>, | ||
| 8332 | without using the <code>__newindex</code> metavalue. | ||
| 8333 | <code>table</code> must be a table, | ||
| 8334 | <code>index</code> any value different from <b>nil</b> and NaN, | ||
| 8335 | and <code>value</code> any Lua value. | ||
| 8336 | |||
| 8337 | |||
| 8338 | <p> | ||
| 8339 | This function returns <code>table</code>. | ||
| 8340 | |||
| 8341 | |||
| 8342 | |||
| 8343 | |||
| 8344 | <p> | ||
| 8345 | <hr><h3><a name="pdf-select"><code>select (index, ···)</code></a></h3> | ||
| 8346 | |||
| 8347 | |||
| 8348 | <p> | ||
| 8349 | If <code>index</code> is a number, | ||
| 8350 | returns all arguments after argument number <code>index</code>; | ||
| 8351 | a negative number indexes from the end (-1 is the last argument). | ||
| 8352 | Otherwise, <code>index</code> must be the string <code>"#"</code>, | ||
| 8353 | and <code>select</code> returns the total number of extra arguments it received. | ||
| 8354 | |||
| 8355 | |||
| 8356 | |||
| 8357 | |||
| 8358 | <p> | ||
| 8359 | <hr><h3><a name="pdf-setmetatable"><code>setmetatable (table, metatable)</code></a></h3> | ||
| 8360 | |||
| 8361 | |||
| 8362 | <p> | ||
| 8363 | Sets the metatable for the given table. | ||
| 8364 | If <code>metatable</code> is <b>nil</b>, | ||
| 8365 | removes the metatable of the given table. | ||
| 8366 | If the original metatable has a <code>__metatable</code> field, | ||
| 8367 | raises an error. | ||
| 8368 | |||
| 8369 | |||
| 8370 | <p> | ||
| 8371 | This function returns <code>table</code>. | ||
| 8372 | |||
| 8373 | |||
| 8374 | <p> | ||
| 8375 | To change the metatable of other types from Lua code, | ||
| 8376 | you must use the debug library (<a href="#6.10">§6.10</a>). | ||
| 8377 | |||
| 8378 | |||
| 8379 | |||
| 8380 | |||
| 8381 | <p> | ||
| 8382 | <hr><h3><a name="pdf-tonumber"><code>tonumber (e [, base])</code></a></h3> | ||
| 8383 | |||
| 8384 | |||
| 8385 | <p> | ||
| 8386 | When called with no <code>base</code>, | ||
| 8387 | <code>tonumber</code> tries to convert its argument to a number. | ||
| 8388 | If the argument is already a number or | ||
| 8389 | a string convertible to a number, | ||
| 8390 | then <code>tonumber</code> returns this number; | ||
| 8391 | otherwise, it returns <b>fail</b>. | ||
| 8392 | |||
| 8393 | |||
| 8394 | <p> | ||
| 8395 | The conversion of strings can result in integers or floats, | ||
| 8396 | according to the lexical conventions of Lua (see <a href="#3.1">§3.1</a>). | ||
| 8397 | The string may have leading and trailing spaces and a sign. | ||
| 8398 | |||
| 8399 | |||
| 8400 | <p> | ||
| 8401 | When called with <code>base</code>, | ||
| 8402 | then <code>e</code> must be a string to be interpreted as | ||
| 8403 | an integer numeral in that base. | ||
| 8404 | The base may be any integer between 2 and 36, inclusive. | ||
| 8405 | In bases above 10, the letter '<code>A</code>' (in either upper or lower case) | ||
| 8406 | represents 10, '<code>B</code>' represents 11, and so forth, | ||
| 8407 | with '<code>Z</code>' representing 35. | ||
| 8408 | If the string <code>e</code> is not a valid numeral in the given base, | ||
| 8409 | the function returns <b>fail</b>. | ||
| 8410 | |||
| 8411 | |||
| 8412 | |||
| 8413 | |||
| 8414 | <p> | ||
| 8415 | <hr><h3><a name="pdf-tostring"><code>tostring (v)</code></a></h3> | ||
| 8416 | |||
| 8417 | |||
| 8418 | <p> | ||
| 8419 | Receives a value of any type and | ||
| 8420 | converts it to a string in a human-readable format. | ||
| 8421 | |||
| 8422 | |||
| 8423 | <p> | ||
| 8424 | If the metatable of <code>v</code> has a <code>__tostring</code> field, | ||
| 8425 | then <code>tostring</code> calls the corresponding value | ||
| 8426 | with <code>v</code> as argument, | ||
| 8427 | and uses the result of the call as its result. | ||
| 8428 | Otherwise, if the metatable of <code>v</code> has a <code>__name</code> field | ||
| 8429 | with a string value, | ||
| 8430 | <code>tostring</code> may use that string in its final result. | ||
| 8431 | |||
| 8432 | |||
| 8433 | <p> | ||
| 8434 | For complete control of how numbers are converted, | ||
| 8435 | use <a href="#pdf-string.format"><code>string.format</code></a>. | ||
| 8436 | |||
| 8437 | |||
| 8438 | |||
| 8439 | |||
| 8440 | <p> | ||
| 8441 | <hr><h3><a name="pdf-type"><code>type (v)</code></a></h3> | ||
| 8442 | |||
| 8443 | |||
| 8444 | <p> | ||
| 8445 | Returns the type of its only argument, coded as a string. | ||
| 8446 | The possible results of this function are | ||
| 8447 | "<code>nil</code>" (a string, not the value <b>nil</b>), | ||
| 8448 | "<code>number</code>", | ||
| 8449 | "<code>string</code>", | ||
| 8450 | "<code>boolean</code>", | ||
| 8451 | "<code>table</code>", | ||
| 8452 | "<code>function</code>", | ||
| 8453 | "<code>thread</code>", | ||
| 8454 | and "<code>userdata</code>". | ||
| 8455 | |||
| 8456 | |||
| 8457 | |||
| 8458 | |||
| 8459 | <p> | ||
| 8460 | <hr><h3><a name="pdf-_VERSION"><code>_VERSION</code></a></h3> | ||
| 8461 | |||
| 8462 | |||
| 8463 | <p> | ||
| 8464 | A global variable (not a function) that | ||
| 8465 | holds a string containing the running Lua version. | ||
| 8466 | The current value of this variable is "<code>Lua 5.4</code>". | ||
| 8467 | |||
| 8468 | |||
| 8469 | |||
| 8470 | |||
| 8471 | <p> | ||
| 8472 | <hr><h3><a name="pdf-warn"><code>warn (msg1, ···)</code></a></h3> | ||
| 8473 | |||
| 8474 | |||
| 8475 | <p> | ||
| 8476 | Emits a warning with a message composed by the concatenation | ||
| 8477 | of all its arguments (which should be strings). | ||
| 8478 | |||
| 8479 | |||
| 8480 | <p> | ||
| 8481 | By convention, | ||
| 8482 | a one-piece message starting with '<code>@</code>' | ||
| 8483 | is intended to be a <em>control message</em>, | ||
| 8484 | which is a message to the warning system itself. | ||
| 8485 | In particular, the standard warning function in Lua | ||
| 8486 | recognizes the control messages "<code>@off</code>", | ||
| 8487 | to stop the emission of warnings, | ||
| 8488 | and "<code>@on</code>", to (re)start the emission; | ||
| 8489 | it ignores unknown control messages. | ||
| 8490 | |||
| 8491 | |||
| 8492 | |||
| 8493 | |||
| 8494 | <p> | ||
| 8495 | <hr><h3><a name="pdf-xpcall"><code>xpcall (f, msgh [, arg1, ···])</code></a></h3> | ||
| 8496 | |||
| 8497 | |||
| 8498 | <p> | ||
| 8499 | This function is similar to <a href="#pdf-pcall"><code>pcall</code></a>, | ||
| 8500 | except that it sets a new message handler <code>msgh</code>. | ||
| 8501 | |||
| 8502 | |||
| 8503 | |||
| 8504 | |||
| 8505 | |||
| 8506 | |||
| 8507 | |||
| 8508 | <h2>6.2 – <a name="6.2">Coroutine Manipulation</a></h2> | ||
| 8509 | |||
| 8510 | <p> | ||
| 8511 | This library comprises the operations to manipulate coroutines, | ||
| 8512 | which come inside the table <a name="pdf-coroutine"><code>coroutine</code></a>. | ||
| 8513 | See <a href="#2.6">§2.6</a> for a general description of coroutines. | ||
| 8514 | |||
| 8515 | |||
| 8516 | <p> | ||
| 8517 | <hr><h3><a name="pdf-coroutine.close"><code>coroutine.close (co)</code></a></h3> | ||
| 8518 | |||
| 8519 | |||
| 8520 | <p> | ||
| 8521 | Closes coroutine <code>co</code>, | ||
| 8522 | that is, | ||
| 8523 | closes all its pending to-be-closed variables | ||
| 8524 | and puts the coroutine in a dead state. | ||
| 8525 | The given coroutine must be dead or suspended. | ||
| 8526 | In case of error | ||
| 8527 | (either the original error that stopped the coroutine or | ||
| 8528 | errors in closing methods), | ||
| 8529 | returns <b>false</b> plus the error object; | ||
| 8530 | otherwise returns <b>true</b>. | ||
| 8531 | |||
| 8532 | |||
| 8533 | |||
| 8534 | |||
| 8535 | <p> | ||
| 8536 | <hr><h3><a name="pdf-coroutine.create"><code>coroutine.create (f)</code></a></h3> | ||
| 8537 | |||
| 8538 | |||
| 8539 | <p> | ||
| 8540 | Creates a new coroutine, with body <code>f</code>. | ||
| 8541 | <code>f</code> must be a function. | ||
| 8542 | Returns this new coroutine, | ||
| 8543 | an object with type <code>"thread"</code>. | ||
| 8544 | |||
| 8545 | |||
| 8546 | |||
| 8547 | |||
| 8548 | <p> | ||
| 8549 | <hr><h3><a name="pdf-coroutine.isyieldable"><code>coroutine.isyieldable ([co])</code></a></h3> | ||
| 8550 | |||
| 8551 | |||
| 8552 | <p> | ||
| 8553 | Returns <b>true</b> when the coroutine <code>co</code> can yield. | ||
| 8554 | The default for <code>co</code> is the running coroutine. | ||
| 8555 | |||
| 8556 | |||
| 8557 | <p> | ||
| 8558 | A coroutine is yieldable if it is not the main thread and | ||
| 8559 | it is not inside a non-yieldable C function. | ||
| 8560 | |||
| 8561 | |||
| 8562 | |||
| 8563 | |||
| 8564 | <p> | ||
| 8565 | <hr><h3><a name="pdf-coroutine.resume"><code>coroutine.resume (co [, val1, ···])</code></a></h3> | ||
| 8566 | |||
| 8567 | |||
| 8568 | <p> | ||
| 8569 | Starts or continues the execution of coroutine <code>co</code>. | ||
| 8570 | The first time you resume a coroutine, | ||
| 8571 | it starts running its body. | ||
| 8572 | The values <code>val1</code>, ... are passed | ||
| 8573 | as the arguments to the body function. | ||
| 8574 | If the coroutine has yielded, | ||
| 8575 | <code>resume</code> restarts it; | ||
| 8576 | the values <code>val1</code>, ... are passed | ||
| 8577 | as the results from the yield. | ||
| 8578 | |||
| 8579 | |||
| 8580 | <p> | ||
| 8581 | If the coroutine runs without any errors, | ||
| 8582 | <code>resume</code> returns <b>true</b> plus any values passed to <code>yield</code> | ||
| 8583 | (when the coroutine yields) or any values returned by the body function | ||
| 8584 | (when the coroutine terminates). | ||
| 8585 | If there is any error, | ||
| 8586 | <code>resume</code> returns <b>false</b> plus the error message. | ||
| 8587 | |||
| 8588 | |||
| 8589 | |||
| 8590 | |||
| 8591 | <p> | ||
| 8592 | <hr><h3><a name="pdf-coroutine.running"><code>coroutine.running ()</code></a></h3> | ||
| 8593 | |||
| 8594 | |||
| 8595 | <p> | ||
| 8596 | Returns the running coroutine plus a boolean, | ||
| 8597 | <b>true</b> when the running coroutine is the main one. | ||
| 8598 | |||
| 8599 | |||
| 8600 | |||
| 8601 | |||
| 8602 | <p> | ||
| 8603 | <hr><h3><a name="pdf-coroutine.status"><code>coroutine.status (co)</code></a></h3> | ||
| 8604 | |||
| 8605 | |||
| 8606 | <p> | ||
| 8607 | Returns the status of the coroutine <code>co</code>, as a string: | ||
| 8608 | <code>"running"</code>, | ||
| 8609 | if the coroutine is running | ||
| 8610 | (that is, it is the one that called <code>status</code>); | ||
| 8611 | <code>"suspended"</code>, if the coroutine is suspended in a call to <code>yield</code>, | ||
| 8612 | or if it has not started running yet; | ||
| 8613 | <code>"normal"</code> if the coroutine is active but not running | ||
| 8614 | (that is, it has resumed another coroutine); | ||
| 8615 | and <code>"dead"</code> if the coroutine has finished its body function, | ||
| 8616 | or if it has stopped with an error. | ||
| 8617 | |||
| 8618 | |||
| 8619 | |||
| 8620 | |||
| 8621 | <p> | ||
| 8622 | <hr><h3><a name="pdf-coroutine.wrap"><code>coroutine.wrap (f)</code></a></h3> | ||
| 8623 | |||
| 8624 | |||
| 8625 | <p> | ||
| 8626 | Creates a new coroutine, with body <code>f</code>; | ||
| 8627 | <code>f</code> must be a function. | ||
| 8628 | Returns a function that resumes the coroutine each time it is called. | ||
| 8629 | Any arguments passed to this function behave as the | ||
| 8630 | extra arguments to <code>resume</code>. | ||
| 8631 | The function returns the same values returned by <code>resume</code>, | ||
| 8632 | except the first boolean. | ||
| 8633 | In case of error, | ||
| 8634 | the function closes the coroutine and propagates the error. | ||
| 8635 | |||
| 8636 | |||
| 8637 | |||
| 8638 | |||
| 8639 | <p> | ||
| 8640 | <hr><h3><a name="pdf-coroutine.yield"><code>coroutine.yield (···)</code></a></h3> | ||
| 8641 | |||
| 8642 | |||
| 8643 | <p> | ||
| 8644 | Suspends the execution of the calling coroutine. | ||
| 8645 | Any arguments to <code>yield</code> are passed as extra results to <code>resume</code>. | ||
| 8646 | |||
| 8647 | |||
| 8648 | |||
| 8649 | |||
| 8650 | |||
| 8651 | |||
| 8652 | |||
| 8653 | <h2>6.3 – <a name="6.3">Modules</a></h2> | ||
| 8654 | |||
| 8655 | <p> | ||
| 8656 | The package library provides basic | ||
| 8657 | facilities for loading modules in Lua. | ||
| 8658 | It exports one function directly in the global environment: | ||
| 8659 | <a href="#pdf-require"><code>require</code></a>. | ||
| 8660 | Everything else is exported in the table <a name="pdf-package"><code>package</code></a>. | ||
| 8661 | |||
| 8662 | |||
| 8663 | <p> | ||
| 8664 | <hr><h3><a name="pdf-require"><code>require (modname)</code></a></h3> | ||
| 8665 | |||
| 8666 | |||
| 8667 | <p> | ||
| 8668 | Loads the given module. | ||
| 8669 | The function starts by looking into the <a href="#pdf-package.loaded"><code>package.loaded</code></a> table | ||
| 8670 | to determine whether <code>modname</code> is already loaded. | ||
| 8671 | If it is, then <code>require</code> returns the value stored | ||
| 8672 | at <code>package.loaded[modname]</code>. | ||
| 8673 | (The absence of a second result in this case | ||
| 8674 | signals that this call did not have to load the module.) | ||
| 8675 | Otherwise, it tries to find a <em>loader</em> for the module. | ||
| 8676 | |||
| 8677 | |||
| 8678 | <p> | ||
| 8679 | To find a loader, | ||
| 8680 | <code>require</code> is guided by the table <a href="#pdf-package.searchers"><code>package.searchers</code></a>. | ||
| 8681 | Each item in this table is a search function, | ||
| 8682 | that searches for the module in a particular way. | ||
| 8683 | By changing this table, | ||
| 8684 | we can change how <code>require</code> looks for a module. | ||
| 8685 | The following explanation is based on the default configuration | ||
| 8686 | for <a href="#pdf-package.searchers"><code>package.searchers</code></a>. | ||
| 8687 | |||
| 8688 | |||
| 8689 | <p> | ||
| 8690 | First <code>require</code> queries <code>package.preload[modname]</code>. | ||
| 8691 | If it has a value, | ||
| 8692 | this value (which must be a function) is the loader. | ||
| 8693 | Otherwise <code>require</code> searches for a Lua loader using the | ||
| 8694 | path stored in <a href="#pdf-package.path"><code>package.path</code></a>. | ||
| 8695 | If that also fails, it searches for a C loader using the | ||
| 8696 | path stored in <a href="#pdf-package.cpath"><code>package.cpath</code></a>. | ||
| 8697 | If that also fails, | ||
| 8698 | it tries an <em>all-in-one</em> loader (see <a href="#pdf-package.searchers"><code>package.searchers</code></a>). | ||
| 8699 | |||
| 8700 | |||
| 8701 | <p> | ||
| 8702 | Once a loader is found, | ||
| 8703 | <code>require</code> calls the loader with two arguments: | ||
| 8704 | <code>modname</code> and an extra value, | ||
| 8705 | a <em>loader data</em>, | ||
| 8706 | also returned by the searcher. | ||
| 8707 | The loader data can be any value useful to the module; | ||
| 8708 | for the default searchers, | ||
| 8709 | it indicates where the loader was found. | ||
| 8710 | (For instance, if the loader came from a file, | ||
| 8711 | this extra value is the file path.) | ||
| 8712 | If the loader returns any non-nil value, | ||
| 8713 | <code>require</code> assigns the returned value to <code>package.loaded[modname]</code>. | ||
| 8714 | If the loader does not return a non-nil value and | ||
| 8715 | has not assigned any value to <code>package.loaded[modname]</code>, | ||
| 8716 | then <code>require</code> assigns <b>true</b> to this entry. | ||
| 8717 | In any case, <code>require</code> returns the | ||
| 8718 | final value of <code>package.loaded[modname]</code>. | ||
| 8719 | Besides that value, <code>require</code> also returns as a second result | ||
| 8720 | the loader data returned by the searcher, | ||
| 8721 | which indicates how <code>require</code> found the module. | ||
| 8722 | |||
| 8723 | |||
| 8724 | <p> | ||
| 8725 | If there is any error loading or running the module, | ||
| 8726 | or if it cannot find any loader for the module, | ||
| 8727 | then <code>require</code> raises an error. | ||
| 8728 | |||
| 8729 | |||
| 8730 | |||
| 8731 | |||
| 8732 | <p> | ||
| 8733 | <hr><h3><a name="pdf-package.config"><code>package.config</code></a></h3> | ||
| 8734 | |||
| 8735 | |||
| 8736 | <p> | ||
| 8737 | A string describing some compile-time configurations for packages. | ||
| 8738 | This string is a sequence of lines: | ||
| 8739 | |||
| 8740 | <ul> | ||
| 8741 | |||
| 8742 | <li>The first line is the directory separator string. | ||
| 8743 | Default is '<code>\</code>' for Windows and '<code>/</code>' for all other systems.</li> | ||
| 8744 | |||
| 8745 | <li>The second line is the character that separates templates in a path. | ||
| 8746 | Default is '<code>;</code>'.</li> | ||
| 8747 | |||
| 8748 | <li>The third line is the string that marks the | ||
| 8749 | substitution points in a template. | ||
| 8750 | Default is '<code>?</code>'.</li> | ||
| 8751 | |||
| 8752 | <li>The fourth line is a string that, in a path in Windows, | ||
| 8753 | is replaced by the executable's directory. | ||
| 8754 | Default is '<code>!</code>'.</li> | ||
| 8755 | |||
| 8756 | <li>The fifth line is a mark to ignore all text after it | ||
| 8757 | when building the <code>luaopen_</code> function name. | ||
| 8758 | Default is '<code>-</code>'.</li> | ||
| 8759 | |||
| 8760 | </ul> | ||
| 8761 | |||
| 8762 | |||
| 8763 | |||
| 8764 | <p> | ||
| 8765 | <hr><h3><a name="pdf-package.cpath"><code>package.cpath</code></a></h3> | ||
| 8766 | |||
| 8767 | |||
| 8768 | <p> | ||
| 8769 | A string with the path used by <a href="#pdf-require"><code>require</code></a> | ||
| 8770 | to search for a C loader. | ||
| 8771 | |||
| 8772 | |||
| 8773 | <p> | ||
| 8774 | Lua initializes the C path <a href="#pdf-package.cpath"><code>package.cpath</code></a> in the same way | ||
| 8775 | it initializes the Lua path <a href="#pdf-package.path"><code>package.path</code></a>, | ||
| 8776 | using the environment variable <a name="pdf-LUA_CPATH_5_4"><code>LUA_CPATH_5_4</code></a>, | ||
| 8777 | or the environment variable <a name="pdf-LUA_CPATH"><code>LUA_CPATH</code></a>, | ||
| 8778 | or a default path defined in <code>luaconf.h</code>. | ||
| 8779 | |||
| 8780 | |||
| 8781 | |||
| 8782 | |||
| 8783 | <p> | ||
| 8784 | <hr><h3><a name="pdf-package.loaded"><code>package.loaded</code></a></h3> | ||
| 8785 | |||
| 8786 | |||
| 8787 | <p> | ||
| 8788 | A table used by <a href="#pdf-require"><code>require</code></a> to control which | ||
| 8789 | modules are already loaded. | ||
| 8790 | When you require a module <code>modname</code> and | ||
| 8791 | <code>package.loaded[modname]</code> is not false, | ||
| 8792 | <a href="#pdf-require"><code>require</code></a> simply returns the value stored there. | ||
| 8793 | |||
| 8794 | |||
| 8795 | <p> | ||
| 8796 | This variable is only a reference to the real table; | ||
| 8797 | assignments to this variable do not change the | ||
| 8798 | table used by <a href="#pdf-require"><code>require</code></a>. | ||
| 8799 | The real table is stored in the C registry (see <a href="#4.3">§4.3</a>), | ||
| 8800 | indexed by the key <a name="pdf-LUA_LOADED_TABLE"><code>LUA_LOADED_TABLE</code></a>, a string. | ||
| 8801 | |||
| 8802 | |||
| 8803 | |||
| 8804 | |||
| 8805 | <p> | ||
| 8806 | <hr><h3><a name="pdf-package.loadlib"><code>package.loadlib (libname, funcname)</code></a></h3> | ||
| 8807 | |||
| 8808 | |||
| 8809 | <p> | ||
| 8810 | Dynamically links the host program with the C library <code>libname</code>. | ||
| 8811 | |||
| 8812 | |||
| 8813 | <p> | ||
| 8814 | If <code>funcname</code> is "<code>*</code>", | ||
| 8815 | then it only links with the library, | ||
| 8816 | making the symbols exported by the library | ||
| 8817 | available to other dynamically linked libraries. | ||
| 8818 | Otherwise, | ||
| 8819 | it looks for a function <code>funcname</code> inside the library | ||
| 8820 | and returns this function as a C function. | ||
| 8821 | So, <code>funcname</code> must follow the <a href="#lua_CFunction"><code>lua_CFunction</code></a> prototype | ||
| 8822 | (see <a href="#lua_CFunction"><code>lua_CFunction</code></a>). | ||
| 8823 | |||
| 8824 | |||
| 8825 | <p> | ||
| 8826 | This is a low-level function. | ||
| 8827 | It completely bypasses the package and module system. | ||
| 8828 | Unlike <a href="#pdf-require"><code>require</code></a>, | ||
| 8829 | it does not perform any path searching and | ||
| 8830 | does not automatically adds extensions. | ||
| 8831 | <code>libname</code> must be the complete file name of the C library, | ||
| 8832 | including if necessary a path and an extension. | ||
| 8833 | <code>funcname</code> must be the exact name exported by the C library | ||
| 8834 | (which may depend on the C compiler and linker used). | ||
| 8835 | |||
| 8836 | |||
| 8837 | <p> | ||
| 8838 | This functionality is not supported by ISO C. | ||
| 8839 | As such, it is only available on some platforms | ||
| 8840 | (Windows, Linux, Mac OS X, Solaris, BSD, | ||
| 8841 | plus other Unix systems that support the <code>dlfcn</code> standard). | ||
| 8842 | |||
| 8843 | |||
| 8844 | <p> | ||
| 8845 | This function is inherently insecure, | ||
| 8846 | as it allows Lua to call any function in any readable dynamic | ||
| 8847 | library in the system. | ||
| 8848 | (Lua calls any function assuming the function | ||
| 8849 | has a proper prototype and respects a proper protocol | ||
| 8850 | (see <a href="#lua_CFunction"><code>lua_CFunction</code></a>). | ||
| 8851 | Therefore, | ||
| 8852 | calling an arbitrary function in an arbitrary dynamic library | ||
| 8853 | more often than not results in an access violation.) | ||
| 8854 | |||
| 8855 | |||
| 8856 | |||
| 8857 | |||
| 8858 | <p> | ||
| 8859 | <hr><h3><a name="pdf-package.path"><code>package.path</code></a></h3> | ||
| 8860 | |||
| 8861 | |||
| 8862 | <p> | ||
| 8863 | A string with the path used by <a href="#pdf-require"><code>require</code></a> | ||
| 8864 | to search for a Lua loader. | ||
| 8865 | |||
| 8866 | |||
| 8867 | <p> | ||
| 8868 | At start-up, Lua initializes this variable with | ||
| 8869 | the value of the environment variable <a name="pdf-LUA_PATH_5_4"><code>LUA_PATH_5_4</code></a> or | ||
| 8870 | the environment variable <a name="pdf-LUA_PATH"><code>LUA_PATH</code></a> or | ||
| 8871 | with a default path defined in <code>luaconf.h</code>, | ||
| 8872 | if those environment variables are not defined. | ||
| 8873 | A "<code>;;</code>" in the value of the environment variable | ||
| 8874 | is replaced by the default path. | ||
| 8875 | |||
| 8876 | |||
| 8877 | |||
| 8878 | |||
| 8879 | <p> | ||
| 8880 | <hr><h3><a name="pdf-package.preload"><code>package.preload</code></a></h3> | ||
| 8881 | |||
| 8882 | |||
| 8883 | <p> | ||
| 8884 | A table to store loaders for specific modules | ||
| 8885 | (see <a href="#pdf-require"><code>require</code></a>). | ||
| 8886 | |||
| 8887 | |||
| 8888 | <p> | ||
| 8889 | This variable is only a reference to the real table; | ||
| 8890 | assignments to this variable do not change the | ||
| 8891 | table used by <a href="#pdf-require"><code>require</code></a>. | ||
| 8892 | The real table is stored in the C registry (see <a href="#4.3">§4.3</a>), | ||
| 8893 | indexed by the key <a name="pdf-LUA_PRELOAD_TABLE"><code>LUA_PRELOAD_TABLE</code></a>, a string. | ||
| 8894 | |||
| 8895 | |||
| 8896 | |||
| 8897 | |||
| 8898 | <p> | ||
| 8899 | <hr><h3><a name="pdf-package.searchers"><code>package.searchers</code></a></h3> | ||
| 8900 | |||
| 8901 | |||
| 8902 | <p> | ||
| 8903 | A table used by <a href="#pdf-require"><code>require</code></a> to control how to find modules. | ||
| 8904 | |||
| 8905 | |||
| 8906 | <p> | ||
| 8907 | Each entry in this table is a <em>searcher function</em>. | ||
| 8908 | When looking for a module, | ||
| 8909 | <a href="#pdf-require"><code>require</code></a> calls each of these searchers in ascending order, | ||
| 8910 | with the module name (the argument given to <a href="#pdf-require"><code>require</code></a>) as its | ||
| 8911 | sole argument. | ||
| 8912 | If the searcher finds the module, | ||
| 8913 | it returns another function, the module <em>loader</em>, | ||
| 8914 | plus an extra value, a <em>loader data</em>, | ||
| 8915 | that will be passed to that loader and | ||
| 8916 | returned as a second result by <a href="#pdf-require"><code>require</code></a>. | ||
| 8917 | If it cannot find the module, | ||
| 8918 | it returns a string explaining why | ||
| 8919 | (or <b>nil</b> if it has nothing to say). | ||
| 8920 | |||
| 8921 | |||
| 8922 | <p> | ||
| 8923 | Lua initializes this table with four searcher functions. | ||
| 8924 | |||
| 8925 | |||
| 8926 | <p> | ||
| 8927 | The first searcher simply looks for a loader in the | ||
| 8928 | <a href="#pdf-package.preload"><code>package.preload</code></a> table. | ||
| 8929 | |||
| 8930 | |||
| 8931 | <p> | ||
| 8932 | The second searcher looks for a loader as a Lua library, | ||
| 8933 | using the path stored at <a href="#pdf-package.path"><code>package.path</code></a>. | ||
| 8934 | The search is done as described in function <a href="#pdf-package.searchpath"><code>package.searchpath</code></a>. | ||
| 8935 | |||
| 8936 | |||
| 8937 | <p> | ||
| 8938 | The third searcher looks for a loader as a C library, | ||
| 8939 | using the path given by the variable <a href="#pdf-package.cpath"><code>package.cpath</code></a>. | ||
| 8940 | Again, | ||
| 8941 | the search is done as described in function <a href="#pdf-package.searchpath"><code>package.searchpath</code></a>. | ||
| 8942 | For instance, | ||
| 8943 | if the C path is the string | ||
| 8944 | |||
| 8945 | <pre> | ||
| 8946 | "./?.so;./?.dll;/usr/local/?/init.so" | ||
| 8947 | </pre><p> | ||
| 8948 | the searcher for module <code>foo</code> | ||
| 8949 | will try to open the files <code>./foo.so</code>, <code>./foo.dll</code>, | ||
| 8950 | and <code>/usr/local/foo/init.so</code>, in that order. | ||
| 8951 | Once it finds a C library, | ||
| 8952 | this searcher first uses a dynamic link facility to link the | ||
| 8953 | application with the library. | ||
| 8954 | Then it tries to find a C function inside the library to | ||
| 8955 | be used as the loader. | ||
| 8956 | The name of this C function is the string "<code>luaopen_</code>" | ||
| 8957 | concatenated with a copy of the module name where each dot | ||
| 8958 | is replaced by an underscore. | ||
| 8959 | Moreover, if the module name has a hyphen, | ||
| 8960 | its suffix after (and including) the first hyphen is removed. | ||
| 8961 | For instance, if the module name is <code>a.b.c-v2.1</code>, | ||
| 8962 | the function name will be <code>luaopen_a_b_c</code>. | ||
| 8963 | |||
| 8964 | |||
| 8965 | <p> | ||
| 8966 | The fourth searcher tries an <em>all-in-one loader</em>. | ||
| 8967 | It searches the C path for a library for | ||
| 8968 | the root name of the given module. | ||
| 8969 | For instance, when requiring <code>a.b.c</code>, | ||
| 8970 | it will search for a C library for <code>a</code>. | ||
| 8971 | If found, it looks into it for an open function for | ||
| 8972 | the submodule; | ||
| 8973 | in our example, that would be <code>luaopen_a_b_c</code>. | ||
| 8974 | With this facility, a package can pack several C submodules | ||
| 8975 | into one single library, | ||
| 8976 | with each submodule keeping its original open function. | ||
| 8977 | |||
| 8978 | |||
| 8979 | <p> | ||
| 8980 | All searchers except the first one (preload) return as the extra value | ||
| 8981 | the file path where the module was found, | ||
| 8982 | as returned by <a href="#pdf-package.searchpath"><code>package.searchpath</code></a>. | ||
| 8983 | The first searcher always returns the string "<code>:preload:</code>". | ||
| 8984 | |||
| 8985 | |||
| 8986 | <p> | ||
| 8987 | Searchers should raise no errors and have no side effects in Lua. | ||
| 8988 | (They may have side effects in C, | ||
| 8989 | for instance by linking the application with a library.) | ||
| 8990 | |||
| 8991 | |||
| 8992 | |||
| 8993 | |||
| 8994 | <p> | ||
| 8995 | <hr><h3><a name="pdf-package.searchpath"><code>package.searchpath (name, path [, sep [, rep]])</code></a></h3> | ||
| 8996 | |||
| 8997 | |||
| 8998 | <p> | ||
| 8999 | Searches for the given <code>name</code> in the given <code>path</code>. | ||
| 9000 | |||
| 9001 | |||
| 9002 | <p> | ||
| 9003 | A path is a string containing a sequence of | ||
| 9004 | <em>templates</em> separated by semicolons. | ||
| 9005 | For each template, | ||
| 9006 | the function replaces each interrogation mark (if any) | ||
| 9007 | in the template with a copy of <code>name</code> | ||
| 9008 | wherein all occurrences of <code>sep</code> | ||
| 9009 | (a dot, by default) | ||
| 9010 | were replaced by <code>rep</code> | ||
| 9011 | (the system's directory separator, by default), | ||
| 9012 | and then tries to open the resulting file name. | ||
| 9013 | |||
| 9014 | |||
| 9015 | <p> | ||
| 9016 | For instance, if the path is the string | ||
| 9017 | |||
| 9018 | <pre> | ||
| 9019 | "./?.lua;./?.lc;/usr/local/?/init.lua" | ||
| 9020 | </pre><p> | ||
| 9021 | the search for the name <code>foo.a</code> | ||
| 9022 | will try to open the files | ||
| 9023 | <code>./foo/a.lua</code>, <code>./foo/a.lc</code>, and | ||
| 9024 | <code>/usr/local/foo/a/init.lua</code>, in that order. | ||
| 9025 | |||
| 9026 | |||
| 9027 | <p> | ||
| 9028 | Returns the resulting name of the first file that it can | ||
| 9029 | open in read mode (after closing the file), | ||
| 9030 | or <b>fail</b> plus an error message if none succeeds. | ||
| 9031 | (This error message lists all file names it tried to open.) | ||
| 9032 | |||
| 9033 | |||
| 9034 | |||
| 9035 | |||
| 9036 | |||
| 9037 | |||
| 9038 | |||
| 9039 | <h2>6.4 – <a name="6.4">String Manipulation</a></h2> | ||
| 9040 | |||
| 9041 | |||
| 9042 | |||
| 9043 | <p> | ||
| 9044 | This library provides generic functions for string manipulation, | ||
| 9045 | such as finding and extracting substrings, and pattern matching. | ||
| 9046 | When indexing a string in Lua, the first character is at position 1 | ||
| 9047 | (not at 0, as in C). | ||
| 9048 | Indices are allowed to be negative and are interpreted as indexing backwards, | ||
| 9049 | from the end of the string. | ||
| 9050 | Thus, the last character is at position -1, and so on. | ||
| 9051 | |||
| 9052 | |||
| 9053 | <p> | ||
| 9054 | The string library provides all its functions inside the table | ||
| 9055 | <a name="pdf-string"><code>string</code></a>. | ||
| 9056 | It also sets a metatable for strings | ||
| 9057 | where the <code>__index</code> field points to the <code>string</code> table. | ||
| 9058 | Therefore, you can use the string functions in object-oriented style. | ||
| 9059 | For instance, <code>string.byte(s,i)</code> | ||
| 9060 | can be written as <code>s:byte(i)</code>. | ||
| 9061 | |||
| 9062 | |||
| 9063 | <p> | ||
| 9064 | The string library assumes one-byte character encodings. | ||
| 9065 | |||
| 9066 | |||
| 9067 | <p> | ||
| 9068 | <hr><h3><a name="pdf-string.byte"><code>string.byte (s [, i [, j]])</code></a></h3> | ||
| 9069 | Returns the internal numeric codes of the characters <code>s[i]</code>, | ||
| 9070 | <code>s[i+1]</code>, ..., <code>s[j]</code>. | ||
| 9071 | The default value for <code>i</code> is 1; | ||
| 9072 | the default value for <code>j</code> is <code>i</code>. | ||
| 9073 | These indices are corrected | ||
| 9074 | following the same rules of function <a href="#pdf-string.sub"><code>string.sub</code></a>. | ||
| 9075 | |||
| 9076 | |||
| 9077 | <p> | ||
| 9078 | Numeric codes are not necessarily portable across platforms. | ||
| 9079 | |||
| 9080 | |||
| 9081 | |||
| 9082 | |||
| 9083 | <p> | ||
| 9084 | <hr><h3><a name="pdf-string.char"><code>string.char (···)</code></a></h3> | ||
| 9085 | Receives zero or more integers. | ||
| 9086 | Returns a string with length equal to the number of arguments, | ||
| 9087 | in which each character has the internal numeric code equal | ||
| 9088 | to its corresponding argument. | ||
| 9089 | |||
| 9090 | |||
| 9091 | <p> | ||
| 9092 | Numeric codes are not necessarily portable across platforms. | ||
| 9093 | |||
| 9094 | |||
| 9095 | |||
| 9096 | |||
| 9097 | <p> | ||
| 9098 | <hr><h3><a name="pdf-string.dump"><code>string.dump (function [, strip])</code></a></h3> | ||
| 9099 | |||
| 9100 | |||
| 9101 | <p> | ||
| 9102 | Returns a string containing a binary representation | ||
| 9103 | (a <em>binary chunk</em>) | ||
| 9104 | of the given function, | ||
| 9105 | so that a later <a href="#pdf-load"><code>load</code></a> on this string returns | ||
| 9106 | a copy of the function (but with new upvalues). | ||
| 9107 | If <code>strip</code> is a true value, | ||
| 9108 | the binary representation may not include all debug information | ||
| 9109 | about the function, | ||
| 9110 | to save space. | ||
| 9111 | |||
| 9112 | |||
| 9113 | <p> | ||
| 9114 | Functions with upvalues have only their number of upvalues saved. | ||
| 9115 | When (re)loaded, | ||
| 9116 | those upvalues receive fresh instances. | ||
| 9117 | (See the <a href="#pdf-load"><code>load</code></a> function for details about | ||
| 9118 | how these upvalues are initialized. | ||
| 9119 | You can use the debug library to serialize | ||
| 9120 | and reload the upvalues of a function | ||
| 9121 | in a way adequate to your needs.) | ||
| 9122 | |||
| 9123 | |||
| 9124 | |||
| 9125 | |||
| 9126 | <p> | ||
| 9127 | <hr><h3><a name="pdf-string.find"><code>string.find (s, pattern [, init [, plain]])</code></a></h3> | ||
| 9128 | |||
| 9129 | |||
| 9130 | <p> | ||
| 9131 | Looks for the first match of | ||
| 9132 | <code>pattern</code> (see <a href="#6.4.1">§6.4.1</a>) in the string <code>s</code>. | ||
| 9133 | If it finds a match, then <code>find</code> returns the indices of <code>s</code> | ||
| 9134 | where this occurrence starts and ends; | ||
| 9135 | otherwise, it returns <b>fail</b>. | ||
| 9136 | A third, optional numeric argument <code>init</code> specifies | ||
| 9137 | where to start the search; | ||
| 9138 | its default value is 1 and can be negative. | ||
| 9139 | A <b>true</b> as a fourth, optional argument <code>plain</code> | ||
| 9140 | turns off the pattern matching facilities, | ||
| 9141 | so the function does a plain "find substring" operation, | ||
| 9142 | with no characters in <code>pattern</code> being considered magic. | ||
| 9143 | |||
| 9144 | |||
| 9145 | <p> | ||
| 9146 | If the pattern has captures, | ||
| 9147 | then in a successful match | ||
| 9148 | the captured values are also returned, | ||
| 9149 | after the two indices. | ||
| 9150 | |||
| 9151 | |||
| 9152 | |||
| 9153 | |||
| 9154 | <p> | ||
| 9155 | <hr><h3><a name="pdf-string.format"><code>string.format (formatstring, ···)</code></a></h3> | ||
| 9156 | |||
| 9157 | |||
| 9158 | <p> | ||
| 9159 | Returns a formatted version of its variable number of arguments | ||
| 9160 | following the description given in its first argument, | ||
| 9161 | which must be a string. | ||
| 9162 | The format string follows the same rules as the ISO C function <code>sprintf</code>. | ||
| 9163 | The only differences are that the conversion specifiers and modifiers | ||
| 9164 | <code>F</code>, <code>n</code>, <code>*</code>, <code>h</code>, <code>L</code>, and <code>l</code> are not supported | ||
| 9165 | and that there is an extra specifier, <code>q</code>. | ||
| 9166 | Both width and precision, when present, | ||
| 9167 | are limited to two digits. | ||
| 9168 | |||
| 9169 | |||
| 9170 | <p> | ||
| 9171 | The specifier <code>q</code> formats booleans, nil, numbers, and strings | ||
| 9172 | in a way that the result is a valid constant in Lua source code. | ||
| 9173 | Booleans and nil are written in the obvious way | ||
| 9174 | (<code>true</code>, <code>false</code>, <code>nil</code>). | ||
| 9175 | Floats are written in hexadecimal, | ||
| 9176 | to preserve full precision. | ||
| 9177 | A string is written between double quotes, | ||
| 9178 | using escape sequences when necessary to ensure that | ||
| 9179 | it can safely be read back by the Lua interpreter. | ||
| 9180 | For instance, the call | ||
| 9181 | |||
| 9182 | <pre> | ||
| 9183 | string.format('%q', 'a string with "quotes" and \n new line') | ||
| 9184 | </pre><p> | ||
| 9185 | may produce the string: | ||
| 9186 | |||
| 9187 | <pre> | ||
| 9188 | "a string with \"quotes\" and \ | ||
| 9189 | new line" | ||
| 9190 | </pre><p> | ||
| 9191 | This specifier does not support modifiers (flags, width, precision). | ||
| 9192 | |||
| 9193 | |||
| 9194 | <p> | ||
| 9195 | The conversion specifiers | ||
| 9196 | <code>A</code>, <code>a</code>, <code>E</code>, <code>e</code>, <code>f</code>, | ||
| 9197 | <code>G</code>, and <code>g</code> all expect a number as argument. | ||
| 9198 | The specifiers <code>c</code>, <code>d</code>, | ||
| 9199 | <code>i</code>, <code>o</code>, <code>u</code>, <code>X</code>, and <code>x</code> | ||
| 9200 | expect an integer. | ||
| 9201 | When Lua is compiled with a C89 compiler, | ||
| 9202 | the specifiers <code>A</code> and <code>a</code> (hexadecimal floats) | ||
| 9203 | do not support modifiers. | ||
| 9204 | |||
| 9205 | |||
| 9206 | <p> | ||
| 9207 | The specifier <code>s</code> expects a string; | ||
| 9208 | if its argument is not a string, | ||
| 9209 | it is converted to one following the same rules of <a href="#pdf-tostring"><code>tostring</code></a>. | ||
| 9210 | If the specifier has any modifier, | ||
| 9211 | the corresponding string argument should not contain embedded zeros. | ||
| 9212 | |||
| 9213 | |||
| 9214 | <p> | ||
| 9215 | The specifier <code>p</code> formats the pointer | ||
| 9216 | returned by <a href="#lua_topointer"><code>lua_topointer</code></a>. | ||
| 9217 | That gives a unique string identifier for tables, userdata, | ||
| 9218 | threads, strings, and functions. | ||
| 9219 | For other values (numbers, nil, booleans), | ||
| 9220 | this specifier results in a string representing | ||
| 9221 | the pointer <code>NULL</code>. | ||
| 9222 | |||
| 9223 | |||
| 9224 | |||
| 9225 | |||
| 9226 | <p> | ||
| 9227 | <hr><h3><a name="pdf-string.gmatch"><code>string.gmatch (s, pattern [, init])</code></a></h3> | ||
| 9228 | Returns an iterator function that, | ||
| 9229 | each time it is called, | ||
| 9230 | returns the next captures from <code>pattern</code> (see <a href="#6.4.1">§6.4.1</a>) | ||
| 9231 | over the string <code>s</code>. | ||
| 9232 | If <code>pattern</code> specifies no captures, | ||
| 9233 | then the whole match is produced in each call. | ||
| 9234 | A third, optional numeric argument <code>init</code> specifies | ||
| 9235 | where to start the search; | ||
| 9236 | its default value is 1 and can be negative. | ||
| 9237 | |||
| 9238 | |||
| 9239 | <p> | ||
| 9240 | As an example, the following loop | ||
| 9241 | will iterate over all the words from string <code>s</code>, | ||
| 9242 | printing one per line: | ||
| 9243 | |||
| 9244 | <pre> | ||
| 9245 | s = "hello world from Lua" | ||
| 9246 | for w in string.gmatch(s, "%a+") do | ||
| 9247 | print(w) | ||
| 9248 | end | ||
| 9249 | </pre><p> | ||
| 9250 | The next example collects all pairs <code>key=value</code> from the | ||
| 9251 | given string into a table: | ||
| 9252 | |||
| 9253 | <pre> | ||
| 9254 | t = {} | ||
| 9255 | s = "from=world, to=Lua" | ||
| 9256 | for k, v in string.gmatch(s, "(%w+)=(%w+)") do | ||
| 9257 | t[k] = v | ||
| 9258 | end | ||
| 9259 | </pre> | ||
| 9260 | |||
| 9261 | <p> | ||
| 9262 | For this function, a caret '<code>^</code>' at the start of a pattern does not | ||
| 9263 | work as an anchor, as this would prevent the iteration. | ||
| 9264 | |||
| 9265 | |||
| 9266 | |||
| 9267 | |||
| 9268 | <p> | ||
| 9269 | <hr><h3><a name="pdf-string.gsub"><code>string.gsub (s, pattern, repl [, n])</code></a></h3> | ||
| 9270 | Returns a copy of <code>s</code> | ||
| 9271 | in which all (or the first <code>n</code>, if given) | ||
| 9272 | occurrences of the <code>pattern</code> (see <a href="#6.4.1">§6.4.1</a>) have been | ||
| 9273 | replaced by a replacement string specified by <code>repl</code>, | ||
| 9274 | which can be a string, a table, or a function. | ||
| 9275 | <code>gsub</code> also returns, as its second value, | ||
| 9276 | the total number of matches that occurred. | ||
| 9277 | The name <code>gsub</code> comes from <em>Global SUBstitution</em>. | ||
| 9278 | |||
| 9279 | |||
| 9280 | <p> | ||
| 9281 | If <code>repl</code> is a string, then its value is used for replacement. | ||
| 9282 | The character <code>%</code> works as an escape character: | ||
| 9283 | any sequence in <code>repl</code> of the form <code>%<em>d</em></code>, | ||
| 9284 | with <em>d</em> between 1 and 9, | ||
| 9285 | stands for the value of the <em>d</em>-th captured substring; | ||
| 9286 | the sequence <code>%0</code> stands for the whole match; | ||
| 9287 | the sequence <code>%%</code> stands for a single <code>%</code>. | ||
| 9288 | |||
| 9289 | |||
| 9290 | <p> | ||
| 9291 | If <code>repl</code> is a table, then the table is queried for every match, | ||
| 9292 | using the first capture as the key. | ||
| 9293 | |||
| 9294 | |||
| 9295 | <p> | ||
| 9296 | If <code>repl</code> is a function, then this function is called every time a | ||
| 9297 | match occurs, with all captured substrings passed as arguments, | ||
| 9298 | in order. | ||
| 9299 | |||
| 9300 | |||
| 9301 | <p> | ||
| 9302 | In any case, | ||
| 9303 | if the pattern specifies no captures, | ||
| 9304 | then it behaves as if the whole pattern was inside a capture. | ||
| 9305 | |||
| 9306 | |||
| 9307 | <p> | ||
| 9308 | If the value returned by the table query or by the function call | ||
| 9309 | is a string or a number, | ||
| 9310 | then it is used as the replacement string; | ||
| 9311 | otherwise, if it is <b>false</b> or <b>nil</b>, | ||
| 9312 | then there is no replacement | ||
| 9313 | (that is, the original match is kept in the string). | ||
| 9314 | |||
| 9315 | |||
| 9316 | <p> | ||
| 9317 | Here are some examples: | ||
| 9318 | |||
| 9319 | <pre> | ||
| 9320 | x = string.gsub("hello world", "(%w+)", "%1 %1") | ||
| 9321 | --> x="hello hello world world" | ||
| 9322 | |||
| 9323 | x = string.gsub("hello world", "%w+", "%0 %0", 1) | ||
| 9324 | --> x="hello hello world" | ||
| 9325 | |||
| 9326 | x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1") | ||
| 9327 | --> x="world hello Lua from" | ||
| 9328 | |||
| 9329 | x = string.gsub("home = $HOME, user = $USER", "%$(%w+)", os.getenv) | ||
| 9330 | --> x="home = /home/roberto, user = roberto" | ||
| 9331 | |||
| 9332 | x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s) | ||
| 9333 | return load(s)() | ||
| 9334 | end) | ||
| 9335 | --> x="4+5 = 9" | ||
| 9336 | |||
| 9337 | local t = {name="lua", version="5.4"} | ||
| 9338 | x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t) | ||
| 9339 | --> x="lua-5.4.tar.gz" | ||
| 9340 | </pre> | ||
| 9341 | |||
| 9342 | |||
| 9343 | |||
| 9344 | <p> | ||
| 9345 | <hr><h3><a name="pdf-string.len"><code>string.len (s)</code></a></h3> | ||
| 9346 | |||
| 9347 | |||
| 9348 | <p> | ||
| 9349 | Receives a string and returns its length. | ||
| 9350 | The empty string <code>""</code> has length 0. | ||
| 9351 | Embedded zeros are counted, | ||
| 9352 | so <code>"a\000bc\000"</code> has length 5. | ||
| 9353 | |||
| 9354 | |||
| 9355 | |||
| 9356 | |||
| 9357 | <p> | ||
| 9358 | <hr><h3><a name="pdf-string.lower"><code>string.lower (s)</code></a></h3> | ||
| 9359 | |||
| 9360 | |||
| 9361 | <p> | ||
| 9362 | Receives a string and returns a copy of this string with all | ||
| 9363 | uppercase letters changed to lowercase. | ||
| 9364 | All other characters are left unchanged. | ||
| 9365 | The definition of what an uppercase letter is depends on the current locale. | ||
| 9366 | |||
| 9367 | |||
| 9368 | |||
| 9369 | |||
| 9370 | <p> | ||
| 9371 | <hr><h3><a name="pdf-string.match"><code>string.match (s, pattern [, init])</code></a></h3> | ||
| 9372 | |||
| 9373 | |||
| 9374 | <p> | ||
| 9375 | Looks for the first <em>match</em> of | ||
| 9376 | the <code>pattern</code> (see <a href="#6.4.1">§6.4.1</a>) in the string <code>s</code>. | ||
| 9377 | If it finds one, then <code>match</code> returns | ||
| 9378 | the captures from the pattern; | ||
| 9379 | otherwise it returns <b>fail</b>. | ||
| 9380 | If <code>pattern</code> specifies no captures, | ||
| 9381 | then the whole match is returned. | ||
| 9382 | A third, optional numeric argument <code>init</code> specifies | ||
| 9383 | where to start the search; | ||
| 9384 | its default value is 1 and can be negative. | ||
| 9385 | |||
| 9386 | |||
| 9387 | |||
| 9388 | |||
| 9389 | <p> | ||
| 9390 | <hr><h3><a name="pdf-string.pack"><code>string.pack (fmt, v1, v2, ···)</code></a></h3> | ||
| 9391 | |||
| 9392 | |||
| 9393 | <p> | ||
| 9394 | Returns a binary string containing the values <code>v1</code>, <code>v2</code>, etc. | ||
| 9395 | serialized in binary form (packed) | ||
| 9396 | according to the format string <code>fmt</code> (see <a href="#6.4.2">§6.4.2</a>). | ||
| 9397 | |||
| 9398 | |||
| 9399 | |||
| 9400 | |||
| 9401 | <p> | ||
| 9402 | <hr><h3><a name="pdf-string.packsize"><code>string.packsize (fmt)</code></a></h3> | ||
| 9403 | |||
| 9404 | |||
| 9405 | <p> | ||
| 9406 | Returns the length of a string resulting from <a href="#pdf-string.pack"><code>string.pack</code></a> | ||
| 9407 | with the given format. | ||
| 9408 | The format string cannot have the variable-length options | ||
| 9409 | '<code>s</code>' or '<code>z</code>' (see <a href="#6.4.2">§6.4.2</a>). | ||
| 9410 | |||
| 9411 | |||
| 9412 | |||
| 9413 | |||
| 9414 | <p> | ||
| 9415 | <hr><h3><a name="pdf-string.rep"><code>string.rep (s, n [, sep])</code></a></h3> | ||
| 9416 | |||
| 9417 | |||
| 9418 | <p> | ||
| 9419 | Returns a string that is the concatenation of <code>n</code> copies of | ||
| 9420 | the string <code>s</code> separated by the string <code>sep</code>. | ||
| 9421 | The default value for <code>sep</code> is the empty string | ||
| 9422 | (that is, no separator). | ||
| 9423 | Returns the empty string if <code>n</code> is not positive. | ||
| 9424 | |||
| 9425 | |||
| 9426 | <p> | ||
| 9427 | (Note that it is very easy to exhaust the memory of your machine | ||
| 9428 | with a single call to this function.) | ||
| 9429 | |||
| 9430 | |||
| 9431 | |||
| 9432 | |||
| 9433 | <p> | ||
| 9434 | <hr><h3><a name="pdf-string.reverse"><code>string.reverse (s)</code></a></h3> | ||
| 9435 | |||
| 9436 | |||
| 9437 | <p> | ||
| 9438 | Returns a string that is the string <code>s</code> reversed. | ||
| 9439 | |||
| 9440 | |||
| 9441 | |||
| 9442 | |||
| 9443 | <p> | ||
| 9444 | <hr><h3><a name="pdf-string.sub"><code>string.sub (s, i [, j])</code></a></h3> | ||
| 9445 | |||
| 9446 | |||
| 9447 | <p> | ||
| 9448 | Returns the substring of <code>s</code> that | ||
| 9449 | starts at <code>i</code> and continues until <code>j</code>; | ||
| 9450 | <code>i</code> and <code>j</code> can be negative. | ||
| 9451 | If <code>j</code> is absent, then it is assumed to be equal to -1 | ||
| 9452 | (which is the same as the string length). | ||
| 9453 | In particular, | ||
| 9454 | the call <code>string.sub(s,1,j)</code> returns a prefix of <code>s</code> | ||
| 9455 | with length <code>j</code>, | ||
| 9456 | and <code>string.sub(s, -i)</code> (for a positive <code>i</code>) | ||
| 9457 | returns a suffix of <code>s</code> | ||
| 9458 | with length <code>i</code>. | ||
| 9459 | |||
| 9460 | |||
| 9461 | <p> | ||
| 9462 | If, after the translation of negative indices, | ||
| 9463 | <code>i</code> is less than 1, | ||
| 9464 | it is corrected to 1. | ||
| 9465 | If <code>j</code> is greater than the string length, | ||
| 9466 | it is corrected to that length. | ||
| 9467 | If, after these corrections, | ||
| 9468 | <code>i</code> is greater than <code>j</code>, | ||
| 9469 | the function returns the empty string. | ||
| 9470 | |||
| 9471 | |||
| 9472 | |||
| 9473 | |||
| 9474 | <p> | ||
| 9475 | <hr><h3><a name="pdf-string.unpack"><code>string.unpack (fmt, s [, pos])</code></a></h3> | ||
| 9476 | |||
| 9477 | |||
| 9478 | <p> | ||
| 9479 | Returns the values packed in string <code>s</code> (see <a href="#pdf-string.pack"><code>string.pack</code></a>) | ||
| 9480 | according to the format string <code>fmt</code> (see <a href="#6.4.2">§6.4.2</a>). | ||
| 9481 | An optional <code>pos</code> marks where | ||
| 9482 | to start reading in <code>s</code> (default is 1). | ||
| 9483 | After the read values, | ||
| 9484 | this function also returns the index of the first unread byte in <code>s</code>. | ||
| 9485 | |||
| 9486 | |||
| 9487 | |||
| 9488 | |||
| 9489 | <p> | ||
| 9490 | <hr><h3><a name="pdf-string.upper"><code>string.upper (s)</code></a></h3> | ||
| 9491 | |||
| 9492 | |||
| 9493 | <p> | ||
| 9494 | Receives a string and returns a copy of this string with all | ||
| 9495 | lowercase letters changed to uppercase. | ||
| 9496 | All other characters are left unchanged. | ||
| 9497 | The definition of what a lowercase letter is depends on the current locale. | ||
| 9498 | |||
| 9499 | |||
| 9500 | |||
| 9501 | |||
| 9502 | |||
| 9503 | |||
| 9504 | |||
| 9505 | <h3>6.4.1 – <a name="6.4.1">Patterns</a></h3> | ||
| 9506 | |||
| 9507 | |||
| 9508 | |||
| 9509 | <p> | ||
| 9510 | Patterns in Lua are described by regular strings, | ||
| 9511 | which are interpreted as patterns by the pattern-matching functions | ||
| 9512 | <a href="#pdf-string.find"><code>string.find</code></a>, | ||
| 9513 | <a href="#pdf-string.gmatch"><code>string.gmatch</code></a>, | ||
| 9514 | <a href="#pdf-string.gsub"><code>string.gsub</code></a>, | ||
| 9515 | and <a href="#pdf-string.match"><code>string.match</code></a>. | ||
| 9516 | This section describes the syntax and the meaning | ||
| 9517 | (that is, what they match) of these strings. | ||
| 9518 | |||
| 9519 | |||
| 9520 | |||
| 9521 | |||
| 9522 | |||
| 9523 | <h4>Character Class:</h4><p> | ||
| 9524 | A <em>character class</em> is used to represent a set of characters. | ||
| 9525 | The following combinations are allowed in describing a character class: | ||
| 9526 | |||
| 9527 | <ul> | ||
| 9528 | |||
| 9529 | <li><b><em>x</em>: </b> | ||
| 9530 | (where <em>x</em> is not one of the <em>magic characters</em> | ||
| 9531 | <code>^$()%.[]*+-?</code>) | ||
| 9532 | represents the character <em>x</em> itself. | ||
| 9533 | </li> | ||
| 9534 | |||
| 9535 | <li><b><code>.</code>: </b> (a dot) represents all characters.</li> | ||
| 9536 | |||
| 9537 | <li><b><code>%a</code>: </b> represents all letters.</li> | ||
| 9538 | |||
| 9539 | <li><b><code>%c</code>: </b> represents all control characters.</li> | ||
| 9540 | |||
| 9541 | <li><b><code>%d</code>: </b> represents all digits.</li> | ||
| 9542 | |||
| 9543 | <li><b><code>%g</code>: </b> represents all printable characters except space.</li> | ||
| 9544 | |||
| 9545 | <li><b><code>%l</code>: </b> represents all lowercase letters.</li> | ||
| 9546 | |||
| 9547 | <li><b><code>%p</code>: </b> represents all punctuation characters.</li> | ||
| 9548 | |||
| 9549 | <li><b><code>%s</code>: </b> represents all space characters.</li> | ||
| 9550 | |||
| 9551 | <li><b><code>%u</code>: </b> represents all uppercase letters.</li> | ||
| 9552 | |||
| 9553 | <li><b><code>%w</code>: </b> represents all alphanumeric characters.</li> | ||
| 9554 | |||
| 9555 | <li><b><code>%x</code>: </b> represents all hexadecimal digits.</li> | ||
| 9556 | |||
| 9557 | <li><b><code>%<em>x</em></code>: </b> (where <em>x</em> is any non-alphanumeric character) | ||
| 9558 | represents the character <em>x</em>. | ||
| 9559 | This is the standard way to escape the magic characters. | ||
| 9560 | Any non-alphanumeric character | ||
| 9561 | (including all punctuation characters, even the non-magical) | ||
| 9562 | can be preceded by a '<code>%</code>' to represent itself in a pattern. | ||
| 9563 | </li> | ||
| 9564 | |||
| 9565 | <li><b><code>[<em>set</em>]</code>: </b> | ||
| 9566 | represents the class which is the union of all | ||
| 9567 | characters in <em>set</em>. | ||
| 9568 | A range of characters can be specified by | ||
| 9569 | separating the end characters of the range, | ||
| 9570 | in ascending order, with a '<code>-</code>'. | ||
| 9571 | All classes <code>%</code><em>x</em> described above can also be used as | ||
| 9572 | components in <em>set</em>. | ||
| 9573 | All other characters in <em>set</em> represent themselves. | ||
| 9574 | For example, <code>[%w_]</code> (or <code>[_%w]</code>) | ||
| 9575 | represents all alphanumeric characters plus the underscore, | ||
| 9576 | <code>[0-7]</code> represents the octal digits, | ||
| 9577 | and <code>[0-7%l%-]</code> represents the octal digits plus | ||
| 9578 | the lowercase letters plus the '<code>-</code>' character. | ||
| 9579 | |||
| 9580 | |||
| 9581 | <p> | ||
| 9582 | You can put a closing square bracket in a set | ||
| 9583 | by positioning it as the first character in the set. | ||
| 9584 | You can put a hyphen in a set | ||
| 9585 | by positioning it as the first or the last character in the set. | ||
| 9586 | (You can also use an escape for both cases.) | ||
| 9587 | |||
| 9588 | |||
| 9589 | <p> | ||
| 9590 | The interaction between ranges and classes is not defined. | ||
| 9591 | Therefore, patterns like <code>[%a-z]</code> or <code>[a-%%]</code> | ||
| 9592 | have no meaning. | ||
| 9593 | </li> | ||
| 9594 | |||
| 9595 | <li><b><code>[^<em>set</em>]</code>: </b> | ||
| 9596 | represents the complement of <em>set</em>, | ||
| 9597 | where <em>set</em> is interpreted as above. | ||
| 9598 | </li> | ||
| 9599 | |||
| 9600 | </ul><p> | ||
| 9601 | For all classes represented by single letters (<code>%a</code>, <code>%c</code>, etc.), | ||
| 9602 | the corresponding uppercase letter represents the complement of the class. | ||
| 9603 | For instance, <code>%S</code> represents all non-space characters. | ||
| 9604 | |||
| 9605 | |||
| 9606 | <p> | ||
| 9607 | The definitions of letter, space, and other character groups | ||
| 9608 | depend on the current locale. | ||
| 9609 | In particular, the class <code>[a-z]</code> may not be equivalent to <code>%l</code>. | ||
| 9610 | |||
| 9611 | |||
| 9612 | |||
| 9613 | |||
| 9614 | |||
| 9615 | <h4>Pattern Item:</h4><p> | ||
| 9616 | A <em>pattern item</em> can be | ||
| 9617 | |||
| 9618 | <ul> | ||
| 9619 | |||
| 9620 | <li> | ||
| 9621 | a single character class, | ||
| 9622 | which matches any single character in the class; | ||
| 9623 | </li> | ||
| 9624 | |||
| 9625 | <li> | ||
| 9626 | a single character class followed by '<code>*</code>', | ||
| 9627 | which matches sequences of zero or more characters in the class. | ||
| 9628 | These repetition items will always match the longest possible sequence; | ||
| 9629 | </li> | ||
| 9630 | |||
| 9631 | <li> | ||
| 9632 | a single character class followed by '<code>+</code>', | ||
| 9633 | which matches sequences of one or more characters in the class. | ||
| 9634 | These repetition items will always match the longest possible sequence; | ||
| 9635 | </li> | ||
| 9636 | |||
| 9637 | <li> | ||
| 9638 | a single character class followed by '<code>-</code>', | ||
| 9639 | which also matches sequences of zero or more characters in the class. | ||
| 9640 | Unlike '<code>*</code>', | ||
| 9641 | these repetition items will always match the shortest possible sequence; | ||
| 9642 | </li> | ||
| 9643 | |||
| 9644 | <li> | ||
| 9645 | a single character class followed by '<code>?</code>', | ||
| 9646 | which matches zero or one occurrence of a character in the class. | ||
| 9647 | It always matches one occurrence if possible; | ||
| 9648 | </li> | ||
| 9649 | |||
| 9650 | <li> | ||
| 9651 | <code>%<em>n</em></code>, for <em>n</em> between 1 and 9; | ||
| 9652 | such item matches a substring equal to the <em>n</em>-th captured string | ||
| 9653 | (see below); | ||
| 9654 | </li> | ||
| 9655 | |||
| 9656 | <li> | ||
| 9657 | <code>%b<em>xy</em></code>, where <em>x</em> and <em>y</em> are two distinct characters; | ||
| 9658 | such item matches strings that start with <em>x</em>, end with <em>y</em>, | ||
| 9659 | and where the <em>x</em> and <em>y</em> are <em>balanced</em>. | ||
| 9660 | This means that, if one reads the string from left to right, | ||
| 9661 | counting <em>+1</em> for an <em>x</em> and <em>-1</em> for a <em>y</em>, | ||
| 9662 | the ending <em>y</em> is the first <em>y</em> where the count reaches 0. | ||
| 9663 | For instance, the item <code>%b()</code> matches expressions with | ||
| 9664 | balanced parentheses. | ||
| 9665 | </li> | ||
| 9666 | |||
| 9667 | <li> | ||
| 9668 | <code>%f[<em>set</em>]</code>, a <em>frontier pattern</em>; | ||
| 9669 | such item matches an empty string at any position such that | ||
| 9670 | the next character belongs to <em>set</em> | ||
| 9671 | and the previous character does not belong to <em>set</em>. | ||
| 9672 | The set <em>set</em> is interpreted as previously described. | ||
| 9673 | The beginning and the end of the subject are handled as if | ||
| 9674 | they were the character '<code>\0</code>'. | ||
| 9675 | </li> | ||
| 9676 | |||
| 9677 | </ul> | ||
| 9678 | |||
| 9679 | |||
| 9680 | |||
| 9681 | |||
| 9682 | <h4>Pattern:</h4><p> | ||
| 9683 | A <em>pattern</em> is a sequence of pattern items. | ||
| 9684 | A caret '<code>^</code>' at the beginning of a pattern anchors the match at the | ||
| 9685 | beginning of the subject string. | ||
| 9686 | A '<code>$</code>' at the end of a pattern anchors the match at the | ||
| 9687 | end of the subject string. | ||
| 9688 | At other positions, | ||
| 9689 | '<code>^</code>' and '<code>$</code>' have no special meaning and represent themselves. | ||
| 9690 | |||
| 9691 | |||
| 9692 | |||
| 9693 | |||
| 9694 | |||
| 9695 | <h4>Captures:</h4><p> | ||
| 9696 | A pattern can contain sub-patterns enclosed in parentheses; | ||
| 9697 | they describe <em>captures</em>. | ||
| 9698 | When a match succeeds, the substrings of the subject string | ||
| 9699 | that match captures are stored (<em>captured</em>) for future use. | ||
| 9700 | Captures are numbered according to their left parentheses. | ||
| 9701 | For instance, in the pattern <code>"(a*(.)%w(%s*))"</code>, | ||
| 9702 | the part of the string matching <code>"a*(.)%w(%s*)"</code> is | ||
| 9703 | stored as the first capture, and therefore has number 1; | ||
| 9704 | the character matching "<code>.</code>" is captured with number 2, | ||
| 9705 | and the part matching "<code>%s*</code>" has number 3. | ||
| 9706 | |||
| 9707 | |||
| 9708 | <p> | ||
| 9709 | As a special case, the capture <code>()</code> captures | ||
| 9710 | the current string position (a number). | ||
| 9711 | For instance, if we apply the pattern <code>"()aa()"</code> on the | ||
| 9712 | string <code>"flaaap"</code>, there will be two captures: 3 and 5. | ||
| 9713 | |||
| 9714 | |||
| 9715 | |||
| 9716 | |||
| 9717 | |||
| 9718 | <h4>Multiple matches:</h4><p> | ||
| 9719 | The function <a href="#pdf-string.gsub"><code>string.gsub</code></a> and the iterator <a href="#pdf-string.gmatch"><code>string.gmatch</code></a> | ||
| 9720 | match multiple occurrences of the given pattern in the subject. | ||
| 9721 | For these functions, | ||
| 9722 | a new match is considered valid only | ||
| 9723 | if it ends at least one byte after the end of the previous match. | ||
| 9724 | In other words, the pattern machine never accepts the | ||
| 9725 | empty string as a match immediately after another match. | ||
| 9726 | As an example, | ||
| 9727 | consider the results of the following code: | ||
| 9728 | |||
| 9729 | <pre> | ||
| 9730 | > string.gsub("abc", "()a*()", print); | ||
| 9731 | --> 1 2 | ||
| 9732 | --> 3 3 | ||
| 9733 | --> 4 4 | ||
| 9734 | </pre><p> | ||
| 9735 | The second and third results come from Lua matching an empty | ||
| 9736 | string after '<code>b</code>' and another one after '<code>c</code>'. | ||
| 9737 | Lua does not match an empty string after '<code>a</code>', | ||
| 9738 | because it would end at the same position of the previous match. | ||
| 9739 | |||
| 9740 | |||
| 9741 | |||
| 9742 | |||
| 9743 | |||
| 9744 | |||
| 9745 | |||
| 9746 | <h3>6.4.2 – <a name="6.4.2">Format Strings for Pack and Unpack</a></h3> | ||
| 9747 | |||
| 9748 | <p> | ||
| 9749 | The first argument to <a href="#pdf-string.pack"><code>string.pack</code></a>, | ||
| 9750 | <a href="#pdf-string.packsize"><code>string.packsize</code></a>, and <a href="#pdf-string.unpack"><code>string.unpack</code></a> | ||
| 9751 | is a format string, | ||
| 9752 | which describes the layout of the structure being created or read. | ||
| 9753 | |||
| 9754 | |||
| 9755 | <p> | ||
| 9756 | A format string is a sequence of conversion options. | ||
| 9757 | The conversion options are as follows: | ||
| 9758 | |||
| 9759 | <ul> | ||
| 9760 | <li><b><code><</code>: </b>sets little endian</li> | ||
| 9761 | <li><b><code>></code>: </b>sets big endian</li> | ||
| 9762 | <li><b><code>=</code>: </b>sets native endian</li> | ||
| 9763 | <li><b><code>![<em>n</em>]</code>: </b>sets maximum alignment to <code>n</code> | ||
| 9764 | (default is native alignment)</li> | ||
| 9765 | <li><b><code>b</code>: </b>a signed byte (<code>char</code>)</li> | ||
| 9766 | <li><b><code>B</code>: </b>an unsigned byte (<code>char</code>)</li> | ||
| 9767 | <li><b><code>h</code>: </b>a signed <code>short</code> (native size)</li> | ||
| 9768 | <li><b><code>H</code>: </b>an unsigned <code>short</code> (native size)</li> | ||
| 9769 | <li><b><code>l</code>: </b>a signed <code>long</code> (native size)</li> | ||
| 9770 | <li><b><code>L</code>: </b>an unsigned <code>long</code> (native size)</li> | ||
| 9771 | <li><b><code>j</code>: </b>a <code>lua_Integer</code></li> | ||
| 9772 | <li><b><code>J</code>: </b>a <code>lua_Unsigned</code></li> | ||
| 9773 | <li><b><code>T</code>: </b>a <code>size_t</code> (native size)</li> | ||
| 9774 | <li><b><code>i[<em>n</em>]</code>: </b>a signed <code>int</code> with <code>n</code> bytes | ||
| 9775 | (default is native size)</li> | ||
| 9776 | <li><b><code>I[<em>n</em>]</code>: </b>an unsigned <code>int</code> with <code>n</code> bytes | ||
| 9777 | (default is native size)</li> | ||
| 9778 | <li><b><code>f</code>: </b>a <code>float</code> (native size)</li> | ||
| 9779 | <li><b><code>d</code>: </b>a <code>double</code> (native size)</li> | ||
| 9780 | <li><b><code>n</code>: </b>a <code>lua_Number</code></li> | ||
| 9781 | <li><b><code>c<em>n</em></code>: </b>a fixed-sized string with <code>n</code> bytes</li> | ||
| 9782 | <li><b><code>z</code>: </b>a zero-terminated string</li> | ||
| 9783 | <li><b><code>s[<em>n</em>]</code>: </b>a string preceded by its length | ||
| 9784 | coded as an unsigned integer with <code>n</code> bytes | ||
| 9785 | (default is a <code>size_t</code>)</li> | ||
| 9786 | <li><b><code>x</code>: </b>one byte of padding</li> | ||
| 9787 | <li><b><code>X<em>op</em></code>: </b>an empty item that aligns | ||
| 9788 | according to option <code>op</code> | ||
| 9789 | (which is otherwise ignored)</li> | ||
| 9790 | <li><b>'<code> </code>': </b>(space) ignored</li> | ||
| 9791 | </ul><p> | ||
| 9792 | (A "<code>[<em>n</em>]</code>" means an optional integral numeral.) | ||
| 9793 | Except for padding, spaces, and configurations | ||
| 9794 | (options "<code>xX <=>!</code>"), | ||
| 9795 | each option corresponds to an argument in <a href="#pdf-string.pack"><code>string.pack</code></a> | ||
| 9796 | or a result in <a href="#pdf-string.unpack"><code>string.unpack</code></a>. | ||
| 9797 | |||
| 9798 | |||
| 9799 | <p> | ||
| 9800 | For options "<code>!<em>n</em></code>", "<code>s<em>n</em></code>", "<code>i<em>n</em></code>", and "<code>I<em>n</em></code>", | ||
| 9801 | <code>n</code> can be any integer between 1 and 16. | ||
| 9802 | All integral options check overflows; | ||
| 9803 | <a href="#pdf-string.pack"><code>string.pack</code></a> checks whether the given value fits in the given size; | ||
| 9804 | <a href="#pdf-string.unpack"><code>string.unpack</code></a> checks whether the read value fits in a Lua integer. | ||
| 9805 | For the unsigned options, | ||
| 9806 | Lua integers are treated as unsigned values too. | ||
| 9807 | |||
| 9808 | |||
| 9809 | <p> | ||
| 9810 | Any format string starts as if prefixed by "<code>!1=</code>", | ||
| 9811 | that is, | ||
| 9812 | with maximum alignment of 1 (no alignment) | ||
| 9813 | and native endianness. | ||
| 9814 | |||
| 9815 | |||
| 9816 | <p> | ||
| 9817 | Native endianness assumes that the whole system is | ||
| 9818 | either big or little endian. | ||
| 9819 | The packing functions will not emulate correctly the behavior | ||
| 9820 | of mixed-endian formats. | ||
| 9821 | |||
| 9822 | |||
| 9823 | <p> | ||
| 9824 | Alignment works as follows: | ||
| 9825 | For each option, | ||
| 9826 | the format gets extra padding until the data starts | ||
| 9827 | at an offset that is a multiple of the minimum between the | ||
| 9828 | option size and the maximum alignment; | ||
| 9829 | this minimum must be a power of 2. | ||
| 9830 | Options "<code>c</code>" and "<code>z</code>" are not aligned; | ||
| 9831 | option "<code>s</code>" follows the alignment of its starting integer. | ||
| 9832 | |||
| 9833 | |||
| 9834 | <p> | ||
| 9835 | All padding is filled with zeros by <a href="#pdf-string.pack"><code>string.pack</code></a> | ||
| 9836 | and ignored by <a href="#pdf-string.unpack"><code>string.unpack</code></a>. | ||
| 9837 | |||
| 9838 | |||
| 9839 | |||
| 9840 | |||
| 9841 | |||
| 9842 | |||
| 9843 | |||
| 9844 | <h2>6.5 – <a name="6.5">UTF-8 Support</a></h2> | ||
| 9845 | |||
| 9846 | <p> | ||
| 9847 | This library provides basic support for UTF-8 encoding. | ||
| 9848 | It provides all its functions inside the table <a name="pdf-utf8"><code>utf8</code></a>. | ||
| 9849 | This library does not provide any support for Unicode other | ||
| 9850 | than the handling of the encoding. | ||
| 9851 | Any operation that needs the meaning of a character, | ||
| 9852 | such as character classification, is outside its scope. | ||
| 9853 | |||
| 9854 | |||
| 9855 | <p> | ||
| 9856 | Unless stated otherwise, | ||
| 9857 | all functions that expect a byte position as a parameter | ||
| 9858 | assume that the given position is either the start of a byte sequence | ||
| 9859 | or one plus the length of the subject string. | ||
| 9860 | As in the string library, | ||
| 9861 | negative indices count from the end of the string. | ||
| 9862 | |||
| 9863 | |||
| 9864 | <p> | ||
| 9865 | Functions that create byte sequences | ||
| 9866 | accept all values up to <code>0x7FFFFFFF</code>, | ||
| 9867 | as defined in the original UTF-8 specification; | ||
| 9868 | that implies byte sequences of up to six bytes. | ||
| 9869 | |||
| 9870 | |||
| 9871 | <p> | ||
| 9872 | Functions that interpret byte sequences only accept | ||
| 9873 | valid sequences (well formed and not overlong). | ||
| 9874 | By default, they only accept byte sequences | ||
| 9875 | that result in valid Unicode code points, | ||
| 9876 | rejecting values greater than <code>10FFFF</code> and surrogates. | ||
| 9877 | A boolean argument <code>lax</code>, when available, | ||
| 9878 | lifts these checks, | ||
| 9879 | so that all values up to <code>0x7FFFFFFF</code> are accepted. | ||
| 9880 | (Not well formed and overlong sequences are still rejected.) | ||
| 9881 | |||
| 9882 | |||
| 9883 | <p> | ||
| 9884 | <hr><h3><a name="pdf-utf8.char"><code>utf8.char (···)</code></a></h3> | ||
| 9885 | |||
| 9886 | |||
| 9887 | <p> | ||
| 9888 | Receives zero or more integers, | ||
| 9889 | converts each one to its corresponding UTF-8 byte sequence | ||
| 9890 | and returns a string with the concatenation of all these sequences. | ||
| 9891 | |||
| 9892 | |||
| 9893 | |||
| 9894 | |||
| 9895 | <p> | ||
| 9896 | <hr><h3><a name="pdf-utf8.charpattern"><code>utf8.charpattern</code></a></h3> | ||
| 9897 | |||
| 9898 | |||
| 9899 | <p> | ||
| 9900 | The pattern (a string, not a function) "<code>[\0-\x7F\xC2-\xFD][\x80-\xBF]*</code>" | ||
| 9901 | (see <a href="#6.4.1">§6.4.1</a>), | ||
| 9902 | which matches exactly one UTF-8 byte sequence, | ||
| 9903 | assuming that the subject is a valid UTF-8 string. | ||
| 9904 | |||
| 9905 | |||
| 9906 | |||
| 9907 | |||
| 9908 | <p> | ||
| 9909 | <hr><h3><a name="pdf-utf8.codes"><code>utf8.codes (s [, lax])</code></a></h3> | ||
| 9910 | |||
| 9911 | |||
| 9912 | <p> | ||
| 9913 | Returns values so that the construction | ||
| 9914 | |||
| 9915 | <pre> | ||
| 9916 | for p, c in utf8.codes(s) do <em>body</em> end | ||
| 9917 | </pre><p> | ||
| 9918 | will iterate over all UTF-8 characters in string <code>s</code>, | ||
| 9919 | with <code>p</code> being the position (in bytes) and <code>c</code> the code point | ||
| 9920 | of each character. | ||
| 9921 | It raises an error if it meets any invalid byte sequence. | ||
| 9922 | |||
| 9923 | |||
| 9924 | |||
| 9925 | |||
| 9926 | <p> | ||
| 9927 | <hr><h3><a name="pdf-utf8.codepoint"><code>utf8.codepoint (s [, i [, j [, lax]]])</code></a></h3> | ||
| 9928 | |||
| 9929 | |||
| 9930 | <p> | ||
| 9931 | Returns the code points (as integers) from all characters in <code>s</code> | ||
| 9932 | that start between byte position <code>i</code> and <code>j</code> (both included). | ||
| 9933 | The default for <code>i</code> is 1 and for <code>j</code> is <code>i</code>. | ||
| 9934 | It raises an error if it meets any invalid byte sequence. | ||
| 9935 | |||
| 9936 | |||
| 9937 | |||
| 9938 | |||
| 9939 | <p> | ||
| 9940 | <hr><h3><a name="pdf-utf8.len"><code>utf8.len (s [, i [, j [, lax]]])</code></a></h3> | ||
| 9941 | |||
| 9942 | |||
| 9943 | <p> | ||
| 9944 | Returns the number of UTF-8 characters in string <code>s</code> | ||
| 9945 | that start between positions <code>i</code> and <code>j</code> (both inclusive). | ||
| 9946 | The default for <code>i</code> is 1 and for <code>j</code> is -1. | ||
| 9947 | If it finds any invalid byte sequence, | ||
| 9948 | returns <b>fail</b> plus the position of the first invalid byte. | ||
| 9949 | |||
| 9950 | |||
| 9951 | |||
| 9952 | |||
| 9953 | <p> | ||
| 9954 | <hr><h3><a name="pdf-utf8.offset"><code>utf8.offset (s, n [, i])</code></a></h3> | ||
| 9955 | |||
| 9956 | |||
| 9957 | <p> | ||
| 9958 | Returns the position (in bytes) where the encoding of the | ||
| 9959 | <code>n</code>-th character of <code>s</code> | ||
| 9960 | (counting from position <code>i</code>) starts. | ||
| 9961 | A negative <code>n</code> gets characters before position <code>i</code>. | ||
| 9962 | The default for <code>i</code> is 1 when <code>n</code> is non-negative | ||
| 9963 | and <code>#s + 1</code> otherwise, | ||
| 9964 | so that <code>utf8.offset(s, -n)</code> gets the offset of the | ||
| 9965 | <code>n</code>-th character from the end of the string. | ||
| 9966 | If the specified character is neither in the subject | ||
| 9967 | nor right after its end, | ||
| 9968 | the function returns <b>fail</b>. | ||
| 9969 | |||
| 9970 | |||
| 9971 | <p> | ||
| 9972 | As a special case, | ||
| 9973 | when <code>n</code> is 0 the function returns the start of the encoding | ||
| 9974 | of the character that contains the <code>i</code>-th byte of <code>s</code>. | ||
| 9975 | |||
| 9976 | |||
| 9977 | <p> | ||
| 9978 | This function assumes that <code>s</code> is a valid UTF-8 string. | ||
| 9979 | |||
| 9980 | |||
| 9981 | |||
| 9982 | |||
| 9983 | |||
| 9984 | |||
| 9985 | |||
| 9986 | <h2>6.6 – <a name="6.6">Table Manipulation</a></h2> | ||
| 9987 | |||
| 9988 | <p> | ||
| 9989 | This library provides generic functions for table manipulation. | ||
| 9990 | It provides all its functions inside the table <a name="pdf-table"><code>table</code></a>. | ||
| 9991 | |||
| 9992 | |||
| 9993 | <p> | ||
| 9994 | Remember that, whenever an operation needs the length of a table, | ||
| 9995 | all caveats about the length operator apply (see <a href="#3.4.7">§3.4.7</a>). | ||
| 9996 | All functions ignore non-numeric keys | ||
| 9997 | in the tables given as arguments. | ||
| 9998 | |||
| 9999 | |||
| 10000 | <p> | ||
| 10001 | <hr><h3><a name="pdf-table.concat"><code>table.concat (list [, sep [, i [, j]]])</code></a></h3> | ||
| 10002 | |||
| 10003 | |||
| 10004 | <p> | ||
| 10005 | Given a list where all elements are strings or numbers, | ||
| 10006 | returns the string <code>list[i]..sep..list[i+1] ··· sep..list[j]</code>. | ||
| 10007 | The default value for <code>sep</code> is the empty string, | ||
| 10008 | the default for <code>i</code> is 1, | ||
| 10009 | and the default for <code>j</code> is <code>#list</code>. | ||
| 10010 | If <code>i</code> is greater than <code>j</code>, returns the empty string. | ||
| 10011 | |||
| 10012 | |||
| 10013 | |||
| 10014 | |||
| 10015 | <p> | ||
| 10016 | <hr><h3><a name="pdf-table.insert"><code>table.insert (list, [pos,] value)</code></a></h3> | ||
| 10017 | |||
| 10018 | |||
| 10019 | <p> | ||
| 10020 | Inserts element <code>value</code> at position <code>pos</code> in <code>list</code>, | ||
| 10021 | shifting up the elements | ||
| 10022 | <code>list[pos], list[pos+1], ···, list[#list]</code>. | ||
| 10023 | The default value for <code>pos</code> is <code>#list+1</code>, | ||
| 10024 | so that a call <code>table.insert(t,x)</code> inserts <code>x</code> at the end | ||
| 10025 | of the list <code>t</code>. | ||
| 10026 | |||
| 10027 | |||
| 10028 | |||
| 10029 | |||
| 10030 | <p> | ||
| 10031 | <hr><h3><a name="pdf-table.move"><code>table.move (a1, f, e, t [,a2])</code></a></h3> | ||
| 10032 | |||
| 10033 | |||
| 10034 | <p> | ||
| 10035 | Moves elements from the table <code>a1</code> to the table <code>a2</code>, | ||
| 10036 | performing the equivalent to the following | ||
| 10037 | multiple assignment: | ||
| 10038 | <code>a2[t],··· = a1[f],···,a1[e]</code>. | ||
| 10039 | The default for <code>a2</code> is <code>a1</code>. | ||
| 10040 | The destination range can overlap with the source range. | ||
| 10041 | The number of elements to be moved must fit in a Lua integer. | ||
| 10042 | |||
| 10043 | |||
| 10044 | <p> | ||
| 10045 | Returns the destination table <code>a2</code>. | ||
| 10046 | |||
| 10047 | |||
| 10048 | |||
| 10049 | |||
| 10050 | <p> | ||
| 10051 | <hr><h3><a name="pdf-table.pack"><code>table.pack (···)</code></a></h3> | ||
| 10052 | |||
| 10053 | |||
| 10054 | <p> | ||
| 10055 | Returns a new table with all arguments stored into keys 1, 2, etc. | ||
| 10056 | and with a field "<code>n</code>" with the total number of arguments. | ||
| 10057 | Note that the resulting table may not be a sequence, | ||
| 10058 | if some arguments are <b>nil</b>. | ||
| 10059 | |||
| 10060 | |||
| 10061 | |||
| 10062 | |||
| 10063 | <p> | ||
| 10064 | <hr><h3><a name="pdf-table.remove"><code>table.remove (list [, pos])</code></a></h3> | ||
| 10065 | |||
| 10066 | |||
| 10067 | <p> | ||
| 10068 | Removes from <code>list</code> the element at position <code>pos</code>, | ||
| 10069 | returning the value of the removed element. | ||
| 10070 | When <code>pos</code> is an integer between 1 and <code>#list</code>, | ||
| 10071 | it shifts down the elements | ||
| 10072 | <code>list[pos+1], list[pos+2], ···, list[#list]</code> | ||
| 10073 | and erases element <code>list[#list]</code>; | ||
| 10074 | The index <code>pos</code> can also be 0 when <code>#list</code> is 0, | ||
| 10075 | or <code>#list + 1</code>. | ||
| 10076 | |||
| 10077 | |||
| 10078 | <p> | ||
| 10079 | The default value for <code>pos</code> is <code>#list</code>, | ||
| 10080 | so that a call <code>table.remove(l)</code> removes the last element | ||
| 10081 | of the list <code>l</code>. | ||
| 10082 | |||
| 10083 | |||
| 10084 | |||
| 10085 | |||
| 10086 | <p> | ||
| 10087 | <hr><h3><a name="pdf-table.sort"><code>table.sort (list [, comp])</code></a></h3> | ||
| 10088 | |||
| 10089 | |||
| 10090 | <p> | ||
| 10091 | Sorts the list elements in a given order, <em>in-place</em>, | ||
| 10092 | from <code>list[1]</code> to <code>list[#list]</code>. | ||
| 10093 | If <code>comp</code> is given, | ||
| 10094 | then it must be a function that receives two list elements | ||
| 10095 | and returns true when the first element must come | ||
| 10096 | before the second in the final order, | ||
| 10097 | so that, after the sort, | ||
| 10098 | <code>i <= j</code> implies <code>not comp(list[j],list[i])</code>. | ||
| 10099 | If <code>comp</code> is not given, | ||
| 10100 | then the standard Lua operator <code><</code> is used instead. | ||
| 10101 | |||
| 10102 | |||
| 10103 | <p> | ||
| 10104 | The <code>comp</code> function must define a consistent order; | ||
| 10105 | more formally, the function must define a strict weak order. | ||
| 10106 | (A weak order is similar to a total order, | ||
| 10107 | but it can equate different elements for comparison purposes.) | ||
| 10108 | |||
| 10109 | |||
| 10110 | <p> | ||
| 10111 | The sort algorithm is not stable: | ||
| 10112 | Different elements considered equal by the given order | ||
| 10113 | may have their relative positions changed by the sort. | ||
| 10114 | |||
| 10115 | |||
| 10116 | |||
| 10117 | |||
| 10118 | <p> | ||
| 10119 | <hr><h3><a name="pdf-table.unpack"><code>table.unpack (list [, i [, j]])</code></a></h3> | ||
| 10120 | |||
| 10121 | |||
| 10122 | <p> | ||
| 10123 | Returns the elements from the given list. | ||
| 10124 | This function is equivalent to | ||
| 10125 | |||
| 10126 | <pre> | ||
| 10127 | return list[i], list[i+1], ···, list[j] | ||
| 10128 | </pre><p> | ||
| 10129 | By default, <code>i</code> is 1 and <code>j</code> is <code>#list</code>. | ||
| 10130 | |||
| 10131 | |||
| 10132 | |||
| 10133 | |||
| 10134 | |||
| 10135 | |||
| 10136 | |||
| 10137 | <h2>6.7 – <a name="6.7">Mathematical Functions</a></h2> | ||
| 10138 | |||
| 10139 | <p> | ||
| 10140 | This library provides basic mathematical functions. | ||
| 10141 | It provides all its functions and constants inside the table <a name="pdf-math"><code>math</code></a>. | ||
| 10142 | Functions with the annotation "<code>integer/float</code>" give | ||
| 10143 | integer results for integer arguments | ||
| 10144 | and float results for non-integer arguments. | ||
| 10145 | The rounding functions | ||
| 10146 | <a href="#pdf-math.ceil"><code>math.ceil</code></a>, <a href="#pdf-math.floor"><code>math.floor</code></a>, and <a href="#pdf-math.modf"><code>math.modf</code></a> | ||
| 10147 | return an integer when the result fits in the range of an integer, | ||
| 10148 | or a float otherwise. | ||
| 10149 | |||
| 10150 | |||
| 10151 | <p> | ||
| 10152 | <hr><h3><a name="pdf-math.abs"><code>math.abs (x)</code></a></h3> | ||
| 10153 | |||
| 10154 | |||
| 10155 | <p> | ||
| 10156 | Returns the maximum value between <code>x</code> and <code>-x</code>. (integer/float) | ||
| 10157 | |||
| 10158 | |||
| 10159 | |||
| 10160 | |||
| 10161 | <p> | ||
| 10162 | <hr><h3><a name="pdf-math.acos"><code>math.acos (x)</code></a></h3> | ||
| 10163 | |||
| 10164 | |||
| 10165 | <p> | ||
| 10166 | Returns the arc cosine of <code>x</code> (in radians). | ||
| 10167 | |||
| 10168 | |||
| 10169 | |||
| 10170 | |||
| 10171 | <p> | ||
| 10172 | <hr><h3><a name="pdf-math.asin"><code>math.asin (x)</code></a></h3> | ||
| 10173 | |||
| 10174 | |||
| 10175 | <p> | ||
| 10176 | Returns the arc sine of <code>x</code> (in radians). | ||
| 10177 | |||
| 10178 | |||
| 10179 | |||
| 10180 | |||
| 10181 | <p> | ||
| 10182 | <hr><h3><a name="pdf-math.atan"><code>math.atan (y [, x])</code></a></h3> | ||
| 10183 | |||
| 10184 | |||
| 10185 | <p> | ||
| 10186 | |||
| 10187 | Returns the arc tangent of <code>y/x</code> (in radians), | ||
| 10188 | using the signs of both arguments to find the | ||
| 10189 | quadrant of the result. | ||
| 10190 | It also handles correctly the case of <code>x</code> being zero. | ||
| 10191 | |||
| 10192 | |||
| 10193 | <p> | ||
| 10194 | The default value for <code>x</code> is 1, | ||
| 10195 | so that the call <code>math.atan(y)</code> | ||
| 10196 | returns the arc tangent of <code>y</code>. | ||
| 10197 | |||
| 10198 | |||
| 10199 | |||
| 10200 | |||
| 10201 | <p> | ||
| 10202 | <hr><h3><a name="pdf-math.ceil"><code>math.ceil (x)</code></a></h3> | ||
| 10203 | |||
| 10204 | |||
| 10205 | <p> | ||
| 10206 | Returns the smallest integral value greater than or equal to <code>x</code>. | ||
| 10207 | |||
| 10208 | |||
| 10209 | |||
| 10210 | |||
| 10211 | <p> | ||
| 10212 | <hr><h3><a name="pdf-math.cos"><code>math.cos (x)</code></a></h3> | ||
| 10213 | |||
| 10214 | |||
| 10215 | <p> | ||
| 10216 | Returns the cosine of <code>x</code> (assumed to be in radians). | ||
| 10217 | |||
| 10218 | |||
| 10219 | |||
| 10220 | |||
| 10221 | <p> | ||
| 10222 | <hr><h3><a name="pdf-math.deg"><code>math.deg (x)</code></a></h3> | ||
| 10223 | |||
| 10224 | |||
| 10225 | <p> | ||
| 10226 | Converts the angle <code>x</code> from radians to degrees. | ||
| 10227 | |||
| 10228 | |||
| 10229 | |||
| 10230 | |||
| 10231 | <p> | ||
| 10232 | <hr><h3><a name="pdf-math.exp"><code>math.exp (x)</code></a></h3> | ||
| 10233 | |||
| 10234 | |||
| 10235 | <p> | ||
| 10236 | Returns the value <em>e<sup>x</sup></em> | ||
| 10237 | (where <code>e</code> is the base of natural logarithms). | ||
| 10238 | |||
| 10239 | |||
| 10240 | |||
| 10241 | |||
| 10242 | <p> | ||
| 10243 | <hr><h3><a name="pdf-math.floor"><code>math.floor (x)</code></a></h3> | ||
| 10244 | |||
| 10245 | |||
| 10246 | <p> | ||
| 10247 | Returns the largest integral value less than or equal to <code>x</code>. | ||
| 10248 | |||
| 10249 | |||
| 10250 | |||
| 10251 | |||
| 10252 | <p> | ||
| 10253 | <hr><h3><a name="pdf-math.fmod"><code>math.fmod (x, y)</code></a></h3> | ||
| 10254 | |||
| 10255 | |||
| 10256 | <p> | ||
| 10257 | Returns the remainder of the division of <code>x</code> by <code>y</code> | ||
| 10258 | that rounds the quotient towards zero. (integer/float) | ||
| 10259 | |||
| 10260 | |||
| 10261 | |||
| 10262 | |||
| 10263 | <p> | ||
| 10264 | <hr><h3><a name="pdf-math.huge"><code>math.huge</code></a></h3> | ||
| 10265 | |||
| 10266 | |||
| 10267 | <p> | ||
| 10268 | The float value <code>HUGE_VAL</code>, | ||
| 10269 | a value greater than any other numeric value. | ||
| 10270 | |||
| 10271 | |||
| 10272 | |||
| 10273 | |||
| 10274 | <p> | ||
| 10275 | <hr><h3><a name="pdf-math.log"><code>math.log (x [, base])</code></a></h3> | ||
| 10276 | |||
| 10277 | |||
| 10278 | <p> | ||
| 10279 | Returns the logarithm of <code>x</code> in the given base. | ||
| 10280 | The default for <code>base</code> is <em>e</em> | ||
| 10281 | (so that the function returns the natural logarithm of <code>x</code>). | ||
| 10282 | |||
| 10283 | |||
| 10284 | |||
| 10285 | |||
| 10286 | <p> | ||
| 10287 | <hr><h3><a name="pdf-math.max"><code>math.max (x, ···)</code></a></h3> | ||
| 10288 | |||
| 10289 | |||
| 10290 | <p> | ||
| 10291 | Returns the argument with the maximum value, | ||
| 10292 | according to the Lua operator <code><</code>. | ||
| 10293 | |||
| 10294 | |||
| 10295 | |||
| 10296 | |||
| 10297 | <p> | ||
| 10298 | <hr><h3><a name="pdf-math.maxinteger"><code>math.maxinteger</code></a></h3> | ||
| 10299 | An integer with the maximum value for an integer. | ||
| 10300 | |||
| 10301 | |||
| 10302 | |||
| 10303 | |||
| 10304 | <p> | ||
| 10305 | <hr><h3><a name="pdf-math.min"><code>math.min (x, ···)</code></a></h3> | ||
| 10306 | |||
| 10307 | |||
| 10308 | <p> | ||
| 10309 | Returns the argument with the minimum value, | ||
| 10310 | according to the Lua operator <code><</code>. | ||
| 10311 | |||
| 10312 | |||
| 10313 | |||
| 10314 | |||
| 10315 | <p> | ||
| 10316 | <hr><h3><a name="pdf-math.mininteger"><code>math.mininteger</code></a></h3> | ||
| 10317 | An integer with the minimum value for an integer. | ||
| 10318 | |||
| 10319 | |||
| 10320 | |||
| 10321 | |||
| 10322 | <p> | ||
| 10323 | <hr><h3><a name="pdf-math.modf"><code>math.modf (x)</code></a></h3> | ||
| 10324 | |||
| 10325 | |||
| 10326 | <p> | ||
| 10327 | Returns the integral part of <code>x</code> and the fractional part of <code>x</code>. | ||
| 10328 | Its second result is always a float. | ||
| 10329 | |||
| 10330 | |||
| 10331 | |||
| 10332 | |||
| 10333 | <p> | ||
| 10334 | <hr><h3><a name="pdf-math.pi"><code>math.pi</code></a></h3> | ||
| 10335 | |||
| 10336 | |||
| 10337 | <p> | ||
| 10338 | The value of <em>π</em>. | ||
| 10339 | |||
| 10340 | |||
| 10341 | |||
| 10342 | |||
| 10343 | <p> | ||
| 10344 | <hr><h3><a name="pdf-math.rad"><code>math.rad (x)</code></a></h3> | ||
| 10345 | |||
| 10346 | |||
| 10347 | <p> | ||
| 10348 | Converts the angle <code>x</code> from degrees to radians. | ||
| 10349 | |||
| 10350 | |||
| 10351 | |||
| 10352 | |||
| 10353 | <p> | ||
| 10354 | <hr><h3><a name="pdf-math.random"><code>math.random ([m [, n]])</code></a></h3> | ||
| 10355 | |||
| 10356 | |||
| 10357 | <p> | ||
| 10358 | When called without arguments, | ||
| 10359 | returns a pseudo-random float with uniform distribution | ||
| 10360 | in the range <em>[0,1)</em>. | ||
| 10361 | When called with two integers <code>m</code> and <code>n</code>, | ||
| 10362 | <code>math.random</code> returns a pseudo-random integer | ||
| 10363 | with uniform distribution in the range <em>[m, n]</em>. | ||
| 10364 | The call <code>math.random(n)</code>, for a positive <code>n</code>, | ||
| 10365 | is equivalent to <code>math.random(1,n)</code>. | ||
| 10366 | The call <code>math.random(0)</code> produces an integer with | ||
| 10367 | all bits (pseudo)random. | ||
| 10368 | |||
| 10369 | |||
| 10370 | <p> | ||
| 10371 | This function uses the <code>xoshiro256**</code> algorithm to produce | ||
| 10372 | pseudo-random 64-bit integers, | ||
| 10373 | which are the results of calls with argument 0. | ||
| 10374 | Other results (ranges and floats) | ||
| 10375 | are unbiased extracted from these integers. | ||
| 10376 | |||
| 10377 | |||
| 10378 | <p> | ||
| 10379 | Lua initializes its pseudo-random generator with the equivalent of | ||
| 10380 | a call to <a href="#pdf-math.randomseed"><code>math.randomseed</code></a> with no arguments, | ||
| 10381 | so that <code>math.random</code> should generate | ||
| 10382 | different sequences of results each time the program runs. | ||
| 10383 | |||
| 10384 | |||
| 10385 | |||
| 10386 | |||
| 10387 | <p> | ||
| 10388 | <hr><h3><a name="pdf-math.randomseed"><code>math.randomseed ([x [, y]])</code></a></h3> | ||
| 10389 | |||
| 10390 | |||
| 10391 | <p> | ||
| 10392 | When called with at least one argument, | ||
| 10393 | the integer parameters <code>x</code> and <code>y</code> are | ||
| 10394 | joined into a 128-bit <em>seed</em> that | ||
| 10395 | is used to reinitialize the pseudo-random generator; | ||
| 10396 | equal seeds produce equal sequences of numbers. | ||
| 10397 | The default for <code>y</code> is zero. | ||
| 10398 | |||
| 10399 | |||
| 10400 | <p> | ||
| 10401 | When called with no arguments, | ||
| 10402 | Lua generates a seed with | ||
| 10403 | a weak attempt for randomness. | ||
| 10404 | |||
| 10405 | |||
| 10406 | <p> | ||
| 10407 | This function returns the two seed components | ||
| 10408 | that were effectively used, | ||
| 10409 | so that setting them again repeats the sequence. | ||
| 10410 | |||
| 10411 | |||
| 10412 | <p> | ||
| 10413 | To ensure a required level of randomness to the initial state | ||
| 10414 | (or contrarily, to have a deterministic sequence, | ||
| 10415 | for instance when debugging a program), | ||
| 10416 | you should call <a href="#pdf-math.randomseed"><code>math.randomseed</code></a> with explicit arguments. | ||
| 10417 | |||
| 10418 | |||
| 10419 | |||
| 10420 | |||
| 10421 | <p> | ||
| 10422 | <hr><h3><a name="pdf-math.sin"><code>math.sin (x)</code></a></h3> | ||
| 10423 | |||
| 10424 | |||
| 10425 | <p> | ||
| 10426 | Returns the sine of <code>x</code> (assumed to be in radians). | ||
| 10427 | |||
| 10428 | |||
| 10429 | |||
| 10430 | |||
| 10431 | <p> | ||
| 10432 | <hr><h3><a name="pdf-math.sqrt"><code>math.sqrt (x)</code></a></h3> | ||
| 10433 | |||
| 10434 | |||
| 10435 | <p> | ||
| 10436 | Returns the square root of <code>x</code>. | ||
| 10437 | (You can also use the expression <code>x^0.5</code> to compute this value.) | ||
| 10438 | |||
| 10439 | |||
| 10440 | |||
| 10441 | |||
| 10442 | <p> | ||
| 10443 | <hr><h3><a name="pdf-math.tan"><code>math.tan (x)</code></a></h3> | ||
| 10444 | |||
| 10445 | |||
| 10446 | <p> | ||
| 10447 | Returns the tangent of <code>x</code> (assumed to be in radians). | ||
| 10448 | |||
| 10449 | |||
| 10450 | |||
| 10451 | |||
| 10452 | <p> | ||
| 10453 | <hr><h3><a name="pdf-math.tointeger"><code>math.tointeger (x)</code></a></h3> | ||
| 10454 | |||
| 10455 | |||
| 10456 | <p> | ||
| 10457 | If the value <code>x</code> is convertible to an integer, | ||
| 10458 | returns that integer. | ||
| 10459 | Otherwise, returns <b>fail</b>. | ||
| 10460 | |||
| 10461 | |||
| 10462 | |||
| 10463 | |||
| 10464 | <p> | ||
| 10465 | <hr><h3><a name="pdf-math.type"><code>math.type (x)</code></a></h3> | ||
| 10466 | |||
| 10467 | |||
| 10468 | <p> | ||
| 10469 | Returns "<code>integer</code>" if <code>x</code> is an integer, | ||
| 10470 | "<code>float</code>" if it is a float, | ||
| 10471 | or <b>fail</b> if <code>x</code> is not a number. | ||
| 10472 | |||
| 10473 | |||
| 10474 | |||
| 10475 | |||
| 10476 | <p> | ||
| 10477 | <hr><h3><a name="pdf-math.ult"><code>math.ult (m, n)</code></a></h3> | ||
| 10478 | |||
| 10479 | |||
| 10480 | <p> | ||
| 10481 | Returns a boolean, | ||
| 10482 | <b>true</b> if and only if integer <code>m</code> is below integer <code>n</code> when | ||
| 10483 | they are compared as unsigned integers. | ||
| 10484 | |||
| 10485 | |||
| 10486 | |||
| 10487 | |||
| 10488 | |||
| 10489 | |||
| 10490 | |||
| 10491 | <h2>6.8 – <a name="6.8">Input and Output Facilities</a></h2> | ||
| 10492 | |||
| 10493 | <p> | ||
| 10494 | The I/O library provides two different styles for file manipulation. | ||
| 10495 | The first one uses implicit file handles; | ||
| 10496 | that is, there are operations to set a default input file and a | ||
| 10497 | default output file, | ||
| 10498 | and all input/output operations are done over these default files. | ||
| 10499 | The second style uses explicit file handles. | ||
| 10500 | |||
| 10501 | |||
| 10502 | <p> | ||
| 10503 | When using implicit file handles, | ||
| 10504 | all operations are supplied by table <a name="pdf-io"><code>io</code></a>. | ||
| 10505 | When using explicit file handles, | ||
| 10506 | the operation <a href="#pdf-io.open"><code>io.open</code></a> returns a file handle | ||
| 10507 | and then all operations are supplied as methods of the file handle. | ||
| 10508 | |||
| 10509 | |||
| 10510 | <p> | ||
| 10511 | The metatable for file handles provides metamethods | ||
| 10512 | for <code>__gc</code> and <code>__close</code> that try | ||
| 10513 | to close the file when called. | ||
| 10514 | |||
| 10515 | |||
| 10516 | <p> | ||
| 10517 | The table <code>io</code> also provides | ||
| 10518 | three predefined file handles with their usual meanings from C: | ||
| 10519 | <a name="pdf-io.stdin"><code>io.stdin</code></a>, <a name="pdf-io.stdout"><code>io.stdout</code></a>, and <a name="pdf-io.stderr"><code>io.stderr</code></a>. | ||
| 10520 | The I/O library never closes these files. | ||
| 10521 | |||
| 10522 | |||
| 10523 | <p> | ||
| 10524 | Unless otherwise stated, | ||
| 10525 | all I/O functions return <b>fail</b> on failure, | ||
| 10526 | plus an error message as a second result and | ||
| 10527 | a system-dependent error code as a third result, | ||
| 10528 | and some non-false value on success. | ||
| 10529 | On non-POSIX systems, | ||
| 10530 | the computation of the error message and error code | ||
| 10531 | in case of errors | ||
| 10532 | may be not thread safe, | ||
| 10533 | because they rely on the global C variable <code>errno</code>. | ||
| 10534 | |||
| 10535 | |||
| 10536 | <p> | ||
| 10537 | <hr><h3><a name="pdf-io.close"><code>io.close ([file])</code></a></h3> | ||
| 10538 | |||
| 10539 | |||
| 10540 | <p> | ||
| 10541 | Equivalent to <code>file:close()</code>. | ||
| 10542 | Without a <code>file</code>, closes the default output file. | ||
| 10543 | |||
| 10544 | |||
| 10545 | |||
| 10546 | |||
| 10547 | <p> | ||
| 10548 | <hr><h3><a name="pdf-io.flush"><code>io.flush ()</code></a></h3> | ||
| 10549 | |||
| 10550 | |||
| 10551 | <p> | ||
| 10552 | Equivalent to <code>io.output():flush()</code>. | ||
| 10553 | |||
| 10554 | |||
| 10555 | |||
| 10556 | |||
| 10557 | <p> | ||
| 10558 | <hr><h3><a name="pdf-io.input"><code>io.input ([file])</code></a></h3> | ||
| 10559 | |||
| 10560 | |||
| 10561 | <p> | ||
| 10562 | When called with a file name, it opens the named file (in text mode), | ||
| 10563 | and sets its handle as the default input file. | ||
| 10564 | When called with a file handle, | ||
| 10565 | it simply sets this file handle as the default input file. | ||
| 10566 | When called without arguments, | ||
| 10567 | it returns the current default input file. | ||
| 10568 | |||
| 10569 | |||
| 10570 | <p> | ||
| 10571 | In case of errors this function raises the error, | ||
| 10572 | instead of returning an error code. | ||
| 10573 | |||
| 10574 | |||
| 10575 | |||
| 10576 | |||
| 10577 | <p> | ||
| 10578 | <hr><h3><a name="pdf-io.lines"><code>io.lines ([filename, ···])</code></a></h3> | ||
| 10579 | |||
| 10580 | |||
| 10581 | <p> | ||
| 10582 | Opens the given file name in read mode | ||
| 10583 | and returns an iterator function that | ||
| 10584 | works like <code>file:lines(···)</code> over the opened file. | ||
| 10585 | When the iterator function fails to read any value, | ||
| 10586 | it automatically closes the file. | ||
| 10587 | Besides the iterator function, | ||
| 10588 | <code>io.lines</code> returns three other values: | ||
| 10589 | two <b>nil</b> values as placeholders, | ||
| 10590 | plus the created file handle. | ||
| 10591 | Therefore, when used in a generic <b>for</b> loop, | ||
| 10592 | the file is closed also if the loop is interrupted by an | ||
| 10593 | error or a <b>break</b>. | ||
| 10594 | |||
| 10595 | |||
| 10596 | <p> | ||
| 10597 | The call <code>io.lines()</code> (with no file name) is equivalent | ||
| 10598 | to <code>io.input():lines("l")</code>; | ||
| 10599 | that is, it iterates over the lines of the default input file. | ||
| 10600 | In this case, the iterator does not close the file when the loop ends. | ||
| 10601 | |||
| 10602 | |||
| 10603 | <p> | ||
| 10604 | In case of errors opening the file, | ||
| 10605 | this function raises the error, | ||
| 10606 | instead of returning an error code. | ||
| 10607 | |||
| 10608 | |||
| 10609 | |||
| 10610 | |||
| 10611 | <p> | ||
| 10612 | <hr><h3><a name="pdf-io.open"><code>io.open (filename [, mode])</code></a></h3> | ||
| 10613 | |||
| 10614 | |||
| 10615 | <p> | ||
| 10616 | This function opens a file, | ||
| 10617 | in the mode specified in the string <code>mode</code>. | ||
| 10618 | In case of success, | ||
| 10619 | it returns a new file handle. | ||
| 10620 | |||
| 10621 | |||
| 10622 | <p> | ||
| 10623 | The <code>mode</code> string can be any of the following: | ||
| 10624 | |||
| 10625 | <ul> | ||
| 10626 | <li><b>"<code>r</code>": </b> read mode (the default);</li> | ||
| 10627 | <li><b>"<code>w</code>": </b> write mode;</li> | ||
| 10628 | <li><b>"<code>a</code>": </b> append mode;</li> | ||
| 10629 | <li><b>"<code>r+</code>": </b> update mode, all previous data is preserved;</li> | ||
| 10630 | <li><b>"<code>w+</code>": </b> update mode, all previous data is erased;</li> | ||
| 10631 | <li><b>"<code>a+</code>": </b> append update mode, previous data is preserved, | ||
| 10632 | writing is only allowed at the end of file.</li> | ||
| 10633 | </ul><p> | ||
| 10634 | The <code>mode</code> string can also have a '<code>b</code>' at the end, | ||
| 10635 | which is needed in some systems to open the file in binary mode. | ||
| 10636 | |||
| 10637 | |||
| 10638 | |||
| 10639 | |||
| 10640 | <p> | ||
| 10641 | <hr><h3><a name="pdf-io.output"><code>io.output ([file])</code></a></h3> | ||
| 10642 | |||
| 10643 | |||
| 10644 | <p> | ||
| 10645 | Similar to <a href="#pdf-io.input"><code>io.input</code></a>, but operates over the default output file. | ||
| 10646 | |||
| 10647 | |||
| 10648 | |||
| 10649 | |||
| 10650 | <p> | ||
| 10651 | <hr><h3><a name="pdf-io.popen"><code>io.popen (prog [, mode])</code></a></h3> | ||
| 10652 | |||
| 10653 | |||
| 10654 | <p> | ||
| 10655 | This function is system dependent and is not available | ||
| 10656 | on all platforms. | ||
| 10657 | |||
| 10658 | |||
| 10659 | <p> | ||
| 10660 | Starts the program <code>prog</code> in a separated process and returns | ||
| 10661 | a file handle that you can use to read data from this program | ||
| 10662 | (if <code>mode</code> is <code>"r"</code>, the default) | ||
| 10663 | or to write data to this program | ||
| 10664 | (if <code>mode</code> is <code>"w"</code>). | ||
| 10665 | |||
| 10666 | |||
| 10667 | |||
| 10668 | |||
| 10669 | <p> | ||
| 10670 | <hr><h3><a name="pdf-io.read"><code>io.read (···)</code></a></h3> | ||
| 10671 | |||
| 10672 | |||
| 10673 | <p> | ||
| 10674 | Equivalent to <code>io.input():read(···)</code>. | ||
| 10675 | |||
| 10676 | |||
| 10677 | |||
| 10678 | |||
| 10679 | <p> | ||
| 10680 | <hr><h3><a name="pdf-io.tmpfile"><code>io.tmpfile ()</code></a></h3> | ||
| 10681 | |||
| 10682 | |||
| 10683 | <p> | ||
| 10684 | In case of success, | ||
| 10685 | returns a handle for a temporary file. | ||
| 10686 | This file is opened in update mode | ||
| 10687 | and it is automatically removed when the program ends. | ||
| 10688 | |||
| 10689 | |||
| 10690 | |||
| 10691 | |||
| 10692 | <p> | ||
| 10693 | <hr><h3><a name="pdf-io.type"><code>io.type (obj)</code></a></h3> | ||
| 10694 | |||
| 10695 | |||
| 10696 | <p> | ||
| 10697 | Checks whether <code>obj</code> is a valid file handle. | ||
| 10698 | Returns the string <code>"file"</code> if <code>obj</code> is an open file handle, | ||
| 10699 | <code>"closed file"</code> if <code>obj</code> is a closed file handle, | ||
| 10700 | or <b>fail</b> if <code>obj</code> is not a file handle. | ||
| 10701 | |||
| 10702 | |||
| 10703 | |||
| 10704 | |||
| 10705 | <p> | ||
| 10706 | <hr><h3><a name="pdf-io.write"><code>io.write (···)</code></a></h3> | ||
| 10707 | |||
| 10708 | |||
| 10709 | <p> | ||
| 10710 | Equivalent to <code>io.output():write(···)</code>. | ||
| 10711 | |||
| 10712 | |||
| 10713 | |||
| 10714 | |||
| 10715 | <p> | ||
| 10716 | <hr><h3><a name="pdf-file:close"><code>file:close ()</code></a></h3> | ||
| 10717 | |||
| 10718 | |||
| 10719 | <p> | ||
| 10720 | Closes <code>file</code>. | ||
| 10721 | Note that files are automatically closed when | ||
| 10722 | their handles are garbage collected, | ||
| 10723 | but that takes an unpredictable amount of time to happen. | ||
| 10724 | |||
| 10725 | |||
| 10726 | <p> | ||
| 10727 | When closing a file handle created with <a href="#pdf-io.popen"><code>io.popen</code></a>, | ||
| 10728 | <a href="#pdf-file:close"><code>file:close</code></a> returns the same values | ||
| 10729 | returned by <a href="#pdf-os.execute"><code>os.execute</code></a>. | ||
| 10730 | |||
| 10731 | |||
| 10732 | |||
| 10733 | |||
| 10734 | <p> | ||
| 10735 | <hr><h3><a name="pdf-file:flush"><code>file:flush ()</code></a></h3> | ||
| 10736 | |||
| 10737 | |||
| 10738 | <p> | ||
| 10739 | Saves any written data to <code>file</code>. | ||
| 10740 | |||
| 10741 | |||
| 10742 | |||
| 10743 | |||
| 10744 | <p> | ||
| 10745 | <hr><h3><a name="pdf-file:lines"><code>file:lines (···)</code></a></h3> | ||
| 10746 | |||
| 10747 | |||
| 10748 | <p> | ||
| 10749 | Returns an iterator function that, | ||
| 10750 | each time it is called, | ||
| 10751 | reads the file according to the given formats. | ||
| 10752 | When no format is given, | ||
| 10753 | uses "<code>l</code>" as a default. | ||
| 10754 | As an example, the construction | ||
| 10755 | |||
| 10756 | <pre> | ||
| 10757 | for c in file:lines(1) do <em>body</em> end | ||
| 10758 | </pre><p> | ||
| 10759 | will iterate over all characters of the file, | ||
| 10760 | starting at the current position. | ||
| 10761 | Unlike <a href="#pdf-io.lines"><code>io.lines</code></a>, this function does not close the file | ||
| 10762 | when the loop ends. | ||
| 10763 | |||
| 10764 | |||
| 10765 | |||
| 10766 | |||
| 10767 | <p> | ||
| 10768 | <hr><h3><a name="pdf-file:read"><code>file:read (···)</code></a></h3> | ||
| 10769 | |||
| 10770 | |||
| 10771 | <p> | ||
| 10772 | Reads the file <code>file</code>, | ||
| 10773 | according to the given formats, which specify what to read. | ||
| 10774 | For each format, | ||
| 10775 | the function returns a string or a number with the characters read, | ||
| 10776 | or <b>fail</b> if it cannot read data with the specified format. | ||
| 10777 | (In this latter case, | ||
| 10778 | the function does not read subsequent formats.) | ||
| 10779 | When called without arguments, | ||
| 10780 | it uses a default format that reads the next line | ||
| 10781 | (see below). | ||
| 10782 | |||
| 10783 | |||
| 10784 | <p> | ||
| 10785 | The available formats are | ||
| 10786 | |||
| 10787 | <ul> | ||
| 10788 | |||
| 10789 | <li><b>"<code>n</code>": </b> | ||
| 10790 | reads a numeral and returns it as a float or an integer, | ||
| 10791 | following the lexical conventions of Lua. | ||
| 10792 | (The numeral may have leading whitespaces and a sign.) | ||
| 10793 | This format always reads the longest input sequence that | ||
| 10794 | is a valid prefix for a numeral; | ||
| 10795 | if that prefix does not form a valid numeral | ||
| 10796 | (e.g., an empty string, "<code>0x</code>", or "<code>3.4e-</code>") | ||
| 10797 | or it is too long (more than 200 characters), | ||
| 10798 | it is discarded and the format returns <b>fail</b>. | ||
| 10799 | </li> | ||
| 10800 | |||
| 10801 | <li><b>"<code>a</code>": </b> | ||
| 10802 | reads the whole file, starting at the current position. | ||
| 10803 | On end of file, it returns the empty string; | ||
| 10804 | this format never fails. | ||
| 10805 | </li> | ||
| 10806 | |||
| 10807 | <li><b>"<code>l</code>": </b> | ||
| 10808 | reads the next line skipping the end of line, | ||
| 10809 | returning <b>fail</b> on end of file. | ||
| 10810 | This is the default format. | ||
| 10811 | </li> | ||
| 10812 | |||
| 10813 | <li><b>"<code>L</code>": </b> | ||
| 10814 | reads the next line keeping the end-of-line character (if present), | ||
| 10815 | returning <b>fail</b> on end of file. | ||
| 10816 | </li> | ||
| 10817 | |||
| 10818 | <li><b><em>number</em>: </b> | ||
| 10819 | reads a string with up to this number of bytes, | ||
| 10820 | returning <b>fail</b> on end of file. | ||
| 10821 | If <code>number</code> is zero, | ||
| 10822 | it reads nothing and returns an empty string, | ||
| 10823 | or <b>fail</b> on end of file. | ||
| 10824 | </li> | ||
| 10825 | |||
| 10826 | </ul><p> | ||
| 10827 | The formats "<code>l</code>" and "<code>L</code>" should be used only for text files. | ||
| 10828 | |||
| 10829 | |||
| 10830 | |||
| 10831 | |||
| 10832 | <p> | ||
| 10833 | <hr><h3><a name="pdf-file:seek"><code>file:seek ([whence [, offset]])</code></a></h3> | ||
| 10834 | |||
| 10835 | |||
| 10836 | <p> | ||
| 10837 | Sets and gets the file position, | ||
| 10838 | measured from the beginning of the file, | ||
| 10839 | to the position given by <code>offset</code> plus a base | ||
| 10840 | specified by the string <code>whence</code>, as follows: | ||
| 10841 | |||
| 10842 | <ul> | ||
| 10843 | <li><b>"<code>set</code>": </b> base is position 0 (beginning of the file);</li> | ||
| 10844 | <li><b>"<code>cur</code>": </b> base is current position;</li> | ||
| 10845 | <li><b>"<code>end</code>": </b> base is end of file;</li> | ||
| 10846 | </ul><p> | ||
| 10847 | In case of success, <code>seek</code> returns the final file position, | ||
| 10848 | measured in bytes from the beginning of the file. | ||
| 10849 | If <code>seek</code> fails, it returns <b>fail</b>, | ||
| 10850 | plus a string describing the error. | ||
| 10851 | |||
| 10852 | |||
| 10853 | <p> | ||
| 10854 | The default value for <code>whence</code> is <code>"cur"</code>, | ||
| 10855 | and for <code>offset</code> is 0. | ||
| 10856 | Therefore, the call <code>file:seek()</code> returns the current | ||
| 10857 | file position, without changing it; | ||
| 10858 | the call <code>file:seek("set")</code> sets the position to the | ||
| 10859 | beginning of the file (and returns 0); | ||
| 10860 | and the call <code>file:seek("end")</code> sets the position to the | ||
| 10861 | end of the file, and returns its size. | ||
| 10862 | |||
| 10863 | |||
| 10864 | |||
| 10865 | |||
| 10866 | <p> | ||
| 10867 | <hr><h3><a name="pdf-file:setvbuf"><code>file:setvbuf (mode [, size])</code></a></h3> | ||
| 10868 | |||
| 10869 | |||
| 10870 | <p> | ||
| 10871 | Sets the buffering mode for a file. | ||
| 10872 | There are three available modes: | ||
| 10873 | |||
| 10874 | <ul> | ||
| 10875 | <li><b>"<code>no</code>": </b> no buffering.</li> | ||
| 10876 | <li><b>"<code>full</code>": </b> full buffering.</li> | ||
| 10877 | <li><b>"<code>line</code>": </b> line buffering.</li> | ||
| 10878 | </ul> | ||
| 10879 | |||
| 10880 | <p> | ||
| 10881 | For the last two cases, | ||
| 10882 | <code>size</code> is a hint for the size of the buffer, in bytes. | ||
| 10883 | The default is an appropriate size. | ||
| 10884 | |||
| 10885 | |||
| 10886 | <p> | ||
| 10887 | The specific behavior of each mode is non portable; | ||
| 10888 | check the underlying ISO C function <code>setvbuf</code> in your platform for | ||
| 10889 | more details. | ||
| 10890 | |||
| 10891 | |||
| 10892 | |||
| 10893 | |||
| 10894 | <p> | ||
| 10895 | <hr><h3><a name="pdf-file:write"><code>file:write (···)</code></a></h3> | ||
| 10896 | |||
| 10897 | |||
| 10898 | <p> | ||
| 10899 | Writes the value of each of its arguments to <code>file</code>. | ||
| 10900 | The arguments must be strings or numbers. | ||
| 10901 | |||
| 10902 | |||
| 10903 | <p> | ||
| 10904 | In case of success, this function returns <code>file</code>. | ||
| 10905 | |||
| 10906 | |||
| 10907 | |||
| 10908 | |||
| 10909 | |||
| 10910 | |||
| 10911 | |||
| 10912 | <h2>6.9 – <a name="6.9">Operating System Facilities</a></h2> | ||
| 10913 | |||
| 10914 | <p> | ||
| 10915 | This library is implemented through table <a name="pdf-os"><code>os</code></a>. | ||
| 10916 | |||
| 10917 | |||
| 10918 | <p> | ||
| 10919 | <hr><h3><a name="pdf-os.clock"><code>os.clock ()</code></a></h3> | ||
| 10920 | |||
| 10921 | |||
| 10922 | <p> | ||
| 10923 | Returns an approximation of the amount in seconds of CPU time | ||
| 10924 | used by the program, | ||
| 10925 | as returned by the underlying ISO C function <code>clock</code>. | ||
| 10926 | |||
| 10927 | |||
| 10928 | |||
| 10929 | |||
| 10930 | <p> | ||
| 10931 | <hr><h3><a name="pdf-os.date"><code>os.date ([format [, time]])</code></a></h3> | ||
| 10932 | |||
| 10933 | |||
| 10934 | <p> | ||
| 10935 | Returns a string or a table containing date and time, | ||
| 10936 | formatted according to the given string <code>format</code>. | ||
| 10937 | |||
| 10938 | |||
| 10939 | <p> | ||
| 10940 | If the <code>time</code> argument is present, | ||
| 10941 | this is the time to be formatted | ||
| 10942 | (see the <a href="#pdf-os.time"><code>os.time</code></a> function for a description of this value). | ||
| 10943 | Otherwise, <code>date</code> formats the current time. | ||
| 10944 | |||
| 10945 | |||
| 10946 | <p> | ||
| 10947 | If <code>format</code> starts with '<code>!</code>', | ||
| 10948 | then the date is formatted in Coordinated Universal Time. | ||
| 10949 | After this optional character, | ||
| 10950 | if <code>format</code> is the string "<code>*t</code>", | ||
| 10951 | then <code>date</code> returns a table with the following fields: | ||
| 10952 | <code>year</code>, <code>month</code> (1–12), <code>day</code> (1–31), | ||
| 10953 | <code>hour</code> (0–23), <code>min</code> (0–59), | ||
| 10954 | <code>sec</code> (0–61, due to leap seconds), | ||
| 10955 | <code>wday</code> (weekday, 1–7, Sunday is 1), | ||
| 10956 | <code>yday</code> (day of the year, 1–366), | ||
| 10957 | and <code>isdst</code> (daylight saving flag, a boolean). | ||
| 10958 | This last field may be absent | ||
| 10959 | if the information is not available. | ||
| 10960 | |||
| 10961 | |||
| 10962 | <p> | ||
| 10963 | If <code>format</code> is not "<code>*t</code>", | ||
| 10964 | then <code>date</code> returns the date as a string, | ||
| 10965 | formatted according to the same rules as the ISO C function <code>strftime</code>. | ||
| 10966 | |||
| 10967 | |||
| 10968 | <p> | ||
| 10969 | If <code>format</code> is absent, it defaults to "<code>%c</code>", | ||
| 10970 | which gives a human-readable date and time representation | ||
| 10971 | using the current locale. | ||
| 10972 | |||
| 10973 | |||
| 10974 | <p> | ||
| 10975 | On non-POSIX systems, | ||
| 10976 | this function may be not thread safe | ||
| 10977 | because of its reliance on C function <code>gmtime</code> and C function <code>localtime</code>. | ||
| 10978 | |||
| 10979 | |||
| 10980 | |||
| 10981 | |||
| 10982 | <p> | ||
| 10983 | <hr><h3><a name="pdf-os.difftime"><code>os.difftime (t2, t1)</code></a></h3> | ||
| 10984 | |||
| 10985 | |||
| 10986 | <p> | ||
| 10987 | Returns the difference, in seconds, | ||
| 10988 | from time <code>t1</code> to time <code>t2</code> | ||
| 10989 | (where the times are values returned by <a href="#pdf-os.time"><code>os.time</code></a>). | ||
| 10990 | In POSIX, Windows, and some other systems, | ||
| 10991 | this value is exactly <code>t2</code><em>-</em><code>t1</code>. | ||
| 10992 | |||
| 10993 | |||
| 10994 | |||
| 10995 | |||
| 10996 | <p> | ||
| 10997 | <hr><h3><a name="pdf-os.execute"><code>os.execute ([command])</code></a></h3> | ||
| 10998 | |||
| 10999 | |||
| 11000 | <p> | ||
| 11001 | This function is equivalent to the ISO C function <code>system</code>. | ||
| 11002 | It passes <code>command</code> to be executed by an operating system shell. | ||
| 11003 | Its first result is <b>true</b> | ||
| 11004 | if the command terminated successfully, | ||
| 11005 | or <b>fail</b> otherwise. | ||
| 11006 | After this first result | ||
| 11007 | the function returns a string plus a number, | ||
| 11008 | as follows: | ||
| 11009 | |||
| 11010 | <ul> | ||
| 11011 | |||
| 11012 | <li><b>"<code>exit</code>": </b> | ||
| 11013 | the command terminated normally; | ||
| 11014 | the following number is the exit status of the command. | ||
| 11015 | </li> | ||
| 11016 | |||
| 11017 | <li><b>"<code>signal</code>": </b> | ||
| 11018 | the command was terminated by a signal; | ||
| 11019 | the following number is the signal that terminated the command. | ||
| 11020 | </li> | ||
| 11021 | |||
| 11022 | </ul> | ||
| 11023 | |||
| 11024 | <p> | ||
| 11025 | When called without a <code>command</code>, | ||
| 11026 | <code>os.execute</code> returns a boolean that is true if a shell is available. | ||
| 11027 | |||
| 11028 | |||
| 11029 | |||
| 11030 | |||
| 11031 | <p> | ||
| 11032 | <hr><h3><a name="pdf-os.exit"><code>os.exit ([code [, close]])</code></a></h3> | ||
| 11033 | |||
| 11034 | |||
| 11035 | <p> | ||
| 11036 | Calls the ISO C function <code>exit</code> to terminate the host program. | ||
| 11037 | If <code>code</code> is <b>true</b>, | ||
| 11038 | the returned status is <code>EXIT_SUCCESS</code>; | ||
| 11039 | if <code>code</code> is <b>false</b>, | ||
| 11040 | the returned status is <code>EXIT_FAILURE</code>; | ||
| 11041 | if <code>code</code> is a number, | ||
| 11042 | the returned status is this number. | ||
| 11043 | The default value for <code>code</code> is <b>true</b>. | ||
| 11044 | |||
| 11045 | |||
| 11046 | <p> | ||
| 11047 | If the optional second argument <code>close</code> is true, | ||
| 11048 | the function closes the Lua state before exiting (see <a href="#lua_close"><code>lua_close</code></a>). | ||
| 11049 | |||
| 11050 | |||
| 11051 | |||
| 11052 | |||
| 11053 | <p> | ||
| 11054 | <hr><h3><a name="pdf-os.getenv"><code>os.getenv (varname)</code></a></h3> | ||
| 11055 | |||
| 11056 | |||
| 11057 | <p> | ||
| 11058 | Returns the value of the process environment variable <code>varname</code> | ||
| 11059 | or <b>fail</b> if the variable is not defined. | ||
| 11060 | |||
| 11061 | |||
| 11062 | |||
| 11063 | |||
| 11064 | <p> | ||
| 11065 | <hr><h3><a name="pdf-os.remove"><code>os.remove (filename)</code></a></h3> | ||
| 11066 | |||
| 11067 | |||
| 11068 | <p> | ||
| 11069 | Deletes the file (or empty directory, on POSIX systems) | ||
| 11070 | with the given name. | ||
| 11071 | If this function fails, it returns <b>fail</b> | ||
| 11072 | plus a string describing the error and the error code. | ||
| 11073 | Otherwise, it returns true. | ||
| 11074 | |||
| 11075 | |||
| 11076 | |||
| 11077 | |||
| 11078 | <p> | ||
| 11079 | <hr><h3><a name="pdf-os.rename"><code>os.rename (oldname, newname)</code></a></h3> | ||
| 11080 | |||
| 11081 | |||
| 11082 | <p> | ||
| 11083 | Renames the file or directory named <code>oldname</code> to <code>newname</code>. | ||
| 11084 | If this function fails, it returns <b>fail</b>, | ||
| 11085 | plus a string describing the error and the error code. | ||
| 11086 | Otherwise, it returns true. | ||
| 11087 | |||
| 11088 | |||
| 11089 | |||
| 11090 | |||
| 11091 | <p> | ||
| 11092 | <hr><h3><a name="pdf-os.setlocale"><code>os.setlocale (locale [, category])</code></a></h3> | ||
| 11093 | |||
| 11094 | |||
| 11095 | <p> | ||
| 11096 | Sets the current locale of the program. | ||
| 11097 | <code>locale</code> is a system-dependent string specifying a locale; | ||
| 11098 | <code>category</code> is an optional string describing which category to change: | ||
| 11099 | <code>"all"</code>, <code>"collate"</code>, <code>"ctype"</code>, | ||
| 11100 | <code>"monetary"</code>, <code>"numeric"</code>, or <code>"time"</code>; | ||
| 11101 | the default category is <code>"all"</code>. | ||
| 11102 | The function returns the name of the new locale, | ||
| 11103 | or <b>fail</b> if the request cannot be honored. | ||
| 11104 | |||
| 11105 | |||
| 11106 | <p> | ||
| 11107 | If <code>locale</code> is the empty string, | ||
| 11108 | the current locale is set to an implementation-defined native locale. | ||
| 11109 | If <code>locale</code> is the string "<code>C</code>", | ||
| 11110 | the current locale is set to the standard C locale. | ||
| 11111 | |||
| 11112 | |||
| 11113 | <p> | ||
| 11114 | When called with <b>nil</b> as the first argument, | ||
| 11115 | this function only returns the name of the current locale | ||
| 11116 | for the given category. | ||
| 11117 | |||
| 11118 | |||
| 11119 | <p> | ||
| 11120 | This function may be not thread safe | ||
| 11121 | because of its reliance on C function <code>setlocale</code>. | ||
| 11122 | |||
| 11123 | |||
| 11124 | |||
| 11125 | |||
| 11126 | <p> | ||
| 11127 | <hr><h3><a name="pdf-os.time"><code>os.time ([table])</code></a></h3> | ||
| 11128 | |||
| 11129 | |||
| 11130 | <p> | ||
| 11131 | Returns the current time when called without arguments, | ||
| 11132 | or a time representing the local date and time specified by the given table. | ||
| 11133 | This table must have fields <code>year</code>, <code>month</code>, and <code>day</code>, | ||
| 11134 | and may have fields | ||
| 11135 | <code>hour</code> (default is 12), | ||
| 11136 | <code>min</code> (default is 0), | ||
| 11137 | <code>sec</code> (default is 0), | ||
| 11138 | and <code>isdst</code> (default is <b>nil</b>). | ||
| 11139 | Other fields are ignored. | ||
| 11140 | For a description of these fields, see the <a href="#pdf-os.date"><code>os.date</code></a> function. | ||
| 11141 | |||
| 11142 | |||
| 11143 | <p> | ||
| 11144 | When the function is called, | ||
| 11145 | the values in these fields do not need to be inside their valid ranges. | ||
| 11146 | For instance, if <code>sec</code> is -10, | ||
| 11147 | it means 10 seconds before the time specified by the other fields; | ||
| 11148 | if <code>hour</code> is 1000, | ||
| 11149 | it means 1000 hours after the time specified by the other fields. | ||
| 11150 | |||
| 11151 | |||
| 11152 | <p> | ||
| 11153 | The returned value is a number, whose meaning depends on your system. | ||
| 11154 | In POSIX, Windows, and some other systems, | ||
| 11155 | this number counts the number | ||
| 11156 | of seconds since some given start time (the "epoch"). | ||
| 11157 | In other systems, the meaning is not specified, | ||
| 11158 | and the number returned by <code>time</code> can be used only as an argument to | ||
| 11159 | <a href="#pdf-os.date"><code>os.date</code></a> and <a href="#pdf-os.difftime"><code>os.difftime</code></a>. | ||
| 11160 | |||
| 11161 | |||
| 11162 | <p> | ||
| 11163 | When called with a table, | ||
| 11164 | <code>os.time</code> also normalizes all the fields | ||
| 11165 | documented in the <a href="#pdf-os.date"><code>os.date</code></a> function, | ||
| 11166 | so that they represent the same time as before the call | ||
| 11167 | but with values inside their valid ranges. | ||
| 11168 | |||
| 11169 | |||
| 11170 | |||
| 11171 | |||
| 11172 | <p> | ||
| 11173 | <hr><h3><a name="pdf-os.tmpname"><code>os.tmpname ()</code></a></h3> | ||
| 11174 | |||
| 11175 | |||
| 11176 | <p> | ||
| 11177 | Returns a string with a file name that can | ||
| 11178 | be used for a temporary file. | ||
| 11179 | The file must be explicitly opened before its use | ||
| 11180 | and explicitly removed when no longer needed. | ||
| 11181 | |||
| 11182 | |||
| 11183 | <p> | ||
| 11184 | In POSIX systems, | ||
| 11185 | this function also creates a file with that name, | ||
| 11186 | to avoid security risks. | ||
| 11187 | (Someone else might create the file with wrong permissions | ||
| 11188 | in the time between getting the name and creating the file.) | ||
| 11189 | You still have to open the file to use it | ||
| 11190 | and to remove it (even if you do not use it). | ||
| 11191 | |||
| 11192 | |||
| 11193 | <p> | ||
| 11194 | When possible, | ||
| 11195 | you may prefer to use <a href="#pdf-io.tmpfile"><code>io.tmpfile</code></a>, | ||
| 11196 | which automatically removes the file when the program ends. | ||
| 11197 | |||
| 11198 | |||
| 11199 | |||
| 11200 | |||
| 11201 | |||
| 11202 | |||
| 11203 | |||
| 11204 | <h2>6.10 – <a name="6.10">The Debug Library</a></h2> | ||
| 11205 | |||
| 11206 | <p> | ||
| 11207 | This library provides | ||
| 11208 | the functionality of the debug interface (<a href="#4.7">§4.7</a>) to Lua programs. | ||
| 11209 | You should exert care when using this library. | ||
| 11210 | Several of its functions | ||
| 11211 | violate basic assumptions about Lua code | ||
| 11212 | (e.g., that variables local to a function | ||
| 11213 | cannot be accessed from outside; | ||
| 11214 | that userdata metatables cannot be changed by Lua code; | ||
| 11215 | that Lua programs do not crash) | ||
| 11216 | and therefore can compromise otherwise secure code. | ||
| 11217 | Moreover, some functions in this library may be slow. | ||
| 11218 | |||
| 11219 | |||
| 11220 | <p> | ||
| 11221 | All functions in this library are provided | ||
| 11222 | inside the <a name="pdf-debug"><code>debug</code></a> table. | ||
| 11223 | All functions that operate over a thread | ||
| 11224 | have an optional first argument which is the | ||
| 11225 | thread to operate over. | ||
| 11226 | The default is always the current thread. | ||
| 11227 | |||
| 11228 | |||
| 11229 | <p> | ||
| 11230 | <hr><h3><a name="pdf-debug.debug"><code>debug.debug ()</code></a></h3> | ||
| 11231 | |||
| 11232 | |||
| 11233 | <p> | ||
| 11234 | Enters an interactive mode with the user, | ||
| 11235 | running each string that the user enters. | ||
| 11236 | Using simple commands and other debug facilities, | ||
| 11237 | the user can inspect global and local variables, | ||
| 11238 | change their values, evaluate expressions, and so on. | ||
| 11239 | A line containing only the word <code>cont</code> finishes this function, | ||
| 11240 | so that the caller continues its execution. | ||
| 11241 | |||
| 11242 | |||
| 11243 | <p> | ||
| 11244 | Note that commands for <code>debug.debug</code> are not lexically nested | ||
| 11245 | within any function and so have no direct access to local variables. | ||
| 11246 | |||
| 11247 | |||
| 11248 | |||
| 11249 | |||
| 11250 | <p> | ||
| 11251 | <hr><h3><a name="pdf-debug.gethook"><code>debug.gethook ([thread])</code></a></h3> | ||
| 11252 | |||
| 11253 | |||
| 11254 | <p> | ||
| 11255 | Returns the current hook settings of the thread, as three values: | ||
| 11256 | the current hook function, the current hook mask, | ||
| 11257 | and the current hook count, | ||
| 11258 | as set by the <a href="#pdf-debug.sethook"><code>debug.sethook</code></a> function. | ||
| 11259 | |||
| 11260 | |||
| 11261 | <p> | ||
| 11262 | Returns <b>fail</b> if there is no active hook. | ||
| 11263 | |||
| 11264 | |||
| 11265 | |||
| 11266 | |||
| 11267 | <p> | ||
| 11268 | <hr><h3><a name="pdf-debug.getinfo"><code>debug.getinfo ([thread,] f [, what])</code></a></h3> | ||
| 11269 | |||
| 11270 | |||
| 11271 | <p> | ||
| 11272 | Returns a table with information about a function. | ||
| 11273 | You can give the function directly | ||
| 11274 | or you can give a number as the value of <code>f</code>, | ||
| 11275 | which means the function running at level <code>f</code> of the call stack | ||
| 11276 | of the given thread: | ||
| 11277 | level 0 is the current function (<code>getinfo</code> itself); | ||
| 11278 | level 1 is the function that called <code>getinfo</code> | ||
| 11279 | (except for tail calls, which do not count in the stack); | ||
| 11280 | and so on. | ||
| 11281 | If <code>f</code> is a number greater than the number of active functions, | ||
| 11282 | then <code>getinfo</code> returns <b>fail</b>. | ||
| 11283 | |||
| 11284 | |||
| 11285 | <p> | ||
| 11286 | The returned table can contain all the fields returned by <a href="#lua_getinfo"><code>lua_getinfo</code></a>, | ||
| 11287 | with the string <code>what</code> describing which fields to fill in. | ||
| 11288 | The default for <code>what</code> is to get all information available, | ||
| 11289 | except the table of valid lines. | ||
| 11290 | The option '<code>f</code>' | ||
| 11291 | adds a field named <code>func</code> with the function itself. | ||
| 11292 | The option '<code>L</code>' adds a field named <code>activelines</code> | ||
| 11293 | with the table of valid lines, | ||
| 11294 | provided the function is a Lua function. | ||
| 11295 | If the function has no debug information, | ||
| 11296 | the table is empty. | ||
| 11297 | |||
| 11298 | |||
| 11299 | <p> | ||
| 11300 | For instance, the expression <code>debug.getinfo(1,"n").name</code> returns | ||
| 11301 | a name for the current function, | ||
| 11302 | if a reasonable name can be found, | ||
| 11303 | and the expression <code>debug.getinfo(print)</code> | ||
| 11304 | returns a table with all available information | ||
| 11305 | about the <a href="#pdf-print"><code>print</code></a> function. | ||
| 11306 | |||
| 11307 | |||
| 11308 | |||
| 11309 | |||
| 11310 | <p> | ||
| 11311 | <hr><h3><a name="pdf-debug.getlocal"><code>debug.getlocal ([thread,] f, local)</code></a></h3> | ||
| 11312 | |||
| 11313 | |||
| 11314 | <p> | ||
| 11315 | This function returns the name and the value of the local variable | ||
| 11316 | with index <code>local</code> of the function at level <code>f</code> of the stack. | ||
| 11317 | This function accesses not only explicit local variables, | ||
| 11318 | but also parameters and temporary values. | ||
| 11319 | |||
| 11320 | |||
| 11321 | <p> | ||
| 11322 | The first parameter or local variable has index 1, and so on, | ||
| 11323 | following the order that they are declared in the code, | ||
| 11324 | counting only the variables that are active | ||
| 11325 | in the current scope of the function. | ||
| 11326 | Compile-time constants may not appear in this listing, | ||
| 11327 | if they were optimized away by the compiler. | ||
| 11328 | Negative indices refer to vararg arguments; | ||
| 11329 | -1 is the first vararg argument. | ||
| 11330 | The function returns <b>fail</b> | ||
| 11331 | if there is no variable with the given index, | ||
| 11332 | and raises an error when called with a level out of range. | ||
| 11333 | (You can call <a href="#pdf-debug.getinfo"><code>debug.getinfo</code></a> to check whether the level is valid.) | ||
| 11334 | |||
| 11335 | |||
| 11336 | <p> | ||
| 11337 | Variable names starting with '<code>(</code>' (open parenthesis) | ||
| 11338 | represent variables with no known names | ||
| 11339 | (internal variables such as loop control variables, | ||
| 11340 | and variables from chunks saved without debug information). | ||
| 11341 | |||
| 11342 | |||
| 11343 | <p> | ||
| 11344 | The parameter <code>f</code> may also be a function. | ||
| 11345 | In that case, <code>getlocal</code> returns only the name of function parameters. | ||
| 11346 | |||
| 11347 | |||
| 11348 | |||
| 11349 | |||
| 11350 | <p> | ||
| 11351 | <hr><h3><a name="pdf-debug.getmetatable"><code>debug.getmetatable (value)</code></a></h3> | ||
| 11352 | |||
| 11353 | |||
| 11354 | <p> | ||
| 11355 | Returns the metatable of the given <code>value</code> | ||
| 11356 | or <b>nil</b> if it does not have a metatable. | ||
| 11357 | |||
| 11358 | |||
| 11359 | |||
| 11360 | |||
| 11361 | <p> | ||
| 11362 | <hr><h3><a name="pdf-debug.getregistry"><code>debug.getregistry ()</code></a></h3> | ||
| 11363 | |||
| 11364 | |||
| 11365 | <p> | ||
| 11366 | Returns the registry table (see <a href="#4.3">§4.3</a>). | ||
| 11367 | |||
| 11368 | |||
| 11369 | |||
| 11370 | |||
| 11371 | <p> | ||
| 11372 | <hr><h3><a name="pdf-debug.getupvalue"><code>debug.getupvalue (f, up)</code></a></h3> | ||
| 11373 | |||
| 11374 | |||
| 11375 | <p> | ||
| 11376 | This function returns the name and the value of the upvalue | ||
| 11377 | with index <code>up</code> of the function <code>f</code>. | ||
| 11378 | The function returns <b>fail</b> | ||
| 11379 | if there is no upvalue with the given index. | ||
| 11380 | |||
| 11381 | |||
| 11382 | <p> | ||
| 11383 | (For Lua functions, | ||
| 11384 | upvalues are the external local variables that the function uses, | ||
| 11385 | and that are consequently included in its closure.) | ||
| 11386 | |||
| 11387 | |||
| 11388 | <p> | ||
| 11389 | For C functions, this function uses the empty string <code>""</code> | ||
| 11390 | as a name for all upvalues. | ||
| 11391 | |||
| 11392 | |||
| 11393 | <p> | ||
| 11394 | Variable name '<code>?</code>' (interrogation mark) | ||
| 11395 | represents variables with no known names | ||
| 11396 | (variables from chunks saved without debug information). | ||
| 11397 | |||
| 11398 | |||
| 11399 | |||
| 11400 | |||
| 11401 | <p> | ||
| 11402 | <hr><h3><a name="pdf-debug.getuservalue"><code>debug.getuservalue (u, n)</code></a></h3> | ||
| 11403 | |||
| 11404 | |||
| 11405 | <p> | ||
| 11406 | Returns the <code>n</code>-th user value associated | ||
| 11407 | to the userdata <code>u</code> plus a boolean, | ||
| 11408 | <b>false</b> if the userdata does not have that value. | ||
| 11409 | |||
| 11410 | |||
| 11411 | |||
| 11412 | |||
| 11413 | <p> | ||
| 11414 | <hr><h3><a name="pdf-debug.sethook"><code>debug.sethook ([thread,] hook, mask [, count])</code></a></h3> | ||
| 11415 | |||
| 11416 | |||
| 11417 | <p> | ||
| 11418 | Sets the given function as the debug hook. | ||
| 11419 | The string <code>mask</code> and the number <code>count</code> describe | ||
| 11420 | when the hook will be called. | ||
| 11421 | The string mask may have any combination of the following characters, | ||
| 11422 | with the given meaning: | ||
| 11423 | |||
| 11424 | <ul> | ||
| 11425 | <li><b>'<code>c</code>': </b> the hook is called every time Lua calls a function;</li> | ||
| 11426 | <li><b>'<code>r</code>': </b> the hook is called every time Lua returns from a function;</li> | ||
| 11427 | <li><b>'<code>l</code>': </b> the hook is called every time Lua enters a new line of code.</li> | ||
| 11428 | </ul><p> | ||
| 11429 | Moreover, | ||
| 11430 | with a <code>count</code> different from zero, | ||
| 11431 | the hook is called also after every <code>count</code> instructions. | ||
| 11432 | |||
| 11433 | |||
| 11434 | <p> | ||
| 11435 | When called without arguments, | ||
| 11436 | <a href="#pdf-debug.sethook"><code>debug.sethook</code></a> turns off the hook. | ||
| 11437 | |||
| 11438 | |||
| 11439 | <p> | ||
| 11440 | When the hook is called, its first parameter is a string | ||
| 11441 | describing the event that has triggered its call: | ||
| 11442 | <code>"call"</code>, <code>"tail call"</code>, <code>"return"</code>, | ||
| 11443 | <code>"line"</code>, and <code>"count"</code>. | ||
| 11444 | For line events, | ||
| 11445 | the hook also gets the new line number as its second parameter. | ||
| 11446 | Inside a hook, | ||
| 11447 | you can call <code>getinfo</code> with level 2 to get more information about | ||
| 11448 | the running function. | ||
| 11449 | (Level 0 is the <code>getinfo</code> function, | ||
| 11450 | and level 1 is the hook function.) | ||
| 11451 | |||
| 11452 | |||
| 11453 | |||
| 11454 | |||
| 11455 | <p> | ||
| 11456 | <hr><h3><a name="pdf-debug.setlocal"><code>debug.setlocal ([thread,] level, local, value)</code></a></h3> | ||
| 11457 | |||
| 11458 | |||
| 11459 | <p> | ||
| 11460 | This function assigns the value <code>value</code> to the local variable | ||
| 11461 | with index <code>local</code> of the function at level <code>level</code> of the stack. | ||
| 11462 | The function returns <b>fail</b> if there is no local | ||
| 11463 | variable with the given index, | ||
| 11464 | and raises an error when called with a <code>level</code> out of range. | ||
| 11465 | (You can call <code>getinfo</code> to check whether the level is valid.) | ||
| 11466 | Otherwise, it returns the name of the local variable. | ||
| 11467 | |||
| 11468 | |||
| 11469 | <p> | ||
| 11470 | See <a href="#pdf-debug.getlocal"><code>debug.getlocal</code></a> for more information about | ||
| 11471 | variable indices and names. | ||
| 11472 | |||
| 11473 | |||
| 11474 | |||
| 11475 | |||
| 11476 | <p> | ||
| 11477 | <hr><h3><a name="pdf-debug.setmetatable"><code>debug.setmetatable (value, table)</code></a></h3> | ||
| 11478 | |||
| 11479 | |||
| 11480 | <p> | ||
| 11481 | Sets the metatable for the given <code>value</code> to the given <code>table</code> | ||
| 11482 | (which can be <b>nil</b>). | ||
| 11483 | Returns <code>value</code>. | ||
| 11484 | |||
| 11485 | |||
| 11486 | |||
| 11487 | |||
| 11488 | <p> | ||
| 11489 | <hr><h3><a name="pdf-debug.setupvalue"><code>debug.setupvalue (f, up, value)</code></a></h3> | ||
| 11490 | |||
| 11491 | |||
| 11492 | <p> | ||
| 11493 | This function assigns the value <code>value</code> to the upvalue | ||
| 11494 | with index <code>up</code> of the function <code>f</code>. | ||
| 11495 | The function returns <b>fail</b> if there is no upvalue | ||
| 11496 | with the given index. | ||
| 11497 | Otherwise, it returns the name of the upvalue. | ||
| 11498 | |||
| 11499 | |||
| 11500 | <p> | ||
| 11501 | See <a href="#pdf-debug.getupvalue"><code>debug.getupvalue</code></a> for more information about upvalues. | ||
| 11502 | |||
| 11503 | |||
| 11504 | |||
| 11505 | |||
| 11506 | <p> | ||
| 11507 | <hr><h3><a name="pdf-debug.setuservalue"><code>debug.setuservalue (udata, value, n)</code></a></h3> | ||
| 11508 | |||
| 11509 | |||
| 11510 | <p> | ||
| 11511 | Sets the given <code>value</code> as | ||
| 11512 | the <code>n</code>-th user value associated to the given <code>udata</code>. | ||
| 11513 | <code>udata</code> must be a full userdata. | ||
| 11514 | |||
| 11515 | |||
| 11516 | <p> | ||
| 11517 | Returns <code>udata</code>, | ||
| 11518 | or <b>fail</b> if the userdata does not have that value. | ||
| 11519 | |||
| 11520 | |||
| 11521 | |||
| 11522 | |||
| 11523 | <p> | ||
| 11524 | <hr><h3><a name="pdf-debug.traceback"><code>debug.traceback ([thread,] [message [, level]])</code></a></h3> | ||
| 11525 | |||
| 11526 | |||
| 11527 | <p> | ||
| 11528 | If <code>message</code> is present but is neither a string nor <b>nil</b>, | ||
| 11529 | this function returns <code>message</code> without further processing. | ||
| 11530 | Otherwise, | ||
| 11531 | it returns a string with a traceback of the call stack. | ||
| 11532 | The optional <code>message</code> string is appended | ||
| 11533 | at the beginning of the traceback. | ||
| 11534 | An optional <code>level</code> number tells at which level | ||
| 11535 | to start the traceback | ||
| 11536 | (default is 1, the function calling <code>traceback</code>). | ||
| 11537 | |||
| 11538 | |||
| 11539 | |||
| 11540 | |||
| 11541 | <p> | ||
| 11542 | <hr><h3><a name="pdf-debug.upvalueid"><code>debug.upvalueid (f, n)</code></a></h3> | ||
| 11543 | |||
| 11544 | |||
| 11545 | <p> | ||
| 11546 | Returns a unique identifier (as a light userdata) | ||
| 11547 | for the upvalue numbered <code>n</code> | ||
| 11548 | from the given function. | ||
| 11549 | |||
| 11550 | |||
| 11551 | <p> | ||
| 11552 | These unique identifiers allow a program to check whether different | ||
| 11553 | closures share upvalues. | ||
| 11554 | Lua closures that share an upvalue | ||
| 11555 | (that is, that access a same external local variable) | ||
| 11556 | will return identical ids for those upvalue indices. | ||
| 11557 | |||
| 11558 | |||
| 11559 | |||
| 11560 | |||
| 11561 | <p> | ||
| 11562 | <hr><h3><a name="pdf-debug.upvaluejoin"><code>debug.upvaluejoin (f1, n1, f2, n2)</code></a></h3> | ||
| 11563 | |||
| 11564 | |||
| 11565 | <p> | ||
| 11566 | Make the <code>n1</code>-th upvalue of the Lua closure <code>f1</code> | ||
| 11567 | refer to the <code>n2</code>-th upvalue of the Lua closure <code>f2</code>. | ||
| 11568 | |||
| 11569 | |||
| 11570 | |||
| 11571 | |||
| 11572 | |||
| 11573 | |||
| 11574 | |||
| 11575 | <h1>7 – <a name="7">Lua Standalone</a></h1> | ||
| 11576 | |||
| 11577 | <p> | ||
| 11578 | Although Lua has been designed as an extension language, | ||
| 11579 | to be embedded in a host C program, | ||
| 11580 | it is also frequently used as a standalone language. | ||
| 11581 | An interpreter for Lua as a standalone language, | ||
| 11582 | called simply <code>lua</code>, | ||
| 11583 | is provided with the standard distribution. | ||
| 11584 | The standalone interpreter includes | ||
| 11585 | all standard libraries. | ||
| 11586 | Its usage is: | ||
| 11587 | |||
| 11588 | <pre> | ||
| 11589 | lua [options] [script [args]] | ||
| 11590 | </pre><p> | ||
| 11591 | The options are: | ||
| 11592 | |||
| 11593 | <ul> | ||
| 11594 | <li><b><code>-e <em>stat</em></code>: </b> execute string <em>stat</em>;</li> | ||
| 11595 | <li><b><code>-i</code>: </b> enter interactive mode after running <em>script</em>;</li> | ||
| 11596 | <li><b><code>-l <em>mod</em></code>: </b> "require" <em>mod</em> and assign the | ||
| 11597 | result to global <em>mod</em>;</li> | ||
| 11598 | <li><b><code>-l <em>g=mod</em></code>: </b> "require" <em>mod</em> and assign the | ||
| 11599 | result to global <em>g</em>;</li> | ||
| 11600 | <li><b><code>-v</code>: </b> print version information;</li> | ||
| 11601 | <li><b><code>-E</code>: </b> ignore environment variables;</li> | ||
| 11602 | <li><b><code>-W</code>: </b> turn warnings on;</li> | ||
| 11603 | <li><b><code>--</code>: </b> stop handling options;</li> | ||
| 11604 | <li><b><code>-</code>: </b> execute <code>stdin</code> as a file and stop handling options.</li> | ||
| 11605 | </ul><p> | ||
| 11606 | (The form <code>-l <em>g=mod</em></code> was introduced in release 5.4.4.) | ||
| 11607 | |||
| 11608 | |||
| 11609 | <p> | ||
| 11610 | After handling its options, <code>lua</code> runs the given <em>script</em>. | ||
| 11611 | When called without arguments, | ||
| 11612 | <code>lua</code> behaves as <code>lua -v -i</code> | ||
| 11613 | when the standard input (<code>stdin</code>) is a terminal, | ||
| 11614 | and as <code>lua -</code> otherwise. | ||
| 11615 | |||
| 11616 | |||
| 11617 | <p> | ||
| 11618 | When called without the option <code>-E</code>, | ||
| 11619 | the interpreter checks for an environment variable <a name="pdf-LUA_INIT_5_4"><code>LUA_INIT_5_4</code></a> | ||
| 11620 | (or <a name="pdf-LUA_INIT"><code>LUA_INIT</code></a> if the versioned name is not defined) | ||
| 11621 | before running any argument. | ||
| 11622 | If the variable content has the format <code>@<em>filename</em></code>, | ||
| 11623 | then <code>lua</code> executes the file. | ||
| 11624 | Otherwise, <code>lua</code> executes the string itself. | ||
| 11625 | |||
| 11626 | |||
| 11627 | <p> | ||
| 11628 | When called with the option <code>-E</code>, | ||
| 11629 | Lua does not consult any environment variables. | ||
| 11630 | In particular, | ||
| 11631 | the values of <a href="#pdf-package.path"><code>package.path</code></a> and <a href="#pdf-package.cpath"><code>package.cpath</code></a> | ||
| 11632 | are set with the default paths defined in <code>luaconf.h</code>. | ||
| 11633 | To signal to the libraries that this option is on, | ||
| 11634 | the stand-alone interpreter sets the field | ||
| 11635 | <code>"LUA_NOENV"</code> in the registry to a true value. | ||
| 11636 | Other libraries may consult this field for the same purpose. | ||
| 11637 | |||
| 11638 | |||
| 11639 | <p> | ||
| 11640 | The options <code>-e</code>, <code>-l</code>, and <code>-W</code> are handled in | ||
| 11641 | the order they appear. | ||
| 11642 | For instance, an invocation like | ||
| 11643 | |||
| 11644 | <pre> | ||
| 11645 | $ lua -e 'a=1' -llib1 script.lua | ||
| 11646 | </pre><p> | ||
| 11647 | will first set <code>a</code> to 1, then require the library <code>lib1</code>, | ||
| 11648 | and finally run the file <code>script.lua</code> with no arguments. | ||
| 11649 | (Here <code>$</code> is the shell prompt. Your prompt may be different.) | ||
| 11650 | |||
| 11651 | |||
| 11652 | <p> | ||
| 11653 | Before running any code, | ||
| 11654 | <code>lua</code> collects all command-line arguments | ||
| 11655 | in a global table called <code>arg</code>. | ||
| 11656 | The script name goes to index 0, | ||
| 11657 | the first argument after the script name goes to index 1, | ||
| 11658 | and so on. | ||
| 11659 | Any arguments before the script name | ||
| 11660 | (that is, the interpreter name plus its options) | ||
| 11661 | go to negative indices. | ||
| 11662 | For instance, in the call | ||
| 11663 | |||
| 11664 | <pre> | ||
| 11665 | $ lua -la b.lua t1 t2 | ||
| 11666 | </pre><p> | ||
| 11667 | the table is like this: | ||
| 11668 | |||
| 11669 | <pre> | ||
| 11670 | arg = { [-2] = "lua", [-1] = "-la", | ||
| 11671 | [0] = "b.lua", | ||
| 11672 | [1] = "t1", [2] = "t2" } | ||
| 11673 | </pre><p> | ||
| 11674 | If there is no script in the call, | ||
| 11675 | the interpreter name goes to index 0, | ||
| 11676 | followed by the other arguments. | ||
| 11677 | For instance, the call | ||
| 11678 | |||
| 11679 | <pre> | ||
| 11680 | $ lua -e "print(arg[1])" | ||
| 11681 | </pre><p> | ||
| 11682 | will print "<code>-e</code>". | ||
| 11683 | If there is a script, | ||
| 11684 | the script is called with arguments | ||
| 11685 | <code>arg[1]</code>, ···, <code>arg[#arg]</code>. | ||
| 11686 | Like all chunks in Lua, | ||
| 11687 | the script is compiled as a variadic function. | ||
| 11688 | |||
| 11689 | |||
| 11690 | <p> | ||
| 11691 | In interactive mode, | ||
| 11692 | Lua repeatedly prompts and waits for a line. | ||
| 11693 | After reading a line, | ||
| 11694 | Lua first try to interpret the line as an expression. | ||
| 11695 | If it succeeds, it prints its value. | ||
| 11696 | Otherwise, it interprets the line as a statement. | ||
| 11697 | If you write an incomplete statement, | ||
| 11698 | the interpreter waits for its completion | ||
| 11699 | by issuing a different prompt. | ||
| 11700 | |||
| 11701 | |||
| 11702 | <p> | ||
| 11703 | If the global variable <a name="pdf-_PROMPT"><code>_PROMPT</code></a> contains a string, | ||
| 11704 | then its value is used as the prompt. | ||
| 11705 | Similarly, if the global variable <a name="pdf-_PROMPT2"><code>_PROMPT2</code></a> contains a string, | ||
| 11706 | its value is used as the secondary prompt | ||
| 11707 | (issued during incomplete statements). | ||
| 11708 | |||
| 11709 | |||
| 11710 | <p> | ||
| 11711 | In case of unprotected errors in the script, | ||
| 11712 | the interpreter reports the error to the standard error stream. | ||
| 11713 | If the error object is not a string but | ||
| 11714 | has a metamethod <code>__tostring</code>, | ||
| 11715 | the interpreter calls this metamethod to produce the final message. | ||
| 11716 | Otherwise, the interpreter converts the error object to a string | ||
| 11717 | and adds a stack traceback to it. | ||
| 11718 | When warnings are on, | ||
| 11719 | they are simply printed in the standard error output. | ||
| 11720 | |||
| 11721 | |||
| 11722 | <p> | ||
| 11723 | When finishing normally, | ||
| 11724 | the interpreter closes its main Lua state | ||
| 11725 | (see <a href="#lua_close"><code>lua_close</code></a>). | ||
| 11726 | The script can avoid this step by | ||
| 11727 | calling <a href="#pdf-os.exit"><code>os.exit</code></a> to terminate. | ||
| 11728 | |||
| 11729 | |||
| 11730 | <p> | ||
| 11731 | To allow the use of Lua as a | ||
| 11732 | script interpreter in Unix systems, | ||
| 11733 | Lua skips the first line of a file chunk if it starts with <code>#</code>. | ||
| 11734 | Therefore, Lua scripts can be made into executable programs | ||
| 11735 | by using <code>chmod +x</code> and the <code>#!</code> form, | ||
| 11736 | as in | ||
| 11737 | |||
| 11738 | <pre> | ||
| 11739 | #!/usr/local/bin/lua | ||
| 11740 | </pre><p> | ||
| 11741 | Of course, | ||
| 11742 | the location of the Lua interpreter may be different in your machine. | ||
| 11743 | If <code>lua</code> is in your <code>PATH</code>, | ||
| 11744 | then | ||
| 11745 | |||
| 11746 | <pre> | ||
| 11747 | #!/usr/bin/env lua | ||
| 11748 | </pre><p> | ||
| 11749 | is a more portable solution. | ||
| 11750 | |||
| 11751 | |||
| 11752 | |||
| 11753 | <h1>8 – <a name="8">Incompatibilities with the Previous Version</a></h1> | ||
| 11754 | |||
| 11755 | |||
| 11756 | |||
| 11757 | <p> | ||
| 11758 | Here we list the incompatibilities that you may find when moving a program | ||
| 11759 | from Lua 5.3 to Lua 5.4. | ||
| 11760 | |||
| 11761 | |||
| 11762 | <p> | ||
| 11763 | You can avoid some incompatibilities by compiling Lua with | ||
| 11764 | appropriate options (see file <code>luaconf.h</code>). | ||
| 11765 | However, | ||
| 11766 | all these compatibility options will be removed in the future. | ||
| 11767 | More often than not, | ||
| 11768 | compatibility issues arise when these compatibility options | ||
| 11769 | are removed. | ||
| 11770 | So, whenever you have the chance, | ||
| 11771 | you should try to test your code with a version of Lua compiled | ||
| 11772 | with all compatibility options turned off. | ||
| 11773 | That will ease transitions to newer versions of Lua. | ||
| 11774 | |||
| 11775 | |||
| 11776 | <p> | ||
| 11777 | Lua versions can always change the C API in ways that | ||
| 11778 | do not imply source-code changes in a program, | ||
| 11779 | such as the numeric values for constants | ||
| 11780 | or the implementation of functions as macros. | ||
| 11781 | Therefore, | ||
| 11782 | you should never assume that binaries are compatible between | ||
| 11783 | different Lua versions. | ||
| 11784 | Always recompile clients of the Lua API when | ||
| 11785 | using a new version. | ||
| 11786 | |||
| 11787 | |||
| 11788 | <p> | ||
| 11789 | Similarly, Lua versions can always change the internal representation | ||
| 11790 | of precompiled chunks; | ||
| 11791 | precompiled chunks are not compatible between different Lua versions. | ||
| 11792 | |||
| 11793 | |||
| 11794 | <p> | ||
| 11795 | The standard paths in the official distribution may | ||
| 11796 | change between versions. | ||
| 11797 | |||
| 11798 | |||
| 11799 | |||
| 11800 | |||
| 11801 | |||
| 11802 | <h2>8.1 – <a name="8.1">Incompatibilities in the Language</a></h2> | ||
| 11803 | <ul> | ||
| 11804 | |||
| 11805 | <li> | ||
| 11806 | The coercion of strings to numbers in | ||
| 11807 | arithmetic and bitwise operations | ||
| 11808 | has been removed from the core language. | ||
| 11809 | The string library does a similar job | ||
| 11810 | for arithmetic (but not for bitwise) operations | ||
| 11811 | using the string metamethods. | ||
| 11812 | However, unlike in previous versions, | ||
| 11813 | the new implementation preserves the implicit type of the numeral | ||
| 11814 | in the string. | ||
| 11815 | For instance, the result of <code>"1" + "2"</code> now is an integer, | ||
| 11816 | not a float. | ||
| 11817 | </li> | ||
| 11818 | |||
| 11819 | <li> | ||
| 11820 | Literal decimal integer constants that overflow are read as floats, | ||
| 11821 | instead of wrapping around. | ||
| 11822 | You can use hexadecimal notation for such constants if you | ||
| 11823 | want the old behavior | ||
| 11824 | (reading them as integers with wrap around). | ||
| 11825 | </li> | ||
| 11826 | |||
| 11827 | <li> | ||
| 11828 | The use of the <code>__lt</code> metamethod to emulate <code>__le</code> | ||
| 11829 | has been removed. | ||
| 11830 | When needed, this metamethod must be explicitly defined. | ||
| 11831 | </li> | ||
| 11832 | |||
| 11833 | <li> | ||
| 11834 | The semantics of the numerical <b>for</b> loop | ||
| 11835 | over integers changed in some details. | ||
| 11836 | In particular, the control variable never wraps around. | ||
| 11837 | </li> | ||
| 11838 | |||
| 11839 | <li> | ||
| 11840 | A label for a <b>goto</b> cannot be declared where a label with the same | ||
| 11841 | name is visible, even if this other label is declared in an enclosing | ||
| 11842 | block. | ||
| 11843 | </li> | ||
| 11844 | |||
| 11845 | <li> | ||
| 11846 | When finalizing an object, | ||
| 11847 | Lua does not ignore <code>__gc</code> metamethods that are not functions. | ||
| 11848 | Any value will be called, if present. | ||
| 11849 | (Non-callable values will generate a warning, | ||
| 11850 | like any other error when calling a finalizer.) | ||
| 11851 | </li> | ||
| 11852 | |||
| 11853 | </ul> | ||
| 11854 | |||
| 11855 | |||
| 11856 | |||
| 11857 | |||
| 11858 | <h2>8.2 – <a name="8.2">Incompatibilities in the Libraries</a></h2> | ||
| 11859 | <ul> | ||
| 11860 | |||
| 11861 | <li> | ||
| 11862 | The function <a href="#pdf-print"><code>print</code></a> does not call <a href="#pdf-tostring"><code>tostring</code></a> | ||
| 11863 | to format its arguments; | ||
| 11864 | instead, it has this functionality hardwired. | ||
| 11865 | You should use <code>__tostring</code> to modify how values are printed. | ||
| 11866 | </li> | ||
| 11867 | |||
| 11868 | <li> | ||
| 11869 | The pseudo-random number generator used by the function <a href="#pdf-math.random"><code>math.random</code></a> | ||
| 11870 | now starts with a somewhat random seed. | ||
| 11871 | Moreover, it uses a different algorithm. | ||
| 11872 | </li> | ||
| 11873 | |||
| 11874 | <li> | ||
| 11875 | By default, the decoding functions in the <a href="#pdf-utf8"><code>utf8</code></a> library | ||
| 11876 | do not accept surrogates as valid code points. | ||
| 11877 | An extra parameter in these functions makes them more permissive. | ||
| 11878 | </li> | ||
| 11879 | |||
| 11880 | <li> | ||
| 11881 | The options "<code>setpause</code>" and "<code>setstepmul</code>" | ||
| 11882 | of the function <a href="#pdf-collectgarbage"><code>collectgarbage</code></a> are deprecated. | ||
| 11883 | You should use the new option "<code>incremental</code>" to set them. | ||
| 11884 | </li> | ||
| 11885 | |||
| 11886 | <li> | ||
| 11887 | The function <a href="#pdf-io.lines"><code>io.lines</code></a> now returns four values, | ||
| 11888 | instead of just one. | ||
| 11889 | That can be a problem when it is used as the sole | ||
| 11890 | argument to another function that has optional parameters, | ||
| 11891 | such as in <code>load(io.lines(filename, "L"))</code>. | ||
| 11892 | To fix that issue, | ||
| 11893 | you can wrap the call into parentheses, | ||
| 11894 | to adjust its number of results to one. | ||
| 11895 | </li> | ||
| 11896 | |||
| 11897 | </ul> | ||
| 11898 | |||
| 11899 | |||
| 11900 | |||
| 11901 | |||
| 11902 | <h2>8.3 – <a name="8.3">Incompatibilities in the API</a></h2> | ||
| 11903 | |||
| 11904 | |||
| 11905 | <ul> | ||
| 11906 | |||
| 11907 | <li> | ||
| 11908 | Full userdata now has an arbitrary number of associated user values. | ||
| 11909 | Therefore, the functions <code>lua_newuserdata</code>, | ||
| 11910 | <code>lua_setuservalue</code>, and <code>lua_getuservalue</code> were | ||
| 11911 | replaced by <a href="#lua_newuserdatauv"><code>lua_newuserdatauv</code></a>, | ||
| 11912 | <a href="#lua_setiuservalue"><code>lua_setiuservalue</code></a>, and <a href="#lua_getiuservalue"><code>lua_getiuservalue</code></a>, | ||
| 11913 | which have an extra argument. | ||
| 11914 | |||
| 11915 | |||
| 11916 | <p> | ||
| 11917 | For compatibility, the old names still work as macros assuming | ||
| 11918 | one single user value. | ||
| 11919 | Note, however, that userdata with zero user values | ||
| 11920 | are more efficient memory-wise. | ||
| 11921 | </li> | ||
| 11922 | |||
| 11923 | <li> | ||
| 11924 | The function <a href="#lua_resume"><code>lua_resume</code></a> has an extra parameter. | ||
| 11925 | This out parameter returns the number of values on | ||
| 11926 | the top of the stack that were yielded or returned by the coroutine. | ||
| 11927 | (In previous versions, | ||
| 11928 | those values were the entire stack.) | ||
| 11929 | </li> | ||
| 11930 | |||
| 11931 | <li> | ||
| 11932 | The function <a href="#lua_version"><code>lua_version</code></a> returns the version number, | ||
| 11933 | instead of an address of the version number. | ||
| 11934 | The Lua core should work correctly with libraries using their | ||
| 11935 | own static copies of the same core, | ||
| 11936 | so there is no need to check whether they are using the same | ||
| 11937 | address space. | ||
| 11938 | </li> | ||
| 11939 | |||
| 11940 | <li> | ||
| 11941 | The constant <code>LUA_ERRGCMM</code> was removed. | ||
| 11942 | Errors in finalizers are never propagated; | ||
| 11943 | instead, they generate a warning. | ||
| 11944 | </li> | ||
| 11945 | |||
| 11946 | <li> | ||
| 11947 | The options <code>LUA_GCSETPAUSE</code> and <code>LUA_GCSETSTEPMUL</code> | ||
| 11948 | of the function <a href="#lua_gc"><code>lua_gc</code></a> are deprecated. | ||
| 11949 | You should use the new option <code>LUA_GCINC</code> to set them. | ||
| 11950 | </li> | ||
| 11951 | |||
| 11952 | </ul> | ||
| 11953 | |||
| 11954 | |||
| 11955 | |||
| 11956 | |||
| 11957 | <h1>9 – <a name="9">The Complete Syntax of Lua</a></h1> | ||
| 11958 | |||
| 11959 | <p> | ||
| 11960 | Here is the complete syntax of Lua in extended BNF. | ||
| 11961 | As usual in extended BNF, | ||
| 11962 | {A} means 0 or more As, | ||
| 11963 | and [A] means an optional A. | ||
| 11964 | (For operator precedences, see <a href="#3.4.8">§3.4.8</a>; | ||
| 11965 | for a description of the terminals | ||
| 11966 | Name, Numeral, | ||
| 11967 | and LiteralString, see <a href="#3.1">§3.1</a>.) | ||
| 11968 | |||
| 11969 | |||
| 11970 | |||
| 11971 | |||
| 11972 | <pre> | ||
| 11973 | |||
| 11974 | chunk ::= block | ||
| 11975 | |||
| 11976 | block ::= {stat} [retstat] | ||
| 11977 | |||
| 11978 | stat ::= ‘<b>;</b>’ | | ||
| 11979 | varlist ‘<b>=</b>’ explist | | ||
| 11980 | functioncall | | ||
| 11981 | label | | ||
| 11982 | <b>break</b> | | ||
| 11983 | <b>goto</b> Name | | ||
| 11984 | <b>do</b> block <b>end</b> | | ||
| 11985 | <b>while</b> exp <b>do</b> block <b>end</b> | | ||
| 11986 | <b>repeat</b> block <b>until</b> exp | | ||
| 11987 | <b>if</b> exp <b>then</b> block {<b>elseif</b> exp <b>then</b> block} [<b>else</b> block] <b>end</b> | | ||
| 11988 | <b>for</b> Name ‘<b>=</b>’ exp ‘<b>,</b>’ exp [‘<b>,</b>’ exp] <b>do</b> block <b>end</b> | | ||
| 11989 | <b>for</b> namelist <b>in</b> explist <b>do</b> block <b>end</b> | | ||
| 11990 | <b>function</b> funcname funcbody | | ||
| 11991 | <b>local</b> <b>function</b> Name funcbody | | ||
| 11992 | <b>local</b> attnamelist [‘<b>=</b>’ explist] | ||
| 11993 | |||
| 11994 | attnamelist ::= Name attrib {‘<b>,</b>’ Name attrib} | ||
| 11995 | |||
| 11996 | attrib ::= [‘<b><</b>’ Name ‘<b>></b>’] | ||
| 11997 | |||
| 11998 | retstat ::= <b>return</b> [explist] [‘<b>;</b>’] | ||
| 11999 | |||
| 12000 | label ::= ‘<b>::</b>’ Name ‘<b>::</b>’ | ||
| 12001 | |||
| 12002 | funcname ::= Name {‘<b>.</b>’ Name} [‘<b>:</b>’ Name] | ||
| 12003 | |||
| 12004 | varlist ::= var {‘<b>,</b>’ var} | ||
| 12005 | |||
| 12006 | var ::= Name | prefixexp ‘<b>[</b>’ exp ‘<b>]</b>’ | prefixexp ‘<b>.</b>’ Name | ||
| 12007 | |||
| 12008 | namelist ::= Name {‘<b>,</b>’ Name} | ||
| 12009 | |||
| 12010 | explist ::= exp {‘<b>,</b>’ exp} | ||
| 12011 | |||
| 12012 | exp ::= <b>nil</b> | <b>false</b> | <b>true</b> | Numeral | LiteralString | ‘<b>...</b>’ | functiondef | | ||
| 12013 | prefixexp | tableconstructor | exp binop exp | unop exp | ||
| 12014 | |||
| 12015 | prefixexp ::= var | functioncall | ‘<b>(</b>’ exp ‘<b>)</b>’ | ||
| 12016 | |||
| 12017 | functioncall ::= prefixexp args | prefixexp ‘<b>:</b>’ Name args | ||
| 12018 | |||
| 12019 | args ::= ‘<b>(</b>’ [explist] ‘<b>)</b>’ | tableconstructor | LiteralString | ||
| 12020 | |||
| 12021 | functiondef ::= <b>function</b> funcbody | ||
| 12022 | |||
| 12023 | funcbody ::= ‘<b>(</b>’ [parlist] ‘<b>)</b>’ block <b>end</b> | ||
| 12024 | |||
| 12025 | parlist ::= namelist [‘<b>,</b>’ ‘<b>...</b>’] | ‘<b>...</b>’ | ||
| 12026 | |||
| 12027 | tableconstructor ::= ‘<b>{</b>’ [fieldlist] ‘<b>}</b>’ | ||
| 12028 | |||
| 12029 | fieldlist ::= field {fieldsep field} [fieldsep] | ||
| 12030 | |||
| 12031 | field ::= ‘<b>[</b>’ exp ‘<b>]</b>’ ‘<b>=</b>’ exp | Name ‘<b>=</b>’ exp | exp | ||
| 12032 | |||
| 12033 | fieldsep ::= ‘<b>,</b>’ | ‘<b>;</b>’ | ||
| 12034 | |||
| 12035 | binop ::= ‘<b>+</b>’ | ‘<b>-</b>’ | ‘<b>*</b>’ | ‘<b>/</b>’ | ‘<b>//</b>’ | ‘<b>^</b>’ | ‘<b>%</b>’ | | ||
| 12036 | ‘<b>&</b>’ | ‘<b>~</b>’ | ‘<b>|</b>’ | ‘<b>>></b>’ | ‘<b><<</b>’ | ‘<b>..</b>’ | | ||
| 12037 | ‘<b><</b>’ | ‘<b><=</b>’ | ‘<b>></b>’ | ‘<b>>=</b>’ | ‘<b>==</b>’ | ‘<b>~=</b>’ | | ||
| 12038 | <b>and</b> | <b>or</b> | ||
| 12039 | |||
| 12040 | unop ::= ‘<b>-</b>’ | <b>not</b> | ‘<b>#</b>’ | ‘<b>~</b>’ | ||
| 12041 | |||
| 12042 | </pre> | ||
| 12043 | |||
| 12044 | <p> | ||
| 12045 | |||
| 12046 | |||
| 12047 | |||
| 12048 | |||
| 12049 | |||
| 12050 | |||
| 12051 | <P CLASS="footer"> | ||
| 12052 | Last update: | ||
| 12053 | Wed May 21 21:09:59 UTC 2025 | ||
| 12054 | </P> | ||
| 12055 | <!-- | ||
| 12056 | Last change: revised for Lua 5.4.8 | ||
| 12057 | --> | ||
| 12058 | |||
| 12059 | </body></html> | ||
| 12060 | |||
diff --git a/vendor/lua-5.4.8/doc/readme.html b/vendor/lua-5.4.8/doc/readme.html new file mode 100644 index 0000000..a4eb59d --- /dev/null +++ b/vendor/lua-5.4.8/doc/readme.html | |||
| @@ -0,0 +1,339 @@ | |||
| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> | ||
| 2 | <HTML> | ||
| 3 | <HEAD> | ||
| 4 | <TITLE>Lua 5.4 readme</TITLE> | ||
| 5 | <LINK REL="stylesheet" TYPE="text/css" HREF="lua.css"> | ||
| 6 | <META HTTP-EQUIV="content-type" CONTENT="text/html; charset=iso-8859-1"> | ||
| 7 | <STYLE TYPE="text/css"> | ||
| 8 | blockquote, .display { | ||
| 9 | border: solid #a0a0a0 2px ; | ||
| 10 | border-radius: 8px ; | ||
| 11 | padding: 1em ; | ||
| 12 | margin: 0px ; | ||
| 13 | } | ||
| 14 | |||
| 15 | .display { | ||
| 16 | word-spacing: 0.25em ; | ||
| 17 | } | ||
| 18 | |||
| 19 | dl.display dd { | ||
| 20 | padding-bottom: 0.2em ; | ||
| 21 | } | ||
| 22 | |||
| 23 | tt, kbd, code { | ||
| 24 | font-size: 12pt ; | ||
| 25 | } | ||
| 26 | </STYLE> | ||
| 27 | </HEAD> | ||
| 28 | |||
| 29 | <BODY> | ||
| 30 | |||
| 31 | <H1> | ||
| 32 | <A HREF="https://www.lua.org/"><IMG SRC="logo.gif" ALT="Lua"></A> | ||
| 33 | Welcome to Lua 5.4 | ||
| 34 | </H1> | ||
| 35 | |||
| 36 | <DIV CLASS="menubar"> | ||
| 37 | <A HREF="#about">about</A> | ||
| 38 | · | ||
| 39 | <A HREF="#install">installation</A> | ||
| 40 | · | ||
| 41 | <A HREF="#changes">changes</A> | ||
| 42 | · | ||
| 43 | <A HREF="#license">license</A> | ||
| 44 | · | ||
| 45 | <A HREF="contents.html">reference manual</A> | ||
| 46 | </DIV> | ||
| 47 | |||
| 48 | <H2><A NAME="about">About Lua</A></H2> | ||
| 49 | <P> | ||
| 50 | Lua is a powerful, efficient, lightweight, embeddable scripting language | ||
| 51 | developed by a | ||
| 52 | <A HREF="https://www.lua.org/authors.html">team</A> | ||
| 53 | at | ||
| 54 | <A HREF="https://www.puc-rio.br/">PUC-Rio</A>, | ||
| 55 | the Pontifical Catholic University of Rio de Janeiro in Brazil. | ||
| 56 | Lua is | ||
| 57 | <A HREF="#license">free software</A> | ||
| 58 | used in | ||
| 59 | <A HREF="https://www.lua.org/uses.html">many products and projects</A> | ||
| 60 | around the world. | ||
| 61 | |||
| 62 | <P> | ||
| 63 | Lua's | ||
| 64 | <A HREF="https://www.lua.org/">official website</A> | ||
| 65 | provides complete information | ||
| 66 | about Lua, | ||
| 67 | including | ||
| 68 | an | ||
| 69 | <A HREF="https://www.lua.org/about.html">executive summary</A>, | ||
| 70 | tips on | ||
| 71 | <A HREF="https://www.lua.org/start.html">getting started</A>, | ||
| 72 | and | ||
| 73 | updated | ||
| 74 | <A HREF="https://www.lua.org/docs.html">documentation</A>, | ||
| 75 | especially the | ||
| 76 | <A HREF="https://www.lua.org/manual/5.4/">reference manual</A>, | ||
| 77 | which may differ slightly from the | ||
| 78 | <A HREF="contents.html">local copy</A> | ||
| 79 | distributed in this package. | ||
| 80 | |||
| 81 | <H2><A NAME="install">Installing Lua</A></H2> | ||
| 82 | <P> | ||
| 83 | Lua is distributed in | ||
| 84 | <A HREF="https://www.lua.org/ftp/">source</A> | ||
| 85 | form. | ||
| 86 | You need to build it before using it. | ||
| 87 | Building Lua should be straightforward | ||
| 88 | because | ||
| 89 | Lua is implemented in pure ANSI C and compiles unmodified in all known | ||
| 90 | platforms that have an ANSI C compiler. | ||
| 91 | Lua also compiles unmodified as C++. | ||
| 92 | The instructions given below for building Lua are for Unix-like platforms, | ||
| 93 | such as Linux and macOS. | ||
| 94 | See also | ||
| 95 | <A HREF="#other">instructions for other systems</A> | ||
| 96 | and | ||
| 97 | <A HREF="#customization">customization options</A>. | ||
| 98 | |||
| 99 | <P> | ||
| 100 | If you don't have the time or the inclination to compile Lua yourself, | ||
| 101 | get a binary from | ||
| 102 | <A HREF="https://luabinaries.sourceforge.net">LuaBinaries</A>. | ||
| 103 | |||
| 104 | <H3>Building Lua</H3> | ||
| 105 | <P> | ||
| 106 | In most common Unix-like platforms, simply do "<KBD>make</KBD>". | ||
| 107 | Here are the details. | ||
| 108 | |||
| 109 | <OL> | ||
| 110 | <LI> | ||
| 111 | Open a terminal window and move to | ||
| 112 | the top-level directory, which is named <TT>lua-5.4.8</TT>. | ||
| 113 | The <TT>Makefile</TT> there controls both the build process and the installation process. | ||
| 114 | <P> | ||
| 115 | <LI> | ||
| 116 | Do "<KBD>make</KBD>". The <TT>Makefile</TT> will guess your platform and build Lua for it. | ||
| 117 | <P> | ||
| 118 | <LI> | ||
| 119 | If the guess failed, do "<KBD>make help</KBD>" and see if your platform is listed. | ||
| 120 | The platforms currently supported are: | ||
| 121 | <P> | ||
| 122 | <P CLASS="display"> | ||
| 123 | guess aix bsd c89 freebsd generic ios linux linux-readline macosx mingw posix solaris | ||
| 124 | </P> | ||
| 125 | <P> | ||
| 126 | If your platform is listed, just do "<KBD>make xxx</KBD>", where xxx | ||
| 127 | is your platform name. | ||
| 128 | <P> | ||
| 129 | If your platform is not listed, try the closest one or posix, generic, | ||
| 130 | c89, in this order. | ||
| 131 | <P> | ||
| 132 | <LI> | ||
| 133 | The compilation takes only a few moments | ||
| 134 | and produces three files in the <TT>src</TT> directory: | ||
| 135 | lua (the interpreter), | ||
| 136 | luac (the compiler), | ||
| 137 | and liblua.a (the library). | ||
| 138 | <P> | ||
| 139 | <LI> | ||
| 140 | To check that Lua has been built correctly, do "<KBD>make test</KBD>" | ||
| 141 | after building Lua. This will run the interpreter and print its version. | ||
| 142 | </OL> | ||
| 143 | <P> | ||
| 144 | If you're running Linux, try "<KBD>make linux-readline</KBD>" to build the interactive Lua interpreter with handy line-editing and history capabilities. | ||
| 145 | If you get compilation errors, | ||
| 146 | make sure you have installed the <TT>readline</TT> development package | ||
| 147 | (which is probably named <TT>libreadline-dev</TT> or <TT>readline-devel</TT>). | ||
| 148 | If you get link errors after that, | ||
| 149 | then try "<KBD>make linux-readline MYLIBS=-ltermcap</KBD>". | ||
| 150 | |||
| 151 | <H3>Installing Lua</H3> | ||
| 152 | <P> | ||
| 153 | Once you have built Lua, you may want to install it in an official | ||
| 154 | place in your system. In this case, do "<KBD>make install</KBD>". The official | ||
| 155 | place and the way to install files are defined in the <TT>Makefile</TT>. You'll | ||
| 156 | probably need the right permissions to install files, and so may need to do "<KBD>sudo make install</KBD>". | ||
| 157 | |||
| 158 | <P> | ||
| 159 | To build and install Lua in one step, do "<KBD>make all install</KBD>", | ||
| 160 | or "<KBD>make xxx install</KBD>", | ||
| 161 | where xxx is your platform name. | ||
| 162 | |||
| 163 | <P> | ||
| 164 | To install Lua locally after building it, do "<KBD>make local</KBD>". | ||
| 165 | This will create a directory <TT>install</TT> with subdirectories | ||
| 166 | <TT>bin</TT>, <TT>include</TT>, <TT>lib</TT>, <TT>man</TT>, <TT>share</TT>, | ||
| 167 | and install Lua as listed below. | ||
| 168 | |||
| 169 | To install Lua locally, but in some other directory, do | ||
| 170 | "<KBD>make install INSTALL_TOP=xxx</KBD>", where xxx is your chosen directory. | ||
| 171 | The installation starts in the <TT>src</TT> and <TT>doc</TT> directories, | ||
| 172 | so take care if <TT>INSTALL_TOP</TT> is not an absolute path. | ||
| 173 | |||
| 174 | <DL CLASS="display"> | ||
| 175 | <DT> | ||
| 176 | bin: | ||
| 177 | <DD> | ||
| 178 | lua luac | ||
| 179 | <DT> | ||
| 180 | include: | ||
| 181 | <DD> | ||
| 182 | lua.h luaconf.h lualib.h lauxlib.h lua.hpp | ||
| 183 | <DT> | ||
| 184 | lib: | ||
| 185 | <DD> | ||
| 186 | liblua.a | ||
| 187 | <DT> | ||
| 188 | man/man1: | ||
| 189 | <DD> | ||
| 190 | lua.1 luac.1 | ||
| 191 | </DL> | ||
| 192 | |||
| 193 | <P> | ||
| 194 | These are the only directories you need for development. | ||
| 195 | If you only want to run Lua programs, | ||
| 196 | you only need the files in <TT>bin</TT> and <TT>man</TT>. | ||
| 197 | The files in <TT>include</TT> and <TT>lib</TT> are needed for | ||
| 198 | embedding Lua in C or C++ programs. | ||
| 199 | |||
| 200 | <H3><A NAME="customization">Customization</A></H3> | ||
| 201 | <P> | ||
| 202 | Three kinds of things can be customized by editing a file: | ||
| 203 | <UL> | ||
| 204 | <LI> Where and how to install Lua — edit <TT>Makefile</TT>. | ||
| 205 | <LI> How to build Lua — edit <TT>src/Makefile</TT>. | ||
| 206 | <LI> Lua features — edit <TT>src/luaconf.h</TT>. | ||
| 207 | </UL> | ||
| 208 | |||
| 209 | <P> | ||
| 210 | You don't actually need to edit the Makefiles because you may set the | ||
| 211 | relevant variables in the command line when invoking make. | ||
| 212 | Nevertheless, it's probably best to edit and save the Makefiles to | ||
| 213 | record the changes you've made. | ||
| 214 | |||
| 215 | <P> | ||
| 216 | On the other hand, if you need to customize some Lua features, | ||
| 217 | edit <TT>src/luaconf.h</TT> before building and installing Lua. | ||
| 218 | The edited file will be the one installed, and | ||
| 219 | it will be used by any Lua clients that you build, to ensure consistency. | ||
| 220 | Further customization is available to experts by editing the Lua sources. | ||
| 221 | |||
| 222 | <H3><A NAME="other">Building Lua on other systems</A></H3> | ||
| 223 | <P> | ||
| 224 | If you're not using the usual Unix tools, then the instructions for | ||
| 225 | building Lua depend on the compiler you use. You'll need to create | ||
| 226 | projects (or whatever your compiler uses) for building the library, | ||
| 227 | the interpreter, and the compiler, as follows: | ||
| 228 | |||
| 229 | <DL CLASS="display"> | ||
| 230 | <DT> | ||
| 231 | library: | ||
| 232 | <DD> | ||
| 233 | lapi.c lcode.c lctype.c ldebug.c ldo.c ldump.c lfunc.c lgc.c llex.c lmem.c lobject.c lopcodes.c lparser.c lstate.c lstring.c ltable.c ltm.c lundump.c lvm.c lzio.c | ||
| 234 | lauxlib.c lbaselib.c lcorolib.c ldblib.c liolib.c lmathlib.c loadlib.c loslib.c lstrlib.c ltablib.c lutf8lib.c linit.c | ||
| 235 | <DT> | ||
| 236 | interpreter: | ||
| 237 | <DD> | ||
| 238 | library, lua.c | ||
| 239 | <DT> | ||
| 240 | compiler: | ||
| 241 | <DD> | ||
| 242 | library, luac.c | ||
| 243 | </DL> | ||
| 244 | |||
| 245 | <P> | ||
| 246 | To use Lua as a library in your own programs, you need to know how to | ||
| 247 | create and use libraries with your compiler. Moreover, to dynamically load | ||
| 248 | C libraries for Lua, you'll need to know how to create dynamic libraries | ||
| 249 | and you'll need to make sure that the Lua API functions are accessible to | ||
| 250 | those dynamic libraries — but <EM>don't</EM> link the Lua library | ||
| 251 | into each dynamic library. For Unix, we recommend that the Lua library | ||
| 252 | be linked statically into the host program and its symbols exported for | ||
| 253 | dynamic linking; <TT>src/Makefile</TT> does this for the Lua interpreter. | ||
| 254 | For Windows, we recommend that the Lua library be a DLL. | ||
| 255 | In all cases, the compiler luac should be linked statically. | ||
| 256 | |||
| 257 | <P> | ||
| 258 | As mentioned above, you may edit <TT>src/luaconf.h</TT> to customize | ||
| 259 | some features before building Lua. | ||
| 260 | |||
| 261 | <H2><A NAME="changes">Changes since Lua 5.3</A></H2> | ||
| 262 | <P> | ||
| 263 | Here are the main changes introduced in Lua 5.4. | ||
| 264 | The | ||
| 265 | <A HREF="contents.html">reference manual</A> | ||
| 266 | lists the | ||
| 267 | <A HREF="manual.html#8">incompatibilities</A> that had to be introduced. | ||
| 268 | |||
| 269 | <H3>Main changes</H3> | ||
| 270 | <UL> | ||
| 271 | <LI> new generational mode for garbage collection | ||
| 272 | <LI> to-be-closed variables | ||
| 273 | <LI> const variables | ||
| 274 | <LI> userdata can have multiple user values | ||
| 275 | <LI> new implementation for math.random | ||
| 276 | <LI> warning system | ||
| 277 | <LI> debug information about function arguments and returns | ||
| 278 | <LI> new semantics for the integer 'for' loop | ||
| 279 | <LI> optional 'init' argument to 'string.gmatch' | ||
| 280 | <LI> new functions 'lua_resetthread' and 'coroutine.close' | ||
| 281 | <LI> string-to-number coercions moved to the string library | ||
| 282 | <LI> allocation function allowed to fail when shrinking a memory block | ||
| 283 | <LI> new format '%p' in 'string.format' | ||
| 284 | <LI> utf8 library accepts codepoints up to 2^31 | ||
| 285 | </UL> | ||
| 286 | |||
| 287 | <H2><A NAME="license">License</A></H2> | ||
| 288 | <P> | ||
| 289 | <A HREF="https://opensource.org/osd"> | ||
| 290 | <IMG SRC="OSIApproved_100X125.png" ALIGN="right" ALT="[Open Source Initiative Approved License]" STYLE="padding-left: 1em" WIDTH=50> | ||
| 291 | </A> | ||
| 292 | Lua is free software distributed under the terms of the | ||
| 293 | <A HREF="https://opensource.org/license/mit">MIT license</A> | ||
| 294 | reproduced below; | ||
| 295 | it may be used for any purpose, including commercial purposes, | ||
| 296 | at absolutely no cost without having to ask us. | ||
| 297 | |||
| 298 | The only requirement is that if you do use Lua, | ||
| 299 | then you should give us credit by including the appropriate copyright notice somewhere in your product or its documentation. | ||
| 300 | |||
| 301 | For details, see the | ||
| 302 | <A HREF="https://www.lua.org/license.html">license page</A>. | ||
| 303 | |||
| 304 | <BLOCKQUOTE STYLE="padding-bottom: 0em"> | ||
| 305 | Copyright © 1994–2025 Lua.org, PUC-Rio. | ||
| 306 | |||
| 307 | <P> | ||
| 308 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 309 | of this software and associated documentation files (the "Software"), to deal | ||
| 310 | in the Software without restriction, including without limitation the rights | ||
| 311 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 312 | copies of the Software, and to permit persons to whom the Software is | ||
| 313 | furnished to do so, subject to the following conditions: | ||
| 314 | |||
| 315 | <P> | ||
| 316 | The above copyright notice and this permission notice shall be included in | ||
| 317 | all copies or substantial portions of the Software. | ||
| 318 | |||
| 319 | <P> | ||
| 320 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 321 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 322 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 323 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 324 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 325 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| 326 | THE SOFTWARE. | ||
| 327 | </BLOCKQUOTE> | ||
| 328 | <P> | ||
| 329 | |||
| 330 | <P CLASS="footer"> | ||
| 331 | Last update: | ||
| 332 | Wed May 21 21:12:01 UTC 2025 | ||
| 333 | </P> | ||
| 334 | <!-- | ||
| 335 | Last change: revised for Lua 5.4.8 | ||
| 336 | --> | ||
| 337 | |||
| 338 | </BODY> | ||
| 339 | </HTML> | ||
