1*088332b5SXin Li<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 2*088332b5SXin Li<HTML> 3*088332b5SXin Li<HEAD> 4*088332b5SXin Li<TITLE>Lua 5.4 Reference Manual</TITLE> 5*088332b5SXin Li<LINK REL="stylesheet" TYPE="text/css" HREF="lua.css"> 6*088332b5SXin Li<LINK REL="stylesheet" TYPE="text/css" HREF="manual.css"> 7*088332b5SXin Li<META HTTP-EQUIV="content-type" CONTENT="text/html; charset=iso-8859-1"> 8*088332b5SXin Li</HEAD> 9*088332b5SXin Li 10*088332b5SXin Li<BODY> 11*088332b5SXin Li 12*088332b5SXin Li<H1> 13*088332b5SXin Li<A HREF="http://www.lua.org/"><IMG SRC="logo.gif" ALT="Lua"></A> 14*088332b5SXin LiLua 5.4 Reference Manual 15*088332b5SXin Li</H1> 16*088332b5SXin Li 17*088332b5SXin Li<P> 18*088332b5SXin Liby Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes 19*088332b5SXin Li 20*088332b5SXin Li<P> 21*088332b5SXin Li<SMALL> 22*088332b5SXin LiCopyright © 2020 Lua.org, PUC-Rio. 23*088332b5SXin LiFreely available under the terms of the 24*088332b5SXin Li<a href="http://www.lua.org/license.html">Lua license</a>. 25*088332b5SXin Li</SMALL> 26*088332b5SXin Li 27*088332b5SXin Li<DIV CLASS="menubar"> 28*088332b5SXin Li<A HREF="contents.html#contents">contents</A> 29*088332b5SXin Li· 30*088332b5SXin Li<A HREF="contents.html#index">index</A> 31*088332b5SXin Li· 32*088332b5SXin Li<A HREF="http://www.lua.org/manual/">other versions</A> 33*088332b5SXin Li</DIV> 34*088332b5SXin Li 35*088332b5SXin Li<!-- ====================================================================== --> 36*088332b5SXin Li<p> 37*088332b5SXin Li 38*088332b5SXin Li<!-- $Id: manual.of $ --> 39*088332b5SXin Li 40*088332b5SXin Li 41*088332b5SXin Li 42*088332b5SXin Li 43*088332b5SXin Li<h1>1 – <a name="1">Introduction</a></h1> 44*088332b5SXin Li 45*088332b5SXin Li<p> 46*088332b5SXin LiLua is a powerful, efficient, lightweight, embeddable scripting language. 47*088332b5SXin LiIt supports procedural programming, 48*088332b5SXin Liobject-oriented programming, functional programming, 49*088332b5SXin Lidata-driven programming, and data description. 50*088332b5SXin Li 51*088332b5SXin Li 52*088332b5SXin Li<p> 53*088332b5SXin LiLua combines simple procedural syntax with powerful data description 54*088332b5SXin Liconstructs based on associative arrays and extensible semantics. 55*088332b5SXin LiLua is dynamically typed, 56*088332b5SXin Liruns by interpreting bytecode with a register-based 57*088332b5SXin Livirtual machine, 58*088332b5SXin Liand has automatic memory management with 59*088332b5SXin Lia generational garbage collection, 60*088332b5SXin Limaking it ideal for configuration, scripting, 61*088332b5SXin Liand rapid prototyping. 62*088332b5SXin Li 63*088332b5SXin Li 64*088332b5SXin Li<p> 65*088332b5SXin LiLua is implemented as a library, written in <em>clean C</em>, 66*088332b5SXin Lithe common subset of Standard C and C++. 67*088332b5SXin LiThe Lua distribution includes a host program called <code>lua</code>, 68*088332b5SXin Liwhich uses the Lua library to offer a complete, 69*088332b5SXin Listandalone Lua interpreter, 70*088332b5SXin Lifor interactive or batch use. 71*088332b5SXin LiLua is intended to be used both as a powerful, lightweight, 72*088332b5SXin Liembeddable scripting language for any program that needs one, 73*088332b5SXin Liand as a powerful but lightweight and efficient stand-alone language. 74*088332b5SXin Li 75*088332b5SXin Li 76*088332b5SXin Li<p> 77*088332b5SXin LiAs an extension language, Lua has no notion of a "main" program: 78*088332b5SXin Liit works <em>embedded</em> in a host client, 79*088332b5SXin Licalled the <em>embedding program</em> or simply the <em>host</em>. 80*088332b5SXin Li(Frequently, this host is the stand-alone <code>lua</code> program.) 81*088332b5SXin LiThe host program can invoke functions to execute a piece of Lua code, 82*088332b5SXin Lican write and read Lua variables, 83*088332b5SXin Liand can register C functions to be called by Lua code. 84*088332b5SXin LiThrough the use of C functions, Lua can be augmented to cope with 85*088332b5SXin Lia wide range of different domains, 86*088332b5SXin Lithus creating customized programming languages sharing a syntactical framework. 87*088332b5SXin Li 88*088332b5SXin Li 89*088332b5SXin Li<p> 90*088332b5SXin LiLua is free software, 91*088332b5SXin Liand is provided as usual with no guarantees, 92*088332b5SXin Lias stated in its license. 93*088332b5SXin LiThe implementation described in this manual is available 94*088332b5SXin Liat Lua's official web site, <code>www.lua.org</code>. 95*088332b5SXin Li 96*088332b5SXin Li 97*088332b5SXin Li<p> 98*088332b5SXin LiLike any other reference manual, 99*088332b5SXin Lithis document is dry in places. 100*088332b5SXin LiFor a discussion of the decisions behind the design of Lua, 101*088332b5SXin Lisee the technical papers available at Lua's web site. 102*088332b5SXin LiFor a detailed introduction to programming in Lua, 103*088332b5SXin Lisee Roberto's book, <em>Programming in Lua</em>. 104*088332b5SXin Li 105*088332b5SXin Li 106*088332b5SXin Li 107*088332b5SXin Li<h1>2 – <a name="2">Basic Concepts</a></h1> 108*088332b5SXin Li 109*088332b5SXin Li 110*088332b5SXin Li 111*088332b5SXin Li<p> 112*088332b5SXin LiThis section describes the basic concepts of the language. 113*088332b5SXin Li 114*088332b5SXin Li 115*088332b5SXin Li 116*088332b5SXin Li 117*088332b5SXin Li 118*088332b5SXin Li<h2>2.1 – <a name="2.1">Values and Types</a></h2> 119*088332b5SXin Li 120*088332b5SXin Li<p> 121*088332b5SXin LiLua is a dynamically typed language. 122*088332b5SXin LiThis means that 123*088332b5SXin Livariables do not have types; only values do. 124*088332b5SXin LiThere are no type definitions in the language. 125*088332b5SXin LiAll values carry their own type. 126*088332b5SXin Li 127*088332b5SXin Li 128*088332b5SXin Li<p> 129*088332b5SXin LiAll values in Lua are first-class values. 130*088332b5SXin LiThis means that all values can be stored in variables, 131*088332b5SXin Lipassed as arguments to other functions, and returned as results. 132*088332b5SXin Li 133*088332b5SXin Li 134*088332b5SXin Li<p> 135*088332b5SXin LiThere are eight basic types in Lua: 136*088332b5SXin Li<em>nil</em>, <em>boolean</em>, <em>number</em>, 137*088332b5SXin Li<em>string</em>, <em>function</em>, <em>userdata</em>, 138*088332b5SXin Li<em>thread</em>, and <em>table</em>. 139*088332b5SXin LiThe type <em>nil</em> has one single value, <b>nil</b>, 140*088332b5SXin Liwhose main property is to be different from any other value; 141*088332b5SXin Liit often represents the absence of a useful value. 142*088332b5SXin LiThe type <em>boolean</em> has two values, <b>false</b> and <b>true</b>. 143*088332b5SXin LiBoth <b>nil</b> and <b>false</b> make a condition false; 144*088332b5SXin Lithey are collectively called <em>false values</em>. 145*088332b5SXin LiAny other value makes a condition true. 146*088332b5SXin Li 147*088332b5SXin Li 148*088332b5SXin Li<p> 149*088332b5SXin LiThe type <em>number</em> represents both 150*088332b5SXin Liinteger numbers and real (floating-point) numbers, 151*088332b5SXin Liusing two subtypes: <em>integer</em> and <em>float</em>. 152*088332b5SXin LiStandard Lua uses 64-bit integers and double-precision (64-bit) floats, 153*088332b5SXin Libut you can also compile Lua so that it 154*088332b5SXin Liuses 32-bit integers and/or single-precision (32-bit) floats. 155*088332b5SXin LiThe option with 32 bits for both integers and floats 156*088332b5SXin Liis particularly attractive 157*088332b5SXin Lifor small machines and embedded systems. 158*088332b5SXin Li(See macro <code>LUA_32BITS</code> in file <code>luaconf.h</code>.) 159*088332b5SXin Li 160*088332b5SXin Li 161*088332b5SXin Li<p> 162*088332b5SXin LiUnless stated otherwise, 163*088332b5SXin Liany overflow when manipulating integer values <em>wrap around</em>, 164*088332b5SXin Liaccording to the usual rules of two-complement arithmetic. 165*088332b5SXin Li(In other words, 166*088332b5SXin Lithe actual result is the unique representable integer 167*088332b5SXin Lithat is equal modulo <em>2<sup>n</sup></em> to the mathematical result, 168*088332b5SXin Liwhere <em>n</em> is the number of bits of the integer type.) 169*088332b5SXin Li 170*088332b5SXin Li 171*088332b5SXin Li<p> 172*088332b5SXin LiLua has explicit rules about when each subtype is used, 173*088332b5SXin Libut it also converts between them automatically as needed (see <a href="#3.4.3">§3.4.3</a>). 174*088332b5SXin LiTherefore, 175*088332b5SXin Lithe programmer may choose to mostly ignore the difference 176*088332b5SXin Libetween integers and floats 177*088332b5SXin Lior to assume complete control over the representation of each number. 178*088332b5SXin Li 179*088332b5SXin Li 180*088332b5SXin Li<p> 181*088332b5SXin LiThe type <em>string</em> represents immutable sequences of bytes. 182*088332b5SXin Li 183*088332b5SXin LiLua is 8-bit clean: 184*088332b5SXin Listrings can contain any 8-bit value, 185*088332b5SXin Liincluding embedded zeros ('<code>\0</code>'). 186*088332b5SXin LiLua is also encoding-agnostic; 187*088332b5SXin Liit makes no assumptions about the contents of a string. 188*088332b5SXin LiThe length of any string in Lua must fit in a Lua integer. 189*088332b5SXin Li 190*088332b5SXin Li 191*088332b5SXin Li<p> 192*088332b5SXin LiLua can call (and manipulate) functions written in Lua and 193*088332b5SXin Lifunctions written in C (see <a href="#3.4.10">§3.4.10</a>). 194*088332b5SXin LiBoth are represented by the type <em>function</em>. 195*088332b5SXin Li 196*088332b5SXin Li 197*088332b5SXin Li<p> 198*088332b5SXin LiThe type <em>userdata</em> is provided to allow arbitrary C data to 199*088332b5SXin Libe stored in Lua variables. 200*088332b5SXin LiA userdata value represents a block of raw memory. 201*088332b5SXin LiThere are two kinds of userdata: 202*088332b5SXin Li<em>full userdata</em>, 203*088332b5SXin Liwhich is an object with a block of memory managed by Lua, 204*088332b5SXin Liand <em>light userdata</em>, 205*088332b5SXin Liwhich is simply a C pointer value. 206*088332b5SXin LiUserdata has no predefined operations in Lua, 207*088332b5SXin Liexcept assignment and identity test. 208*088332b5SXin LiBy using <em>metatables</em>, 209*088332b5SXin Lithe programmer can define operations for full userdata values 210*088332b5SXin Li(see <a href="#2.4">§2.4</a>). 211*088332b5SXin LiUserdata values cannot be created or modified in Lua, 212*088332b5SXin Lionly through the C API. 213*088332b5SXin LiThis guarantees the integrity of data owned by 214*088332b5SXin Lithe host program and C libraries. 215*088332b5SXin Li 216*088332b5SXin Li 217*088332b5SXin Li<p> 218*088332b5SXin LiThe type <em>thread</em> represents independent threads of execution 219*088332b5SXin Liand it is used to implement coroutines (see <a href="#2.6">§2.6</a>). 220*088332b5SXin LiLua threads are not related to operating-system threads. 221*088332b5SXin LiLua supports coroutines on all systems, 222*088332b5SXin Lieven those that do not support threads natively. 223*088332b5SXin Li 224*088332b5SXin Li 225*088332b5SXin Li<p> 226*088332b5SXin LiThe type <em>table</em> implements associative arrays, 227*088332b5SXin Lithat is, arrays that can have as indices not only numbers, 228*088332b5SXin Libut any Lua value except <b>nil</b> and NaN. 229*088332b5SXin Li(<em>Not a Number</em> is a special floating-point value 230*088332b5SXin Liused by the IEEE 754 standard to represent 231*088332b5SXin Liundefined numerical results, such as <code>0/0</code>.) 232*088332b5SXin LiTables can be <em>heterogeneous</em>; 233*088332b5SXin Lithat is, they can contain values of all types (except <b>nil</b>). 234*088332b5SXin LiAny key associated to the value <b>nil</b> is not considered part of the table. 235*088332b5SXin LiConversely, any key that is not part of a table has 236*088332b5SXin Lian associated value <b>nil</b>. 237*088332b5SXin Li 238*088332b5SXin Li 239*088332b5SXin Li<p> 240*088332b5SXin LiTables are the sole data-structuring mechanism in Lua; 241*088332b5SXin Lithey can be used to represent ordinary arrays, lists, 242*088332b5SXin Lisymbol tables, sets, records, graphs, trees, etc. 243*088332b5SXin LiTo represent records, Lua uses the field name as an index. 244*088332b5SXin LiThe language supports this representation by 245*088332b5SXin Liproviding <code>a.name</code> as syntactic sugar for <code>a["name"]</code>. 246*088332b5SXin LiThere are several convenient ways to create tables in Lua 247*088332b5SXin Li(see <a href="#3.4.9">§3.4.9</a>). 248*088332b5SXin Li 249*088332b5SXin Li 250*088332b5SXin Li<p> 251*088332b5SXin LiLike indices, 252*088332b5SXin Lithe values of table fields can be of any type. 253*088332b5SXin LiIn particular, 254*088332b5SXin Libecause functions are first-class values, 255*088332b5SXin Litable fields can contain functions. 256*088332b5SXin LiThus tables can also carry <em>methods</em> (see <a href="#3.4.11">§3.4.11</a>). 257*088332b5SXin Li 258*088332b5SXin Li 259*088332b5SXin Li<p> 260*088332b5SXin LiThe indexing of tables follows 261*088332b5SXin Lithe definition of raw equality in the language. 262*088332b5SXin LiThe expressions <code>a[i]</code> and <code>a[j]</code> 263*088332b5SXin Lidenote the same table element 264*088332b5SXin Liif and only if <code>i</code> and <code>j</code> are raw equal 265*088332b5SXin Li(that is, equal without metamethods). 266*088332b5SXin LiIn particular, floats with integral values 267*088332b5SXin Liare equal to their respective integers 268*088332b5SXin Li(e.g., <code>1.0 == 1</code>). 269*088332b5SXin LiTo avoid ambiguities, 270*088332b5SXin Liany float used as a key that is equal to an integer 271*088332b5SXin Liis converted to that integer. 272*088332b5SXin LiFor instance, if you write <code>a[2.0] = true</code>, 273*088332b5SXin Lithe actual key inserted into the table will be the integer <code>2</code>. 274*088332b5SXin Li 275*088332b5SXin Li 276*088332b5SXin Li<p> 277*088332b5SXin LiTables, functions, threads, and (full) userdata values are <em>objects</em>: 278*088332b5SXin Livariables do not actually <em>contain</em> these values, 279*088332b5SXin Lionly <em>references</em> to them. 280*088332b5SXin LiAssignment, parameter passing, and function returns 281*088332b5SXin Lialways manipulate references to such values; 282*088332b5SXin Lithese operations do not imply any kind of copy. 283*088332b5SXin Li 284*088332b5SXin Li 285*088332b5SXin Li<p> 286*088332b5SXin LiThe library function <a href="#pdf-type"><code>type</code></a> returns a string describing the type 287*088332b5SXin Liof a given value (see <a href="#pdf-type"><code>type</code></a>). 288*088332b5SXin Li 289*088332b5SXin Li 290*088332b5SXin Li 291*088332b5SXin Li 292*088332b5SXin Li 293*088332b5SXin Li<h2>2.2 – <a name="2.2">Environments and the Global Environment</a></h2> 294*088332b5SXin Li 295*088332b5SXin Li<p> 296*088332b5SXin LiAs we will discuss further in <a href="#3.2">§3.2</a> and <a href="#3.3.3">§3.3.3</a>, 297*088332b5SXin Liany reference to a free name 298*088332b5SXin Li(that is, a name not bound to any declaration) <code>var</code> 299*088332b5SXin Liis syntactically translated to <code>_ENV.var</code>. 300*088332b5SXin LiMoreover, every chunk is compiled in the scope of 301*088332b5SXin Lian external local variable named <code>_ENV</code> (see <a href="#3.3.2">§3.3.2</a>), 302*088332b5SXin Liso <code>_ENV</code> itself is never a free name in a chunk. 303*088332b5SXin Li 304*088332b5SXin Li 305*088332b5SXin Li<p> 306*088332b5SXin LiDespite the existence of this external <code>_ENV</code> variable and 307*088332b5SXin Lithe translation of free names, 308*088332b5SXin Li<code>_ENV</code> is a completely regular name. 309*088332b5SXin LiIn particular, 310*088332b5SXin Liyou can define new variables and parameters with that name. 311*088332b5SXin LiEach reference to a free name uses the <code>_ENV</code> that is 312*088332b5SXin Livisible at that point in the program, 313*088332b5SXin Lifollowing the usual visibility rules of Lua (see <a href="#3.5">§3.5</a>). 314*088332b5SXin Li 315*088332b5SXin Li 316*088332b5SXin Li<p> 317*088332b5SXin LiAny table used as the value of <code>_ENV</code> is called an <em>environment</em>. 318*088332b5SXin Li 319*088332b5SXin Li 320*088332b5SXin Li<p> 321*088332b5SXin LiLua keeps a distinguished environment called the <em>global environment</em>. 322*088332b5SXin LiThis value is kept at a special index in the C registry (see <a href="#4.3">§4.3</a>). 323*088332b5SXin LiIn Lua, the global variable <a href="#pdf-_G"><code>_G</code></a> is initialized with this same value. 324*088332b5SXin Li(<a href="#pdf-_G"><code>_G</code></a> is never used internally, 325*088332b5SXin Liso changing its value will affect only your own code.) 326*088332b5SXin Li 327*088332b5SXin Li 328*088332b5SXin Li<p> 329*088332b5SXin LiWhen Lua loads a chunk, 330*088332b5SXin Lithe default value for its <code>_ENV</code> variable 331*088332b5SXin Liis the global environment (see <a href="#pdf-load"><code>load</code></a>). 332*088332b5SXin LiTherefore, by default, 333*088332b5SXin Lifree names in Lua code refer to entries in the global environment 334*088332b5SXin Liand, therefore, they are also called <em>global variables</em>. 335*088332b5SXin LiMoreover, all standard libraries are loaded in the global environment 336*088332b5SXin Liand some functions there operate on that environment. 337*088332b5SXin LiYou can use <a href="#pdf-load"><code>load</code></a> (or <a href="#pdf-loadfile"><code>loadfile</code></a>) 338*088332b5SXin Lito load a chunk with a different environment. 339*088332b5SXin Li(In C, you have to load the chunk and then change the value 340*088332b5SXin Liof its first upvalue; see <a href="#lua_setupvalue"><code>lua_setupvalue</code></a>.) 341*088332b5SXin Li 342*088332b5SXin Li 343*088332b5SXin Li 344*088332b5SXin Li 345*088332b5SXin Li 346*088332b5SXin Li<h2>2.3 – <a name="2.3">Error Handling</a></h2> 347*088332b5SXin Li 348*088332b5SXin Li<p> 349*088332b5SXin LiSeveral operations in Lua can <em>raise</em> an error. 350*088332b5SXin LiAn error interrupts the normal flow of the program, 351*088332b5SXin Liwhich can continue by <em>catching</em> the error. 352*088332b5SXin Li 353*088332b5SXin Li 354*088332b5SXin Li<p> 355*088332b5SXin LiLua code can explicitly raise an error by calling the 356*088332b5SXin Li<a href="#pdf-error"><code>error</code></a> function. 357*088332b5SXin Li(This function never returns.) 358*088332b5SXin Li 359*088332b5SXin Li 360*088332b5SXin Li<p> 361*088332b5SXin LiTo catch errors in Lua, 362*088332b5SXin Liyou can do a <em>protected call</em>, 363*088332b5SXin Liusing <a href="#pdf-pcall"><code>pcall</code></a> (or <a href="#pdf-xpcall"><code>xpcall</code></a>). 364*088332b5SXin LiThe function <a href="#pdf-pcall"><code>pcall</code></a> calls a given function in <em>protected mode</em>. 365*088332b5SXin LiAny error while running the function stops its execution, 366*088332b5SXin Liand control returns immediately to <code>pcall</code>, 367*088332b5SXin Liwhich returns a status code. 368*088332b5SXin Li 369*088332b5SXin Li 370*088332b5SXin Li<p> 371*088332b5SXin LiBecause Lua is an embedded extension language, 372*088332b5SXin LiLua code starts running by a call 373*088332b5SXin Lifrom C code in the host program. 374*088332b5SXin Li(When you use Lua standalone, 375*088332b5SXin Lithe <code>lua</code> application is the host program.) 376*088332b5SXin LiUsually, this call is protected; 377*088332b5SXin Liso, when an otherwise unprotected error occurs during 378*088332b5SXin Lithe compilation or execution of a Lua chunk, 379*088332b5SXin Licontrol returns to the host, 380*088332b5SXin Liwhich can take appropriate measures, 381*088332b5SXin Lisuch as printing an error message. 382*088332b5SXin Li 383*088332b5SXin Li 384*088332b5SXin Li<p> 385*088332b5SXin LiWhenever there is an error, 386*088332b5SXin Lian <em>error object</em> 387*088332b5SXin Liis propagated with information about the error. 388*088332b5SXin LiLua itself only generates errors whose error object is a string, 389*088332b5SXin Libut programs may generate errors with 390*088332b5SXin Liany value as the error object. 391*088332b5SXin LiIt is up to the Lua program or its host to handle such error objects. 392*088332b5SXin LiFor historical reasons, 393*088332b5SXin Lian error object is often called an <em>error message</em>, 394*088332b5SXin Lieven though it does not have to be a string. 395*088332b5SXin Li 396*088332b5SXin Li 397*088332b5SXin Li<p> 398*088332b5SXin LiWhen you use <a href="#pdf-xpcall"><code>xpcall</code></a> (or <a href="#lua_pcall"><code>lua_pcall</code></a>, in C) 399*088332b5SXin Liyou may give a <em>message handler</em> 400*088332b5SXin Lito be called in case of errors. 401*088332b5SXin LiThis function is called with the original error object 402*088332b5SXin Liand returns a new error object. 403*088332b5SXin LiIt is called before the error unwinds the stack, 404*088332b5SXin Liso that it can gather more information about the error, 405*088332b5SXin Lifor instance by inspecting the stack and creating a stack traceback. 406*088332b5SXin LiThis message handler is still protected by the protected call; 407*088332b5SXin Liso, an error inside the message handler 408*088332b5SXin Liwill call the message handler again. 409*088332b5SXin LiIf this loop goes on for too long, 410*088332b5SXin LiLua breaks it and returns an appropriate message. 411*088332b5SXin LiThe message handler is called only for regular runtime errors. 412*088332b5SXin LiIt is not called for memory-allocation errors 413*088332b5SXin Linor for errors while running finalizers or other message handlers. 414*088332b5SXin Li 415*088332b5SXin Li 416*088332b5SXin Li<p> 417*088332b5SXin LiLua also offers a system of <em>warnings</em> (see <a href="#pdf-warn"><code>warn</code></a>). 418*088332b5SXin LiUnlike errors, warnings do not interfere 419*088332b5SXin Liin any way with program execution. 420*088332b5SXin LiThey typically only generate a message to the user, 421*088332b5SXin Lialthough this behavior can be adapted from C (see <a href="#lua_setwarnf"><code>lua_setwarnf</code></a>). 422*088332b5SXin Li 423*088332b5SXin Li 424*088332b5SXin Li 425*088332b5SXin Li 426*088332b5SXin Li 427*088332b5SXin Li<h2>2.4 – <a name="2.4">Metatables and Metamethods</a></h2> 428*088332b5SXin Li 429*088332b5SXin Li<p> 430*088332b5SXin LiEvery value in Lua can have a <em>metatable</em>. 431*088332b5SXin LiThis <em>metatable</em> is an ordinary Lua table 432*088332b5SXin Lithat defines the behavior of the original value 433*088332b5SXin Liunder certain events. 434*088332b5SXin LiYou can change several aspects of the behavior 435*088332b5SXin Liof a value by setting specific fields in its metatable. 436*088332b5SXin LiFor instance, when a non-numeric value is the operand of an addition, 437*088332b5SXin LiLua checks for a function in the field "<code>__add</code>" of the value's metatable. 438*088332b5SXin LiIf it finds one, 439*088332b5SXin LiLua calls this function to perform the addition. 440*088332b5SXin Li 441*088332b5SXin Li 442*088332b5SXin Li<p> 443*088332b5SXin LiThe key for each event in a metatable is a string 444*088332b5SXin Liwith the event name prefixed by two underscores; 445*088332b5SXin Lithe corresponding value is called a <em>metavalue</em>. 446*088332b5SXin LiFor most events, the metavalue must be a function, 447*088332b5SXin Liwhich is then called a <em>metamethod</em>. 448*088332b5SXin LiIn the previous example, the key is the string "<code>__add</code>" 449*088332b5SXin Liand the metamethod is the function that performs the addition. 450*088332b5SXin LiUnless stated otherwise, 451*088332b5SXin Lia metamethod may in fact be any callable value, 452*088332b5SXin Liwhich is either a function or a value with a <code>__call</code> metamethod. 453*088332b5SXin Li 454*088332b5SXin Li 455*088332b5SXin Li<p> 456*088332b5SXin LiYou can query the metatable of any value 457*088332b5SXin Liusing the <a href="#pdf-getmetatable"><code>getmetatable</code></a> function. 458*088332b5SXin LiLua queries metamethods in metatables using a raw access (see <a href="#pdf-rawget"><code>rawget</code></a>). 459*088332b5SXin Li 460*088332b5SXin Li 461*088332b5SXin Li<p> 462*088332b5SXin LiYou can replace the metatable of tables 463*088332b5SXin Liusing the <a href="#pdf-setmetatable"><code>setmetatable</code></a> function. 464*088332b5SXin LiYou cannot change the metatable of other types from Lua code, 465*088332b5SXin Liexcept by using the debug library (<a href="#6.10">§6.10</a>). 466*088332b5SXin Li 467*088332b5SXin Li 468*088332b5SXin Li<p> 469*088332b5SXin LiTables and full userdata have individual metatables, 470*088332b5SXin Lialthough multiple tables and userdata can share their metatables. 471*088332b5SXin LiValues of all other types share one single metatable per type; 472*088332b5SXin Lithat is, there is one single metatable for all numbers, 473*088332b5SXin Lione for all strings, etc. 474*088332b5SXin LiBy default, a value has no metatable, 475*088332b5SXin Libut the string library sets a metatable for the string type (see <a href="#6.4">§6.4</a>). 476*088332b5SXin Li 477*088332b5SXin Li 478*088332b5SXin Li<p> 479*088332b5SXin LiA detailed list of operations controlled by metatables is given next. 480*088332b5SXin LiEach event is identified by its corresponding key. 481*088332b5SXin LiBy convention, all metatable keys used by Lua are composed by 482*088332b5SXin Litwo underscores followed by lowercase Latin letters. 483*088332b5SXin Li 484*088332b5SXin Li 485*088332b5SXin Li 486*088332b5SXin Li<ul> 487*088332b5SXin Li 488*088332b5SXin Li<li><b><code>__add</code>: </b> 489*088332b5SXin Lithe addition (<code>+</code>) operation. 490*088332b5SXin LiIf any operand for an addition is not a number, 491*088332b5SXin LiLua will try to call a metamethod. 492*088332b5SXin LiIt starts by checking the first operand (even if it is a number); 493*088332b5SXin Liif that operand does not define a metamethod for <code>__add</code>, 494*088332b5SXin Lithen Lua will check the second operand. 495*088332b5SXin LiIf Lua can find a metamethod, 496*088332b5SXin Liit calls the metamethod with the two operands as arguments, 497*088332b5SXin Liand the result of the call 498*088332b5SXin Li(adjusted to one value) 499*088332b5SXin Liis the result of the operation. 500*088332b5SXin LiOtherwise, if no metamethod is found, 501*088332b5SXin LiLua raises an error. 502*088332b5SXin Li</li> 503*088332b5SXin Li 504*088332b5SXin Li<li><b><code>__sub</code>: </b> 505*088332b5SXin Lithe subtraction (<code>-</code>) operation. 506*088332b5SXin LiBehavior similar to the addition operation. 507*088332b5SXin Li</li> 508*088332b5SXin Li 509*088332b5SXin Li<li><b><code>__mul</code>: </b> 510*088332b5SXin Lithe multiplication (<code>*</code>) operation. 511*088332b5SXin LiBehavior similar to the addition operation. 512*088332b5SXin Li</li> 513*088332b5SXin Li 514*088332b5SXin Li<li><b><code>__div</code>: </b> 515*088332b5SXin Lithe division (<code>/</code>) operation. 516*088332b5SXin LiBehavior similar to the addition operation. 517*088332b5SXin Li</li> 518*088332b5SXin Li 519*088332b5SXin Li<li><b><code>__mod</code>: </b> 520*088332b5SXin Lithe modulo (<code>%</code>) operation. 521*088332b5SXin LiBehavior similar to the addition operation. 522*088332b5SXin Li</li> 523*088332b5SXin Li 524*088332b5SXin Li<li><b><code>__pow</code>: </b> 525*088332b5SXin Lithe exponentiation (<code>^</code>) operation. 526*088332b5SXin LiBehavior similar to the addition operation. 527*088332b5SXin Li</li> 528*088332b5SXin Li 529*088332b5SXin Li<li><b><code>__unm</code>: </b> 530*088332b5SXin Lithe negation (unary <code>-</code>) operation. 531*088332b5SXin LiBehavior similar to the addition operation. 532*088332b5SXin Li</li> 533*088332b5SXin Li 534*088332b5SXin Li<li><b><code>__idiv</code>: </b> 535*088332b5SXin Lithe floor division (<code>//</code>) operation. 536*088332b5SXin LiBehavior similar to the addition operation. 537*088332b5SXin Li</li> 538*088332b5SXin Li 539*088332b5SXin Li<li><b><code>__band</code>: </b> 540*088332b5SXin Lithe bitwise AND (<code>&</code>) operation. 541*088332b5SXin LiBehavior similar to the addition operation, 542*088332b5SXin Liexcept that Lua will try a metamethod 543*088332b5SXin Liif any operand is neither an integer 544*088332b5SXin Linor a float coercible to an integer (see <a href="#3.4.3">§3.4.3</a>). 545*088332b5SXin Li</li> 546*088332b5SXin Li 547*088332b5SXin Li<li><b><code>__bor</code>: </b> 548*088332b5SXin Lithe bitwise OR (<code>|</code>) operation. 549*088332b5SXin LiBehavior similar to the bitwise AND operation. 550*088332b5SXin Li</li> 551*088332b5SXin Li 552*088332b5SXin Li<li><b><code>__bxor</code>: </b> 553*088332b5SXin Lithe bitwise exclusive OR (binary <code>~</code>) operation. 554*088332b5SXin LiBehavior similar to the bitwise AND operation. 555*088332b5SXin Li</li> 556*088332b5SXin Li 557*088332b5SXin Li<li><b><code>__bnot</code>: </b> 558*088332b5SXin Lithe bitwise NOT (unary <code>~</code>) operation. 559*088332b5SXin LiBehavior similar to the bitwise AND operation. 560*088332b5SXin Li</li> 561*088332b5SXin Li 562*088332b5SXin Li<li><b><code>__shl</code>: </b> 563*088332b5SXin Lithe bitwise left shift (<code><<</code>) operation. 564*088332b5SXin LiBehavior similar to the bitwise AND operation. 565*088332b5SXin Li</li> 566*088332b5SXin Li 567*088332b5SXin Li<li><b><code>__shr</code>: </b> 568*088332b5SXin Lithe bitwise right shift (<code>>></code>) operation. 569*088332b5SXin LiBehavior similar to the bitwise AND operation. 570*088332b5SXin Li</li> 571*088332b5SXin Li 572*088332b5SXin Li<li><b><code>__concat</code>: </b> 573*088332b5SXin Lithe concatenation (<code>..</code>) operation. 574*088332b5SXin LiBehavior similar to the addition operation, 575*088332b5SXin Liexcept that Lua will try a metamethod 576*088332b5SXin Liif any operand is neither a string nor a number 577*088332b5SXin Li(which is always coercible to a string). 578*088332b5SXin Li</li> 579*088332b5SXin Li 580*088332b5SXin Li<li><b><code>__len</code>: </b> 581*088332b5SXin Lithe length (<code>#</code>) operation. 582*088332b5SXin LiIf the object is not a string, 583*088332b5SXin LiLua will try its metamethod. 584*088332b5SXin LiIf there is a metamethod, 585*088332b5SXin LiLua calls it with the object as argument, 586*088332b5SXin Liand the result of the call 587*088332b5SXin Li(always adjusted to one value) 588*088332b5SXin Liis the result of the operation. 589*088332b5SXin LiIf there is no metamethod but the object is a table, 590*088332b5SXin Lithen Lua uses the table length operation (see <a href="#3.4.7">§3.4.7</a>). 591*088332b5SXin LiOtherwise, Lua raises an error. 592*088332b5SXin Li</li> 593*088332b5SXin Li 594*088332b5SXin Li<li><b><code>__eq</code>: </b> 595*088332b5SXin Lithe equal (<code>==</code>) operation. 596*088332b5SXin LiBehavior similar to the addition operation, 597*088332b5SXin Liexcept that Lua will try a metamethod only when the values 598*088332b5SXin Libeing compared are either both tables or both full userdata 599*088332b5SXin Liand they are not primitively equal. 600*088332b5SXin LiThe result of the call is always converted to a boolean. 601*088332b5SXin Li</li> 602*088332b5SXin Li 603*088332b5SXin Li<li><b><code>__lt</code>: </b> 604*088332b5SXin Lithe less than (<code><</code>) operation. 605*088332b5SXin LiBehavior similar to the addition operation, 606*088332b5SXin Liexcept that Lua will try a metamethod only when the values 607*088332b5SXin Libeing compared are neither both numbers nor both strings. 608*088332b5SXin LiMoreover, the result of the call is always converted to a boolean. 609*088332b5SXin Li</li> 610*088332b5SXin Li 611*088332b5SXin Li<li><b><code>__le</code>: </b> 612*088332b5SXin Lithe less equal (<code><=</code>) operation. 613*088332b5SXin LiBehavior similar to the less than operation. 614*088332b5SXin Li</li> 615*088332b5SXin Li 616*088332b5SXin Li<li><b><code>__index</code>: </b> 617*088332b5SXin LiThe indexing access operation <code>table[key]</code>. 618*088332b5SXin LiThis event happens when <code>table</code> is not a table or 619*088332b5SXin Liwhen <code>key</code> is not present in <code>table</code>. 620*088332b5SXin LiThe metavalue is looked up in the metatable of <code>table</code>. 621*088332b5SXin Li 622*088332b5SXin Li 623*088332b5SXin Li<p> 624*088332b5SXin LiThe metavalue for this event can be either a function, a table, 625*088332b5SXin Lior any value with an <code>__index</code> metavalue. 626*088332b5SXin LiIf it is a function, 627*088332b5SXin Liit is called with <code>table</code> and <code>key</code> as arguments, 628*088332b5SXin Liand the result of the call 629*088332b5SXin Li(adjusted to one value) 630*088332b5SXin Liis the result of the operation. 631*088332b5SXin LiOtherwise, 632*088332b5SXin Lithe final result is the result of indexing this metavalue with <code>key</code>. 633*088332b5SXin LiThis indexing is regular, not raw, 634*088332b5SXin Liand therefore can trigger another <code>__index</code> metavalue. 635*088332b5SXin Li</li> 636*088332b5SXin Li 637*088332b5SXin Li<li><b><code>__newindex</code>: </b> 638*088332b5SXin LiThe indexing assignment <code>table[key] = value</code>. 639*088332b5SXin LiLike the index event, 640*088332b5SXin Lithis event happens when <code>table</code> is not a table or 641*088332b5SXin Liwhen <code>key</code> is not present in <code>table</code>. 642*088332b5SXin LiThe metavalue is looked up in the metatable of <code>table</code>. 643*088332b5SXin Li 644*088332b5SXin Li 645*088332b5SXin Li<p> 646*088332b5SXin LiLike with indexing, 647*088332b5SXin Lithe metavalue for this event can be either a function, a table, 648*088332b5SXin Lior any value with an <code>__newindex</code> metavalue. 649*088332b5SXin LiIf it is a function, 650*088332b5SXin Liit is called with <code>table</code>, <code>key</code>, and <code>value</code> as arguments. 651*088332b5SXin LiOtherwise, 652*088332b5SXin LiLua repeats the indexing assignment over this metavalue 653*088332b5SXin Liwith the same key and value. 654*088332b5SXin LiThis assignment is regular, not raw, 655*088332b5SXin Liand therefore can trigger another <code>__newindex</code> metavalue. 656*088332b5SXin Li 657*088332b5SXin Li 658*088332b5SXin Li<p> 659*088332b5SXin LiWhenever a <code>__newindex</code> metavalue is invoked, 660*088332b5SXin LiLua does not perform the primitive assignment. 661*088332b5SXin LiIf needed, 662*088332b5SXin Lithe metamethod itself can call <a href="#pdf-rawset"><code>rawset</code></a> 663*088332b5SXin Lito do the assignment. 664*088332b5SXin Li</li> 665*088332b5SXin Li 666*088332b5SXin Li<li><b><code>__call</code>: </b> 667*088332b5SXin LiThe call operation <code>func(args)</code>. 668*088332b5SXin LiThis event happens when Lua tries to call a non-function value 669*088332b5SXin Li(that is, <code>func</code> is not a function). 670*088332b5SXin LiThe metamethod is looked up in <code>func</code>. 671*088332b5SXin LiIf present, 672*088332b5SXin Lithe metamethod is called with <code>func</code> as its first argument, 673*088332b5SXin Lifollowed by the arguments of the original call (<code>args</code>). 674*088332b5SXin LiAll results of the call 675*088332b5SXin Liare the results of the operation. 676*088332b5SXin LiThis is the only metamethod that allows multiple results. 677*088332b5SXin Li</li> 678*088332b5SXin Li 679*088332b5SXin Li</ul> 680*088332b5SXin Li 681*088332b5SXin Li<p> 682*088332b5SXin LiIn addition to the previous list, 683*088332b5SXin Lithe interpreter also respects the following keys in metatables: 684*088332b5SXin Li<code>__gc</code> (see <a href="#2.5.3">§2.5.3</a>), 685*088332b5SXin Li<code>__close</code> (see <a href="#3.3.8">§3.3.8</a>), 686*088332b5SXin Li<code>__mode</code> (see <a href="#2.5.4">§2.5.4</a>), 687*088332b5SXin Liand <code>__name</code>. 688*088332b5SXin Li(The entry <code>__name</code>, 689*088332b5SXin Liwhen it contains a string, 690*088332b5SXin Limay be used by <a href="#pdf-tostring"><code>tostring</code></a> and in error messages.) 691*088332b5SXin Li 692*088332b5SXin Li 693*088332b5SXin Li<p> 694*088332b5SXin LiFor the unary operators (negation, length, and bitwise NOT), 695*088332b5SXin Lithe metamethod is computed and called with a dummy second operand, 696*088332b5SXin Liequal to the first one. 697*088332b5SXin LiThis extra operand is only to simplify Lua's internals 698*088332b5SXin Li(by making these operators behave like a binary operation) 699*088332b5SXin Liand may be removed in future versions. 700*088332b5SXin LiFor most uses this extra operand is irrelevant. 701*088332b5SXin Li 702*088332b5SXin Li 703*088332b5SXin Li<p> 704*088332b5SXin LiBecause metatables are regular tables, 705*088332b5SXin Lithey can contain arbitrary fields, 706*088332b5SXin Linot only the event names defined above. 707*088332b5SXin LiSome functions in the standard library 708*088332b5SXin Li(e.g., <a href="#pdf-tostring"><code>tostring</code></a>) 709*088332b5SXin Liuse other fields in metatables for their own purposes. 710*088332b5SXin Li 711*088332b5SXin Li 712*088332b5SXin Li<p> 713*088332b5SXin LiIt is a good practice to add all needed metamethods to a table 714*088332b5SXin Libefore setting it as a metatable of some object. 715*088332b5SXin LiIn particular, the <code>__gc</code> metamethod works only when this order 716*088332b5SXin Liis followed (see <a href="#2.5.3">§2.5.3</a>). 717*088332b5SXin LiIt is also a good practice to set the metatable of an object 718*088332b5SXin Liright after its creation. 719*088332b5SXin Li 720*088332b5SXin Li 721*088332b5SXin Li 722*088332b5SXin Li 723*088332b5SXin Li 724*088332b5SXin Li<h2>2.5 – <a name="2.5">Garbage Collection</a></h2> 725*088332b5SXin Li 726*088332b5SXin Li 727*088332b5SXin Li 728*088332b5SXin Li<p> 729*088332b5SXin LiLua performs automatic memory management. 730*088332b5SXin LiThis means that 731*088332b5SXin Liyou do not have to worry about allocating memory for new objects 732*088332b5SXin Lior freeing it when the objects are no longer needed. 733*088332b5SXin LiLua manages memory automatically by running 734*088332b5SXin Lia <em>garbage collector</em> to collect all <em>dead</em> objects. 735*088332b5SXin LiAll memory used by Lua is subject to automatic management: 736*088332b5SXin Listrings, tables, userdata, functions, threads, internal structures, etc. 737*088332b5SXin Li 738*088332b5SXin Li 739*088332b5SXin Li<p> 740*088332b5SXin LiAn object is considered <em>dead</em> 741*088332b5SXin Lias soon as the collector can be sure the object 742*088332b5SXin Liwill not be accessed again in the normal execution of the program. 743*088332b5SXin Li("Normal execution" here excludes finalizers, 744*088332b5SXin Liwhich can resurrect dead objects (see <a href="#2.5.3">§2.5.3</a>), 745*088332b5SXin Liand excludes also operations using the debug library.) 746*088332b5SXin LiNote that the time when the collector can be sure that an object 747*088332b5SXin Liis dead may not coincide with the programmer's expectations. 748*088332b5SXin LiThe only guarantees are that Lua will not collect an object 749*088332b5SXin Lithat may still be accessed in the normal execution of the program, 750*088332b5SXin Liand it will eventually collect an object 751*088332b5SXin Lithat is inaccessible from Lua. 752*088332b5SXin Li(Here, 753*088332b5SXin Li<em>inaccessible from Lua</em> means that neither a variable nor 754*088332b5SXin Lianother live object refer to the object.) 755*088332b5SXin LiBecause Lua has no knowledge about C code, 756*088332b5SXin Liit never collects objects accessible through the registry (see <a href="#4.3">§4.3</a>), 757*088332b5SXin Liwhich includes the global environment (see <a href="#2.2">§2.2</a>). 758*088332b5SXin Li 759*088332b5SXin Li 760*088332b5SXin Li<p> 761*088332b5SXin LiThe garbage collector (GC) in Lua can work in two modes: 762*088332b5SXin Liincremental and generational. 763*088332b5SXin Li 764*088332b5SXin Li 765*088332b5SXin Li<p> 766*088332b5SXin LiThe default GC mode with the default parameters 767*088332b5SXin Liare adequate for most uses. 768*088332b5SXin LiHowever, programs that waste a large proportion of their time 769*088332b5SXin Liallocating and freeing memory can benefit from other settings. 770*088332b5SXin LiKeep in mind that the GC behavior is non-portable 771*088332b5SXin Liboth across platforms and across different Lua releases; 772*088332b5SXin Litherefore, optimal settings are also non-portable. 773*088332b5SXin Li 774*088332b5SXin Li 775*088332b5SXin Li<p> 776*088332b5SXin LiYou can change the GC mode and parameters by calling 777*088332b5SXin Li<a href="#lua_gc"><code>lua_gc</code></a> in C 778*088332b5SXin Lior <a href="#pdf-collectgarbage"><code>collectgarbage</code></a> in Lua. 779*088332b5SXin LiYou can also use these functions to control 780*088332b5SXin Lithe collector directly (e.g., to stop and restart it). 781*088332b5SXin Li 782*088332b5SXin Li 783*088332b5SXin Li 784*088332b5SXin Li 785*088332b5SXin Li 786*088332b5SXin Li<h3>2.5.1 – <a name="2.5.1">Incremental Garbage Collection</a></h3> 787*088332b5SXin Li 788*088332b5SXin Li<p> 789*088332b5SXin LiIn incremental mode, 790*088332b5SXin Lieach GC cycle performs a mark-and-sweep collection in small steps 791*088332b5SXin Liinterleaved with the program's execution. 792*088332b5SXin LiIn this mode, 793*088332b5SXin Lithe collector uses three numbers to control its garbage-collection cycles: 794*088332b5SXin Lithe <em>garbage-collector pause</em>, 795*088332b5SXin Lithe <em>garbage-collector step multiplier</em>, 796*088332b5SXin Liand the <em>garbage-collector step size</em>. 797*088332b5SXin Li 798*088332b5SXin Li 799*088332b5SXin Li<p> 800*088332b5SXin LiThe garbage-collector pause 801*088332b5SXin Licontrols how long the collector waits before starting a new cycle. 802*088332b5SXin LiThe collector starts a new cycle when the use of memory 803*088332b5SXin Lihits <em>n%</em> of the use after the previous collection. 804*088332b5SXin LiLarger values make the collector less aggressive. 805*088332b5SXin LiValues equal to or less than 100 mean the collector will not wait to 806*088332b5SXin Listart a new cycle. 807*088332b5SXin LiA value of 200 means that the collector waits for the total memory in use 808*088332b5SXin Lito double before starting a new cycle. 809*088332b5SXin LiThe default value is 200; the maximum value is 1000. 810*088332b5SXin Li 811*088332b5SXin Li 812*088332b5SXin Li<p> 813*088332b5SXin LiThe garbage-collector step multiplier 814*088332b5SXin Licontrols the speed of the collector relative to 815*088332b5SXin Limemory allocation, 816*088332b5SXin Lithat is, 817*088332b5SXin Lihow many elements it marks or sweeps for each 818*088332b5SXin Likilobyte of memory allocated. 819*088332b5SXin LiLarger values make the collector more aggressive but also increase 820*088332b5SXin Lithe size of each incremental step. 821*088332b5SXin LiYou should not use values less than 100, 822*088332b5SXin Libecause they make the collector too slow and 823*088332b5SXin Lican result in the collector never finishing a cycle. 824*088332b5SXin LiThe default value is 100; the maximum value is 1000. 825*088332b5SXin Li 826*088332b5SXin Li 827*088332b5SXin Li<p> 828*088332b5SXin LiThe garbage-collector step size controls the 829*088332b5SXin Lisize of each incremental step, 830*088332b5SXin Lispecifically how many bytes the interpreter allocates 831*088332b5SXin Libefore performing a step. 832*088332b5SXin LiThis parameter is logarithmic: 833*088332b5SXin LiA value of <em>n</em> means the interpreter will allocate <em>2<sup>n</sup></em> 834*088332b5SXin Libytes between steps and perform equivalent work during the step. 835*088332b5SXin LiA large value (e.g., 60) makes the collector a stop-the-world 836*088332b5SXin Li(non-incremental) collector. 837*088332b5SXin LiThe default value is 13, 838*088332b5SXin Liwhich means steps of approximately 8 Kbytes. 839*088332b5SXin Li 840*088332b5SXin Li 841*088332b5SXin Li 842*088332b5SXin Li 843*088332b5SXin Li 844*088332b5SXin Li<h3>2.5.2 – <a name="2.5.2">Generational Garbage Collection</a></h3> 845*088332b5SXin Li 846*088332b5SXin Li<p> 847*088332b5SXin LiIn generational mode, 848*088332b5SXin Lithe collector does frequent <em>minor</em> collections, 849*088332b5SXin Liwhich traverses only objects recently created. 850*088332b5SXin LiIf after a minor collection the use of memory is still above a limit, 851*088332b5SXin Lithe collector does a stop-the-world <em>major</em> collection, 852*088332b5SXin Liwhich traverses all objects. 853*088332b5SXin LiThe generational mode uses two parameters: 854*088332b5SXin Lithe <em>minor multiplier</em> and the <em>the major multiplier</em>. 855*088332b5SXin Li 856*088332b5SXin Li 857*088332b5SXin Li<p> 858*088332b5SXin LiThe minor multiplier controls the frequency of minor collections. 859*088332b5SXin LiFor a minor multiplier <em>x</em>, 860*088332b5SXin Lia new minor collection will be done when memory 861*088332b5SXin Ligrows <em>x%</em> larger than the memory in use after the previous major 862*088332b5SXin Licollection. 863*088332b5SXin LiFor instance, for a multiplier of 20, 864*088332b5SXin Lithe collector will do a minor collection when the use of memory 865*088332b5SXin Ligets 20% larger than the use after the previous major collection. 866*088332b5SXin LiThe default value is 20; the maximum value is 200. 867*088332b5SXin Li 868*088332b5SXin Li 869*088332b5SXin Li<p> 870*088332b5SXin LiThe major multiplier controls the frequency of major collections. 871*088332b5SXin LiFor a major multiplier <em>x</em>, 872*088332b5SXin Lia new major collection will be done when memory 873*088332b5SXin Ligrows <em>x%</em> larger than the memory in use after the previous major 874*088332b5SXin Licollection. 875*088332b5SXin LiFor instance, for a multiplier of 100, 876*088332b5SXin Lithe collector will do a major collection when the use of memory 877*088332b5SXin Ligets larger than twice the use after the previous collection. 878*088332b5SXin LiThe default value is 100; the maximum value is 1000. 879*088332b5SXin Li 880*088332b5SXin Li 881*088332b5SXin Li 882*088332b5SXin Li 883*088332b5SXin Li 884*088332b5SXin Li<h3>2.5.3 – <a name="2.5.3">Garbage-Collection Metamethods</a></h3> 885*088332b5SXin Li 886*088332b5SXin Li<p> 887*088332b5SXin LiYou can set garbage-collector metamethods for tables 888*088332b5SXin Liand, using the C API, 889*088332b5SXin Lifor full userdata (see <a href="#2.4">§2.4</a>). 890*088332b5SXin LiThese metamethods, called <em>finalizers</em>, 891*088332b5SXin Liare called when the garbage collector detects that the 892*088332b5SXin Licorresponding table or userdata is dead. 893*088332b5SXin LiFinalizers allow you to coordinate Lua's garbage collection 894*088332b5SXin Liwith external resource management such as closing files, 895*088332b5SXin Linetwork or database connections, 896*088332b5SXin Lior freeing your own memory. 897*088332b5SXin Li 898*088332b5SXin Li 899*088332b5SXin Li<p> 900*088332b5SXin LiFor an object (table or userdata) to be finalized when collected, 901*088332b5SXin Liyou must <em>mark</em> it for finalization. 902*088332b5SXin Li 903*088332b5SXin LiYou mark an object for finalization when you set its metatable 904*088332b5SXin Liand the metatable has a field indexed by the string "<code>__gc</code>". 905*088332b5SXin LiNote that if you set a metatable without a <code>__gc</code> field 906*088332b5SXin Liand later create that field in the metatable, 907*088332b5SXin Lithe object will not be marked for finalization. 908*088332b5SXin Li 909*088332b5SXin Li 910*088332b5SXin Li<p> 911*088332b5SXin LiWhen a marked object becomes dead, 912*088332b5SXin Liit is not collected immediately by the garbage collector. 913*088332b5SXin LiInstead, Lua puts it in a list. 914*088332b5SXin LiAfter the collection, 915*088332b5SXin LiLua goes through that list. 916*088332b5SXin LiFor each object in the list, 917*088332b5SXin Liit checks the object's <code>__gc</code> metamethod: 918*088332b5SXin LiIf it is present, 919*088332b5SXin LiLua calls it with the object as its single argument. 920*088332b5SXin Li 921*088332b5SXin Li 922*088332b5SXin Li<p> 923*088332b5SXin LiAt the end of each garbage-collection cycle, 924*088332b5SXin Lithe finalizers are called in 925*088332b5SXin Lithe reverse order that the objects were marked for finalization, 926*088332b5SXin Liamong those collected in that cycle; 927*088332b5SXin Lithat is, the first finalizer to be called is the one associated 928*088332b5SXin Liwith the object marked last in the program. 929*088332b5SXin LiThe execution of each finalizer may occur at any point during 930*088332b5SXin Lithe execution of the regular code. 931*088332b5SXin Li 932*088332b5SXin Li 933*088332b5SXin Li<p> 934*088332b5SXin LiBecause the object being collected must still be used by the finalizer, 935*088332b5SXin Lithat object (and other objects accessible only through it) 936*088332b5SXin Limust be <em>resurrected</em> by Lua. 937*088332b5SXin LiUsually, this resurrection is transient, 938*088332b5SXin Liand the object memory is freed in the next garbage-collection cycle. 939*088332b5SXin LiHowever, if the finalizer stores the object in some global place 940*088332b5SXin Li(e.g., a global variable), 941*088332b5SXin Lithen the resurrection is permanent. 942*088332b5SXin LiMoreover, if the finalizer marks a finalizing object for finalization again, 943*088332b5SXin Liits finalizer will be called again in the next cycle where the 944*088332b5SXin Liobject is dead. 945*088332b5SXin LiIn any case, 946*088332b5SXin Lithe object memory is freed only in a GC cycle where 947*088332b5SXin Lithe object is dead and not marked for finalization. 948*088332b5SXin Li 949*088332b5SXin Li 950*088332b5SXin Li<p> 951*088332b5SXin LiWhen you close a state (see <a href="#lua_close"><code>lua_close</code></a>), 952*088332b5SXin LiLua calls the finalizers of all objects marked for finalization, 953*088332b5SXin Lifollowing the reverse order that they were marked. 954*088332b5SXin LiIf any finalizer marks objects for collection during that phase, 955*088332b5SXin Lithese marks have no effect. 956*088332b5SXin Li 957*088332b5SXin Li 958*088332b5SXin Li<p> 959*088332b5SXin LiFinalizers cannot yield. 960*088332b5SXin LiExcept for that, they can do anything, 961*088332b5SXin Lisuch as raise errors, create new objects, 962*088332b5SXin Lior even run the garbage collector. 963*088332b5SXin LiHowever, because they can run in unpredictable times, 964*088332b5SXin Liit is good practice to restrict each finalizer 965*088332b5SXin Lito the minimum necessary to properly release 966*088332b5SXin Liits associated resource. 967*088332b5SXin Li 968*088332b5SXin Li 969*088332b5SXin Li<p> 970*088332b5SXin LiAny error while running a finalizer generates a warning; 971*088332b5SXin Lithe error is not propagated. 972*088332b5SXin Li 973*088332b5SXin Li 974*088332b5SXin Li 975*088332b5SXin Li 976*088332b5SXin Li 977*088332b5SXin Li<h3>2.5.4 – <a name="2.5.4">Weak Tables</a></h3> 978*088332b5SXin Li 979*088332b5SXin Li<p> 980*088332b5SXin LiA <em>weak table</em> is a table whose elements are 981*088332b5SXin Li<em>weak references</em>. 982*088332b5SXin LiA weak reference is ignored by the garbage collector. 983*088332b5SXin LiIn other words, 984*088332b5SXin Liif the only references to an object are weak references, 985*088332b5SXin Lithen the garbage collector will collect that object. 986*088332b5SXin Li 987*088332b5SXin Li 988*088332b5SXin Li<p> 989*088332b5SXin LiA weak table can have weak keys, weak values, or both. 990*088332b5SXin LiA table with weak values allows the collection of its values, 991*088332b5SXin Libut prevents the collection of its keys. 992*088332b5SXin LiA table with both weak keys and weak values allows the collection of 993*088332b5SXin Liboth keys and values. 994*088332b5SXin LiIn any case, if either the key or the value is collected, 995*088332b5SXin Lithe whole pair is removed from the table. 996*088332b5SXin LiThe weakness of a table is controlled by the 997*088332b5SXin Li<code>__mode</code> field of its metatable. 998*088332b5SXin LiThis metavalue, if present, must be one of the following strings: 999*088332b5SXin Li"<code>k</code>", for a table with weak keys; 1000*088332b5SXin Li"<code>v</code>", for a table with weak values; 1001*088332b5SXin Lior "<code>kv</code>", for a table with both weak keys and values. 1002*088332b5SXin Li 1003*088332b5SXin Li 1004*088332b5SXin Li<p> 1005*088332b5SXin LiA table with weak keys and strong values 1006*088332b5SXin Liis also called an <em>ephemeron table</em>. 1007*088332b5SXin LiIn an ephemeron table, 1008*088332b5SXin Lia value is considered reachable only if its key is reachable. 1009*088332b5SXin LiIn particular, 1010*088332b5SXin Liif the only reference to a key comes through its value, 1011*088332b5SXin Lithe pair is removed. 1012*088332b5SXin Li 1013*088332b5SXin Li 1014*088332b5SXin Li<p> 1015*088332b5SXin LiAny change in the weakness of a table may take effect only 1016*088332b5SXin Liat the next collect cycle. 1017*088332b5SXin LiIn particular, if you change the weakness to a stronger mode, 1018*088332b5SXin LiLua may still collect some items from that table 1019*088332b5SXin Libefore the change takes effect. 1020*088332b5SXin Li 1021*088332b5SXin Li 1022*088332b5SXin Li<p> 1023*088332b5SXin LiOnly objects that have an explicit construction 1024*088332b5SXin Liare removed from weak tables. 1025*088332b5SXin LiValues, such as numbers and light C functions, 1026*088332b5SXin Liare not subject to garbage collection, 1027*088332b5SXin Liand therefore are not removed from weak tables 1028*088332b5SXin Li(unless their associated values are collected). 1029*088332b5SXin LiAlthough strings are subject to garbage collection, 1030*088332b5SXin Lithey do not have an explicit construction and 1031*088332b5SXin Litheir equality is by value; 1032*088332b5SXin Lithey behave more like values than like objects. 1033*088332b5SXin LiTherefore, they are not removed from weak tables. 1034*088332b5SXin Li 1035*088332b5SXin Li 1036*088332b5SXin Li<p> 1037*088332b5SXin LiResurrected objects 1038*088332b5SXin Li(that is, objects being finalized 1039*088332b5SXin Liand objects accessible only through objects being finalized) 1040*088332b5SXin Lihave a special behavior in weak tables. 1041*088332b5SXin LiThey are removed from weak values before running their finalizers, 1042*088332b5SXin Libut are removed from weak keys only in the next collection 1043*088332b5SXin Liafter running their finalizers, when such objects are actually freed. 1044*088332b5SXin LiThis behavior allows the finalizer to access properties 1045*088332b5SXin Liassociated with the object through weak tables. 1046*088332b5SXin Li 1047*088332b5SXin Li 1048*088332b5SXin Li<p> 1049*088332b5SXin LiIf a weak table is among the resurrected objects in a collection cycle, 1050*088332b5SXin Liit may not be properly cleared until the next cycle. 1051*088332b5SXin Li 1052*088332b5SXin Li 1053*088332b5SXin Li 1054*088332b5SXin Li 1055*088332b5SXin Li 1056*088332b5SXin Li 1057*088332b5SXin Li 1058*088332b5SXin Li<h2>2.6 – <a name="2.6">Coroutines</a></h2> 1059*088332b5SXin Li 1060*088332b5SXin Li<p> 1061*088332b5SXin LiLua supports coroutines, 1062*088332b5SXin Lialso called <em>collaborative multithreading</em>. 1063*088332b5SXin LiA coroutine in Lua represents an independent thread of execution. 1064*088332b5SXin LiUnlike threads in multithread systems, however, 1065*088332b5SXin Lia coroutine only suspends its execution by explicitly calling 1066*088332b5SXin Lia yield function. 1067*088332b5SXin Li 1068*088332b5SXin Li 1069*088332b5SXin Li<p> 1070*088332b5SXin LiYou create a coroutine by calling <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>. 1071*088332b5SXin LiIts sole argument is a function 1072*088332b5SXin Lithat is the main function of the coroutine. 1073*088332b5SXin LiThe <code>create</code> function only creates a new coroutine and 1074*088332b5SXin Lireturns a handle to it (an object of type <em>thread</em>); 1075*088332b5SXin Liit does not start the coroutine. 1076*088332b5SXin Li 1077*088332b5SXin Li 1078*088332b5SXin Li<p> 1079*088332b5SXin LiYou execute a coroutine by calling <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>. 1080*088332b5SXin LiWhen you first call <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>, 1081*088332b5SXin Lipassing as its first argument 1082*088332b5SXin Lia thread returned by <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>, 1083*088332b5SXin Lithe coroutine starts its execution by 1084*088332b5SXin Licalling its main function. 1085*088332b5SXin LiExtra arguments passed to <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> are passed 1086*088332b5SXin Lias arguments to that function. 1087*088332b5SXin LiAfter the coroutine starts running, 1088*088332b5SXin Liit runs until it terminates or <em>yields</em>. 1089*088332b5SXin Li 1090*088332b5SXin Li 1091*088332b5SXin Li<p> 1092*088332b5SXin LiA coroutine can terminate its execution in two ways: 1093*088332b5SXin Linormally, when its main function returns 1094*088332b5SXin Li(explicitly or implicitly, after the last instruction); 1095*088332b5SXin Liand abnormally, if there is an unprotected error. 1096*088332b5SXin LiIn case of normal termination, 1097*088332b5SXin Li<a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> returns <b>true</b>, 1098*088332b5SXin Liplus any values returned by the coroutine main function. 1099*088332b5SXin LiIn case of errors, <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> returns <b>false</b> 1100*088332b5SXin Liplus the error object. 1101*088332b5SXin LiIn this case, the coroutine does not unwind its stack, 1102*088332b5SXin Liso that it is possible to inspect it after the error 1103*088332b5SXin Liwith the debug API. 1104*088332b5SXin Li 1105*088332b5SXin Li 1106*088332b5SXin Li<p> 1107*088332b5SXin LiA coroutine yields by calling <a href="#pdf-coroutine.yield"><code>coroutine.yield</code></a>. 1108*088332b5SXin LiWhen a coroutine yields, 1109*088332b5SXin Lithe corresponding <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> returns immediately, 1110*088332b5SXin Lieven if the yield happens inside nested function calls 1111*088332b5SXin Li(that is, not in the main function, 1112*088332b5SXin Libut in a function directly or indirectly called by the main function). 1113*088332b5SXin LiIn the case of a yield, <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> also returns <b>true</b>, 1114*088332b5SXin Liplus any values passed to <a href="#pdf-coroutine.yield"><code>coroutine.yield</code></a>. 1115*088332b5SXin LiThe next time you resume the same coroutine, 1116*088332b5SXin Liit continues its execution from the point where it yielded, 1117*088332b5SXin Liwith the call to <a href="#pdf-coroutine.yield"><code>coroutine.yield</code></a> returning any extra 1118*088332b5SXin Liarguments passed to <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>. 1119*088332b5SXin Li 1120*088332b5SXin Li 1121*088332b5SXin Li<p> 1122*088332b5SXin LiLike <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>, 1123*088332b5SXin Lithe <a href="#pdf-coroutine.wrap"><code>coroutine.wrap</code></a> function also creates a coroutine, 1124*088332b5SXin Libut instead of returning the coroutine itself, 1125*088332b5SXin Liit returns a function that, when called, resumes the coroutine. 1126*088332b5SXin LiAny arguments passed to this function 1127*088332b5SXin Ligo as extra arguments to <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>. 1128*088332b5SXin Li<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>, 1129*088332b5SXin Liexcept the first one (the boolean error code). 1130*088332b5SXin LiUnlike <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>, 1131*088332b5SXin Lithe function created by <a href="#pdf-coroutine.wrap"><code>coroutine.wrap</code></a> 1132*088332b5SXin Lipropagates any error to the caller. 1133*088332b5SXin LiIn this case, 1134*088332b5SXin Lithe function also closes the coroutine (see <a href="#pdf-coroutine.close"><code>coroutine.close</code></a>). 1135*088332b5SXin Li 1136*088332b5SXin Li 1137*088332b5SXin Li<p> 1138*088332b5SXin LiAs an example of how coroutines work, 1139*088332b5SXin Liconsider the following code: 1140*088332b5SXin Li 1141*088332b5SXin Li<pre> 1142*088332b5SXin Li function foo (a) 1143*088332b5SXin Li print("foo", a) 1144*088332b5SXin Li return coroutine.yield(2*a) 1145*088332b5SXin Li end 1146*088332b5SXin Li 1147*088332b5SXin Li co = coroutine.create(function (a,b) 1148*088332b5SXin Li print("co-body", a, b) 1149*088332b5SXin Li local r = foo(a+1) 1150*088332b5SXin Li print("co-body", r) 1151*088332b5SXin Li local r, s = coroutine.yield(a+b, a-b) 1152*088332b5SXin Li print("co-body", r, s) 1153*088332b5SXin Li return b, "end" 1154*088332b5SXin Li end) 1155*088332b5SXin Li 1156*088332b5SXin Li print("main", coroutine.resume(co, 1, 10)) 1157*088332b5SXin Li print("main", coroutine.resume(co, "r")) 1158*088332b5SXin Li print("main", coroutine.resume(co, "x", "y")) 1159*088332b5SXin Li print("main", coroutine.resume(co, "x", "y")) 1160*088332b5SXin Li</pre><p> 1161*088332b5SXin LiWhen you run it, it produces the following output: 1162*088332b5SXin Li 1163*088332b5SXin Li<pre> 1164*088332b5SXin Li co-body 1 10 1165*088332b5SXin Li foo 2 1166*088332b5SXin Li main true 4 1167*088332b5SXin Li co-body r 1168*088332b5SXin Li main true 11 -9 1169*088332b5SXin Li co-body x y 1170*088332b5SXin Li main true 10 end 1171*088332b5SXin Li main false cannot resume dead coroutine 1172*088332b5SXin Li</pre> 1173*088332b5SXin Li 1174*088332b5SXin Li<p> 1175*088332b5SXin LiYou can also create and manipulate coroutines through the C API: 1176*088332b5SXin Lisee functions <a href="#lua_newthread"><code>lua_newthread</code></a>, <a href="#lua_resume"><code>lua_resume</code></a>, 1177*088332b5SXin Liand <a href="#lua_yield"><code>lua_yield</code></a>. 1178*088332b5SXin Li 1179*088332b5SXin Li 1180*088332b5SXin Li 1181*088332b5SXin Li 1182*088332b5SXin Li 1183*088332b5SXin Li<h1>3 – <a name="3">The Language</a></h1> 1184*088332b5SXin Li 1185*088332b5SXin Li 1186*088332b5SXin Li 1187*088332b5SXin Li<p> 1188*088332b5SXin LiThis section describes the lexis, the syntax, and the semantics of Lua. 1189*088332b5SXin LiIn other words, 1190*088332b5SXin Lithis section describes 1191*088332b5SXin Liwhich tokens are valid, 1192*088332b5SXin Lihow they can be combined, 1193*088332b5SXin Liand what their combinations mean. 1194*088332b5SXin Li 1195*088332b5SXin Li 1196*088332b5SXin Li<p> 1197*088332b5SXin LiLanguage constructs will be explained using the usual extended BNF notation, 1198*088332b5SXin Liin which 1199*088332b5SXin Li{<em>a</em>} means 0 or more <em>a</em>'s, and 1200*088332b5SXin Li[<em>a</em>] means an optional <em>a</em>. 1201*088332b5SXin LiNon-terminals are shown like non-terminal, 1202*088332b5SXin Likeywords are shown like <b>kword</b>, 1203*088332b5SXin Liand other terminal symbols are shown like ‘<b>=</b>’. 1204*088332b5SXin LiThe complete syntax of Lua can be found in <a href="#9">§9</a> 1205*088332b5SXin Liat the end of this manual. 1206*088332b5SXin Li 1207*088332b5SXin Li 1208*088332b5SXin Li 1209*088332b5SXin Li 1210*088332b5SXin Li 1211*088332b5SXin Li<h2>3.1 – <a name="3.1">Lexical Conventions</a></h2> 1212*088332b5SXin Li 1213*088332b5SXin Li<p> 1214*088332b5SXin LiLua is a free-form language. 1215*088332b5SXin LiIt ignores spaces and comments between lexical elements (tokens), 1216*088332b5SXin Liexcept as delimiters between two tokens. 1217*088332b5SXin LiIn source code, 1218*088332b5SXin LiLua recognizes as spaces the standard ASCII whitespace 1219*088332b5SXin Licharacters space, form feed, newline, 1220*088332b5SXin Licarriage return, horizontal tab, and vertical tab. 1221*088332b5SXin Li 1222*088332b5SXin Li 1223*088332b5SXin Li<p> 1224*088332b5SXin Li<em>Names</em> 1225*088332b5SXin Li(also called <em>identifiers</em>) 1226*088332b5SXin Liin Lua can be any string of Latin letters, 1227*088332b5SXin LiArabic-Indic digits, and underscores, 1228*088332b5SXin Linot beginning with a digit and 1229*088332b5SXin Linot being a reserved word. 1230*088332b5SXin LiIdentifiers are used to name variables, table fields, and labels. 1231*088332b5SXin Li 1232*088332b5SXin Li 1233*088332b5SXin Li<p> 1234*088332b5SXin LiThe following <em>keywords</em> are reserved 1235*088332b5SXin Liand cannot be used as names: 1236*088332b5SXin Li 1237*088332b5SXin Li 1238*088332b5SXin Li<pre> 1239*088332b5SXin Li and break do else elseif end 1240*088332b5SXin Li false for function goto if in 1241*088332b5SXin Li local nil not or repeat return 1242*088332b5SXin Li then true until while 1243*088332b5SXin Li</pre> 1244*088332b5SXin Li 1245*088332b5SXin Li<p> 1246*088332b5SXin LiLua is a case-sensitive language: 1247*088332b5SXin Li<code>and</code> is a reserved word, but <code>And</code> and <code>AND</code> 1248*088332b5SXin Liare two different, valid names. 1249*088332b5SXin LiAs a convention, 1250*088332b5SXin Liprograms should avoid creating 1251*088332b5SXin Linames that start with an underscore followed by 1252*088332b5SXin Lione or more uppercase letters (such as <a href="#pdf-_VERSION"><code>_VERSION</code></a>). 1253*088332b5SXin Li 1254*088332b5SXin Li 1255*088332b5SXin Li<p> 1256*088332b5SXin LiThe following strings denote other tokens: 1257*088332b5SXin Li 1258*088332b5SXin Li<pre> 1259*088332b5SXin Li + - * / % ^ # 1260*088332b5SXin Li & ~ | << >> // 1261*088332b5SXin Li == ~= <= >= < > = 1262*088332b5SXin Li ( ) { } [ ] :: 1263*088332b5SXin Li ; : , . .. ... 1264*088332b5SXin Li</pre> 1265*088332b5SXin Li 1266*088332b5SXin Li<p> 1267*088332b5SXin LiA <em>short literal string</em> 1268*088332b5SXin Lican be delimited by matching single or double quotes, 1269*088332b5SXin Liand can contain the following C-like escape sequences: 1270*088332b5SXin Li'<code>\a</code>' (bell), 1271*088332b5SXin Li'<code>\b</code>' (backspace), 1272*088332b5SXin Li'<code>\f</code>' (form feed), 1273*088332b5SXin Li'<code>\n</code>' (newline), 1274*088332b5SXin Li'<code>\r</code>' (carriage return), 1275*088332b5SXin Li'<code>\t</code>' (horizontal tab), 1276*088332b5SXin Li'<code>\v</code>' (vertical tab), 1277*088332b5SXin Li'<code>\\</code>' (backslash), 1278*088332b5SXin Li'<code>\"</code>' (quotation mark [double quote]), 1279*088332b5SXin Liand '<code>\'</code>' (apostrophe [single quote]). 1280*088332b5SXin LiA backslash followed by a line break 1281*088332b5SXin Liresults in a newline in the string. 1282*088332b5SXin LiThe escape sequence '<code>\z</code>' skips the following span 1283*088332b5SXin Liof whitespace characters, 1284*088332b5SXin Liincluding line breaks; 1285*088332b5SXin Liit is particularly useful to break and indent a long literal string 1286*088332b5SXin Liinto multiple lines without adding the newlines and spaces 1287*088332b5SXin Liinto the string contents. 1288*088332b5SXin LiA short literal string cannot contain unescaped line breaks 1289*088332b5SXin Linor escapes not forming a valid escape sequence. 1290*088332b5SXin Li 1291*088332b5SXin Li 1292*088332b5SXin Li<p> 1293*088332b5SXin LiWe can specify any byte in a short literal string, 1294*088332b5SXin Liincluding embedded zeros, 1295*088332b5SXin Liby its numeric value. 1296*088332b5SXin LiThis can be done 1297*088332b5SXin Liwith the escape sequence <code>\x<em>XX</em></code>, 1298*088332b5SXin Liwhere <em>XX</em> is a sequence of exactly two hexadecimal digits, 1299*088332b5SXin Lior with the escape sequence <code>\<em>ddd</em></code>, 1300*088332b5SXin Liwhere <em>ddd</em> is a sequence of up to three decimal digits. 1301*088332b5SXin Li(Note that if a decimal escape sequence is to be followed by a digit, 1302*088332b5SXin Liit must be expressed using exactly three digits.) 1303*088332b5SXin Li 1304*088332b5SXin Li 1305*088332b5SXin Li<p> 1306*088332b5SXin LiThe UTF-8 encoding of a Unicode character 1307*088332b5SXin Lican be inserted in a literal string with 1308*088332b5SXin Lithe escape sequence <code>\u{<em>XXX</em>}</code> 1309*088332b5SXin Li(with mandatory enclosing braces), 1310*088332b5SXin Liwhere <em>XXX</em> is a sequence of one or more hexadecimal digits 1311*088332b5SXin Lirepresenting the character code point. 1312*088332b5SXin LiThis code point can be any value less than <em>2<sup>31</sup></em>. 1313*088332b5SXin Li(Lua uses the original UTF-8 specification here, 1314*088332b5SXin Liwhich is not restricted to valid Unicode code points.) 1315*088332b5SXin Li 1316*088332b5SXin Li 1317*088332b5SXin Li<p> 1318*088332b5SXin LiLiteral strings can also be defined using a long format 1319*088332b5SXin Lienclosed by <em>long brackets</em>. 1320*088332b5SXin LiWe define an <em>opening long bracket of level <em>n</em></em> as an opening 1321*088332b5SXin Lisquare bracket followed by <em>n</em> equal signs followed by another 1322*088332b5SXin Liopening square bracket. 1323*088332b5SXin LiSo, an opening long bracket of level 0 is written as <code>[[</code>, 1324*088332b5SXin Lian opening long bracket of level 1 is written as <code>[=[</code>, 1325*088332b5SXin Liand so on. 1326*088332b5SXin LiA <em>closing long bracket</em> is defined similarly; 1327*088332b5SXin Lifor instance, 1328*088332b5SXin Lia closing long bracket of level 4 is written as <code>]====]</code>. 1329*088332b5SXin LiA <em>long literal</em> starts with an opening long bracket of any level and 1330*088332b5SXin Liends at the first closing long bracket of the same level. 1331*088332b5SXin LiIt can contain any text except a closing bracket of the same level. 1332*088332b5SXin LiLiterals in this bracketed form can run for several lines, 1333*088332b5SXin Lido not interpret any escape sequences, 1334*088332b5SXin Liand ignore long brackets of any other level. 1335*088332b5SXin LiAny kind of end-of-line sequence 1336*088332b5SXin Li(carriage return, newline, carriage return followed by newline, 1337*088332b5SXin Lior newline followed by carriage return) 1338*088332b5SXin Liis converted to a simple newline. 1339*088332b5SXin LiWhen the opening long bracket is immediately followed by a newline, 1340*088332b5SXin Lithe newline is not included in the string. 1341*088332b5SXin Li 1342*088332b5SXin Li 1343*088332b5SXin Li<p> 1344*088332b5SXin LiAs an example, in a system using ASCII 1345*088332b5SXin Li(in which '<code>a</code>' is coded as 97, 1346*088332b5SXin Linewline is coded as 10, and '<code>1</code>' is coded as 49), 1347*088332b5SXin Lithe five literal strings below denote the same string: 1348*088332b5SXin Li 1349*088332b5SXin Li<pre> 1350*088332b5SXin Li a = 'alo\n123"' 1351*088332b5SXin Li a = "alo\n123\"" 1352*088332b5SXin Li a = '\97lo\10\04923"' 1353*088332b5SXin Li a = [[alo 1354*088332b5SXin Li 123"]] 1355*088332b5SXin Li a = [==[ 1356*088332b5SXin Li alo 1357*088332b5SXin Li 123"]==] 1358*088332b5SXin Li</pre> 1359*088332b5SXin Li 1360*088332b5SXin Li<p> 1361*088332b5SXin LiAny byte in a literal string not 1362*088332b5SXin Liexplicitly affected by the previous rules represents itself. 1363*088332b5SXin LiHowever, Lua opens files for parsing in text mode, 1364*088332b5SXin Liand the system's file functions may have problems with 1365*088332b5SXin Lisome control characters. 1366*088332b5SXin LiSo, it is safer to represent 1367*088332b5SXin Libinary data as a quoted literal with 1368*088332b5SXin Liexplicit escape sequences for the non-text characters. 1369*088332b5SXin Li 1370*088332b5SXin Li 1371*088332b5SXin Li<p> 1372*088332b5SXin LiA <em>numeric constant</em> (or <em>numeral</em>) 1373*088332b5SXin Lican be written with an optional fractional part 1374*088332b5SXin Liand an optional decimal exponent, 1375*088332b5SXin Limarked by a letter '<code>e</code>' or '<code>E</code>'. 1376*088332b5SXin LiLua also accepts hexadecimal constants, 1377*088332b5SXin Liwhich start with <code>0x</code> or <code>0X</code>. 1378*088332b5SXin LiHexadecimal constants also accept an optional fractional part 1379*088332b5SXin Liplus an optional binary exponent, 1380*088332b5SXin Limarked by a letter '<code>p</code>' or '<code>P</code>'. 1381*088332b5SXin Li 1382*088332b5SXin Li 1383*088332b5SXin Li<p> 1384*088332b5SXin LiA numeric constant with a radix point or an exponent 1385*088332b5SXin Lidenotes a float; 1386*088332b5SXin Liotherwise, 1387*088332b5SXin Liif its value fits in an integer or it is a hexadecimal constant, 1388*088332b5SXin Liit denotes an integer; 1389*088332b5SXin Liotherwise (that is, a decimal integer numeral that overflows), 1390*088332b5SXin Liit denotes a float. 1391*088332b5SXin LiHexadecimal numerals with neither a radix point nor an exponent 1392*088332b5SXin Lialways denote an integer value; 1393*088332b5SXin Liif the value overflows, it <em>wraps around</em> 1394*088332b5SXin Lito fit into a valid integer. 1395*088332b5SXin Li 1396*088332b5SXin Li 1397*088332b5SXin Li<p> 1398*088332b5SXin LiExamples of valid integer constants are 1399*088332b5SXin Li 1400*088332b5SXin Li<pre> 1401*088332b5SXin Li 3 345 0xff 0xBEBADA 1402*088332b5SXin Li</pre><p> 1403*088332b5SXin LiExamples of valid float constants are 1404*088332b5SXin Li 1405*088332b5SXin Li<pre> 1406*088332b5SXin Li 3.0 3.1416 314.16e-2 0.31416E1 34e1 1407*088332b5SXin Li 0x0.1E 0xA23p-4 0X1.921FB54442D18P+1 1408*088332b5SXin Li</pre> 1409*088332b5SXin Li 1410*088332b5SXin Li<p> 1411*088332b5SXin LiA <em>comment</em> starts with a double hyphen (<code>--</code>) 1412*088332b5SXin Lianywhere outside a string. 1413*088332b5SXin LiIf the text immediately after <code>--</code> is not an opening long bracket, 1414*088332b5SXin Lithe comment is a <em>short comment</em>, 1415*088332b5SXin Liwhich runs until the end of the line. 1416*088332b5SXin LiOtherwise, it is a <em>long comment</em>, 1417*088332b5SXin Liwhich runs until the corresponding closing long bracket. 1418*088332b5SXin Li 1419*088332b5SXin Li 1420*088332b5SXin Li 1421*088332b5SXin Li 1422*088332b5SXin Li 1423*088332b5SXin Li<h2>3.2 – <a name="3.2">Variables</a></h2> 1424*088332b5SXin Li 1425*088332b5SXin Li<p> 1426*088332b5SXin LiVariables are places that store values. 1427*088332b5SXin LiThere are three kinds of variables in Lua: 1428*088332b5SXin Liglobal variables, local variables, and table fields. 1429*088332b5SXin Li 1430*088332b5SXin Li 1431*088332b5SXin Li<p> 1432*088332b5SXin LiA single name can denote a global variable or a local variable 1433*088332b5SXin Li(or a function's formal parameter, 1434*088332b5SXin Liwhich is a particular kind of local variable): 1435*088332b5SXin Li 1436*088332b5SXin Li<pre> 1437*088332b5SXin Li var ::= Name 1438*088332b5SXin Li</pre><p> 1439*088332b5SXin LiName denotes identifiers (see <a href="#3.1">§3.1</a>). 1440*088332b5SXin Li 1441*088332b5SXin Li 1442*088332b5SXin Li<p> 1443*088332b5SXin LiAny variable name is assumed to be global unless explicitly declared 1444*088332b5SXin Lias a local (see <a href="#3.3.7">§3.3.7</a>). 1445*088332b5SXin LiLocal variables are <em>lexically scoped</em>: 1446*088332b5SXin Lilocal variables can be freely accessed by functions 1447*088332b5SXin Lidefined inside their scope (see <a href="#3.5">§3.5</a>). 1448*088332b5SXin Li 1449*088332b5SXin Li 1450*088332b5SXin Li<p> 1451*088332b5SXin LiBefore the first assignment to a variable, its value is <b>nil</b>. 1452*088332b5SXin Li 1453*088332b5SXin Li 1454*088332b5SXin Li<p> 1455*088332b5SXin LiSquare brackets are used to index a table: 1456*088332b5SXin Li 1457*088332b5SXin Li<pre> 1458*088332b5SXin Li var ::= prefixexp ‘<b>[</b>’ exp ‘<b>]</b>’ 1459*088332b5SXin Li</pre><p> 1460*088332b5SXin LiThe meaning of accesses to table fields can be changed via metatables 1461*088332b5SXin Li(see <a href="#2.4">§2.4</a>). 1462*088332b5SXin Li 1463*088332b5SXin Li 1464*088332b5SXin Li<p> 1465*088332b5SXin LiThe syntax <code>var.Name</code> is just syntactic sugar for 1466*088332b5SXin Li<code>var["Name"]</code>: 1467*088332b5SXin Li 1468*088332b5SXin Li<pre> 1469*088332b5SXin Li var ::= prefixexp ‘<b>.</b>’ Name 1470*088332b5SXin Li</pre> 1471*088332b5SXin Li 1472*088332b5SXin Li<p> 1473*088332b5SXin LiAn access to a global variable <code>x</code> 1474*088332b5SXin Liis equivalent to <code>_ENV.x</code>. 1475*088332b5SXin LiDue to the way that chunks are compiled, 1476*088332b5SXin Lithe variable <code>_ENV</code> itself is never global (see <a href="#2.2">§2.2</a>). 1477*088332b5SXin Li 1478*088332b5SXin Li 1479*088332b5SXin Li 1480*088332b5SXin Li 1481*088332b5SXin Li 1482*088332b5SXin Li<h2>3.3 – <a name="3.3">Statements</a></h2> 1483*088332b5SXin Li 1484*088332b5SXin Li 1485*088332b5SXin Li 1486*088332b5SXin Li<p> 1487*088332b5SXin LiLua supports an almost conventional set of statements, 1488*088332b5SXin Lisimilar to those in other conventional languages. 1489*088332b5SXin LiThis set includes 1490*088332b5SXin Liblocks, assignments, control structures, function calls, 1491*088332b5SXin Liand variable declarations. 1492*088332b5SXin Li 1493*088332b5SXin Li 1494*088332b5SXin Li 1495*088332b5SXin Li 1496*088332b5SXin Li 1497*088332b5SXin Li<h3>3.3.1 – <a name="3.3.1">Blocks</a></h3> 1498*088332b5SXin Li 1499*088332b5SXin Li<p> 1500*088332b5SXin LiA block is a list of statements, 1501*088332b5SXin Liwhich are executed sequentially: 1502*088332b5SXin Li 1503*088332b5SXin Li<pre> 1504*088332b5SXin Li block ::= {stat} 1505*088332b5SXin Li</pre><p> 1506*088332b5SXin LiLua has <em>empty statements</em> 1507*088332b5SXin Lithat allow you to separate statements with semicolons, 1508*088332b5SXin Listart a block with a semicolon 1509*088332b5SXin Lior write two semicolons in sequence: 1510*088332b5SXin Li 1511*088332b5SXin Li<pre> 1512*088332b5SXin Li stat ::= ‘<b>;</b>’ 1513*088332b5SXin Li</pre> 1514*088332b5SXin Li 1515*088332b5SXin Li<p> 1516*088332b5SXin LiBoth function calls and assignments 1517*088332b5SXin Lican start with an open parenthesis. 1518*088332b5SXin LiThis possibility leads to an ambiguity in Lua's grammar. 1519*088332b5SXin LiConsider the following fragment: 1520*088332b5SXin Li 1521*088332b5SXin Li<pre> 1522*088332b5SXin Li a = b + c 1523*088332b5SXin Li (print or io.write)('done') 1524*088332b5SXin Li</pre><p> 1525*088332b5SXin LiThe grammar could see this fragment in two ways: 1526*088332b5SXin Li 1527*088332b5SXin Li<pre> 1528*088332b5SXin Li a = b + c(print or io.write)('done') 1529*088332b5SXin Li 1530*088332b5SXin Li a = b + c; (print or io.write)('done') 1531*088332b5SXin Li</pre><p> 1532*088332b5SXin LiThe current parser always sees such constructions 1533*088332b5SXin Liin the first way, 1534*088332b5SXin Liinterpreting the open parenthesis 1535*088332b5SXin Lias the start of the arguments to a call. 1536*088332b5SXin LiTo avoid this ambiguity, 1537*088332b5SXin Liit is a good practice to always precede with a semicolon 1538*088332b5SXin Listatements that start with a parenthesis: 1539*088332b5SXin Li 1540*088332b5SXin Li<pre> 1541*088332b5SXin Li ;(print or io.write)('done') 1542*088332b5SXin Li</pre> 1543*088332b5SXin Li 1544*088332b5SXin Li<p> 1545*088332b5SXin LiA block can be explicitly delimited to produce a single statement: 1546*088332b5SXin Li 1547*088332b5SXin Li<pre> 1548*088332b5SXin Li stat ::= <b>do</b> block <b>end</b> 1549*088332b5SXin Li</pre><p> 1550*088332b5SXin LiExplicit blocks are useful 1551*088332b5SXin Lito control the scope of variable declarations. 1552*088332b5SXin LiExplicit blocks are also sometimes used to 1553*088332b5SXin Liadd a <b>return</b> statement in the middle 1554*088332b5SXin Liof another block (see <a href="#3.3.4">§3.3.4</a>). 1555*088332b5SXin Li 1556*088332b5SXin Li 1557*088332b5SXin Li 1558*088332b5SXin Li 1559*088332b5SXin Li 1560*088332b5SXin Li<h3>3.3.2 – <a name="3.3.2">Chunks</a></h3> 1561*088332b5SXin Li 1562*088332b5SXin Li<p> 1563*088332b5SXin LiThe unit of compilation of Lua is called a <em>chunk</em>. 1564*088332b5SXin LiSyntactically, 1565*088332b5SXin Lia chunk is simply a block: 1566*088332b5SXin Li 1567*088332b5SXin Li<pre> 1568*088332b5SXin Li chunk ::= block 1569*088332b5SXin Li</pre> 1570*088332b5SXin Li 1571*088332b5SXin Li<p> 1572*088332b5SXin LiLua handles a chunk as the body of an anonymous function 1573*088332b5SXin Liwith a variable number of arguments 1574*088332b5SXin Li(see <a href="#3.4.11">§3.4.11</a>). 1575*088332b5SXin LiAs such, chunks can define local variables, 1576*088332b5SXin Lireceive arguments, and return values. 1577*088332b5SXin LiMoreover, such anonymous function is compiled as in the 1578*088332b5SXin Liscope of an external local variable called <code>_ENV</code> (see <a href="#2.2">§2.2</a>). 1579*088332b5SXin LiThe resulting function always has <code>_ENV</code> as its only external variable, 1580*088332b5SXin Lieven if it does not use that variable. 1581*088332b5SXin Li 1582*088332b5SXin Li 1583*088332b5SXin Li<p> 1584*088332b5SXin LiA chunk can be stored in a file or in a string inside the host program. 1585*088332b5SXin LiTo execute a chunk, 1586*088332b5SXin LiLua first <em>loads</em> it, 1587*088332b5SXin Liprecompiling the chunk's code into instructions for a virtual machine, 1588*088332b5SXin Liand then Lua executes the compiled code 1589*088332b5SXin Liwith an interpreter for the virtual machine. 1590*088332b5SXin Li 1591*088332b5SXin Li 1592*088332b5SXin Li<p> 1593*088332b5SXin LiChunks can also be precompiled into binary form; 1594*088332b5SXin Lisee the program <code>luac</code> and the function <a href="#pdf-string.dump"><code>string.dump</code></a> for details. 1595*088332b5SXin LiPrograms in source and compiled forms are interchangeable; 1596*088332b5SXin LiLua automatically detects the file type and acts accordingly (see <a href="#pdf-load"><code>load</code></a>). 1597*088332b5SXin Li 1598*088332b5SXin Li 1599*088332b5SXin Li 1600*088332b5SXin Li 1601*088332b5SXin Li 1602*088332b5SXin Li<h3>3.3.3 – <a name="3.3.3">Assignment</a></h3> 1603*088332b5SXin Li 1604*088332b5SXin Li<p> 1605*088332b5SXin LiLua allows multiple assignments. 1606*088332b5SXin LiTherefore, the syntax for assignment 1607*088332b5SXin Lidefines a list of variables on the left side 1608*088332b5SXin Liand a list of expressions on the right side. 1609*088332b5SXin LiThe elements in both lists are separated by commas: 1610*088332b5SXin Li 1611*088332b5SXin Li<pre> 1612*088332b5SXin Li stat ::= varlist ‘<b>=</b>’ explist 1613*088332b5SXin Li varlist ::= var {‘<b>,</b>’ var} 1614*088332b5SXin Li explist ::= exp {‘<b>,</b>’ exp} 1615*088332b5SXin Li</pre><p> 1616*088332b5SXin LiExpressions are discussed in <a href="#3.4">§3.4</a>. 1617*088332b5SXin Li 1618*088332b5SXin Li 1619*088332b5SXin Li<p> 1620*088332b5SXin LiBefore the assignment, 1621*088332b5SXin Lithe list of values is <em>adjusted</em> to the length of 1622*088332b5SXin Lithe list of variables. 1623*088332b5SXin LiIf there are more values than needed, 1624*088332b5SXin Lithe excess values are thrown away. 1625*088332b5SXin LiIf there are fewer values than needed, 1626*088332b5SXin Lithe list is extended with <b>nil</b>'s. 1627*088332b5SXin LiIf the list of expressions ends with a function call, 1628*088332b5SXin Lithen all values returned by that call enter the list of values, 1629*088332b5SXin Libefore the adjustment 1630*088332b5SXin Li(except when the call is enclosed in parentheses; see <a href="#3.4">§3.4</a>). 1631*088332b5SXin Li 1632*088332b5SXin Li 1633*088332b5SXin Li<p> 1634*088332b5SXin LiThe assignment statement first evaluates all its expressions 1635*088332b5SXin Liand only then the assignments are performed. 1636*088332b5SXin LiThus the code 1637*088332b5SXin Li 1638*088332b5SXin Li<pre> 1639*088332b5SXin Li i = 3 1640*088332b5SXin Li i, a[i] = i+1, 20 1641*088332b5SXin Li</pre><p> 1642*088332b5SXin Lisets <code>a[3]</code> to 20, without affecting <code>a[4]</code> 1643*088332b5SXin Libecause the <code>i</code> in <code>a[i]</code> is evaluated (to 3) 1644*088332b5SXin Libefore it is assigned 4. 1645*088332b5SXin LiSimilarly, the line 1646*088332b5SXin Li 1647*088332b5SXin Li<pre> 1648*088332b5SXin Li x, y = y, x 1649*088332b5SXin Li</pre><p> 1650*088332b5SXin Liexchanges the values of <code>x</code> and <code>y</code>, 1651*088332b5SXin Liand 1652*088332b5SXin Li 1653*088332b5SXin Li<pre> 1654*088332b5SXin Li x, y, z = y, z, x 1655*088332b5SXin Li</pre><p> 1656*088332b5SXin Licyclically permutes the values of <code>x</code>, <code>y</code>, and <code>z</code>. 1657*088332b5SXin Li 1658*088332b5SXin Li 1659*088332b5SXin Li<p> 1660*088332b5SXin LiAn assignment to a global name <code>x = val</code> 1661*088332b5SXin Liis equivalent to the assignment 1662*088332b5SXin Li<code>_ENV.x = val</code> (see <a href="#2.2">§2.2</a>). 1663*088332b5SXin Li 1664*088332b5SXin Li 1665*088332b5SXin Li<p> 1666*088332b5SXin LiThe meaning of assignments to table fields and 1667*088332b5SXin Liglobal variables (which are actually table fields, too) 1668*088332b5SXin Lican be changed via metatables (see <a href="#2.4">§2.4</a>). 1669*088332b5SXin Li 1670*088332b5SXin Li 1671*088332b5SXin Li 1672*088332b5SXin Li 1673*088332b5SXin Li 1674*088332b5SXin Li<h3>3.3.4 – <a name="3.3.4">Control Structures</a></h3><p> 1675*088332b5SXin LiThe control structures 1676*088332b5SXin Li<b>if</b>, <b>while</b>, and <b>repeat</b> have the usual meaning and 1677*088332b5SXin Lifamiliar syntax: 1678*088332b5SXin Li 1679*088332b5SXin Li 1680*088332b5SXin Li 1681*088332b5SXin Li 1682*088332b5SXin Li<pre> 1683*088332b5SXin Li stat ::= <b>while</b> exp <b>do</b> block <b>end</b> 1684*088332b5SXin Li stat ::= <b>repeat</b> block <b>until</b> exp 1685*088332b5SXin Li stat ::= <b>if</b> exp <b>then</b> block {<b>elseif</b> exp <b>then</b> block} [<b>else</b> block] <b>end</b> 1686*088332b5SXin Li</pre><p> 1687*088332b5SXin LiLua also has a <b>for</b> statement, in two flavors (see <a href="#3.3.5">§3.3.5</a>). 1688*088332b5SXin Li 1689*088332b5SXin Li 1690*088332b5SXin Li<p> 1691*088332b5SXin LiThe condition expression of a 1692*088332b5SXin Licontrol structure can return any value. 1693*088332b5SXin LiBoth <b>false</b> and <b>nil</b> test false. 1694*088332b5SXin LiAll values different from <b>nil</b> and <b>false</b> test true. 1695*088332b5SXin LiIn particular, the number 0 and the empty string also test true. 1696*088332b5SXin Li 1697*088332b5SXin Li 1698*088332b5SXin Li<p> 1699*088332b5SXin LiIn the <b>repeat</b>–<b>until</b> loop, 1700*088332b5SXin Lithe inner block does not end at the <b>until</b> keyword, 1701*088332b5SXin Libut only after the condition. 1702*088332b5SXin LiSo, the condition can refer to local variables 1703*088332b5SXin Lideclared inside the loop block. 1704*088332b5SXin Li 1705*088332b5SXin Li 1706*088332b5SXin Li<p> 1707*088332b5SXin LiThe <b>goto</b> statement transfers the program control to a label. 1708*088332b5SXin LiFor syntactical reasons, 1709*088332b5SXin Lilabels in Lua are considered statements too: 1710*088332b5SXin Li 1711*088332b5SXin Li 1712*088332b5SXin Li 1713*088332b5SXin Li<pre> 1714*088332b5SXin Li stat ::= <b>goto</b> Name 1715*088332b5SXin Li stat ::= label 1716*088332b5SXin Li label ::= ‘<b>::</b>’ Name ‘<b>::</b>’ 1717*088332b5SXin Li</pre> 1718*088332b5SXin Li 1719*088332b5SXin Li<p> 1720*088332b5SXin LiA label is visible in the entire block where it is defined, 1721*088332b5SXin Liexcept inside nested functions. 1722*088332b5SXin LiA goto may jump to any visible label as long as it does not 1723*088332b5SXin Lienter into the scope of a local variable. 1724*088332b5SXin LiA label should not be declared 1725*088332b5SXin Liwhere a label with the same name is visible, 1726*088332b5SXin Lieven if this other label has been declared in an enclosing block. 1727*088332b5SXin Li 1728*088332b5SXin Li 1729*088332b5SXin Li<p> 1730*088332b5SXin LiLabels and empty statements are called <em>void statements</em>, 1731*088332b5SXin Lias they perform no actions. 1732*088332b5SXin Li 1733*088332b5SXin Li 1734*088332b5SXin Li<p> 1735*088332b5SXin LiThe <b>break</b> statement terminates the execution of a 1736*088332b5SXin Li<b>while</b>, <b>repeat</b>, or <b>for</b> loop, 1737*088332b5SXin Liskipping to the next statement after the loop: 1738*088332b5SXin Li 1739*088332b5SXin Li 1740*088332b5SXin Li<pre> 1741*088332b5SXin Li stat ::= <b>break</b> 1742*088332b5SXin Li</pre><p> 1743*088332b5SXin LiA <b>break</b> ends the innermost enclosing loop. 1744*088332b5SXin Li 1745*088332b5SXin Li 1746*088332b5SXin Li<p> 1747*088332b5SXin LiThe <b>return</b> statement is used to return values 1748*088332b5SXin Lifrom a function or a chunk 1749*088332b5SXin Li(which is handled as an anonymous function). 1750*088332b5SXin Li 1751*088332b5SXin LiFunctions can return more than one value, 1752*088332b5SXin Liso the syntax for the <b>return</b> statement is 1753*088332b5SXin Li 1754*088332b5SXin Li<pre> 1755*088332b5SXin Li stat ::= <b>return</b> [explist] [‘<b>;</b>’] 1756*088332b5SXin Li</pre> 1757*088332b5SXin Li 1758*088332b5SXin Li<p> 1759*088332b5SXin LiThe <b>return</b> statement can only be written 1760*088332b5SXin Lias the last statement of a block. 1761*088332b5SXin LiIf it is necessary to <b>return</b> in the middle of a block, 1762*088332b5SXin Lithen an explicit inner block can be used, 1763*088332b5SXin Lias in the idiom <code>do return end</code>, 1764*088332b5SXin Libecause now <b>return</b> is the last statement in its (inner) block. 1765*088332b5SXin Li 1766*088332b5SXin Li 1767*088332b5SXin Li 1768*088332b5SXin Li 1769*088332b5SXin Li 1770*088332b5SXin Li<h3>3.3.5 – <a name="3.3.5">For Statement</a></h3> 1771*088332b5SXin Li 1772*088332b5SXin Li<p> 1773*088332b5SXin Li 1774*088332b5SXin LiThe <b>for</b> statement has two forms: 1775*088332b5SXin Lione numerical and one generic. 1776*088332b5SXin Li 1777*088332b5SXin Li 1778*088332b5SXin Li 1779*088332b5SXin Li<h4>The numerical <b>for</b> loop</h4> 1780*088332b5SXin Li 1781*088332b5SXin Li<p> 1782*088332b5SXin LiThe numerical <b>for</b> loop repeats a block of code while a 1783*088332b5SXin Licontrol variable goes through an arithmetic progression. 1784*088332b5SXin LiIt has the following syntax: 1785*088332b5SXin Li 1786*088332b5SXin Li<pre> 1787*088332b5SXin Li stat ::= <b>for</b> Name ‘<b>=</b>’ exp ‘<b>,</b>’ exp [‘<b>,</b>’ exp] <b>do</b> block <b>end</b> 1788*088332b5SXin Li</pre><p> 1789*088332b5SXin LiThe given identifier (Name) defines the control variable, 1790*088332b5SXin Liwhich is a new variable local to the loop body (<em>block</em>). 1791*088332b5SXin Li 1792*088332b5SXin Li 1793*088332b5SXin Li<p> 1794*088332b5SXin LiThe loop starts by evaluating once the three control expressions. 1795*088332b5SXin LiTheir values are called respectively 1796*088332b5SXin Lithe <em>initial value</em>, the <em>limit</em>, and the <em>step</em>. 1797*088332b5SXin LiIf the step is absent, it defaults to 1. 1798*088332b5SXin Li 1799*088332b5SXin Li 1800*088332b5SXin Li<p> 1801*088332b5SXin LiIf both the initial value and the step are integers, 1802*088332b5SXin Lithe loop is done with integers; 1803*088332b5SXin Linote that the limit may not be an integer. 1804*088332b5SXin LiOtherwise, the three values are converted to 1805*088332b5SXin Lifloats and the loop is done with floats. 1806*088332b5SXin LiBeware of floating-point accuracy in this case. 1807*088332b5SXin Li 1808*088332b5SXin Li 1809*088332b5SXin Li<p> 1810*088332b5SXin LiAfter that initialization, 1811*088332b5SXin Lithe loop body is repeated with the value of the control variable 1812*088332b5SXin Ligoing through an arithmetic progression, 1813*088332b5SXin Listarting at the initial value, 1814*088332b5SXin Liwith a common difference given by the step. 1815*088332b5SXin LiA negative step makes a decreasing sequence; 1816*088332b5SXin Lia step equal to zero raises an error. 1817*088332b5SXin LiThe loop continues while the value is less than 1818*088332b5SXin Lior equal to the limit 1819*088332b5SXin Li(greater than or equal to for a negative step). 1820*088332b5SXin LiIf the initial value is already greater than the limit 1821*088332b5SXin Li(or less than, if the step is negative), 1822*088332b5SXin Lithe body is not executed. 1823*088332b5SXin Li 1824*088332b5SXin Li 1825*088332b5SXin Li<p> 1826*088332b5SXin LiFor integer loops, 1827*088332b5SXin Lithe control variable never wraps around; 1828*088332b5SXin Liinstead, the loop ends in case of an overflow. 1829*088332b5SXin Li 1830*088332b5SXin Li 1831*088332b5SXin Li<p> 1832*088332b5SXin LiYou should not change the value of the control variable 1833*088332b5SXin Liduring the loop. 1834*088332b5SXin LiIf you need its value after the loop, 1835*088332b5SXin Liassign it to another variable before exiting the loop. 1836*088332b5SXin Li 1837*088332b5SXin Li 1838*088332b5SXin Li 1839*088332b5SXin Li 1840*088332b5SXin Li 1841*088332b5SXin Li<h4>The generic <b>for</b> loop</h4> 1842*088332b5SXin Li 1843*088332b5SXin Li<p> 1844*088332b5SXin LiThe generic <b>for</b> statement works over functions, 1845*088332b5SXin Licalled <em>iterators</em>. 1846*088332b5SXin LiOn each iteration, the iterator function is called to produce a new value, 1847*088332b5SXin Listopping when this new value is <b>nil</b>. 1848*088332b5SXin LiThe generic <b>for</b> loop has the following syntax: 1849*088332b5SXin Li 1850*088332b5SXin Li<pre> 1851*088332b5SXin Li stat ::= <b>for</b> namelist <b>in</b> explist <b>do</b> block <b>end</b> 1852*088332b5SXin Li namelist ::= Name {‘<b>,</b>’ Name} 1853*088332b5SXin Li</pre><p> 1854*088332b5SXin LiA <b>for</b> statement like 1855*088332b5SXin Li 1856*088332b5SXin Li<pre> 1857*088332b5SXin Li for <em>var_1</em>, ···, <em>var_n</em> in <em>explist</em> do <em>body</em> end 1858*088332b5SXin Li</pre><p> 1859*088332b5SXin Liworks as follows. 1860*088332b5SXin Li 1861*088332b5SXin Li 1862*088332b5SXin Li<p> 1863*088332b5SXin LiThe names <em>var_i</em> declare loop variables local to the loop body. 1864*088332b5SXin LiThe first of these variables is the <em>control variable</em>. 1865*088332b5SXin Li 1866*088332b5SXin Li 1867*088332b5SXin Li<p> 1868*088332b5SXin LiThe loop starts by evaluating <em>explist</em> 1869*088332b5SXin Lito produce four values: 1870*088332b5SXin Lian <em>iterator function</em>, 1871*088332b5SXin Lia <em>state</em>, 1872*088332b5SXin Lian initial value for the control variable, 1873*088332b5SXin Liand a <em>closing value</em>. 1874*088332b5SXin Li 1875*088332b5SXin Li 1876*088332b5SXin Li<p> 1877*088332b5SXin LiThen, at each iteration, 1878*088332b5SXin LiLua calls the iterator function with two arguments: 1879*088332b5SXin Lithe state and the control variable. 1880*088332b5SXin LiThe results from this call are then assigned to the loop variables, 1881*088332b5SXin Lifollowing the rules of multiple assignments (see <a href="#3.3.3">§3.3.3</a>). 1882*088332b5SXin LiIf the control variable becomes <b>nil</b>, 1883*088332b5SXin Lithe loop terminates. 1884*088332b5SXin LiOtherwise, the body is executed and the loop goes 1885*088332b5SXin Lito the next iteration. 1886*088332b5SXin Li 1887*088332b5SXin Li 1888*088332b5SXin Li<p> 1889*088332b5SXin LiThe closing value behaves like a 1890*088332b5SXin Lito-be-closed variable (see <a href="#3.3.8">§3.3.8</a>), 1891*088332b5SXin Liwhich can be used to release resources when the loop ends. 1892*088332b5SXin LiOtherwise, it does not interfere with the loop. 1893*088332b5SXin Li 1894*088332b5SXin Li 1895*088332b5SXin Li<p> 1896*088332b5SXin LiYou should not change the value of the control variable 1897*088332b5SXin Liduring the loop. 1898*088332b5SXin Li 1899*088332b5SXin Li 1900*088332b5SXin Li 1901*088332b5SXin Li 1902*088332b5SXin Li 1903*088332b5SXin Li 1904*088332b5SXin Li 1905*088332b5SXin Li<h3>3.3.6 – <a name="3.3.6">Function Calls as Statements</a></h3><p> 1906*088332b5SXin LiTo allow possible side-effects, 1907*088332b5SXin Lifunction calls can be executed as statements: 1908*088332b5SXin Li 1909*088332b5SXin Li<pre> 1910*088332b5SXin Li stat ::= functioncall 1911*088332b5SXin Li</pre><p> 1912*088332b5SXin LiIn this case, all returned values are thrown away. 1913*088332b5SXin LiFunction calls are explained in <a href="#3.4.10">§3.4.10</a>. 1914*088332b5SXin Li 1915*088332b5SXin Li 1916*088332b5SXin Li 1917*088332b5SXin Li 1918*088332b5SXin Li 1919*088332b5SXin Li<h3>3.3.7 – <a name="3.3.7">Local Declarations</a></h3><p> 1920*088332b5SXin LiLocal variables can be declared anywhere inside a block. 1921*088332b5SXin LiThe declaration can include an initialization: 1922*088332b5SXin Li 1923*088332b5SXin Li<pre> 1924*088332b5SXin Li stat ::= <b>local</b> attnamelist [‘<b>=</b>’ explist] 1925*088332b5SXin Li attnamelist ::= Name attrib {‘<b>,</b>’ Name attrib} 1926*088332b5SXin Li</pre><p> 1927*088332b5SXin LiIf present, an initial assignment has the same semantics 1928*088332b5SXin Liof a multiple assignment (see <a href="#3.3.3">§3.3.3</a>). 1929*088332b5SXin LiOtherwise, all variables are initialized with <b>nil</b>. 1930*088332b5SXin Li 1931*088332b5SXin Li 1932*088332b5SXin Li<p> 1933*088332b5SXin LiEach variable name may be postfixed by an attribute 1934*088332b5SXin Li(a name between angle brackets): 1935*088332b5SXin Li 1936*088332b5SXin Li<pre> 1937*088332b5SXin Li attrib ::= [‘<b><</b>’ Name ‘<b>></b>’] 1938*088332b5SXin Li</pre><p> 1939*088332b5SXin LiThere are two possible attributes: 1940*088332b5SXin Li<code>const</code>, which declares a constant variable, 1941*088332b5SXin Lithat is, a variable that cannot be assigned to 1942*088332b5SXin Liafter its initialization; 1943*088332b5SXin Liand <code>close</code>, which declares a to-be-closed variable (see <a href="#3.3.8">§3.3.8</a>). 1944*088332b5SXin LiA list of variables can contain at most one to-be-closed variable. 1945*088332b5SXin Li 1946*088332b5SXin Li 1947*088332b5SXin Li<p> 1948*088332b5SXin LiA chunk is also a block (see <a href="#3.3.2">§3.3.2</a>), 1949*088332b5SXin Liand so local variables can be declared in a chunk outside any explicit block. 1950*088332b5SXin Li 1951*088332b5SXin Li 1952*088332b5SXin Li<p> 1953*088332b5SXin LiThe visibility rules for local variables are explained in <a href="#3.5">§3.5</a>. 1954*088332b5SXin Li 1955*088332b5SXin Li 1956*088332b5SXin Li 1957*088332b5SXin Li 1958*088332b5SXin Li 1959*088332b5SXin Li<h3>3.3.8 – <a name="3.3.8">To-be-closed Variables</a></h3> 1960*088332b5SXin Li 1961*088332b5SXin Li<p> 1962*088332b5SXin LiA to-be-closed variable behaves like a constant local variable, 1963*088332b5SXin Liexcept that its value is <em>closed</em> whenever the variable 1964*088332b5SXin Ligoes out of scope, including normal block termination, 1965*088332b5SXin Liexiting its block by <b>break</b>/<b>goto</b>/<b>return</b>, 1966*088332b5SXin Lior exiting by an error. 1967*088332b5SXin Li 1968*088332b5SXin Li 1969*088332b5SXin Li<p> 1970*088332b5SXin LiHere, to <em>close</em> a value means 1971*088332b5SXin Lito call its <code>__close</code> metamethod. 1972*088332b5SXin LiWhen calling the metamethod, 1973*088332b5SXin Lithe value itself is passed as the first argument 1974*088332b5SXin Liand the error object that caused the exit (if any) 1975*088332b5SXin Liis passed as a second argument; 1976*088332b5SXin Liif there was no error, the second argument is <b>nil</b>. 1977*088332b5SXin Li 1978*088332b5SXin Li 1979*088332b5SXin Li<p> 1980*088332b5SXin LiThe value assigned to a to-be-closed variable 1981*088332b5SXin Limust have a <code>__close</code> metamethod 1982*088332b5SXin Lior be a false value. 1983*088332b5SXin Li(<b>nil</b> and <b>false</b> are ignored as to-be-closed values.) 1984*088332b5SXin Li 1985*088332b5SXin Li 1986*088332b5SXin Li<p> 1987*088332b5SXin LiIf several to-be-closed variables go out of scope at the same event, 1988*088332b5SXin Lithey are closed in the reverse order that they were declared. 1989*088332b5SXin Li 1990*088332b5SXin Li 1991*088332b5SXin Li<p> 1992*088332b5SXin LiIf there is any error while running a closing method, 1993*088332b5SXin Lithat error is handled like an error in the regular code 1994*088332b5SXin Liwhere the variable was defined. 1995*088332b5SXin LiHowever, Lua may call the method one more time. 1996*088332b5SXin Li 1997*088332b5SXin Li 1998*088332b5SXin Li<p> 1999*088332b5SXin LiAfter an error, 2000*088332b5SXin Lithe other pending closing methods will still be called. 2001*088332b5SXin LiErrors in these methods 2002*088332b5SXin Liinterrupt the respective method and generate a warning, 2003*088332b5SXin Libut are otherwise ignored; 2004*088332b5SXin Lithe error reported is only the original one. 2005*088332b5SXin Li 2006*088332b5SXin Li 2007*088332b5SXin Li<p> 2008*088332b5SXin LiIf a coroutine yields and is never resumed again, 2009*088332b5SXin Lisome variables may never go out of scope, 2010*088332b5SXin Liand therefore they will never be closed. 2011*088332b5SXin Li(These variables are the ones created inside the coroutine 2012*088332b5SXin Liand in scope at the point where the coroutine yielded.) 2013*088332b5SXin LiSimilarly, if a coroutine ends with an error, 2014*088332b5SXin Liit does not unwind its stack, 2015*088332b5SXin Liso it does not close any variable. 2016*088332b5SXin LiIn both cases, 2017*088332b5SXin Liyou can either use finalizers 2018*088332b5SXin Lior call <a href="#pdf-coroutine.close"><code>coroutine.close</code></a> to close the variables. 2019*088332b5SXin LiHowever, if the coroutine was created 2020*088332b5SXin Lithrough <a href="#pdf-coroutine.wrap"><code>coroutine.wrap</code></a>, 2021*088332b5SXin Lithen its corresponding function will close the coroutine 2022*088332b5SXin Liin case of errors. 2023*088332b5SXin Li 2024*088332b5SXin Li 2025*088332b5SXin Li 2026*088332b5SXin Li 2027*088332b5SXin Li 2028*088332b5SXin Li 2029*088332b5SXin Li 2030*088332b5SXin Li<h2>3.4 – <a name="3.4">Expressions</a></h2> 2031*088332b5SXin Li 2032*088332b5SXin Li 2033*088332b5SXin Li 2034*088332b5SXin Li<p> 2035*088332b5SXin LiThe basic expressions in Lua are the following: 2036*088332b5SXin Li 2037*088332b5SXin Li<pre> 2038*088332b5SXin Li exp ::= prefixexp 2039*088332b5SXin Li exp ::= <b>nil</b> | <b>false</b> | <b>true</b> 2040*088332b5SXin Li exp ::= Numeral 2041*088332b5SXin Li exp ::= LiteralString 2042*088332b5SXin Li exp ::= functiondef 2043*088332b5SXin Li exp ::= tableconstructor 2044*088332b5SXin Li exp ::= ‘<b>...</b>’ 2045*088332b5SXin Li exp ::= exp binop exp 2046*088332b5SXin Li exp ::= unop exp 2047*088332b5SXin Li prefixexp ::= var | functioncall | ‘<b>(</b>’ exp ‘<b>)</b>’ 2048*088332b5SXin Li</pre> 2049*088332b5SXin Li 2050*088332b5SXin Li<p> 2051*088332b5SXin LiNumerals and literal strings are explained in <a href="#3.1">§3.1</a>; 2052*088332b5SXin Livariables are explained in <a href="#3.2">§3.2</a>; 2053*088332b5SXin Lifunction definitions are explained in <a href="#3.4.11">§3.4.11</a>; 2054*088332b5SXin Lifunction calls are explained in <a href="#3.4.10">§3.4.10</a>; 2055*088332b5SXin Litable constructors are explained in <a href="#3.4.9">§3.4.9</a>. 2056*088332b5SXin LiVararg expressions, 2057*088332b5SXin Lidenoted by three dots ('<code>...</code>'), can only be used when 2058*088332b5SXin Lidirectly inside a vararg function; 2059*088332b5SXin Lithey are explained in <a href="#3.4.11">§3.4.11</a>. 2060*088332b5SXin Li 2061*088332b5SXin Li 2062*088332b5SXin Li<p> 2063*088332b5SXin LiBinary operators comprise arithmetic operators (see <a href="#3.4.1">§3.4.1</a>), 2064*088332b5SXin Libitwise operators (see <a href="#3.4.2">§3.4.2</a>), 2065*088332b5SXin Lirelational operators (see <a href="#3.4.4">§3.4.4</a>), logical operators (see <a href="#3.4.5">§3.4.5</a>), 2066*088332b5SXin Liand the concatenation operator (see <a href="#3.4.6">§3.4.6</a>). 2067*088332b5SXin LiUnary operators comprise the unary minus (see <a href="#3.4.1">§3.4.1</a>), 2068*088332b5SXin Lithe unary bitwise NOT (see <a href="#3.4.2">§3.4.2</a>), 2069*088332b5SXin Lithe unary logical <b>not</b> (see <a href="#3.4.5">§3.4.5</a>), 2070*088332b5SXin Liand the unary <em>length operator</em> (see <a href="#3.4.7">§3.4.7</a>). 2071*088332b5SXin Li 2072*088332b5SXin Li 2073*088332b5SXin Li<p> 2074*088332b5SXin LiBoth function calls and vararg expressions can result in multiple values. 2075*088332b5SXin LiIf a function call is used as a statement (see <a href="#3.3.6">§3.3.6</a>), 2076*088332b5SXin Lithen its return list is adjusted to zero elements, 2077*088332b5SXin Lithus discarding all returned values. 2078*088332b5SXin LiIf an expression is used as the last (or the only) element 2079*088332b5SXin Liof a list of expressions, 2080*088332b5SXin Lithen no adjustment is made 2081*088332b5SXin Li(unless the expression is enclosed in parentheses). 2082*088332b5SXin LiIn all other contexts, 2083*088332b5SXin LiLua adjusts the result list to one element, 2084*088332b5SXin Lieither discarding all values except the first one 2085*088332b5SXin Lior adding a single <b>nil</b> if there are no values. 2086*088332b5SXin Li 2087*088332b5SXin Li 2088*088332b5SXin Li<p> 2089*088332b5SXin LiHere are some examples: 2090*088332b5SXin Li 2091*088332b5SXin Li<pre> 2092*088332b5SXin Li f() -- adjusted to 0 results 2093*088332b5SXin Li g(f(), x) -- f() is adjusted to 1 result 2094*088332b5SXin Li g(x, f()) -- g gets x plus all results from f() 2095*088332b5SXin Li a,b,c = f(), x -- f() is adjusted to 1 result (c gets nil) 2096*088332b5SXin Li a,b = ... -- a gets the first vararg argument, b gets 2097*088332b5SXin Li -- the second (both a and b can get nil if there 2098*088332b5SXin Li -- is no corresponding vararg argument) 2099*088332b5SXin Li 2100*088332b5SXin Li a,b,c = x, f() -- f() is adjusted to 2 results 2101*088332b5SXin Li a,b,c = f() -- f() is adjusted to 3 results 2102*088332b5SXin Li return f() -- returns all results from f() 2103*088332b5SXin Li return ... -- returns all received vararg arguments 2104*088332b5SXin Li return x,y,f() -- returns x, y, and all results from f() 2105*088332b5SXin Li {f()} -- creates a list with all results from f() 2106*088332b5SXin Li {...} -- creates a list with all vararg arguments 2107*088332b5SXin Li {f(), nil} -- f() is adjusted to 1 result 2108*088332b5SXin Li</pre> 2109*088332b5SXin Li 2110*088332b5SXin Li<p> 2111*088332b5SXin LiAny expression enclosed in parentheses always results in only one value. 2112*088332b5SXin LiThus, 2113*088332b5SXin Li<code>(f(x,y,z))</code> is always a single value, 2114*088332b5SXin Lieven if <code>f</code> returns several values. 2115*088332b5SXin Li(The value of <code>(f(x,y,z))</code> is the first value returned by <code>f</code> 2116*088332b5SXin Lior <b>nil</b> if <code>f</code> does not return any values.) 2117*088332b5SXin Li 2118*088332b5SXin Li 2119*088332b5SXin Li 2120*088332b5SXin Li 2121*088332b5SXin Li 2122*088332b5SXin Li<h3>3.4.1 – <a name="3.4.1">Arithmetic Operators</a></h3><p> 2123*088332b5SXin LiLua supports the following arithmetic operators: 2124*088332b5SXin Li 2125*088332b5SXin Li<ul> 2126*088332b5SXin Li<li><b><code>+</code>: </b>addition</li> 2127*088332b5SXin Li<li><b><code>-</code>: </b>subtraction</li> 2128*088332b5SXin Li<li><b><code>*</code>: </b>multiplication</li> 2129*088332b5SXin Li<li><b><code>/</code>: </b>float division</li> 2130*088332b5SXin Li<li><b><code>//</code>: </b>floor division</li> 2131*088332b5SXin Li<li><b><code>%</code>: </b>modulo</li> 2132*088332b5SXin Li<li><b><code>^</code>: </b>exponentiation</li> 2133*088332b5SXin Li<li><b><code>-</code>: </b>unary minus</li> 2134*088332b5SXin Li</ul> 2135*088332b5SXin Li 2136*088332b5SXin Li<p> 2137*088332b5SXin LiWith the exception of exponentiation and float division, 2138*088332b5SXin Lithe arithmetic operators work as follows: 2139*088332b5SXin LiIf both operands are integers, 2140*088332b5SXin Lithe operation is performed over integers and the result is an integer. 2141*088332b5SXin LiOtherwise, if both operands are numbers, 2142*088332b5SXin Lithen they are converted to floats, 2143*088332b5SXin Lithe operation is performed following the machine's rules 2144*088332b5SXin Lifor floating-point arithmetic 2145*088332b5SXin Li(usually the IEEE 754 standard), 2146*088332b5SXin Liand the result is a float. 2147*088332b5SXin Li(The string library coerces strings to numbers in 2148*088332b5SXin Liarithmetic operations; see <a href="#3.4.3">§3.4.3</a> for details.) 2149*088332b5SXin Li 2150*088332b5SXin Li 2151*088332b5SXin Li<p> 2152*088332b5SXin LiExponentiation and float division (<code>/</code>) 2153*088332b5SXin Lialways convert their operands to floats 2154*088332b5SXin Liand the result is always a float. 2155*088332b5SXin LiExponentiation uses the ISO C function <code>pow</code>, 2156*088332b5SXin Liso that it works for non-integer exponents too. 2157*088332b5SXin Li 2158*088332b5SXin Li 2159*088332b5SXin Li<p> 2160*088332b5SXin LiFloor division (<code>//</code>) is a division 2161*088332b5SXin Lithat rounds the quotient towards minus infinity, 2162*088332b5SXin Liresulting in the floor of the division of its operands. 2163*088332b5SXin Li 2164*088332b5SXin Li 2165*088332b5SXin Li<p> 2166*088332b5SXin LiModulo is defined as the remainder of a division 2167*088332b5SXin Lithat rounds the quotient towards minus infinity (floor division). 2168*088332b5SXin Li 2169*088332b5SXin Li 2170*088332b5SXin Li<p> 2171*088332b5SXin LiIn case of overflows in integer arithmetic, 2172*088332b5SXin Liall operations <em>wrap around</em>. 2173*088332b5SXin Li 2174*088332b5SXin Li 2175*088332b5SXin Li 2176*088332b5SXin Li<h3>3.4.2 – <a name="3.4.2">Bitwise Operators</a></h3><p> 2177*088332b5SXin LiLua supports the following bitwise operators: 2178*088332b5SXin Li 2179*088332b5SXin Li<ul> 2180*088332b5SXin Li<li><b><code>&</code>: </b>bitwise AND</li> 2181*088332b5SXin Li<li><b><code>|</code>: </b>bitwise OR</li> 2182*088332b5SXin Li<li><b><code>~</code>: </b>bitwise exclusive OR</li> 2183*088332b5SXin Li<li><b><code>>></code>: </b>right shift</li> 2184*088332b5SXin Li<li><b><code><<</code>: </b>left shift</li> 2185*088332b5SXin Li<li><b><code>~</code>: </b>unary bitwise NOT</li> 2186*088332b5SXin Li</ul> 2187*088332b5SXin Li 2188*088332b5SXin Li<p> 2189*088332b5SXin LiAll bitwise operations convert its operands to integers 2190*088332b5SXin Li(see <a href="#3.4.3">§3.4.3</a>), 2191*088332b5SXin Lioperate on all bits of those integers, 2192*088332b5SXin Liand result in an integer. 2193*088332b5SXin Li 2194*088332b5SXin Li 2195*088332b5SXin Li<p> 2196*088332b5SXin LiBoth right and left shifts fill the vacant bits with zeros. 2197*088332b5SXin LiNegative displacements shift to the other direction; 2198*088332b5SXin Lidisplacements with absolute values equal to or higher than 2199*088332b5SXin Lithe number of bits in an integer 2200*088332b5SXin Liresult in zero (as all bits are shifted out). 2201*088332b5SXin Li 2202*088332b5SXin Li 2203*088332b5SXin Li 2204*088332b5SXin Li 2205*088332b5SXin Li 2206*088332b5SXin Li<h3>3.4.3 – <a name="3.4.3">Coercions and Conversions</a></h3><p> 2207*088332b5SXin LiLua provides some automatic conversions between some 2208*088332b5SXin Litypes and representations at run time. 2209*088332b5SXin LiBitwise operators always convert float operands to integers. 2210*088332b5SXin LiExponentiation and float division 2211*088332b5SXin Lialways convert integer operands to floats. 2212*088332b5SXin LiAll other arithmetic operations applied to mixed numbers 2213*088332b5SXin Li(integers and floats) convert the integer operand to a float. 2214*088332b5SXin LiThe C API also converts both integers to floats and 2215*088332b5SXin Lifloats to integers, as needed. 2216*088332b5SXin LiMoreover, string concatenation accepts numbers as arguments, 2217*088332b5SXin Libesides strings. 2218*088332b5SXin Li 2219*088332b5SXin Li 2220*088332b5SXin Li<p> 2221*088332b5SXin LiIn a conversion from integer to float, 2222*088332b5SXin Liif the integer value has an exact representation as a float, 2223*088332b5SXin Lithat is the result. 2224*088332b5SXin LiOtherwise, 2225*088332b5SXin Lithe conversion gets the nearest higher or 2226*088332b5SXin Lithe nearest lower representable value. 2227*088332b5SXin LiThis kind of conversion never fails. 2228*088332b5SXin Li 2229*088332b5SXin Li 2230*088332b5SXin Li<p> 2231*088332b5SXin LiThe conversion from float to integer 2232*088332b5SXin Lichecks whether the float has an exact representation as an integer 2233*088332b5SXin Li(that is, the float has an integral value and 2234*088332b5SXin Liit is in the range of integer representation). 2235*088332b5SXin LiIf it does, that representation is the result. 2236*088332b5SXin LiOtherwise, the conversion fails. 2237*088332b5SXin Li 2238*088332b5SXin Li 2239*088332b5SXin Li<p> 2240*088332b5SXin LiSeveral places in Lua coerce strings to numbers when necessary. 2241*088332b5SXin LiIn particular, 2242*088332b5SXin Lithe string library sets metamethods that try to coerce 2243*088332b5SXin Listrings to numbers in all arithmetic operations. 2244*088332b5SXin LiIf the conversion fails, 2245*088332b5SXin Lithe library calls the metamethod of the other operand 2246*088332b5SXin Li(if present) or it raises an error. 2247*088332b5SXin LiNote that bitwise operators do not do this coercion. 2248*088332b5SXin Li 2249*088332b5SXin Li 2250*088332b5SXin Li<p> 2251*088332b5SXin LiNonetheless, it is always a good practice not to rely on these 2252*088332b5SXin Liimplicit coercions, as they are not always applied; 2253*088332b5SXin Liin particular, <code>"1"==1</code> is false and <code>"1"<1</code> raises an error 2254*088332b5SXin Li(see <a href="#3.4.4">§3.4.4</a>). 2255*088332b5SXin LiThese coercions exist mainly for compatibility and may be removed 2256*088332b5SXin Liin future versions of the language. 2257*088332b5SXin Li 2258*088332b5SXin Li 2259*088332b5SXin Li<p> 2260*088332b5SXin LiA string is converted to an integer or a float 2261*088332b5SXin Lifollowing its syntax and the rules of the Lua lexer. 2262*088332b5SXin LiThe string may have also leading and trailing whitespaces and a sign. 2263*088332b5SXin LiAll conversions from strings to numbers 2264*088332b5SXin Liaccept both a dot and the current locale mark 2265*088332b5SXin Lias the radix character. 2266*088332b5SXin Li(The Lua lexer, however, accepts only a dot.) 2267*088332b5SXin LiIf the string is not a valid numeral, 2268*088332b5SXin Lithe conversion fails. 2269*088332b5SXin LiIf necessary, the result of this first step is then converted 2270*088332b5SXin Lito a specific number subtype following the previous rules 2271*088332b5SXin Lifor conversions between floats and integers. 2272*088332b5SXin Li 2273*088332b5SXin Li 2274*088332b5SXin Li<p> 2275*088332b5SXin LiThe conversion from numbers to strings uses a 2276*088332b5SXin Linon-specified human-readable format. 2277*088332b5SXin LiTo convert numbers to strings in any specific way, 2278*088332b5SXin Liuse the function <a href="#pdf-string.format"><code>string.format</code></a>. 2279*088332b5SXin Li 2280*088332b5SXin Li 2281*088332b5SXin Li 2282*088332b5SXin Li 2283*088332b5SXin Li 2284*088332b5SXin Li<h3>3.4.4 – <a name="3.4.4">Relational Operators</a></h3><p> 2285*088332b5SXin LiLua supports the following relational operators: 2286*088332b5SXin Li 2287*088332b5SXin Li<ul> 2288*088332b5SXin Li<li><b><code>==</code>: </b>equality</li> 2289*088332b5SXin Li<li><b><code>~=</code>: </b>inequality</li> 2290*088332b5SXin Li<li><b><code><</code>: </b>less than</li> 2291*088332b5SXin Li<li><b><code>></code>: </b>greater than</li> 2292*088332b5SXin Li<li><b><code><=</code>: </b>less or equal</li> 2293*088332b5SXin Li<li><b><code>>=</code>: </b>greater or equal</li> 2294*088332b5SXin Li</ul><p> 2295*088332b5SXin LiThese operators always result in <b>false</b> or <b>true</b>. 2296*088332b5SXin Li 2297*088332b5SXin Li 2298*088332b5SXin Li<p> 2299*088332b5SXin LiEquality (<code>==</code>) first compares the type of its operands. 2300*088332b5SXin LiIf the types are different, then the result is <b>false</b>. 2301*088332b5SXin LiOtherwise, the values of the operands are compared. 2302*088332b5SXin LiStrings are equal if they have the same byte content. 2303*088332b5SXin LiNumbers are equal if they denote the same mathematical value. 2304*088332b5SXin Li 2305*088332b5SXin Li 2306*088332b5SXin Li<p> 2307*088332b5SXin LiTables, userdata, and threads 2308*088332b5SXin Liare compared by reference: 2309*088332b5SXin Litwo objects are considered equal only if they are the same object. 2310*088332b5SXin LiEvery time you create a new object 2311*088332b5SXin Li(a table, a userdata, or a thread), 2312*088332b5SXin Lithis new object is different from any previously existing object. 2313*088332b5SXin LiA function is always equal to itself. 2314*088332b5SXin LiFunctions with any detectable difference 2315*088332b5SXin Li(different behavior, different definition) are always different. 2316*088332b5SXin LiFunctions created at different times but with no detectable differences 2317*088332b5SXin Limay be classified as equal or not 2318*088332b5SXin Li(depending on internal caching details). 2319*088332b5SXin Li 2320*088332b5SXin Li 2321*088332b5SXin Li<p> 2322*088332b5SXin LiYou can change the way that Lua compares tables and userdata 2323*088332b5SXin Liby using the <code>__eq</code> metamethod (see <a href="#2.4">§2.4</a>). 2324*088332b5SXin Li 2325*088332b5SXin Li 2326*088332b5SXin Li<p> 2327*088332b5SXin LiEquality comparisons do not convert strings to numbers 2328*088332b5SXin Lior vice versa. 2329*088332b5SXin LiThus, <code>"0"==0</code> evaluates to <b>false</b>, 2330*088332b5SXin Liand <code>t[0]</code> and <code>t["0"]</code> denote different 2331*088332b5SXin Lientries in a table. 2332*088332b5SXin Li 2333*088332b5SXin Li 2334*088332b5SXin Li<p> 2335*088332b5SXin LiThe operator <code>~=</code> is exactly the negation of equality (<code>==</code>). 2336*088332b5SXin Li 2337*088332b5SXin Li 2338*088332b5SXin Li<p> 2339*088332b5SXin LiThe order operators work as follows. 2340*088332b5SXin LiIf both arguments are numbers, 2341*088332b5SXin Lithen they are compared according to their mathematical values, 2342*088332b5SXin Liregardless of their subtypes. 2343*088332b5SXin LiOtherwise, if both arguments are strings, 2344*088332b5SXin Lithen their values are compared according to the current locale. 2345*088332b5SXin LiOtherwise, Lua tries to call the <code>__lt</code> or the <code>__le</code> 2346*088332b5SXin Limetamethod (see <a href="#2.4">§2.4</a>). 2347*088332b5SXin LiA comparison <code>a > b</code> is translated to <code>b < a</code> 2348*088332b5SXin Liand <code>a >= b</code> is translated to <code>b <= a</code>. 2349*088332b5SXin Li 2350*088332b5SXin Li 2351*088332b5SXin Li<p> 2352*088332b5SXin LiFollowing the IEEE 754 standard, 2353*088332b5SXin Lithe special value NaN is considered neither less than, 2354*088332b5SXin Linor equal to, nor greater than any value, including itself. 2355*088332b5SXin Li 2356*088332b5SXin Li 2357*088332b5SXin Li 2358*088332b5SXin Li 2359*088332b5SXin Li 2360*088332b5SXin Li<h3>3.4.5 – <a name="3.4.5">Logical Operators</a></h3><p> 2361*088332b5SXin LiThe logical operators in Lua are 2362*088332b5SXin Li<b>and</b>, <b>or</b>, and <b>not</b>. 2363*088332b5SXin LiLike the control structures (see <a href="#3.3.4">§3.3.4</a>), 2364*088332b5SXin Liall logical operators consider both <b>false</b> and <b>nil</b> as false 2365*088332b5SXin Liand anything else as true. 2366*088332b5SXin Li 2367*088332b5SXin Li 2368*088332b5SXin Li<p> 2369*088332b5SXin LiThe negation operator <b>not</b> always returns <b>false</b> or <b>true</b>. 2370*088332b5SXin LiThe conjunction operator <b>and</b> returns its first argument 2371*088332b5SXin Liif this value is <b>false</b> or <b>nil</b>; 2372*088332b5SXin Liotherwise, <b>and</b> returns its second argument. 2373*088332b5SXin LiThe disjunction operator <b>or</b> returns its first argument 2374*088332b5SXin Liif this value is different from <b>nil</b> and <b>false</b>; 2375*088332b5SXin Liotherwise, <b>or</b> returns its second argument. 2376*088332b5SXin LiBoth <b>and</b> and <b>or</b> use short-circuit evaluation; 2377*088332b5SXin Lithat is, 2378*088332b5SXin Lithe second operand is evaluated only if necessary. 2379*088332b5SXin LiHere are some examples: 2380*088332b5SXin Li 2381*088332b5SXin Li<pre> 2382*088332b5SXin Li 10 or 20 --> 10 2383*088332b5SXin Li 10 or error() --> 10 2384*088332b5SXin Li nil or "a" --> "a" 2385*088332b5SXin Li nil and 10 --> nil 2386*088332b5SXin Li false and error() --> false 2387*088332b5SXin Li false and nil --> false 2388*088332b5SXin Li false or nil --> nil 2389*088332b5SXin Li 10 and 20 --> 20 2390*088332b5SXin Li</pre> 2391*088332b5SXin Li 2392*088332b5SXin Li 2393*088332b5SXin Li 2394*088332b5SXin Li 2395*088332b5SXin Li<h3>3.4.6 – <a name="3.4.6">Concatenation</a></h3><p> 2396*088332b5SXin LiThe string concatenation operator in Lua is 2397*088332b5SXin Lidenoted by two dots ('<code>..</code>'). 2398*088332b5SXin LiIf both operands are strings or numbers, 2399*088332b5SXin Lithen the numbers are converted to strings 2400*088332b5SXin Liin a non-specified format (see <a href="#3.4.3">§3.4.3</a>). 2401*088332b5SXin LiOtherwise, the <code>__concat</code> metamethod is called (see <a href="#2.4">§2.4</a>). 2402*088332b5SXin Li 2403*088332b5SXin Li 2404*088332b5SXin Li 2405*088332b5SXin Li 2406*088332b5SXin Li 2407*088332b5SXin Li<h3>3.4.7 – <a name="3.4.7">The Length Operator</a></h3> 2408*088332b5SXin Li 2409*088332b5SXin Li<p> 2410*088332b5SXin LiThe length operator is denoted by the unary prefix operator <code>#</code>. 2411*088332b5SXin Li 2412*088332b5SXin Li 2413*088332b5SXin Li<p> 2414*088332b5SXin LiThe length of a string is its number of bytes. 2415*088332b5SXin Li(That is the usual meaning of string length when each 2416*088332b5SXin Licharacter is one byte.) 2417*088332b5SXin Li 2418*088332b5SXin Li 2419*088332b5SXin Li<p> 2420*088332b5SXin LiThe length operator applied on a table 2421*088332b5SXin Lireturns a border in that table. 2422*088332b5SXin LiA <em>border</em> in a table <code>t</code> is any natural number 2423*088332b5SXin Lithat satisfies the following condition: 2424*088332b5SXin Li 2425*088332b5SXin Li<pre> 2426*088332b5SXin Li (border == 0 or t[border] ~= nil) and t[border + 1] == nil 2427*088332b5SXin Li</pre><p> 2428*088332b5SXin LiIn words, 2429*088332b5SXin Lia border is any (natural) index present in the table 2430*088332b5SXin Lithat is followed by an absent index 2431*088332b5SXin Li(or zero, when index 1 is absent). 2432*088332b5SXin Li 2433*088332b5SXin Li 2434*088332b5SXin Li<p> 2435*088332b5SXin LiA table with exactly one border is called a <em>sequence</em>. 2436*088332b5SXin LiFor instance, the table <code>{10, 20, 30, 40, 50}</code> is a sequence, 2437*088332b5SXin Lias it has only one border (5). 2438*088332b5SXin LiThe table <code>{10, 20, 30, nil, 50}</code> has two borders (3 and 5), 2439*088332b5SXin Liand therefore it is not a sequence. 2440*088332b5SXin Li(The <b>nil</b> at index 4 is called a <em>hole</em>.) 2441*088332b5SXin LiThe table <code>{nil, 20, 30, nil, nil, 60, nil}</code> 2442*088332b5SXin Lihas three borders (0, 3, and 6) and three holes 2443*088332b5SXin Li(at indices 1, 4, and 5), 2444*088332b5SXin Liso it is not a sequence, too. 2445*088332b5SXin LiThe table <code>{}</code> is a sequence with border 0. 2446*088332b5SXin LiNote that non-natural keys do not interfere 2447*088332b5SXin Liwith whether a table is a sequence. 2448*088332b5SXin Li 2449*088332b5SXin Li 2450*088332b5SXin Li<p> 2451*088332b5SXin LiWhen <code>t</code> is a sequence, 2452*088332b5SXin Li<code>#t</code> returns its only border, 2453*088332b5SXin Liwhich corresponds to the intuitive notion of the length of the sequence. 2454*088332b5SXin LiWhen <code>t</code> is not a sequence, 2455*088332b5SXin Li<code>#t</code> can return any of its borders. 2456*088332b5SXin Li(The exact one depends on details of 2457*088332b5SXin Lithe internal representation of the table, 2458*088332b5SXin Liwhich in turn can depend on how the table was populated and 2459*088332b5SXin Lithe memory addresses of its non-numeric keys.) 2460*088332b5SXin Li 2461*088332b5SXin Li 2462*088332b5SXin Li<p> 2463*088332b5SXin LiThe computation of the length of a table 2464*088332b5SXin Lihas a guaranteed worst time of <em>O(log n)</em>, 2465*088332b5SXin Liwhere <em>n</em> is the largest natural key in the table. 2466*088332b5SXin Li 2467*088332b5SXin Li 2468*088332b5SXin Li<p> 2469*088332b5SXin LiA program can modify the behavior of the length operator for 2470*088332b5SXin Liany value but strings through the <code>__len</code> metamethod (see <a href="#2.4">§2.4</a>). 2471*088332b5SXin Li 2472*088332b5SXin Li 2473*088332b5SXin Li 2474*088332b5SXin Li 2475*088332b5SXin Li 2476*088332b5SXin Li<h3>3.4.8 – <a name="3.4.8">Precedence</a></h3><p> 2477*088332b5SXin LiOperator precedence in Lua follows the table below, 2478*088332b5SXin Lifrom lower to higher priority: 2479*088332b5SXin Li 2480*088332b5SXin Li<pre> 2481*088332b5SXin Li or 2482*088332b5SXin Li and 2483*088332b5SXin Li < > <= >= ~= == 2484*088332b5SXin Li | 2485*088332b5SXin Li ~ 2486*088332b5SXin Li & 2487*088332b5SXin Li << >> 2488*088332b5SXin Li .. 2489*088332b5SXin Li + - 2490*088332b5SXin Li * / // % 2491*088332b5SXin Li unary operators (not # - ~) 2492*088332b5SXin Li ^ 2493*088332b5SXin Li</pre><p> 2494*088332b5SXin LiAs usual, 2495*088332b5SXin Liyou can use parentheses to change the precedences of an expression. 2496*088332b5SXin LiThe concatenation ('<code>..</code>') and exponentiation ('<code>^</code>') 2497*088332b5SXin Lioperators are right associative. 2498*088332b5SXin LiAll other binary operators are left associative. 2499*088332b5SXin Li 2500*088332b5SXin Li 2501*088332b5SXin Li 2502*088332b5SXin Li 2503*088332b5SXin Li 2504*088332b5SXin Li<h3>3.4.9 – <a name="3.4.9">Table Constructors</a></h3><p> 2505*088332b5SXin LiTable constructors are expressions that create tables. 2506*088332b5SXin LiEvery time a constructor is evaluated, a new table is created. 2507*088332b5SXin LiA constructor can be used to create an empty table 2508*088332b5SXin Lior to create a table and initialize some of its fields. 2509*088332b5SXin LiThe general syntax for constructors is 2510*088332b5SXin Li 2511*088332b5SXin Li<pre> 2512*088332b5SXin Li tableconstructor ::= ‘<b>{</b>’ [fieldlist] ‘<b>}</b>’ 2513*088332b5SXin Li fieldlist ::= field {fieldsep field} [fieldsep] 2514*088332b5SXin Li field ::= ‘<b>[</b>’ exp ‘<b>]</b>’ ‘<b>=</b>’ exp | Name ‘<b>=</b>’ exp | exp 2515*088332b5SXin Li fieldsep ::= ‘<b>,</b>’ | ‘<b>;</b>’ 2516*088332b5SXin Li</pre> 2517*088332b5SXin Li 2518*088332b5SXin Li<p> 2519*088332b5SXin LiEach field of the form <code>[exp1] = exp2</code> adds to the new table an entry 2520*088332b5SXin Liwith key <code>exp1</code> and value <code>exp2</code>. 2521*088332b5SXin LiA field of the form <code>name = exp</code> is equivalent to 2522*088332b5SXin Li<code>["name"] = exp</code>. 2523*088332b5SXin LiFields of the form <code>exp</code> are equivalent to 2524*088332b5SXin Li<code>[i] = exp</code>, where <code>i</code> are consecutive integers 2525*088332b5SXin Listarting with 1; 2526*088332b5SXin Lifields in the other formats do not affect this counting. 2527*088332b5SXin LiFor example, 2528*088332b5SXin Li 2529*088332b5SXin Li<pre> 2530*088332b5SXin Li a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 } 2531*088332b5SXin Li</pre><p> 2532*088332b5SXin Liis equivalent to 2533*088332b5SXin Li 2534*088332b5SXin Li<pre> 2535*088332b5SXin Li do 2536*088332b5SXin Li local t = {} 2537*088332b5SXin Li t[f(1)] = g 2538*088332b5SXin Li t[1] = "x" -- 1st exp 2539*088332b5SXin Li t[2] = "y" -- 2nd exp 2540*088332b5SXin Li t.x = 1 -- t["x"] = 1 2541*088332b5SXin Li t[3] = f(x) -- 3rd exp 2542*088332b5SXin Li t[30] = 23 2543*088332b5SXin Li t[4] = 45 -- 4th exp 2544*088332b5SXin Li a = t 2545*088332b5SXin Li end 2546*088332b5SXin Li</pre> 2547*088332b5SXin Li 2548*088332b5SXin Li<p> 2549*088332b5SXin LiThe order of the assignments in a constructor is undefined. 2550*088332b5SXin Li(This order would be relevant only when there are repeated keys.) 2551*088332b5SXin Li 2552*088332b5SXin Li 2553*088332b5SXin Li<p> 2554*088332b5SXin LiIf the last field in the list has the form <code>exp</code> 2555*088332b5SXin Liand the expression is a function call or a vararg expression, 2556*088332b5SXin Lithen all values returned by this expression enter the list consecutively 2557*088332b5SXin Li(see <a href="#3.4.10">§3.4.10</a>). 2558*088332b5SXin Li 2559*088332b5SXin Li 2560*088332b5SXin Li<p> 2561*088332b5SXin LiThe field list can have an optional trailing separator, 2562*088332b5SXin Lias a convenience for machine-generated code. 2563*088332b5SXin Li 2564*088332b5SXin Li 2565*088332b5SXin Li 2566*088332b5SXin Li 2567*088332b5SXin Li 2568*088332b5SXin Li<h3>3.4.10 – <a name="3.4.10">Function Calls</a></h3><p> 2569*088332b5SXin LiA function call in Lua has the following syntax: 2570*088332b5SXin Li 2571*088332b5SXin Li<pre> 2572*088332b5SXin Li functioncall ::= prefixexp args 2573*088332b5SXin Li</pre><p> 2574*088332b5SXin LiIn a function call, 2575*088332b5SXin Lifirst prefixexp and args are evaluated. 2576*088332b5SXin LiIf the value of prefixexp has type <em>function</em>, 2577*088332b5SXin Lithen this function is called 2578*088332b5SXin Liwith the given arguments. 2579*088332b5SXin LiOtherwise, if present, 2580*088332b5SXin Lithe prefixexp <code>__call</code> metamethod is called: 2581*088332b5SXin Liits first argument is the value of prefixexp, 2582*088332b5SXin Lifollowed by the original call arguments 2583*088332b5SXin Li(see <a href="#2.4">§2.4</a>). 2584*088332b5SXin Li 2585*088332b5SXin Li 2586*088332b5SXin Li<p> 2587*088332b5SXin LiThe form 2588*088332b5SXin Li 2589*088332b5SXin Li<pre> 2590*088332b5SXin Li functioncall ::= prefixexp ‘<b>:</b>’ Name args 2591*088332b5SXin Li</pre><p> 2592*088332b5SXin Lican be used to emulate methods. 2593*088332b5SXin LiA call <code>v:name(<em>args</em>)</code> 2594*088332b5SXin Liis syntactic sugar for <code>v.name(v,<em>args</em>)</code>, 2595*088332b5SXin Liexcept that <code>v</code> is evaluated only once. 2596*088332b5SXin Li 2597*088332b5SXin Li 2598*088332b5SXin Li<p> 2599*088332b5SXin LiArguments have the following syntax: 2600*088332b5SXin Li 2601*088332b5SXin Li<pre> 2602*088332b5SXin Li args ::= ‘<b>(</b>’ [explist] ‘<b>)</b>’ 2603*088332b5SXin Li args ::= tableconstructor 2604*088332b5SXin Li args ::= LiteralString 2605*088332b5SXin Li</pre><p> 2606*088332b5SXin LiAll argument expressions are evaluated before the call. 2607*088332b5SXin LiA call of the form <code>f{<em>fields</em>}</code> is 2608*088332b5SXin Lisyntactic sugar for <code>f({<em>fields</em>})</code>; 2609*088332b5SXin Lithat is, the argument list is a single new table. 2610*088332b5SXin LiA call of the form <code>f'<em>string</em>'</code> 2611*088332b5SXin Li(or <code>f"<em>string</em>"</code> or <code>f[[<em>string</em>]]</code>) 2612*088332b5SXin Liis syntactic sugar for <code>f('<em>string</em>')</code>; 2613*088332b5SXin Lithat is, the argument list is a single literal string. 2614*088332b5SXin Li 2615*088332b5SXin Li 2616*088332b5SXin Li<p> 2617*088332b5SXin LiA call of the form <code>return <em>functioncall</em></code> not in the 2618*088332b5SXin Liscope of a to-be-closed variable is called a <em>tail call</em>. 2619*088332b5SXin LiLua implements <em>proper tail calls</em> 2620*088332b5SXin Li(or <em>proper tail recursion</em>): 2621*088332b5SXin Liin a tail call, 2622*088332b5SXin Lithe called function reuses the stack entry of the calling function. 2623*088332b5SXin LiTherefore, there is no limit on the number of nested tail calls that 2624*088332b5SXin Lia program can execute. 2625*088332b5SXin LiHowever, a tail call erases any debug information about the 2626*088332b5SXin Licalling function. 2627*088332b5SXin LiNote that a tail call only happens with a particular syntax, 2628*088332b5SXin Liwhere the <b>return</b> has one single function call as argument, 2629*088332b5SXin Liand it is outside the scope of any to-be-closed variable. 2630*088332b5SXin LiThis syntax makes the calling function return exactly 2631*088332b5SXin Lithe returns of the called function, 2632*088332b5SXin Liwithout any intervening action. 2633*088332b5SXin LiSo, none of the following examples are tail calls: 2634*088332b5SXin Li 2635*088332b5SXin Li<pre> 2636*088332b5SXin Li return (f(x)) -- results adjusted to 1 2637*088332b5SXin Li return 2 * f(x) -- result multiplied by 2 2638*088332b5SXin Li return x, f(x) -- additional results 2639*088332b5SXin Li f(x); return -- results discarded 2640*088332b5SXin Li return x or f(x) -- results adjusted to 1 2641*088332b5SXin Li</pre> 2642*088332b5SXin Li 2643*088332b5SXin Li 2644*088332b5SXin Li 2645*088332b5SXin Li 2646*088332b5SXin Li<h3>3.4.11 – <a name="3.4.11">Function Definitions</a></h3> 2647*088332b5SXin Li 2648*088332b5SXin Li<p> 2649*088332b5SXin LiThe syntax for function definition is 2650*088332b5SXin Li 2651*088332b5SXin Li<pre> 2652*088332b5SXin Li functiondef ::= <b>function</b> funcbody 2653*088332b5SXin Li funcbody ::= ‘<b>(</b>’ [parlist] ‘<b>)</b>’ block <b>end</b> 2654*088332b5SXin Li</pre> 2655*088332b5SXin Li 2656*088332b5SXin Li<p> 2657*088332b5SXin LiThe following syntactic sugar simplifies function definitions: 2658*088332b5SXin Li 2659*088332b5SXin Li<pre> 2660*088332b5SXin Li stat ::= <b>function</b> funcname funcbody 2661*088332b5SXin Li stat ::= <b>local</b> <b>function</b> Name funcbody 2662*088332b5SXin Li funcname ::= Name {‘<b>.</b>’ Name} [‘<b>:</b>’ Name] 2663*088332b5SXin Li</pre><p> 2664*088332b5SXin LiThe statement 2665*088332b5SXin Li 2666*088332b5SXin Li<pre> 2667*088332b5SXin Li function f () <em>body</em> end 2668*088332b5SXin Li</pre><p> 2669*088332b5SXin Litranslates to 2670*088332b5SXin Li 2671*088332b5SXin Li<pre> 2672*088332b5SXin Li f = function () <em>body</em> end 2673*088332b5SXin Li</pre><p> 2674*088332b5SXin LiThe statement 2675*088332b5SXin Li 2676*088332b5SXin Li<pre> 2677*088332b5SXin Li function t.a.b.c.f () <em>body</em> end 2678*088332b5SXin Li</pre><p> 2679*088332b5SXin Litranslates to 2680*088332b5SXin Li 2681*088332b5SXin Li<pre> 2682*088332b5SXin Li t.a.b.c.f = function () <em>body</em> end 2683*088332b5SXin Li</pre><p> 2684*088332b5SXin LiThe statement 2685*088332b5SXin Li 2686*088332b5SXin Li<pre> 2687*088332b5SXin Li local function f () <em>body</em> end 2688*088332b5SXin Li</pre><p> 2689*088332b5SXin Litranslates to 2690*088332b5SXin Li 2691*088332b5SXin Li<pre> 2692*088332b5SXin Li local f; f = function () <em>body</em> end 2693*088332b5SXin Li</pre><p> 2694*088332b5SXin Linot to 2695*088332b5SXin Li 2696*088332b5SXin Li<pre> 2697*088332b5SXin Li local f = function () <em>body</em> end 2698*088332b5SXin Li</pre><p> 2699*088332b5SXin Li(This only makes a difference when the body of the function 2700*088332b5SXin Licontains references to <code>f</code>.) 2701*088332b5SXin Li 2702*088332b5SXin Li 2703*088332b5SXin Li<p> 2704*088332b5SXin LiA function definition is an executable expression, 2705*088332b5SXin Liwhose value has type <em>function</em>. 2706*088332b5SXin LiWhen Lua precompiles a chunk, 2707*088332b5SXin Liall its function bodies are precompiled too, 2708*088332b5SXin Libut they are not created yet. 2709*088332b5SXin LiThen, whenever Lua executes the function definition, 2710*088332b5SXin Lithe function is <em>instantiated</em> (or <em>closed</em>). 2711*088332b5SXin LiThis function instance, or <em>closure</em>, 2712*088332b5SXin Liis the final value of the expression. 2713*088332b5SXin Li 2714*088332b5SXin Li 2715*088332b5SXin Li<p> 2716*088332b5SXin LiParameters act as local variables that are 2717*088332b5SXin Liinitialized with the argument values: 2718*088332b5SXin Li 2719*088332b5SXin Li<pre> 2720*088332b5SXin Li parlist ::= namelist [‘<b>,</b>’ ‘<b>...</b>’] | ‘<b>...</b>’ 2721*088332b5SXin Li</pre><p> 2722*088332b5SXin LiWhen a Lua function is called, 2723*088332b5SXin Liit adjusts its list of arguments to 2724*088332b5SXin Lithe length of its list of parameters, 2725*088332b5SXin Liunless the function is a <em>vararg function</em>, 2726*088332b5SXin Liwhich is indicated by three dots ('<code>...</code>') 2727*088332b5SXin Liat the end of its parameter list. 2728*088332b5SXin LiA vararg function does not adjust its argument list; 2729*088332b5SXin Liinstead, it collects all extra arguments and supplies them 2730*088332b5SXin Lito the function through a <em>vararg expression</em>, 2731*088332b5SXin Liwhich is also written as three dots. 2732*088332b5SXin LiThe value of this expression is a list of all actual extra arguments, 2733*088332b5SXin Lisimilar to a function with multiple results. 2734*088332b5SXin LiIf a vararg expression is used inside another expression 2735*088332b5SXin Lior in the middle of a list of expressions, 2736*088332b5SXin Lithen its return list is adjusted to one element. 2737*088332b5SXin LiIf the expression is used as the last element of a list of expressions, 2738*088332b5SXin Lithen no adjustment is made 2739*088332b5SXin Li(unless that last expression is enclosed in parentheses). 2740*088332b5SXin Li 2741*088332b5SXin Li 2742*088332b5SXin Li<p> 2743*088332b5SXin LiAs an example, consider the following definitions: 2744*088332b5SXin Li 2745*088332b5SXin Li<pre> 2746*088332b5SXin Li function f(a, b) end 2747*088332b5SXin Li function g(a, b, ...) end 2748*088332b5SXin Li function r() return 1,2,3 end 2749*088332b5SXin Li</pre><p> 2750*088332b5SXin LiThen, we have the following mapping from arguments to parameters and 2751*088332b5SXin Lito the vararg expression: 2752*088332b5SXin Li 2753*088332b5SXin Li<pre> 2754*088332b5SXin Li CALL PARAMETERS 2755*088332b5SXin Li 2756*088332b5SXin Li f(3) a=3, b=nil 2757*088332b5SXin Li f(3, 4) a=3, b=4 2758*088332b5SXin Li f(3, 4, 5) a=3, b=4 2759*088332b5SXin Li f(r(), 10) a=1, b=10 2760*088332b5SXin Li f(r()) a=1, b=2 2761*088332b5SXin Li 2762*088332b5SXin Li g(3) a=3, b=nil, ... --> (nothing) 2763*088332b5SXin Li g(3, 4) a=3, b=4, ... --> (nothing) 2764*088332b5SXin Li g(3, 4, 5, 8) a=3, b=4, ... --> 5 8 2765*088332b5SXin Li g(5, r()) a=5, b=1, ... --> 2 3 2766*088332b5SXin Li</pre> 2767*088332b5SXin Li 2768*088332b5SXin Li<p> 2769*088332b5SXin LiResults are returned using the <b>return</b> statement (see <a href="#3.3.4">§3.3.4</a>). 2770*088332b5SXin LiIf control reaches the end of a function 2771*088332b5SXin Liwithout encountering a <b>return</b> statement, 2772*088332b5SXin Lithen the function returns with no results. 2773*088332b5SXin Li 2774*088332b5SXin Li 2775*088332b5SXin Li<p> 2776*088332b5SXin Li 2777*088332b5SXin LiThere is a system-dependent limit on the number of values 2778*088332b5SXin Lithat a function may return. 2779*088332b5SXin LiThis limit is guaranteed to be greater than 1000. 2780*088332b5SXin Li 2781*088332b5SXin Li 2782*088332b5SXin Li<p> 2783*088332b5SXin LiThe <em>colon</em> syntax 2784*088332b5SXin Liis used to emulate <em>methods</em>, 2785*088332b5SXin Liadding an implicit extra parameter <code>self</code> to the function. 2786*088332b5SXin LiThus, the statement 2787*088332b5SXin Li 2788*088332b5SXin Li<pre> 2789*088332b5SXin Li function t.a.b.c:f (<em>params</em>) <em>body</em> end 2790*088332b5SXin Li</pre><p> 2791*088332b5SXin Liis syntactic sugar for 2792*088332b5SXin Li 2793*088332b5SXin Li<pre> 2794*088332b5SXin Li t.a.b.c.f = function (self, <em>params</em>) <em>body</em> end 2795*088332b5SXin Li</pre> 2796*088332b5SXin Li 2797*088332b5SXin Li 2798*088332b5SXin Li 2799*088332b5SXin Li 2800*088332b5SXin Li 2801*088332b5SXin Li 2802*088332b5SXin Li<h2>3.5 – <a name="3.5">Visibility Rules</a></h2> 2803*088332b5SXin Li 2804*088332b5SXin Li<p> 2805*088332b5SXin Li 2806*088332b5SXin LiLua is a lexically scoped language. 2807*088332b5SXin LiThe scope of a local variable begins at the first statement after 2808*088332b5SXin Liits declaration and lasts until the last non-void statement 2809*088332b5SXin Liof the innermost block that includes the declaration. 2810*088332b5SXin LiConsider the following example: 2811*088332b5SXin Li 2812*088332b5SXin Li<pre> 2813*088332b5SXin Li x = 10 -- global variable 2814*088332b5SXin Li do -- new block 2815*088332b5SXin Li local x = x -- new 'x', with value 10 2816*088332b5SXin Li print(x) --> 10 2817*088332b5SXin Li x = x+1 2818*088332b5SXin Li do -- another block 2819*088332b5SXin Li local x = x+1 -- another 'x' 2820*088332b5SXin Li print(x) --> 12 2821*088332b5SXin Li end 2822*088332b5SXin Li print(x) --> 11 2823*088332b5SXin Li end 2824*088332b5SXin Li print(x) --> 10 (the global one) 2825*088332b5SXin Li</pre> 2826*088332b5SXin Li 2827*088332b5SXin Li<p> 2828*088332b5SXin LiNotice that, in a declaration like <code>local x = x</code>, 2829*088332b5SXin Lithe new <code>x</code> being declared is not in scope yet, 2830*088332b5SXin Liand so the second <code>x</code> refers to the outside variable. 2831*088332b5SXin Li 2832*088332b5SXin Li 2833*088332b5SXin Li<p> 2834*088332b5SXin LiBecause of the lexical scoping rules, 2835*088332b5SXin Lilocal variables can be freely accessed by functions 2836*088332b5SXin Lidefined inside their scope. 2837*088332b5SXin LiA local variable used by an inner function is called an <em>upvalue</em> 2838*088332b5SXin Li(or <em>external local variable</em>, or simply <em>external variable</em>) 2839*088332b5SXin Liinside the inner function. 2840*088332b5SXin Li 2841*088332b5SXin Li 2842*088332b5SXin Li<p> 2843*088332b5SXin LiNotice that each execution of a <b>local</b> statement 2844*088332b5SXin Lidefines new local variables. 2845*088332b5SXin LiConsider the following example: 2846*088332b5SXin Li 2847*088332b5SXin Li<pre> 2848*088332b5SXin Li a = {} 2849*088332b5SXin Li local x = 20 2850*088332b5SXin Li for i = 1, 10 do 2851*088332b5SXin Li local y = 0 2852*088332b5SXin Li a[i] = function () y = y + 1; return x + y end 2853*088332b5SXin Li end 2854*088332b5SXin Li</pre><p> 2855*088332b5SXin LiThe loop creates ten closures 2856*088332b5SXin Li(that is, ten instances of the anonymous function). 2857*088332b5SXin LiEach of these closures uses a different <code>y</code> variable, 2858*088332b5SXin Liwhile all of them share the same <code>x</code>. 2859*088332b5SXin Li 2860*088332b5SXin Li 2861*088332b5SXin Li 2862*088332b5SXin Li 2863*088332b5SXin Li 2864*088332b5SXin Li<h1>4 – <a name="4">The Application Program Interface</a></h1> 2865*088332b5SXin Li 2866*088332b5SXin Li 2867*088332b5SXin Li 2868*088332b5SXin Li<p> 2869*088332b5SXin Li 2870*088332b5SXin LiThis section describes the C API for Lua, that is, 2871*088332b5SXin Lithe set of C functions available to the host program to communicate 2872*088332b5SXin Liwith Lua. 2873*088332b5SXin LiAll API functions and related types and constants 2874*088332b5SXin Liare declared in the header file <a name="pdf-lua.h"><code>lua.h</code></a>. 2875*088332b5SXin Li 2876*088332b5SXin Li 2877*088332b5SXin Li<p> 2878*088332b5SXin LiEven when we use the term "function", 2879*088332b5SXin Liany facility in the API may be provided as a macro instead. 2880*088332b5SXin LiExcept where stated otherwise, 2881*088332b5SXin Liall such macros use each of their arguments exactly once 2882*088332b5SXin Li(except for the first argument, which is always a Lua state), 2883*088332b5SXin Liand so do not generate any hidden side-effects. 2884*088332b5SXin Li 2885*088332b5SXin Li 2886*088332b5SXin Li<p> 2887*088332b5SXin LiAs in most C libraries, 2888*088332b5SXin Lithe Lua API functions do not check their arguments 2889*088332b5SXin Lifor validity or consistency. 2890*088332b5SXin LiHowever, you can change this behavior by compiling Lua 2891*088332b5SXin Liwith the macro <a name="pdf-LUA_USE_APICHECK"><code>LUA_USE_APICHECK</code></a> defined. 2892*088332b5SXin Li 2893*088332b5SXin Li 2894*088332b5SXin Li<p> 2895*088332b5SXin LiThe Lua library is fully reentrant: 2896*088332b5SXin Liit has no global variables. 2897*088332b5SXin LiIt keeps all information it needs in a dynamic structure, 2898*088332b5SXin Licalled the <em>Lua state</em>. 2899*088332b5SXin Li 2900*088332b5SXin Li 2901*088332b5SXin Li<p> 2902*088332b5SXin LiEach Lua state has one or more threads, 2903*088332b5SXin Liwhich correspond to independent, cooperative lines of execution. 2904*088332b5SXin LiThe type <a href="#lua_State"><code>lua_State</code></a> (despite its name) refers to a thread. 2905*088332b5SXin Li(Indirectly, through the thread, it also refers to the 2906*088332b5SXin LiLua state associated to the thread.) 2907*088332b5SXin Li 2908*088332b5SXin Li 2909*088332b5SXin Li<p> 2910*088332b5SXin LiA pointer to a thread must be passed as the first argument to 2911*088332b5SXin Lievery function in the library, except to <a href="#lua_newstate"><code>lua_newstate</code></a>, 2912*088332b5SXin Liwhich creates a Lua state from scratch and returns a pointer 2913*088332b5SXin Lito the <em>main thread</em> in the new state. 2914*088332b5SXin Li 2915*088332b5SXin Li 2916*088332b5SXin Li 2917*088332b5SXin Li 2918*088332b5SXin Li 2919*088332b5SXin Li<h2>4.1 – <a name="4.1">The Stack</a></h2> 2920*088332b5SXin Li 2921*088332b5SXin Li 2922*088332b5SXin Li 2923*088332b5SXin Li<p> 2924*088332b5SXin LiLua uses a <em>virtual stack</em> to pass values to and from C. 2925*088332b5SXin LiEach element in this stack represents a Lua value 2926*088332b5SXin Li(<b>nil</b>, number, string, etc.). 2927*088332b5SXin LiFunctions in the API can access this stack through the 2928*088332b5SXin LiLua state parameter that they receive. 2929*088332b5SXin Li 2930*088332b5SXin Li 2931*088332b5SXin Li<p> 2932*088332b5SXin LiWhenever Lua calls C, the called function gets a new stack, 2933*088332b5SXin Liwhich is independent of previous stacks and of stacks of 2934*088332b5SXin LiC functions that are still active. 2935*088332b5SXin LiThis stack initially contains any arguments to the C function 2936*088332b5SXin Liand it is where the C function can store temporary 2937*088332b5SXin LiLua values and must push its results 2938*088332b5SXin Lito be returned to the caller (see <a href="#lua_CFunction"><code>lua_CFunction</code></a>). 2939*088332b5SXin Li 2940*088332b5SXin Li 2941*088332b5SXin Li<p> 2942*088332b5SXin LiFor convenience, 2943*088332b5SXin Limost query operations in the API do not follow a strict stack discipline. 2944*088332b5SXin LiInstead, they can refer to any element in the stack 2945*088332b5SXin Liby using an <em>index</em>: 2946*088332b5SXin LiA positive index represents an absolute stack position, 2947*088332b5SXin Listarting at 1 as the bottom of the stack; 2948*088332b5SXin Lia negative index represents an offset relative to the top of the stack. 2949*088332b5SXin LiMore specifically, if the stack has <em>n</em> elements, 2950*088332b5SXin Lithen index 1 represents the first element 2951*088332b5SXin Li(that is, the element that was pushed onto the stack first) 2952*088332b5SXin Liand 2953*088332b5SXin Liindex <em>n</em> represents the last element; 2954*088332b5SXin Liindex -1 also represents the last element 2955*088332b5SXin Li(that is, the element at the top) 2956*088332b5SXin Liand index <em>-n</em> represents the first element. 2957*088332b5SXin Li 2958*088332b5SXin Li 2959*088332b5SXin Li 2960*088332b5SXin Li 2961*088332b5SXin Li 2962*088332b5SXin Li<h3>4.1.1 – <a name="4.1.1">Stack Size</a></h3> 2963*088332b5SXin Li 2964*088332b5SXin Li<p> 2965*088332b5SXin LiWhen you interact with the Lua API, 2966*088332b5SXin Liyou are responsible for ensuring consistency. 2967*088332b5SXin LiIn particular, 2968*088332b5SXin Li<em>you are responsible for controlling stack overflow</em>. 2969*088332b5SXin LiYou can use the function <a href="#lua_checkstack"><code>lua_checkstack</code></a> 2970*088332b5SXin Lito ensure that the stack has enough space for pushing new elements. 2971*088332b5SXin Li 2972*088332b5SXin Li 2973*088332b5SXin Li<p> 2974*088332b5SXin LiWhenever Lua calls C, 2975*088332b5SXin Liit ensures that the stack has space for 2976*088332b5SXin Liat least <a name="pdf-LUA_MINSTACK"><code>LUA_MINSTACK</code></a> extra elements; 2977*088332b5SXin Lithat is, you can safely push up to <code>LUA_MINSTACK</code> values into it. 2978*088332b5SXin Li<code>LUA_MINSTACK</code> is defined as 20, 2979*088332b5SXin Liso that usually you do not have to worry about stack space 2980*088332b5SXin Liunless your code has loops pushing elements onto the stack. 2981*088332b5SXin Li 2982*088332b5SXin Li 2983*088332b5SXin Li<p> 2984*088332b5SXin LiWhen you call a Lua function 2985*088332b5SXin Liwithout a fixed number of results (see <a href="#lua_call"><code>lua_call</code></a>), 2986*088332b5SXin LiLua ensures that the stack has enough space for all results, 2987*088332b5SXin Libut it does not ensure any extra space. 2988*088332b5SXin LiSo, before pushing anything on the stack after such a call 2989*088332b5SXin Liyou should use <a href="#lua_checkstack"><code>lua_checkstack</code></a>. 2990*088332b5SXin Li 2991*088332b5SXin Li 2992*088332b5SXin Li 2993*088332b5SXin Li 2994*088332b5SXin Li 2995*088332b5SXin Li<h3>4.1.2 – <a name="4.1.2">Valid and Acceptable Indices</a></h3> 2996*088332b5SXin Li 2997*088332b5SXin Li<p> 2998*088332b5SXin LiAny function in the API that receives stack indices 2999*088332b5SXin Liworks only with <em>valid indices</em> or <em>acceptable indices</em>. 3000*088332b5SXin Li 3001*088332b5SXin Li 3002*088332b5SXin Li<p> 3003*088332b5SXin LiA <em>valid index</em> is an index that refers to a 3004*088332b5SXin Liposition that stores a modifiable Lua value. 3005*088332b5SXin LiIt comprises stack indices between 1 and the stack top 3006*088332b5SXin Li(<code>1 ≤ abs(index) ≤ top</code>) 3007*088332b5SXin Li 3008*088332b5SXin Liplus <em>pseudo-indices</em>, 3009*088332b5SXin Liwhich represent some positions that are accessible to C code 3010*088332b5SXin Libut that are not in the stack. 3011*088332b5SXin LiPseudo-indices are used to access the registry (see <a href="#4.3">§4.3</a>) 3012*088332b5SXin Liand the upvalues of a C function (see <a href="#4.2">§4.2</a>). 3013*088332b5SXin Li 3014*088332b5SXin Li 3015*088332b5SXin Li<p> 3016*088332b5SXin LiFunctions that do not need a specific mutable position, 3017*088332b5SXin Libut only a value (e.g., query functions), 3018*088332b5SXin Lican be called with acceptable indices. 3019*088332b5SXin LiAn <em>acceptable index</em> can be any valid index, 3020*088332b5SXin Libut it also can be any positive index after the stack top 3021*088332b5SXin Liwithin the space allocated for the stack, 3022*088332b5SXin Lithat is, indices up to the stack size. 3023*088332b5SXin Li(Note that 0 is never an acceptable index.) 3024*088332b5SXin LiIndices to upvalues (see <a href="#4.2">§4.2</a>) greater than the real number 3025*088332b5SXin Liof upvalues in the current C function are also acceptable (but invalid). 3026*088332b5SXin LiExcept when noted otherwise, 3027*088332b5SXin Lifunctions in the API work with acceptable indices. 3028*088332b5SXin Li 3029*088332b5SXin Li 3030*088332b5SXin Li<p> 3031*088332b5SXin LiAcceptable indices serve to avoid extra tests 3032*088332b5SXin Liagainst the stack top when querying the stack. 3033*088332b5SXin LiFor instance, a C function can query its third argument 3034*088332b5SXin Liwithout the need to check whether there is a third argument, 3035*088332b5SXin Lithat is, without the need to check whether 3 is a valid index. 3036*088332b5SXin Li 3037*088332b5SXin Li 3038*088332b5SXin Li<p> 3039*088332b5SXin LiFor functions that can be called with acceptable indices, 3040*088332b5SXin Liany non-valid index is treated as if it 3041*088332b5SXin Licontains a value of a virtual type <a name="pdf-LUA_TNONE"><code>LUA_TNONE</code></a>, 3042*088332b5SXin Liwhich behaves like a nil value. 3043*088332b5SXin Li 3044*088332b5SXin Li 3045*088332b5SXin Li 3046*088332b5SXin Li 3047*088332b5SXin Li 3048*088332b5SXin Li<h3>4.1.3 – <a name="4.1.3">Pointers to strings</a></h3> 3049*088332b5SXin Li 3050*088332b5SXin Li<p> 3051*088332b5SXin LiSeveral functions in the API return pointers (<code>const char*</code>) 3052*088332b5SXin Lito Lua strings in the stack. 3053*088332b5SXin Li(See <a href="#lua_pushfstring"><code>lua_pushfstring</code></a>, <a href="#lua_pushlstring"><code>lua_pushlstring</code></a>, 3054*088332b5SXin Li<a href="#lua_pushstring"><code>lua_pushstring</code></a>, and <a href="#lua_tolstring"><code>lua_tolstring</code></a>. 3055*088332b5SXin LiSee also <a href="#luaL_checklstring"><code>luaL_checklstring</code></a>, <a href="#luaL_checkstring"><code>luaL_checkstring</code></a>, 3056*088332b5SXin Liand <a href="#luaL_tolstring"><code>luaL_tolstring</code></a> in the auxiliary library.) 3057*088332b5SXin Li 3058*088332b5SXin Li 3059*088332b5SXin Li<p> 3060*088332b5SXin LiIn general, 3061*088332b5SXin LiLua's garbage collection can free or move internal memory 3062*088332b5SXin Liand then invalidate pointers to internal strings. 3063*088332b5SXin LiTo allow a safe use of these pointers, 3064*088332b5SXin LiThe API guarantees that any pointer to a string in a stack index 3065*088332b5SXin Liis valid while the value at that index is neither modified nor popped. 3066*088332b5SXin LiWhen the index is a pseudo-index (referring to an upvalue), 3067*088332b5SXin Lithe pointer is valid while the corresponding call is active and 3068*088332b5SXin Lithe corresponding upvalue is not modified. 3069*088332b5SXin Li 3070*088332b5SXin Li 3071*088332b5SXin Li<p> 3072*088332b5SXin LiSome functions in the debug interface 3073*088332b5SXin Lialso return pointers to strings, 3074*088332b5SXin Linamely <a href="#lua_getlocal"><code>lua_getlocal</code></a>, <a href="#lua_getupvalue"><code>lua_getupvalue</code></a>, 3075*088332b5SXin Li<a href="#lua_setlocal"><code>lua_setlocal</code></a>, and <a href="#lua_setupvalue"><code>lua_setupvalue</code></a>. 3076*088332b5SXin LiFor these functions, the pointer is guaranteed to 3077*088332b5SXin Libe valid while the caller function is active and 3078*088332b5SXin Lithe given closure (if one was given) is in the stack. 3079*088332b5SXin Li 3080*088332b5SXin Li 3081*088332b5SXin Li<p> 3082*088332b5SXin LiExcept for these guarantees, 3083*088332b5SXin Lithe garbage collector is free to invalidate 3084*088332b5SXin Liany pointer to internal strings. 3085*088332b5SXin Li 3086*088332b5SXin Li 3087*088332b5SXin Li 3088*088332b5SXin Li 3089*088332b5SXin Li 3090*088332b5SXin Li 3091*088332b5SXin Li 3092*088332b5SXin Li<h2>4.2 – <a name="4.2">C Closures</a></h2> 3093*088332b5SXin Li 3094*088332b5SXin Li<p> 3095*088332b5SXin LiWhen a C function is created, 3096*088332b5SXin Liit is possible to associate some values with it, 3097*088332b5SXin Lithus creating a <em>C closure</em> 3098*088332b5SXin Li(see <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a>); 3099*088332b5SXin Lithese values are called <em>upvalues</em> and are 3100*088332b5SXin Liaccessible to the function whenever it is called. 3101*088332b5SXin Li 3102*088332b5SXin Li 3103*088332b5SXin Li<p> 3104*088332b5SXin LiWhenever a C function is called, 3105*088332b5SXin Liits upvalues are located at specific pseudo-indices. 3106*088332b5SXin LiThese pseudo-indices are produced by the macro 3107*088332b5SXin Li<a href="#lua_upvalueindex"><code>lua_upvalueindex</code></a>. 3108*088332b5SXin LiThe first upvalue associated with a function is at index 3109*088332b5SXin Li<code>lua_upvalueindex(1)</code>, and so on. 3110*088332b5SXin LiAny access to <code>lua_upvalueindex(<em>n</em>)</code>, 3111*088332b5SXin Liwhere <em>n</em> is greater than the number of upvalues of the 3112*088332b5SXin Licurrent function 3113*088332b5SXin Li(but not greater than 256, 3114*088332b5SXin Liwhich is one plus the maximum number of upvalues in a closure), 3115*088332b5SXin Liproduces an acceptable but invalid index. 3116*088332b5SXin Li 3117*088332b5SXin Li 3118*088332b5SXin Li<p> 3119*088332b5SXin LiA C closure can also change the values 3120*088332b5SXin Liof its corresponding upvalues. 3121*088332b5SXin Li 3122*088332b5SXin Li 3123*088332b5SXin Li 3124*088332b5SXin Li 3125*088332b5SXin Li 3126*088332b5SXin Li<h2>4.3 – <a name="4.3">Registry</a></h2> 3127*088332b5SXin Li 3128*088332b5SXin Li<p> 3129*088332b5SXin LiLua provides a <em>registry</em>, 3130*088332b5SXin Lia predefined table that can be used by any C code to 3131*088332b5SXin Listore whatever Lua values it needs to store. 3132*088332b5SXin LiThe registry table is always accessible at pseudo-index 3133*088332b5SXin Li<a name="pdf-LUA_REGISTRYINDEX"><code>LUA_REGISTRYINDEX</code></a>. 3134*088332b5SXin LiAny C library can store data into this table, 3135*088332b5SXin Libut it must take care to choose keys 3136*088332b5SXin Lithat are different from those used 3137*088332b5SXin Liby other libraries, to avoid collisions. 3138*088332b5SXin LiTypically, you should use as key a string containing your library name, 3139*088332b5SXin Lior a light userdata with the address of a C object in your code, 3140*088332b5SXin Lior any Lua object created by your code. 3141*088332b5SXin LiAs with variable names, 3142*088332b5SXin Listring keys starting with an underscore followed by 3143*088332b5SXin Liuppercase letters are reserved for Lua. 3144*088332b5SXin Li 3145*088332b5SXin Li 3146*088332b5SXin Li<p> 3147*088332b5SXin LiThe integer keys in the registry are used 3148*088332b5SXin Liby the reference mechanism (see <a href="#luaL_ref"><code>luaL_ref</code></a>) 3149*088332b5SXin Liand by some predefined values. 3150*088332b5SXin LiTherefore, integer keys in the registry 3151*088332b5SXin Limust not be used for other purposes. 3152*088332b5SXin Li 3153*088332b5SXin Li 3154*088332b5SXin Li<p> 3155*088332b5SXin LiWhen you create a new Lua state, 3156*088332b5SXin Liits registry comes with some predefined values. 3157*088332b5SXin LiThese predefined values are indexed with integer keys 3158*088332b5SXin Lidefined as constants in <code>lua.h</code>. 3159*088332b5SXin LiThe following constants are defined: 3160*088332b5SXin Li 3161*088332b5SXin Li<ul> 3162*088332b5SXin Li<li><b><a name="pdf-LUA_RIDX_MAINTHREAD"><code>LUA_RIDX_MAINTHREAD</code></a>: </b> At this index the registry has 3163*088332b5SXin Lithe main thread of the state. 3164*088332b5SXin Li(The main thread is the one created together with the state.) 3165*088332b5SXin Li</li> 3166*088332b5SXin Li 3167*088332b5SXin Li<li><b><a name="pdf-LUA_RIDX_GLOBALS"><code>LUA_RIDX_GLOBALS</code></a>: </b> At this index the registry has 3168*088332b5SXin Lithe global environment. 3169*088332b5SXin Li</li> 3170*088332b5SXin Li</ul> 3171*088332b5SXin Li 3172*088332b5SXin Li 3173*088332b5SXin Li 3174*088332b5SXin Li 3175*088332b5SXin Li<h2>4.4 – <a name="4.4">Error Handling in C</a></h2> 3176*088332b5SXin Li 3177*088332b5SXin Li 3178*088332b5SXin Li 3179*088332b5SXin Li<p> 3180*088332b5SXin LiInternally, Lua uses the C <code>longjmp</code> facility to handle errors. 3181*088332b5SXin Li(Lua will use exceptions if you compile it as C++; 3182*088332b5SXin Lisearch for <code>LUAI_THROW</code> in the source code for details.) 3183*088332b5SXin LiWhen Lua faces any error, 3184*088332b5SXin Lisuch as a memory allocation error or a type error, 3185*088332b5SXin Liit <em>raises</em> an error; 3186*088332b5SXin Lithat is, it does a long jump. 3187*088332b5SXin LiA <em>protected environment</em> uses <code>setjmp</code> 3188*088332b5SXin Lito set a recovery point; 3189*088332b5SXin Liany error jumps to the most recent active recovery point. 3190*088332b5SXin Li 3191*088332b5SXin Li 3192*088332b5SXin Li<p> 3193*088332b5SXin LiInside a C function you can raise an error explicitly 3194*088332b5SXin Liby calling <a href="#lua_error"><code>lua_error</code></a>. 3195*088332b5SXin Li 3196*088332b5SXin Li 3197*088332b5SXin Li<p> 3198*088332b5SXin LiMost functions in the API can raise an error, 3199*088332b5SXin Lifor instance due to a memory allocation error. 3200*088332b5SXin LiThe documentation for each function indicates whether 3201*088332b5SXin Liit can raise errors. 3202*088332b5SXin Li 3203*088332b5SXin Li 3204*088332b5SXin Li<p> 3205*088332b5SXin LiIf an error happens outside any protected environment, 3206*088332b5SXin LiLua calls a <em>panic function</em> (see <a href="#lua_atpanic"><code>lua_atpanic</code></a>) 3207*088332b5SXin Liand then calls <code>abort</code>, 3208*088332b5SXin Lithus exiting the host application. 3209*088332b5SXin LiYour panic function can avoid this exit by 3210*088332b5SXin Linever returning 3211*088332b5SXin Li(e.g., doing a long jump to your own recovery point outside Lua). 3212*088332b5SXin Li 3213*088332b5SXin Li 3214*088332b5SXin Li<p> 3215*088332b5SXin LiThe panic function, 3216*088332b5SXin Lias its name implies, 3217*088332b5SXin Liis a mechanism of last resort. 3218*088332b5SXin LiPrograms should avoid it. 3219*088332b5SXin LiAs a general rule, 3220*088332b5SXin Liwhen a C function is called by Lua with a Lua state, 3221*088332b5SXin Liit can do whatever it wants on that Lua state, 3222*088332b5SXin Lias it should be already protected. 3223*088332b5SXin LiHowever, 3224*088332b5SXin Liwhen C code operates on other Lua states 3225*088332b5SXin Li(e.g., a Lua-state argument to the function, 3226*088332b5SXin Lia Lua state stored in the registry, or 3227*088332b5SXin Lithe result of <a href="#lua_newthread"><code>lua_newthread</code></a>), 3228*088332b5SXin Liit should use them only in API calls that cannot raise errors. 3229*088332b5SXin Li 3230*088332b5SXin Li 3231*088332b5SXin Li<p> 3232*088332b5SXin LiThe panic function runs as if it were a message handler (see <a href="#2.3">§2.3</a>); 3233*088332b5SXin Liin particular, the error object is on the top of the stack. 3234*088332b5SXin LiHowever, there is no guarantee about stack space. 3235*088332b5SXin LiTo push anything on the stack, 3236*088332b5SXin Lithe panic function must first check the available space (see <a href="#4.1.1">§4.1.1</a>). 3237*088332b5SXin Li 3238*088332b5SXin Li 3239*088332b5SXin Li 3240*088332b5SXin Li 3241*088332b5SXin Li 3242*088332b5SXin Li<h3>4.4.1 – <a name="4.4.1">Status Codes</a></h3> 3243*088332b5SXin Li 3244*088332b5SXin Li<p> 3245*088332b5SXin LiSeveral functions that report errors in the API use the following 3246*088332b5SXin Listatus codes to indicate different kinds of errors or other conditions: 3247*088332b5SXin Li 3248*088332b5SXin Li<ul> 3249*088332b5SXin Li 3250*088332b5SXin Li<li><b><a name="pdf-LUA_OK"><code>LUA_OK</code></a> (0): </b> no errors.</li> 3251*088332b5SXin Li 3252*088332b5SXin Li<li><b><a name="pdf-LUA_ERRRUN"><code>LUA_ERRRUN</code></a>: </b> a runtime error.</li> 3253*088332b5SXin Li 3254*088332b5SXin Li<li><b><a name="pdf-LUA_ERRMEM"><code>LUA_ERRMEM</code></a>: </b> 3255*088332b5SXin Limemory allocation error. 3256*088332b5SXin LiFor such errors, Lua does not call the message handler. 3257*088332b5SXin Li</li> 3258*088332b5SXin Li 3259*088332b5SXin Li<li><b><a name="pdf-LUA_ERRERR"><code>LUA_ERRERR</code></a>: </b> error while running the message handler.</li> 3260*088332b5SXin Li 3261*088332b5SXin Li<li><b><a name="pdf-LUA_ERRSYNTAX"><code>LUA_ERRSYNTAX</code></a>: </b> syntax error during precompilation.</li> 3262*088332b5SXin Li 3263*088332b5SXin Li<li><b><a name="pdf-LUA_YIELD"><code>LUA_YIELD</code></a>: </b> the thread (coroutine) yields.</li> 3264*088332b5SXin Li 3265*088332b5SXin Li<li><b><a name="pdf-LUA_ERRFILE"><code>LUA_ERRFILE</code></a>: </b> a file-related error; 3266*088332b5SXin Lie.g., it cannot open or read the file.</li> 3267*088332b5SXin Li 3268*088332b5SXin Li</ul><p> 3269*088332b5SXin LiThese constants are defined in the header file <code>lua.h</code>. 3270*088332b5SXin Li 3271*088332b5SXin Li 3272*088332b5SXin Li 3273*088332b5SXin Li 3274*088332b5SXin Li 3275*088332b5SXin Li 3276*088332b5SXin Li 3277*088332b5SXin Li<h2>4.5 – <a name="4.5">Handling Yields in C</a></h2> 3278*088332b5SXin Li 3279*088332b5SXin Li<p> 3280*088332b5SXin LiInternally, Lua uses the C <code>longjmp</code> facility to yield a coroutine. 3281*088332b5SXin LiTherefore, if a C function <code>foo</code> calls an API function 3282*088332b5SXin Liand this API function yields 3283*088332b5SXin Li(directly or indirectly by calling another function that yields), 3284*088332b5SXin LiLua cannot return to <code>foo</code> any more, 3285*088332b5SXin Libecause the <code>longjmp</code> removes its frame from the C stack. 3286*088332b5SXin Li 3287*088332b5SXin Li 3288*088332b5SXin Li<p> 3289*088332b5SXin LiTo avoid this kind of problem, 3290*088332b5SXin LiLua raises an error whenever it tries to yield across an API call, 3291*088332b5SXin Liexcept for three functions: 3292*088332b5SXin Li<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>. 3293*088332b5SXin LiAll those functions receive a <em>continuation function</em> 3294*088332b5SXin Li(as a parameter named <code>k</code>) to continue execution after a yield. 3295*088332b5SXin Li 3296*088332b5SXin Li 3297*088332b5SXin Li<p> 3298*088332b5SXin LiWe need to set some terminology to explain continuations. 3299*088332b5SXin LiWe have a C function called from Lua which we will call 3300*088332b5SXin Lithe <em>original function</em>. 3301*088332b5SXin LiThis original function then calls one of those three functions in the C API, 3302*088332b5SXin Liwhich we will call the <em>callee function</em>, 3303*088332b5SXin Lithat then yields the current thread. 3304*088332b5SXin LiThis can happen when the callee function is <a href="#lua_yieldk"><code>lua_yieldk</code></a>, 3305*088332b5SXin Lior 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> 3306*088332b5SXin Liand the function called by them yields. 3307*088332b5SXin Li 3308*088332b5SXin Li 3309*088332b5SXin Li<p> 3310*088332b5SXin LiSuppose the running thread yields while executing the callee function. 3311*088332b5SXin LiAfter the thread resumes, 3312*088332b5SXin Liit eventually will finish running the callee function. 3313*088332b5SXin LiHowever, 3314*088332b5SXin Lithe callee function cannot return to the original function, 3315*088332b5SXin Libecause its frame in the C stack was destroyed by the yield. 3316*088332b5SXin LiInstead, Lua calls a <em>continuation function</em>, 3317*088332b5SXin Liwhich was given as an argument to the callee function. 3318*088332b5SXin LiAs the name implies, 3319*088332b5SXin Lithe continuation function should continue the task 3320*088332b5SXin Liof the original function. 3321*088332b5SXin Li 3322*088332b5SXin Li 3323*088332b5SXin Li<p> 3324*088332b5SXin LiAs an illustration, consider the following function: 3325*088332b5SXin Li 3326*088332b5SXin Li<pre> 3327*088332b5SXin Li int original_function (lua_State *L) { 3328*088332b5SXin Li ... /* code 1 */ 3329*088332b5SXin Li status = lua_pcall(L, n, m, h); /* calls Lua */ 3330*088332b5SXin Li ... /* code 2 */ 3331*088332b5SXin Li } 3332*088332b5SXin Li</pre><p> 3333*088332b5SXin LiNow we want to allow 3334*088332b5SXin Lithe Lua code being run by <a href="#lua_pcall"><code>lua_pcall</code></a> to yield. 3335*088332b5SXin LiFirst, we can rewrite our function like here: 3336*088332b5SXin Li 3337*088332b5SXin Li<pre> 3338*088332b5SXin Li int k (lua_State *L, int status, lua_KContext ctx) { 3339*088332b5SXin Li ... /* code 2 */ 3340*088332b5SXin Li } 3341*088332b5SXin Li 3342*088332b5SXin Li int original_function (lua_State *L) { 3343*088332b5SXin Li ... /* code 1 */ 3344*088332b5SXin Li return k(L, lua_pcall(L, n, m, h), ctx); 3345*088332b5SXin Li } 3346*088332b5SXin Li</pre><p> 3347*088332b5SXin LiIn the above code, 3348*088332b5SXin Lithe new function <code>k</code> is a 3349*088332b5SXin Li<em>continuation function</em> (with type <a href="#lua_KFunction"><code>lua_KFunction</code></a>), 3350*088332b5SXin Liwhich should do all the work that the original function 3351*088332b5SXin Liwas doing after calling <a href="#lua_pcall"><code>lua_pcall</code></a>. 3352*088332b5SXin LiNow, we must inform Lua that it must call <code>k</code> if the Lua code 3353*088332b5SXin Libeing executed by <a href="#lua_pcall"><code>lua_pcall</code></a> gets interrupted in some way 3354*088332b5SXin Li(errors or yielding), 3355*088332b5SXin Liso we rewrite the code as here, 3356*088332b5SXin Lireplacing <a href="#lua_pcall"><code>lua_pcall</code></a> by <a href="#lua_pcallk"><code>lua_pcallk</code></a>: 3357*088332b5SXin Li 3358*088332b5SXin Li<pre> 3359*088332b5SXin Li int original_function (lua_State *L) { 3360*088332b5SXin Li ... /* code 1 */ 3361*088332b5SXin Li return k(L, lua_pcallk(L, n, m, h, ctx2, k), ctx1); 3362*088332b5SXin Li } 3363*088332b5SXin Li</pre><p> 3364*088332b5SXin LiNote the external, explicit call to the continuation: 3365*088332b5SXin LiLua will call the continuation only if needed, that is, 3366*088332b5SXin Liin case of errors or resuming after a yield. 3367*088332b5SXin LiIf the called function returns normally without ever yielding, 3368*088332b5SXin Li<a href="#lua_pcallk"><code>lua_pcallk</code></a> (and <a href="#lua_callk"><code>lua_callk</code></a>) will also return normally. 3369*088332b5SXin Li(Of course, instead of calling the continuation in that case, 3370*088332b5SXin Liyou can do the equivalent work directly inside the original function.) 3371*088332b5SXin Li 3372*088332b5SXin Li 3373*088332b5SXin Li<p> 3374*088332b5SXin LiBesides the Lua state, 3375*088332b5SXin Lithe continuation function has two other parameters: 3376*088332b5SXin Lithe final status of the call and the context value (<code>ctx</code>) that 3377*088332b5SXin Liwas passed originally to <a href="#lua_pcallk"><code>lua_pcallk</code></a>. 3378*088332b5SXin LiLua does not use this context value; 3379*088332b5SXin Liit only passes this value from the original function to the 3380*088332b5SXin Licontinuation function. 3381*088332b5SXin LiFor <a href="#lua_pcallk"><code>lua_pcallk</code></a>, 3382*088332b5SXin Lithe status is the same value that would be returned by <a href="#lua_pcallk"><code>lua_pcallk</code></a>, 3383*088332b5SXin Liexcept that it is <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> when being executed after a yield 3384*088332b5SXin Li(instead of <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>). 3385*088332b5SXin LiFor <a href="#lua_yieldk"><code>lua_yieldk</code></a> and <a href="#lua_callk"><code>lua_callk</code></a>, 3386*088332b5SXin Lithe status is always <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> when Lua calls the continuation. 3387*088332b5SXin Li(For these two functions, 3388*088332b5SXin LiLua will not call the continuation in case of errors, 3389*088332b5SXin Libecause they do not handle errors.) 3390*088332b5SXin LiSimilarly, when using <a href="#lua_callk"><code>lua_callk</code></a>, 3391*088332b5SXin Liyou should call the continuation function 3392*088332b5SXin Liwith <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> as the status. 3393*088332b5SXin Li(For <a href="#lua_yieldk"><code>lua_yieldk</code></a>, there is not much point in calling 3394*088332b5SXin Lidirectly the continuation function, 3395*088332b5SXin Libecause <a href="#lua_yieldk"><code>lua_yieldk</code></a> usually does not return.) 3396*088332b5SXin Li 3397*088332b5SXin Li 3398*088332b5SXin Li<p> 3399*088332b5SXin LiLua treats the continuation function as if it were the original function. 3400*088332b5SXin LiThe continuation function receives the same Lua stack 3401*088332b5SXin Lifrom the original function, 3402*088332b5SXin Liin the same state it would be if the callee function had returned. 3403*088332b5SXin Li(For instance, 3404*088332b5SXin Liafter a <a href="#lua_callk"><code>lua_callk</code></a> the function and its arguments are 3405*088332b5SXin Liremoved from the stack and replaced by the results from the call.) 3406*088332b5SXin LiIt also has the same upvalues. 3407*088332b5SXin LiWhatever it returns is handled by Lua as if it were the return 3408*088332b5SXin Liof the original function. 3409*088332b5SXin Li 3410*088332b5SXin Li 3411*088332b5SXin Li 3412*088332b5SXin Li 3413*088332b5SXin Li 3414*088332b5SXin Li<h2>4.6 – <a name="4.6">Functions and Types</a></h2> 3415*088332b5SXin Li 3416*088332b5SXin Li<p> 3417*088332b5SXin LiHere we list all functions and types from the C API in 3418*088332b5SXin Lialphabetical order. 3419*088332b5SXin LiEach function has an indicator like this: 3420*088332b5SXin Li<span class="apii">[-o, +p, <em>x</em>]</span> 3421*088332b5SXin Li 3422*088332b5SXin Li 3423*088332b5SXin Li<p> 3424*088332b5SXin LiThe first field, <code>o</code>, 3425*088332b5SXin Liis how many elements the function pops from the stack. 3426*088332b5SXin LiThe second field, <code>p</code>, 3427*088332b5SXin Liis how many elements the function pushes onto the stack. 3428*088332b5SXin Li(Any function always pushes its results after popping its arguments.) 3429*088332b5SXin LiA field in the form <code>x|y</code> means the function can push (or pop) 3430*088332b5SXin Li<code>x</code> or <code>y</code> elements, 3431*088332b5SXin Lidepending on the situation; 3432*088332b5SXin Lian interrogation mark '<code>?</code>' means that 3433*088332b5SXin Liwe cannot know how many elements the function pops/pushes 3434*088332b5SXin Liby looking only at its arguments. 3435*088332b5SXin Li(For instance, they may depend on what is in the stack.) 3436*088332b5SXin LiThe third field, <code>x</code>, 3437*088332b5SXin Litells whether the function may raise errors: 3438*088332b5SXin Li'<code>-</code>' means the function never raises any error; 3439*088332b5SXin Li'<code>m</code>' means the function may raise only out-of-memory errors; 3440*088332b5SXin Li'<code>v</code>' means the function may raise the errors explained in the text; 3441*088332b5SXin Li'<code>e</code>' means the function can run arbitrary Lua code, 3442*088332b5SXin Lieither directly or through metamethods, 3443*088332b5SXin Liand therefore may raise any errors. 3444*088332b5SXin Li 3445*088332b5SXin Li 3446*088332b5SXin Li 3447*088332b5SXin Li<hr><h3><a name="lua_absindex"><code>lua_absindex</code></a></h3><p> 3448*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 3449*088332b5SXin Li<pre>int lua_absindex (lua_State *L, int idx);</pre> 3450*088332b5SXin Li 3451*088332b5SXin Li<p> 3452*088332b5SXin LiConverts the acceptable index <code>idx</code> 3453*088332b5SXin Liinto an equivalent absolute index 3454*088332b5SXin Li(that is, one that does not depend on the stack top). 3455*088332b5SXin Li 3456*088332b5SXin Li 3457*088332b5SXin Li 3458*088332b5SXin Li 3459*088332b5SXin Li 3460*088332b5SXin Li<hr><h3><a name="lua_Alloc"><code>lua_Alloc</code></a></h3> 3461*088332b5SXin Li<pre>typedef void * (*lua_Alloc) (void *ud, 3462*088332b5SXin Li void *ptr, 3463*088332b5SXin Li size_t osize, 3464*088332b5SXin Li size_t nsize);</pre> 3465*088332b5SXin Li 3466*088332b5SXin Li<p> 3467*088332b5SXin LiThe type of the memory-allocation function used by Lua states. 3468*088332b5SXin LiThe allocator function must provide a 3469*088332b5SXin Lifunctionality similar to <code>realloc</code>, 3470*088332b5SXin Libut not exactly the same. 3471*088332b5SXin LiIts arguments are 3472*088332b5SXin Li<code>ud</code>, an opaque pointer passed to <a href="#lua_newstate"><code>lua_newstate</code></a>; 3473*088332b5SXin Li<code>ptr</code>, a pointer to the block being allocated/reallocated/freed; 3474*088332b5SXin Li<code>osize</code>, the original size of the block or some code about what 3475*088332b5SXin Liis being allocated; 3476*088332b5SXin Liand <code>nsize</code>, the new size of the block. 3477*088332b5SXin Li 3478*088332b5SXin Li 3479*088332b5SXin Li<p> 3480*088332b5SXin LiWhen <code>ptr</code> is not <code>NULL</code>, 3481*088332b5SXin Li<code>osize</code> is the size of the block pointed by <code>ptr</code>, 3482*088332b5SXin Lithat is, the size given when it was allocated or reallocated. 3483*088332b5SXin Li 3484*088332b5SXin Li 3485*088332b5SXin Li<p> 3486*088332b5SXin LiWhen <code>ptr</code> is <code>NULL</code>, 3487*088332b5SXin Li<code>osize</code> encodes the kind of object that Lua is allocating. 3488*088332b5SXin Li<code>osize</code> is any of 3489*088332b5SXin Li<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>, 3490*088332b5SXin Li<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) 3491*088332b5SXin LiLua is creating a new object of that type. 3492*088332b5SXin LiWhen <code>osize</code> is some other value, 3493*088332b5SXin LiLua is allocating memory for something else. 3494*088332b5SXin Li 3495*088332b5SXin Li 3496*088332b5SXin Li<p> 3497*088332b5SXin LiLua assumes the following behavior from the allocator function: 3498*088332b5SXin Li 3499*088332b5SXin Li 3500*088332b5SXin Li<p> 3501*088332b5SXin LiWhen <code>nsize</code> is zero, 3502*088332b5SXin Lithe allocator must behave like <code>free</code> 3503*088332b5SXin Liand then return <code>NULL</code>. 3504*088332b5SXin Li 3505*088332b5SXin Li 3506*088332b5SXin Li<p> 3507*088332b5SXin LiWhen <code>nsize</code> is not zero, 3508*088332b5SXin Lithe allocator must behave like <code>realloc</code>. 3509*088332b5SXin LiIn particular, the allocator returns <code>NULL</code> 3510*088332b5SXin Liif and only if it cannot fulfill the request. 3511*088332b5SXin Li 3512*088332b5SXin Li 3513*088332b5SXin Li<p> 3514*088332b5SXin LiHere is a simple implementation for the allocator function. 3515*088332b5SXin LiIt is used in the auxiliary library by <a href="#luaL_newstate"><code>luaL_newstate</code></a>. 3516*088332b5SXin Li 3517*088332b5SXin Li<pre> 3518*088332b5SXin Li static void *l_alloc (void *ud, void *ptr, size_t osize, 3519*088332b5SXin Li size_t nsize) { 3520*088332b5SXin Li (void)ud; (void)osize; /* not used */ 3521*088332b5SXin Li if (nsize == 0) { 3522*088332b5SXin Li free(ptr); 3523*088332b5SXin Li return NULL; 3524*088332b5SXin Li } 3525*088332b5SXin Li else 3526*088332b5SXin Li return realloc(ptr, nsize); 3527*088332b5SXin Li } 3528*088332b5SXin Li</pre><p> 3529*088332b5SXin LiNote that Standard C ensures 3530*088332b5SXin Lithat <code>free(NULL)</code> has no effect and that 3531*088332b5SXin Li<code>realloc(NULL,size)</code> is equivalent to <code>malloc(size)</code>. 3532*088332b5SXin Li 3533*088332b5SXin Li 3534*088332b5SXin Li 3535*088332b5SXin Li 3536*088332b5SXin Li 3537*088332b5SXin Li<hr><h3><a name="lua_arith"><code>lua_arith</code></a></h3><p> 3538*088332b5SXin Li<span class="apii">[-(2|1), +1, <em>e</em>]</span> 3539*088332b5SXin Li<pre>void lua_arith (lua_State *L, int op);</pre> 3540*088332b5SXin Li 3541*088332b5SXin Li<p> 3542*088332b5SXin LiPerforms an arithmetic or bitwise operation over the two values 3543*088332b5SXin Li(or one, in the case of negations) 3544*088332b5SXin Liat the top of the stack, 3545*088332b5SXin Liwith the value on the top being the second operand, 3546*088332b5SXin Lipops these values, and pushes the result of the operation. 3547*088332b5SXin LiThe function follows the semantics of the corresponding Lua operator 3548*088332b5SXin Li(that is, it may call metamethods). 3549*088332b5SXin Li 3550*088332b5SXin Li 3551*088332b5SXin Li<p> 3552*088332b5SXin LiThe value of <code>op</code> must be one of the following constants: 3553*088332b5SXin Li 3554*088332b5SXin Li<ul> 3555*088332b5SXin Li 3556*088332b5SXin Li<li><b><a name="pdf-LUA_OPADD"><code>LUA_OPADD</code></a>: </b> performs addition (<code>+</code>)</li> 3557*088332b5SXin Li<li><b><a name="pdf-LUA_OPSUB"><code>LUA_OPSUB</code></a>: </b> performs subtraction (<code>-</code>)</li> 3558*088332b5SXin Li<li><b><a name="pdf-LUA_OPMUL"><code>LUA_OPMUL</code></a>: </b> performs multiplication (<code>*</code>)</li> 3559*088332b5SXin Li<li><b><a name="pdf-LUA_OPDIV"><code>LUA_OPDIV</code></a>: </b> performs float division (<code>/</code>)</li> 3560*088332b5SXin Li<li><b><a name="pdf-LUA_OPIDIV"><code>LUA_OPIDIV</code></a>: </b> performs floor division (<code>//</code>)</li> 3561*088332b5SXin Li<li><b><a name="pdf-LUA_OPMOD"><code>LUA_OPMOD</code></a>: </b> performs modulo (<code>%</code>)</li> 3562*088332b5SXin Li<li><b><a name="pdf-LUA_OPPOW"><code>LUA_OPPOW</code></a>: </b> performs exponentiation (<code>^</code>)</li> 3563*088332b5SXin Li<li><b><a name="pdf-LUA_OPUNM"><code>LUA_OPUNM</code></a>: </b> performs mathematical negation (unary <code>-</code>)</li> 3564*088332b5SXin Li<li><b><a name="pdf-LUA_OPBNOT"><code>LUA_OPBNOT</code></a>: </b> performs bitwise NOT (<code>~</code>)</li> 3565*088332b5SXin Li<li><b><a name="pdf-LUA_OPBAND"><code>LUA_OPBAND</code></a>: </b> performs bitwise AND (<code>&</code>)</li> 3566*088332b5SXin Li<li><b><a name="pdf-LUA_OPBOR"><code>LUA_OPBOR</code></a>: </b> performs bitwise OR (<code>|</code>)</li> 3567*088332b5SXin Li<li><b><a name="pdf-LUA_OPBXOR"><code>LUA_OPBXOR</code></a>: </b> performs bitwise exclusive OR (<code>~</code>)</li> 3568*088332b5SXin Li<li><b><a name="pdf-LUA_OPSHL"><code>LUA_OPSHL</code></a>: </b> performs left shift (<code><<</code>)</li> 3569*088332b5SXin Li<li><b><a name="pdf-LUA_OPSHR"><code>LUA_OPSHR</code></a>: </b> performs right shift (<code>>></code>)</li> 3570*088332b5SXin Li 3571*088332b5SXin Li</ul> 3572*088332b5SXin Li 3573*088332b5SXin Li 3574*088332b5SXin Li 3575*088332b5SXin Li 3576*088332b5SXin Li<hr><h3><a name="lua_atpanic"><code>lua_atpanic</code></a></h3><p> 3577*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 3578*088332b5SXin Li<pre>lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf);</pre> 3579*088332b5SXin Li 3580*088332b5SXin Li<p> 3581*088332b5SXin LiSets a new panic function and returns the old one (see <a href="#4.4">§4.4</a>). 3582*088332b5SXin Li 3583*088332b5SXin Li 3584*088332b5SXin Li 3585*088332b5SXin Li 3586*088332b5SXin Li 3587*088332b5SXin Li<hr><h3><a name="lua_call"><code>lua_call</code></a></h3><p> 3588*088332b5SXin Li<span class="apii">[-(nargs+1), +nresults, <em>e</em>]</span> 3589*088332b5SXin Li<pre>void lua_call (lua_State *L, int nargs, int nresults);</pre> 3590*088332b5SXin Li 3591*088332b5SXin Li<p> 3592*088332b5SXin LiCalls a function. 3593*088332b5SXin LiLike regular Lua calls, 3594*088332b5SXin Li<code>lua_call</code> respects the <code>__call</code> metamethod. 3595*088332b5SXin LiSo, here the word "function" 3596*088332b5SXin Limeans any callable value. 3597*088332b5SXin Li 3598*088332b5SXin Li 3599*088332b5SXin Li<p> 3600*088332b5SXin LiTo do a call you must use the following protocol: 3601*088332b5SXin Lifirst, the function to be called is pushed onto the stack; 3602*088332b5SXin Lithen, the arguments to the call are pushed 3603*088332b5SXin Liin direct order; 3604*088332b5SXin Lithat is, the first argument is pushed first. 3605*088332b5SXin LiFinally you call <a href="#lua_call"><code>lua_call</code></a>; 3606*088332b5SXin Li<code>nargs</code> is the number of arguments that you pushed onto the stack. 3607*088332b5SXin LiWhen the function returns, 3608*088332b5SXin Liall arguments and the function value are popped 3609*088332b5SXin Liand the call results are pushed onto the stack. 3610*088332b5SXin LiThe number of results is adjusted to <code>nresults</code>, 3611*088332b5SXin Liunless <code>nresults</code> is <a name="pdf-LUA_MULTRET"><code>LUA_MULTRET</code></a>. 3612*088332b5SXin LiIn this case, all results from the function are pushed; 3613*088332b5SXin LiLua takes care that the returned values fit into the stack space, 3614*088332b5SXin Libut it does not ensure any extra space in the stack. 3615*088332b5SXin LiThe function results are pushed onto the stack in direct order 3616*088332b5SXin Li(the first result is pushed first), 3617*088332b5SXin Liso that after the call the last result is on the top of the stack. 3618*088332b5SXin Li 3619*088332b5SXin Li 3620*088332b5SXin Li<p> 3621*088332b5SXin LiAny error while calling and running the function is propagated upwards 3622*088332b5SXin Li(with a <code>longjmp</code>). 3623*088332b5SXin Li 3624*088332b5SXin Li 3625*088332b5SXin Li<p> 3626*088332b5SXin LiThe following example shows how the host program can do the 3627*088332b5SXin Liequivalent to this Lua code: 3628*088332b5SXin Li 3629*088332b5SXin Li<pre> 3630*088332b5SXin Li a = f("how", t.x, 14) 3631*088332b5SXin Li</pre><p> 3632*088332b5SXin LiHere it is in C: 3633*088332b5SXin Li 3634*088332b5SXin Li<pre> 3635*088332b5SXin Li lua_getglobal(L, "f"); /* function to be called */ 3636*088332b5SXin Li lua_pushliteral(L, "how"); /* 1st argument */ 3637*088332b5SXin Li lua_getglobal(L, "t"); /* table to be indexed */ 3638*088332b5SXin Li lua_getfield(L, -1, "x"); /* push result of t.x (2nd arg) */ 3639*088332b5SXin Li lua_remove(L, -2); /* remove 't' from the stack */ 3640*088332b5SXin Li lua_pushinteger(L, 14); /* 3rd argument */ 3641*088332b5SXin Li lua_call(L, 3, 1); /* call 'f' with 3 arguments and 1 result */ 3642*088332b5SXin Li lua_setglobal(L, "a"); /* set global 'a' */ 3643*088332b5SXin Li</pre><p> 3644*088332b5SXin LiNote that the code above is <em>balanced</em>: 3645*088332b5SXin Liat its end, the stack is back to its original configuration. 3646*088332b5SXin LiThis is considered good programming practice. 3647*088332b5SXin Li 3648*088332b5SXin Li 3649*088332b5SXin Li 3650*088332b5SXin Li 3651*088332b5SXin Li 3652*088332b5SXin Li<hr><h3><a name="lua_callk"><code>lua_callk</code></a></h3><p> 3653*088332b5SXin Li<span class="apii">[-(nargs + 1), +nresults, <em>e</em>]</span> 3654*088332b5SXin Li<pre>void lua_callk (lua_State *L, 3655*088332b5SXin Li int nargs, 3656*088332b5SXin Li int nresults, 3657*088332b5SXin Li lua_KContext ctx, 3658*088332b5SXin Li lua_KFunction k);</pre> 3659*088332b5SXin Li 3660*088332b5SXin Li<p> 3661*088332b5SXin LiThis function behaves exactly like <a href="#lua_call"><code>lua_call</code></a>, 3662*088332b5SXin Libut allows the called function to yield (see <a href="#4.5">§4.5</a>). 3663*088332b5SXin Li 3664*088332b5SXin Li 3665*088332b5SXin Li 3666*088332b5SXin Li 3667*088332b5SXin Li 3668*088332b5SXin Li<hr><h3><a name="lua_CFunction"><code>lua_CFunction</code></a></h3> 3669*088332b5SXin Li<pre>typedef int (*lua_CFunction) (lua_State *L);</pre> 3670*088332b5SXin Li 3671*088332b5SXin Li<p> 3672*088332b5SXin LiType for C functions. 3673*088332b5SXin Li 3674*088332b5SXin Li 3675*088332b5SXin Li<p> 3676*088332b5SXin LiIn order to communicate properly with Lua, 3677*088332b5SXin Lia C function must use the following protocol, 3678*088332b5SXin Liwhich defines the way parameters and results are passed: 3679*088332b5SXin Lia C function receives its arguments from Lua in its stack 3680*088332b5SXin Liin direct order (the first argument is pushed first). 3681*088332b5SXin LiSo, when the function starts, 3682*088332b5SXin Li<code>lua_gettop(L)</code> returns the number of arguments received by the function. 3683*088332b5SXin LiThe first argument (if any) is at index 1 3684*088332b5SXin Liand its last argument is at index <code>lua_gettop(L)</code>. 3685*088332b5SXin LiTo return values to Lua, a C function just pushes them onto the stack, 3686*088332b5SXin Liin direct order (the first result is pushed first), 3687*088332b5SXin Liand returns in C the number of results. 3688*088332b5SXin LiAny other value in the stack below the results will be properly 3689*088332b5SXin Lidiscarded by Lua. 3690*088332b5SXin LiLike a Lua function, a C function called by Lua can also return 3691*088332b5SXin Limany results. 3692*088332b5SXin Li 3693*088332b5SXin Li 3694*088332b5SXin Li<p> 3695*088332b5SXin LiAs an example, the following function receives a variable number 3696*088332b5SXin Liof numeric arguments and returns their average and their sum: 3697*088332b5SXin Li 3698*088332b5SXin Li<pre> 3699*088332b5SXin Li static int foo (lua_State *L) { 3700*088332b5SXin Li int n = lua_gettop(L); /* number of arguments */ 3701*088332b5SXin Li lua_Number sum = 0.0; 3702*088332b5SXin Li int i; 3703*088332b5SXin Li for (i = 1; i <= n; i++) { 3704*088332b5SXin Li if (!lua_isnumber(L, i)) { 3705*088332b5SXin Li lua_pushliteral(L, "incorrect argument"); 3706*088332b5SXin Li lua_error(L); 3707*088332b5SXin Li } 3708*088332b5SXin Li sum += lua_tonumber(L, i); 3709*088332b5SXin Li } 3710*088332b5SXin Li lua_pushnumber(L, sum/n); /* first result */ 3711*088332b5SXin Li lua_pushnumber(L, sum); /* second result */ 3712*088332b5SXin Li return 2; /* number of results */ 3713*088332b5SXin Li } 3714*088332b5SXin Li</pre> 3715*088332b5SXin Li 3716*088332b5SXin Li 3717*088332b5SXin Li 3718*088332b5SXin Li 3719*088332b5SXin Li<hr><h3><a name="lua_checkstack"><code>lua_checkstack</code></a></h3><p> 3720*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 3721*088332b5SXin Li<pre>int lua_checkstack (lua_State *L, int n);</pre> 3722*088332b5SXin Li 3723*088332b5SXin Li<p> 3724*088332b5SXin LiEnsures that the stack has space for at least <code>n</code> extra elements, 3725*088332b5SXin Lithat is, that you can safely push up to <code>n</code> values into it. 3726*088332b5SXin LiIt returns false if it cannot fulfill the request, 3727*088332b5SXin Lieither because it would cause the stack 3728*088332b5SXin Lito be greater than a fixed maximum size 3729*088332b5SXin Li(typically at least several thousand elements) or 3730*088332b5SXin Libecause it cannot allocate memory for the extra space. 3731*088332b5SXin LiThis function never shrinks the stack; 3732*088332b5SXin Liif the stack already has space for the extra elements, 3733*088332b5SXin Liit is left unchanged. 3734*088332b5SXin Li 3735*088332b5SXin Li 3736*088332b5SXin Li 3737*088332b5SXin Li 3738*088332b5SXin Li 3739*088332b5SXin Li<hr><h3><a name="lua_close"><code>lua_close</code></a></h3><p> 3740*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 3741*088332b5SXin Li<pre>void lua_close (lua_State *L);</pre> 3742*088332b5SXin Li 3743*088332b5SXin Li<p> 3744*088332b5SXin LiClose all active to-be-closed variables in the main thread, 3745*088332b5SXin Lirelease all objects in the given Lua state 3746*088332b5SXin Li(calling the corresponding garbage-collection metamethods, if any), 3747*088332b5SXin Liand frees all dynamic memory used by this state. 3748*088332b5SXin Li 3749*088332b5SXin Li 3750*088332b5SXin Li<p> 3751*088332b5SXin LiOn several platforms, you may not need to call this function, 3752*088332b5SXin Libecause all resources are naturally released when the host program ends. 3753*088332b5SXin LiOn the other hand, long-running programs that create multiple states, 3754*088332b5SXin Lisuch as daemons or web servers, 3755*088332b5SXin Liwill probably need to close states as soon as they are not needed. 3756*088332b5SXin Li 3757*088332b5SXin Li 3758*088332b5SXin Li 3759*088332b5SXin Li 3760*088332b5SXin Li 3761*088332b5SXin Li<hr><h3><a name="lua_compare"><code>lua_compare</code></a></h3><p> 3762*088332b5SXin Li<span class="apii">[-0, +0, <em>e</em>]</span> 3763*088332b5SXin Li<pre>int lua_compare (lua_State *L, int index1, int index2, int op);</pre> 3764*088332b5SXin Li 3765*088332b5SXin Li<p> 3766*088332b5SXin LiCompares two Lua values. 3767*088332b5SXin LiReturns 1 if the value at index <code>index1</code> satisfies <code>op</code> 3768*088332b5SXin Liwhen compared with the value at index <code>index2</code>, 3769*088332b5SXin Lifollowing the semantics of the corresponding Lua operator 3770*088332b5SXin Li(that is, it may call metamethods). 3771*088332b5SXin LiOtherwise returns 0. 3772*088332b5SXin LiAlso returns 0 if any of the indices is not valid. 3773*088332b5SXin Li 3774*088332b5SXin Li 3775*088332b5SXin Li<p> 3776*088332b5SXin LiThe value of <code>op</code> must be one of the following constants: 3777*088332b5SXin Li 3778*088332b5SXin Li<ul> 3779*088332b5SXin Li 3780*088332b5SXin Li<li><b><a name="pdf-LUA_OPEQ"><code>LUA_OPEQ</code></a>: </b> compares for equality (<code>==</code>)</li> 3781*088332b5SXin Li<li><b><a name="pdf-LUA_OPLT"><code>LUA_OPLT</code></a>: </b> compares for less than (<code><</code>)</li> 3782*088332b5SXin Li<li><b><a name="pdf-LUA_OPLE"><code>LUA_OPLE</code></a>: </b> compares for less or equal (<code><=</code>)</li> 3783*088332b5SXin Li 3784*088332b5SXin Li</ul> 3785*088332b5SXin Li 3786*088332b5SXin Li 3787*088332b5SXin Li 3788*088332b5SXin Li 3789*088332b5SXin Li<hr><h3><a name="lua_concat"><code>lua_concat</code></a></h3><p> 3790*088332b5SXin Li<span class="apii">[-n, +1, <em>e</em>]</span> 3791*088332b5SXin Li<pre>void lua_concat (lua_State *L, int n);</pre> 3792*088332b5SXin Li 3793*088332b5SXin Li<p> 3794*088332b5SXin LiConcatenates the <code>n</code> values at the top of the stack, 3795*088332b5SXin Lipops them, and leaves the result on the top. 3796*088332b5SXin LiIf <code>n</code> is 1, the result is the single value on the stack 3797*088332b5SXin Li(that is, the function does nothing); 3798*088332b5SXin Liif <code>n</code> is 0, the result is the empty string. 3799*088332b5SXin LiConcatenation is performed following the usual semantics of Lua 3800*088332b5SXin Li(see <a href="#3.4.6">§3.4.6</a>). 3801*088332b5SXin Li 3802*088332b5SXin Li 3803*088332b5SXin Li 3804*088332b5SXin Li 3805*088332b5SXin Li 3806*088332b5SXin Li<hr><h3><a name="lua_copy"><code>lua_copy</code></a></h3><p> 3807*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 3808*088332b5SXin Li<pre>void lua_copy (lua_State *L, int fromidx, int toidx);</pre> 3809*088332b5SXin Li 3810*088332b5SXin Li<p> 3811*088332b5SXin LiCopies the element at index <code>fromidx</code> 3812*088332b5SXin Liinto the valid index <code>toidx</code>, 3813*088332b5SXin Lireplacing the value at that position. 3814*088332b5SXin LiValues at other positions are not affected. 3815*088332b5SXin Li 3816*088332b5SXin Li 3817*088332b5SXin Li 3818*088332b5SXin Li 3819*088332b5SXin Li 3820*088332b5SXin Li<hr><h3><a name="lua_createtable"><code>lua_createtable</code></a></h3><p> 3821*088332b5SXin Li<span class="apii">[-0, +1, <em>m</em>]</span> 3822*088332b5SXin Li<pre>void lua_createtable (lua_State *L, int narr, int nrec);</pre> 3823*088332b5SXin Li 3824*088332b5SXin Li<p> 3825*088332b5SXin LiCreates a new empty table and pushes it onto the stack. 3826*088332b5SXin LiParameter <code>narr</code> is a hint for how many elements the table 3827*088332b5SXin Liwill have as a sequence; 3828*088332b5SXin Liparameter <code>nrec</code> is a hint for how many other elements 3829*088332b5SXin Lithe table will have. 3830*088332b5SXin LiLua may use these hints to preallocate memory for the new table. 3831*088332b5SXin LiThis preallocation may help performance when you know in advance 3832*088332b5SXin Lihow many elements the table will have. 3833*088332b5SXin LiOtherwise you can use the function <a href="#lua_newtable"><code>lua_newtable</code></a>. 3834*088332b5SXin Li 3835*088332b5SXin Li 3836*088332b5SXin Li 3837*088332b5SXin Li 3838*088332b5SXin Li 3839*088332b5SXin Li<hr><h3><a name="lua_dump"><code>lua_dump</code></a></h3><p> 3840*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 3841*088332b5SXin Li<pre>int lua_dump (lua_State *L, 3842*088332b5SXin Li lua_Writer writer, 3843*088332b5SXin Li void *data, 3844*088332b5SXin Li int strip);</pre> 3845*088332b5SXin Li 3846*088332b5SXin Li<p> 3847*088332b5SXin LiDumps a function as a binary chunk. 3848*088332b5SXin LiReceives a Lua function on the top of the stack 3849*088332b5SXin Liand produces a binary chunk that, 3850*088332b5SXin Liif loaded again, 3851*088332b5SXin Liresults in a function equivalent to the one dumped. 3852*088332b5SXin LiAs it produces parts of the chunk, 3853*088332b5SXin Li<a href="#lua_dump"><code>lua_dump</code></a> calls function <code>writer</code> (see <a href="#lua_Writer"><code>lua_Writer</code></a>) 3854*088332b5SXin Liwith the given <code>data</code> 3855*088332b5SXin Lito write them. 3856*088332b5SXin Li 3857*088332b5SXin Li 3858*088332b5SXin Li<p> 3859*088332b5SXin LiIf <code>strip</code> is true, 3860*088332b5SXin Lithe binary representation may not include all debug information 3861*088332b5SXin Liabout the function, 3862*088332b5SXin Lito save space. 3863*088332b5SXin Li 3864*088332b5SXin Li 3865*088332b5SXin Li<p> 3866*088332b5SXin LiThe value returned is the error code returned by the last 3867*088332b5SXin Licall to the writer; 3868*088332b5SXin Li0 means no errors. 3869*088332b5SXin Li 3870*088332b5SXin Li 3871*088332b5SXin Li<p> 3872*088332b5SXin LiThis function does not pop the Lua function from the stack. 3873*088332b5SXin Li 3874*088332b5SXin Li 3875*088332b5SXin Li 3876*088332b5SXin Li 3877*088332b5SXin Li 3878*088332b5SXin Li<hr><h3><a name="lua_error"><code>lua_error</code></a></h3><p> 3879*088332b5SXin Li<span class="apii">[-1, +0, <em>v</em>]</span> 3880*088332b5SXin Li<pre>int lua_error (lua_State *L);</pre> 3881*088332b5SXin Li 3882*088332b5SXin Li<p> 3883*088332b5SXin LiRaises a Lua error, 3884*088332b5SXin Liusing the value on the top of the stack as the error object. 3885*088332b5SXin LiThis function does a long jump, 3886*088332b5SXin Liand therefore never returns 3887*088332b5SXin Li(see <a href="#luaL_error"><code>luaL_error</code></a>). 3888*088332b5SXin Li 3889*088332b5SXin Li 3890*088332b5SXin Li 3891*088332b5SXin Li 3892*088332b5SXin Li 3893*088332b5SXin Li<hr><h3><a name="lua_gc"><code>lua_gc</code></a></h3><p> 3894*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 3895*088332b5SXin Li<pre>int lua_gc (lua_State *L, int what, ...);</pre> 3896*088332b5SXin Li 3897*088332b5SXin Li<p> 3898*088332b5SXin LiControls the garbage collector. 3899*088332b5SXin Li 3900*088332b5SXin Li 3901*088332b5SXin Li<p> 3902*088332b5SXin LiThis function performs several tasks, 3903*088332b5SXin Liaccording to the value of the parameter <code>what</code>. 3904*088332b5SXin LiFor options that need extra arguments, 3905*088332b5SXin Lithey are listed after the option. 3906*088332b5SXin Li 3907*088332b5SXin Li<ul> 3908*088332b5SXin Li 3909*088332b5SXin Li<li><b><code>LUA_GCCOLLECT</code>: </b> 3910*088332b5SXin LiPerforms a full garbage-collection cycle. 3911*088332b5SXin Li</li> 3912*088332b5SXin Li 3913*088332b5SXin Li<li><b><code>LUA_GCSTOP</code>: </b> 3914*088332b5SXin LiStops the garbage collector. 3915*088332b5SXin Li</li> 3916*088332b5SXin Li 3917*088332b5SXin Li<li><b><code>LUA_GCRESTART</code>: </b> 3918*088332b5SXin LiRestarts the garbage collector. 3919*088332b5SXin Li</li> 3920*088332b5SXin Li 3921*088332b5SXin Li<li><b><code>LUA_GCCOUNT</code>: </b> 3922*088332b5SXin LiReturns the current amount of memory (in Kbytes) in use by Lua. 3923*088332b5SXin Li</li> 3924*088332b5SXin Li 3925*088332b5SXin Li<li><b><code>LUA_GCCOUNTB</code>: </b> 3926*088332b5SXin LiReturns the remainder of dividing the current amount of bytes of 3927*088332b5SXin Limemory in use by Lua by 1024. 3928*088332b5SXin Li</li> 3929*088332b5SXin Li 3930*088332b5SXin Li<li><b><code>LUA_GCSTEP</code> <code>(int stepsize)</code>: </b> 3931*088332b5SXin LiPerforms an incremental step of garbage collection, 3932*088332b5SXin Licorresponding to the allocation of <code>stepsize</code> Kbytes. 3933*088332b5SXin Li</li> 3934*088332b5SXin Li 3935*088332b5SXin Li<li><b><code>LUA_GCISRUNNING</code>: </b> 3936*088332b5SXin LiReturns a boolean that tells whether the collector is running 3937*088332b5SXin Li(i.e., not stopped). 3938*088332b5SXin Li</li> 3939*088332b5SXin Li 3940*088332b5SXin Li<li><b><code>LUA_GCINC</code> (int pause, int stepmul, stepsize): </b> 3941*088332b5SXin LiChanges the collector to incremental mode 3942*088332b5SXin Liwith the given parameters (see <a href="#2.5.1">§2.5.1</a>). 3943*088332b5SXin LiReturns the previous mode (<code>LUA_GCGEN</code> or <code>LUA_GCINC</code>). 3944*088332b5SXin Li</li> 3945*088332b5SXin Li 3946*088332b5SXin Li<li><b><code>LUA_GCGEN</code> (int minormul, int majormul): </b> 3947*088332b5SXin LiChanges the collector to generational mode 3948*088332b5SXin Liwith the given parameters (see <a href="#2.5.2">§2.5.2</a>). 3949*088332b5SXin LiReturns the previous mode (<code>LUA_GCGEN</code> or <code>LUA_GCINC</code>). 3950*088332b5SXin Li</li> 3951*088332b5SXin Li 3952*088332b5SXin Li</ul><p> 3953*088332b5SXin LiFor more details about these options, 3954*088332b5SXin Lisee <a href="#pdf-collectgarbage"><code>collectgarbage</code></a>. 3955*088332b5SXin Li 3956*088332b5SXin Li 3957*088332b5SXin Li 3958*088332b5SXin Li 3959*088332b5SXin Li 3960*088332b5SXin Li<hr><h3><a name="lua_getallocf"><code>lua_getallocf</code></a></h3><p> 3961*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 3962*088332b5SXin Li<pre>lua_Alloc lua_getallocf (lua_State *L, void **ud);</pre> 3963*088332b5SXin Li 3964*088332b5SXin Li<p> 3965*088332b5SXin LiReturns the memory-allocation function of a given state. 3966*088332b5SXin LiIf <code>ud</code> is not <code>NULL</code>, Lua stores in <code>*ud</code> the 3967*088332b5SXin Liopaque pointer given when the memory-allocator function was set. 3968*088332b5SXin Li 3969*088332b5SXin Li 3970*088332b5SXin Li 3971*088332b5SXin Li 3972*088332b5SXin Li 3973*088332b5SXin Li<hr><h3><a name="lua_getfield"><code>lua_getfield</code></a></h3><p> 3974*088332b5SXin Li<span class="apii">[-0, +1, <em>e</em>]</span> 3975*088332b5SXin Li<pre>int lua_getfield (lua_State *L, int index, const char *k);</pre> 3976*088332b5SXin Li 3977*088332b5SXin Li<p> 3978*088332b5SXin LiPushes onto the stack the value <code>t[k]</code>, 3979*088332b5SXin Liwhere <code>t</code> is the value at the given index. 3980*088332b5SXin LiAs in Lua, this function may trigger a metamethod 3981*088332b5SXin Lifor the "index" event (see <a href="#2.4">§2.4</a>). 3982*088332b5SXin Li 3983*088332b5SXin Li 3984*088332b5SXin Li<p> 3985*088332b5SXin LiReturns the type of the pushed value. 3986*088332b5SXin Li 3987*088332b5SXin Li 3988*088332b5SXin Li 3989*088332b5SXin Li 3990*088332b5SXin Li 3991*088332b5SXin Li<hr><h3><a name="lua_getextraspace"><code>lua_getextraspace</code></a></h3><p> 3992*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 3993*088332b5SXin Li<pre>void *lua_getextraspace (lua_State *L);</pre> 3994*088332b5SXin Li 3995*088332b5SXin Li<p> 3996*088332b5SXin LiReturns a pointer to a raw memory area associated with the 3997*088332b5SXin Ligiven Lua state. 3998*088332b5SXin LiThe application can use this area for any purpose; 3999*088332b5SXin LiLua does not use it for anything. 4000*088332b5SXin Li 4001*088332b5SXin Li 4002*088332b5SXin Li<p> 4003*088332b5SXin LiEach new thread has this area initialized with a copy 4004*088332b5SXin Liof the area of the main thread. 4005*088332b5SXin Li 4006*088332b5SXin Li 4007*088332b5SXin Li<p> 4008*088332b5SXin LiBy default, this area has the size of a pointer to void, 4009*088332b5SXin Libut you can recompile Lua with a different size for this area. 4010*088332b5SXin Li(See <code>LUA_EXTRASPACE</code> in <code>luaconf.h</code>.) 4011*088332b5SXin Li 4012*088332b5SXin Li 4013*088332b5SXin Li 4014*088332b5SXin Li 4015*088332b5SXin Li 4016*088332b5SXin Li<hr><h3><a name="lua_getglobal"><code>lua_getglobal</code></a></h3><p> 4017*088332b5SXin Li<span class="apii">[-0, +1, <em>e</em>]</span> 4018*088332b5SXin Li<pre>int lua_getglobal (lua_State *L, const char *name);</pre> 4019*088332b5SXin Li 4020*088332b5SXin Li<p> 4021*088332b5SXin LiPushes onto the stack the value of the global <code>name</code>. 4022*088332b5SXin LiReturns the type of that value. 4023*088332b5SXin Li 4024*088332b5SXin Li 4025*088332b5SXin Li 4026*088332b5SXin Li 4027*088332b5SXin Li 4028*088332b5SXin Li<hr><h3><a name="lua_geti"><code>lua_geti</code></a></h3><p> 4029*088332b5SXin Li<span class="apii">[-0, +1, <em>e</em>]</span> 4030*088332b5SXin Li<pre>int lua_geti (lua_State *L, int index, lua_Integer i);</pre> 4031*088332b5SXin Li 4032*088332b5SXin Li<p> 4033*088332b5SXin LiPushes onto the stack the value <code>t[i]</code>, 4034*088332b5SXin Liwhere <code>t</code> is the value at the given index. 4035*088332b5SXin LiAs in Lua, this function may trigger a metamethod 4036*088332b5SXin Lifor the "index" event (see <a href="#2.4">§2.4</a>). 4037*088332b5SXin Li 4038*088332b5SXin Li 4039*088332b5SXin Li<p> 4040*088332b5SXin LiReturns the type of the pushed value. 4041*088332b5SXin Li 4042*088332b5SXin Li 4043*088332b5SXin Li 4044*088332b5SXin Li 4045*088332b5SXin Li 4046*088332b5SXin Li<hr><h3><a name="lua_getmetatable"><code>lua_getmetatable</code></a></h3><p> 4047*088332b5SXin Li<span class="apii">[-0, +(0|1), –]</span> 4048*088332b5SXin Li<pre>int lua_getmetatable (lua_State *L, int index);</pre> 4049*088332b5SXin Li 4050*088332b5SXin Li<p> 4051*088332b5SXin LiIf the value at the given index has a metatable, 4052*088332b5SXin Lithe function pushes that metatable onto the stack and returns 1. 4053*088332b5SXin LiOtherwise, 4054*088332b5SXin Lithe function returns 0 and pushes nothing on the stack. 4055*088332b5SXin Li 4056*088332b5SXin Li 4057*088332b5SXin Li 4058*088332b5SXin Li 4059*088332b5SXin Li 4060*088332b5SXin Li<hr><h3><a name="lua_gettable"><code>lua_gettable</code></a></h3><p> 4061*088332b5SXin Li<span class="apii">[-1, +1, <em>e</em>]</span> 4062*088332b5SXin Li<pre>int lua_gettable (lua_State *L, int index);</pre> 4063*088332b5SXin Li 4064*088332b5SXin Li<p> 4065*088332b5SXin LiPushes onto the stack the value <code>t[k]</code>, 4066*088332b5SXin Liwhere <code>t</code> is the value at the given index 4067*088332b5SXin Liand <code>k</code> is the value on the top of the stack. 4068*088332b5SXin Li 4069*088332b5SXin Li 4070*088332b5SXin Li<p> 4071*088332b5SXin LiThis function pops the key from the stack, 4072*088332b5SXin Lipushing the resulting value in its place. 4073*088332b5SXin LiAs in Lua, this function may trigger a metamethod 4074*088332b5SXin Lifor the "index" event (see <a href="#2.4">§2.4</a>). 4075*088332b5SXin Li 4076*088332b5SXin Li 4077*088332b5SXin Li<p> 4078*088332b5SXin LiReturns the type of the pushed value. 4079*088332b5SXin Li 4080*088332b5SXin Li 4081*088332b5SXin Li 4082*088332b5SXin Li 4083*088332b5SXin Li 4084*088332b5SXin Li<hr><h3><a name="lua_gettop"><code>lua_gettop</code></a></h3><p> 4085*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 4086*088332b5SXin Li<pre>int lua_gettop (lua_State *L);</pre> 4087*088332b5SXin Li 4088*088332b5SXin Li<p> 4089*088332b5SXin LiReturns the index of the top element in the stack. 4090*088332b5SXin LiBecause indices start at 1, 4091*088332b5SXin Lithis result is equal to the number of elements in the stack; 4092*088332b5SXin Liin particular, 0 means an empty stack. 4093*088332b5SXin Li 4094*088332b5SXin Li 4095*088332b5SXin Li 4096*088332b5SXin Li 4097*088332b5SXin Li 4098*088332b5SXin Li<hr><h3><a name="lua_getiuservalue"><code>lua_getiuservalue</code></a></h3><p> 4099*088332b5SXin Li<span class="apii">[-0, +1, –]</span> 4100*088332b5SXin Li<pre>int lua_getiuservalue (lua_State *L, int index, int n);</pre> 4101*088332b5SXin Li 4102*088332b5SXin Li<p> 4103*088332b5SXin LiPushes onto the stack the <code>n</code>-th user value associated with the 4104*088332b5SXin Lifull userdata at the given index and 4105*088332b5SXin Lireturns the type of the pushed value. 4106*088332b5SXin Li 4107*088332b5SXin Li 4108*088332b5SXin Li<p> 4109*088332b5SXin LiIf the userdata does not have that value, 4110*088332b5SXin Lipushes <b>nil</b> and returns <a href="#pdf-LUA_TNONE"><code>LUA_TNONE</code></a>. 4111*088332b5SXin Li 4112*088332b5SXin Li 4113*088332b5SXin Li 4114*088332b5SXin Li 4115*088332b5SXin Li 4116*088332b5SXin Li<hr><h3><a name="lua_insert"><code>lua_insert</code></a></h3><p> 4117*088332b5SXin Li<span class="apii">[-1, +1, –]</span> 4118*088332b5SXin Li<pre>void lua_insert (lua_State *L, int index);</pre> 4119*088332b5SXin Li 4120*088332b5SXin Li<p> 4121*088332b5SXin LiMoves the top element into the given valid index, 4122*088332b5SXin Lishifting up the elements above this index to open space. 4123*088332b5SXin LiThis function cannot be called with a pseudo-index, 4124*088332b5SXin Libecause a pseudo-index is not an actual stack position. 4125*088332b5SXin Li 4126*088332b5SXin Li 4127*088332b5SXin Li 4128*088332b5SXin Li 4129*088332b5SXin Li 4130*088332b5SXin Li<hr><h3><a name="lua_Integer"><code>lua_Integer</code></a></h3> 4131*088332b5SXin Li<pre>typedef ... lua_Integer;</pre> 4132*088332b5SXin Li 4133*088332b5SXin Li<p> 4134*088332b5SXin LiThe type of integers in Lua. 4135*088332b5SXin Li 4136*088332b5SXin Li 4137*088332b5SXin Li<p> 4138*088332b5SXin LiBy default this type is <code>long long</code>, 4139*088332b5SXin Li(usually a 64-bit two-complement integer), 4140*088332b5SXin Libut that can be changed to <code>long</code> or <code>int</code> 4141*088332b5SXin Li(usually a 32-bit two-complement integer). 4142*088332b5SXin Li(See <code>LUA_INT_TYPE</code> in <code>luaconf.h</code>.) 4143*088332b5SXin Li 4144*088332b5SXin Li 4145*088332b5SXin Li<p> 4146*088332b5SXin LiLua also defines the constants 4147*088332b5SXin Li<a name="pdf-LUA_MININTEGER"><code>LUA_MININTEGER</code></a> and <a name="pdf-LUA_MAXINTEGER"><code>LUA_MAXINTEGER</code></a>, 4148*088332b5SXin Liwith the minimum and the maximum values that fit in this type. 4149*088332b5SXin Li 4150*088332b5SXin Li 4151*088332b5SXin Li 4152*088332b5SXin Li 4153*088332b5SXin Li 4154*088332b5SXin Li<hr><h3><a name="lua_isboolean"><code>lua_isboolean</code></a></h3><p> 4155*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 4156*088332b5SXin Li<pre>int lua_isboolean (lua_State *L, int index);</pre> 4157*088332b5SXin Li 4158*088332b5SXin Li<p> 4159*088332b5SXin LiReturns 1 if the value at the given index is a boolean, 4160*088332b5SXin Liand 0 otherwise. 4161*088332b5SXin Li 4162*088332b5SXin Li 4163*088332b5SXin Li 4164*088332b5SXin Li 4165*088332b5SXin Li 4166*088332b5SXin Li<hr><h3><a name="lua_iscfunction"><code>lua_iscfunction</code></a></h3><p> 4167*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 4168*088332b5SXin Li<pre>int lua_iscfunction (lua_State *L, int index);</pre> 4169*088332b5SXin Li 4170*088332b5SXin Li<p> 4171*088332b5SXin LiReturns 1 if the value at the given index is a C function, 4172*088332b5SXin Liand 0 otherwise. 4173*088332b5SXin Li 4174*088332b5SXin Li 4175*088332b5SXin Li 4176*088332b5SXin Li 4177*088332b5SXin Li 4178*088332b5SXin Li<hr><h3><a name="lua_isfunction"><code>lua_isfunction</code></a></h3><p> 4179*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 4180*088332b5SXin Li<pre>int lua_isfunction (lua_State *L, int index);</pre> 4181*088332b5SXin Li 4182*088332b5SXin Li<p> 4183*088332b5SXin LiReturns 1 if the value at the given index is a function 4184*088332b5SXin Li(either C or Lua), and 0 otherwise. 4185*088332b5SXin Li 4186*088332b5SXin Li 4187*088332b5SXin Li 4188*088332b5SXin Li 4189*088332b5SXin Li 4190*088332b5SXin Li<hr><h3><a name="lua_isinteger"><code>lua_isinteger</code></a></h3><p> 4191*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 4192*088332b5SXin Li<pre>int lua_isinteger (lua_State *L, int index);</pre> 4193*088332b5SXin Li 4194*088332b5SXin Li<p> 4195*088332b5SXin LiReturns 1 if the value at the given index is an integer 4196*088332b5SXin Li(that is, the value is a number and is represented as an integer), 4197*088332b5SXin Liand 0 otherwise. 4198*088332b5SXin Li 4199*088332b5SXin Li 4200*088332b5SXin Li 4201*088332b5SXin Li 4202*088332b5SXin Li 4203*088332b5SXin Li<hr><h3><a name="lua_islightuserdata"><code>lua_islightuserdata</code></a></h3><p> 4204*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 4205*088332b5SXin Li<pre>int lua_islightuserdata (lua_State *L, int index);</pre> 4206*088332b5SXin Li 4207*088332b5SXin Li<p> 4208*088332b5SXin LiReturns 1 if the value at the given index is a light userdata, 4209*088332b5SXin Liand 0 otherwise. 4210*088332b5SXin Li 4211*088332b5SXin Li 4212*088332b5SXin Li 4213*088332b5SXin Li 4214*088332b5SXin Li 4215*088332b5SXin Li<hr><h3><a name="lua_isnil"><code>lua_isnil</code></a></h3><p> 4216*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 4217*088332b5SXin Li<pre>int lua_isnil (lua_State *L, int index);</pre> 4218*088332b5SXin Li 4219*088332b5SXin Li<p> 4220*088332b5SXin LiReturns 1 if the value at the given index is <b>nil</b>, 4221*088332b5SXin Liand 0 otherwise. 4222*088332b5SXin Li 4223*088332b5SXin Li 4224*088332b5SXin Li 4225*088332b5SXin Li 4226*088332b5SXin Li 4227*088332b5SXin Li<hr><h3><a name="lua_isnone"><code>lua_isnone</code></a></h3><p> 4228*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 4229*088332b5SXin Li<pre>int lua_isnone (lua_State *L, int index);</pre> 4230*088332b5SXin Li 4231*088332b5SXin Li<p> 4232*088332b5SXin LiReturns 1 if the given index is not valid, 4233*088332b5SXin Liand 0 otherwise. 4234*088332b5SXin Li 4235*088332b5SXin Li 4236*088332b5SXin Li 4237*088332b5SXin Li 4238*088332b5SXin Li 4239*088332b5SXin Li<hr><h3><a name="lua_isnoneornil"><code>lua_isnoneornil</code></a></h3><p> 4240*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 4241*088332b5SXin Li<pre>int lua_isnoneornil (lua_State *L, int index);</pre> 4242*088332b5SXin Li 4243*088332b5SXin Li<p> 4244*088332b5SXin LiReturns 1 if the given index is not valid 4245*088332b5SXin Lior if the value at this index is <b>nil</b>, 4246*088332b5SXin Liand 0 otherwise. 4247*088332b5SXin Li 4248*088332b5SXin Li 4249*088332b5SXin Li 4250*088332b5SXin Li 4251*088332b5SXin Li 4252*088332b5SXin Li<hr><h3><a name="lua_isnumber"><code>lua_isnumber</code></a></h3><p> 4253*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 4254*088332b5SXin Li<pre>int lua_isnumber (lua_State *L, int index);</pre> 4255*088332b5SXin Li 4256*088332b5SXin Li<p> 4257*088332b5SXin LiReturns 1 if the value at the given index is a number 4258*088332b5SXin Lior a string convertible to a number, 4259*088332b5SXin Liand 0 otherwise. 4260*088332b5SXin Li 4261*088332b5SXin Li 4262*088332b5SXin Li 4263*088332b5SXin Li 4264*088332b5SXin Li 4265*088332b5SXin Li<hr><h3><a name="lua_isstring"><code>lua_isstring</code></a></h3><p> 4266*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 4267*088332b5SXin Li<pre>int lua_isstring (lua_State *L, int index);</pre> 4268*088332b5SXin Li 4269*088332b5SXin Li<p> 4270*088332b5SXin LiReturns 1 if the value at the given index is a string 4271*088332b5SXin Lior a number (which is always convertible to a string), 4272*088332b5SXin Liand 0 otherwise. 4273*088332b5SXin Li 4274*088332b5SXin Li 4275*088332b5SXin Li 4276*088332b5SXin Li 4277*088332b5SXin Li 4278*088332b5SXin Li<hr><h3><a name="lua_istable"><code>lua_istable</code></a></h3><p> 4279*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 4280*088332b5SXin Li<pre>int lua_istable (lua_State *L, int index);</pre> 4281*088332b5SXin Li 4282*088332b5SXin Li<p> 4283*088332b5SXin LiReturns 1 if the value at the given index is a table, 4284*088332b5SXin Liand 0 otherwise. 4285*088332b5SXin Li 4286*088332b5SXin Li 4287*088332b5SXin Li 4288*088332b5SXin Li 4289*088332b5SXin Li 4290*088332b5SXin Li<hr><h3><a name="lua_isthread"><code>lua_isthread</code></a></h3><p> 4291*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 4292*088332b5SXin Li<pre>int lua_isthread (lua_State *L, int index);</pre> 4293*088332b5SXin Li 4294*088332b5SXin Li<p> 4295*088332b5SXin LiReturns 1 if the value at the given index is a thread, 4296*088332b5SXin Liand 0 otherwise. 4297*088332b5SXin Li 4298*088332b5SXin Li 4299*088332b5SXin Li 4300*088332b5SXin Li 4301*088332b5SXin Li 4302*088332b5SXin Li<hr><h3><a name="lua_isuserdata"><code>lua_isuserdata</code></a></h3><p> 4303*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 4304*088332b5SXin Li<pre>int lua_isuserdata (lua_State *L, int index);</pre> 4305*088332b5SXin Li 4306*088332b5SXin Li<p> 4307*088332b5SXin LiReturns 1 if the value at the given index is a userdata 4308*088332b5SXin Li(either full or light), and 0 otherwise. 4309*088332b5SXin Li 4310*088332b5SXin Li 4311*088332b5SXin Li 4312*088332b5SXin Li 4313*088332b5SXin Li 4314*088332b5SXin Li<hr><h3><a name="lua_isyieldable"><code>lua_isyieldable</code></a></h3><p> 4315*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 4316*088332b5SXin Li<pre>int lua_isyieldable (lua_State *L);</pre> 4317*088332b5SXin Li 4318*088332b5SXin Li<p> 4319*088332b5SXin LiReturns 1 if the given coroutine can yield, 4320*088332b5SXin Liand 0 otherwise. 4321*088332b5SXin Li 4322*088332b5SXin Li 4323*088332b5SXin Li 4324*088332b5SXin Li 4325*088332b5SXin Li 4326*088332b5SXin Li<hr><h3><a name="lua_KContext"><code>lua_KContext</code></a></h3> 4327*088332b5SXin Li<pre>typedef ... lua_KContext;</pre> 4328*088332b5SXin Li 4329*088332b5SXin Li<p> 4330*088332b5SXin LiThe type for continuation-function contexts. 4331*088332b5SXin LiIt must be a numeric type. 4332*088332b5SXin LiThis type is defined as <code>intptr_t</code> 4333*088332b5SXin Liwhen <code>intptr_t</code> is available, 4334*088332b5SXin Liso that it can store pointers too. 4335*088332b5SXin LiOtherwise, it is defined as <code>ptrdiff_t</code>. 4336*088332b5SXin Li 4337*088332b5SXin Li 4338*088332b5SXin Li 4339*088332b5SXin Li 4340*088332b5SXin Li 4341*088332b5SXin Li<hr><h3><a name="lua_KFunction"><code>lua_KFunction</code></a></h3> 4342*088332b5SXin Li<pre>typedef int (*lua_KFunction) (lua_State *L, int status, lua_KContext ctx);</pre> 4343*088332b5SXin Li 4344*088332b5SXin Li<p> 4345*088332b5SXin LiType for continuation functions (see <a href="#4.5">§4.5</a>). 4346*088332b5SXin Li 4347*088332b5SXin Li 4348*088332b5SXin Li 4349*088332b5SXin Li 4350*088332b5SXin Li 4351*088332b5SXin Li<hr><h3><a name="lua_len"><code>lua_len</code></a></h3><p> 4352*088332b5SXin Li<span class="apii">[-0, +1, <em>e</em>]</span> 4353*088332b5SXin Li<pre>void lua_len (lua_State *L, int index);</pre> 4354*088332b5SXin Li 4355*088332b5SXin Li<p> 4356*088332b5SXin LiReturns the length of the value at the given index. 4357*088332b5SXin LiIt is equivalent to the '<code>#</code>' operator in Lua (see <a href="#3.4.7">§3.4.7</a>) and 4358*088332b5SXin Limay trigger a metamethod for the "length" event (see <a href="#2.4">§2.4</a>). 4359*088332b5SXin LiThe result is pushed on the stack. 4360*088332b5SXin Li 4361*088332b5SXin Li 4362*088332b5SXin Li 4363*088332b5SXin Li 4364*088332b5SXin Li 4365*088332b5SXin Li<hr><h3><a name="lua_load"><code>lua_load</code></a></h3><p> 4366*088332b5SXin Li<span class="apii">[-0, +1, –]</span> 4367*088332b5SXin Li<pre>int lua_load (lua_State *L, 4368*088332b5SXin Li lua_Reader reader, 4369*088332b5SXin Li void *data, 4370*088332b5SXin Li const char *chunkname, 4371*088332b5SXin Li const char *mode);</pre> 4372*088332b5SXin Li 4373*088332b5SXin Li<p> 4374*088332b5SXin LiLoads a Lua chunk without running it. 4375*088332b5SXin LiIf there are no errors, 4376*088332b5SXin Li<code>lua_load</code> pushes the compiled chunk as a Lua 4377*088332b5SXin Lifunction on top of the stack. 4378*088332b5SXin LiOtherwise, it pushes an error message. 4379*088332b5SXin Li 4380*088332b5SXin Li 4381*088332b5SXin Li<p> 4382*088332b5SXin LiThe <code>lua_load</code> function uses a user-supplied <code>reader</code> function 4383*088332b5SXin Lito read the chunk (see <a href="#lua_Reader"><code>lua_Reader</code></a>). 4384*088332b5SXin LiThe <code>data</code> argument is an opaque value passed to the reader function. 4385*088332b5SXin Li 4386*088332b5SXin Li 4387*088332b5SXin Li<p> 4388*088332b5SXin LiThe <code>chunkname</code> argument gives a name to the chunk, 4389*088332b5SXin Liwhich is used for error messages and in debug information (see <a href="#4.7">§4.7</a>). 4390*088332b5SXin Li 4391*088332b5SXin Li 4392*088332b5SXin Li<p> 4393*088332b5SXin Li<code>lua_load</code> automatically detects whether the chunk is text or binary 4394*088332b5SXin Liand loads it accordingly (see program <code>luac</code>). 4395*088332b5SXin LiThe string <code>mode</code> works as in function <a href="#pdf-load"><code>load</code></a>, 4396*088332b5SXin Liwith the addition that 4397*088332b5SXin Lia <code>NULL</code> value is equivalent to the string "<code>bt</code>". 4398*088332b5SXin Li 4399*088332b5SXin Li 4400*088332b5SXin Li<p> 4401*088332b5SXin Li<code>lua_load</code> uses the stack internally, 4402*088332b5SXin Liso the reader function must always leave the stack 4403*088332b5SXin Liunmodified when returning. 4404*088332b5SXin Li 4405*088332b5SXin Li 4406*088332b5SXin Li<p> 4407*088332b5SXin Li<code>lua_load</code> can return 4408*088332b5SXin Li<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>. 4409*088332b5SXin LiThe function may also return other values corresponding to 4410*088332b5SXin Lierrors raised by the read function (see <a href="#4.4.1">§4.4.1</a>). 4411*088332b5SXin Li 4412*088332b5SXin Li 4413*088332b5SXin Li<p> 4414*088332b5SXin LiIf the resulting function has upvalues, 4415*088332b5SXin Liits first upvalue is set to the value of the global environment 4416*088332b5SXin Listored at index <code>LUA_RIDX_GLOBALS</code> in the registry (see <a href="#4.3">§4.3</a>). 4417*088332b5SXin LiWhen loading main chunks, 4418*088332b5SXin Lithis upvalue will be the <code>_ENV</code> variable (see <a href="#2.2">§2.2</a>). 4419*088332b5SXin LiOther upvalues are initialized with <b>nil</b>. 4420*088332b5SXin Li 4421*088332b5SXin Li 4422*088332b5SXin Li 4423*088332b5SXin Li 4424*088332b5SXin Li 4425*088332b5SXin Li<hr><h3><a name="lua_newstate"><code>lua_newstate</code></a></h3><p> 4426*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 4427*088332b5SXin Li<pre>lua_State *lua_newstate (lua_Alloc f, void *ud);</pre> 4428*088332b5SXin Li 4429*088332b5SXin Li<p> 4430*088332b5SXin LiCreates a new independent state and returns its main thread. 4431*088332b5SXin LiReturns <code>NULL</code> if it cannot create the state 4432*088332b5SXin Li(due to lack of memory). 4433*088332b5SXin LiThe argument <code>f</code> is the allocator function; 4434*088332b5SXin LiLua will do all memory allocation for this state 4435*088332b5SXin Lithrough this function (see <a href="#lua_Alloc"><code>lua_Alloc</code></a>). 4436*088332b5SXin LiThe second argument, <code>ud</code>, is an opaque pointer that Lua 4437*088332b5SXin Lipasses to the allocator in every call. 4438*088332b5SXin Li 4439*088332b5SXin Li 4440*088332b5SXin Li 4441*088332b5SXin Li 4442*088332b5SXin Li 4443*088332b5SXin Li<hr><h3><a name="lua_newtable"><code>lua_newtable</code></a></h3><p> 4444*088332b5SXin Li<span class="apii">[-0, +1, <em>m</em>]</span> 4445*088332b5SXin Li<pre>void lua_newtable (lua_State *L);</pre> 4446*088332b5SXin Li 4447*088332b5SXin Li<p> 4448*088332b5SXin LiCreates a new empty table and pushes it onto the stack. 4449*088332b5SXin LiIt is equivalent to <code>lua_createtable(L, 0, 0)</code>. 4450*088332b5SXin Li 4451*088332b5SXin Li 4452*088332b5SXin Li 4453*088332b5SXin Li 4454*088332b5SXin Li 4455*088332b5SXin Li<hr><h3><a name="lua_newthread"><code>lua_newthread</code></a></h3><p> 4456*088332b5SXin Li<span class="apii">[-0, +1, <em>m</em>]</span> 4457*088332b5SXin Li<pre>lua_State *lua_newthread (lua_State *L);</pre> 4458*088332b5SXin Li 4459*088332b5SXin Li<p> 4460*088332b5SXin LiCreates a new thread, pushes it on the stack, 4461*088332b5SXin Liand returns a pointer to a <a href="#lua_State"><code>lua_State</code></a> that represents this new thread. 4462*088332b5SXin LiThe new thread returned by this function shares with the original thread 4463*088332b5SXin Liits global environment, 4464*088332b5SXin Libut has an independent execution stack. 4465*088332b5SXin Li 4466*088332b5SXin Li 4467*088332b5SXin Li<p> 4468*088332b5SXin LiThreads are subject to garbage collection, 4469*088332b5SXin Lilike any Lua object. 4470*088332b5SXin Li 4471*088332b5SXin Li 4472*088332b5SXin Li 4473*088332b5SXin Li 4474*088332b5SXin Li 4475*088332b5SXin Li<hr><h3><a name="lua_newuserdatauv"><code>lua_newuserdatauv</code></a></h3><p> 4476*088332b5SXin Li<span class="apii">[-0, +1, <em>m</em>]</span> 4477*088332b5SXin Li<pre>void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue);</pre> 4478*088332b5SXin Li 4479*088332b5SXin Li<p> 4480*088332b5SXin LiThis function creates and pushes on the stack a new full userdata, 4481*088332b5SXin Liwith <code>nuvalue</code> associated Lua values, called <code>user values</code>, 4482*088332b5SXin Liplus an associated block of raw memory with <code>size</code> bytes. 4483*088332b5SXin Li(The user values can be set and read with the functions 4484*088332b5SXin Li<a href="#lua_setiuservalue"><code>lua_setiuservalue</code></a> and <a href="#lua_getiuservalue"><code>lua_getiuservalue</code></a>.) 4485*088332b5SXin Li 4486*088332b5SXin Li 4487*088332b5SXin Li<p> 4488*088332b5SXin LiThe function returns the address of the block of memory. 4489*088332b5SXin LiLua ensures that this address is valid as long as 4490*088332b5SXin Lithe corresponding userdata is alive (see <a href="#2.5">§2.5</a>). 4491*088332b5SXin LiMoreover, if the userdata is marked for finalization (see <a href="#2.5.3">§2.5.3</a>), 4492*088332b5SXin Liits address is valid at least until the call to its finalizer. 4493*088332b5SXin Li 4494*088332b5SXin Li 4495*088332b5SXin Li 4496*088332b5SXin Li 4497*088332b5SXin Li 4498*088332b5SXin Li<hr><h3><a name="lua_next"><code>lua_next</code></a></h3><p> 4499*088332b5SXin Li<span class="apii">[-1, +(2|0), <em>v</em>]</span> 4500*088332b5SXin Li<pre>int lua_next (lua_State *L, int index);</pre> 4501*088332b5SXin Li 4502*088332b5SXin Li<p> 4503*088332b5SXin LiPops a key from the stack, 4504*088332b5SXin Liand pushes a key–value pair from the table at the given index, 4505*088332b5SXin Lithe "next" pair after the given key. 4506*088332b5SXin LiIf there are no more elements in the table, 4507*088332b5SXin Lithen <a href="#lua_next"><code>lua_next</code></a> returns 0 and pushes nothing. 4508*088332b5SXin Li 4509*088332b5SXin Li 4510*088332b5SXin Li<p> 4511*088332b5SXin LiA typical table traversal looks like this: 4512*088332b5SXin Li 4513*088332b5SXin Li<pre> 4514*088332b5SXin Li /* table is in the stack at index 't' */ 4515*088332b5SXin Li lua_pushnil(L); /* first key */ 4516*088332b5SXin Li while (lua_next(L, t) != 0) { 4517*088332b5SXin Li /* uses 'key' (at index -2) and 'value' (at index -1) */ 4518*088332b5SXin Li printf("%s - %s\n", 4519*088332b5SXin Li lua_typename(L, lua_type(L, -2)), 4520*088332b5SXin Li lua_typename(L, lua_type(L, -1))); 4521*088332b5SXin Li /* removes 'value'; keeps 'key' for next iteration */ 4522*088332b5SXin Li lua_pop(L, 1); 4523*088332b5SXin Li } 4524*088332b5SXin Li</pre> 4525*088332b5SXin Li 4526*088332b5SXin Li<p> 4527*088332b5SXin LiWhile traversing a table, 4528*088332b5SXin Liavoid calling <a href="#lua_tolstring"><code>lua_tolstring</code></a> directly on a key, 4529*088332b5SXin Liunless you know that the key is actually a string. 4530*088332b5SXin LiRecall that <a href="#lua_tolstring"><code>lua_tolstring</code></a> may change 4531*088332b5SXin Lithe value at the given index; 4532*088332b5SXin Lithis confuses the next call to <a href="#lua_next"><code>lua_next</code></a>. 4533*088332b5SXin Li 4534*088332b5SXin Li 4535*088332b5SXin Li<p> 4536*088332b5SXin LiThis function may raise an error if the given key 4537*088332b5SXin Liis neither <b>nil</b> nor present in the table. 4538*088332b5SXin LiSee function <a href="#pdf-next"><code>next</code></a> for the caveats of modifying 4539*088332b5SXin Lithe table during its traversal. 4540*088332b5SXin Li 4541*088332b5SXin Li 4542*088332b5SXin Li 4543*088332b5SXin Li 4544*088332b5SXin Li 4545*088332b5SXin Li<hr><h3><a name="lua_Number"><code>lua_Number</code></a></h3> 4546*088332b5SXin Li<pre>typedef ... lua_Number;</pre> 4547*088332b5SXin Li 4548*088332b5SXin Li<p> 4549*088332b5SXin LiThe type of floats in Lua. 4550*088332b5SXin Li 4551*088332b5SXin Li 4552*088332b5SXin Li<p> 4553*088332b5SXin LiBy default this type is double, 4554*088332b5SXin Libut that can be changed to a single float or a long double. 4555*088332b5SXin Li(See <code>LUA_FLOAT_TYPE</code> in <code>luaconf.h</code>.) 4556*088332b5SXin Li 4557*088332b5SXin Li 4558*088332b5SXin Li 4559*088332b5SXin Li 4560*088332b5SXin Li 4561*088332b5SXin Li<hr><h3><a name="lua_numbertointeger"><code>lua_numbertointeger</code></a></h3> 4562*088332b5SXin Li<pre>int lua_numbertointeger (lua_Number n, lua_Integer *p);</pre> 4563*088332b5SXin Li 4564*088332b5SXin Li<p> 4565*088332b5SXin LiTries to convert a Lua float to a Lua integer; 4566*088332b5SXin Lithe float <code>n</code> must have an integral value. 4567*088332b5SXin LiIf that value is within the range of Lua integers, 4568*088332b5SXin Liit is converted to an integer and assigned to <code>*p</code>. 4569*088332b5SXin LiThe macro results in a boolean indicating whether the 4570*088332b5SXin Liconversion was successful. 4571*088332b5SXin Li(Note that this range test can be tricky to do 4572*088332b5SXin Licorrectly without this macro, due to rounding.) 4573*088332b5SXin Li 4574*088332b5SXin Li 4575*088332b5SXin Li<p> 4576*088332b5SXin LiThis macro may evaluate its arguments more than once. 4577*088332b5SXin Li 4578*088332b5SXin Li 4579*088332b5SXin Li 4580*088332b5SXin Li 4581*088332b5SXin Li 4582*088332b5SXin Li<hr><h3><a name="lua_pcall"><code>lua_pcall</code></a></h3><p> 4583*088332b5SXin Li<span class="apii">[-(nargs + 1), +(nresults|1), –]</span> 4584*088332b5SXin Li<pre>int lua_pcall (lua_State *L, int nargs, int nresults, int msgh);</pre> 4585*088332b5SXin Li 4586*088332b5SXin Li<p> 4587*088332b5SXin LiCalls a function (or a callable object) in protected mode. 4588*088332b5SXin Li 4589*088332b5SXin Li 4590*088332b5SXin Li<p> 4591*088332b5SXin LiBoth <code>nargs</code> and <code>nresults</code> have the same meaning as 4592*088332b5SXin Liin <a href="#lua_call"><code>lua_call</code></a>. 4593*088332b5SXin LiIf there are no errors during the call, 4594*088332b5SXin Li<a href="#lua_pcall"><code>lua_pcall</code></a> behaves exactly like <a href="#lua_call"><code>lua_call</code></a>. 4595*088332b5SXin LiHowever, if there is any error, 4596*088332b5SXin Li<a href="#lua_pcall"><code>lua_pcall</code></a> catches it, 4597*088332b5SXin Lipushes a single value on the stack (the error object), 4598*088332b5SXin Liand returns an error code. 4599*088332b5SXin LiLike <a href="#lua_call"><code>lua_call</code></a>, 4600*088332b5SXin Li<a href="#lua_pcall"><code>lua_pcall</code></a> always removes the function 4601*088332b5SXin Liand its arguments from the stack. 4602*088332b5SXin Li 4603*088332b5SXin Li 4604*088332b5SXin Li<p> 4605*088332b5SXin LiIf <code>msgh</code> is 0, 4606*088332b5SXin Lithen the error object returned on the stack 4607*088332b5SXin Liis exactly the original error object. 4608*088332b5SXin LiOtherwise, <code>msgh</code> is the stack index of a 4609*088332b5SXin Li<em>message handler</em>. 4610*088332b5SXin Li(This index cannot be a pseudo-index.) 4611*088332b5SXin LiIn case of runtime errors, 4612*088332b5SXin Lithis handler will be called with the error object 4613*088332b5SXin Liand its return value will be the object 4614*088332b5SXin Lireturned on the stack by <a href="#lua_pcall"><code>lua_pcall</code></a>. 4615*088332b5SXin Li 4616*088332b5SXin Li 4617*088332b5SXin Li<p> 4618*088332b5SXin LiTypically, the message handler is used to add more debug 4619*088332b5SXin Liinformation to the error object, such as a stack traceback. 4620*088332b5SXin LiSuch information cannot be gathered after the return of <a href="#lua_pcall"><code>lua_pcall</code></a>, 4621*088332b5SXin Lisince by then the stack has unwound. 4622*088332b5SXin Li 4623*088332b5SXin Li 4624*088332b5SXin Li<p> 4625*088332b5SXin LiThe <a href="#lua_pcall"><code>lua_pcall</code></a> function returns one of the following status codes: 4626*088332b5SXin Li<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>. 4627*088332b5SXin Li 4628*088332b5SXin Li 4629*088332b5SXin Li 4630*088332b5SXin Li 4631*088332b5SXin Li 4632*088332b5SXin Li<hr><h3><a name="lua_pcallk"><code>lua_pcallk</code></a></h3><p> 4633*088332b5SXin Li<span class="apii">[-(nargs + 1), +(nresults|1), –]</span> 4634*088332b5SXin Li<pre>int lua_pcallk (lua_State *L, 4635*088332b5SXin Li int nargs, 4636*088332b5SXin Li int nresults, 4637*088332b5SXin Li int msgh, 4638*088332b5SXin Li lua_KContext ctx, 4639*088332b5SXin Li lua_KFunction k);</pre> 4640*088332b5SXin Li 4641*088332b5SXin Li<p> 4642*088332b5SXin LiThis function behaves exactly like <a href="#lua_pcall"><code>lua_pcall</code></a>, 4643*088332b5SXin Liexcept that it allows the called function to yield (see <a href="#4.5">§4.5</a>). 4644*088332b5SXin Li 4645*088332b5SXin Li 4646*088332b5SXin Li 4647*088332b5SXin Li 4648*088332b5SXin Li 4649*088332b5SXin Li<hr><h3><a name="lua_pop"><code>lua_pop</code></a></h3><p> 4650*088332b5SXin Li<span class="apii">[-n, +0, –]</span> 4651*088332b5SXin Li<pre>void lua_pop (lua_State *L, int n);</pre> 4652*088332b5SXin Li 4653*088332b5SXin Li<p> 4654*088332b5SXin LiPops <code>n</code> elements from the stack. 4655*088332b5SXin Li 4656*088332b5SXin Li 4657*088332b5SXin Li 4658*088332b5SXin Li 4659*088332b5SXin Li 4660*088332b5SXin Li<hr><h3><a name="lua_pushboolean"><code>lua_pushboolean</code></a></h3><p> 4661*088332b5SXin Li<span class="apii">[-0, +1, –]</span> 4662*088332b5SXin Li<pre>void lua_pushboolean (lua_State *L, int b);</pre> 4663*088332b5SXin Li 4664*088332b5SXin Li<p> 4665*088332b5SXin LiPushes a boolean value with value <code>b</code> onto the stack. 4666*088332b5SXin Li 4667*088332b5SXin Li 4668*088332b5SXin Li 4669*088332b5SXin Li 4670*088332b5SXin Li 4671*088332b5SXin Li<hr><h3><a name="lua_pushcclosure"><code>lua_pushcclosure</code></a></h3><p> 4672*088332b5SXin Li<span class="apii">[-n, +1, <em>m</em>]</span> 4673*088332b5SXin Li<pre>void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);</pre> 4674*088332b5SXin Li 4675*088332b5SXin Li<p> 4676*088332b5SXin LiPushes a new C closure onto the stack. 4677*088332b5SXin LiThis function receives a pointer to a C function 4678*088332b5SXin Liand pushes onto the stack a Lua value of type <code>function</code> that, 4679*088332b5SXin Liwhen called, invokes the corresponding C function. 4680*088332b5SXin LiThe parameter <code>n</code> tells how many upvalues this function will have 4681*088332b5SXin Li(see <a href="#4.2">§4.2</a>). 4682*088332b5SXin Li 4683*088332b5SXin Li 4684*088332b5SXin Li<p> 4685*088332b5SXin LiAny function to be callable by Lua must 4686*088332b5SXin Lifollow the correct protocol to receive its parameters 4687*088332b5SXin Liand return its results (see <a href="#lua_CFunction"><code>lua_CFunction</code></a>). 4688*088332b5SXin Li 4689*088332b5SXin Li 4690*088332b5SXin Li<p> 4691*088332b5SXin LiWhen a C function is created, 4692*088332b5SXin Liit is possible to associate some values with it, 4693*088332b5SXin Lithe so called upvalues; 4694*088332b5SXin Lithese upvalues are then accessible to the function whenever it is called. 4695*088332b5SXin LiThis association is called a C closure (see <a href="#4.2">§4.2</a>). 4696*088332b5SXin LiTo create a C closure, 4697*088332b5SXin Lifirst the initial values for its upvalues must be pushed onto the stack. 4698*088332b5SXin Li(When there are multiple upvalues, the first value is pushed first.) 4699*088332b5SXin LiThen <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a> 4700*088332b5SXin Liis called to create and push the C function onto the stack, 4701*088332b5SXin Liwith the argument <code>n</code> telling how many values will be 4702*088332b5SXin Liassociated with the function. 4703*088332b5SXin Li<a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a> also pops these values from the stack. 4704*088332b5SXin Li 4705*088332b5SXin Li 4706*088332b5SXin Li<p> 4707*088332b5SXin LiThe maximum value for <code>n</code> is 255. 4708*088332b5SXin Li 4709*088332b5SXin Li 4710*088332b5SXin Li<p> 4711*088332b5SXin LiWhen <code>n</code> is zero, 4712*088332b5SXin Lithis function creates a <em>light C function</em>, 4713*088332b5SXin Liwhich is just a pointer to the C function. 4714*088332b5SXin LiIn that case, it never raises a memory error. 4715*088332b5SXin Li 4716*088332b5SXin Li 4717*088332b5SXin Li 4718*088332b5SXin Li 4719*088332b5SXin Li 4720*088332b5SXin Li<hr><h3><a name="lua_pushcfunction"><code>lua_pushcfunction</code></a></h3><p> 4721*088332b5SXin Li<span class="apii">[-0, +1, –]</span> 4722*088332b5SXin Li<pre>void lua_pushcfunction (lua_State *L, lua_CFunction f);</pre> 4723*088332b5SXin Li 4724*088332b5SXin Li<p> 4725*088332b5SXin LiPushes a C function onto the stack. 4726*088332b5SXin LiThis function is equivalent to <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a> with no upvalues. 4727*088332b5SXin Li 4728*088332b5SXin Li 4729*088332b5SXin Li 4730*088332b5SXin Li 4731*088332b5SXin Li 4732*088332b5SXin Li<hr><h3><a name="lua_pushfstring"><code>lua_pushfstring</code></a></h3><p> 4733*088332b5SXin Li<span class="apii">[-0, +1, <em>v</em>]</span> 4734*088332b5SXin Li<pre>const char *lua_pushfstring (lua_State *L, const char *fmt, ...);</pre> 4735*088332b5SXin Li 4736*088332b5SXin Li<p> 4737*088332b5SXin LiPushes onto the stack a formatted string 4738*088332b5SXin Liand returns a pointer to this string (see <a href="#4.1.3">§4.1.3</a>). 4739*088332b5SXin LiIt is similar to the ISO C function <code>sprintf</code>, 4740*088332b5SXin Libut has two important differences. 4741*088332b5SXin LiFirst, 4742*088332b5SXin Liyou do not have to allocate space for the result; 4743*088332b5SXin Lithe result is a Lua string and Lua takes care of memory allocation 4744*088332b5SXin Li(and deallocation, through garbage collection). 4745*088332b5SXin LiSecond, 4746*088332b5SXin Lithe conversion specifiers are quite restricted. 4747*088332b5SXin LiThere are no flags, widths, or precisions. 4748*088332b5SXin LiThe conversion specifiers can only be 4749*088332b5SXin Li'<code>%%</code>' (inserts the character '<code>%</code>'), 4750*088332b5SXin Li'<code>%s</code>' (inserts a zero-terminated string, with no size restrictions), 4751*088332b5SXin Li'<code>%f</code>' (inserts a <a href="#lua_Number"><code>lua_Number</code></a>), 4752*088332b5SXin Li'<code>%I</code>' (inserts a <a href="#lua_Integer"><code>lua_Integer</code></a>), 4753*088332b5SXin Li'<code>%p</code>' (inserts a pointer), 4754*088332b5SXin Li'<code>%d</code>' (inserts an <code>int</code>), 4755*088332b5SXin Li'<code>%c</code>' (inserts an <code>int</code> as a one-byte character), and 4756*088332b5SXin Li'<code>%U</code>' (inserts a <code>long int</code> as a UTF-8 byte sequence). 4757*088332b5SXin Li 4758*088332b5SXin Li 4759*088332b5SXin Li<p> 4760*088332b5SXin LiThis function may raise errors due to memory overflow 4761*088332b5SXin Lior an invalid conversion specifier. 4762*088332b5SXin Li 4763*088332b5SXin Li 4764*088332b5SXin Li 4765*088332b5SXin Li 4766*088332b5SXin Li 4767*088332b5SXin Li<hr><h3><a name="lua_pushglobaltable"><code>lua_pushglobaltable</code></a></h3><p> 4768*088332b5SXin Li<span class="apii">[-0, +1, –]</span> 4769*088332b5SXin Li<pre>void lua_pushglobaltable (lua_State *L);</pre> 4770*088332b5SXin Li 4771*088332b5SXin Li<p> 4772*088332b5SXin LiPushes the global environment onto the stack. 4773*088332b5SXin Li 4774*088332b5SXin Li 4775*088332b5SXin Li 4776*088332b5SXin Li 4777*088332b5SXin Li 4778*088332b5SXin Li<hr><h3><a name="lua_pushinteger"><code>lua_pushinteger</code></a></h3><p> 4779*088332b5SXin Li<span class="apii">[-0, +1, –]</span> 4780*088332b5SXin Li<pre>void lua_pushinteger (lua_State *L, lua_Integer n);</pre> 4781*088332b5SXin Li 4782*088332b5SXin Li<p> 4783*088332b5SXin LiPushes an integer with value <code>n</code> onto the stack. 4784*088332b5SXin Li 4785*088332b5SXin Li 4786*088332b5SXin Li 4787*088332b5SXin Li 4788*088332b5SXin Li 4789*088332b5SXin Li<hr><h3><a name="lua_pushlightuserdata"><code>lua_pushlightuserdata</code></a></h3><p> 4790*088332b5SXin Li<span class="apii">[-0, +1, –]</span> 4791*088332b5SXin Li<pre>void lua_pushlightuserdata (lua_State *L, void *p);</pre> 4792*088332b5SXin Li 4793*088332b5SXin Li<p> 4794*088332b5SXin LiPushes a light userdata onto the stack. 4795*088332b5SXin Li 4796*088332b5SXin Li 4797*088332b5SXin Li<p> 4798*088332b5SXin LiUserdata represent C values in Lua. 4799*088332b5SXin LiA <em>light userdata</em> represents a pointer, a <code>void*</code>. 4800*088332b5SXin LiIt is a value (like a number): 4801*088332b5SXin Liyou do not create it, it has no individual metatable, 4802*088332b5SXin Liand it is not collected (as it was never created). 4803*088332b5SXin LiA light userdata is equal to "any" 4804*088332b5SXin Lilight userdata with the same C address. 4805*088332b5SXin Li 4806*088332b5SXin Li 4807*088332b5SXin Li 4808*088332b5SXin Li 4809*088332b5SXin Li 4810*088332b5SXin Li<hr><h3><a name="lua_pushliteral"><code>lua_pushliteral</code></a></h3><p> 4811*088332b5SXin Li<span class="apii">[-0, +1, <em>m</em>]</span> 4812*088332b5SXin Li<pre>const char *lua_pushliteral (lua_State *L, const char *s);</pre> 4813*088332b5SXin Li 4814*088332b5SXin Li<p> 4815*088332b5SXin LiThis macro is equivalent to <a href="#lua_pushstring"><code>lua_pushstring</code></a>, 4816*088332b5SXin Libut should be used only when <code>s</code> is a literal string. 4817*088332b5SXin Li(Lua may optimize this case.) 4818*088332b5SXin Li 4819*088332b5SXin Li 4820*088332b5SXin Li 4821*088332b5SXin Li 4822*088332b5SXin Li 4823*088332b5SXin Li<hr><h3><a name="lua_pushlstring"><code>lua_pushlstring</code></a></h3><p> 4824*088332b5SXin Li<span class="apii">[-0, +1, <em>m</em>]</span> 4825*088332b5SXin Li<pre>const char *lua_pushlstring (lua_State *L, const char *s, size_t len);</pre> 4826*088332b5SXin Li 4827*088332b5SXin Li<p> 4828*088332b5SXin LiPushes the string pointed to by <code>s</code> with size <code>len</code> 4829*088332b5SXin Lionto the stack. 4830*088332b5SXin LiLua will make or reuse an internal copy of the given string, 4831*088332b5SXin Liso the memory at <code>s</code> can be freed or reused immediately after 4832*088332b5SXin Lithe function returns. 4833*088332b5SXin LiThe string can contain any binary data, 4834*088332b5SXin Liincluding embedded zeros. 4835*088332b5SXin Li 4836*088332b5SXin Li 4837*088332b5SXin Li<p> 4838*088332b5SXin LiReturns a pointer to the internal copy of the string (see <a href="#4.1.3">§4.1.3</a>). 4839*088332b5SXin Li 4840*088332b5SXin Li 4841*088332b5SXin Li 4842*088332b5SXin Li 4843*088332b5SXin Li 4844*088332b5SXin Li<hr><h3><a name="lua_pushnil"><code>lua_pushnil</code></a></h3><p> 4845*088332b5SXin Li<span class="apii">[-0, +1, –]</span> 4846*088332b5SXin Li<pre>void lua_pushnil (lua_State *L);</pre> 4847*088332b5SXin Li 4848*088332b5SXin Li<p> 4849*088332b5SXin LiPushes a nil value onto the stack. 4850*088332b5SXin Li 4851*088332b5SXin Li 4852*088332b5SXin Li 4853*088332b5SXin Li 4854*088332b5SXin Li 4855*088332b5SXin Li<hr><h3><a name="lua_pushnumber"><code>lua_pushnumber</code></a></h3><p> 4856*088332b5SXin Li<span class="apii">[-0, +1, –]</span> 4857*088332b5SXin Li<pre>void lua_pushnumber (lua_State *L, lua_Number n);</pre> 4858*088332b5SXin Li 4859*088332b5SXin Li<p> 4860*088332b5SXin LiPushes a float with value <code>n</code> onto the stack. 4861*088332b5SXin Li 4862*088332b5SXin Li 4863*088332b5SXin Li 4864*088332b5SXin Li 4865*088332b5SXin Li 4866*088332b5SXin Li<hr><h3><a name="lua_pushstring"><code>lua_pushstring</code></a></h3><p> 4867*088332b5SXin Li<span class="apii">[-0, +1, <em>m</em>]</span> 4868*088332b5SXin Li<pre>const char *lua_pushstring (lua_State *L, const char *s);</pre> 4869*088332b5SXin Li 4870*088332b5SXin Li<p> 4871*088332b5SXin LiPushes the zero-terminated string pointed to by <code>s</code> 4872*088332b5SXin Lionto the stack. 4873*088332b5SXin LiLua will make or reuse an internal copy of the given string, 4874*088332b5SXin Liso the memory at <code>s</code> can be freed or reused immediately after 4875*088332b5SXin Lithe function returns. 4876*088332b5SXin Li 4877*088332b5SXin Li 4878*088332b5SXin Li<p> 4879*088332b5SXin LiReturns a pointer to the internal copy of the string (see <a href="#4.1.3">§4.1.3</a>). 4880*088332b5SXin Li 4881*088332b5SXin Li 4882*088332b5SXin Li<p> 4883*088332b5SXin LiIf <code>s</code> is <code>NULL</code>, pushes <b>nil</b> and returns <code>NULL</code>. 4884*088332b5SXin Li 4885*088332b5SXin Li 4886*088332b5SXin Li 4887*088332b5SXin Li 4888*088332b5SXin Li 4889*088332b5SXin Li<hr><h3><a name="lua_pushthread"><code>lua_pushthread</code></a></h3><p> 4890*088332b5SXin Li<span class="apii">[-0, +1, –]</span> 4891*088332b5SXin Li<pre>int lua_pushthread (lua_State *L);</pre> 4892*088332b5SXin Li 4893*088332b5SXin Li<p> 4894*088332b5SXin LiPushes the thread represented by <code>L</code> onto the stack. 4895*088332b5SXin LiReturns 1 if this thread is the main thread of its state. 4896*088332b5SXin Li 4897*088332b5SXin Li 4898*088332b5SXin Li 4899*088332b5SXin Li 4900*088332b5SXin Li 4901*088332b5SXin Li<hr><h3><a name="lua_pushvalue"><code>lua_pushvalue</code></a></h3><p> 4902*088332b5SXin Li<span class="apii">[-0, +1, –]</span> 4903*088332b5SXin Li<pre>void lua_pushvalue (lua_State *L, int index);</pre> 4904*088332b5SXin Li 4905*088332b5SXin Li<p> 4906*088332b5SXin LiPushes a copy of the element at the given index 4907*088332b5SXin Lionto the stack. 4908*088332b5SXin Li 4909*088332b5SXin Li 4910*088332b5SXin Li 4911*088332b5SXin Li 4912*088332b5SXin Li 4913*088332b5SXin Li<hr><h3><a name="lua_pushvfstring"><code>lua_pushvfstring</code></a></h3><p> 4914*088332b5SXin Li<span class="apii">[-0, +1, <em>v</em>]</span> 4915*088332b5SXin Li<pre>const char *lua_pushvfstring (lua_State *L, 4916*088332b5SXin Li const char *fmt, 4917*088332b5SXin Li va_list argp);</pre> 4918*088332b5SXin Li 4919*088332b5SXin Li<p> 4920*088332b5SXin LiEquivalent to <a href="#lua_pushfstring"><code>lua_pushfstring</code></a>, except that it receives a <code>va_list</code> 4921*088332b5SXin Liinstead of a variable number of arguments. 4922*088332b5SXin Li 4923*088332b5SXin Li 4924*088332b5SXin Li 4925*088332b5SXin Li 4926*088332b5SXin Li 4927*088332b5SXin Li<hr><h3><a name="lua_rawequal"><code>lua_rawequal</code></a></h3><p> 4928*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 4929*088332b5SXin Li<pre>int lua_rawequal (lua_State *L, int index1, int index2);</pre> 4930*088332b5SXin Li 4931*088332b5SXin Li<p> 4932*088332b5SXin LiReturns 1 if the two values in indices <code>index1</code> and 4933*088332b5SXin Li<code>index2</code> are primitively equal 4934*088332b5SXin Li(that is, equal without calling the <code>__eq</code> metamethod). 4935*088332b5SXin LiOtherwise returns 0. 4936*088332b5SXin LiAlso returns 0 if any of the indices are not valid. 4937*088332b5SXin Li 4938*088332b5SXin Li 4939*088332b5SXin Li 4940*088332b5SXin Li 4941*088332b5SXin Li 4942*088332b5SXin Li<hr><h3><a name="lua_rawget"><code>lua_rawget</code></a></h3><p> 4943*088332b5SXin Li<span class="apii">[-1, +1, –]</span> 4944*088332b5SXin Li<pre>int lua_rawget (lua_State *L, int index);</pre> 4945*088332b5SXin Li 4946*088332b5SXin Li<p> 4947*088332b5SXin LiSimilar to <a href="#lua_gettable"><code>lua_gettable</code></a>, but does a raw access 4948*088332b5SXin Li(i.e., without metamethods). 4949*088332b5SXin Li 4950*088332b5SXin Li 4951*088332b5SXin Li 4952*088332b5SXin Li 4953*088332b5SXin Li 4954*088332b5SXin Li<hr><h3><a name="lua_rawgeti"><code>lua_rawgeti</code></a></h3><p> 4955*088332b5SXin Li<span class="apii">[-0, +1, –]</span> 4956*088332b5SXin Li<pre>int lua_rawgeti (lua_State *L, int index, lua_Integer n);</pre> 4957*088332b5SXin Li 4958*088332b5SXin Li<p> 4959*088332b5SXin LiPushes onto the stack the value <code>t[n]</code>, 4960*088332b5SXin Liwhere <code>t</code> is the table at the given index. 4961*088332b5SXin LiThe access is raw, 4962*088332b5SXin Lithat is, it does not use the <code>__index</code> metavalue. 4963*088332b5SXin Li 4964*088332b5SXin Li 4965*088332b5SXin Li<p> 4966*088332b5SXin LiReturns the type of the pushed value. 4967*088332b5SXin Li 4968*088332b5SXin Li 4969*088332b5SXin Li 4970*088332b5SXin Li 4971*088332b5SXin Li 4972*088332b5SXin Li<hr><h3><a name="lua_rawgetp"><code>lua_rawgetp</code></a></h3><p> 4973*088332b5SXin Li<span class="apii">[-0, +1, –]</span> 4974*088332b5SXin Li<pre>int lua_rawgetp (lua_State *L, int index, const void *p);</pre> 4975*088332b5SXin Li 4976*088332b5SXin Li<p> 4977*088332b5SXin LiPushes onto the stack the value <code>t[k]</code>, 4978*088332b5SXin Liwhere <code>t</code> is the table at the given index and 4979*088332b5SXin Li<code>k</code> is the pointer <code>p</code> represented as a light userdata. 4980*088332b5SXin LiThe access is raw; 4981*088332b5SXin Lithat is, it does not use the <code>__index</code> metavalue. 4982*088332b5SXin Li 4983*088332b5SXin Li 4984*088332b5SXin Li<p> 4985*088332b5SXin LiReturns the type of the pushed value. 4986*088332b5SXin Li 4987*088332b5SXin Li 4988*088332b5SXin Li 4989*088332b5SXin Li 4990*088332b5SXin Li 4991*088332b5SXin Li<hr><h3><a name="lua_rawlen"><code>lua_rawlen</code></a></h3><p> 4992*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 4993*088332b5SXin Li<pre>lua_Unsigned lua_rawlen (lua_State *L, int index);</pre> 4994*088332b5SXin Li 4995*088332b5SXin Li<p> 4996*088332b5SXin LiReturns the raw "length" of the value at the given index: 4997*088332b5SXin Lifor strings, this is the string length; 4998*088332b5SXin Lifor tables, this is the result of the length operator ('<code>#</code>') 4999*088332b5SXin Liwith no metamethods; 5000*088332b5SXin Lifor userdata, this is the size of the block of memory allocated 5001*088332b5SXin Lifor the userdata. 5002*088332b5SXin LiFor other values, this call returns 0. 5003*088332b5SXin Li 5004*088332b5SXin Li 5005*088332b5SXin Li 5006*088332b5SXin Li 5007*088332b5SXin Li 5008*088332b5SXin Li<hr><h3><a name="lua_rawset"><code>lua_rawset</code></a></h3><p> 5009*088332b5SXin Li<span class="apii">[-2, +0, <em>m</em>]</span> 5010*088332b5SXin Li<pre>void lua_rawset (lua_State *L, int index);</pre> 5011*088332b5SXin Li 5012*088332b5SXin Li<p> 5013*088332b5SXin LiSimilar to <a href="#lua_settable"><code>lua_settable</code></a>, but does a raw assignment 5014*088332b5SXin Li(i.e., without metamethods). 5015*088332b5SXin Li 5016*088332b5SXin Li 5017*088332b5SXin Li 5018*088332b5SXin Li 5019*088332b5SXin Li 5020*088332b5SXin Li<hr><h3><a name="lua_rawseti"><code>lua_rawseti</code></a></h3><p> 5021*088332b5SXin Li<span class="apii">[-1, +0, <em>m</em>]</span> 5022*088332b5SXin Li<pre>void lua_rawseti (lua_State *L, int index, lua_Integer i);</pre> 5023*088332b5SXin Li 5024*088332b5SXin Li<p> 5025*088332b5SXin LiDoes the equivalent of <code>t[i] = v</code>, 5026*088332b5SXin Liwhere <code>t</code> is the table at the given index 5027*088332b5SXin Liand <code>v</code> is the value on the top of the stack. 5028*088332b5SXin Li 5029*088332b5SXin Li 5030*088332b5SXin Li<p> 5031*088332b5SXin LiThis function pops the value from the stack. 5032*088332b5SXin LiThe assignment is raw, 5033*088332b5SXin Lithat is, it does not use the <code>__newindex</code> metavalue. 5034*088332b5SXin Li 5035*088332b5SXin Li 5036*088332b5SXin Li 5037*088332b5SXin Li 5038*088332b5SXin Li 5039*088332b5SXin Li<hr><h3><a name="lua_rawsetp"><code>lua_rawsetp</code></a></h3><p> 5040*088332b5SXin Li<span class="apii">[-1, +0, <em>m</em>]</span> 5041*088332b5SXin Li<pre>void lua_rawsetp (lua_State *L, int index, const void *p);</pre> 5042*088332b5SXin Li 5043*088332b5SXin Li<p> 5044*088332b5SXin LiDoes the equivalent of <code>t[p] = v</code>, 5045*088332b5SXin Liwhere <code>t</code> is the table at the given index, 5046*088332b5SXin Li<code>p</code> is encoded as a light userdata, 5047*088332b5SXin Liand <code>v</code> is the value on the top of the stack. 5048*088332b5SXin Li 5049*088332b5SXin Li 5050*088332b5SXin Li<p> 5051*088332b5SXin LiThis function pops the value from the stack. 5052*088332b5SXin LiThe assignment is raw, 5053*088332b5SXin Lithat is, it does not use the <code>__newindex</code> metavalue. 5054*088332b5SXin Li 5055*088332b5SXin Li 5056*088332b5SXin Li 5057*088332b5SXin Li 5058*088332b5SXin Li 5059*088332b5SXin Li<hr><h3><a name="lua_Reader"><code>lua_Reader</code></a></h3> 5060*088332b5SXin Li<pre>typedef const char * (*lua_Reader) (lua_State *L, 5061*088332b5SXin Li void *data, 5062*088332b5SXin Li size_t *size);</pre> 5063*088332b5SXin Li 5064*088332b5SXin Li<p> 5065*088332b5SXin LiThe reader function used by <a href="#lua_load"><code>lua_load</code></a>. 5066*088332b5SXin LiEvery time <a href="#lua_load"><code>lua_load</code></a> needs another piece of the chunk, 5067*088332b5SXin Liit calls the reader, 5068*088332b5SXin Lipassing along its <code>data</code> parameter. 5069*088332b5SXin LiThe reader must return a pointer to a block of memory 5070*088332b5SXin Liwith a new piece of the chunk 5071*088332b5SXin Liand set <code>size</code> to the block size. 5072*088332b5SXin LiThe block must exist until the reader function is called again. 5073*088332b5SXin LiTo signal the end of the chunk, 5074*088332b5SXin Lithe reader must return <code>NULL</code> or set <code>size</code> to zero. 5075*088332b5SXin LiThe reader function may return pieces of any size greater than zero. 5076*088332b5SXin Li 5077*088332b5SXin Li 5078*088332b5SXin Li 5079*088332b5SXin Li 5080*088332b5SXin Li 5081*088332b5SXin Li<hr><h3><a name="lua_register"><code>lua_register</code></a></h3><p> 5082*088332b5SXin Li<span class="apii">[-0, +0, <em>e</em>]</span> 5083*088332b5SXin Li<pre>void lua_register (lua_State *L, const char *name, lua_CFunction f);</pre> 5084*088332b5SXin Li 5085*088332b5SXin Li<p> 5086*088332b5SXin LiSets the C function <code>f</code> as the new value of global <code>name</code>. 5087*088332b5SXin LiIt is defined as a macro: 5088*088332b5SXin Li 5089*088332b5SXin Li<pre> 5090*088332b5SXin Li #define lua_register(L,n,f) \ 5091*088332b5SXin Li (lua_pushcfunction(L, f), lua_setglobal(L, n)) 5092*088332b5SXin Li</pre> 5093*088332b5SXin Li 5094*088332b5SXin Li 5095*088332b5SXin Li 5096*088332b5SXin Li 5097*088332b5SXin Li<hr><h3><a name="lua_remove"><code>lua_remove</code></a></h3><p> 5098*088332b5SXin Li<span class="apii">[-1, +0, –]</span> 5099*088332b5SXin Li<pre>void lua_remove (lua_State *L, int index);</pre> 5100*088332b5SXin Li 5101*088332b5SXin Li<p> 5102*088332b5SXin LiRemoves the element at the given valid index, 5103*088332b5SXin Lishifting down the elements above this index to fill the gap. 5104*088332b5SXin LiThis function cannot be called with a pseudo-index, 5105*088332b5SXin Libecause a pseudo-index is not an actual stack position. 5106*088332b5SXin Li 5107*088332b5SXin Li 5108*088332b5SXin Li 5109*088332b5SXin Li 5110*088332b5SXin Li 5111*088332b5SXin Li<hr><h3><a name="lua_replace"><code>lua_replace</code></a></h3><p> 5112*088332b5SXin Li<span class="apii">[-1, +0, –]</span> 5113*088332b5SXin Li<pre>void lua_replace (lua_State *L, int index);</pre> 5114*088332b5SXin Li 5115*088332b5SXin Li<p> 5116*088332b5SXin LiMoves the top element into the given valid index 5117*088332b5SXin Liwithout shifting any element 5118*088332b5SXin Li(therefore replacing the value at that given index), 5119*088332b5SXin Liand then pops the top element. 5120*088332b5SXin Li 5121*088332b5SXin Li 5122*088332b5SXin Li 5123*088332b5SXin Li 5124*088332b5SXin Li 5125*088332b5SXin Li<hr><h3><a name="lua_resetthread"><code>lua_resetthread</code></a></h3><p> 5126*088332b5SXin Li<span class="apii">[-0, +?, –]</span> 5127*088332b5SXin Li<pre>int lua_resetthread (lua_State *L);</pre> 5128*088332b5SXin Li 5129*088332b5SXin Li<p> 5130*088332b5SXin LiResets a thread, cleaning its call stack and closing all pending 5131*088332b5SXin Lito-be-closed variables. 5132*088332b5SXin LiReturns a status code: 5133*088332b5SXin Li<a href="#pdf-LUA_OK"><code>LUA_OK</code></a> for no errors in closing methods, 5134*088332b5SXin Lior an error status otherwise. 5135*088332b5SXin LiIn case of error, 5136*088332b5SXin Lileaves the error object on the top of the stack, 5137*088332b5SXin Li 5138*088332b5SXin Li 5139*088332b5SXin Li 5140*088332b5SXin Li 5141*088332b5SXin Li 5142*088332b5SXin Li<hr><h3><a name="lua_resume"><code>lua_resume</code></a></h3><p> 5143*088332b5SXin Li<span class="apii">[-?, +?, –]</span> 5144*088332b5SXin Li<pre>int lua_resume (lua_State *L, lua_State *from, int nargs, 5145*088332b5SXin Li int *nresults);</pre> 5146*088332b5SXin Li 5147*088332b5SXin Li<p> 5148*088332b5SXin LiStarts and resumes a coroutine in the given thread <code>L</code>. 5149*088332b5SXin Li 5150*088332b5SXin Li 5151*088332b5SXin Li<p> 5152*088332b5SXin LiTo start a coroutine, 5153*088332b5SXin Liyou push the main function plus any arguments 5154*088332b5SXin Lionto the empty stack of the thread. 5155*088332b5SXin Lithen you call <a href="#lua_resume"><code>lua_resume</code></a>, 5156*088332b5SXin Liwith <code>nargs</code> being the number of arguments. 5157*088332b5SXin LiThis call returns when the coroutine suspends or finishes its execution. 5158*088332b5SXin LiWhen it returns, 5159*088332b5SXin Li<code>*nresults</code> is updated and 5160*088332b5SXin Lithe top of the stack contains 5161*088332b5SXin Lithe <code>*nresults</code> values passed to <a href="#lua_yield"><code>lua_yield</code></a> 5162*088332b5SXin Lior returned by the body function. 5163*088332b5SXin Li<a href="#lua_resume"><code>lua_resume</code></a> returns 5164*088332b5SXin Li<a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> if the coroutine yields, 5165*088332b5SXin Li<a href="#pdf-LUA_OK"><code>LUA_OK</code></a> if the coroutine finishes its execution 5166*088332b5SXin Liwithout errors, 5167*088332b5SXin Lior an error code in case of errors (see <a href="#4.4.1">§4.4.1</a>). 5168*088332b5SXin LiIn case of errors, 5169*088332b5SXin Lithe error object is on the top of the stack. 5170*088332b5SXin Li 5171*088332b5SXin Li 5172*088332b5SXin Li<p> 5173*088332b5SXin LiTo resume a coroutine, 5174*088332b5SXin Liyou remove the <code>*nresults</code> yielded values from its stack, 5175*088332b5SXin Lipush the values to be passed as results from <code>yield</code>, 5176*088332b5SXin Liand then call <a href="#lua_resume"><code>lua_resume</code></a>. 5177*088332b5SXin Li 5178*088332b5SXin Li 5179*088332b5SXin Li<p> 5180*088332b5SXin LiThe parameter <code>from</code> represents the coroutine that is resuming <code>L</code>. 5181*088332b5SXin LiIf there is no such coroutine, 5182*088332b5SXin Lithis parameter can be <code>NULL</code>. 5183*088332b5SXin Li 5184*088332b5SXin Li 5185*088332b5SXin Li 5186*088332b5SXin Li 5187*088332b5SXin Li 5188*088332b5SXin Li<hr><h3><a name="lua_rotate"><code>lua_rotate</code></a></h3><p> 5189*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 5190*088332b5SXin Li<pre>void lua_rotate (lua_State *L, int idx, int n);</pre> 5191*088332b5SXin Li 5192*088332b5SXin Li<p> 5193*088332b5SXin LiRotates the stack elements between the valid index <code>idx</code> 5194*088332b5SXin Liand the top of the stack. 5195*088332b5SXin LiThe elements are rotated <code>n</code> positions in the direction of the top, 5196*088332b5SXin Lifor a positive <code>n</code>, 5197*088332b5SXin Lior <code>-n</code> positions in the direction of the bottom, 5198*088332b5SXin Lifor a negative <code>n</code>. 5199*088332b5SXin LiThe absolute value of <code>n</code> must not be greater than the size 5200*088332b5SXin Liof the slice being rotated. 5201*088332b5SXin LiThis function cannot be called with a pseudo-index, 5202*088332b5SXin Libecause a pseudo-index is not an actual stack position. 5203*088332b5SXin Li 5204*088332b5SXin Li 5205*088332b5SXin Li 5206*088332b5SXin Li 5207*088332b5SXin Li 5208*088332b5SXin Li<hr><h3><a name="lua_setallocf"><code>lua_setallocf</code></a></h3><p> 5209*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 5210*088332b5SXin Li<pre>void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);</pre> 5211*088332b5SXin Li 5212*088332b5SXin Li<p> 5213*088332b5SXin LiChanges the allocator function of a given state to <code>f</code> 5214*088332b5SXin Liwith user data <code>ud</code>. 5215*088332b5SXin Li 5216*088332b5SXin Li 5217*088332b5SXin Li 5218*088332b5SXin Li 5219*088332b5SXin Li 5220*088332b5SXin Li<hr><h3><a name="lua_setfield"><code>lua_setfield</code></a></h3><p> 5221*088332b5SXin Li<span class="apii">[-1, +0, <em>e</em>]</span> 5222*088332b5SXin Li<pre>void lua_setfield (lua_State *L, int index, const char *k);</pre> 5223*088332b5SXin Li 5224*088332b5SXin Li<p> 5225*088332b5SXin LiDoes the equivalent to <code>t[k] = v</code>, 5226*088332b5SXin Liwhere <code>t</code> is the value at the given index 5227*088332b5SXin Liand <code>v</code> is the value on the top of the stack. 5228*088332b5SXin Li 5229*088332b5SXin Li 5230*088332b5SXin Li<p> 5231*088332b5SXin LiThis function pops the value from the stack. 5232*088332b5SXin LiAs in Lua, this function may trigger a metamethod 5233*088332b5SXin Lifor the "newindex" event (see <a href="#2.4">§2.4</a>). 5234*088332b5SXin Li 5235*088332b5SXin Li 5236*088332b5SXin Li 5237*088332b5SXin Li 5238*088332b5SXin Li 5239*088332b5SXin Li<hr><h3><a name="lua_setglobal"><code>lua_setglobal</code></a></h3><p> 5240*088332b5SXin Li<span class="apii">[-1, +0, <em>e</em>]</span> 5241*088332b5SXin Li<pre>void lua_setglobal (lua_State *L, const char *name);</pre> 5242*088332b5SXin Li 5243*088332b5SXin Li<p> 5244*088332b5SXin LiPops a value from the stack and 5245*088332b5SXin Lisets it as the new value of global <code>name</code>. 5246*088332b5SXin Li 5247*088332b5SXin Li 5248*088332b5SXin Li 5249*088332b5SXin Li 5250*088332b5SXin Li 5251*088332b5SXin Li<hr><h3><a name="lua_seti"><code>lua_seti</code></a></h3><p> 5252*088332b5SXin Li<span class="apii">[-1, +0, <em>e</em>]</span> 5253*088332b5SXin Li<pre>void lua_seti (lua_State *L, int index, lua_Integer n);</pre> 5254*088332b5SXin Li 5255*088332b5SXin Li<p> 5256*088332b5SXin LiDoes the equivalent to <code>t[n] = v</code>, 5257*088332b5SXin Liwhere <code>t</code> is the value at the given index 5258*088332b5SXin Liand <code>v</code> is the value on the top of the stack. 5259*088332b5SXin Li 5260*088332b5SXin Li 5261*088332b5SXin Li<p> 5262*088332b5SXin LiThis function pops the value from the stack. 5263*088332b5SXin LiAs in Lua, this function may trigger a metamethod 5264*088332b5SXin Lifor the "newindex" event (see <a href="#2.4">§2.4</a>). 5265*088332b5SXin Li 5266*088332b5SXin Li 5267*088332b5SXin Li 5268*088332b5SXin Li 5269*088332b5SXin Li 5270*088332b5SXin Li<hr><h3><a name="lua_setiuservalue"><code>lua_setiuservalue</code></a></h3><p> 5271*088332b5SXin Li<span class="apii">[-1, +0, –]</span> 5272*088332b5SXin Li<pre>int lua_setiuservalue (lua_State *L, int index, int n);</pre> 5273*088332b5SXin Li 5274*088332b5SXin Li<p> 5275*088332b5SXin LiPops a value from the stack and sets it as 5276*088332b5SXin Lithe new <code>n</code>-th user value associated to the 5277*088332b5SXin Lifull userdata at the given index. 5278*088332b5SXin LiReturns 0 if the userdata does not have that value. 5279*088332b5SXin Li 5280*088332b5SXin Li 5281*088332b5SXin Li 5282*088332b5SXin Li 5283*088332b5SXin Li 5284*088332b5SXin Li<hr><h3><a name="lua_setmetatable"><code>lua_setmetatable</code></a></h3><p> 5285*088332b5SXin Li<span class="apii">[-1, +0, –]</span> 5286*088332b5SXin Li<pre>int lua_setmetatable (lua_State *L, int index);</pre> 5287*088332b5SXin Li 5288*088332b5SXin Li<p> 5289*088332b5SXin LiPops a table or <b>nil</b> from the stack and 5290*088332b5SXin Lisets that value as the new metatable for the value at the given index. 5291*088332b5SXin Li(<b>nil</b> means no metatable.) 5292*088332b5SXin Li 5293*088332b5SXin Li 5294*088332b5SXin Li<p> 5295*088332b5SXin Li(For historical reasons, this function returns an <code>int</code>, 5296*088332b5SXin Liwhich now is always 1.) 5297*088332b5SXin Li 5298*088332b5SXin Li 5299*088332b5SXin Li 5300*088332b5SXin Li 5301*088332b5SXin Li 5302*088332b5SXin Li<hr><h3><a name="lua_settable"><code>lua_settable</code></a></h3><p> 5303*088332b5SXin Li<span class="apii">[-2, +0, <em>e</em>]</span> 5304*088332b5SXin Li<pre>void lua_settable (lua_State *L, int index);</pre> 5305*088332b5SXin Li 5306*088332b5SXin Li<p> 5307*088332b5SXin LiDoes the equivalent to <code>t[k] = v</code>, 5308*088332b5SXin Liwhere <code>t</code> is the value at the given index, 5309*088332b5SXin Li<code>v</code> is the value on the top of the stack, 5310*088332b5SXin Liand <code>k</code> is the value just below the top. 5311*088332b5SXin Li 5312*088332b5SXin Li 5313*088332b5SXin Li<p> 5314*088332b5SXin LiThis function pops both the key and the value from the stack. 5315*088332b5SXin LiAs in Lua, this function may trigger a metamethod 5316*088332b5SXin Lifor the "newindex" event (see <a href="#2.4">§2.4</a>). 5317*088332b5SXin Li 5318*088332b5SXin Li 5319*088332b5SXin Li 5320*088332b5SXin Li 5321*088332b5SXin Li 5322*088332b5SXin Li<hr><h3><a name="lua_settop"><code>lua_settop</code></a></h3><p> 5323*088332b5SXin Li<span class="apii">[-?, +?, –]</span> 5324*088332b5SXin Li<pre>void lua_settop (lua_State *L, int index);</pre> 5325*088332b5SXin Li 5326*088332b5SXin Li<p> 5327*088332b5SXin LiAccepts any index, or 0, 5328*088332b5SXin Liand sets the stack top to this index. 5329*088332b5SXin LiIf the new top is greater than the old one, 5330*088332b5SXin Lithen the new elements are filled with <b>nil</b>. 5331*088332b5SXin LiIf <code>index</code> is 0, then all stack elements are removed. 5332*088332b5SXin Li 5333*088332b5SXin Li 5334*088332b5SXin Li 5335*088332b5SXin Li 5336*088332b5SXin Li 5337*088332b5SXin Li<hr><h3><a name="lua_setwarnf"><code>lua_setwarnf</code></a></h3><p> 5338*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 5339*088332b5SXin Li<pre>void lua_setwarnf (lua_State *L, lua_WarnFunction f, void *ud);</pre> 5340*088332b5SXin Li 5341*088332b5SXin Li<p> 5342*088332b5SXin LiSets the warning function to be used by Lua to emit warnings 5343*088332b5SXin Li(see <a href="#lua_WarnFunction"><code>lua_WarnFunction</code></a>). 5344*088332b5SXin LiThe <code>ud</code> parameter sets the value <code>ud</code> passed to 5345*088332b5SXin Lithe warning function. 5346*088332b5SXin Li 5347*088332b5SXin Li 5348*088332b5SXin Li 5349*088332b5SXin Li 5350*088332b5SXin Li 5351*088332b5SXin Li<hr><h3><a name="lua_State"><code>lua_State</code></a></h3> 5352*088332b5SXin Li<pre>typedef struct lua_State lua_State;</pre> 5353*088332b5SXin Li 5354*088332b5SXin Li<p> 5355*088332b5SXin LiAn opaque structure that points to a thread and indirectly 5356*088332b5SXin Li(through the thread) to the whole state of a Lua interpreter. 5357*088332b5SXin LiThe Lua library is fully reentrant: 5358*088332b5SXin Liit has no global variables. 5359*088332b5SXin LiAll information about a state is accessible through this structure. 5360*088332b5SXin Li 5361*088332b5SXin Li 5362*088332b5SXin Li<p> 5363*088332b5SXin LiA pointer to this structure must be passed as the first argument to 5364*088332b5SXin Lievery function in the library, except to <a href="#lua_newstate"><code>lua_newstate</code></a>, 5365*088332b5SXin Liwhich creates a Lua state from scratch. 5366*088332b5SXin Li 5367*088332b5SXin Li 5368*088332b5SXin Li 5369*088332b5SXin Li 5370*088332b5SXin Li 5371*088332b5SXin Li<hr><h3><a name="lua_status"><code>lua_status</code></a></h3><p> 5372*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 5373*088332b5SXin Li<pre>int lua_status (lua_State *L);</pre> 5374*088332b5SXin Li 5375*088332b5SXin Li<p> 5376*088332b5SXin LiReturns the status of the thread <code>L</code>. 5377*088332b5SXin Li 5378*088332b5SXin Li 5379*088332b5SXin Li<p> 5380*088332b5SXin LiThe status can be <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> for a normal thread, 5381*088332b5SXin Lian error code if the thread finished the execution 5382*088332b5SXin Liof a <a href="#lua_resume"><code>lua_resume</code></a> with an error, 5383*088332b5SXin Lior <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> if the thread is suspended. 5384*088332b5SXin Li 5385*088332b5SXin Li 5386*088332b5SXin Li<p> 5387*088332b5SXin LiYou can call functions only in threads with status <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>. 5388*088332b5SXin LiYou can resume threads with status <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> 5389*088332b5SXin Li(to start a new coroutine) or <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> 5390*088332b5SXin Li(to resume a coroutine). 5391*088332b5SXin Li 5392*088332b5SXin Li 5393*088332b5SXin Li 5394*088332b5SXin Li 5395*088332b5SXin Li 5396*088332b5SXin Li<hr><h3><a name="lua_stringtonumber"><code>lua_stringtonumber</code></a></h3><p> 5397*088332b5SXin Li<span class="apii">[-0, +1, –]</span> 5398*088332b5SXin Li<pre>size_t lua_stringtonumber (lua_State *L, const char *s);</pre> 5399*088332b5SXin Li 5400*088332b5SXin Li<p> 5401*088332b5SXin LiConverts the zero-terminated string <code>s</code> to a number, 5402*088332b5SXin Lipushes that number into the stack, 5403*088332b5SXin Liand returns the total size of the string, 5404*088332b5SXin Lithat is, its length plus one. 5405*088332b5SXin LiThe conversion can result in an integer or a float, 5406*088332b5SXin Liaccording to the lexical conventions of Lua (see <a href="#3.1">§3.1</a>). 5407*088332b5SXin LiThe string may have leading and trailing whitespaces and a sign. 5408*088332b5SXin LiIf the string is not a valid numeral, 5409*088332b5SXin Lireturns 0 and pushes nothing. 5410*088332b5SXin Li(Note that the result can be used as a boolean, 5411*088332b5SXin Litrue if the conversion succeeds.) 5412*088332b5SXin Li 5413*088332b5SXin Li 5414*088332b5SXin Li 5415*088332b5SXin Li 5416*088332b5SXin Li 5417*088332b5SXin Li<hr><h3><a name="lua_toboolean"><code>lua_toboolean</code></a></h3><p> 5418*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 5419*088332b5SXin Li<pre>int lua_toboolean (lua_State *L, int index);</pre> 5420*088332b5SXin Li 5421*088332b5SXin Li<p> 5422*088332b5SXin LiConverts the Lua value at the given index to a C boolean 5423*088332b5SXin Livalue (0 or 1). 5424*088332b5SXin LiLike all tests in Lua, 5425*088332b5SXin Li<a href="#lua_toboolean"><code>lua_toboolean</code></a> returns true for any Lua value 5426*088332b5SXin Lidifferent from <b>false</b> and <b>nil</b>; 5427*088332b5SXin Liotherwise it returns false. 5428*088332b5SXin Li(If you want to accept only actual boolean values, 5429*088332b5SXin Liuse <a href="#lua_isboolean"><code>lua_isboolean</code></a> to test the value's type.) 5430*088332b5SXin Li 5431*088332b5SXin Li 5432*088332b5SXin Li 5433*088332b5SXin Li 5434*088332b5SXin Li 5435*088332b5SXin Li<hr><h3><a name="lua_tocfunction"><code>lua_tocfunction</code></a></h3><p> 5436*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 5437*088332b5SXin Li<pre>lua_CFunction lua_tocfunction (lua_State *L, int index);</pre> 5438*088332b5SXin Li 5439*088332b5SXin Li<p> 5440*088332b5SXin LiConverts a value at the given index to a C function. 5441*088332b5SXin LiThat value must be a C function; 5442*088332b5SXin Liotherwise, returns <code>NULL</code>. 5443*088332b5SXin Li 5444*088332b5SXin Li 5445*088332b5SXin Li 5446*088332b5SXin Li 5447*088332b5SXin Li 5448*088332b5SXin Li<hr><h3><a name="lua_toclose"><code>lua_toclose</code></a></h3><p> 5449*088332b5SXin Li<span class="apii">[-0, +0, <em>m</em>]</span> 5450*088332b5SXin Li<pre>void lua_toclose (lua_State *L, int index);</pre> 5451*088332b5SXin Li 5452*088332b5SXin Li<p> 5453*088332b5SXin LiMarks the given index in the stack as a 5454*088332b5SXin Lito-be-closed "variable" (see <a href="#3.3.8">§3.3.8</a>). 5455*088332b5SXin LiLike a to-be-closed variable in Lua, 5456*088332b5SXin Lithe value at that index in the stack will be closed 5457*088332b5SXin Liwhen it goes out of scope. 5458*088332b5SXin LiHere, in the context of a C function, 5459*088332b5SXin Lito go out of scope means that the running function returns to Lua, 5460*088332b5SXin Lithere is an error, 5461*088332b5SXin Lior the index is removed from the stack through 5462*088332b5SXin Li<a href="#lua_settop"><code>lua_settop</code></a> or <a href="#lua_pop"><code>lua_pop</code></a>. 5463*088332b5SXin LiAn index marked as to-be-closed should not be removed from the stack 5464*088332b5SXin Liby 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>. 5465*088332b5SXin Li 5466*088332b5SXin Li 5467*088332b5SXin Li<p> 5468*088332b5SXin LiThis function should not be called for an index 5469*088332b5SXin Lithat is equal to or below an active to-be-closed index. 5470*088332b5SXin Li 5471*088332b5SXin Li 5472*088332b5SXin Li<p> 5473*088332b5SXin LiIn the case of an out-of-memory error, 5474*088332b5SXin Lithe value in the given index is immediately closed, 5475*088332b5SXin Lias if it was already marked. 5476*088332b5SXin Li 5477*088332b5SXin Li 5478*088332b5SXin Li<p> 5479*088332b5SXin LiNote that, both in case of errors and of a regular return, 5480*088332b5SXin Liby the time the <code>__close</code> metamethod runs, 5481*088332b5SXin Lithe C stack was already unwound, 5482*088332b5SXin Liso that any automatic C variable declared in the calling function 5483*088332b5SXin Liwill be out of scope. 5484*088332b5SXin Li 5485*088332b5SXin Li 5486*088332b5SXin Li 5487*088332b5SXin Li 5488*088332b5SXin Li 5489*088332b5SXin Li<hr><h3><a name="lua_tointeger"><code>lua_tointeger</code></a></h3><p> 5490*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 5491*088332b5SXin Li<pre>lua_Integer lua_tointeger (lua_State *L, int index);</pre> 5492*088332b5SXin Li 5493*088332b5SXin Li<p> 5494*088332b5SXin LiEquivalent to <a href="#lua_tointegerx"><code>lua_tointegerx</code></a> with <code>isnum</code> equal to <code>NULL</code>. 5495*088332b5SXin Li 5496*088332b5SXin Li 5497*088332b5SXin Li 5498*088332b5SXin Li 5499*088332b5SXin Li 5500*088332b5SXin Li<hr><h3><a name="lua_tointegerx"><code>lua_tointegerx</code></a></h3><p> 5501*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 5502*088332b5SXin Li<pre>lua_Integer lua_tointegerx (lua_State *L, int index, int *isnum);</pre> 5503*088332b5SXin Li 5504*088332b5SXin Li<p> 5505*088332b5SXin LiConverts the Lua value at the given index 5506*088332b5SXin Lito the signed integral type <a href="#lua_Integer"><code>lua_Integer</code></a>. 5507*088332b5SXin LiThe Lua value must be an integer, 5508*088332b5SXin Lior a number or string convertible to an integer (see <a href="#3.4.3">§3.4.3</a>); 5509*088332b5SXin Liotherwise, <code>lua_tointegerx</code> returns 0. 5510*088332b5SXin Li 5511*088332b5SXin Li 5512*088332b5SXin Li<p> 5513*088332b5SXin LiIf <code>isnum</code> is not <code>NULL</code>, 5514*088332b5SXin Liits referent is assigned a boolean value that 5515*088332b5SXin Liindicates whether the operation succeeded. 5516*088332b5SXin Li 5517*088332b5SXin Li 5518*088332b5SXin Li 5519*088332b5SXin Li 5520*088332b5SXin Li 5521*088332b5SXin Li<hr><h3><a name="lua_tolstring"><code>lua_tolstring</code></a></h3><p> 5522*088332b5SXin Li<span class="apii">[-0, +0, <em>m</em>]</span> 5523*088332b5SXin Li<pre>const char *lua_tolstring (lua_State *L, int index, size_t *len);</pre> 5524*088332b5SXin Li 5525*088332b5SXin Li<p> 5526*088332b5SXin LiConverts the Lua value at the given index to a C string. 5527*088332b5SXin LiIf <code>len</code> is not <code>NULL</code>, 5528*088332b5SXin Liit sets <code>*len</code> with the string length. 5529*088332b5SXin LiThe Lua value must be a string or a number; 5530*088332b5SXin Liotherwise, the function returns <code>NULL</code>. 5531*088332b5SXin LiIf the value is a number, 5532*088332b5SXin Lithen <code>lua_tolstring</code> also 5533*088332b5SXin Li<em>changes the actual value in the stack to a string</em>. 5534*088332b5SXin Li(This change confuses <a href="#lua_next"><code>lua_next</code></a> 5535*088332b5SXin Liwhen <code>lua_tolstring</code> is applied to keys during a table traversal.) 5536*088332b5SXin Li 5537*088332b5SXin Li 5538*088332b5SXin Li<p> 5539*088332b5SXin Li<code>lua_tolstring</code> returns a pointer 5540*088332b5SXin Lito a string inside the Lua state (see <a href="#4.1.3">§4.1.3</a>). 5541*088332b5SXin LiThis string always has a zero ('<code>\0</code>') 5542*088332b5SXin Liafter its last character (as in C), 5543*088332b5SXin Libut can contain other zeros in its body. 5544*088332b5SXin Li 5545*088332b5SXin Li 5546*088332b5SXin Li 5547*088332b5SXin Li 5548*088332b5SXin Li 5549*088332b5SXin Li<hr><h3><a name="lua_tonumber"><code>lua_tonumber</code></a></h3><p> 5550*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 5551*088332b5SXin Li<pre>lua_Number lua_tonumber (lua_State *L, int index);</pre> 5552*088332b5SXin Li 5553*088332b5SXin Li<p> 5554*088332b5SXin LiEquivalent to <a href="#lua_tonumberx"><code>lua_tonumberx</code></a> with <code>isnum</code> equal to <code>NULL</code>. 5555*088332b5SXin Li 5556*088332b5SXin Li 5557*088332b5SXin Li 5558*088332b5SXin Li 5559*088332b5SXin Li 5560*088332b5SXin Li<hr><h3><a name="lua_tonumberx"><code>lua_tonumberx</code></a></h3><p> 5561*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 5562*088332b5SXin Li<pre>lua_Number lua_tonumberx (lua_State *L, int index, int *isnum);</pre> 5563*088332b5SXin Li 5564*088332b5SXin Li<p> 5565*088332b5SXin LiConverts the Lua value at the given index 5566*088332b5SXin Lito the C type <a href="#lua_Number"><code>lua_Number</code></a> (see <a href="#lua_Number"><code>lua_Number</code></a>). 5567*088332b5SXin LiThe Lua value must be a number or a string convertible to a number 5568*088332b5SXin Li(see <a href="#3.4.3">§3.4.3</a>); 5569*088332b5SXin Liotherwise, <a href="#lua_tonumberx"><code>lua_tonumberx</code></a> returns 0. 5570*088332b5SXin Li 5571*088332b5SXin Li 5572*088332b5SXin Li<p> 5573*088332b5SXin LiIf <code>isnum</code> is not <code>NULL</code>, 5574*088332b5SXin Liits referent is assigned a boolean value that 5575*088332b5SXin Liindicates whether the operation succeeded. 5576*088332b5SXin Li 5577*088332b5SXin Li 5578*088332b5SXin Li 5579*088332b5SXin Li 5580*088332b5SXin Li 5581*088332b5SXin Li<hr><h3><a name="lua_topointer"><code>lua_topointer</code></a></h3><p> 5582*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 5583*088332b5SXin Li<pre>const void *lua_topointer (lua_State *L, int index);</pre> 5584*088332b5SXin Li 5585*088332b5SXin Li<p> 5586*088332b5SXin LiConverts the value at the given index to a generic 5587*088332b5SXin LiC pointer (<code>void*</code>). 5588*088332b5SXin LiThe value can be a userdata, a table, a thread, a string, or a function; 5589*088332b5SXin Liotherwise, <code>lua_topointer</code> returns <code>NULL</code>. 5590*088332b5SXin LiDifferent objects will give different pointers. 5591*088332b5SXin LiThere is no way to convert the pointer back to its original value. 5592*088332b5SXin Li 5593*088332b5SXin Li 5594*088332b5SXin Li<p> 5595*088332b5SXin LiTypically this function is used only for hashing and debug information. 5596*088332b5SXin Li 5597*088332b5SXin Li 5598*088332b5SXin Li 5599*088332b5SXin Li 5600*088332b5SXin Li 5601*088332b5SXin Li<hr><h3><a name="lua_tostring"><code>lua_tostring</code></a></h3><p> 5602*088332b5SXin Li<span class="apii">[-0, +0, <em>m</em>]</span> 5603*088332b5SXin Li<pre>const char *lua_tostring (lua_State *L, int index);</pre> 5604*088332b5SXin Li 5605*088332b5SXin Li<p> 5606*088332b5SXin LiEquivalent to <a href="#lua_tolstring"><code>lua_tolstring</code></a> with <code>len</code> equal to <code>NULL</code>. 5607*088332b5SXin Li 5608*088332b5SXin Li 5609*088332b5SXin Li 5610*088332b5SXin Li 5611*088332b5SXin Li 5612*088332b5SXin Li<hr><h3><a name="lua_tothread"><code>lua_tothread</code></a></h3><p> 5613*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 5614*088332b5SXin Li<pre>lua_State *lua_tothread (lua_State *L, int index);</pre> 5615*088332b5SXin Li 5616*088332b5SXin Li<p> 5617*088332b5SXin LiConverts the value at the given index to a Lua thread 5618*088332b5SXin Li(represented as <code>lua_State*</code>). 5619*088332b5SXin LiThis value must be a thread; 5620*088332b5SXin Liotherwise, the function returns <code>NULL</code>. 5621*088332b5SXin Li 5622*088332b5SXin Li 5623*088332b5SXin Li 5624*088332b5SXin Li 5625*088332b5SXin Li 5626*088332b5SXin Li<hr><h3><a name="lua_touserdata"><code>lua_touserdata</code></a></h3><p> 5627*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 5628*088332b5SXin Li<pre>void *lua_touserdata (lua_State *L, int index);</pre> 5629*088332b5SXin Li 5630*088332b5SXin Li<p> 5631*088332b5SXin LiIf the value at the given index is a full userdata, 5632*088332b5SXin Lireturns its memory-block address. 5633*088332b5SXin LiIf the value is a light userdata, 5634*088332b5SXin Lireturns its value (a pointer). 5635*088332b5SXin LiOtherwise, returns <code>NULL</code>. 5636*088332b5SXin Li 5637*088332b5SXin Li 5638*088332b5SXin Li 5639*088332b5SXin Li 5640*088332b5SXin Li 5641*088332b5SXin Li<hr><h3><a name="lua_type"><code>lua_type</code></a></h3><p> 5642*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 5643*088332b5SXin Li<pre>int lua_type (lua_State *L, int index);</pre> 5644*088332b5SXin Li 5645*088332b5SXin Li<p> 5646*088332b5SXin LiReturns the type of the value in the given valid index, 5647*088332b5SXin Lior <code>LUA_TNONE</code> for a non-valid but acceptable index. 5648*088332b5SXin LiThe types returned by <a href="#lua_type"><code>lua_type</code></a> are coded by the following constants 5649*088332b5SXin Lidefined in <code>lua.h</code>: 5650*088332b5SXin Li<a name="pdf-LUA_TNIL"><code>LUA_TNIL</code></a>, 5651*088332b5SXin Li<a name="pdf-LUA_TNUMBER"><code>LUA_TNUMBER</code></a>, 5652*088332b5SXin Li<a name="pdf-LUA_TBOOLEAN"><code>LUA_TBOOLEAN</code></a>, 5653*088332b5SXin Li<a name="pdf-LUA_TSTRING"><code>LUA_TSTRING</code></a>, 5654*088332b5SXin Li<a name="pdf-LUA_TTABLE"><code>LUA_TTABLE</code></a>, 5655*088332b5SXin Li<a name="pdf-LUA_TFUNCTION"><code>LUA_TFUNCTION</code></a>, 5656*088332b5SXin Li<a name="pdf-LUA_TUSERDATA"><code>LUA_TUSERDATA</code></a>, 5657*088332b5SXin Li<a name="pdf-LUA_TTHREAD"><code>LUA_TTHREAD</code></a>, 5658*088332b5SXin Liand 5659*088332b5SXin Li<a name="pdf-LUA_TLIGHTUSERDATA"><code>LUA_TLIGHTUSERDATA</code></a>. 5660*088332b5SXin Li 5661*088332b5SXin Li 5662*088332b5SXin Li 5663*088332b5SXin Li 5664*088332b5SXin Li 5665*088332b5SXin Li<hr><h3><a name="lua_typename"><code>lua_typename</code></a></h3><p> 5666*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 5667*088332b5SXin Li<pre>const char *lua_typename (lua_State *L, int tp);</pre> 5668*088332b5SXin Li 5669*088332b5SXin Li<p> 5670*088332b5SXin LiReturns the name of the type encoded by the value <code>tp</code>, 5671*088332b5SXin Liwhich must be one the values returned by <a href="#lua_type"><code>lua_type</code></a>. 5672*088332b5SXin Li 5673*088332b5SXin Li 5674*088332b5SXin Li 5675*088332b5SXin Li 5676*088332b5SXin Li 5677*088332b5SXin Li<hr><h3><a name="lua_Unsigned"><code>lua_Unsigned</code></a></h3> 5678*088332b5SXin Li<pre>typedef ... lua_Unsigned;</pre> 5679*088332b5SXin Li 5680*088332b5SXin Li<p> 5681*088332b5SXin LiThe unsigned version of <a href="#lua_Integer"><code>lua_Integer</code></a>. 5682*088332b5SXin Li 5683*088332b5SXin Li 5684*088332b5SXin Li 5685*088332b5SXin Li 5686*088332b5SXin Li 5687*088332b5SXin Li<hr><h3><a name="lua_upvalueindex"><code>lua_upvalueindex</code></a></h3><p> 5688*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 5689*088332b5SXin Li<pre>int lua_upvalueindex (int i);</pre> 5690*088332b5SXin Li 5691*088332b5SXin Li<p> 5692*088332b5SXin LiReturns the pseudo-index that represents the <code>i</code>-th upvalue of 5693*088332b5SXin Lithe running function (see <a href="#4.2">§4.2</a>). 5694*088332b5SXin Li<code>i</code> must be in the range <em>[1,256]</em>. 5695*088332b5SXin Li 5696*088332b5SXin Li 5697*088332b5SXin Li 5698*088332b5SXin Li 5699*088332b5SXin Li 5700*088332b5SXin Li<hr><h3><a name="lua_version"><code>lua_version</code></a></h3><p> 5701*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 5702*088332b5SXin Li<pre>lua_Number lua_version (lua_State *L);</pre> 5703*088332b5SXin Li 5704*088332b5SXin Li<p> 5705*088332b5SXin LiReturns the version number of this core. 5706*088332b5SXin Li 5707*088332b5SXin Li 5708*088332b5SXin Li 5709*088332b5SXin Li 5710*088332b5SXin Li 5711*088332b5SXin Li<hr><h3><a name="lua_WarnFunction"><code>lua_WarnFunction</code></a></h3> 5712*088332b5SXin Li<pre>typedef void (*lua_WarnFunction) (void *ud, const char *msg, int tocont);</pre> 5713*088332b5SXin Li 5714*088332b5SXin Li<p> 5715*088332b5SXin LiThe type of warning functions, called by Lua to emit warnings. 5716*088332b5SXin LiThe first parameter is an opaque pointer 5717*088332b5SXin Liset by <a href="#lua_setwarnf"><code>lua_setwarnf</code></a>. 5718*088332b5SXin LiThe second parameter is the warning message. 5719*088332b5SXin LiThe third parameter is a boolean that 5720*088332b5SXin Liindicates whether the message is 5721*088332b5SXin Lito be continued by the message in the next call. 5722*088332b5SXin Li 5723*088332b5SXin Li 5724*088332b5SXin Li<p> 5725*088332b5SXin LiSee <a href="#pdf-warn"><code>warn</code></a> for more details about warnings. 5726*088332b5SXin Li 5727*088332b5SXin Li 5728*088332b5SXin Li 5729*088332b5SXin Li 5730*088332b5SXin Li 5731*088332b5SXin Li<hr><h3><a name="lua_warning"><code>lua_warning</code></a></h3><p> 5732*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 5733*088332b5SXin Li<pre>void lua_warning (lua_State *L, const char *msg, int tocont);</pre> 5734*088332b5SXin Li 5735*088332b5SXin Li<p> 5736*088332b5SXin LiEmits a warning with the given message. 5737*088332b5SXin LiA message in a call with <code>tocont</code> true should be 5738*088332b5SXin Licontinued in another call to this function. 5739*088332b5SXin Li 5740*088332b5SXin Li 5741*088332b5SXin Li<p> 5742*088332b5SXin LiSee <a href="#pdf-warn"><code>warn</code></a> for more details about warnings. 5743*088332b5SXin Li 5744*088332b5SXin Li 5745*088332b5SXin Li 5746*088332b5SXin Li 5747*088332b5SXin Li 5748*088332b5SXin Li<hr><h3><a name="lua_Writer"><code>lua_Writer</code></a></h3> 5749*088332b5SXin Li<pre>typedef int (*lua_Writer) (lua_State *L, 5750*088332b5SXin Li const void* p, 5751*088332b5SXin Li size_t sz, 5752*088332b5SXin Li void* ud);</pre> 5753*088332b5SXin Li 5754*088332b5SXin Li<p> 5755*088332b5SXin LiThe type of the writer function used by <a href="#lua_dump"><code>lua_dump</code></a>. 5756*088332b5SXin LiEvery time <a href="#lua_dump"><code>lua_dump</code></a> produces another piece of chunk, 5757*088332b5SXin Liit calls the writer, 5758*088332b5SXin Lipassing along the buffer to be written (<code>p</code>), 5759*088332b5SXin Liits size (<code>sz</code>), 5760*088332b5SXin Liand the <code>ud</code> parameter supplied to <a href="#lua_dump"><code>lua_dump</code></a>. 5761*088332b5SXin Li 5762*088332b5SXin Li 5763*088332b5SXin Li<p> 5764*088332b5SXin LiThe writer returns an error code: 5765*088332b5SXin Li0 means no errors; 5766*088332b5SXin Liany other value means an error and stops <a href="#lua_dump"><code>lua_dump</code></a> from 5767*088332b5SXin Licalling the writer again. 5768*088332b5SXin Li 5769*088332b5SXin Li 5770*088332b5SXin Li 5771*088332b5SXin Li 5772*088332b5SXin Li 5773*088332b5SXin Li<hr><h3><a name="lua_xmove"><code>lua_xmove</code></a></h3><p> 5774*088332b5SXin Li<span class="apii">[-?, +?, –]</span> 5775*088332b5SXin Li<pre>void lua_xmove (lua_State *from, lua_State *to, int n);</pre> 5776*088332b5SXin Li 5777*088332b5SXin Li<p> 5778*088332b5SXin LiExchange values between different threads of the same state. 5779*088332b5SXin Li 5780*088332b5SXin Li 5781*088332b5SXin Li<p> 5782*088332b5SXin LiThis function pops <code>n</code> values from the stack <code>from</code>, 5783*088332b5SXin Liand pushes them onto the stack <code>to</code>. 5784*088332b5SXin Li 5785*088332b5SXin Li 5786*088332b5SXin Li 5787*088332b5SXin Li 5788*088332b5SXin Li 5789*088332b5SXin Li<hr><h3><a name="lua_yield"><code>lua_yield</code></a></h3><p> 5790*088332b5SXin Li<span class="apii">[-?, +?, <em>v</em>]</span> 5791*088332b5SXin Li<pre>int lua_yield (lua_State *L, int nresults);</pre> 5792*088332b5SXin Li 5793*088332b5SXin Li<p> 5794*088332b5SXin LiThis function is equivalent to <a href="#lua_yieldk"><code>lua_yieldk</code></a>, 5795*088332b5SXin Libut it has no continuation (see <a href="#4.5">§4.5</a>). 5796*088332b5SXin LiTherefore, when the thread resumes, 5797*088332b5SXin Liit continues the function that called 5798*088332b5SXin Lithe function calling <code>lua_yield</code>. 5799*088332b5SXin LiTo avoid surprises, 5800*088332b5SXin Lithis function should be called only in a tail call. 5801*088332b5SXin Li 5802*088332b5SXin Li 5803*088332b5SXin Li 5804*088332b5SXin Li 5805*088332b5SXin Li 5806*088332b5SXin Li<hr><h3><a name="lua_yieldk"><code>lua_yieldk</code></a></h3><p> 5807*088332b5SXin Li<span class="apii">[-?, +?, <em>v</em>]</span> 5808*088332b5SXin Li<pre>int lua_yieldk (lua_State *L, 5809*088332b5SXin Li int nresults, 5810*088332b5SXin Li lua_KContext ctx, 5811*088332b5SXin Li lua_KFunction k);</pre> 5812*088332b5SXin Li 5813*088332b5SXin Li<p> 5814*088332b5SXin LiYields a coroutine (thread). 5815*088332b5SXin Li 5816*088332b5SXin Li 5817*088332b5SXin Li<p> 5818*088332b5SXin LiWhen a C function calls <a href="#lua_yieldk"><code>lua_yieldk</code></a>, 5819*088332b5SXin Lithe running coroutine suspends its execution, 5820*088332b5SXin Liand the call to <a href="#lua_resume"><code>lua_resume</code></a> that started this coroutine returns. 5821*088332b5SXin LiThe parameter <code>nresults</code> is the number of values from the stack 5822*088332b5SXin Lithat will be passed as results to <a href="#lua_resume"><code>lua_resume</code></a>. 5823*088332b5SXin Li 5824*088332b5SXin Li 5825*088332b5SXin Li<p> 5826*088332b5SXin LiWhen the coroutine is resumed again, 5827*088332b5SXin LiLua calls the given continuation function <code>k</code> to continue 5828*088332b5SXin Lithe execution of the C function that yielded (see <a href="#4.5">§4.5</a>). 5829*088332b5SXin LiThis continuation function receives the same stack 5830*088332b5SXin Lifrom the previous function, 5831*088332b5SXin Liwith the <code>n</code> results removed and 5832*088332b5SXin Lireplaced by the arguments passed to <a href="#lua_resume"><code>lua_resume</code></a>. 5833*088332b5SXin LiMoreover, 5834*088332b5SXin Lithe continuation function receives the value <code>ctx</code> 5835*088332b5SXin Lithat was passed to <a href="#lua_yieldk"><code>lua_yieldk</code></a>. 5836*088332b5SXin Li 5837*088332b5SXin Li 5838*088332b5SXin Li<p> 5839*088332b5SXin LiUsually, this function does not return; 5840*088332b5SXin Liwhen the coroutine eventually resumes, 5841*088332b5SXin Liit continues executing the continuation function. 5842*088332b5SXin LiHowever, there is one special case, 5843*088332b5SXin Liwhich is when this function is called 5844*088332b5SXin Lifrom inside a line or a count hook (see <a href="#4.7">§4.7</a>). 5845*088332b5SXin LiIn that case, <code>lua_yieldk</code> should be called with no continuation 5846*088332b5SXin Li(probably in the form of <a href="#lua_yield"><code>lua_yield</code></a>) and no results, 5847*088332b5SXin Liand the hook should return immediately after the call. 5848*088332b5SXin LiLua will yield and, 5849*088332b5SXin Liwhen the coroutine resumes again, 5850*088332b5SXin Liit will continue the normal execution 5851*088332b5SXin Liof the (Lua) function that triggered the hook. 5852*088332b5SXin Li 5853*088332b5SXin Li 5854*088332b5SXin Li<p> 5855*088332b5SXin LiThis function can raise an error if it is called from a thread 5856*088332b5SXin Liwith a pending C call with no continuation function 5857*088332b5SXin Li(what is called a <em>C-call boundary</em>), 5858*088332b5SXin Lior it is called from a thread that is not running inside a resume 5859*088332b5SXin Li(typically the main thread). 5860*088332b5SXin Li 5861*088332b5SXin Li 5862*088332b5SXin Li 5863*088332b5SXin Li 5864*088332b5SXin Li 5865*088332b5SXin Li 5866*088332b5SXin Li 5867*088332b5SXin Li<h2>4.7 – <a name="4.7">The Debug Interface</a></h2> 5868*088332b5SXin Li 5869*088332b5SXin Li<p> 5870*088332b5SXin LiLua has no built-in debugging facilities. 5871*088332b5SXin LiInstead, it offers a special interface 5872*088332b5SXin Liby means of functions and <em>hooks</em>. 5873*088332b5SXin LiThis interface allows the construction of different 5874*088332b5SXin Likinds of debuggers, profilers, and other tools 5875*088332b5SXin Lithat need "inside information" from the interpreter. 5876*088332b5SXin Li 5877*088332b5SXin Li 5878*088332b5SXin Li 5879*088332b5SXin Li<hr><h3><a name="lua_Debug"><code>lua_Debug</code></a></h3> 5880*088332b5SXin Li<pre>typedef struct lua_Debug { 5881*088332b5SXin Li int event; 5882*088332b5SXin Li const char *name; /* (n) */ 5883*088332b5SXin Li const char *namewhat; /* (n) */ 5884*088332b5SXin Li const char *what; /* (S) */ 5885*088332b5SXin Li const char *source; /* (S) */ 5886*088332b5SXin Li size_t srclen; /* (S) */ 5887*088332b5SXin Li int currentline; /* (l) */ 5888*088332b5SXin Li int linedefined; /* (S) */ 5889*088332b5SXin Li int lastlinedefined; /* (S) */ 5890*088332b5SXin Li unsigned char nups; /* (u) number of upvalues */ 5891*088332b5SXin Li unsigned char nparams; /* (u) number of parameters */ 5892*088332b5SXin Li char isvararg; /* (u) */ 5893*088332b5SXin Li char istailcall; /* (t) */ 5894*088332b5SXin Li unsigned short ftransfer; /* (r) index of first value transferred */ 5895*088332b5SXin Li unsigned short ntransfer; /* (r) number of transferred values */ 5896*088332b5SXin Li char short_src[LUA_IDSIZE]; /* (S) */ 5897*088332b5SXin Li /* private part */ 5898*088332b5SXin Li <em>other fields</em> 5899*088332b5SXin Li} lua_Debug;</pre> 5900*088332b5SXin Li 5901*088332b5SXin Li<p> 5902*088332b5SXin LiA structure used to carry different pieces of 5903*088332b5SXin Liinformation about a function or an activation record. 5904*088332b5SXin Li<a href="#lua_getstack"><code>lua_getstack</code></a> fills only the private part 5905*088332b5SXin Liof this structure, for later use. 5906*088332b5SXin LiTo fill the other fields of <a href="#lua_Debug"><code>lua_Debug</code></a> with useful information, 5907*088332b5SXin Liyou must call <a href="#lua_getinfo"><code>lua_getinfo</code></a>. 5908*088332b5SXin Li 5909*088332b5SXin Li 5910*088332b5SXin Li<p> 5911*088332b5SXin LiThe fields of <a href="#lua_Debug"><code>lua_Debug</code></a> have the following meaning: 5912*088332b5SXin Li 5913*088332b5SXin Li<ul> 5914*088332b5SXin Li 5915*088332b5SXin Li<li><b><code>source</code>: </b> 5916*088332b5SXin Lithe source of the chunk that created the function. 5917*088332b5SXin LiIf <code>source</code> starts with a '<code>@</code>', 5918*088332b5SXin Liit means that the function was defined in a file where 5919*088332b5SXin Lithe file name follows the '<code>@</code>'. 5920*088332b5SXin LiIf <code>source</code> starts with a '<code>=</code>', 5921*088332b5SXin Lithe remainder of its contents describes the source in a user-dependent manner. 5922*088332b5SXin LiOtherwise, 5923*088332b5SXin Lithe function was defined in a string where 5924*088332b5SXin Li<code>source</code> is that string. 5925*088332b5SXin Li</li> 5926*088332b5SXin Li 5927*088332b5SXin Li<li><b><code>srclen</code>: </b> 5928*088332b5SXin LiThe length of the string <code>source</code>. 5929*088332b5SXin Li</li> 5930*088332b5SXin Li 5931*088332b5SXin Li<li><b><code>short_src</code>: </b> 5932*088332b5SXin Lia "printable" version of <code>source</code>, to be used in error messages. 5933*088332b5SXin Li</li> 5934*088332b5SXin Li 5935*088332b5SXin Li<li><b><code>linedefined</code>: </b> 5936*088332b5SXin Lithe line number where the definition of the function starts. 5937*088332b5SXin Li</li> 5938*088332b5SXin Li 5939*088332b5SXin Li<li><b><code>lastlinedefined</code>: </b> 5940*088332b5SXin Lithe line number where the definition of the function ends. 5941*088332b5SXin Li</li> 5942*088332b5SXin Li 5943*088332b5SXin Li<li><b><code>what</code>: </b> 5944*088332b5SXin Lithe string <code>"Lua"</code> if the function is a Lua function, 5945*088332b5SXin Li<code>"C"</code> if it is a C function, 5946*088332b5SXin Li<code>"main"</code> if it is the main part of a chunk. 5947*088332b5SXin Li</li> 5948*088332b5SXin Li 5949*088332b5SXin Li<li><b><code>currentline</code>: </b> 5950*088332b5SXin Lithe current line where the given function is executing. 5951*088332b5SXin LiWhen no line information is available, 5952*088332b5SXin Li<code>currentline</code> is set to -1. 5953*088332b5SXin Li</li> 5954*088332b5SXin Li 5955*088332b5SXin Li<li><b><code>name</code>: </b> 5956*088332b5SXin Lia reasonable name for the given function. 5957*088332b5SXin LiBecause functions in Lua are first-class values, 5958*088332b5SXin Lithey do not have a fixed name: 5959*088332b5SXin Lisome functions can be the value of multiple global variables, 5960*088332b5SXin Liwhile others can be stored only in a table field. 5961*088332b5SXin LiThe <code>lua_getinfo</code> function checks how the function was 5962*088332b5SXin Licalled to find a suitable name. 5963*088332b5SXin LiIf it cannot find a name, 5964*088332b5SXin Lithen <code>name</code> is set to <code>NULL</code>. 5965*088332b5SXin Li</li> 5966*088332b5SXin Li 5967*088332b5SXin Li<li><b><code>namewhat</code>: </b> 5968*088332b5SXin Liexplains the <code>name</code> field. 5969*088332b5SXin LiThe value of <code>namewhat</code> can be 5970*088332b5SXin Li<code>"global"</code>, <code>"local"</code>, <code>"method"</code>, 5971*088332b5SXin Li<code>"field"</code>, <code>"upvalue"</code>, or <code>""</code> (the empty string), 5972*088332b5SXin Liaccording to how the function was called. 5973*088332b5SXin Li(Lua uses the empty string when no other option seems to apply.) 5974*088332b5SXin Li</li> 5975*088332b5SXin Li 5976*088332b5SXin Li<li><b><code>istailcall</code>: </b> 5977*088332b5SXin Litrue if this function invocation was called by a tail call. 5978*088332b5SXin LiIn this case, the caller of this level is not in the stack. 5979*088332b5SXin Li</li> 5980*088332b5SXin Li 5981*088332b5SXin Li<li><b><code>nups</code>: </b> 5982*088332b5SXin Lithe number of upvalues of the function. 5983*088332b5SXin Li</li> 5984*088332b5SXin Li 5985*088332b5SXin Li<li><b><code>nparams</code>: </b> 5986*088332b5SXin Lithe number of parameters of the function 5987*088332b5SXin Li(always 0 for C functions). 5988*088332b5SXin Li</li> 5989*088332b5SXin Li 5990*088332b5SXin Li<li><b><code>isvararg</code>: </b> 5991*088332b5SXin Litrue if the function is a vararg function 5992*088332b5SXin Li(always true for C functions). 5993*088332b5SXin Li</li> 5994*088332b5SXin Li 5995*088332b5SXin Li<li><b><code>ftransfer</code>: </b> 5996*088332b5SXin Lithe index in the stack of the first value being "transferred", 5997*088332b5SXin Lithat is, parameters in a call or return values in a return. 5998*088332b5SXin Li(The other values are in consecutive indices.) 5999*088332b5SXin LiUsing this index, you can access and modify these values 6000*088332b5SXin Lithrough <a href="#lua_getlocal"><code>lua_getlocal</code></a> and <a href="#lua_setlocal"><code>lua_setlocal</code></a>. 6001*088332b5SXin LiThis field is only meaningful during a 6002*088332b5SXin Licall hook, denoting the first parameter, 6003*088332b5SXin Lior a return hook, denoting the first value being returned. 6004*088332b5SXin Li(For call hooks, this value is always 1.) 6005*088332b5SXin Li</li> 6006*088332b5SXin Li 6007*088332b5SXin Li<li><b><code>ntransfer</code>: </b> 6008*088332b5SXin LiThe number of values being transferred (see previous item). 6009*088332b5SXin Li(For calls of Lua functions, 6010*088332b5SXin Lithis value is always equal to <code>nparams</code>.) 6011*088332b5SXin Li</li> 6012*088332b5SXin Li 6013*088332b5SXin Li</ul> 6014*088332b5SXin Li 6015*088332b5SXin Li 6016*088332b5SXin Li 6017*088332b5SXin Li 6018*088332b5SXin Li<hr><h3><a name="lua_gethook"><code>lua_gethook</code></a></h3><p> 6019*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 6020*088332b5SXin Li<pre>lua_Hook lua_gethook (lua_State *L);</pre> 6021*088332b5SXin Li 6022*088332b5SXin Li<p> 6023*088332b5SXin LiReturns the current hook function. 6024*088332b5SXin Li 6025*088332b5SXin Li 6026*088332b5SXin Li 6027*088332b5SXin Li 6028*088332b5SXin Li 6029*088332b5SXin Li<hr><h3><a name="lua_gethookcount"><code>lua_gethookcount</code></a></h3><p> 6030*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 6031*088332b5SXin Li<pre>int lua_gethookcount (lua_State *L);</pre> 6032*088332b5SXin Li 6033*088332b5SXin Li<p> 6034*088332b5SXin LiReturns the current hook count. 6035*088332b5SXin Li 6036*088332b5SXin Li 6037*088332b5SXin Li 6038*088332b5SXin Li 6039*088332b5SXin Li 6040*088332b5SXin Li<hr><h3><a name="lua_gethookmask"><code>lua_gethookmask</code></a></h3><p> 6041*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 6042*088332b5SXin Li<pre>int lua_gethookmask (lua_State *L);</pre> 6043*088332b5SXin Li 6044*088332b5SXin Li<p> 6045*088332b5SXin LiReturns the current hook mask. 6046*088332b5SXin Li 6047*088332b5SXin Li 6048*088332b5SXin Li 6049*088332b5SXin Li 6050*088332b5SXin Li 6051*088332b5SXin Li<hr><h3><a name="lua_getinfo"><code>lua_getinfo</code></a></h3><p> 6052*088332b5SXin Li<span class="apii">[-(0|1), +(0|1|2), <em>m</em>]</span> 6053*088332b5SXin Li<pre>int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);</pre> 6054*088332b5SXin Li 6055*088332b5SXin Li<p> 6056*088332b5SXin LiGets information about a specific function or function invocation. 6057*088332b5SXin Li 6058*088332b5SXin Li 6059*088332b5SXin Li<p> 6060*088332b5SXin LiTo get information about a function invocation, 6061*088332b5SXin Lithe parameter <code>ar</code> must be a valid activation record that was 6062*088332b5SXin Lifilled by a previous call to <a href="#lua_getstack"><code>lua_getstack</code></a> or 6063*088332b5SXin Ligiven as argument to a hook (see <a href="#lua_Hook"><code>lua_Hook</code></a>). 6064*088332b5SXin Li 6065*088332b5SXin Li 6066*088332b5SXin Li<p> 6067*088332b5SXin LiTo get information about a function, you push it onto the stack 6068*088332b5SXin Liand start the <code>what</code> string with the character '<code>></code>'. 6069*088332b5SXin Li(In that case, 6070*088332b5SXin Li<code>lua_getinfo</code> pops the function from the top of the stack.) 6071*088332b5SXin LiFor instance, to know in which line a function <code>f</code> was defined, 6072*088332b5SXin Liyou can write the following code: 6073*088332b5SXin Li 6074*088332b5SXin Li<pre> 6075*088332b5SXin Li lua_Debug ar; 6076*088332b5SXin Li lua_getglobal(L, "f"); /* get global 'f' */ 6077*088332b5SXin Li lua_getinfo(L, ">S", &ar); 6078*088332b5SXin Li printf("%d\n", ar.linedefined); 6079*088332b5SXin Li</pre> 6080*088332b5SXin Li 6081*088332b5SXin Li<p> 6082*088332b5SXin LiEach character in the string <code>what</code> 6083*088332b5SXin Liselects some fields of the structure <code>ar</code> to be filled or 6084*088332b5SXin Lia value to be pushed on the stack: 6085*088332b5SXin Li 6086*088332b5SXin Li<ul> 6087*088332b5SXin Li 6088*088332b5SXin Li<li><b>'<code>n</code>': </b> fills in the field <code>name</code> and <code>namewhat</code>; 6089*088332b5SXin Li</li> 6090*088332b5SXin Li 6091*088332b5SXin Li<li><b>'<code>S</code>': </b> 6092*088332b5SXin Lifills in the fields <code>source</code>, <code>short_src</code>, 6093*088332b5SXin Li<code>linedefined</code>, <code>lastlinedefined</code>, and <code>what</code>; 6094*088332b5SXin Li</li> 6095*088332b5SXin Li 6096*088332b5SXin Li<li><b>'<code>l</code>': </b> fills in the field <code>currentline</code>; 6097*088332b5SXin Li</li> 6098*088332b5SXin Li 6099*088332b5SXin Li<li><b>'<code>t</code>': </b> fills in the field <code>istailcall</code>; 6100*088332b5SXin Li</li> 6101*088332b5SXin Li 6102*088332b5SXin Li<li><b>'<code>u</code>': </b> fills in the fields 6103*088332b5SXin Li<code>nups</code>, <code>nparams</code>, and <code>isvararg</code>; 6104*088332b5SXin Li</li> 6105*088332b5SXin Li 6106*088332b5SXin Li<li><b>'<code>f</code>': </b> 6107*088332b5SXin Lipushes onto the stack the function that is 6108*088332b5SXin Lirunning at the given level; 6109*088332b5SXin Li</li> 6110*088332b5SXin Li 6111*088332b5SXin Li<li><b>'<code>L</code>': </b> 6112*088332b5SXin Lipushes onto the stack a table whose indices are the 6113*088332b5SXin Linumbers of the lines that are valid on the function. 6114*088332b5SXin Li(A <em>valid line</em> is a line with some associated code, 6115*088332b5SXin Lithat is, a line where you can put a break point. 6116*088332b5SXin LiNon-valid lines include empty lines and comments.) 6117*088332b5SXin Li 6118*088332b5SXin Li 6119*088332b5SXin Li<p> 6120*088332b5SXin LiIf this option is given together with option '<code>f</code>', 6121*088332b5SXin Liits table is pushed after the function. 6122*088332b5SXin Li 6123*088332b5SXin Li 6124*088332b5SXin Li<p> 6125*088332b5SXin LiThis is the only option that can raise a memory error. 6126*088332b5SXin Li</li> 6127*088332b5SXin Li 6128*088332b5SXin Li</ul> 6129*088332b5SXin Li 6130*088332b5SXin Li<p> 6131*088332b5SXin LiThis function returns 0 to signal an invalid option in <code>what</code>; 6132*088332b5SXin Lieven then the valid options are handled correctly. 6133*088332b5SXin Li 6134*088332b5SXin Li 6135*088332b5SXin Li 6136*088332b5SXin Li 6137*088332b5SXin Li 6138*088332b5SXin Li<hr><h3><a name="lua_getlocal"><code>lua_getlocal</code></a></h3><p> 6139*088332b5SXin Li<span class="apii">[-0, +(0|1), –]</span> 6140*088332b5SXin Li<pre>const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n);</pre> 6141*088332b5SXin Li 6142*088332b5SXin Li<p> 6143*088332b5SXin LiGets information about a local variable or a temporary value 6144*088332b5SXin Liof a given activation record or a given function. 6145*088332b5SXin Li 6146*088332b5SXin Li 6147*088332b5SXin Li<p> 6148*088332b5SXin LiIn the first case, 6149*088332b5SXin Lithe parameter <code>ar</code> must be a valid activation record that was 6150*088332b5SXin Lifilled by a previous call to <a href="#lua_getstack"><code>lua_getstack</code></a> or 6151*088332b5SXin Ligiven as argument to a hook (see <a href="#lua_Hook"><code>lua_Hook</code></a>). 6152*088332b5SXin LiThe index <code>n</code> selects which local variable to inspect; 6153*088332b5SXin Lisee <a href="#pdf-debug.getlocal"><code>debug.getlocal</code></a> for details about variable indices 6154*088332b5SXin Liand names. 6155*088332b5SXin Li 6156*088332b5SXin Li 6157*088332b5SXin Li<p> 6158*088332b5SXin Li<a href="#lua_getlocal"><code>lua_getlocal</code></a> pushes the variable's value onto the stack 6159*088332b5SXin Liand returns its name. 6160*088332b5SXin Li 6161*088332b5SXin Li 6162*088332b5SXin Li<p> 6163*088332b5SXin LiIn the second case, <code>ar</code> must be <code>NULL</code> and the function 6164*088332b5SXin Lito be inspected must be on the top of the stack. 6165*088332b5SXin LiIn this case, only parameters of Lua functions are visible 6166*088332b5SXin Li(as there is no information about what variables are active) 6167*088332b5SXin Liand no values are pushed onto the stack. 6168*088332b5SXin Li 6169*088332b5SXin Li 6170*088332b5SXin Li<p> 6171*088332b5SXin LiReturns <code>NULL</code> (and pushes nothing) 6172*088332b5SXin Liwhen the index is greater than 6173*088332b5SXin Lithe number of active local variables. 6174*088332b5SXin Li 6175*088332b5SXin Li 6176*088332b5SXin Li 6177*088332b5SXin Li 6178*088332b5SXin Li 6179*088332b5SXin Li<hr><h3><a name="lua_getstack"><code>lua_getstack</code></a></h3><p> 6180*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 6181*088332b5SXin Li<pre>int lua_getstack (lua_State *L, int level, lua_Debug *ar);</pre> 6182*088332b5SXin Li 6183*088332b5SXin Li<p> 6184*088332b5SXin LiGets information about the interpreter runtime stack. 6185*088332b5SXin Li 6186*088332b5SXin Li 6187*088332b5SXin Li<p> 6188*088332b5SXin LiThis function fills parts of a <a href="#lua_Debug"><code>lua_Debug</code></a> structure with 6189*088332b5SXin Lian identification of the <em>activation record</em> 6190*088332b5SXin Liof the function executing at a given level. 6191*088332b5SXin LiLevel 0 is the current running function, 6192*088332b5SXin Liwhereas level <em>n+1</em> is the function that has called level <em>n</em> 6193*088332b5SXin Li(except for tail calls, which do not count in the stack). 6194*088332b5SXin LiWhen called with a level greater than the stack depth, 6195*088332b5SXin Li<a href="#lua_getstack"><code>lua_getstack</code></a> returns 0; 6196*088332b5SXin Liotherwise it returns 1. 6197*088332b5SXin Li 6198*088332b5SXin Li 6199*088332b5SXin Li 6200*088332b5SXin Li 6201*088332b5SXin Li 6202*088332b5SXin Li<hr><h3><a name="lua_getupvalue"><code>lua_getupvalue</code></a></h3><p> 6203*088332b5SXin Li<span class="apii">[-0, +(0|1), –]</span> 6204*088332b5SXin Li<pre>const char *lua_getupvalue (lua_State *L, int funcindex, int n);</pre> 6205*088332b5SXin Li 6206*088332b5SXin Li<p> 6207*088332b5SXin LiGets information about the <code>n</code>-th upvalue 6208*088332b5SXin Liof the closure at index <code>funcindex</code>. 6209*088332b5SXin LiIt pushes the upvalue's value onto the stack 6210*088332b5SXin Liand returns its name. 6211*088332b5SXin LiReturns <code>NULL</code> (and pushes nothing) 6212*088332b5SXin Liwhen the index <code>n</code> is greater than the number of upvalues. 6213*088332b5SXin Li 6214*088332b5SXin Li 6215*088332b5SXin Li<p> 6216*088332b5SXin LiSee <a href="#pdf-debug.getupvalue"><code>debug.getupvalue</code></a> for more information about upvalues. 6217*088332b5SXin Li 6218*088332b5SXin Li 6219*088332b5SXin Li 6220*088332b5SXin Li 6221*088332b5SXin Li 6222*088332b5SXin Li<hr><h3><a name="lua_Hook"><code>lua_Hook</code></a></h3> 6223*088332b5SXin Li<pre>typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);</pre> 6224*088332b5SXin Li 6225*088332b5SXin Li<p> 6226*088332b5SXin LiType for debugging hook functions. 6227*088332b5SXin Li 6228*088332b5SXin Li 6229*088332b5SXin Li<p> 6230*088332b5SXin LiWhenever a hook is called, its <code>ar</code> argument has its field 6231*088332b5SXin Li<code>event</code> set to the specific event that triggered the hook. 6232*088332b5SXin LiLua identifies these events with the following constants: 6233*088332b5SXin Li<a name="pdf-LUA_HOOKCALL"><code>LUA_HOOKCALL</code></a>, <a name="pdf-LUA_HOOKRET"><code>LUA_HOOKRET</code></a>, 6234*088332b5SXin Li<a name="pdf-LUA_HOOKTAILCALL"><code>LUA_HOOKTAILCALL</code></a>, <a name="pdf-LUA_HOOKLINE"><code>LUA_HOOKLINE</code></a>, 6235*088332b5SXin Liand <a name="pdf-LUA_HOOKCOUNT"><code>LUA_HOOKCOUNT</code></a>. 6236*088332b5SXin LiMoreover, for line events, the field <code>currentline</code> is also set. 6237*088332b5SXin LiTo get the value of any other field in <code>ar</code>, 6238*088332b5SXin Lithe hook must call <a href="#lua_getinfo"><code>lua_getinfo</code></a>. 6239*088332b5SXin Li 6240*088332b5SXin Li 6241*088332b5SXin Li<p> 6242*088332b5SXin LiFor call events, <code>event</code> can be <code>LUA_HOOKCALL</code>, 6243*088332b5SXin Lithe normal value, or <code>LUA_HOOKTAILCALL</code>, for a tail call; 6244*088332b5SXin Liin this case, there will be no corresponding return event. 6245*088332b5SXin Li 6246*088332b5SXin Li 6247*088332b5SXin Li<p> 6248*088332b5SXin LiWhile Lua is running a hook, it disables other calls to hooks. 6249*088332b5SXin LiTherefore, if a hook calls back Lua to execute a function or a chunk, 6250*088332b5SXin Lithis execution occurs without any calls to hooks. 6251*088332b5SXin Li 6252*088332b5SXin Li 6253*088332b5SXin Li<p> 6254*088332b5SXin LiHook functions cannot have continuations, 6255*088332b5SXin Lithat is, they cannot call <a href="#lua_yieldk"><code>lua_yieldk</code></a>, 6256*088332b5SXin Li<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>. 6257*088332b5SXin Li 6258*088332b5SXin Li 6259*088332b5SXin Li<p> 6260*088332b5SXin LiHook functions can yield under the following conditions: 6261*088332b5SXin LiOnly count and line events can yield; 6262*088332b5SXin Lito yield, a hook function must finish its execution 6263*088332b5SXin Licalling <a href="#lua_yield"><code>lua_yield</code></a> with <code>nresults</code> equal to zero 6264*088332b5SXin Li(that is, with no values). 6265*088332b5SXin Li 6266*088332b5SXin Li 6267*088332b5SXin Li 6268*088332b5SXin Li 6269*088332b5SXin Li 6270*088332b5SXin Li<hr><h3><a name="lua_setcstacklimit"><code>lua_setcstacklimit</code></a></h3><p> 6271*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 6272*088332b5SXin Li<pre>int (lua_setcstacklimit) (lua_State *L, unsigned int limit);</pre> 6273*088332b5SXin Li 6274*088332b5SXin Li<p> 6275*088332b5SXin LiSets a new limit for the C stack. 6276*088332b5SXin LiThis limit controls how deeply nested calls can go in Lua, 6277*088332b5SXin Liwith the intent of avoiding a stack overflow. 6278*088332b5SXin LiReturns the old limit in case of success, 6279*088332b5SXin Lior zero in case of error. 6280*088332b5SXin LiFor more details about this function, 6281*088332b5SXin Lisee <a href="#pdf-debug.setcstacklimit"><code>debug.setcstacklimit</code></a>, 6282*088332b5SXin Liits equivalent in the standard library. 6283*088332b5SXin Li 6284*088332b5SXin Li 6285*088332b5SXin Li 6286*088332b5SXin Li 6287*088332b5SXin Li 6288*088332b5SXin Li<hr><h3><a name="lua_sethook"><code>lua_sethook</code></a></h3><p> 6289*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 6290*088332b5SXin Li<pre>void lua_sethook (lua_State *L, lua_Hook f, int mask, int count);</pre> 6291*088332b5SXin Li 6292*088332b5SXin Li<p> 6293*088332b5SXin LiSets the debugging hook function. 6294*088332b5SXin Li 6295*088332b5SXin Li 6296*088332b5SXin Li<p> 6297*088332b5SXin LiArgument <code>f</code> is the hook function. 6298*088332b5SXin Li<code>mask</code> specifies on which events the hook will be called: 6299*088332b5SXin Liit is formed by a bitwise OR of the constants 6300*088332b5SXin Li<a name="pdf-LUA_MASKCALL"><code>LUA_MASKCALL</code></a>, 6301*088332b5SXin Li<a name="pdf-LUA_MASKRET"><code>LUA_MASKRET</code></a>, 6302*088332b5SXin Li<a name="pdf-LUA_MASKLINE"><code>LUA_MASKLINE</code></a>, 6303*088332b5SXin Liand <a name="pdf-LUA_MASKCOUNT"><code>LUA_MASKCOUNT</code></a>. 6304*088332b5SXin LiThe <code>count</code> argument is only meaningful when the mask 6305*088332b5SXin Liincludes <code>LUA_MASKCOUNT</code>. 6306*088332b5SXin LiFor each event, the hook is called as explained below: 6307*088332b5SXin Li 6308*088332b5SXin Li<ul> 6309*088332b5SXin Li 6310*088332b5SXin Li<li><b>The call hook: </b> is called when the interpreter calls a function. 6311*088332b5SXin LiThe hook is called just after Lua enters the new function. 6312*088332b5SXin Li</li> 6313*088332b5SXin Li 6314*088332b5SXin Li<li><b>The return hook: </b> is called when the interpreter returns from a function. 6315*088332b5SXin LiThe hook is called just before Lua leaves the function. 6316*088332b5SXin Li</li> 6317*088332b5SXin Li 6318*088332b5SXin Li<li><b>The line hook: </b> is called when the interpreter is about to 6319*088332b5SXin Listart the execution of a new line of code, 6320*088332b5SXin Lior when it jumps back in the code (even to the same line). 6321*088332b5SXin LiThis event only happens while Lua is executing a Lua function. 6322*088332b5SXin Li</li> 6323*088332b5SXin Li 6324*088332b5SXin Li<li><b>The count hook: </b> is called after the interpreter executes every 6325*088332b5SXin Li<code>count</code> instructions. 6326*088332b5SXin LiThis event only happens while Lua is executing a Lua function. 6327*088332b5SXin Li</li> 6328*088332b5SXin Li 6329*088332b5SXin Li</ul> 6330*088332b5SXin Li 6331*088332b5SXin Li<p> 6332*088332b5SXin LiHooks are disabled by setting <code>mask</code> to zero. 6333*088332b5SXin Li 6334*088332b5SXin Li 6335*088332b5SXin Li 6336*088332b5SXin Li 6337*088332b5SXin Li 6338*088332b5SXin Li<hr><h3><a name="lua_setlocal"><code>lua_setlocal</code></a></h3><p> 6339*088332b5SXin Li<span class="apii">[-(0|1), +0, –]</span> 6340*088332b5SXin Li<pre>const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n);</pre> 6341*088332b5SXin Li 6342*088332b5SXin Li<p> 6343*088332b5SXin LiSets the value of a local variable of a given activation record. 6344*088332b5SXin LiIt assigns the value on the top of the stack 6345*088332b5SXin Lito the variable and returns its name. 6346*088332b5SXin LiIt also pops the value from the stack. 6347*088332b5SXin Li 6348*088332b5SXin Li 6349*088332b5SXin Li<p> 6350*088332b5SXin LiReturns <code>NULL</code> (and pops nothing) 6351*088332b5SXin Liwhen the index is greater than 6352*088332b5SXin Lithe number of active local variables. 6353*088332b5SXin Li 6354*088332b5SXin Li 6355*088332b5SXin Li<p> 6356*088332b5SXin LiParameters <code>ar</code> and <code>n</code> are as in the function <a href="#lua_getlocal"><code>lua_getlocal</code></a>. 6357*088332b5SXin Li 6358*088332b5SXin Li 6359*088332b5SXin Li 6360*088332b5SXin Li 6361*088332b5SXin Li 6362*088332b5SXin Li<hr><h3><a name="lua_setupvalue"><code>lua_setupvalue</code></a></h3><p> 6363*088332b5SXin Li<span class="apii">[-(0|1), +0, –]</span> 6364*088332b5SXin Li<pre>const char *lua_setupvalue (lua_State *L, int funcindex, int n);</pre> 6365*088332b5SXin Li 6366*088332b5SXin Li<p> 6367*088332b5SXin LiSets the value of a closure's upvalue. 6368*088332b5SXin LiIt assigns the value on the top of the stack 6369*088332b5SXin Lito the upvalue and returns its name. 6370*088332b5SXin LiIt also pops the value from the stack. 6371*088332b5SXin Li 6372*088332b5SXin Li 6373*088332b5SXin Li<p> 6374*088332b5SXin LiReturns <code>NULL</code> (and pops nothing) 6375*088332b5SXin Liwhen the index <code>n</code> is greater than the number of upvalues. 6376*088332b5SXin Li 6377*088332b5SXin Li 6378*088332b5SXin Li<p> 6379*088332b5SXin LiParameters <code>funcindex</code> and <code>n</code> are as in 6380*088332b5SXin Lithe function <a href="#lua_getupvalue"><code>lua_getupvalue</code></a>. 6381*088332b5SXin Li 6382*088332b5SXin Li 6383*088332b5SXin Li 6384*088332b5SXin Li 6385*088332b5SXin Li 6386*088332b5SXin Li<hr><h3><a name="lua_upvalueid"><code>lua_upvalueid</code></a></h3><p> 6387*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 6388*088332b5SXin Li<pre>void *lua_upvalueid (lua_State *L, int funcindex, int n);</pre> 6389*088332b5SXin Li 6390*088332b5SXin Li<p> 6391*088332b5SXin LiReturns a unique identifier for the upvalue numbered <code>n</code> 6392*088332b5SXin Lifrom the closure at index <code>funcindex</code>. 6393*088332b5SXin Li 6394*088332b5SXin Li 6395*088332b5SXin Li<p> 6396*088332b5SXin LiThese unique identifiers allow a program to check whether different 6397*088332b5SXin Liclosures share upvalues. 6398*088332b5SXin LiLua closures that share an upvalue 6399*088332b5SXin Li(that is, that access a same external local variable) 6400*088332b5SXin Liwill return identical ids for those upvalue indices. 6401*088332b5SXin Li 6402*088332b5SXin Li 6403*088332b5SXin Li<p> 6404*088332b5SXin LiParameters <code>funcindex</code> and <code>n</code> are as in 6405*088332b5SXin Lithe function <a href="#lua_getupvalue"><code>lua_getupvalue</code></a>, 6406*088332b5SXin Libut <code>n</code> cannot be greater than the number of upvalues. 6407*088332b5SXin Li 6408*088332b5SXin Li 6409*088332b5SXin Li 6410*088332b5SXin Li 6411*088332b5SXin Li 6412*088332b5SXin Li<hr><h3><a name="lua_upvaluejoin"><code>lua_upvaluejoin</code></a></h3><p> 6413*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 6414*088332b5SXin Li<pre>void lua_upvaluejoin (lua_State *L, int funcindex1, int n1, 6415*088332b5SXin Li int funcindex2, int n2);</pre> 6416*088332b5SXin Li 6417*088332b5SXin Li<p> 6418*088332b5SXin LiMake the <code>n1</code>-th upvalue of the Lua closure at index <code>funcindex1</code> 6419*088332b5SXin Lirefer to the <code>n2</code>-th upvalue of the Lua closure at index <code>funcindex2</code>. 6420*088332b5SXin Li 6421*088332b5SXin Li 6422*088332b5SXin Li 6423*088332b5SXin Li 6424*088332b5SXin Li 6425*088332b5SXin Li 6426*088332b5SXin Li 6427*088332b5SXin Li<h1>5 – <a name="5">The Auxiliary Library</a></h1> 6428*088332b5SXin Li 6429*088332b5SXin Li 6430*088332b5SXin Li 6431*088332b5SXin Li<p> 6432*088332b5SXin Li 6433*088332b5SXin LiThe <em>auxiliary library</em> provides several convenient functions 6434*088332b5SXin Lito interface C with Lua. 6435*088332b5SXin LiWhile the basic API provides the primitive functions for all 6436*088332b5SXin Liinteractions between C and Lua, 6437*088332b5SXin Lithe auxiliary library provides higher-level functions for some 6438*088332b5SXin Licommon tasks. 6439*088332b5SXin Li 6440*088332b5SXin Li 6441*088332b5SXin Li<p> 6442*088332b5SXin LiAll functions and types from the auxiliary library 6443*088332b5SXin Liare defined in header file <code>lauxlib.h</code> and 6444*088332b5SXin Lihave a prefix <code>luaL_</code>. 6445*088332b5SXin Li 6446*088332b5SXin Li 6447*088332b5SXin Li<p> 6448*088332b5SXin LiAll functions in the auxiliary library are built on 6449*088332b5SXin Litop of the basic API, 6450*088332b5SXin Liand so they provide nothing that cannot be done with that API. 6451*088332b5SXin LiNevertheless, the use of the auxiliary library ensures 6452*088332b5SXin Limore consistency to your code. 6453*088332b5SXin Li 6454*088332b5SXin Li 6455*088332b5SXin Li<p> 6456*088332b5SXin LiSeveral functions in the auxiliary library use internally some 6457*088332b5SXin Liextra stack slots. 6458*088332b5SXin LiWhen a function in the auxiliary library uses less than five slots, 6459*088332b5SXin Liit does not check the stack size; 6460*088332b5SXin Liit simply assumes that there are enough slots. 6461*088332b5SXin Li 6462*088332b5SXin Li 6463*088332b5SXin Li<p> 6464*088332b5SXin LiSeveral functions in the auxiliary library are used to 6465*088332b5SXin Licheck C function arguments. 6466*088332b5SXin LiBecause the error message is formatted for arguments 6467*088332b5SXin Li(e.g., "<code>bad argument #1</code>"), 6468*088332b5SXin Liyou should not use these functions for other stack values. 6469*088332b5SXin Li 6470*088332b5SXin Li 6471*088332b5SXin Li<p> 6472*088332b5SXin LiFunctions called <code>luaL_check*</code> 6473*088332b5SXin Lialways raise an error if the check is not satisfied. 6474*088332b5SXin Li 6475*088332b5SXin Li 6476*088332b5SXin Li 6477*088332b5SXin Li 6478*088332b5SXin Li 6479*088332b5SXin Li<h2>5.1 – <a name="5.1">Functions and Types</a></h2> 6480*088332b5SXin Li 6481*088332b5SXin Li<p> 6482*088332b5SXin LiHere we list all functions and types from the auxiliary library 6483*088332b5SXin Liin alphabetical order. 6484*088332b5SXin Li 6485*088332b5SXin Li 6486*088332b5SXin Li 6487*088332b5SXin Li<hr><h3><a name="luaL_addchar"><code>luaL_addchar</code></a></h3><p> 6488*088332b5SXin Li<span class="apii">[-?, +?, <em>m</em>]</span> 6489*088332b5SXin Li<pre>void luaL_addchar (luaL_Buffer *B, char c);</pre> 6490*088332b5SXin Li 6491*088332b5SXin Li<p> 6492*088332b5SXin LiAdds the byte <code>c</code> to the buffer <code>B</code> 6493*088332b5SXin Li(see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>). 6494*088332b5SXin Li 6495*088332b5SXin Li 6496*088332b5SXin Li 6497*088332b5SXin Li 6498*088332b5SXin Li 6499*088332b5SXin Li<hr><h3><a name="luaL_addgsub"><code>luaL_addgsub</code></a></h3><p> 6500*088332b5SXin Li<span class="apii">[-0, +0, <em>m</em>]</span> 6501*088332b5SXin Li<pre>const void luaL_addgsub (luaL_Buffer *B, const char *s, 6502*088332b5SXin Li const char *p, const char *r);</pre> 6503*088332b5SXin Li 6504*088332b5SXin Li<p> 6505*088332b5SXin LiAdds a copy of the string <code>s</code> to the buffer <code>B</code> (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>), 6506*088332b5SXin Lireplacing any occurrence of the string <code>p</code> 6507*088332b5SXin Liwith the string <code>r</code>. 6508*088332b5SXin Li 6509*088332b5SXin Li 6510*088332b5SXin Li 6511*088332b5SXin Li 6512*088332b5SXin Li 6513*088332b5SXin Li<hr><h3><a name="luaL_addlstring"><code>luaL_addlstring</code></a></h3><p> 6514*088332b5SXin Li<span class="apii">[-?, +?, <em>m</em>]</span> 6515*088332b5SXin Li<pre>void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l);</pre> 6516*088332b5SXin Li 6517*088332b5SXin Li<p> 6518*088332b5SXin LiAdds the string pointed to by <code>s</code> with length <code>l</code> to 6519*088332b5SXin Lithe buffer <code>B</code> 6520*088332b5SXin Li(see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>). 6521*088332b5SXin LiThe string can contain embedded zeros. 6522*088332b5SXin Li 6523*088332b5SXin Li 6524*088332b5SXin Li 6525*088332b5SXin Li 6526*088332b5SXin Li 6527*088332b5SXin Li<hr><h3><a name="luaL_addsize"><code>luaL_addsize</code></a></h3><p> 6528*088332b5SXin Li<span class="apii">[-?, +?, –]</span> 6529*088332b5SXin Li<pre>void luaL_addsize (luaL_Buffer *B, size_t n);</pre> 6530*088332b5SXin Li 6531*088332b5SXin Li<p> 6532*088332b5SXin LiAdds to the buffer <code>B</code> 6533*088332b5SXin Lia string of length <code>n</code> previously copied to the 6534*088332b5SXin Libuffer area (see <a href="#luaL_prepbuffer"><code>luaL_prepbuffer</code></a>). 6535*088332b5SXin Li 6536*088332b5SXin Li 6537*088332b5SXin Li 6538*088332b5SXin Li 6539*088332b5SXin Li 6540*088332b5SXin Li<hr><h3><a name="luaL_addstring"><code>luaL_addstring</code></a></h3><p> 6541*088332b5SXin Li<span class="apii">[-?, +?, <em>m</em>]</span> 6542*088332b5SXin Li<pre>void luaL_addstring (luaL_Buffer *B, const char *s);</pre> 6543*088332b5SXin Li 6544*088332b5SXin Li<p> 6545*088332b5SXin LiAdds the zero-terminated string pointed to by <code>s</code> 6546*088332b5SXin Lito the buffer <code>B</code> 6547*088332b5SXin Li(see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>). 6548*088332b5SXin Li 6549*088332b5SXin Li 6550*088332b5SXin Li 6551*088332b5SXin Li 6552*088332b5SXin Li 6553*088332b5SXin Li<hr><h3><a name="luaL_addvalue"><code>luaL_addvalue</code></a></h3><p> 6554*088332b5SXin Li<span class="apii">[-1, +?, <em>m</em>]</span> 6555*088332b5SXin Li<pre>void luaL_addvalue (luaL_Buffer *B);</pre> 6556*088332b5SXin Li 6557*088332b5SXin Li<p> 6558*088332b5SXin LiAdds the value on the top of the stack 6559*088332b5SXin Lito the buffer <code>B</code> 6560*088332b5SXin Li(see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>). 6561*088332b5SXin LiPops the value. 6562*088332b5SXin Li 6563*088332b5SXin Li 6564*088332b5SXin Li<p> 6565*088332b5SXin LiThis is the only function on string buffers that can (and must) 6566*088332b5SXin Libe called with an extra element on the stack, 6567*088332b5SXin Liwhich is the value to be added to the buffer. 6568*088332b5SXin Li 6569*088332b5SXin Li 6570*088332b5SXin Li 6571*088332b5SXin Li 6572*088332b5SXin Li 6573*088332b5SXin Li<hr><h3><a name="luaL_argcheck"><code>luaL_argcheck</code></a></h3><p> 6574*088332b5SXin Li<span class="apii">[-0, +0, <em>v</em>]</span> 6575*088332b5SXin Li<pre>void luaL_argcheck (lua_State *L, 6576*088332b5SXin Li int cond, 6577*088332b5SXin Li int arg, 6578*088332b5SXin Li const char *extramsg);</pre> 6579*088332b5SXin Li 6580*088332b5SXin Li<p> 6581*088332b5SXin LiChecks whether <code>cond</code> is true. 6582*088332b5SXin LiIf it is not, raises an error with a standard message (see <a href="#luaL_argerror"><code>luaL_argerror</code></a>). 6583*088332b5SXin Li 6584*088332b5SXin Li 6585*088332b5SXin Li 6586*088332b5SXin Li 6587*088332b5SXin Li 6588*088332b5SXin Li<hr><h3><a name="luaL_argerror"><code>luaL_argerror</code></a></h3><p> 6589*088332b5SXin Li<span class="apii">[-0, +0, <em>v</em>]</span> 6590*088332b5SXin Li<pre>int luaL_argerror (lua_State *L, int arg, const char *extramsg);</pre> 6591*088332b5SXin Li 6592*088332b5SXin Li<p> 6593*088332b5SXin LiRaises an error reporting a problem with argument <code>arg</code> 6594*088332b5SXin Liof the C function that called it, 6595*088332b5SXin Liusing a standard message 6596*088332b5SXin Lithat includes <code>extramsg</code> as a comment: 6597*088332b5SXin Li 6598*088332b5SXin Li<pre> 6599*088332b5SXin Li bad argument #<em>arg</em> to '<em>funcname</em>' (<em>extramsg</em>) 6600*088332b5SXin Li</pre><p> 6601*088332b5SXin LiThis function never returns. 6602*088332b5SXin Li 6603*088332b5SXin Li 6604*088332b5SXin Li 6605*088332b5SXin Li 6606*088332b5SXin Li 6607*088332b5SXin Li<hr><h3><a name="luaL_argexpected"><code>luaL_argexpected</code></a></h3><p> 6608*088332b5SXin Li<span class="apii">[-0, +0, <em>v</em>]</span> 6609*088332b5SXin Li<pre>void luaL_argexpected (lua_State *L, 6610*088332b5SXin Li int cond, 6611*088332b5SXin Li int arg, 6612*088332b5SXin Li const char *tname);</pre> 6613*088332b5SXin Li 6614*088332b5SXin Li<p> 6615*088332b5SXin LiChecks whether <code>cond</code> is true. 6616*088332b5SXin LiIf it is not, raises an error about the type of the argument <code>arg</code> 6617*088332b5SXin Liwith a standard message (see <a href="#luaL_typeerror"><code>luaL_typeerror</code></a>). 6618*088332b5SXin Li 6619*088332b5SXin Li 6620*088332b5SXin Li 6621*088332b5SXin Li 6622*088332b5SXin Li 6623*088332b5SXin Li<hr><h3><a name="luaL_Buffer"><code>luaL_Buffer</code></a></h3> 6624*088332b5SXin Li<pre>typedef struct luaL_Buffer luaL_Buffer;</pre> 6625*088332b5SXin Li 6626*088332b5SXin Li<p> 6627*088332b5SXin LiType for a <em>string buffer</em>. 6628*088332b5SXin Li 6629*088332b5SXin Li 6630*088332b5SXin Li<p> 6631*088332b5SXin LiA string buffer allows C code to build Lua strings piecemeal. 6632*088332b5SXin LiIts pattern of use is as follows: 6633*088332b5SXin Li 6634*088332b5SXin Li<ul> 6635*088332b5SXin Li 6636*088332b5SXin Li<li>First declare a variable <code>b</code> of type <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>.</li> 6637*088332b5SXin Li 6638*088332b5SXin Li<li>Then initialize it with a call <code>luaL_buffinit(L, &b)</code>.</li> 6639*088332b5SXin Li 6640*088332b5SXin Li<li> 6641*088332b5SXin LiThen add string pieces to the buffer calling any of 6642*088332b5SXin Lithe <code>luaL_add*</code> functions. 6643*088332b5SXin Li</li> 6644*088332b5SXin Li 6645*088332b5SXin Li<li> 6646*088332b5SXin LiFinish by calling <code>luaL_pushresult(&b)</code>. 6647*088332b5SXin LiThis call leaves the final string on the top of the stack. 6648*088332b5SXin Li</li> 6649*088332b5SXin Li 6650*088332b5SXin Li</ul> 6651*088332b5SXin Li 6652*088332b5SXin Li<p> 6653*088332b5SXin LiIf you know beforehand the maximum size of the resulting string, 6654*088332b5SXin Liyou can use the buffer like this: 6655*088332b5SXin Li 6656*088332b5SXin Li<ul> 6657*088332b5SXin Li 6658*088332b5SXin Li<li>First declare a variable <code>b</code> of type <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>.</li> 6659*088332b5SXin Li 6660*088332b5SXin Li<li>Then initialize it and preallocate a space of 6661*088332b5SXin Lisize <code>sz</code> with a call <code>luaL_buffinitsize(L, &b, sz)</code>.</li> 6662*088332b5SXin Li 6663*088332b5SXin Li<li>Then produce the string into that space.</li> 6664*088332b5SXin Li 6665*088332b5SXin Li<li> 6666*088332b5SXin LiFinish by calling <code>luaL_pushresultsize(&b, sz)</code>, 6667*088332b5SXin Liwhere <code>sz</code> is the total size of the resulting string 6668*088332b5SXin Licopied into that space (which may be less than or 6669*088332b5SXin Liequal to the preallocated size). 6670*088332b5SXin Li</li> 6671*088332b5SXin Li 6672*088332b5SXin Li</ul> 6673*088332b5SXin Li 6674*088332b5SXin Li<p> 6675*088332b5SXin LiDuring its normal operation, 6676*088332b5SXin Lia string buffer uses a variable number of stack slots. 6677*088332b5SXin LiSo, while using a buffer, you cannot assume that you know where 6678*088332b5SXin Lithe top of the stack is. 6679*088332b5SXin LiYou can use the stack between successive calls to buffer operations 6680*088332b5SXin Lias long as that use is balanced; 6681*088332b5SXin Lithat is, 6682*088332b5SXin Liwhen you call a buffer operation, 6683*088332b5SXin Lithe stack is at the same level 6684*088332b5SXin Liit was immediately after the previous buffer operation. 6685*088332b5SXin Li(The only exception to this rule is <a href="#luaL_addvalue"><code>luaL_addvalue</code></a>.) 6686*088332b5SXin LiAfter calling <a href="#luaL_pushresult"><code>luaL_pushresult</code></a>, 6687*088332b5SXin Lithe stack is back to its level when the buffer was initialized, 6688*088332b5SXin Liplus the final string on its top. 6689*088332b5SXin Li 6690*088332b5SXin Li 6691*088332b5SXin Li 6692*088332b5SXin Li 6693*088332b5SXin Li 6694*088332b5SXin Li<hr><h3><a name="luaL_buffaddr"><code>luaL_buffaddr</code></a></h3><p> 6695*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 6696*088332b5SXin Li<pre>char *luaL_buffaddr (luaL_Buffer *B);</pre> 6697*088332b5SXin Li 6698*088332b5SXin Li<p> 6699*088332b5SXin LiReturns the address of the current content of buffer <code>B</code> 6700*088332b5SXin Li(see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>). 6701*088332b5SXin LiNote that any addition to the buffer may invalidate this address. 6702*088332b5SXin Li 6703*088332b5SXin Li 6704*088332b5SXin Li 6705*088332b5SXin Li 6706*088332b5SXin Li 6707*088332b5SXin Li<hr><h3><a name="luaL_buffinit"><code>luaL_buffinit</code></a></h3><p> 6708*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 6709*088332b5SXin Li<pre>void luaL_buffinit (lua_State *L, luaL_Buffer *B);</pre> 6710*088332b5SXin Li 6711*088332b5SXin Li<p> 6712*088332b5SXin LiInitializes a buffer <code>B</code> 6713*088332b5SXin Li(see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>). 6714*088332b5SXin LiThis function does not allocate any space; 6715*088332b5SXin Lithe buffer must be declared as a variable. 6716*088332b5SXin Li 6717*088332b5SXin Li 6718*088332b5SXin Li 6719*088332b5SXin Li 6720*088332b5SXin Li 6721*088332b5SXin Li<hr><h3><a name="luaL_bufflen"><code>luaL_bufflen</code></a></h3><p> 6722*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 6723*088332b5SXin Li<pre>size_t luaL_bufflen (luaL_Buffer *B);</pre> 6724*088332b5SXin Li 6725*088332b5SXin Li<p> 6726*088332b5SXin LiReturns the length of the current content of buffer <code>B</code> 6727*088332b5SXin Li(see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>). 6728*088332b5SXin Li 6729*088332b5SXin Li 6730*088332b5SXin Li 6731*088332b5SXin Li 6732*088332b5SXin Li 6733*088332b5SXin Li<hr><h3><a name="luaL_buffinitsize"><code>luaL_buffinitsize</code></a></h3><p> 6734*088332b5SXin Li<span class="apii">[-?, +?, <em>m</em>]</span> 6735*088332b5SXin Li<pre>char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz);</pre> 6736*088332b5SXin Li 6737*088332b5SXin Li<p> 6738*088332b5SXin LiEquivalent to the sequence 6739*088332b5SXin Li<a href="#luaL_buffinit"><code>luaL_buffinit</code></a>, <a href="#luaL_prepbuffsize"><code>luaL_prepbuffsize</code></a>. 6740*088332b5SXin Li 6741*088332b5SXin Li 6742*088332b5SXin Li 6743*088332b5SXin Li 6744*088332b5SXin Li 6745*088332b5SXin Li<hr><h3><a name="luaL_buffsub"><code>luaL_buffsub</code></a></h3><p> 6746*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 6747*088332b5SXin Li<pre>void luaL_buffsub (luaL_Buffer *B, int n);</pre> 6748*088332b5SXin Li 6749*088332b5SXin Li<p> 6750*088332b5SXin LiRemoves <code>n</code> bytes from the the buffer <code>B</code> 6751*088332b5SXin Li(see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>). 6752*088332b5SXin LiThe buffer must have at least that many bytes. 6753*088332b5SXin Li 6754*088332b5SXin Li 6755*088332b5SXin Li 6756*088332b5SXin Li 6757*088332b5SXin Li 6758*088332b5SXin Li<hr><h3><a name="luaL_callmeta"><code>luaL_callmeta</code></a></h3><p> 6759*088332b5SXin Li<span class="apii">[-0, +(0|1), <em>e</em>]</span> 6760*088332b5SXin Li<pre>int luaL_callmeta (lua_State *L, int obj, const char *e);</pre> 6761*088332b5SXin Li 6762*088332b5SXin Li<p> 6763*088332b5SXin LiCalls a metamethod. 6764*088332b5SXin Li 6765*088332b5SXin Li 6766*088332b5SXin Li<p> 6767*088332b5SXin LiIf the object at index <code>obj</code> has a metatable and this 6768*088332b5SXin Limetatable has a field <code>e</code>, 6769*088332b5SXin Lithis function calls this field passing the object as its only argument. 6770*088332b5SXin LiIn this case this function returns true and pushes onto the 6771*088332b5SXin Listack the value returned by the call. 6772*088332b5SXin LiIf there is no metatable or no metamethod, 6773*088332b5SXin Lithis function returns false without pushing any value on the stack. 6774*088332b5SXin Li 6775*088332b5SXin Li 6776*088332b5SXin Li 6777*088332b5SXin Li 6778*088332b5SXin Li 6779*088332b5SXin Li<hr><h3><a name="luaL_checkany"><code>luaL_checkany</code></a></h3><p> 6780*088332b5SXin Li<span class="apii">[-0, +0, <em>v</em>]</span> 6781*088332b5SXin Li<pre>void luaL_checkany (lua_State *L, int arg);</pre> 6782*088332b5SXin Li 6783*088332b5SXin Li<p> 6784*088332b5SXin LiChecks whether the function has an argument 6785*088332b5SXin Liof any type (including <b>nil</b>) at position <code>arg</code>. 6786*088332b5SXin Li 6787*088332b5SXin Li 6788*088332b5SXin Li 6789*088332b5SXin Li 6790*088332b5SXin Li 6791*088332b5SXin Li<hr><h3><a name="luaL_checkinteger"><code>luaL_checkinteger</code></a></h3><p> 6792*088332b5SXin Li<span class="apii">[-0, +0, <em>v</em>]</span> 6793*088332b5SXin Li<pre>lua_Integer luaL_checkinteger (lua_State *L, int arg);</pre> 6794*088332b5SXin Li 6795*088332b5SXin Li<p> 6796*088332b5SXin LiChecks whether the function argument <code>arg</code> is an integer 6797*088332b5SXin Li(or can be converted to an integer) 6798*088332b5SXin Liand returns this integer. 6799*088332b5SXin Li 6800*088332b5SXin Li 6801*088332b5SXin Li 6802*088332b5SXin Li 6803*088332b5SXin Li 6804*088332b5SXin Li<hr><h3><a name="luaL_checklstring"><code>luaL_checklstring</code></a></h3><p> 6805*088332b5SXin Li<span class="apii">[-0, +0, <em>v</em>]</span> 6806*088332b5SXin Li<pre>const char *luaL_checklstring (lua_State *L, int arg, size_t *l);</pre> 6807*088332b5SXin Li 6808*088332b5SXin Li<p> 6809*088332b5SXin LiChecks whether the function argument <code>arg</code> is a string 6810*088332b5SXin Liand returns this string; 6811*088332b5SXin Liif <code>l</code> is not <code>NULL</code> fills its referent 6812*088332b5SXin Liwith the string's length. 6813*088332b5SXin Li 6814*088332b5SXin Li 6815*088332b5SXin Li<p> 6816*088332b5SXin LiThis function uses <a href="#lua_tolstring"><code>lua_tolstring</code></a> to get its result, 6817*088332b5SXin Liso all conversions and caveats of that function apply here. 6818*088332b5SXin Li 6819*088332b5SXin Li 6820*088332b5SXin Li 6821*088332b5SXin Li 6822*088332b5SXin Li 6823*088332b5SXin Li<hr><h3><a name="luaL_checknumber"><code>luaL_checknumber</code></a></h3><p> 6824*088332b5SXin Li<span class="apii">[-0, +0, <em>v</em>]</span> 6825*088332b5SXin Li<pre>lua_Number luaL_checknumber (lua_State *L, int arg);</pre> 6826*088332b5SXin Li 6827*088332b5SXin Li<p> 6828*088332b5SXin LiChecks whether the function argument <code>arg</code> is a number 6829*088332b5SXin Liand returns this number converted to a <code>lua_Number</code>. 6830*088332b5SXin Li 6831*088332b5SXin Li 6832*088332b5SXin Li 6833*088332b5SXin Li 6834*088332b5SXin Li 6835*088332b5SXin Li<hr><h3><a name="luaL_checkoption"><code>luaL_checkoption</code></a></h3><p> 6836*088332b5SXin Li<span class="apii">[-0, +0, <em>v</em>]</span> 6837*088332b5SXin Li<pre>int luaL_checkoption (lua_State *L, 6838*088332b5SXin Li int arg, 6839*088332b5SXin Li const char *def, 6840*088332b5SXin Li const char *const lst[]);</pre> 6841*088332b5SXin Li 6842*088332b5SXin Li<p> 6843*088332b5SXin LiChecks whether the function argument <code>arg</code> is a string and 6844*088332b5SXin Lisearches for this string in the array <code>lst</code> 6845*088332b5SXin Li(which must be NULL-terminated). 6846*088332b5SXin LiReturns the index in the array where the string was found. 6847*088332b5SXin LiRaises an error if the argument is not a string or 6848*088332b5SXin Liif the string cannot be found. 6849*088332b5SXin Li 6850*088332b5SXin Li 6851*088332b5SXin Li<p> 6852*088332b5SXin LiIf <code>def</code> is not <code>NULL</code>, 6853*088332b5SXin Lithe function uses <code>def</code> as a default value when 6854*088332b5SXin Lithere is no argument <code>arg</code> or when this argument is <b>nil</b>. 6855*088332b5SXin Li 6856*088332b5SXin Li 6857*088332b5SXin Li<p> 6858*088332b5SXin LiThis is a useful function for mapping strings to C enums. 6859*088332b5SXin Li(The usual convention in Lua libraries is 6860*088332b5SXin Lito use strings instead of numbers to select options.) 6861*088332b5SXin Li 6862*088332b5SXin Li 6863*088332b5SXin Li 6864*088332b5SXin Li 6865*088332b5SXin Li 6866*088332b5SXin Li<hr><h3><a name="luaL_checkstack"><code>luaL_checkstack</code></a></h3><p> 6867*088332b5SXin Li<span class="apii">[-0, +0, <em>v</em>]</span> 6868*088332b5SXin Li<pre>void luaL_checkstack (lua_State *L, int sz, const char *msg);</pre> 6869*088332b5SXin Li 6870*088332b5SXin Li<p> 6871*088332b5SXin LiGrows the stack size to <code>top + sz</code> elements, 6872*088332b5SXin Liraising an error if the stack cannot grow to that size. 6873*088332b5SXin Li<code>msg</code> is an additional text to go into the error message 6874*088332b5SXin Li(or <code>NULL</code> for no additional text). 6875*088332b5SXin Li 6876*088332b5SXin Li 6877*088332b5SXin Li 6878*088332b5SXin Li 6879*088332b5SXin Li 6880*088332b5SXin Li<hr><h3><a name="luaL_checkstring"><code>luaL_checkstring</code></a></h3><p> 6881*088332b5SXin Li<span class="apii">[-0, +0, <em>v</em>]</span> 6882*088332b5SXin Li<pre>const char *luaL_checkstring (lua_State *L, int arg);</pre> 6883*088332b5SXin Li 6884*088332b5SXin Li<p> 6885*088332b5SXin LiChecks whether the function argument <code>arg</code> is a string 6886*088332b5SXin Liand returns this string. 6887*088332b5SXin Li 6888*088332b5SXin Li 6889*088332b5SXin Li<p> 6890*088332b5SXin LiThis function uses <a href="#lua_tolstring"><code>lua_tolstring</code></a> to get its result, 6891*088332b5SXin Liso all conversions and caveats of that function apply here. 6892*088332b5SXin Li 6893*088332b5SXin Li 6894*088332b5SXin Li 6895*088332b5SXin Li 6896*088332b5SXin Li 6897*088332b5SXin Li<hr><h3><a name="luaL_checktype"><code>luaL_checktype</code></a></h3><p> 6898*088332b5SXin Li<span class="apii">[-0, +0, <em>v</em>]</span> 6899*088332b5SXin Li<pre>void luaL_checktype (lua_State *L, int arg, int t);</pre> 6900*088332b5SXin Li 6901*088332b5SXin Li<p> 6902*088332b5SXin LiChecks whether the function argument <code>arg</code> has type <code>t</code>. 6903*088332b5SXin LiSee <a href="#lua_type"><code>lua_type</code></a> for the encoding of types for <code>t</code>. 6904*088332b5SXin Li 6905*088332b5SXin Li 6906*088332b5SXin Li 6907*088332b5SXin Li 6908*088332b5SXin Li 6909*088332b5SXin Li<hr><h3><a name="luaL_checkudata"><code>luaL_checkudata</code></a></h3><p> 6910*088332b5SXin Li<span class="apii">[-0, +0, <em>v</em>]</span> 6911*088332b5SXin Li<pre>void *luaL_checkudata (lua_State *L, int arg, const char *tname);</pre> 6912*088332b5SXin Li 6913*088332b5SXin Li<p> 6914*088332b5SXin LiChecks whether the function argument <code>arg</code> is a userdata 6915*088332b5SXin Liof the type <code>tname</code> (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>) and 6916*088332b5SXin Lireturns the userdata's memory-block address (see <a href="#lua_touserdata"><code>lua_touserdata</code></a>). 6917*088332b5SXin Li 6918*088332b5SXin Li 6919*088332b5SXin Li 6920*088332b5SXin Li 6921*088332b5SXin Li 6922*088332b5SXin Li<hr><h3><a name="luaL_checkversion"><code>luaL_checkversion</code></a></h3><p> 6923*088332b5SXin Li<span class="apii">[-0, +0, <em>v</em>]</span> 6924*088332b5SXin Li<pre>void luaL_checkversion (lua_State *L);</pre> 6925*088332b5SXin Li 6926*088332b5SXin Li<p> 6927*088332b5SXin LiChecks whether the code making the call and the Lua library being called 6928*088332b5SXin Liare using the same version of Lua and the same numeric types. 6929*088332b5SXin Li 6930*088332b5SXin Li 6931*088332b5SXin Li 6932*088332b5SXin Li 6933*088332b5SXin Li 6934*088332b5SXin Li<hr><h3><a name="luaL_dofile"><code>luaL_dofile</code></a></h3><p> 6935*088332b5SXin Li<span class="apii">[-0, +?, <em>m</em>]</span> 6936*088332b5SXin Li<pre>int luaL_dofile (lua_State *L, const char *filename);</pre> 6937*088332b5SXin Li 6938*088332b5SXin Li<p> 6939*088332b5SXin LiLoads and runs the given file. 6940*088332b5SXin LiIt is defined as the following macro: 6941*088332b5SXin Li 6942*088332b5SXin Li<pre> 6943*088332b5SXin Li (luaL_loadfile(L, filename) || lua_pcall(L, 0, LUA_MULTRET, 0)) 6944*088332b5SXin Li</pre><p> 6945*088332b5SXin LiIt returns <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> if there are no errors, 6946*088332b5SXin Lior an error code in case of errors (see <a href="#4.4.1">§4.4.1</a>). 6947*088332b5SXin Li 6948*088332b5SXin Li 6949*088332b5SXin Li 6950*088332b5SXin Li 6951*088332b5SXin Li 6952*088332b5SXin Li<hr><h3><a name="luaL_dostring"><code>luaL_dostring</code></a></h3><p> 6953*088332b5SXin Li<span class="apii">[-0, +?, –]</span> 6954*088332b5SXin Li<pre>int luaL_dostring (lua_State *L, const char *str);</pre> 6955*088332b5SXin Li 6956*088332b5SXin Li<p> 6957*088332b5SXin LiLoads and runs the given string. 6958*088332b5SXin LiIt is defined as the following macro: 6959*088332b5SXin Li 6960*088332b5SXin Li<pre> 6961*088332b5SXin Li (luaL_loadstring(L, str) || lua_pcall(L, 0, LUA_MULTRET, 0)) 6962*088332b5SXin Li</pre><p> 6963*088332b5SXin LiIt returns <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> if there are no errors, 6964*088332b5SXin Lior an error code in case of errors (see <a href="#4.4.1">§4.4.1</a>). 6965*088332b5SXin Li 6966*088332b5SXin Li 6967*088332b5SXin Li 6968*088332b5SXin Li 6969*088332b5SXin Li 6970*088332b5SXin Li<hr><h3><a name="luaL_error"><code>luaL_error</code></a></h3><p> 6971*088332b5SXin Li<span class="apii">[-0, +0, <em>v</em>]</span> 6972*088332b5SXin Li<pre>int luaL_error (lua_State *L, const char *fmt, ...);</pre> 6973*088332b5SXin Li 6974*088332b5SXin Li<p> 6975*088332b5SXin LiRaises an error. 6976*088332b5SXin LiThe error message format is given by <code>fmt</code> 6977*088332b5SXin Liplus any extra arguments, 6978*088332b5SXin Lifollowing the same rules of <a href="#lua_pushfstring"><code>lua_pushfstring</code></a>. 6979*088332b5SXin LiIt also adds at the beginning of the message the file name and 6980*088332b5SXin Lithe line number where the error occurred, 6981*088332b5SXin Liif this information is available. 6982*088332b5SXin Li 6983*088332b5SXin Li 6984*088332b5SXin Li<p> 6985*088332b5SXin LiThis function never returns, 6986*088332b5SXin Libut it is an idiom to use it in C functions 6987*088332b5SXin Lias <code>return luaL_error(<em>args</em>)</code>. 6988*088332b5SXin Li 6989*088332b5SXin Li 6990*088332b5SXin Li 6991*088332b5SXin Li 6992*088332b5SXin Li 6993*088332b5SXin Li<hr><h3><a name="luaL_execresult"><code>luaL_execresult</code></a></h3><p> 6994*088332b5SXin Li<span class="apii">[-0, +3, <em>m</em>]</span> 6995*088332b5SXin Li<pre>int luaL_execresult (lua_State *L, int stat);</pre> 6996*088332b5SXin Li 6997*088332b5SXin Li<p> 6998*088332b5SXin LiThis function produces the return values for 6999*088332b5SXin Liprocess-related functions in the standard library 7000*088332b5SXin Li(<a href="#pdf-os.execute"><code>os.execute</code></a> and <a href="#pdf-io.close"><code>io.close</code></a>). 7001*088332b5SXin Li 7002*088332b5SXin Li 7003*088332b5SXin Li 7004*088332b5SXin Li 7005*088332b5SXin Li 7006*088332b5SXin Li<hr><h3><a name="luaL_fileresult"><code>luaL_fileresult</code></a></h3><p> 7007*088332b5SXin Li<span class="apii">[-0, +(1|3), <em>m</em>]</span> 7008*088332b5SXin Li<pre>int luaL_fileresult (lua_State *L, int stat, const char *fname);</pre> 7009*088332b5SXin Li 7010*088332b5SXin Li<p> 7011*088332b5SXin LiThis function produces the return values for 7012*088332b5SXin Lifile-related functions in the standard library 7013*088332b5SXin Li(<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.). 7014*088332b5SXin Li 7015*088332b5SXin Li 7016*088332b5SXin Li 7017*088332b5SXin Li 7018*088332b5SXin Li 7019*088332b5SXin Li<hr><h3><a name="luaL_getmetafield"><code>luaL_getmetafield</code></a></h3><p> 7020*088332b5SXin Li<span class="apii">[-0, +(0|1), <em>m</em>]</span> 7021*088332b5SXin Li<pre>int luaL_getmetafield (lua_State *L, int obj, const char *e);</pre> 7022*088332b5SXin Li 7023*088332b5SXin Li<p> 7024*088332b5SXin LiPushes onto the stack the field <code>e</code> from the metatable 7025*088332b5SXin Liof the object at index <code>obj</code> and returns the type of the pushed value. 7026*088332b5SXin LiIf the object does not have a metatable, 7027*088332b5SXin Lior if the metatable does not have this field, 7028*088332b5SXin Lipushes nothing and returns <code>LUA_TNIL</code>. 7029*088332b5SXin Li 7030*088332b5SXin Li 7031*088332b5SXin Li 7032*088332b5SXin Li 7033*088332b5SXin Li 7034*088332b5SXin Li<hr><h3><a name="luaL_getmetatable"><code>luaL_getmetatable</code></a></h3><p> 7035*088332b5SXin Li<span class="apii">[-0, +1, <em>m</em>]</span> 7036*088332b5SXin Li<pre>int luaL_getmetatable (lua_State *L, const char *tname);</pre> 7037*088332b5SXin Li 7038*088332b5SXin Li<p> 7039*088332b5SXin LiPushes onto the stack the metatable associated with the name <code>tname</code> 7040*088332b5SXin Liin the registry (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>), 7041*088332b5SXin Lior <b>nil</b> if there is no metatable associated with that name. 7042*088332b5SXin LiReturns the type of the pushed value. 7043*088332b5SXin Li 7044*088332b5SXin Li 7045*088332b5SXin Li 7046*088332b5SXin Li 7047*088332b5SXin Li 7048*088332b5SXin Li<hr><h3><a name="luaL_getsubtable"><code>luaL_getsubtable</code></a></h3><p> 7049*088332b5SXin Li<span class="apii">[-0, +1, <em>e</em>]</span> 7050*088332b5SXin Li<pre>int luaL_getsubtable (lua_State *L, int idx, const char *fname);</pre> 7051*088332b5SXin Li 7052*088332b5SXin Li<p> 7053*088332b5SXin LiEnsures that the value <code>t[fname]</code>, 7054*088332b5SXin Liwhere <code>t</code> is the value at index <code>idx</code>, 7055*088332b5SXin Liis a table, 7056*088332b5SXin Liand pushes that table onto the stack. 7057*088332b5SXin LiReturns true if it finds a previous table there 7058*088332b5SXin Liand false if it creates a new table. 7059*088332b5SXin Li 7060*088332b5SXin Li 7061*088332b5SXin Li 7062*088332b5SXin Li 7063*088332b5SXin Li 7064*088332b5SXin Li<hr><h3><a name="luaL_gsub"><code>luaL_gsub</code></a></h3><p> 7065*088332b5SXin Li<span class="apii">[-0, +1, <em>m</em>]</span> 7066*088332b5SXin Li<pre>const char *luaL_gsub (lua_State *L, 7067*088332b5SXin Li const char *s, 7068*088332b5SXin Li const char *p, 7069*088332b5SXin Li const char *r);</pre> 7070*088332b5SXin Li 7071*088332b5SXin Li<p> 7072*088332b5SXin LiCreates a copy of string <code>s</code>, 7073*088332b5SXin Lireplacing any occurrence of the string <code>p</code> 7074*088332b5SXin Liwith the string <code>r</code>. 7075*088332b5SXin LiPushes the resulting string on the stack and returns it. 7076*088332b5SXin Li 7077*088332b5SXin Li 7078*088332b5SXin Li 7079*088332b5SXin Li 7080*088332b5SXin Li 7081*088332b5SXin Li<hr><h3><a name="luaL_len"><code>luaL_len</code></a></h3><p> 7082*088332b5SXin Li<span class="apii">[-0, +0, <em>e</em>]</span> 7083*088332b5SXin Li<pre>lua_Integer luaL_len (lua_State *L, int index);</pre> 7084*088332b5SXin Li 7085*088332b5SXin Li<p> 7086*088332b5SXin LiReturns the "length" of the value at the given index 7087*088332b5SXin Lias a number; 7088*088332b5SXin Liit is equivalent to the '<code>#</code>' operator in Lua (see <a href="#3.4.7">§3.4.7</a>). 7089*088332b5SXin LiRaises an error if the result of the operation is not an integer. 7090*088332b5SXin Li(This case can only happen through metamethods.) 7091*088332b5SXin Li 7092*088332b5SXin Li 7093*088332b5SXin Li 7094*088332b5SXin Li 7095*088332b5SXin Li 7096*088332b5SXin Li<hr><h3><a name="luaL_loadbuffer"><code>luaL_loadbuffer</code></a></h3><p> 7097*088332b5SXin Li<span class="apii">[-0, +1, –]</span> 7098*088332b5SXin Li<pre>int luaL_loadbuffer (lua_State *L, 7099*088332b5SXin Li const char *buff, 7100*088332b5SXin Li size_t sz, 7101*088332b5SXin Li const char *name);</pre> 7102*088332b5SXin Li 7103*088332b5SXin Li<p> 7104*088332b5SXin LiEquivalent to <a href="#luaL_loadbufferx"><code>luaL_loadbufferx</code></a> with <code>mode</code> equal to <code>NULL</code>. 7105*088332b5SXin Li 7106*088332b5SXin Li 7107*088332b5SXin Li 7108*088332b5SXin Li 7109*088332b5SXin Li 7110*088332b5SXin Li<hr><h3><a name="luaL_loadbufferx"><code>luaL_loadbufferx</code></a></h3><p> 7111*088332b5SXin Li<span class="apii">[-0, +1, –]</span> 7112*088332b5SXin Li<pre>int luaL_loadbufferx (lua_State *L, 7113*088332b5SXin Li const char *buff, 7114*088332b5SXin Li size_t sz, 7115*088332b5SXin Li const char *name, 7116*088332b5SXin Li const char *mode);</pre> 7117*088332b5SXin Li 7118*088332b5SXin Li<p> 7119*088332b5SXin LiLoads a buffer as a Lua chunk. 7120*088332b5SXin LiThis function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in the 7121*088332b5SXin Libuffer pointed to by <code>buff</code> with size <code>sz</code>. 7122*088332b5SXin Li 7123*088332b5SXin Li 7124*088332b5SXin Li<p> 7125*088332b5SXin LiThis function returns the same results as <a href="#lua_load"><code>lua_load</code></a>. 7126*088332b5SXin Li<code>name</code> is the chunk name, 7127*088332b5SXin Liused for debug information and error messages. 7128*088332b5SXin LiThe string <code>mode</code> works as in the function <a href="#lua_load"><code>lua_load</code></a>. 7129*088332b5SXin Li 7130*088332b5SXin Li 7131*088332b5SXin Li 7132*088332b5SXin Li 7133*088332b5SXin Li 7134*088332b5SXin Li<hr><h3><a name="luaL_loadfile"><code>luaL_loadfile</code></a></h3><p> 7135*088332b5SXin Li<span class="apii">[-0, +1, <em>m</em>]</span> 7136*088332b5SXin Li<pre>int luaL_loadfile (lua_State *L, const char *filename);</pre> 7137*088332b5SXin Li 7138*088332b5SXin Li<p> 7139*088332b5SXin LiEquivalent to <a href="#luaL_loadfilex"><code>luaL_loadfilex</code></a> with <code>mode</code> equal to <code>NULL</code>. 7140*088332b5SXin Li 7141*088332b5SXin Li 7142*088332b5SXin Li 7143*088332b5SXin Li 7144*088332b5SXin Li 7145*088332b5SXin Li<hr><h3><a name="luaL_loadfilex"><code>luaL_loadfilex</code></a></h3><p> 7146*088332b5SXin Li<span class="apii">[-0, +1, <em>m</em>]</span> 7147*088332b5SXin Li<pre>int luaL_loadfilex (lua_State *L, const char *filename, 7148*088332b5SXin Li const char *mode);</pre> 7149*088332b5SXin Li 7150*088332b5SXin Li<p> 7151*088332b5SXin LiLoads a file as a Lua chunk. 7152*088332b5SXin LiThis function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in the file 7153*088332b5SXin Linamed <code>filename</code>. 7154*088332b5SXin LiIf <code>filename</code> is <code>NULL</code>, 7155*088332b5SXin Lithen it loads from the standard input. 7156*088332b5SXin LiThe first line in the file is ignored if it starts with a <code>#</code>. 7157*088332b5SXin Li 7158*088332b5SXin Li 7159*088332b5SXin Li<p> 7160*088332b5SXin LiThe string <code>mode</code> works as in the function <a href="#lua_load"><code>lua_load</code></a>. 7161*088332b5SXin Li 7162*088332b5SXin Li 7163*088332b5SXin Li<p> 7164*088332b5SXin LiThis function returns the same results as <a href="#lua_load"><code>lua_load</code></a> 7165*088332b5SXin Lior <a href="#pdf-LUA_ERRFILE"><code>LUA_ERRFILE</code></a> for file-related errors. 7166*088332b5SXin Li 7167*088332b5SXin Li 7168*088332b5SXin Li<p> 7169*088332b5SXin LiAs <a href="#lua_load"><code>lua_load</code></a>, this function only loads the chunk; 7170*088332b5SXin Liit does not run it. 7171*088332b5SXin Li 7172*088332b5SXin Li 7173*088332b5SXin Li 7174*088332b5SXin Li 7175*088332b5SXin Li 7176*088332b5SXin Li<hr><h3><a name="luaL_loadstring"><code>luaL_loadstring</code></a></h3><p> 7177*088332b5SXin Li<span class="apii">[-0, +1, –]</span> 7178*088332b5SXin Li<pre>int luaL_loadstring (lua_State *L, const char *s);</pre> 7179*088332b5SXin Li 7180*088332b5SXin Li<p> 7181*088332b5SXin LiLoads a string as a Lua chunk. 7182*088332b5SXin LiThis function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in 7183*088332b5SXin Lithe zero-terminated string <code>s</code>. 7184*088332b5SXin Li 7185*088332b5SXin Li 7186*088332b5SXin Li<p> 7187*088332b5SXin LiThis function returns the same results as <a href="#lua_load"><code>lua_load</code></a>. 7188*088332b5SXin Li 7189*088332b5SXin Li 7190*088332b5SXin Li<p> 7191*088332b5SXin LiAlso as <a href="#lua_load"><code>lua_load</code></a>, this function only loads the chunk; 7192*088332b5SXin Liit does not run it. 7193*088332b5SXin Li 7194*088332b5SXin Li 7195*088332b5SXin Li 7196*088332b5SXin Li 7197*088332b5SXin Li 7198*088332b5SXin Li<hr><h3><a name="luaL_newlib"><code>luaL_newlib</code></a></h3><p> 7199*088332b5SXin Li<span class="apii">[-0, +1, <em>m</em>]</span> 7200*088332b5SXin Li<pre>void luaL_newlib (lua_State *L, const luaL_Reg l[]);</pre> 7201*088332b5SXin Li 7202*088332b5SXin Li<p> 7203*088332b5SXin LiCreates a new table and registers there 7204*088332b5SXin Lithe functions in the list <code>l</code>. 7205*088332b5SXin Li 7206*088332b5SXin Li 7207*088332b5SXin Li<p> 7208*088332b5SXin LiIt is implemented as the following macro: 7209*088332b5SXin Li 7210*088332b5SXin Li<pre> 7211*088332b5SXin Li (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) 7212*088332b5SXin Li</pre><p> 7213*088332b5SXin LiThe array <code>l</code> must be the actual array, 7214*088332b5SXin Linot a pointer to it. 7215*088332b5SXin Li 7216*088332b5SXin Li 7217*088332b5SXin Li 7218*088332b5SXin Li 7219*088332b5SXin Li 7220*088332b5SXin Li<hr><h3><a name="luaL_newlibtable"><code>luaL_newlibtable</code></a></h3><p> 7221*088332b5SXin Li<span class="apii">[-0, +1, <em>m</em>]</span> 7222*088332b5SXin Li<pre>void luaL_newlibtable (lua_State *L, const luaL_Reg l[]);</pre> 7223*088332b5SXin Li 7224*088332b5SXin Li<p> 7225*088332b5SXin LiCreates a new table with a size optimized 7226*088332b5SXin Lito store all entries in the array <code>l</code> 7227*088332b5SXin Li(but does not actually store them). 7228*088332b5SXin LiIt is intended to be used in conjunction with <a href="#luaL_setfuncs"><code>luaL_setfuncs</code></a> 7229*088332b5SXin Li(see <a href="#luaL_newlib"><code>luaL_newlib</code></a>). 7230*088332b5SXin Li 7231*088332b5SXin Li 7232*088332b5SXin Li<p> 7233*088332b5SXin LiIt is implemented as a macro. 7234*088332b5SXin LiThe array <code>l</code> must be the actual array, 7235*088332b5SXin Linot a pointer to it. 7236*088332b5SXin Li 7237*088332b5SXin Li 7238*088332b5SXin Li 7239*088332b5SXin Li 7240*088332b5SXin Li 7241*088332b5SXin Li<hr><h3><a name="luaL_newmetatable"><code>luaL_newmetatable</code></a></h3><p> 7242*088332b5SXin Li<span class="apii">[-0, +1, <em>m</em>]</span> 7243*088332b5SXin Li<pre>int luaL_newmetatable (lua_State *L, const char *tname);</pre> 7244*088332b5SXin Li 7245*088332b5SXin Li<p> 7246*088332b5SXin LiIf the registry already has the key <code>tname</code>, 7247*088332b5SXin Lireturns 0. 7248*088332b5SXin LiOtherwise, 7249*088332b5SXin Licreates a new table to be used as a metatable for userdata, 7250*088332b5SXin Liadds to this new table the pair <code>__name = tname</code>, 7251*088332b5SXin Liadds to the registry the pair <code>[tname] = new table</code>, 7252*088332b5SXin Liand returns 1. 7253*088332b5SXin Li 7254*088332b5SXin Li 7255*088332b5SXin Li<p> 7256*088332b5SXin LiIn both cases, 7257*088332b5SXin Lithe function pushes onto the stack the final value associated 7258*088332b5SXin Liwith <code>tname</code> in the registry. 7259*088332b5SXin Li 7260*088332b5SXin Li 7261*088332b5SXin Li 7262*088332b5SXin Li 7263*088332b5SXin Li 7264*088332b5SXin Li<hr><h3><a name="luaL_newstate"><code>luaL_newstate</code></a></h3><p> 7265*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 7266*088332b5SXin Li<pre>lua_State *luaL_newstate (void);</pre> 7267*088332b5SXin Li 7268*088332b5SXin Li<p> 7269*088332b5SXin LiCreates a new Lua state. 7270*088332b5SXin LiIt calls <a href="#lua_newstate"><code>lua_newstate</code></a> with an 7271*088332b5SXin Liallocator based on the standard C allocation functions 7272*088332b5SXin Liand then sets a warning function and a panic function (see <a href="#4.4">§4.4</a>) 7273*088332b5SXin Lithat print messages to the standard error output. 7274*088332b5SXin Li 7275*088332b5SXin Li 7276*088332b5SXin Li<p> 7277*088332b5SXin LiReturns the new state, 7278*088332b5SXin Lior <code>NULL</code> if there is a memory allocation error. 7279*088332b5SXin Li 7280*088332b5SXin Li 7281*088332b5SXin Li 7282*088332b5SXin Li 7283*088332b5SXin Li 7284*088332b5SXin Li<hr><h3><a name="luaL_openlibs"><code>luaL_openlibs</code></a></h3><p> 7285*088332b5SXin Li<span class="apii">[-0, +0, <em>e</em>]</span> 7286*088332b5SXin Li<pre>void luaL_openlibs (lua_State *L);</pre> 7287*088332b5SXin Li 7288*088332b5SXin Li<p> 7289*088332b5SXin LiOpens all standard Lua libraries into the given state. 7290*088332b5SXin Li 7291*088332b5SXin Li 7292*088332b5SXin Li 7293*088332b5SXin Li 7294*088332b5SXin Li 7295*088332b5SXin Li<hr><h3><a name="luaL_opt"><code>luaL_opt</code></a></h3><p> 7296*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 7297*088332b5SXin Li<pre>T luaL_opt (L, func, arg, dflt);</pre> 7298*088332b5SXin Li 7299*088332b5SXin Li<p> 7300*088332b5SXin LiThis macro is defined as follows: 7301*088332b5SXin Li 7302*088332b5SXin Li<pre> 7303*088332b5SXin Li (lua_isnoneornil(L,(arg)) ? (dflt) : func(L,(arg))) 7304*088332b5SXin Li</pre><p> 7305*088332b5SXin LiIn words, if the argument <code>arg</code> is nil or absent, 7306*088332b5SXin Lithe macro results in the default <code>dflt</code>. 7307*088332b5SXin LiOtherwise, it results in the result of calling <code>func</code> 7308*088332b5SXin Liwith the state <code>L</code> and the argument index <code>arg</code> as 7309*088332b5SXin Liarguments. 7310*088332b5SXin LiNote that it evaluates the expression <code>dflt</code> only if needed. 7311*088332b5SXin Li 7312*088332b5SXin Li 7313*088332b5SXin Li 7314*088332b5SXin Li 7315*088332b5SXin Li 7316*088332b5SXin Li<hr><h3><a name="luaL_optinteger"><code>luaL_optinteger</code></a></h3><p> 7317*088332b5SXin Li<span class="apii">[-0, +0, <em>v</em>]</span> 7318*088332b5SXin Li<pre>lua_Integer luaL_optinteger (lua_State *L, 7319*088332b5SXin Li int arg, 7320*088332b5SXin Li lua_Integer d);</pre> 7321*088332b5SXin Li 7322*088332b5SXin Li<p> 7323*088332b5SXin LiIf the function argument <code>arg</code> is an integer 7324*088332b5SXin Li(or it is convertible to an integer), 7325*088332b5SXin Lireturns this integer. 7326*088332b5SXin LiIf this argument is absent or is <b>nil</b>, 7327*088332b5SXin Lireturns <code>d</code>. 7328*088332b5SXin LiOtherwise, raises an error. 7329*088332b5SXin Li 7330*088332b5SXin Li 7331*088332b5SXin Li 7332*088332b5SXin Li 7333*088332b5SXin Li 7334*088332b5SXin Li<hr><h3><a name="luaL_optlstring"><code>luaL_optlstring</code></a></h3><p> 7335*088332b5SXin Li<span class="apii">[-0, +0, <em>v</em>]</span> 7336*088332b5SXin Li<pre>const char *luaL_optlstring (lua_State *L, 7337*088332b5SXin Li int arg, 7338*088332b5SXin Li const char *d, 7339*088332b5SXin Li size_t *l);</pre> 7340*088332b5SXin Li 7341*088332b5SXin Li<p> 7342*088332b5SXin LiIf the function argument <code>arg</code> is a string, 7343*088332b5SXin Lireturns this string. 7344*088332b5SXin LiIf this argument is absent or is <b>nil</b>, 7345*088332b5SXin Lireturns <code>d</code>. 7346*088332b5SXin LiOtherwise, raises an error. 7347*088332b5SXin Li 7348*088332b5SXin Li 7349*088332b5SXin Li<p> 7350*088332b5SXin LiIf <code>l</code> is not <code>NULL</code>, 7351*088332b5SXin Lifills its referent with the result's length. 7352*088332b5SXin LiIf the result is <code>NULL</code> 7353*088332b5SXin Li(only possible when returning <code>d</code> and <code>d == NULL</code>), 7354*088332b5SXin Liits length is considered zero. 7355*088332b5SXin Li 7356*088332b5SXin Li 7357*088332b5SXin Li<p> 7358*088332b5SXin LiThis function uses <a href="#lua_tolstring"><code>lua_tolstring</code></a> to get its result, 7359*088332b5SXin Liso all conversions and caveats of that function apply here. 7360*088332b5SXin Li 7361*088332b5SXin Li 7362*088332b5SXin Li 7363*088332b5SXin Li 7364*088332b5SXin Li 7365*088332b5SXin Li<hr><h3><a name="luaL_optnumber"><code>luaL_optnumber</code></a></h3><p> 7366*088332b5SXin Li<span class="apii">[-0, +0, <em>v</em>]</span> 7367*088332b5SXin Li<pre>lua_Number luaL_optnumber (lua_State *L, int arg, lua_Number d);</pre> 7368*088332b5SXin Li 7369*088332b5SXin Li<p> 7370*088332b5SXin LiIf the function argument <code>arg</code> is a number, 7371*088332b5SXin Lireturns this number as a <code>lua_Number</code>. 7372*088332b5SXin LiIf this argument is absent or is <b>nil</b>, 7373*088332b5SXin Lireturns <code>d</code>. 7374*088332b5SXin LiOtherwise, raises an error. 7375*088332b5SXin Li 7376*088332b5SXin Li 7377*088332b5SXin Li 7378*088332b5SXin Li 7379*088332b5SXin Li 7380*088332b5SXin Li<hr><h3><a name="luaL_optstring"><code>luaL_optstring</code></a></h3><p> 7381*088332b5SXin Li<span class="apii">[-0, +0, <em>v</em>]</span> 7382*088332b5SXin Li<pre>const char *luaL_optstring (lua_State *L, 7383*088332b5SXin Li int arg, 7384*088332b5SXin Li const char *d);</pre> 7385*088332b5SXin Li 7386*088332b5SXin Li<p> 7387*088332b5SXin LiIf the function argument <code>arg</code> is a string, 7388*088332b5SXin Lireturns this string. 7389*088332b5SXin LiIf this argument is absent or is <b>nil</b>, 7390*088332b5SXin Lireturns <code>d</code>. 7391*088332b5SXin LiOtherwise, raises an error. 7392*088332b5SXin Li 7393*088332b5SXin Li 7394*088332b5SXin Li 7395*088332b5SXin Li 7396*088332b5SXin Li 7397*088332b5SXin Li<hr><h3><a name="luaL_prepbuffer"><code>luaL_prepbuffer</code></a></h3><p> 7398*088332b5SXin Li<span class="apii">[-?, +?, <em>m</em>]</span> 7399*088332b5SXin Li<pre>char *luaL_prepbuffer (luaL_Buffer *B);</pre> 7400*088332b5SXin Li 7401*088332b5SXin Li<p> 7402*088332b5SXin LiEquivalent to <a href="#luaL_prepbuffsize"><code>luaL_prepbuffsize</code></a> 7403*088332b5SXin Liwith the predefined size <a name="pdf-LUAL_BUFFERSIZE"><code>LUAL_BUFFERSIZE</code></a>. 7404*088332b5SXin Li 7405*088332b5SXin Li 7406*088332b5SXin Li 7407*088332b5SXin Li 7408*088332b5SXin Li 7409*088332b5SXin Li<hr><h3><a name="luaL_prepbuffsize"><code>luaL_prepbuffsize</code></a></h3><p> 7410*088332b5SXin Li<span class="apii">[-?, +?, <em>m</em>]</span> 7411*088332b5SXin Li<pre>char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz);</pre> 7412*088332b5SXin Li 7413*088332b5SXin Li<p> 7414*088332b5SXin LiReturns an address to a space of size <code>sz</code> 7415*088332b5SXin Liwhere you can copy a string to be added to buffer <code>B</code> 7416*088332b5SXin Li(see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>). 7417*088332b5SXin LiAfter copying the string into this space you must call 7418*088332b5SXin Li<a href="#luaL_addsize"><code>luaL_addsize</code></a> with the size of the string to actually add 7419*088332b5SXin Liit to the buffer. 7420*088332b5SXin Li 7421*088332b5SXin Li 7422*088332b5SXin Li 7423*088332b5SXin Li 7424*088332b5SXin Li 7425*088332b5SXin Li<hr><h3><a name="luaL_pushfail"><code>luaL_pushfail</code></a></h3><p> 7426*088332b5SXin Li<span class="apii">[-0, +1, –]</span> 7427*088332b5SXin Li<pre>void luaL_pushfail (lua_State *L);</pre> 7428*088332b5SXin Li 7429*088332b5SXin Li<p> 7430*088332b5SXin LiPushes the <b>fail</b> value onto the stack (see <a href="#6">§6</a>). 7431*088332b5SXin Li 7432*088332b5SXin Li 7433*088332b5SXin Li 7434*088332b5SXin Li 7435*088332b5SXin Li 7436*088332b5SXin Li<hr><h3><a name="luaL_pushresult"><code>luaL_pushresult</code></a></h3><p> 7437*088332b5SXin Li<span class="apii">[-?, +1, <em>m</em>]</span> 7438*088332b5SXin Li<pre>void luaL_pushresult (luaL_Buffer *B);</pre> 7439*088332b5SXin Li 7440*088332b5SXin Li<p> 7441*088332b5SXin LiFinishes the use of buffer <code>B</code> leaving the final string on 7442*088332b5SXin Lithe top of the stack. 7443*088332b5SXin Li 7444*088332b5SXin Li 7445*088332b5SXin Li 7446*088332b5SXin Li 7447*088332b5SXin Li 7448*088332b5SXin Li<hr><h3><a name="luaL_pushresultsize"><code>luaL_pushresultsize</code></a></h3><p> 7449*088332b5SXin Li<span class="apii">[-?, +1, <em>m</em>]</span> 7450*088332b5SXin Li<pre>void luaL_pushresultsize (luaL_Buffer *B, size_t sz);</pre> 7451*088332b5SXin Li 7452*088332b5SXin Li<p> 7453*088332b5SXin LiEquivalent to the sequence <a href="#luaL_addsize"><code>luaL_addsize</code></a>, <a href="#luaL_pushresult"><code>luaL_pushresult</code></a>. 7454*088332b5SXin Li 7455*088332b5SXin Li 7456*088332b5SXin Li 7457*088332b5SXin Li 7458*088332b5SXin Li 7459*088332b5SXin Li<hr><h3><a name="luaL_ref"><code>luaL_ref</code></a></h3><p> 7460*088332b5SXin Li<span class="apii">[-1, +0, <em>m</em>]</span> 7461*088332b5SXin Li<pre>int luaL_ref (lua_State *L, int t);</pre> 7462*088332b5SXin Li 7463*088332b5SXin Li<p> 7464*088332b5SXin LiCreates and returns a <em>reference</em>, 7465*088332b5SXin Liin the table at index <code>t</code>, 7466*088332b5SXin Lifor the object on the top of the stack (and pops the object). 7467*088332b5SXin Li 7468*088332b5SXin Li 7469*088332b5SXin Li<p> 7470*088332b5SXin LiA reference is a unique integer key. 7471*088332b5SXin LiAs long as you do not manually add integer keys into the table <code>t</code>, 7472*088332b5SXin Li<a href="#luaL_ref"><code>luaL_ref</code></a> ensures the uniqueness of the key it returns. 7473*088332b5SXin LiYou can retrieve an object referred by the reference <code>r</code> 7474*088332b5SXin Liby calling <code>lua_rawgeti(L, t, r)</code>. 7475*088332b5SXin LiThe function <a href="#luaL_unref"><code>luaL_unref</code></a> frees a reference. 7476*088332b5SXin Li 7477*088332b5SXin Li 7478*088332b5SXin Li<p> 7479*088332b5SXin LiIf the object on the top of the stack is <b>nil</b>, 7480*088332b5SXin Li<a href="#luaL_ref"><code>luaL_ref</code></a> returns the constant <a name="pdf-LUA_REFNIL"><code>LUA_REFNIL</code></a>. 7481*088332b5SXin LiThe constant <a name="pdf-LUA_NOREF"><code>LUA_NOREF</code></a> is guaranteed to be different 7482*088332b5SXin Lifrom any reference returned by <a href="#luaL_ref"><code>luaL_ref</code></a>. 7483*088332b5SXin Li 7484*088332b5SXin Li 7485*088332b5SXin Li 7486*088332b5SXin Li 7487*088332b5SXin Li 7488*088332b5SXin Li<hr><h3><a name="luaL_Reg"><code>luaL_Reg</code></a></h3> 7489*088332b5SXin Li<pre>typedef struct luaL_Reg { 7490*088332b5SXin Li const char *name; 7491*088332b5SXin Li lua_CFunction func; 7492*088332b5SXin Li} luaL_Reg;</pre> 7493*088332b5SXin Li 7494*088332b5SXin Li<p> 7495*088332b5SXin LiType for arrays of functions to be registered by 7496*088332b5SXin Li<a href="#luaL_setfuncs"><code>luaL_setfuncs</code></a>. 7497*088332b5SXin Li<code>name</code> is the function name and <code>func</code> is a pointer to 7498*088332b5SXin Lithe function. 7499*088332b5SXin LiAny array of <a href="#luaL_Reg"><code>luaL_Reg</code></a> must end with a sentinel entry 7500*088332b5SXin Liin which both <code>name</code> and <code>func</code> are <code>NULL</code>. 7501*088332b5SXin Li 7502*088332b5SXin Li 7503*088332b5SXin Li 7504*088332b5SXin Li 7505*088332b5SXin Li 7506*088332b5SXin Li<hr><h3><a name="luaL_requiref"><code>luaL_requiref</code></a></h3><p> 7507*088332b5SXin Li<span class="apii">[-0, +1, <em>e</em>]</span> 7508*088332b5SXin Li<pre>void luaL_requiref (lua_State *L, const char *modname, 7509*088332b5SXin Li lua_CFunction openf, int glb);</pre> 7510*088332b5SXin Li 7511*088332b5SXin Li<p> 7512*088332b5SXin LiIf <code>package.loaded[modname]</code> is not true, 7513*088332b5SXin Licalls the function <code>openf</code> with the string <code>modname</code> as an argument 7514*088332b5SXin Liand sets the call result to <code>package.loaded[modname]</code>, 7515*088332b5SXin Lias if that function has been called through <a href="#pdf-require"><code>require</code></a>. 7516*088332b5SXin Li 7517*088332b5SXin Li 7518*088332b5SXin Li<p> 7519*088332b5SXin LiIf <code>glb</code> is true, 7520*088332b5SXin Lialso stores the module into the global <code>modname</code>. 7521*088332b5SXin Li 7522*088332b5SXin Li 7523*088332b5SXin Li<p> 7524*088332b5SXin LiLeaves a copy of the module on the stack. 7525*088332b5SXin Li 7526*088332b5SXin Li 7527*088332b5SXin Li 7528*088332b5SXin Li 7529*088332b5SXin Li 7530*088332b5SXin Li<hr><h3><a name="luaL_setfuncs"><code>luaL_setfuncs</code></a></h3><p> 7531*088332b5SXin Li<span class="apii">[-nup, +0, <em>m</em>]</span> 7532*088332b5SXin Li<pre>void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup);</pre> 7533*088332b5SXin Li 7534*088332b5SXin Li<p> 7535*088332b5SXin LiRegisters all functions in the array <code>l</code> 7536*088332b5SXin Li(see <a href="#luaL_Reg"><code>luaL_Reg</code></a>) into the table on the top of the stack 7537*088332b5SXin Li(below optional upvalues, see next). 7538*088332b5SXin Li 7539*088332b5SXin Li 7540*088332b5SXin Li<p> 7541*088332b5SXin LiWhen <code>nup</code> is not zero, 7542*088332b5SXin Liall functions are created with <code>nup</code> upvalues, 7543*088332b5SXin Liinitialized with copies of the <code>nup</code> values 7544*088332b5SXin Lipreviously pushed on the stack 7545*088332b5SXin Lion top of the library table. 7546*088332b5SXin LiThese values are popped from the stack after the registration. 7547*088332b5SXin Li 7548*088332b5SXin Li 7549*088332b5SXin Li 7550*088332b5SXin Li 7551*088332b5SXin Li 7552*088332b5SXin Li<hr><h3><a name="luaL_setmetatable"><code>luaL_setmetatable</code></a></h3><p> 7553*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 7554*088332b5SXin Li<pre>void luaL_setmetatable (lua_State *L, const char *tname);</pre> 7555*088332b5SXin Li 7556*088332b5SXin Li<p> 7557*088332b5SXin LiSets the metatable of the object on the top of the stack 7558*088332b5SXin Lias the metatable associated with name <code>tname</code> 7559*088332b5SXin Liin the registry (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>). 7560*088332b5SXin Li 7561*088332b5SXin Li 7562*088332b5SXin Li 7563*088332b5SXin Li 7564*088332b5SXin Li 7565*088332b5SXin Li<hr><h3><a name="luaL_Stream"><code>luaL_Stream</code></a></h3> 7566*088332b5SXin Li<pre>typedef struct luaL_Stream { 7567*088332b5SXin Li FILE *f; 7568*088332b5SXin Li lua_CFunction closef; 7569*088332b5SXin Li} luaL_Stream;</pre> 7570*088332b5SXin Li 7571*088332b5SXin Li<p> 7572*088332b5SXin LiThe standard representation for file handles 7573*088332b5SXin Liused by the standard I/O library. 7574*088332b5SXin Li 7575*088332b5SXin Li 7576*088332b5SXin Li<p> 7577*088332b5SXin LiA file handle is implemented as a full userdata, 7578*088332b5SXin Liwith a metatable called <code>LUA_FILEHANDLE</code> 7579*088332b5SXin Li(where <code>LUA_FILEHANDLE</code> is a macro with the actual metatable's name). 7580*088332b5SXin LiThe metatable is created by the I/O library 7581*088332b5SXin Li(see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>). 7582*088332b5SXin Li 7583*088332b5SXin Li 7584*088332b5SXin Li<p> 7585*088332b5SXin LiThis userdata must start with the structure <code>luaL_Stream</code>; 7586*088332b5SXin Liit can contain other data after this initial structure. 7587*088332b5SXin LiThe field <code>f</code> points to the corresponding C stream 7588*088332b5SXin Li(or it can be <code>NULL</code> to indicate an incompletely created handle). 7589*088332b5SXin LiThe field <code>closef</code> points to a Lua function 7590*088332b5SXin Lithat will be called to close the stream 7591*088332b5SXin Liwhen the handle is closed or collected; 7592*088332b5SXin Lithis function receives the file handle as its sole argument and 7593*088332b5SXin Limust return either a true value, in case of success, 7594*088332b5SXin Lior a false value plus an error message, in case of error. 7595*088332b5SXin LiOnce Lua calls this field, 7596*088332b5SXin Liit changes the field value to <code>NULL</code> 7597*088332b5SXin Lito signal that the handle is closed. 7598*088332b5SXin Li 7599*088332b5SXin Li 7600*088332b5SXin Li 7601*088332b5SXin Li 7602*088332b5SXin Li 7603*088332b5SXin Li<hr><h3><a name="luaL_testudata"><code>luaL_testudata</code></a></h3><p> 7604*088332b5SXin Li<span class="apii">[-0, +0, <em>m</em>]</span> 7605*088332b5SXin Li<pre>void *luaL_testudata (lua_State *L, int arg, const char *tname);</pre> 7606*088332b5SXin Li 7607*088332b5SXin Li<p> 7608*088332b5SXin LiThis function works like <a href="#luaL_checkudata"><code>luaL_checkudata</code></a>, 7609*088332b5SXin Liexcept that, when the test fails, 7610*088332b5SXin Liit returns <code>NULL</code> instead of raising an error. 7611*088332b5SXin Li 7612*088332b5SXin Li 7613*088332b5SXin Li 7614*088332b5SXin Li 7615*088332b5SXin Li 7616*088332b5SXin Li<hr><h3><a name="luaL_tolstring"><code>luaL_tolstring</code></a></h3><p> 7617*088332b5SXin Li<span class="apii">[-0, +1, <em>e</em>]</span> 7618*088332b5SXin Li<pre>const char *luaL_tolstring (lua_State *L, int idx, size_t *len);</pre> 7619*088332b5SXin Li 7620*088332b5SXin Li<p> 7621*088332b5SXin LiConverts any Lua value at the given index to a C string 7622*088332b5SXin Liin a reasonable format. 7623*088332b5SXin LiThe resulting string is pushed onto the stack and also 7624*088332b5SXin Lireturned by the function (see <a href="#4.1.3">§4.1.3</a>). 7625*088332b5SXin LiIf <code>len</code> is not <code>NULL</code>, 7626*088332b5SXin Lithe function also sets <code>*len</code> with the string length. 7627*088332b5SXin Li 7628*088332b5SXin Li 7629*088332b5SXin Li<p> 7630*088332b5SXin LiIf the value has a metatable with a <code>__tostring</code> field, 7631*088332b5SXin Lithen <code>luaL_tolstring</code> calls the corresponding metamethod 7632*088332b5SXin Liwith the value as argument, 7633*088332b5SXin Liand uses the result of the call as its result. 7634*088332b5SXin Li 7635*088332b5SXin Li 7636*088332b5SXin Li 7637*088332b5SXin Li 7638*088332b5SXin Li 7639*088332b5SXin Li<hr><h3><a name="luaL_traceback"><code>luaL_traceback</code></a></h3><p> 7640*088332b5SXin Li<span class="apii">[-0, +1, <em>m</em>]</span> 7641*088332b5SXin Li<pre>void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, 7642*088332b5SXin Li int level);</pre> 7643*088332b5SXin Li 7644*088332b5SXin Li<p> 7645*088332b5SXin LiCreates and pushes a traceback of the stack <code>L1</code>. 7646*088332b5SXin LiIf <code>msg</code> is not <code>NULL</code>, it is appended 7647*088332b5SXin Liat the beginning of the traceback. 7648*088332b5SXin LiThe <code>level</code> parameter tells at which level 7649*088332b5SXin Lito start the traceback. 7650*088332b5SXin Li 7651*088332b5SXin Li 7652*088332b5SXin Li 7653*088332b5SXin Li 7654*088332b5SXin Li 7655*088332b5SXin Li<hr><h3><a name="luaL_typeerror"><code>luaL_typeerror</code></a></h3><p> 7656*088332b5SXin Li<span class="apii">[-0, +0, <em>v</em>]</span> 7657*088332b5SXin Li<pre>const char *luaL_typeerror (lua_State *L, 7658*088332b5SXin Li int arg, 7659*088332b5SXin Li const char *tname);</pre> 7660*088332b5SXin Li 7661*088332b5SXin Li<p> 7662*088332b5SXin LiRaises a type error for the argument <code>arg</code> 7663*088332b5SXin Liof the C function that called it, 7664*088332b5SXin Liusing a standard message; 7665*088332b5SXin Li<code>tname</code> is a "name" for the expected type. 7666*088332b5SXin LiThis function never returns. 7667*088332b5SXin Li 7668*088332b5SXin Li 7669*088332b5SXin Li 7670*088332b5SXin Li 7671*088332b5SXin Li 7672*088332b5SXin Li<hr><h3><a name="luaL_typename"><code>luaL_typename</code></a></h3><p> 7673*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 7674*088332b5SXin Li<pre>const char *luaL_typename (lua_State *L, int index);</pre> 7675*088332b5SXin Li 7676*088332b5SXin Li<p> 7677*088332b5SXin LiReturns the name of the type of the value at the given index. 7678*088332b5SXin Li 7679*088332b5SXin Li 7680*088332b5SXin Li 7681*088332b5SXin Li 7682*088332b5SXin Li 7683*088332b5SXin Li<hr><h3><a name="luaL_unref"><code>luaL_unref</code></a></h3><p> 7684*088332b5SXin Li<span class="apii">[-0, +0, –]</span> 7685*088332b5SXin Li<pre>void luaL_unref (lua_State *L, int t, int ref);</pre> 7686*088332b5SXin Li 7687*088332b5SXin Li<p> 7688*088332b5SXin LiReleases the reference <code>ref</code> from the table at index <code>t</code> 7689*088332b5SXin Li(see <a href="#luaL_ref"><code>luaL_ref</code></a>). 7690*088332b5SXin LiThe entry is removed from the table, 7691*088332b5SXin Liso that the referred object can be collected. 7692*088332b5SXin LiThe reference <code>ref</code> is also freed to be used again. 7693*088332b5SXin Li 7694*088332b5SXin Li 7695*088332b5SXin Li<p> 7696*088332b5SXin LiIf <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>, 7697*088332b5SXin Li<a href="#luaL_unref"><code>luaL_unref</code></a> does nothing. 7698*088332b5SXin Li 7699*088332b5SXin Li 7700*088332b5SXin Li 7701*088332b5SXin Li 7702*088332b5SXin Li 7703*088332b5SXin Li<hr><h3><a name="luaL_where"><code>luaL_where</code></a></h3><p> 7704*088332b5SXin Li<span class="apii">[-0, +1, <em>m</em>]</span> 7705*088332b5SXin Li<pre>void luaL_where (lua_State *L, int lvl);</pre> 7706*088332b5SXin Li 7707*088332b5SXin Li<p> 7708*088332b5SXin LiPushes onto the stack a string identifying the current position 7709*088332b5SXin Liof the control at level <code>lvl</code> in the call stack. 7710*088332b5SXin LiTypically this string has the following format: 7711*088332b5SXin Li 7712*088332b5SXin Li<pre> 7713*088332b5SXin Li <em>chunkname</em>:<em>currentline</em>: 7714*088332b5SXin Li</pre><p> 7715*088332b5SXin LiLevel 0 is the running function, 7716*088332b5SXin Lilevel 1 is the function that called the running function, 7717*088332b5SXin Lietc. 7718*088332b5SXin Li 7719*088332b5SXin Li 7720*088332b5SXin Li<p> 7721*088332b5SXin LiThis function is used to build a prefix for error messages. 7722*088332b5SXin Li 7723*088332b5SXin Li 7724*088332b5SXin Li 7725*088332b5SXin Li 7726*088332b5SXin Li 7727*088332b5SXin Li 7728*088332b5SXin Li 7729*088332b5SXin Li<h1>6 – <a name="6">The Standard Libraries</a></h1> 7730*088332b5SXin Li 7731*088332b5SXin Li 7732*088332b5SXin Li 7733*088332b5SXin Li<p> 7734*088332b5SXin LiThe standard Lua libraries provide useful functions 7735*088332b5SXin Lithat are implemented in C through the C API. 7736*088332b5SXin LiSome of these functions provide essential services to the language 7737*088332b5SXin Li(e.g., <a href="#pdf-type"><code>type</code></a> and <a href="#pdf-getmetatable"><code>getmetatable</code></a>); 7738*088332b5SXin Liothers provide access to outside services (e.g., I/O); 7739*088332b5SXin Liand others could be implemented in Lua itself, 7740*088332b5SXin Libut that for different reasons 7741*088332b5SXin Lideserve an implementation in C (e.g., <a href="#pdf-table.sort"><code>table.sort</code></a>). 7742*088332b5SXin Li 7743*088332b5SXin Li 7744*088332b5SXin Li<p> 7745*088332b5SXin LiAll libraries are implemented through the official C API 7746*088332b5SXin Liand are provided as separate C modules. 7747*088332b5SXin LiUnless otherwise noted, 7748*088332b5SXin Lithese library functions do not adjust its number of arguments 7749*088332b5SXin Lito its expected parameters. 7750*088332b5SXin LiFor instance, a function documented as <code>foo(arg)</code> 7751*088332b5SXin Lishould not be called without an argument. 7752*088332b5SXin Li 7753*088332b5SXin Li 7754*088332b5SXin Li<p> 7755*088332b5SXin LiThe notation <b>fail</b> means a false value representing 7756*088332b5SXin Lisome kind of failure. 7757*088332b5SXin Li(Currently, <b>fail</b> is equal to <b>nil</b>, 7758*088332b5SXin Libut that may change in future versions. 7759*088332b5SXin LiThe recommendation is to always test the success of these functions 7760*088332b5SXin Liwith <code>(not status)</code>, instead of <code>(status == nil)</code>.) 7761*088332b5SXin Li 7762*088332b5SXin Li 7763*088332b5SXin Li<p> 7764*088332b5SXin LiCurrently, Lua has the following standard libraries: 7765*088332b5SXin Li 7766*088332b5SXin Li<ul> 7767*088332b5SXin Li 7768*088332b5SXin Li<li>basic library (<a href="#6.1">§6.1</a>);</li> 7769*088332b5SXin Li 7770*088332b5SXin Li<li>coroutine library (<a href="#6.2">§6.2</a>);</li> 7771*088332b5SXin Li 7772*088332b5SXin Li<li>package library (<a href="#6.3">§6.3</a>);</li> 7773*088332b5SXin Li 7774*088332b5SXin Li<li>string manipulation (<a href="#6.4">§6.4</a>);</li> 7775*088332b5SXin Li 7776*088332b5SXin Li<li>basic UTF-8 support (<a href="#6.5">§6.5</a>);</li> 7777*088332b5SXin Li 7778*088332b5SXin Li<li>table manipulation (<a href="#6.6">§6.6</a>);</li> 7779*088332b5SXin Li 7780*088332b5SXin Li<li>mathematical functions (<a href="#6.7">§6.7</a>) (sin, log, etc.);</li> 7781*088332b5SXin Li 7782*088332b5SXin Li<li>input and output (<a href="#6.8">§6.8</a>);</li> 7783*088332b5SXin Li 7784*088332b5SXin Li<li>operating system facilities (<a href="#6.9">§6.9</a>);</li> 7785*088332b5SXin Li 7786*088332b5SXin Li<li>debug facilities (<a href="#6.10">§6.10</a>).</li> 7787*088332b5SXin Li 7788*088332b5SXin Li</ul><p> 7789*088332b5SXin LiExcept for the basic and the package libraries, 7790*088332b5SXin Lieach library provides all its functions as fields of a global table 7791*088332b5SXin Lior as methods of its objects. 7792*088332b5SXin Li 7793*088332b5SXin Li 7794*088332b5SXin Li<p> 7795*088332b5SXin LiTo have access to these libraries, 7796*088332b5SXin Lithe C host program should call the <a href="#luaL_openlibs"><code>luaL_openlibs</code></a> function, 7797*088332b5SXin Liwhich opens all standard libraries. 7798*088332b5SXin LiAlternatively, 7799*088332b5SXin Lithe host program can open them individually by using 7800*088332b5SXin Li<a href="#luaL_requiref"><code>luaL_requiref</code></a> to call 7801*088332b5SXin Li<a name="pdf-luaopen_base"><code>luaopen_base</code></a> (for the basic library), 7802*088332b5SXin Li<a name="pdf-luaopen_package"><code>luaopen_package</code></a> (for the package library), 7803*088332b5SXin Li<a name="pdf-luaopen_coroutine"><code>luaopen_coroutine</code></a> (for the coroutine library), 7804*088332b5SXin Li<a name="pdf-luaopen_string"><code>luaopen_string</code></a> (for the string library), 7805*088332b5SXin Li<a name="pdf-luaopen_utf8"><code>luaopen_utf8</code></a> (for the UTF-8 library), 7806*088332b5SXin Li<a name="pdf-luaopen_table"><code>luaopen_table</code></a> (for the table library), 7807*088332b5SXin Li<a name="pdf-luaopen_math"><code>luaopen_math</code></a> (for the mathematical library), 7808*088332b5SXin Li<a name="pdf-luaopen_io"><code>luaopen_io</code></a> (for the I/O library), 7809*088332b5SXin Li<a name="pdf-luaopen_os"><code>luaopen_os</code></a> (for the operating system library), 7810*088332b5SXin Liand <a name="pdf-luaopen_debug"><code>luaopen_debug</code></a> (for the debug library). 7811*088332b5SXin LiThese functions are declared in <a name="pdf-lualib.h"><code>lualib.h</code></a>. 7812*088332b5SXin Li 7813*088332b5SXin Li 7814*088332b5SXin Li 7815*088332b5SXin Li 7816*088332b5SXin Li 7817*088332b5SXin Li<h2>6.1 – <a name="6.1">Basic Functions</a></h2> 7818*088332b5SXin Li 7819*088332b5SXin Li<p> 7820*088332b5SXin LiThe basic library provides core functions to Lua. 7821*088332b5SXin LiIf you do not include this library in your application, 7822*088332b5SXin Liyou should check carefully whether you need to provide 7823*088332b5SXin Liimplementations for some of its facilities. 7824*088332b5SXin Li 7825*088332b5SXin Li 7826*088332b5SXin Li<p> 7827*088332b5SXin Li<hr><h3><a name="pdf-assert"><code>assert (v [, message])</code></a></h3> 7828*088332b5SXin Li 7829*088332b5SXin Li 7830*088332b5SXin Li<p> 7831*088332b5SXin LiRaises an error if 7832*088332b5SXin Lithe value of its argument <code>v</code> is false (i.e., <b>nil</b> or <b>false</b>); 7833*088332b5SXin Liotherwise, returns all its arguments. 7834*088332b5SXin LiIn case of error, 7835*088332b5SXin Li<code>message</code> is the error object; 7836*088332b5SXin Liwhen absent, it defaults to "<code>assertion failed!</code>" 7837*088332b5SXin Li 7838*088332b5SXin Li 7839*088332b5SXin Li 7840*088332b5SXin Li 7841*088332b5SXin Li<p> 7842*088332b5SXin Li<hr><h3><a name="pdf-collectgarbage"><code>collectgarbage ([opt [, arg]])</code></a></h3> 7843*088332b5SXin Li 7844*088332b5SXin Li 7845*088332b5SXin Li<p> 7846*088332b5SXin LiThis function is a generic interface to the garbage collector. 7847*088332b5SXin LiIt performs different functions according to its first argument, <code>opt</code>: 7848*088332b5SXin Li 7849*088332b5SXin Li<ul> 7850*088332b5SXin Li 7851*088332b5SXin Li<li><b>"<code>collect</code>": </b> 7852*088332b5SXin LiPerforms a full garbage-collection cycle. 7853*088332b5SXin LiThis is the default option. 7854*088332b5SXin Li</li> 7855*088332b5SXin Li 7856*088332b5SXin Li<li><b>"<code>stop</code>": </b> 7857*088332b5SXin LiStops automatic execution of the garbage collector. 7858*088332b5SXin LiThe collector will run only when explicitly invoked, 7859*088332b5SXin Liuntil a call to restart it. 7860*088332b5SXin Li</li> 7861*088332b5SXin Li 7862*088332b5SXin Li<li><b>"<code>restart</code>": </b> 7863*088332b5SXin LiRestarts automatic execution of the garbage collector. 7864*088332b5SXin Li</li> 7865*088332b5SXin Li 7866*088332b5SXin Li<li><b>"<code>count</code>": </b> 7867*088332b5SXin LiReturns the total memory in use by Lua in Kbytes. 7868*088332b5SXin LiThe value has a fractional part, 7869*088332b5SXin Liso that it multiplied by 1024 7870*088332b5SXin Ligives the exact number of bytes in use by Lua. 7871*088332b5SXin Li</li> 7872*088332b5SXin Li 7873*088332b5SXin Li<li><b>"<code>step</code>": </b> 7874*088332b5SXin LiPerforms a garbage-collection step. 7875*088332b5SXin LiThe step "size" is controlled by <code>arg</code>. 7876*088332b5SXin LiWith a zero value, 7877*088332b5SXin Lithe collector will perform one basic (indivisible) step. 7878*088332b5SXin LiFor non-zero values, 7879*088332b5SXin Lithe collector will perform as if that amount of memory 7880*088332b5SXin Li(in Kbytes) had been allocated by Lua. 7881*088332b5SXin LiReturns <b>true</b> if the step finished a collection cycle. 7882*088332b5SXin Li</li> 7883*088332b5SXin Li 7884*088332b5SXin Li<li><b>"<code>isrunning</code>": </b> 7885*088332b5SXin LiReturns a boolean that tells whether the collector is running 7886*088332b5SXin Li(i.e., not stopped). 7887*088332b5SXin Li</li> 7888*088332b5SXin Li 7889*088332b5SXin Li<li><b>"<code>incremental</code>": </b> 7890*088332b5SXin LiChange the collector mode to incremental. 7891*088332b5SXin LiThis option can be followed by three numbers: 7892*088332b5SXin Lithe garbage-collector pause, 7893*088332b5SXin Lithe step multiplier, 7894*088332b5SXin Liand the step size (see <a href="#2.5.1">§2.5.1</a>). 7895*088332b5SXin LiA zero means to not change that value. 7896*088332b5SXin Li</li> 7897*088332b5SXin Li 7898*088332b5SXin Li<li><b>"<code>generational</code>": </b> 7899*088332b5SXin LiChange the collector mode to generational. 7900*088332b5SXin LiThis option can be followed by two numbers: 7901*088332b5SXin Lithe garbage-collector minor multiplier 7902*088332b5SXin Liand the major multiplier (see <a href="#2.5.2">§2.5.2</a>). 7903*088332b5SXin LiA zero means to not change that value. 7904*088332b5SXin Li</li> 7905*088332b5SXin Li 7906*088332b5SXin Li</ul><p> 7907*088332b5SXin LiSee <a href="#2.5">§2.5</a> for more details about garbage collection 7908*088332b5SXin Liand some of these options. 7909*088332b5SXin Li 7910*088332b5SXin Li 7911*088332b5SXin Li 7912*088332b5SXin Li 7913*088332b5SXin Li<p> 7914*088332b5SXin Li<hr><h3><a name="pdf-dofile"><code>dofile ([filename])</code></a></h3> 7915*088332b5SXin LiOpens the named file and executes its content as a Lua chunk. 7916*088332b5SXin LiWhen called without arguments, 7917*088332b5SXin Li<code>dofile</code> executes the content of the standard input (<code>stdin</code>). 7918*088332b5SXin LiReturns all values returned by the chunk. 7919*088332b5SXin LiIn case of errors, <code>dofile</code> propagates the error 7920*088332b5SXin Lito its caller. 7921*088332b5SXin Li(That is, <code>dofile</code> does not run in protected mode.) 7922*088332b5SXin Li 7923*088332b5SXin Li 7924*088332b5SXin Li 7925*088332b5SXin Li 7926*088332b5SXin Li<p> 7927*088332b5SXin Li<hr><h3><a name="pdf-error"><code>error (message [, level])</code></a></h3> 7928*088332b5SXin LiRaises an error (see <a href="#2.3">§2.3</a>) with @{message} as the error object. 7929*088332b5SXin LiThis function never returns. 7930*088332b5SXin Li 7931*088332b5SXin Li 7932*088332b5SXin Li<p> 7933*088332b5SXin LiUsually, <code>error</code> adds some information about the error position 7934*088332b5SXin Liat the beginning of the message, if the message is a string. 7935*088332b5SXin LiThe <code>level</code> argument specifies how to get the error position. 7936*088332b5SXin LiWith level 1 (the default), the error position is where the 7937*088332b5SXin Li<code>error</code> function was called. 7938*088332b5SXin LiLevel 2 points the error to where the function 7939*088332b5SXin Lithat called <code>error</code> was called; and so on. 7940*088332b5SXin LiPassing a level 0 avoids the addition of error position information 7941*088332b5SXin Lito the message. 7942*088332b5SXin Li 7943*088332b5SXin Li 7944*088332b5SXin Li 7945*088332b5SXin Li 7946*088332b5SXin Li<p> 7947*088332b5SXin Li<hr><h3><a name="pdf-_G"><code>_G</code></a></h3> 7948*088332b5SXin LiA global variable (not a function) that 7949*088332b5SXin Liholds the global environment (see <a href="#2.2">§2.2</a>). 7950*088332b5SXin LiLua itself does not use this variable; 7951*088332b5SXin Lichanging its value does not affect any environment, 7952*088332b5SXin Linor vice versa. 7953*088332b5SXin Li 7954*088332b5SXin Li 7955*088332b5SXin Li 7956*088332b5SXin Li 7957*088332b5SXin Li<p> 7958*088332b5SXin Li<hr><h3><a name="pdf-getmetatable"><code>getmetatable (object)</code></a></h3> 7959*088332b5SXin Li 7960*088332b5SXin Li 7961*088332b5SXin Li<p> 7962*088332b5SXin LiIf <code>object</code> does not have a metatable, returns <b>nil</b>. 7963*088332b5SXin LiOtherwise, 7964*088332b5SXin Liif the object's metatable has a <code>__metatable</code> field, 7965*088332b5SXin Lireturns the associated value. 7966*088332b5SXin LiOtherwise, returns the metatable of the given object. 7967*088332b5SXin Li 7968*088332b5SXin Li 7969*088332b5SXin Li 7970*088332b5SXin Li 7971*088332b5SXin Li<p> 7972*088332b5SXin Li<hr><h3><a name="pdf-ipairs"><code>ipairs (t)</code></a></h3> 7973*088332b5SXin Li 7974*088332b5SXin Li 7975*088332b5SXin Li<p> 7976*088332b5SXin LiReturns three values (an iterator function, the table <code>t</code>, and 0) 7977*088332b5SXin Liso that the construction 7978*088332b5SXin Li 7979*088332b5SXin Li<pre> 7980*088332b5SXin Li for i,v in ipairs(t) do <em>body</em> end 7981*088332b5SXin Li</pre><p> 7982*088332b5SXin Liwill iterate over the key–value pairs 7983*088332b5SXin Li(<code>1,t[1]</code>), (<code>2,t[2]</code>), ..., 7984*088332b5SXin Liup to the first absent index. 7985*088332b5SXin Li 7986*088332b5SXin Li 7987*088332b5SXin Li 7988*088332b5SXin Li 7989*088332b5SXin Li<p> 7990*088332b5SXin Li<hr><h3><a name="pdf-load"><code>load (chunk [, chunkname [, mode [, env]]])</code></a></h3> 7991*088332b5SXin Li 7992*088332b5SXin Li 7993*088332b5SXin Li<p> 7994*088332b5SXin LiLoads a chunk. 7995*088332b5SXin Li 7996*088332b5SXin Li 7997*088332b5SXin Li<p> 7998*088332b5SXin LiIf <code>chunk</code> is a string, the chunk is this string. 7999*088332b5SXin LiIf <code>chunk</code> is a function, 8000*088332b5SXin Li<code>load</code> calls it repeatedly to get the chunk pieces. 8001*088332b5SXin LiEach call to <code>chunk</code> must return a string that concatenates 8002*088332b5SXin Liwith previous results. 8003*088332b5SXin LiA return of an empty string, <b>nil</b>, or no value signals the end of the chunk. 8004*088332b5SXin Li 8005*088332b5SXin Li 8006*088332b5SXin Li<p> 8007*088332b5SXin LiIf there are no syntactic errors, 8008*088332b5SXin Li<code>load</code> returns the compiled chunk as a function; 8009*088332b5SXin Liotherwise, it returns <b>fail</b> plus the error message. 8010*088332b5SXin Li 8011*088332b5SXin Li 8012*088332b5SXin Li<p> 8013*088332b5SXin LiWhen you load a main chunk, 8014*088332b5SXin Lithe resulting function will always have exactly one upvalue, 8015*088332b5SXin Lithe <code>_ENV</code> variable (see <a href="#2.2">§2.2</a>). 8016*088332b5SXin LiHowever, 8017*088332b5SXin Liwhen you load a binary chunk created from a function (see <a href="#pdf-string.dump"><code>string.dump</code></a>), 8018*088332b5SXin Lithe resulting function can have an arbitrary number of upvalues, 8019*088332b5SXin Liand there is no guarantee that its first upvalue will be 8020*088332b5SXin Lithe <code>_ENV</code> variable. 8021*088332b5SXin Li(A non-main function may not even have an <code>_ENV</code> upvalue.) 8022*088332b5SXin Li 8023*088332b5SXin Li 8024*088332b5SXin Li<p> 8025*088332b5SXin LiRegardless, if the resulting function has any upvalues, 8026*088332b5SXin Liits first upvalue is set to the value of <code>env</code>, 8027*088332b5SXin Liif that parameter is given, 8028*088332b5SXin Lior to the value of the global environment. 8029*088332b5SXin LiOther upvalues are initialized with <b>nil</b>. 8030*088332b5SXin LiAll upvalues are fresh, that is, 8031*088332b5SXin Lithey are not shared with any other function. 8032*088332b5SXin Li 8033*088332b5SXin Li 8034*088332b5SXin Li<p> 8035*088332b5SXin Li<code>chunkname</code> is used as the name of the chunk for error messages 8036*088332b5SXin Liand debug information (see <a href="#4.7">§4.7</a>). 8037*088332b5SXin LiWhen absent, 8038*088332b5SXin Liit defaults to <code>chunk</code>, if <code>chunk</code> is a string, 8039*088332b5SXin Lior to "<code>=(load)</code>" otherwise. 8040*088332b5SXin Li 8041*088332b5SXin Li 8042*088332b5SXin Li<p> 8043*088332b5SXin LiThe string <code>mode</code> controls whether the chunk can be text or binary 8044*088332b5SXin Li(that is, a precompiled chunk). 8045*088332b5SXin LiIt may be the string "<code>b</code>" (only binary chunks), 8046*088332b5SXin Li"<code>t</code>" (only text chunks), 8047*088332b5SXin Lior "<code>bt</code>" (both binary and text). 8048*088332b5SXin LiThe default is "<code>bt</code>". 8049*088332b5SXin Li 8050*088332b5SXin Li 8051*088332b5SXin Li<p> 8052*088332b5SXin LiIt is safe to load malformed binary chunks; 8053*088332b5SXin Li<code>load</code> signals an appropriate error. 8054*088332b5SXin LiHowever, 8055*088332b5SXin LiLua does not check the consistency of the code inside binary chunks; 8056*088332b5SXin Lirunning maliciously crafted bytecode can crash the interpreter. 8057*088332b5SXin Li 8058*088332b5SXin Li 8059*088332b5SXin Li 8060*088332b5SXin Li 8061*088332b5SXin Li<p> 8062*088332b5SXin Li<hr><h3><a name="pdf-loadfile"><code>loadfile ([filename [, mode [, env]]])</code></a></h3> 8063*088332b5SXin Li 8064*088332b5SXin Li 8065*088332b5SXin Li<p> 8066*088332b5SXin LiSimilar to <a href="#pdf-load"><code>load</code></a>, 8067*088332b5SXin Libut gets the chunk from file <code>filename</code> 8068*088332b5SXin Lior from the standard input, 8069*088332b5SXin Liif no file name is given. 8070*088332b5SXin Li 8071*088332b5SXin Li 8072*088332b5SXin Li 8073*088332b5SXin Li 8074*088332b5SXin Li<p> 8075*088332b5SXin Li<hr><h3><a name="pdf-next"><code>next (table [, index])</code></a></h3> 8076*088332b5SXin Li 8077*088332b5SXin Li 8078*088332b5SXin Li<p> 8079*088332b5SXin LiAllows a program to traverse all fields of a table. 8080*088332b5SXin LiIts first argument is a table and its second argument 8081*088332b5SXin Liis an index in this table. 8082*088332b5SXin LiA call to <code>next</code> returns the next index of the table 8083*088332b5SXin Liand its associated value. 8084*088332b5SXin LiWhen called with <b>nil</b> as its second argument, 8085*088332b5SXin Li<code>next</code> returns an initial index 8086*088332b5SXin Liand its associated value. 8087*088332b5SXin LiWhen called with the last index, 8088*088332b5SXin Lior with <b>nil</b> in an empty table, 8089*088332b5SXin Li<code>next</code> returns <b>nil</b>. 8090*088332b5SXin LiIf the second argument is absent, then it is interpreted as <b>nil</b>. 8091*088332b5SXin LiIn particular, 8092*088332b5SXin Liyou can use <code>next(t)</code> to check whether a table is empty. 8093*088332b5SXin Li 8094*088332b5SXin Li 8095*088332b5SXin Li<p> 8096*088332b5SXin LiThe order in which the indices are enumerated is not specified, 8097*088332b5SXin Li<em>even for numeric indices</em>. 8098*088332b5SXin Li(To traverse a table in numerical order, 8099*088332b5SXin Liuse a numerical <b>for</b>.) 8100*088332b5SXin Li 8101*088332b5SXin Li 8102*088332b5SXin Li<p> 8103*088332b5SXin LiThe behavior of <code>next</code> is undefined if, 8104*088332b5SXin Liduring the traversal, 8105*088332b5SXin Liyou assign any value to a non-existent field in the table. 8106*088332b5SXin LiYou may however modify existing fields. 8107*088332b5SXin LiIn particular, you may set existing fields to nil. 8108*088332b5SXin Li 8109*088332b5SXin Li 8110*088332b5SXin Li 8111*088332b5SXin Li 8112*088332b5SXin Li<p> 8113*088332b5SXin Li<hr><h3><a name="pdf-pairs"><code>pairs (t)</code></a></h3> 8114*088332b5SXin Li 8115*088332b5SXin Li 8116*088332b5SXin Li<p> 8117*088332b5SXin LiIf <code>t</code> has a metamethod <code>__pairs</code>, 8118*088332b5SXin Licalls it with <code>t</code> as argument and returns the first three 8119*088332b5SXin Liresults from the call. 8120*088332b5SXin Li 8121*088332b5SXin Li 8122*088332b5SXin Li<p> 8123*088332b5SXin LiOtherwise, 8124*088332b5SXin Lireturns three values: the <a href="#pdf-next"><code>next</code></a> function, the table <code>t</code>, and <b>nil</b>, 8125*088332b5SXin Liso that the construction 8126*088332b5SXin Li 8127*088332b5SXin Li<pre> 8128*088332b5SXin Li for k,v in pairs(t) do <em>body</em> end 8129*088332b5SXin Li</pre><p> 8130*088332b5SXin Liwill iterate over all key–value pairs of table <code>t</code>. 8131*088332b5SXin Li 8132*088332b5SXin Li 8133*088332b5SXin Li<p> 8134*088332b5SXin LiSee function <a href="#pdf-next"><code>next</code></a> for the caveats of modifying 8135*088332b5SXin Lithe table during its traversal. 8136*088332b5SXin Li 8137*088332b5SXin Li 8138*088332b5SXin Li 8139*088332b5SXin Li 8140*088332b5SXin Li<p> 8141*088332b5SXin Li<hr><h3><a name="pdf-pcall"><code>pcall (f [, arg1, ···])</code></a></h3> 8142*088332b5SXin Li 8143*088332b5SXin Li 8144*088332b5SXin Li<p> 8145*088332b5SXin LiCalls the function <code>f</code> with 8146*088332b5SXin Lithe given arguments in <em>protected mode</em>. 8147*088332b5SXin LiThis means that any error inside <code>f</code> is not propagated; 8148*088332b5SXin Liinstead, <code>pcall</code> catches the error 8149*088332b5SXin Liand returns a status code. 8150*088332b5SXin LiIts first result is the status code (a boolean), 8151*088332b5SXin Liwhich is true if the call succeeds without errors. 8152*088332b5SXin LiIn such case, <code>pcall</code> also returns all results from the call, 8153*088332b5SXin Liafter this first result. 8154*088332b5SXin LiIn case of any error, <code>pcall</code> returns <b>false</b> plus the error object. 8155*088332b5SXin LiNote that errors caught by <code>pcall</code> do not call a message handler. 8156*088332b5SXin Li 8157*088332b5SXin Li 8158*088332b5SXin Li 8159*088332b5SXin Li 8160*088332b5SXin Li<p> 8161*088332b5SXin Li<hr><h3><a name="pdf-print"><code>print (···)</code></a></h3> 8162*088332b5SXin LiReceives any number of arguments 8163*088332b5SXin Liand prints their values to <code>stdout</code>, 8164*088332b5SXin Liconverting each argument to a string 8165*088332b5SXin Lifollowing the same rules of <a href="#pdf-tostring"><code>tostring</code></a>. 8166*088332b5SXin Li 8167*088332b5SXin Li 8168*088332b5SXin Li<p> 8169*088332b5SXin LiThe function <code>print</code> is not intended for formatted output, 8170*088332b5SXin Libut only as a quick way to show a value, 8171*088332b5SXin Lifor instance for debugging. 8172*088332b5SXin LiFor complete control over the output, 8173*088332b5SXin Liuse <a href="#pdf-string.format"><code>string.format</code></a> and <a href="#pdf-io.write"><code>io.write</code></a>. 8174*088332b5SXin Li 8175*088332b5SXin Li 8176*088332b5SXin Li 8177*088332b5SXin Li 8178*088332b5SXin Li<p> 8179*088332b5SXin Li<hr><h3><a name="pdf-rawequal"><code>rawequal (v1, v2)</code></a></h3> 8180*088332b5SXin LiChecks whether <code>v1</code> is equal to <code>v2</code>, 8181*088332b5SXin Liwithout invoking the <code>__eq</code> metamethod. 8182*088332b5SXin LiReturns a boolean. 8183*088332b5SXin Li 8184*088332b5SXin Li 8185*088332b5SXin Li 8186*088332b5SXin Li 8187*088332b5SXin Li<p> 8188*088332b5SXin Li<hr><h3><a name="pdf-rawget"><code>rawget (table, index)</code></a></h3> 8189*088332b5SXin LiGets the real value of <code>table[index]</code>, 8190*088332b5SXin Liwithout using the <code>__index</code> metavalue. 8191*088332b5SXin Li<code>table</code> must be a table; 8192*088332b5SXin Li<code>index</code> may be any value. 8193*088332b5SXin Li 8194*088332b5SXin Li 8195*088332b5SXin Li 8196*088332b5SXin Li 8197*088332b5SXin Li<p> 8198*088332b5SXin Li<hr><h3><a name="pdf-rawlen"><code>rawlen (v)</code></a></h3> 8199*088332b5SXin LiReturns the length of the object <code>v</code>, 8200*088332b5SXin Liwhich must be a table or a string, 8201*088332b5SXin Liwithout invoking the <code>__len</code> metamethod. 8202*088332b5SXin LiReturns an integer. 8203*088332b5SXin Li 8204*088332b5SXin Li 8205*088332b5SXin Li 8206*088332b5SXin Li 8207*088332b5SXin Li<p> 8208*088332b5SXin Li<hr><h3><a name="pdf-rawset"><code>rawset (table, index, value)</code></a></h3> 8209*088332b5SXin LiSets the real value of <code>table[index]</code> to <code>value</code>, 8210*088332b5SXin Liwithout using the <code>__newindex</code> metavalue. 8211*088332b5SXin Li<code>table</code> must be a table, 8212*088332b5SXin Li<code>index</code> any value different from <b>nil</b> and NaN, 8213*088332b5SXin Liand <code>value</code> any Lua value. 8214*088332b5SXin Li 8215*088332b5SXin Li 8216*088332b5SXin Li<p> 8217*088332b5SXin LiThis function returns <code>table</code>. 8218*088332b5SXin Li 8219*088332b5SXin Li 8220*088332b5SXin Li 8221*088332b5SXin Li 8222*088332b5SXin Li<p> 8223*088332b5SXin Li<hr><h3><a name="pdf-select"><code>select (index, ···)</code></a></h3> 8224*088332b5SXin Li 8225*088332b5SXin Li 8226*088332b5SXin Li<p> 8227*088332b5SXin LiIf <code>index</code> is a number, 8228*088332b5SXin Lireturns all arguments after argument number <code>index</code>; 8229*088332b5SXin Lia negative number indexes from the end (-1 is the last argument). 8230*088332b5SXin LiOtherwise, <code>index</code> must be the string <code>"#"</code>, 8231*088332b5SXin Liand <code>select</code> returns the total number of extra arguments it received. 8232*088332b5SXin Li 8233*088332b5SXin Li 8234*088332b5SXin Li 8235*088332b5SXin Li 8236*088332b5SXin Li<p> 8237*088332b5SXin Li<hr><h3><a name="pdf-setmetatable"><code>setmetatable (table, metatable)</code></a></h3> 8238*088332b5SXin Li 8239*088332b5SXin Li 8240*088332b5SXin Li<p> 8241*088332b5SXin LiSets the metatable for the given table. 8242*088332b5SXin LiIf <code>metatable</code> is <b>nil</b>, 8243*088332b5SXin Liremoves the metatable of the given table. 8244*088332b5SXin LiIf the original metatable has a <code>__metatable</code> field, 8245*088332b5SXin Liraises an error. 8246*088332b5SXin Li 8247*088332b5SXin Li 8248*088332b5SXin Li<p> 8249*088332b5SXin LiThis function returns <code>table</code>. 8250*088332b5SXin Li 8251*088332b5SXin Li 8252*088332b5SXin Li<p> 8253*088332b5SXin LiTo change the metatable of other types from Lua code, 8254*088332b5SXin Liyou must use the debug library (<a href="#6.10">§6.10</a>). 8255*088332b5SXin Li 8256*088332b5SXin Li 8257*088332b5SXin Li 8258*088332b5SXin Li 8259*088332b5SXin Li<p> 8260*088332b5SXin Li<hr><h3><a name="pdf-tonumber"><code>tonumber (e [, base])</code></a></h3> 8261*088332b5SXin Li 8262*088332b5SXin Li 8263*088332b5SXin Li<p> 8264*088332b5SXin LiWhen called with no <code>base</code>, 8265*088332b5SXin Li<code>tonumber</code> tries to convert its argument to a number. 8266*088332b5SXin LiIf the argument is already a number or 8267*088332b5SXin Lia string convertible to a number, 8268*088332b5SXin Lithen <code>tonumber</code> returns this number; 8269*088332b5SXin Liotherwise, it returns <b>fail</b>. 8270*088332b5SXin Li 8271*088332b5SXin Li 8272*088332b5SXin Li<p> 8273*088332b5SXin LiThe conversion of strings can result in integers or floats, 8274*088332b5SXin Liaccording to the lexical conventions of Lua (see <a href="#3.1">§3.1</a>). 8275*088332b5SXin LiThe string may have leading and trailing spaces and a sign. 8276*088332b5SXin Li 8277*088332b5SXin Li 8278*088332b5SXin Li<p> 8279*088332b5SXin LiWhen called with <code>base</code>, 8280*088332b5SXin Lithen <code>e</code> must be a string to be interpreted as 8281*088332b5SXin Lian integer numeral in that base. 8282*088332b5SXin LiThe base may be any integer between 2 and 36, inclusive. 8283*088332b5SXin LiIn bases above 10, the letter '<code>A</code>' (in either upper or lower case) 8284*088332b5SXin Lirepresents 10, '<code>B</code>' represents 11, and so forth, 8285*088332b5SXin Liwith '<code>Z</code>' representing 35. 8286*088332b5SXin LiIf the string <code>e</code> is not a valid numeral in the given base, 8287*088332b5SXin Lithe function returns <b>fail</b>. 8288*088332b5SXin Li 8289*088332b5SXin Li 8290*088332b5SXin Li 8291*088332b5SXin Li 8292*088332b5SXin Li<p> 8293*088332b5SXin Li<hr><h3><a name="pdf-tostring"><code>tostring (v)</code></a></h3> 8294*088332b5SXin Li 8295*088332b5SXin Li 8296*088332b5SXin Li<p> 8297*088332b5SXin LiReceives a value of any type and 8298*088332b5SXin Liconverts it to a string in a human-readable format. 8299*088332b5SXin Li 8300*088332b5SXin Li 8301*088332b5SXin Li<p> 8302*088332b5SXin LiIf the metatable of <code>v</code> has a <code>__tostring</code> field, 8303*088332b5SXin Lithen <code>tostring</code> calls the corresponding value 8304*088332b5SXin Liwith <code>v</code> as argument, 8305*088332b5SXin Liand uses the result of the call as its result. 8306*088332b5SXin LiOtherwise, if the metatable of <code>v</code> has a <code>__name</code> field 8307*088332b5SXin Liwith a string value, 8308*088332b5SXin Li<code>tostring</code> may use that string in its final result. 8309*088332b5SXin Li 8310*088332b5SXin Li 8311*088332b5SXin Li<p> 8312*088332b5SXin LiFor complete control of how numbers are converted, 8313*088332b5SXin Liuse <a href="#pdf-string.format"><code>string.format</code></a>. 8314*088332b5SXin Li 8315*088332b5SXin Li 8316*088332b5SXin Li 8317*088332b5SXin Li 8318*088332b5SXin Li<p> 8319*088332b5SXin Li<hr><h3><a name="pdf-type"><code>type (v)</code></a></h3> 8320*088332b5SXin Li 8321*088332b5SXin Li 8322*088332b5SXin Li<p> 8323*088332b5SXin LiReturns the type of its only argument, coded as a string. 8324*088332b5SXin LiThe possible results of this function are 8325*088332b5SXin Li"<code>nil</code>" (a string, not the value <b>nil</b>), 8326*088332b5SXin Li"<code>number</code>", 8327*088332b5SXin Li"<code>string</code>", 8328*088332b5SXin Li"<code>boolean</code>", 8329*088332b5SXin Li"<code>table</code>", 8330*088332b5SXin Li"<code>function</code>", 8331*088332b5SXin Li"<code>thread</code>", 8332*088332b5SXin Liand "<code>userdata</code>". 8333*088332b5SXin Li 8334*088332b5SXin Li 8335*088332b5SXin Li 8336*088332b5SXin Li 8337*088332b5SXin Li<p> 8338*088332b5SXin Li<hr><h3><a name="pdf-_VERSION"><code>_VERSION</code></a></h3> 8339*088332b5SXin Li 8340*088332b5SXin Li 8341*088332b5SXin Li<p> 8342*088332b5SXin LiA global variable (not a function) that 8343*088332b5SXin Liholds a string containing the running Lua version. 8344*088332b5SXin LiThe current value of this variable is "<code>Lua 5.4</code>". 8345*088332b5SXin Li 8346*088332b5SXin Li 8347*088332b5SXin Li 8348*088332b5SXin Li 8349*088332b5SXin Li<p> 8350*088332b5SXin Li<hr><h3><a name="pdf-warn"><code>warn (msg1, ···)</code></a></h3> 8351*088332b5SXin Li 8352*088332b5SXin Li 8353*088332b5SXin Li<p> 8354*088332b5SXin LiEmits a warning with a message composed by the concatenation 8355*088332b5SXin Liof all its arguments (which should be strings). 8356*088332b5SXin Li 8357*088332b5SXin Li 8358*088332b5SXin Li<p> 8359*088332b5SXin LiBy convention, 8360*088332b5SXin Lia one-piece message starting with '<code>@</code>' 8361*088332b5SXin Liis intended to be a <em>control message</em>, 8362*088332b5SXin Liwhich is a message to the warning system itself. 8363*088332b5SXin LiIn particular, the standard warning function in Lua 8364*088332b5SXin Lirecognizes the control messages "<code>@off</code>", 8365*088332b5SXin Lito stop the emission of warnings, 8366*088332b5SXin Liand "<code>@on</code>", to (re)start the emission; 8367*088332b5SXin Liit ignores unknown control messages. 8368*088332b5SXin Li 8369*088332b5SXin Li 8370*088332b5SXin Li 8371*088332b5SXin Li 8372*088332b5SXin Li<p> 8373*088332b5SXin Li<hr><h3><a name="pdf-xpcall"><code>xpcall (f, msgh [, arg1, ···])</code></a></h3> 8374*088332b5SXin Li 8375*088332b5SXin Li 8376*088332b5SXin Li<p> 8377*088332b5SXin LiThis function is similar to <a href="#pdf-pcall"><code>pcall</code></a>, 8378*088332b5SXin Liexcept that it sets a new message handler <code>msgh</code>. 8379*088332b5SXin Li 8380*088332b5SXin Li 8381*088332b5SXin Li 8382*088332b5SXin Li 8383*088332b5SXin Li 8384*088332b5SXin Li 8385*088332b5SXin Li 8386*088332b5SXin Li<h2>6.2 – <a name="6.2">Coroutine Manipulation</a></h2> 8387*088332b5SXin Li 8388*088332b5SXin Li<p> 8389*088332b5SXin LiThis library comprises the operations to manipulate coroutines, 8390*088332b5SXin Liwhich come inside the table <a name="pdf-coroutine"><code>coroutine</code></a>. 8391*088332b5SXin LiSee <a href="#2.6">§2.6</a> for a general description of coroutines. 8392*088332b5SXin Li 8393*088332b5SXin Li 8394*088332b5SXin Li<p> 8395*088332b5SXin Li<hr><h3><a name="pdf-coroutine.close"><code>coroutine.close (co)</code></a></h3> 8396*088332b5SXin Li 8397*088332b5SXin Li 8398*088332b5SXin Li<p> 8399*088332b5SXin LiCloses coroutine <code>co</code>, 8400*088332b5SXin Lithat is, 8401*088332b5SXin Licloses all its pending to-be-closed variables 8402*088332b5SXin Liand puts the coroutine in a dead state. 8403*088332b5SXin LiThe given coroutine must be dead or suspended. 8404*088332b5SXin LiIn case of error closing some variable, 8405*088332b5SXin Lireturns <b>false</b> plus the error object; 8406*088332b5SXin Liotherwise returns <b>true</b>. 8407*088332b5SXin Li 8408*088332b5SXin Li 8409*088332b5SXin Li 8410*088332b5SXin Li 8411*088332b5SXin Li<p> 8412*088332b5SXin Li<hr><h3><a name="pdf-coroutine.create"><code>coroutine.create (f)</code></a></h3> 8413*088332b5SXin Li 8414*088332b5SXin Li 8415*088332b5SXin Li<p> 8416*088332b5SXin LiCreates a new coroutine, with body <code>f</code>. 8417*088332b5SXin Li<code>f</code> must be a function. 8418*088332b5SXin LiReturns this new coroutine, 8419*088332b5SXin Lian object with type <code>"thread"</code>. 8420*088332b5SXin Li 8421*088332b5SXin Li 8422*088332b5SXin Li 8423*088332b5SXin Li 8424*088332b5SXin Li<p> 8425*088332b5SXin Li<hr><h3><a name="pdf-coroutine.isyieldable"><code>coroutine.isyieldable ([co])</code></a></h3> 8426*088332b5SXin Li 8427*088332b5SXin Li 8428*088332b5SXin Li<p> 8429*088332b5SXin LiReturns true when the coroutine <code>co</code> can yield. 8430*088332b5SXin LiThe default for <code>co</code> is the running coroutine. 8431*088332b5SXin Li 8432*088332b5SXin Li 8433*088332b5SXin Li<p> 8434*088332b5SXin LiA coroutine is yieldable if it is not the main thread and 8435*088332b5SXin Liit is not inside a non-yieldable C function. 8436*088332b5SXin Li 8437*088332b5SXin Li 8438*088332b5SXin Li 8439*088332b5SXin Li 8440*088332b5SXin Li<p> 8441*088332b5SXin Li<hr><h3><a name="pdf-coroutine.resume"><code>coroutine.resume (co [, val1, ···])</code></a></h3> 8442*088332b5SXin Li 8443*088332b5SXin Li 8444*088332b5SXin Li<p> 8445*088332b5SXin LiStarts or continues the execution of coroutine <code>co</code>. 8446*088332b5SXin LiThe first time you resume a coroutine, 8447*088332b5SXin Liit starts running its body. 8448*088332b5SXin LiThe values <code>val1</code>, ... are passed 8449*088332b5SXin Lias the arguments to the body function. 8450*088332b5SXin LiIf the coroutine has yielded, 8451*088332b5SXin Li<code>resume</code> restarts it; 8452*088332b5SXin Lithe values <code>val1</code>, ... are passed 8453*088332b5SXin Lias the results from the yield. 8454*088332b5SXin Li 8455*088332b5SXin Li 8456*088332b5SXin Li<p> 8457*088332b5SXin LiIf the coroutine runs without any errors, 8458*088332b5SXin Li<code>resume</code> returns <b>true</b> plus any values passed to <code>yield</code> 8459*088332b5SXin Li(when the coroutine yields) or any values returned by the body function 8460*088332b5SXin Li(when the coroutine terminates). 8461*088332b5SXin LiIf there is any error, 8462*088332b5SXin Li<code>resume</code> returns <b>false</b> plus the error message. 8463*088332b5SXin Li 8464*088332b5SXin Li 8465*088332b5SXin Li 8466*088332b5SXin Li 8467*088332b5SXin Li<p> 8468*088332b5SXin Li<hr><h3><a name="pdf-coroutine.running"><code>coroutine.running ()</code></a></h3> 8469*088332b5SXin Li 8470*088332b5SXin Li 8471*088332b5SXin Li<p> 8472*088332b5SXin LiReturns the running coroutine plus a boolean, 8473*088332b5SXin Litrue when the running coroutine is the main one. 8474*088332b5SXin Li 8475*088332b5SXin Li 8476*088332b5SXin Li 8477*088332b5SXin Li 8478*088332b5SXin Li<p> 8479*088332b5SXin Li<hr><h3><a name="pdf-coroutine.status"><code>coroutine.status (co)</code></a></h3> 8480*088332b5SXin Li 8481*088332b5SXin Li 8482*088332b5SXin Li<p> 8483*088332b5SXin LiReturns the status of the coroutine <code>co</code>, as a string: 8484*088332b5SXin Li<code>"running"</code>, 8485*088332b5SXin Liif the coroutine is running 8486*088332b5SXin Li(that is, it is the one that called <code>status</code>); 8487*088332b5SXin Li<code>"suspended"</code>, if the coroutine is suspended in a call to <code>yield</code>, 8488*088332b5SXin Lior if it has not started running yet; 8489*088332b5SXin Li<code>"normal"</code> if the coroutine is active but not running 8490*088332b5SXin Li(that is, it has resumed another coroutine); 8491*088332b5SXin Liand <code>"dead"</code> if the coroutine has finished its body function, 8492*088332b5SXin Lior if it has stopped with an error. 8493*088332b5SXin Li 8494*088332b5SXin Li 8495*088332b5SXin Li 8496*088332b5SXin Li 8497*088332b5SXin Li<p> 8498*088332b5SXin Li<hr><h3><a name="pdf-coroutine.wrap"><code>coroutine.wrap (f)</code></a></h3> 8499*088332b5SXin Li 8500*088332b5SXin Li 8501*088332b5SXin Li<p> 8502*088332b5SXin LiCreates a new coroutine, with body <code>f</code>; 8503*088332b5SXin Li<code>f</code> must be a function. 8504*088332b5SXin LiReturns a function that resumes the coroutine each time it is called. 8505*088332b5SXin LiAny arguments passed to this function behave as the 8506*088332b5SXin Liextra arguments to <code>resume</code>. 8507*088332b5SXin LiThe function returns the same values returned by <code>resume</code>, 8508*088332b5SXin Liexcept the first boolean. 8509*088332b5SXin LiIn case of error, 8510*088332b5SXin Lithe function closes the coroutine and propagates the error. 8511*088332b5SXin Li 8512*088332b5SXin Li 8513*088332b5SXin Li 8514*088332b5SXin Li 8515*088332b5SXin Li<p> 8516*088332b5SXin Li<hr><h3><a name="pdf-coroutine.yield"><code>coroutine.yield (···)</code></a></h3> 8517*088332b5SXin Li 8518*088332b5SXin Li 8519*088332b5SXin Li<p> 8520*088332b5SXin LiSuspends the execution of the calling coroutine. 8521*088332b5SXin LiAny arguments to <code>yield</code> are passed as extra results to <code>resume</code>. 8522*088332b5SXin Li 8523*088332b5SXin Li 8524*088332b5SXin Li 8525*088332b5SXin Li 8526*088332b5SXin Li 8527*088332b5SXin Li 8528*088332b5SXin Li 8529*088332b5SXin Li<h2>6.3 – <a name="6.3">Modules</a></h2> 8530*088332b5SXin Li 8531*088332b5SXin Li<p> 8532*088332b5SXin LiThe package library provides basic 8533*088332b5SXin Lifacilities for loading modules in Lua. 8534*088332b5SXin LiIt exports one function directly in the global environment: 8535*088332b5SXin Li<a href="#pdf-require"><code>require</code></a>. 8536*088332b5SXin LiEverything else is exported in the table <a name="pdf-package"><code>package</code></a>. 8537*088332b5SXin Li 8538*088332b5SXin Li 8539*088332b5SXin Li<p> 8540*088332b5SXin Li<hr><h3><a name="pdf-require"><code>require (modname)</code></a></h3> 8541*088332b5SXin Li 8542*088332b5SXin Li 8543*088332b5SXin Li<p> 8544*088332b5SXin LiLoads the given module. 8545*088332b5SXin LiThe function starts by looking into the <a href="#pdf-package.loaded"><code>package.loaded</code></a> table 8546*088332b5SXin Lito determine whether <code>modname</code> is already loaded. 8547*088332b5SXin LiIf it is, then <code>require</code> returns the value stored 8548*088332b5SXin Liat <code>package.loaded[modname]</code>. 8549*088332b5SXin Li(The absence of a second result in this case 8550*088332b5SXin Lisignals that this call did not have to load the module.) 8551*088332b5SXin LiOtherwise, it tries to find a <em>loader</em> for the module. 8552*088332b5SXin Li 8553*088332b5SXin Li 8554*088332b5SXin Li<p> 8555*088332b5SXin LiTo find a loader, 8556*088332b5SXin Li<code>require</code> is guided by the table <a href="#pdf-package.searchers"><code>package.searchers</code></a>. 8557*088332b5SXin LiEach item in this table is a search function, 8558*088332b5SXin Lithat searches for the module in a particular way. 8559*088332b5SXin LiBy changing this table, 8560*088332b5SXin Liwe can change how <code>require</code> looks for a module. 8561*088332b5SXin LiThe following explanation is based on the default configuration 8562*088332b5SXin Lifor <a href="#pdf-package.searchers"><code>package.searchers</code></a>. 8563*088332b5SXin Li 8564*088332b5SXin Li 8565*088332b5SXin Li<p> 8566*088332b5SXin LiFirst <code>require</code> queries <code>package.preload[modname]</code>. 8567*088332b5SXin LiIf it has a value, 8568*088332b5SXin Lithis value (which must be a function) is the loader. 8569*088332b5SXin LiOtherwise <code>require</code> searches for a Lua loader using the 8570*088332b5SXin Lipath stored in <a href="#pdf-package.path"><code>package.path</code></a>. 8571*088332b5SXin LiIf that also fails, it searches for a C loader using the 8572*088332b5SXin Lipath stored in <a href="#pdf-package.cpath"><code>package.cpath</code></a>. 8573*088332b5SXin LiIf that also fails, 8574*088332b5SXin Liit tries an <em>all-in-one</em> loader (see <a href="#pdf-package.searchers"><code>package.searchers</code></a>). 8575*088332b5SXin Li 8576*088332b5SXin Li 8577*088332b5SXin Li<p> 8578*088332b5SXin LiOnce a loader is found, 8579*088332b5SXin Li<code>require</code> calls the loader with two arguments: 8580*088332b5SXin Li<code>modname</code> and an extra value, 8581*088332b5SXin Lia <em>loader data</em>, 8582*088332b5SXin Lialso returned by the searcher. 8583*088332b5SXin LiThe loader data can be any value useful to the module; 8584*088332b5SXin Lifor the default searchers, 8585*088332b5SXin Liit indicates where the loader was found. 8586*088332b5SXin Li(For instance, if the loader came from a file, 8587*088332b5SXin Lithis extra value is the file path.) 8588*088332b5SXin LiIf the loader returns any non-nil value, 8589*088332b5SXin Li<code>require</code> assigns the returned value to <code>package.loaded[modname]</code>. 8590*088332b5SXin LiIf the loader does not return a non-nil value and 8591*088332b5SXin Lihas not assigned any value to <code>package.loaded[modname]</code>, 8592*088332b5SXin Lithen <code>require</code> assigns <b>true</b> to this entry. 8593*088332b5SXin LiIn any case, <code>require</code> returns the 8594*088332b5SXin Lifinal value of <code>package.loaded[modname]</code>. 8595*088332b5SXin LiBesides that value, <code>require</code> also returns as a second result 8596*088332b5SXin Lithe loader data returned by the searcher, 8597*088332b5SXin Liwhich indicates how <code>require</code> found the module. 8598*088332b5SXin Li 8599*088332b5SXin Li 8600*088332b5SXin Li<p> 8601*088332b5SXin LiIf there is any error loading or running the module, 8602*088332b5SXin Lior if it cannot find any loader for the module, 8603*088332b5SXin Lithen <code>require</code> raises an error. 8604*088332b5SXin Li 8605*088332b5SXin Li 8606*088332b5SXin Li 8607*088332b5SXin Li 8608*088332b5SXin Li<p> 8609*088332b5SXin Li<hr><h3><a name="pdf-package.config"><code>package.config</code></a></h3> 8610*088332b5SXin Li 8611*088332b5SXin Li 8612*088332b5SXin Li<p> 8613*088332b5SXin LiA string describing some compile-time configurations for packages. 8614*088332b5SXin LiThis string is a sequence of lines: 8615*088332b5SXin Li 8616*088332b5SXin Li<ul> 8617*088332b5SXin Li 8618*088332b5SXin Li<li>The first line is the directory separator string. 8619*088332b5SXin LiDefault is '<code>\</code>' for Windows and '<code>/</code>' for all other systems.</li> 8620*088332b5SXin Li 8621*088332b5SXin Li<li>The second line is the character that separates templates in a path. 8622*088332b5SXin LiDefault is '<code>;</code>'.</li> 8623*088332b5SXin Li 8624*088332b5SXin Li<li>The third line is the string that marks the 8625*088332b5SXin Lisubstitution points in a template. 8626*088332b5SXin LiDefault is '<code>?</code>'.</li> 8627*088332b5SXin Li 8628*088332b5SXin Li<li>The fourth line is a string that, in a path in Windows, 8629*088332b5SXin Liis replaced by the executable's directory. 8630*088332b5SXin LiDefault is '<code>!</code>'.</li> 8631*088332b5SXin Li 8632*088332b5SXin Li<li>The fifth line is a mark to ignore all text after it 8633*088332b5SXin Liwhen building the <code>luaopen_</code> function name. 8634*088332b5SXin LiDefault is '<code>-</code>'.</li> 8635*088332b5SXin Li 8636*088332b5SXin Li</ul> 8637*088332b5SXin Li 8638*088332b5SXin Li 8639*088332b5SXin Li 8640*088332b5SXin Li<p> 8641*088332b5SXin Li<hr><h3><a name="pdf-package.cpath"><code>package.cpath</code></a></h3> 8642*088332b5SXin Li 8643*088332b5SXin Li 8644*088332b5SXin Li<p> 8645*088332b5SXin LiA string with the path used by <a href="#pdf-require"><code>require</code></a> 8646*088332b5SXin Lito search for a C loader. 8647*088332b5SXin Li 8648*088332b5SXin Li 8649*088332b5SXin Li<p> 8650*088332b5SXin LiLua initializes the C path <a href="#pdf-package.cpath"><code>package.cpath</code></a> in the same way 8651*088332b5SXin Liit initializes the Lua path <a href="#pdf-package.path"><code>package.path</code></a>, 8652*088332b5SXin Liusing the environment variable <a name="pdf-LUA_CPATH_5_4"><code>LUA_CPATH_5_4</code></a>, 8653*088332b5SXin Lior the environment variable <a name="pdf-LUA_CPATH"><code>LUA_CPATH</code></a>, 8654*088332b5SXin Lior a default path defined in <code>luaconf.h</code>. 8655*088332b5SXin Li 8656*088332b5SXin Li 8657*088332b5SXin Li 8658*088332b5SXin Li 8659*088332b5SXin Li<p> 8660*088332b5SXin Li<hr><h3><a name="pdf-package.loaded"><code>package.loaded</code></a></h3> 8661*088332b5SXin Li 8662*088332b5SXin Li 8663*088332b5SXin Li<p> 8664*088332b5SXin LiA table used by <a href="#pdf-require"><code>require</code></a> to control which 8665*088332b5SXin Limodules are already loaded. 8666*088332b5SXin LiWhen you require a module <code>modname</code> and 8667*088332b5SXin Li<code>package.loaded[modname]</code> is not false, 8668*088332b5SXin Li<a href="#pdf-require"><code>require</code></a> simply returns the value stored there. 8669*088332b5SXin Li 8670*088332b5SXin Li 8671*088332b5SXin Li<p> 8672*088332b5SXin LiThis variable is only a reference to the real table; 8673*088332b5SXin Liassignments to this variable do not change the 8674*088332b5SXin Litable used by <a href="#pdf-require"><code>require</code></a>. 8675*088332b5SXin Li 8676*088332b5SXin Li 8677*088332b5SXin Li 8678*088332b5SXin Li 8679*088332b5SXin Li<p> 8680*088332b5SXin Li<hr><h3><a name="pdf-package.loadlib"><code>package.loadlib (libname, funcname)</code></a></h3> 8681*088332b5SXin Li 8682*088332b5SXin Li 8683*088332b5SXin Li<p> 8684*088332b5SXin LiDynamically links the host program with the C library <code>libname</code>. 8685*088332b5SXin Li 8686*088332b5SXin Li 8687*088332b5SXin Li<p> 8688*088332b5SXin LiIf <code>funcname</code> is "<code>*</code>", 8689*088332b5SXin Lithen it only links with the library, 8690*088332b5SXin Limaking the symbols exported by the library 8691*088332b5SXin Liavailable to other dynamically linked libraries. 8692*088332b5SXin LiOtherwise, 8693*088332b5SXin Liit looks for a function <code>funcname</code> inside the library 8694*088332b5SXin Liand returns this function as a C function. 8695*088332b5SXin LiSo, <code>funcname</code> must follow the <a href="#lua_CFunction"><code>lua_CFunction</code></a> prototype 8696*088332b5SXin Li(see <a href="#lua_CFunction"><code>lua_CFunction</code></a>). 8697*088332b5SXin Li 8698*088332b5SXin Li 8699*088332b5SXin Li<p> 8700*088332b5SXin LiThis is a low-level function. 8701*088332b5SXin LiIt completely bypasses the package and module system. 8702*088332b5SXin LiUnlike <a href="#pdf-require"><code>require</code></a>, 8703*088332b5SXin Liit does not perform any path searching and 8704*088332b5SXin Lidoes not automatically adds extensions. 8705*088332b5SXin Li<code>libname</code> must be the complete file name of the C library, 8706*088332b5SXin Liincluding if necessary a path and an extension. 8707*088332b5SXin Li<code>funcname</code> must be the exact name exported by the C library 8708*088332b5SXin Li(which may depend on the C compiler and linker used). 8709*088332b5SXin Li 8710*088332b5SXin Li 8711*088332b5SXin Li<p> 8712*088332b5SXin LiThis function is not supported by Standard C. 8713*088332b5SXin LiAs such, it is only available on some platforms 8714*088332b5SXin Li(Windows, Linux, Mac OS X, Solaris, BSD, 8715*088332b5SXin Liplus other Unix systems that support the <code>dlfcn</code> standard). 8716*088332b5SXin Li 8717*088332b5SXin Li 8718*088332b5SXin Li<p> 8719*088332b5SXin LiThis function is inherently insecure, 8720*088332b5SXin Lias it allows Lua to call any function in any readable dynamic 8721*088332b5SXin Lilibrary in the system. 8722*088332b5SXin Li(Lua calls any function assuming the function 8723*088332b5SXin Lihas a proper prototype and respects a proper protocol 8724*088332b5SXin Li(see <a href="#lua_CFunction"><code>lua_CFunction</code></a>). 8725*088332b5SXin LiTherefore, 8726*088332b5SXin Licalling an arbitrary function in an arbitrary dynamic library 8727*088332b5SXin Limore often than not results in an access violation.) 8728*088332b5SXin Li 8729*088332b5SXin Li 8730*088332b5SXin Li 8731*088332b5SXin Li 8732*088332b5SXin Li<p> 8733*088332b5SXin Li<hr><h3><a name="pdf-package.path"><code>package.path</code></a></h3> 8734*088332b5SXin Li 8735*088332b5SXin Li 8736*088332b5SXin Li<p> 8737*088332b5SXin LiA string with the path used by <a href="#pdf-require"><code>require</code></a> 8738*088332b5SXin Lito search for a Lua loader. 8739*088332b5SXin Li 8740*088332b5SXin Li 8741*088332b5SXin Li<p> 8742*088332b5SXin LiAt start-up, Lua initializes this variable with 8743*088332b5SXin Lithe value of the environment variable <a name="pdf-LUA_PATH_5_4"><code>LUA_PATH_5_4</code></a> or 8744*088332b5SXin Lithe environment variable <a name="pdf-LUA_PATH"><code>LUA_PATH</code></a> or 8745*088332b5SXin Liwith a default path defined in <code>luaconf.h</code>, 8746*088332b5SXin Liif those environment variables are not defined. 8747*088332b5SXin LiA "<code>;;</code>" in the value of the environment variable 8748*088332b5SXin Liis replaced by the default path. 8749*088332b5SXin Li 8750*088332b5SXin Li 8751*088332b5SXin Li 8752*088332b5SXin Li 8753*088332b5SXin Li<p> 8754*088332b5SXin Li<hr><h3><a name="pdf-package.preload"><code>package.preload</code></a></h3> 8755*088332b5SXin Li 8756*088332b5SXin Li 8757*088332b5SXin Li<p> 8758*088332b5SXin LiA table to store loaders for specific modules 8759*088332b5SXin Li(see <a href="#pdf-require"><code>require</code></a>). 8760*088332b5SXin Li 8761*088332b5SXin Li 8762*088332b5SXin Li<p> 8763*088332b5SXin LiThis variable is only a reference to the real table; 8764*088332b5SXin Liassignments to this variable do not change the 8765*088332b5SXin Litable used by <a href="#pdf-require"><code>require</code></a>. 8766*088332b5SXin Li 8767*088332b5SXin Li 8768*088332b5SXin Li 8769*088332b5SXin Li 8770*088332b5SXin Li<p> 8771*088332b5SXin Li<hr><h3><a name="pdf-package.searchers"><code>package.searchers</code></a></h3> 8772*088332b5SXin Li 8773*088332b5SXin Li 8774*088332b5SXin Li<p> 8775*088332b5SXin LiA table used by <a href="#pdf-require"><code>require</code></a> to control how to find modules. 8776*088332b5SXin Li 8777*088332b5SXin Li 8778*088332b5SXin Li<p> 8779*088332b5SXin LiEach entry in this table is a <em>searcher function</em>. 8780*088332b5SXin LiWhen looking for a module, 8781*088332b5SXin Li<a href="#pdf-require"><code>require</code></a> calls each of these searchers in ascending order, 8782*088332b5SXin Liwith the module name (the argument given to <a href="#pdf-require"><code>require</code></a>) as its 8783*088332b5SXin Lisole argument. 8784*088332b5SXin LiIf the searcher finds the module, 8785*088332b5SXin Liit returns another function, the module <em>loader</em>, 8786*088332b5SXin Liplus an extra value, a <em>loader data</em>, 8787*088332b5SXin Lithat will be passed to that loader and 8788*088332b5SXin Lireturned as a second result by <a href="#pdf-require"><code>require</code></a>. 8789*088332b5SXin LiIf it cannot find the module, 8790*088332b5SXin Liit returns a string explaining why 8791*088332b5SXin Li(or <b>nil</b> if it has nothing to say). 8792*088332b5SXin Li 8793*088332b5SXin Li 8794*088332b5SXin Li<p> 8795*088332b5SXin LiLua initializes this table with four searcher functions. 8796*088332b5SXin Li 8797*088332b5SXin Li 8798*088332b5SXin Li<p> 8799*088332b5SXin LiThe first searcher simply looks for a loader in the 8800*088332b5SXin Li<a href="#pdf-package.preload"><code>package.preload</code></a> table. 8801*088332b5SXin Li 8802*088332b5SXin Li 8803*088332b5SXin Li<p> 8804*088332b5SXin LiThe second searcher looks for a loader as a Lua library, 8805*088332b5SXin Liusing the path stored at <a href="#pdf-package.path"><code>package.path</code></a>. 8806*088332b5SXin LiThe search is done as described in function <a href="#pdf-package.searchpath"><code>package.searchpath</code></a>. 8807*088332b5SXin Li 8808*088332b5SXin Li 8809*088332b5SXin Li<p> 8810*088332b5SXin LiThe third searcher looks for a loader as a C library, 8811*088332b5SXin Liusing the path given by the variable <a href="#pdf-package.cpath"><code>package.cpath</code></a>. 8812*088332b5SXin LiAgain, 8813*088332b5SXin Lithe search is done as described in function <a href="#pdf-package.searchpath"><code>package.searchpath</code></a>. 8814*088332b5SXin LiFor instance, 8815*088332b5SXin Liif the C path is the string 8816*088332b5SXin Li 8817*088332b5SXin Li<pre> 8818*088332b5SXin Li "./?.so;./?.dll;/usr/local/?/init.so" 8819*088332b5SXin Li</pre><p> 8820*088332b5SXin Lithe searcher for module <code>foo</code> 8821*088332b5SXin Liwill try to open the files <code>./foo.so</code>, <code>./foo.dll</code>, 8822*088332b5SXin Liand <code>/usr/local/foo/init.so</code>, in that order. 8823*088332b5SXin LiOnce it finds a C library, 8824*088332b5SXin Lithis searcher first uses a dynamic link facility to link the 8825*088332b5SXin Liapplication with the library. 8826*088332b5SXin LiThen it tries to find a C function inside the library to 8827*088332b5SXin Libe used as the loader. 8828*088332b5SXin LiThe name of this C function is the string "<code>luaopen_</code>" 8829*088332b5SXin Liconcatenated with a copy of the module name where each dot 8830*088332b5SXin Liis replaced by an underscore. 8831*088332b5SXin LiMoreover, if the module name has a hyphen, 8832*088332b5SXin Liits suffix after (and including) the first hyphen is removed. 8833*088332b5SXin LiFor instance, if the module name is <code>a.b.c-v2.1</code>, 8834*088332b5SXin Lithe function name will be <code>luaopen_a_b_c</code>. 8835*088332b5SXin Li 8836*088332b5SXin Li 8837*088332b5SXin Li<p> 8838*088332b5SXin LiThe fourth searcher tries an <em>all-in-one loader</em>. 8839*088332b5SXin LiIt searches the C path for a library for 8840*088332b5SXin Lithe root name of the given module. 8841*088332b5SXin LiFor instance, when requiring <code>a.b.c</code>, 8842*088332b5SXin Liit will search for a C library for <code>a</code>. 8843*088332b5SXin LiIf found, it looks into it for an open function for 8844*088332b5SXin Lithe submodule; 8845*088332b5SXin Liin our example, that would be <code>luaopen_a_b_c</code>. 8846*088332b5SXin LiWith this facility, a package can pack several C submodules 8847*088332b5SXin Liinto one single library, 8848*088332b5SXin Liwith each submodule keeping its original open function. 8849*088332b5SXin Li 8850*088332b5SXin Li 8851*088332b5SXin Li<p> 8852*088332b5SXin LiAll searchers except the first one (preload) return as the extra value 8853*088332b5SXin Lithe file path where the module was found, 8854*088332b5SXin Lias returned by <a href="#pdf-package.searchpath"><code>package.searchpath</code></a>. 8855*088332b5SXin LiThe first searcher always returns the string "<code>:preload:</code>". 8856*088332b5SXin Li 8857*088332b5SXin Li 8858*088332b5SXin Li<p> 8859*088332b5SXin LiSearchers should raise no errors and have no side effects in Lua. 8860*088332b5SXin Li(They may have side effects in C, 8861*088332b5SXin Lifor instance by linking the application with a library.) 8862*088332b5SXin Li 8863*088332b5SXin Li 8864*088332b5SXin Li 8865*088332b5SXin Li 8866*088332b5SXin Li<p> 8867*088332b5SXin Li<hr><h3><a name="pdf-package.searchpath"><code>package.searchpath (name, path [, sep [, rep]])</code></a></h3> 8868*088332b5SXin Li 8869*088332b5SXin Li 8870*088332b5SXin Li<p> 8871*088332b5SXin LiSearches for the given <code>name</code> in the given <code>path</code>. 8872*088332b5SXin Li 8873*088332b5SXin Li 8874*088332b5SXin Li<p> 8875*088332b5SXin LiA path is a string containing a sequence of 8876*088332b5SXin Li<em>templates</em> separated by semicolons. 8877*088332b5SXin LiFor each template, 8878*088332b5SXin Lithe function replaces each interrogation mark (if any) 8879*088332b5SXin Liin the template with a copy of <code>name</code> 8880*088332b5SXin Liwherein all occurrences of <code>sep</code> 8881*088332b5SXin Li(a dot, by default) 8882*088332b5SXin Liwere replaced by <code>rep</code> 8883*088332b5SXin Li(the system's directory separator, by default), 8884*088332b5SXin Liand then tries to open the resulting file name. 8885*088332b5SXin Li 8886*088332b5SXin Li 8887*088332b5SXin Li<p> 8888*088332b5SXin LiFor instance, if the path is the string 8889*088332b5SXin Li 8890*088332b5SXin Li<pre> 8891*088332b5SXin Li "./?.lua;./?.lc;/usr/local/?/init.lua" 8892*088332b5SXin Li</pre><p> 8893*088332b5SXin Lithe search for the name <code>foo.a</code> 8894*088332b5SXin Liwill try to open the files 8895*088332b5SXin Li<code>./foo/a.lua</code>, <code>./foo/a.lc</code>, and 8896*088332b5SXin Li<code>/usr/local/foo/a/init.lua</code>, in that order. 8897*088332b5SXin Li 8898*088332b5SXin Li 8899*088332b5SXin Li<p> 8900*088332b5SXin LiReturns the resulting name of the first file that it can 8901*088332b5SXin Liopen in read mode (after closing the file), 8902*088332b5SXin Lior <b>fail</b> plus an error message if none succeeds. 8903*088332b5SXin Li(This error message lists all file names it tried to open.) 8904*088332b5SXin Li 8905*088332b5SXin Li 8906*088332b5SXin Li 8907*088332b5SXin Li 8908*088332b5SXin Li 8909*088332b5SXin Li 8910*088332b5SXin Li 8911*088332b5SXin Li<h2>6.4 – <a name="6.4">String Manipulation</a></h2> 8912*088332b5SXin Li 8913*088332b5SXin Li 8914*088332b5SXin Li 8915*088332b5SXin Li<p> 8916*088332b5SXin LiThis library provides generic functions for string manipulation, 8917*088332b5SXin Lisuch as finding and extracting substrings, and pattern matching. 8918*088332b5SXin LiWhen indexing a string in Lua, the first character is at position 1 8919*088332b5SXin Li(not at 0, as in C). 8920*088332b5SXin LiIndices are allowed to be negative and are interpreted as indexing backwards, 8921*088332b5SXin Lifrom the end of the string. 8922*088332b5SXin LiThus, the last character is at position -1, and so on. 8923*088332b5SXin Li 8924*088332b5SXin Li 8925*088332b5SXin Li<p> 8926*088332b5SXin LiThe string library provides all its functions inside the table 8927*088332b5SXin Li<a name="pdf-string"><code>string</code></a>. 8928*088332b5SXin LiIt also sets a metatable for strings 8929*088332b5SXin Liwhere the <code>__index</code> field points to the <code>string</code> table. 8930*088332b5SXin LiTherefore, you can use the string functions in object-oriented style. 8931*088332b5SXin LiFor instance, <code>string.byte(s,i)</code> 8932*088332b5SXin Lican be written as <code>s:byte(i)</code>. 8933*088332b5SXin Li 8934*088332b5SXin Li 8935*088332b5SXin Li<p> 8936*088332b5SXin LiThe string library assumes one-byte character encodings. 8937*088332b5SXin Li 8938*088332b5SXin Li 8939*088332b5SXin Li<p> 8940*088332b5SXin Li<hr><h3><a name="pdf-string.byte"><code>string.byte (s [, i [, j]])</code></a></h3> 8941*088332b5SXin LiReturns the internal numeric codes of the characters <code>s[i]</code>, 8942*088332b5SXin Li<code>s[i+1]</code>, ..., <code>s[j]</code>. 8943*088332b5SXin LiThe default value for <code>i</code> is 1; 8944*088332b5SXin Lithe default value for <code>j</code> is <code>i</code>. 8945*088332b5SXin LiThese indices are corrected 8946*088332b5SXin Lifollowing the same rules of function <a href="#pdf-string.sub"><code>string.sub</code></a>. 8947*088332b5SXin Li 8948*088332b5SXin Li 8949*088332b5SXin Li<p> 8950*088332b5SXin LiNumeric codes are not necessarily portable across platforms. 8951*088332b5SXin Li 8952*088332b5SXin Li 8953*088332b5SXin Li 8954*088332b5SXin Li 8955*088332b5SXin Li<p> 8956*088332b5SXin Li<hr><h3><a name="pdf-string.char"><code>string.char (···)</code></a></h3> 8957*088332b5SXin LiReceives zero or more integers. 8958*088332b5SXin LiReturns a string with length equal to the number of arguments, 8959*088332b5SXin Liin which each character has the internal numeric code equal 8960*088332b5SXin Lito its corresponding argument. 8961*088332b5SXin Li 8962*088332b5SXin Li 8963*088332b5SXin Li<p> 8964*088332b5SXin LiNumeric codes are not necessarily portable across platforms. 8965*088332b5SXin Li 8966*088332b5SXin Li 8967*088332b5SXin Li 8968*088332b5SXin Li 8969*088332b5SXin Li<p> 8970*088332b5SXin Li<hr><h3><a name="pdf-string.dump"><code>string.dump (function [, strip])</code></a></h3> 8971*088332b5SXin Li 8972*088332b5SXin Li 8973*088332b5SXin Li<p> 8974*088332b5SXin LiReturns a string containing a binary representation 8975*088332b5SXin Li(a <em>binary chunk</em>) 8976*088332b5SXin Liof the given function, 8977*088332b5SXin Liso that a later <a href="#pdf-load"><code>load</code></a> on this string returns 8978*088332b5SXin Lia copy of the function (but with new upvalues). 8979*088332b5SXin LiIf <code>strip</code> is a true value, 8980*088332b5SXin Lithe binary representation may not include all debug information 8981*088332b5SXin Liabout the function, 8982*088332b5SXin Lito save space. 8983*088332b5SXin Li 8984*088332b5SXin Li 8985*088332b5SXin Li<p> 8986*088332b5SXin LiFunctions with upvalues have only their number of upvalues saved. 8987*088332b5SXin LiWhen (re)loaded, 8988*088332b5SXin Lithose upvalues receive fresh instances. 8989*088332b5SXin Li(See the <a href="#pdf-load"><code>load</code></a> function for details about 8990*088332b5SXin Lihow these upvalues are initialized. 8991*088332b5SXin LiYou can use the debug library to serialize 8992*088332b5SXin Liand reload the upvalues of a function 8993*088332b5SXin Liin a way adequate to your needs.) 8994*088332b5SXin Li 8995*088332b5SXin Li 8996*088332b5SXin Li 8997*088332b5SXin Li 8998*088332b5SXin Li<p> 8999*088332b5SXin Li<hr><h3><a name="pdf-string.find"><code>string.find (s, pattern [, init [, plain]])</code></a></h3> 9000*088332b5SXin Li 9001*088332b5SXin Li 9002*088332b5SXin Li<p> 9003*088332b5SXin LiLooks for the first match of 9004*088332b5SXin Li<code>pattern</code> (see <a href="#6.4.1">§6.4.1</a>) in the string <code>s</code>. 9005*088332b5SXin LiIf it finds a match, then <code>find</code> returns the indices of <code>s</code> 9006*088332b5SXin Liwhere this occurrence starts and ends; 9007*088332b5SXin Liotherwise, it returns <b>fail</b>. 9008*088332b5SXin LiA third, optional numeric argument <code>init</code> specifies 9009*088332b5SXin Liwhere to start the search; 9010*088332b5SXin Liits default value is 1 and can be negative. 9011*088332b5SXin LiA value of <b>true</b> as a fourth, optional argument <code>plain</code> 9012*088332b5SXin Liturns off the pattern matching facilities, 9013*088332b5SXin Liso the function does a plain "find substring" operation, 9014*088332b5SXin Liwith no characters in <code>pattern</code> being considered magic. 9015*088332b5SXin Li 9016*088332b5SXin Li 9017*088332b5SXin Li<p> 9018*088332b5SXin LiIf the pattern has captures, 9019*088332b5SXin Lithen in a successful match 9020*088332b5SXin Lithe captured values are also returned, 9021*088332b5SXin Liafter the two indices. 9022*088332b5SXin Li 9023*088332b5SXin Li 9024*088332b5SXin Li 9025*088332b5SXin Li 9026*088332b5SXin Li<p> 9027*088332b5SXin Li<hr><h3><a name="pdf-string.format"><code>string.format (formatstring, ···)</code></a></h3> 9028*088332b5SXin Li 9029*088332b5SXin Li 9030*088332b5SXin Li<p> 9031*088332b5SXin LiReturns a formatted version of its variable number of arguments 9032*088332b5SXin Lifollowing the description given in its first argument, 9033*088332b5SXin Liwhich must be a string. 9034*088332b5SXin LiThe format string follows the same rules as the ISO C function <code>sprintf</code>. 9035*088332b5SXin LiThe only differences are that the conversion specifiers and modifiers 9036*088332b5SXin Li<code>*</code>, <code>h</code>, <code>L</code>, <code>l</code>, and <code>n</code> are not supported 9037*088332b5SXin Liand that there is an extra specifier, <code>q</code>. 9038*088332b5SXin Li 9039*088332b5SXin Li 9040*088332b5SXin Li<p> 9041*088332b5SXin LiThe specifier <code>q</code> formats booleans, nil, numbers, and strings 9042*088332b5SXin Liin a way that the result is a valid constant in Lua source code. 9043*088332b5SXin LiBooleans and nil are written in the obvious way 9044*088332b5SXin Li(<code>true</code>, <code>false</code>, <code>nil</code>). 9045*088332b5SXin LiFloats are written in hexadecimal, 9046*088332b5SXin Lito preserve full precision. 9047*088332b5SXin LiA string is written between double quotes, 9048*088332b5SXin Liusing escape sequences when necessary to ensure that 9049*088332b5SXin Liit can safely be read back by the Lua interpreter. 9050*088332b5SXin LiFor instance, the call 9051*088332b5SXin Li 9052*088332b5SXin Li<pre> 9053*088332b5SXin Li string.format('%q', 'a string with "quotes" and \n new line') 9054*088332b5SXin Li</pre><p> 9055*088332b5SXin Limay produce the string: 9056*088332b5SXin Li 9057*088332b5SXin Li<pre> 9058*088332b5SXin Li "a string with \"quotes\" and \ 9059*088332b5SXin Li new line" 9060*088332b5SXin Li</pre><p> 9061*088332b5SXin LiThis specifier does not support modifiers (flags, width, length). 9062*088332b5SXin Li 9063*088332b5SXin Li 9064*088332b5SXin Li<p> 9065*088332b5SXin LiThe conversion specifiers 9066*088332b5SXin Li<code>A</code>, <code>a</code>, <code>E</code>, <code>e</code>, <code>f</code>, 9067*088332b5SXin Li<code>G</code>, and <code>g</code> all expect a number as argument. 9068*088332b5SXin LiThe specifiers <code>c</code>, <code>d</code>, 9069*088332b5SXin Li<code>i</code>, <code>o</code>, <code>u</code>, <code>X</code>, and <code>x</code> 9070*088332b5SXin Liexpect an integer. 9071*088332b5SXin LiWhen Lua is compiled with a C89 compiler, 9072*088332b5SXin Lithe specifiers <code>A</code> and <code>a</code> (hexadecimal floats) 9073*088332b5SXin Lido not support modifiers. 9074*088332b5SXin Li 9075*088332b5SXin Li 9076*088332b5SXin Li<p> 9077*088332b5SXin LiThe specifier <code>s</code> expects a string; 9078*088332b5SXin Liif its argument is not a string, 9079*088332b5SXin Liit is converted to one following the same rules of <a href="#pdf-tostring"><code>tostring</code></a>. 9080*088332b5SXin LiIf the specifier has any modifier, 9081*088332b5SXin Lithe corresponding string argument should not contain embedded zeros. 9082*088332b5SXin Li 9083*088332b5SXin Li 9084*088332b5SXin Li<p> 9085*088332b5SXin LiThe specifier <code>p</code> formats the pointer 9086*088332b5SXin Lireturned by <a href="#lua_topointer"><code>lua_topointer</code></a>. 9087*088332b5SXin LiThat gives a unique string identifier for tables, userdata, 9088*088332b5SXin Lithreads, strings, and functions. 9089*088332b5SXin LiFor other values (numbers, nil, booleans), 9090*088332b5SXin Lithis specifier results in a string representing 9091*088332b5SXin Lithe pointer <code>NULL</code>. 9092*088332b5SXin Li 9093*088332b5SXin Li 9094*088332b5SXin Li 9095*088332b5SXin Li 9096*088332b5SXin Li<p> 9097*088332b5SXin Li<hr><h3><a name="pdf-string.gmatch"><code>string.gmatch (s, pattern [, init])</code></a></h3> 9098*088332b5SXin LiReturns an iterator function that, 9099*088332b5SXin Lieach time it is called, 9100*088332b5SXin Lireturns the next captures from <code>pattern</code> (see <a href="#6.4.1">§6.4.1</a>) 9101*088332b5SXin Liover the string <code>s</code>. 9102*088332b5SXin LiIf <code>pattern</code> specifies no captures, 9103*088332b5SXin Lithen the whole match is produced in each call. 9104*088332b5SXin LiA third, optional numeric argument <code>init</code> specifies 9105*088332b5SXin Liwhere to start the search; 9106*088332b5SXin Liits default value is 1 and can be negative. 9107*088332b5SXin Li 9108*088332b5SXin Li 9109*088332b5SXin Li<p> 9110*088332b5SXin LiAs an example, the following loop 9111*088332b5SXin Liwill iterate over all the words from string <code>s</code>, 9112*088332b5SXin Liprinting one per line: 9113*088332b5SXin Li 9114*088332b5SXin Li<pre> 9115*088332b5SXin Li s = "hello world from Lua" 9116*088332b5SXin Li for w in string.gmatch(s, "%a+") do 9117*088332b5SXin Li print(w) 9118*088332b5SXin Li end 9119*088332b5SXin Li</pre><p> 9120*088332b5SXin LiThe next example collects all pairs <code>key=value</code> from the 9121*088332b5SXin Ligiven string into a table: 9122*088332b5SXin Li 9123*088332b5SXin Li<pre> 9124*088332b5SXin Li t = {} 9125*088332b5SXin Li s = "from=world, to=Lua" 9126*088332b5SXin Li for k, v in string.gmatch(s, "(%w+)=(%w+)") do 9127*088332b5SXin Li t[k] = v 9128*088332b5SXin Li end 9129*088332b5SXin Li</pre> 9130*088332b5SXin Li 9131*088332b5SXin Li<p> 9132*088332b5SXin LiFor this function, a caret '<code>^</code>' at the start of a pattern does not 9133*088332b5SXin Liwork as an anchor, as this would prevent the iteration. 9134*088332b5SXin Li 9135*088332b5SXin Li 9136*088332b5SXin Li 9137*088332b5SXin Li 9138*088332b5SXin Li<p> 9139*088332b5SXin Li<hr><h3><a name="pdf-string.gsub"><code>string.gsub (s, pattern, repl [, n])</code></a></h3> 9140*088332b5SXin LiReturns a copy of <code>s</code> 9141*088332b5SXin Liin which all (or the first <code>n</code>, if given) 9142*088332b5SXin Lioccurrences of the <code>pattern</code> (see <a href="#6.4.1">§6.4.1</a>) have been 9143*088332b5SXin Lireplaced by a replacement string specified by <code>repl</code>, 9144*088332b5SXin Liwhich can be a string, a table, or a function. 9145*088332b5SXin Li<code>gsub</code> also returns, as its second value, 9146*088332b5SXin Lithe total number of matches that occurred. 9147*088332b5SXin LiThe name <code>gsub</code> comes from <em>Global SUBstitution</em>. 9148*088332b5SXin Li 9149*088332b5SXin Li 9150*088332b5SXin Li<p> 9151*088332b5SXin LiIf <code>repl</code> is a string, then its value is used for replacement. 9152*088332b5SXin LiThe character <code>%</code> works as an escape character: 9153*088332b5SXin Liany sequence in <code>repl</code> of the form <code>%<em>d</em></code>, 9154*088332b5SXin Liwith <em>d</em> between 1 and 9, 9155*088332b5SXin Listands for the value of the <em>d</em>-th captured substring; 9156*088332b5SXin Lithe sequence <code>%0</code> stands for the whole match; 9157*088332b5SXin Lithe sequence <code>%%</code> stands for a single <code>%</code>. 9158*088332b5SXin Li 9159*088332b5SXin Li 9160*088332b5SXin Li<p> 9161*088332b5SXin LiIf <code>repl</code> is a table, then the table is queried for every match, 9162*088332b5SXin Liusing the first capture as the key. 9163*088332b5SXin Li 9164*088332b5SXin Li 9165*088332b5SXin Li<p> 9166*088332b5SXin LiIf <code>repl</code> is a function, then this function is called every time a 9167*088332b5SXin Limatch occurs, with all captured substrings passed as arguments, 9168*088332b5SXin Liin order. 9169*088332b5SXin Li 9170*088332b5SXin Li 9171*088332b5SXin Li<p> 9172*088332b5SXin LiIn any case, 9173*088332b5SXin Liif the pattern specifies no captures, 9174*088332b5SXin Lithen it behaves as if the whole pattern was inside a capture. 9175*088332b5SXin Li 9176*088332b5SXin Li 9177*088332b5SXin Li<p> 9178*088332b5SXin LiIf the value returned by the table query or by the function call 9179*088332b5SXin Liis a string or a number, 9180*088332b5SXin Lithen it is used as the replacement string; 9181*088332b5SXin Liotherwise, if it is <b>false</b> or <b>nil</b>, 9182*088332b5SXin Lithen there is no replacement 9183*088332b5SXin Li(that is, the original match is kept in the string). 9184*088332b5SXin Li 9185*088332b5SXin Li 9186*088332b5SXin Li<p> 9187*088332b5SXin LiHere are some examples: 9188*088332b5SXin Li 9189*088332b5SXin Li<pre> 9190*088332b5SXin Li x = string.gsub("hello world", "(%w+)", "%1 %1") 9191*088332b5SXin Li --> x="hello hello world world" 9192*088332b5SXin Li 9193*088332b5SXin Li x = string.gsub("hello world", "%w+", "%0 %0", 1) 9194*088332b5SXin Li --> x="hello hello world" 9195*088332b5SXin Li 9196*088332b5SXin Li x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1") 9197*088332b5SXin Li --> x="world hello Lua from" 9198*088332b5SXin Li 9199*088332b5SXin Li x = string.gsub("home = $HOME, user = $USER", "%$(%w+)", os.getenv) 9200*088332b5SXin Li --> x="home = /home/roberto, user = roberto" 9201*088332b5SXin Li 9202*088332b5SXin Li x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s) 9203*088332b5SXin Li return load(s)() 9204*088332b5SXin Li end) 9205*088332b5SXin Li --> x="4+5 = 9" 9206*088332b5SXin Li 9207*088332b5SXin Li local t = {name="lua", version="5.4"} 9208*088332b5SXin Li x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t) 9209*088332b5SXin Li --> x="lua-5.4.tar.gz" 9210*088332b5SXin Li</pre> 9211*088332b5SXin Li 9212*088332b5SXin Li 9213*088332b5SXin Li 9214*088332b5SXin Li<p> 9215*088332b5SXin Li<hr><h3><a name="pdf-string.len"><code>string.len (s)</code></a></h3> 9216*088332b5SXin Li 9217*088332b5SXin Li 9218*088332b5SXin Li<p> 9219*088332b5SXin LiReceives a string and returns its length. 9220*088332b5SXin LiThe empty string <code>""</code> has length 0. 9221*088332b5SXin LiEmbedded zeros are counted, 9222*088332b5SXin Liso <code>"a\000bc\000"</code> has length 5. 9223*088332b5SXin Li 9224*088332b5SXin Li 9225*088332b5SXin Li 9226*088332b5SXin Li 9227*088332b5SXin Li<p> 9228*088332b5SXin Li<hr><h3><a name="pdf-string.lower"><code>string.lower (s)</code></a></h3> 9229*088332b5SXin Li 9230*088332b5SXin Li 9231*088332b5SXin Li<p> 9232*088332b5SXin LiReceives a string and returns a copy of this string with all 9233*088332b5SXin Liuppercase letters changed to lowercase. 9234*088332b5SXin LiAll other characters are left unchanged. 9235*088332b5SXin LiThe definition of what an uppercase letter is depends on the current locale. 9236*088332b5SXin Li 9237*088332b5SXin Li 9238*088332b5SXin Li 9239*088332b5SXin Li 9240*088332b5SXin Li<p> 9241*088332b5SXin Li<hr><h3><a name="pdf-string.match"><code>string.match (s, pattern [, init])</code></a></h3> 9242*088332b5SXin Li 9243*088332b5SXin Li 9244*088332b5SXin Li<p> 9245*088332b5SXin LiLooks for the first <em>match</em> of 9246*088332b5SXin Lithe <code>pattern</code> (see <a href="#6.4.1">§6.4.1</a>) in the string <code>s</code>. 9247*088332b5SXin LiIf it finds one, then <code>match</code> returns 9248*088332b5SXin Lithe captures from the pattern; 9249*088332b5SXin Liotherwise it returns <b>fail</b>. 9250*088332b5SXin LiIf <code>pattern</code> specifies no captures, 9251*088332b5SXin Lithen the whole match is returned. 9252*088332b5SXin LiA third, optional numeric argument <code>init</code> specifies 9253*088332b5SXin Liwhere to start the search; 9254*088332b5SXin Liits default value is 1 and can be negative. 9255*088332b5SXin Li 9256*088332b5SXin Li 9257*088332b5SXin Li 9258*088332b5SXin Li 9259*088332b5SXin Li<p> 9260*088332b5SXin Li<hr><h3><a name="pdf-string.pack"><code>string.pack (fmt, v1, v2, ···)</code></a></h3> 9261*088332b5SXin Li 9262*088332b5SXin Li 9263*088332b5SXin Li<p> 9264*088332b5SXin LiReturns a binary string containing the values <code>v1</code>, <code>v2</code>, etc. 9265*088332b5SXin Liserialized in binary form (packed) 9266*088332b5SXin Liaccording to the format string <code>fmt</code> (see <a href="#6.4.2">§6.4.2</a>). 9267*088332b5SXin Li 9268*088332b5SXin Li 9269*088332b5SXin Li 9270*088332b5SXin Li 9271*088332b5SXin Li<p> 9272*088332b5SXin Li<hr><h3><a name="pdf-string.packsize"><code>string.packsize (fmt)</code></a></h3> 9273*088332b5SXin Li 9274*088332b5SXin Li 9275*088332b5SXin Li<p> 9276*088332b5SXin LiReturns the size of a string resulting from <a href="#pdf-string.pack"><code>string.pack</code></a> 9277*088332b5SXin Liwith the given format. 9278*088332b5SXin LiThe format string cannot have the variable-length options 9279*088332b5SXin Li'<code>s</code>' or '<code>z</code>' (see <a href="#6.4.2">§6.4.2</a>). 9280*088332b5SXin Li 9281*088332b5SXin Li 9282*088332b5SXin Li 9283*088332b5SXin Li 9284*088332b5SXin Li<p> 9285*088332b5SXin Li<hr><h3><a name="pdf-string.rep"><code>string.rep (s, n [, sep])</code></a></h3> 9286*088332b5SXin Li 9287*088332b5SXin Li 9288*088332b5SXin Li<p> 9289*088332b5SXin LiReturns a string that is the concatenation of <code>n</code> copies of 9290*088332b5SXin Lithe string <code>s</code> separated by the string <code>sep</code>. 9291*088332b5SXin LiThe default value for <code>sep</code> is the empty string 9292*088332b5SXin Li(that is, no separator). 9293*088332b5SXin LiReturns the empty string if <code>n</code> is not positive. 9294*088332b5SXin Li 9295*088332b5SXin Li 9296*088332b5SXin Li<p> 9297*088332b5SXin Li(Note that it is very easy to exhaust the memory of your machine 9298*088332b5SXin Liwith a single call to this function.) 9299*088332b5SXin Li 9300*088332b5SXin Li 9301*088332b5SXin Li 9302*088332b5SXin Li 9303*088332b5SXin Li<p> 9304*088332b5SXin Li<hr><h3><a name="pdf-string.reverse"><code>string.reverse (s)</code></a></h3> 9305*088332b5SXin Li 9306*088332b5SXin Li 9307*088332b5SXin Li<p> 9308*088332b5SXin LiReturns a string that is the string <code>s</code> reversed. 9309*088332b5SXin Li 9310*088332b5SXin Li 9311*088332b5SXin Li 9312*088332b5SXin Li 9313*088332b5SXin Li<p> 9314*088332b5SXin Li<hr><h3><a name="pdf-string.sub"><code>string.sub (s, i [, j])</code></a></h3> 9315*088332b5SXin Li 9316*088332b5SXin Li 9317*088332b5SXin Li<p> 9318*088332b5SXin LiReturns the substring of <code>s</code> that 9319*088332b5SXin Listarts at <code>i</code> and continues until <code>j</code>; 9320*088332b5SXin Li<code>i</code> and <code>j</code> can be negative. 9321*088332b5SXin LiIf <code>j</code> is absent, then it is assumed to be equal to -1 9322*088332b5SXin Li(which is the same as the string length). 9323*088332b5SXin LiIn particular, 9324*088332b5SXin Lithe call <code>string.sub(s,1,j)</code> returns a prefix of <code>s</code> 9325*088332b5SXin Liwith length <code>j</code>, 9326*088332b5SXin Liand <code>string.sub(s, -i)</code> (for a positive <code>i</code>) 9327*088332b5SXin Lireturns a suffix of <code>s</code> 9328*088332b5SXin Liwith length <code>i</code>. 9329*088332b5SXin Li 9330*088332b5SXin Li 9331*088332b5SXin Li<p> 9332*088332b5SXin LiIf, after the translation of negative indices, 9333*088332b5SXin Li<code>i</code> is less than 1, 9334*088332b5SXin Liit is corrected to 1. 9335*088332b5SXin LiIf <code>j</code> is greater than the string length, 9336*088332b5SXin Liit is corrected to that length. 9337*088332b5SXin LiIf, after these corrections, 9338*088332b5SXin Li<code>i</code> is greater than <code>j</code>, 9339*088332b5SXin Lithe function returns the empty string. 9340*088332b5SXin Li 9341*088332b5SXin Li 9342*088332b5SXin Li 9343*088332b5SXin Li 9344*088332b5SXin Li<p> 9345*088332b5SXin Li<hr><h3><a name="pdf-string.unpack"><code>string.unpack (fmt, s [, pos])</code></a></h3> 9346*088332b5SXin Li 9347*088332b5SXin Li 9348*088332b5SXin Li<p> 9349*088332b5SXin LiReturns the values packed in string <code>s</code> (see <a href="#pdf-string.pack"><code>string.pack</code></a>) 9350*088332b5SXin Liaccording to the format string <code>fmt</code> (see <a href="#6.4.2">§6.4.2</a>). 9351*088332b5SXin LiAn optional <code>pos</code> marks where 9352*088332b5SXin Lito start reading in <code>s</code> (default is 1). 9353*088332b5SXin LiAfter the read values, 9354*088332b5SXin Lithis function also returns the index of the first unread byte in <code>s</code>. 9355*088332b5SXin Li 9356*088332b5SXin Li 9357*088332b5SXin Li 9358*088332b5SXin Li 9359*088332b5SXin Li<p> 9360*088332b5SXin Li<hr><h3><a name="pdf-string.upper"><code>string.upper (s)</code></a></h3> 9361*088332b5SXin Li 9362*088332b5SXin Li 9363*088332b5SXin Li<p> 9364*088332b5SXin LiReceives a string and returns a copy of this string with all 9365*088332b5SXin Lilowercase letters changed to uppercase. 9366*088332b5SXin LiAll other characters are left unchanged. 9367*088332b5SXin LiThe definition of what a lowercase letter is depends on the current locale. 9368*088332b5SXin Li 9369*088332b5SXin Li 9370*088332b5SXin Li 9371*088332b5SXin Li 9372*088332b5SXin Li 9373*088332b5SXin Li 9374*088332b5SXin Li 9375*088332b5SXin Li<h3>6.4.1 – <a name="6.4.1">Patterns</a></h3> 9376*088332b5SXin Li 9377*088332b5SXin Li 9378*088332b5SXin Li 9379*088332b5SXin Li<p> 9380*088332b5SXin LiPatterns in Lua are described by regular strings, 9381*088332b5SXin Liwhich are interpreted as patterns by the pattern-matching functions 9382*088332b5SXin Li<a href="#pdf-string.find"><code>string.find</code></a>, 9383*088332b5SXin Li<a href="#pdf-string.gmatch"><code>string.gmatch</code></a>, 9384*088332b5SXin Li<a href="#pdf-string.gsub"><code>string.gsub</code></a>, 9385*088332b5SXin Liand <a href="#pdf-string.match"><code>string.match</code></a>. 9386*088332b5SXin LiThis section describes the syntax and the meaning 9387*088332b5SXin Li(that is, what they match) of these strings. 9388*088332b5SXin Li 9389*088332b5SXin Li 9390*088332b5SXin Li 9391*088332b5SXin Li 9392*088332b5SXin Li 9393*088332b5SXin Li<h4>Character Class:</h4><p> 9394*088332b5SXin LiA <em>character class</em> is used to represent a set of characters. 9395*088332b5SXin LiThe following combinations are allowed in describing a character class: 9396*088332b5SXin Li 9397*088332b5SXin Li<ul> 9398*088332b5SXin Li 9399*088332b5SXin Li<li><b><em>x</em>: </b> 9400*088332b5SXin Li(where <em>x</em> is not one of the <em>magic characters</em> 9401*088332b5SXin Li<code>^$()%.[]*+-?</code>) 9402*088332b5SXin Lirepresents the character <em>x</em> itself. 9403*088332b5SXin Li</li> 9404*088332b5SXin Li 9405*088332b5SXin Li<li><b><code>.</code>: </b> (a dot) represents all characters.</li> 9406*088332b5SXin Li 9407*088332b5SXin Li<li><b><code>%a</code>: </b> represents all letters.</li> 9408*088332b5SXin Li 9409*088332b5SXin Li<li><b><code>%c</code>: </b> represents all control characters.</li> 9410*088332b5SXin Li 9411*088332b5SXin Li<li><b><code>%d</code>: </b> represents all digits.</li> 9412*088332b5SXin Li 9413*088332b5SXin Li<li><b><code>%g</code>: </b> represents all printable characters except space.</li> 9414*088332b5SXin Li 9415*088332b5SXin Li<li><b><code>%l</code>: </b> represents all lowercase letters.</li> 9416*088332b5SXin Li 9417*088332b5SXin Li<li><b><code>%p</code>: </b> represents all punctuation characters.</li> 9418*088332b5SXin Li 9419*088332b5SXin Li<li><b><code>%s</code>: </b> represents all space characters.</li> 9420*088332b5SXin Li 9421*088332b5SXin Li<li><b><code>%u</code>: </b> represents all uppercase letters.</li> 9422*088332b5SXin Li 9423*088332b5SXin Li<li><b><code>%w</code>: </b> represents all alphanumeric characters.</li> 9424*088332b5SXin Li 9425*088332b5SXin Li<li><b><code>%x</code>: </b> represents all hexadecimal digits.</li> 9426*088332b5SXin Li 9427*088332b5SXin Li<li><b><code>%<em>x</em></code>: </b> (where <em>x</em> is any non-alphanumeric character) 9428*088332b5SXin Lirepresents the character <em>x</em>. 9429*088332b5SXin LiThis is the standard way to escape the magic characters. 9430*088332b5SXin LiAny non-alphanumeric character 9431*088332b5SXin Li(including all punctuation characters, even the non-magical) 9432*088332b5SXin Lican be preceded by a '<code>%</code>' to represent itself in a pattern. 9433*088332b5SXin Li</li> 9434*088332b5SXin Li 9435*088332b5SXin Li<li><b><code>[<em>set</em>]</code>: </b> 9436*088332b5SXin Lirepresents the class which is the union of all 9437*088332b5SXin Licharacters in <em>set</em>. 9438*088332b5SXin LiA range of characters can be specified by 9439*088332b5SXin Liseparating the end characters of the range, 9440*088332b5SXin Liin ascending order, with a '<code>-</code>'. 9441*088332b5SXin LiAll classes <code>%</code><em>x</em> described above can also be used as 9442*088332b5SXin Licomponents in <em>set</em>. 9443*088332b5SXin LiAll other characters in <em>set</em> represent themselves. 9444*088332b5SXin LiFor example, <code>[%w_]</code> (or <code>[_%w]</code>) 9445*088332b5SXin Lirepresents all alphanumeric characters plus the underscore, 9446*088332b5SXin Li<code>[0-7]</code> represents the octal digits, 9447*088332b5SXin Liand <code>[0-7%l%-]</code> represents the octal digits plus 9448*088332b5SXin Lithe lowercase letters plus the '<code>-</code>' character. 9449*088332b5SXin Li 9450*088332b5SXin Li 9451*088332b5SXin Li<p> 9452*088332b5SXin LiYou can put a closing square bracket in a set 9453*088332b5SXin Liby positioning it as the first character in the set. 9454*088332b5SXin LiYou can put a hyphen in a set 9455*088332b5SXin Liby positioning it as the first or the last character in the set. 9456*088332b5SXin Li(You can also use an escape for both cases.) 9457*088332b5SXin Li 9458*088332b5SXin Li 9459*088332b5SXin Li<p> 9460*088332b5SXin LiThe interaction between ranges and classes is not defined. 9461*088332b5SXin LiTherefore, patterns like <code>[%a-z]</code> or <code>[a-%%]</code> 9462*088332b5SXin Lihave no meaning. 9463*088332b5SXin Li</li> 9464*088332b5SXin Li 9465*088332b5SXin Li<li><b><code>[^<em>set</em>]</code>: </b> 9466*088332b5SXin Lirepresents the complement of <em>set</em>, 9467*088332b5SXin Liwhere <em>set</em> is interpreted as above. 9468*088332b5SXin Li</li> 9469*088332b5SXin Li 9470*088332b5SXin Li</ul><p> 9471*088332b5SXin LiFor all classes represented by single letters (<code>%a</code>, <code>%c</code>, etc.), 9472*088332b5SXin Lithe corresponding uppercase letter represents the complement of the class. 9473*088332b5SXin LiFor instance, <code>%S</code> represents all non-space characters. 9474*088332b5SXin Li 9475*088332b5SXin Li 9476*088332b5SXin Li<p> 9477*088332b5SXin LiThe definitions of letter, space, and other character groups 9478*088332b5SXin Lidepend on the current locale. 9479*088332b5SXin LiIn particular, the class <code>[a-z]</code> may not be equivalent to <code>%l</code>. 9480*088332b5SXin Li 9481*088332b5SXin Li 9482*088332b5SXin Li 9483*088332b5SXin Li 9484*088332b5SXin Li 9485*088332b5SXin Li<h4>Pattern Item:</h4><p> 9486*088332b5SXin LiA <em>pattern item</em> can be 9487*088332b5SXin Li 9488*088332b5SXin Li<ul> 9489*088332b5SXin Li 9490*088332b5SXin Li<li> 9491*088332b5SXin Lia single character class, 9492*088332b5SXin Liwhich matches any single character in the class; 9493*088332b5SXin Li</li> 9494*088332b5SXin Li 9495*088332b5SXin Li<li> 9496*088332b5SXin Lia single character class followed by '<code>*</code>', 9497*088332b5SXin Liwhich matches sequences of zero or more characters in the class. 9498*088332b5SXin LiThese repetition items will always match the longest possible sequence; 9499*088332b5SXin Li</li> 9500*088332b5SXin Li 9501*088332b5SXin Li<li> 9502*088332b5SXin Lia single character class followed by '<code>+</code>', 9503*088332b5SXin Liwhich matches sequences of one or more characters in the class. 9504*088332b5SXin LiThese repetition items will always match the longest possible sequence; 9505*088332b5SXin Li</li> 9506*088332b5SXin Li 9507*088332b5SXin Li<li> 9508*088332b5SXin Lia single character class followed by '<code>-</code>', 9509*088332b5SXin Liwhich also matches sequences of zero or more characters in the class. 9510*088332b5SXin LiUnlike '<code>*</code>', 9511*088332b5SXin Lithese repetition items will always match the shortest possible sequence; 9512*088332b5SXin Li</li> 9513*088332b5SXin Li 9514*088332b5SXin Li<li> 9515*088332b5SXin Lia single character class followed by '<code>?</code>', 9516*088332b5SXin Liwhich matches zero or one occurrence of a character in the class. 9517*088332b5SXin LiIt always matches one occurrence if possible; 9518*088332b5SXin Li</li> 9519*088332b5SXin Li 9520*088332b5SXin Li<li> 9521*088332b5SXin Li<code>%<em>n</em></code>, for <em>n</em> between 1 and 9; 9522*088332b5SXin Lisuch item matches a substring equal to the <em>n</em>-th captured string 9523*088332b5SXin Li(see below); 9524*088332b5SXin Li</li> 9525*088332b5SXin Li 9526*088332b5SXin Li<li> 9527*088332b5SXin Li<code>%b<em>xy</em></code>, where <em>x</em> and <em>y</em> are two distinct characters; 9528*088332b5SXin Lisuch item matches strings that start with <em>x</em>, end with <em>y</em>, 9529*088332b5SXin Liand where the <em>x</em> and <em>y</em> are <em>balanced</em>. 9530*088332b5SXin LiThis means that, if one reads the string from left to right, 9531*088332b5SXin Licounting <em>+1</em> for an <em>x</em> and <em>-1</em> for a <em>y</em>, 9532*088332b5SXin Lithe ending <em>y</em> is the first <em>y</em> where the count reaches 0. 9533*088332b5SXin LiFor instance, the item <code>%b()</code> matches expressions with 9534*088332b5SXin Libalanced parentheses. 9535*088332b5SXin Li</li> 9536*088332b5SXin Li 9537*088332b5SXin Li<li> 9538*088332b5SXin Li<code>%f[<em>set</em>]</code>, a <em>frontier pattern</em>; 9539*088332b5SXin Lisuch item matches an empty string at any position such that 9540*088332b5SXin Lithe next character belongs to <em>set</em> 9541*088332b5SXin Liand the previous character does not belong to <em>set</em>. 9542*088332b5SXin LiThe set <em>set</em> is interpreted as previously described. 9543*088332b5SXin LiThe beginning and the end of the subject are handled as if 9544*088332b5SXin Lithey were the character '<code>\0</code>'. 9545*088332b5SXin Li</li> 9546*088332b5SXin Li 9547*088332b5SXin Li</ul> 9548*088332b5SXin Li 9549*088332b5SXin Li 9550*088332b5SXin Li 9551*088332b5SXin Li 9552*088332b5SXin Li<h4>Pattern:</h4><p> 9553*088332b5SXin LiA <em>pattern</em> is a sequence of pattern items. 9554*088332b5SXin LiA caret '<code>^</code>' at the beginning of a pattern anchors the match at the 9555*088332b5SXin Libeginning of the subject string. 9556*088332b5SXin LiA '<code>$</code>' at the end of a pattern anchors the match at the 9557*088332b5SXin Liend of the subject string. 9558*088332b5SXin LiAt other positions, 9559*088332b5SXin Li'<code>^</code>' and '<code>$</code>' have no special meaning and represent themselves. 9560*088332b5SXin Li 9561*088332b5SXin Li 9562*088332b5SXin Li 9563*088332b5SXin Li 9564*088332b5SXin Li 9565*088332b5SXin Li<h4>Captures:</h4><p> 9566*088332b5SXin LiA pattern can contain sub-patterns enclosed in parentheses; 9567*088332b5SXin Lithey describe <em>captures</em>. 9568*088332b5SXin LiWhen a match succeeds, the substrings of the subject string 9569*088332b5SXin Lithat match captures are stored (<em>captured</em>) for future use. 9570*088332b5SXin LiCaptures are numbered according to their left parentheses. 9571*088332b5SXin LiFor instance, in the pattern <code>"(a*(.)%w(%s*))"</code>, 9572*088332b5SXin Lithe part of the string matching <code>"a*(.)%w(%s*)"</code> is 9573*088332b5SXin Listored as the first capture, and therefore has number 1; 9574*088332b5SXin Lithe character matching "<code>.</code>" is captured with number 2, 9575*088332b5SXin Liand the part matching "<code>%s*</code>" has number 3. 9576*088332b5SXin Li 9577*088332b5SXin Li 9578*088332b5SXin Li<p> 9579*088332b5SXin LiAs a special case, the capture <code>()</code> captures 9580*088332b5SXin Lithe current string position (a number). 9581*088332b5SXin LiFor instance, if we apply the pattern <code>"()aa()"</code> on the 9582*088332b5SXin Listring <code>"flaaap"</code>, there will be two captures: 3 and 5. 9583*088332b5SXin Li 9584*088332b5SXin Li 9585*088332b5SXin Li 9586*088332b5SXin Li 9587*088332b5SXin Li 9588*088332b5SXin Li<h4>Multiple matches:</h4><p> 9589*088332b5SXin LiThe function <a href="#pdf-string.gsub"><code>string.gsub</code></a> and the iterator <a href="#pdf-string.gmatch"><code>string.gmatch</code></a> 9590*088332b5SXin Limatch multiple occurrences of the given pattern in the subject. 9591*088332b5SXin LiFor these functions, 9592*088332b5SXin Lia new match is considered valid only 9593*088332b5SXin Liif it ends at least one byte after the end of the previous match. 9594*088332b5SXin LiIn other words, the pattern machine never accepts the 9595*088332b5SXin Liempty string as a match immediately after another match. 9596*088332b5SXin LiAs an example, 9597*088332b5SXin Liconsider the results of the following code: 9598*088332b5SXin Li 9599*088332b5SXin Li<pre> 9600*088332b5SXin Li > string.gsub("abc", "()a*()", print); 9601*088332b5SXin Li --> 1 2 9602*088332b5SXin Li --> 3 3 9603*088332b5SXin Li --> 4 4 9604*088332b5SXin Li</pre><p> 9605*088332b5SXin LiThe second and third results come from Lua matching an empty 9606*088332b5SXin Listring after '<code>b</code>' and another one after '<code>c</code>'. 9607*088332b5SXin LiLua does not match an empty string after '<code>a</code>', 9608*088332b5SXin Libecause it would end at the same position of the previous match. 9609*088332b5SXin Li 9610*088332b5SXin Li 9611*088332b5SXin Li 9612*088332b5SXin Li 9613*088332b5SXin Li 9614*088332b5SXin Li 9615*088332b5SXin Li 9616*088332b5SXin Li<h3>6.4.2 – <a name="6.4.2">Format Strings for Pack and Unpack</a></h3> 9617*088332b5SXin Li 9618*088332b5SXin Li<p> 9619*088332b5SXin LiThe first argument to <a href="#pdf-string.pack"><code>string.pack</code></a>, 9620*088332b5SXin Li<a href="#pdf-string.packsize"><code>string.packsize</code></a>, and <a href="#pdf-string.unpack"><code>string.unpack</code></a> 9621*088332b5SXin Liis a format string, 9622*088332b5SXin Liwhich describes the layout of the structure being created or read. 9623*088332b5SXin Li 9624*088332b5SXin Li 9625*088332b5SXin Li<p> 9626*088332b5SXin LiA format string is a sequence of conversion options. 9627*088332b5SXin LiThe conversion options are as follows: 9628*088332b5SXin Li 9629*088332b5SXin Li<ul> 9630*088332b5SXin Li<li><b><code><</code>: </b>sets little endian</li> 9631*088332b5SXin Li<li><b><code>></code>: </b>sets big endian</li> 9632*088332b5SXin Li<li><b><code>=</code>: </b>sets native endian</li> 9633*088332b5SXin Li<li><b><code>![<em>n</em>]</code>: </b>sets maximum alignment to <code>n</code> 9634*088332b5SXin Li(default is native alignment)</li> 9635*088332b5SXin Li<li><b><code>b</code>: </b>a signed byte (<code>char</code>)</li> 9636*088332b5SXin Li<li><b><code>B</code>: </b>an unsigned byte (<code>char</code>)</li> 9637*088332b5SXin Li<li><b><code>h</code>: </b>a signed <code>short</code> (native size)</li> 9638*088332b5SXin Li<li><b><code>H</code>: </b>an unsigned <code>short</code> (native size)</li> 9639*088332b5SXin Li<li><b><code>l</code>: </b>a signed <code>long</code> (native size)</li> 9640*088332b5SXin Li<li><b><code>L</code>: </b>an unsigned <code>long</code> (native size)</li> 9641*088332b5SXin Li<li><b><code>j</code>: </b>a <code>lua_Integer</code></li> 9642*088332b5SXin Li<li><b><code>J</code>: </b>a <code>lua_Unsigned</code></li> 9643*088332b5SXin Li<li><b><code>T</code>: </b>a <code>size_t</code> (native size)</li> 9644*088332b5SXin Li<li><b><code>i[<em>n</em>]</code>: </b>a signed <code>int</code> with <code>n</code> bytes 9645*088332b5SXin Li(default is native size)</li> 9646*088332b5SXin Li<li><b><code>I[<em>n</em>]</code>: </b>an unsigned <code>int</code> with <code>n</code> bytes 9647*088332b5SXin Li(default is native size)</li> 9648*088332b5SXin Li<li><b><code>f</code>: </b>a <code>float</code> (native size)</li> 9649*088332b5SXin Li<li><b><code>d</code>: </b>a <code>double</code> (native size)</li> 9650*088332b5SXin Li<li><b><code>n</code>: </b>a <code>lua_Number</code></li> 9651*088332b5SXin Li<li><b><code>c<em>n</em></code>: </b>a fixed-sized string with <code>n</code> bytes</li> 9652*088332b5SXin Li<li><b><code>z</code>: </b>a zero-terminated string</li> 9653*088332b5SXin Li<li><b><code>s[<em>n</em>]</code>: </b>a string preceded by its length 9654*088332b5SXin Licoded as an unsigned integer with <code>n</code> bytes 9655*088332b5SXin Li(default is a <code>size_t</code>)</li> 9656*088332b5SXin Li<li><b><code>x</code>: </b>one byte of padding</li> 9657*088332b5SXin Li<li><b><code>X<em>op</em></code>: </b>an empty item that aligns 9658*088332b5SXin Liaccording to option <code>op</code> 9659*088332b5SXin Li(which is otherwise ignored)</li> 9660*088332b5SXin Li<li><b>'<code> </code>': </b>(space) ignored</li> 9661*088332b5SXin Li</ul><p> 9662*088332b5SXin Li(A "<code>[<em>n</em>]</code>" means an optional integral numeral.) 9663*088332b5SXin LiExcept for padding, spaces, and configurations 9664*088332b5SXin Li(options "<code>xX <=>!</code>"), 9665*088332b5SXin Lieach option corresponds to an argument in <a href="#pdf-string.pack"><code>string.pack</code></a> 9666*088332b5SXin Lior a result in <a href="#pdf-string.unpack"><code>string.unpack</code></a>. 9667*088332b5SXin Li 9668*088332b5SXin Li 9669*088332b5SXin Li<p> 9670*088332b5SXin LiFor 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>", 9671*088332b5SXin Li<code>n</code> can be any integer between 1 and 16. 9672*088332b5SXin LiAll integral options check overflows; 9673*088332b5SXin Li<a href="#pdf-string.pack"><code>string.pack</code></a> checks whether the given value fits in the given size; 9674*088332b5SXin Li<a href="#pdf-string.unpack"><code>string.unpack</code></a> checks whether the read value fits in a Lua integer. 9675*088332b5SXin LiFor the unsigned options, 9676*088332b5SXin LiLua integers are treated as unsigned values too. 9677*088332b5SXin Li 9678*088332b5SXin Li 9679*088332b5SXin Li<p> 9680*088332b5SXin LiAny format string starts as if prefixed by "<code>!1=</code>", 9681*088332b5SXin Lithat is, 9682*088332b5SXin Liwith maximum alignment of 1 (no alignment) 9683*088332b5SXin Liand native endianness. 9684*088332b5SXin Li 9685*088332b5SXin Li 9686*088332b5SXin Li<p> 9687*088332b5SXin LiNative endianness assumes that the whole system is 9688*088332b5SXin Lieither big or little endian. 9689*088332b5SXin LiThe packing functions will not emulate correctly the behavior 9690*088332b5SXin Liof mixed-endian formats. 9691*088332b5SXin Li 9692*088332b5SXin Li 9693*088332b5SXin Li<p> 9694*088332b5SXin LiAlignment works as follows: 9695*088332b5SXin LiFor each option, 9696*088332b5SXin Lithe format gets extra padding until the data starts 9697*088332b5SXin Liat an offset that is a multiple of the minimum between the 9698*088332b5SXin Lioption size and the maximum alignment; 9699*088332b5SXin Lithis minimum must be a power of 2. 9700*088332b5SXin LiOptions "<code>c</code>" and "<code>z</code>" are not aligned; 9701*088332b5SXin Lioption "<code>s</code>" follows the alignment of its starting integer. 9702*088332b5SXin Li 9703*088332b5SXin Li 9704*088332b5SXin Li<p> 9705*088332b5SXin LiAll padding is filled with zeros by <a href="#pdf-string.pack"><code>string.pack</code></a> 9706*088332b5SXin Liand ignored by <a href="#pdf-string.unpack"><code>string.unpack</code></a>. 9707*088332b5SXin Li 9708*088332b5SXin Li 9709*088332b5SXin Li 9710*088332b5SXin Li 9711*088332b5SXin Li 9712*088332b5SXin Li 9713*088332b5SXin Li 9714*088332b5SXin Li<h2>6.5 – <a name="6.5">UTF-8 Support</a></h2> 9715*088332b5SXin Li 9716*088332b5SXin Li<p> 9717*088332b5SXin LiThis library provides basic support for UTF-8 encoding. 9718*088332b5SXin LiIt provides all its functions inside the table <a name="pdf-utf8"><code>utf8</code></a>. 9719*088332b5SXin LiThis library does not provide any support for Unicode other 9720*088332b5SXin Lithan the handling of the encoding. 9721*088332b5SXin LiAny operation that needs the meaning of a character, 9722*088332b5SXin Lisuch as character classification, is outside its scope. 9723*088332b5SXin Li 9724*088332b5SXin Li 9725*088332b5SXin Li<p> 9726*088332b5SXin LiUnless stated otherwise, 9727*088332b5SXin Liall functions that expect a byte position as a parameter 9728*088332b5SXin Liassume that the given position is either the start of a byte sequence 9729*088332b5SXin Lior one plus the length of the subject string. 9730*088332b5SXin LiAs in the string library, 9731*088332b5SXin Linegative indices count from the end of the string. 9732*088332b5SXin Li 9733*088332b5SXin Li 9734*088332b5SXin Li<p> 9735*088332b5SXin LiFunctions that create byte sequences 9736*088332b5SXin Liaccept all values up to <code>0x7FFFFFFF</code>, 9737*088332b5SXin Lias defined in the original UTF-8 specification; 9738*088332b5SXin Lithat implies byte sequences of up to six bytes. 9739*088332b5SXin Li 9740*088332b5SXin Li 9741*088332b5SXin Li<p> 9742*088332b5SXin LiFunctions that interpret byte sequences only accept 9743*088332b5SXin Livalid sequences (well formed and not overlong). 9744*088332b5SXin LiBy default, they only accept byte sequences 9745*088332b5SXin Lithat result in valid Unicode code points, 9746*088332b5SXin Lirejecting values greater than <code>10FFFF</code> and surrogates. 9747*088332b5SXin LiA boolean argument <code>lax</code>, when available, 9748*088332b5SXin Lilifts these checks, 9749*088332b5SXin Liso that all values up to <code>0x7FFFFFFF</code> are accepted. 9750*088332b5SXin Li(Not well formed and overlong sequences are still rejected.) 9751*088332b5SXin Li 9752*088332b5SXin Li 9753*088332b5SXin Li<p> 9754*088332b5SXin Li<hr><h3><a name="pdf-utf8.char"><code>utf8.char (···)</code></a></h3> 9755*088332b5SXin Li 9756*088332b5SXin Li 9757*088332b5SXin Li<p> 9758*088332b5SXin LiReceives zero or more integers, 9759*088332b5SXin Liconverts each one to its corresponding UTF-8 byte sequence 9760*088332b5SXin Liand returns a string with the concatenation of all these sequences. 9761*088332b5SXin Li 9762*088332b5SXin Li 9763*088332b5SXin Li 9764*088332b5SXin Li 9765*088332b5SXin Li<p> 9766*088332b5SXin Li<hr><h3><a name="pdf-utf8.charpattern"><code>utf8.charpattern</code></a></h3> 9767*088332b5SXin Li 9768*088332b5SXin Li 9769*088332b5SXin Li<p> 9770*088332b5SXin LiThe pattern (a string, not a function) "<code>[\0-\x7F\xC2-\xFD][\x80-\xBF]*</code>" 9771*088332b5SXin Li(see <a href="#6.4.1">§6.4.1</a>), 9772*088332b5SXin Liwhich matches exactly one UTF-8 byte sequence, 9773*088332b5SXin Liassuming that the subject is a valid UTF-8 string. 9774*088332b5SXin Li 9775*088332b5SXin Li 9776*088332b5SXin Li 9777*088332b5SXin Li 9778*088332b5SXin Li<p> 9779*088332b5SXin Li<hr><h3><a name="pdf-utf8.codes"><code>utf8.codes (s [, lax])</code></a></h3> 9780*088332b5SXin Li 9781*088332b5SXin Li 9782*088332b5SXin Li<p> 9783*088332b5SXin LiReturns values so that the construction 9784*088332b5SXin Li 9785*088332b5SXin Li<pre> 9786*088332b5SXin Li for p, c in utf8.codes(s) do <em>body</em> end 9787*088332b5SXin Li</pre><p> 9788*088332b5SXin Liwill iterate over all UTF-8 characters in string <code>s</code>, 9789*088332b5SXin Liwith <code>p</code> being the position (in bytes) and <code>c</code> the code point 9790*088332b5SXin Liof each character. 9791*088332b5SXin LiIt raises an error if it meets any invalid byte sequence. 9792*088332b5SXin Li 9793*088332b5SXin Li 9794*088332b5SXin Li 9795*088332b5SXin Li 9796*088332b5SXin Li<p> 9797*088332b5SXin Li<hr><h3><a name="pdf-utf8.codepoint"><code>utf8.codepoint (s [, i [, j [, lax]]])</code></a></h3> 9798*088332b5SXin Li 9799*088332b5SXin Li 9800*088332b5SXin Li<p> 9801*088332b5SXin LiReturns the code points (as integers) from all characters in <code>s</code> 9802*088332b5SXin Lithat start between byte position <code>i</code> and <code>j</code> (both included). 9803*088332b5SXin LiThe default for <code>i</code> is 1 and for <code>j</code> is <code>i</code>. 9804*088332b5SXin LiIt raises an error if it meets any invalid byte sequence. 9805*088332b5SXin Li 9806*088332b5SXin Li 9807*088332b5SXin Li 9808*088332b5SXin Li 9809*088332b5SXin Li<p> 9810*088332b5SXin Li<hr><h3><a name="pdf-utf8.len"><code>utf8.len (s [, i [, j [, lax]]])</code></a></h3> 9811*088332b5SXin Li 9812*088332b5SXin Li 9813*088332b5SXin Li<p> 9814*088332b5SXin LiReturns the number of UTF-8 characters in string <code>s</code> 9815*088332b5SXin Lithat start between positions <code>i</code> and <code>j</code> (both inclusive). 9816*088332b5SXin LiThe default for <code>i</code> is 1 and for <code>j</code> is -1. 9817*088332b5SXin LiIf it finds any invalid byte sequence, 9818*088332b5SXin Lireturns <b>fail</b> plus the position of the first invalid byte. 9819*088332b5SXin Li 9820*088332b5SXin Li 9821*088332b5SXin Li 9822*088332b5SXin Li 9823*088332b5SXin Li<p> 9824*088332b5SXin Li<hr><h3><a name="pdf-utf8.offset"><code>utf8.offset (s, n [, i])</code></a></h3> 9825*088332b5SXin Li 9826*088332b5SXin Li 9827*088332b5SXin Li<p> 9828*088332b5SXin LiReturns the position (in bytes) where the encoding of the 9829*088332b5SXin Li<code>n</code>-th character of <code>s</code> 9830*088332b5SXin Li(counting from position <code>i</code>) starts. 9831*088332b5SXin LiA negative <code>n</code> gets characters before position <code>i</code>. 9832*088332b5SXin LiThe default for <code>i</code> is 1 when <code>n</code> is non-negative 9833*088332b5SXin Liand <code>#s + 1</code> otherwise, 9834*088332b5SXin Liso that <code>utf8.offset(s, -n)</code> gets the offset of the 9835*088332b5SXin Li<code>n</code>-th character from the end of the string. 9836*088332b5SXin LiIf the specified character is neither in the subject 9837*088332b5SXin Linor right after its end, 9838*088332b5SXin Lithe function returns <b>fail</b>. 9839*088332b5SXin Li 9840*088332b5SXin Li 9841*088332b5SXin Li<p> 9842*088332b5SXin LiAs a special case, 9843*088332b5SXin Liwhen <code>n</code> is 0 the function returns the start of the encoding 9844*088332b5SXin Liof the character that contains the <code>i</code>-th byte of <code>s</code>. 9845*088332b5SXin Li 9846*088332b5SXin Li 9847*088332b5SXin Li<p> 9848*088332b5SXin LiThis function assumes that <code>s</code> is a valid UTF-8 string. 9849*088332b5SXin Li 9850*088332b5SXin Li 9851*088332b5SXin Li 9852*088332b5SXin Li 9853*088332b5SXin Li 9854*088332b5SXin Li 9855*088332b5SXin Li 9856*088332b5SXin Li<h2>6.6 – <a name="6.6">Table Manipulation</a></h2> 9857*088332b5SXin Li 9858*088332b5SXin Li<p> 9859*088332b5SXin LiThis library provides generic functions for table manipulation. 9860*088332b5SXin LiIt provides all its functions inside the table <a name="pdf-table"><code>table</code></a>. 9861*088332b5SXin Li 9862*088332b5SXin Li 9863*088332b5SXin Li<p> 9864*088332b5SXin LiRemember that, whenever an operation needs the length of a table, 9865*088332b5SXin Liall caveats about the length operator apply (see <a href="#3.4.7">§3.4.7</a>). 9866*088332b5SXin LiAll functions ignore non-numeric keys 9867*088332b5SXin Liin the tables given as arguments. 9868*088332b5SXin Li 9869*088332b5SXin Li 9870*088332b5SXin Li<p> 9871*088332b5SXin Li<hr><h3><a name="pdf-table.concat"><code>table.concat (list [, sep [, i [, j]]])</code></a></h3> 9872*088332b5SXin Li 9873*088332b5SXin Li 9874*088332b5SXin Li<p> 9875*088332b5SXin LiGiven a list where all elements are strings or numbers, 9876*088332b5SXin Lireturns the string <code>list[i]..sep..list[i+1] ··· sep..list[j]</code>. 9877*088332b5SXin LiThe default value for <code>sep</code> is the empty string, 9878*088332b5SXin Lithe default for <code>i</code> is 1, 9879*088332b5SXin Liand the default for <code>j</code> is <code>#list</code>. 9880*088332b5SXin LiIf <code>i</code> is greater than <code>j</code>, returns the empty string. 9881*088332b5SXin Li 9882*088332b5SXin Li 9883*088332b5SXin Li 9884*088332b5SXin Li 9885*088332b5SXin Li<p> 9886*088332b5SXin Li<hr><h3><a name="pdf-table.insert"><code>table.insert (list, [pos,] value)</code></a></h3> 9887*088332b5SXin Li 9888*088332b5SXin Li 9889*088332b5SXin Li<p> 9890*088332b5SXin LiInserts element <code>value</code> at position <code>pos</code> in <code>list</code>, 9891*088332b5SXin Lishifting up the elements 9892*088332b5SXin Li<code>list[pos], list[pos+1], ···, list[#list]</code>. 9893*088332b5SXin LiThe default value for <code>pos</code> is <code>#list+1</code>, 9894*088332b5SXin Liso that a call <code>table.insert(t,x)</code> inserts <code>x</code> at the end 9895*088332b5SXin Liof the list <code>t</code>. 9896*088332b5SXin Li 9897*088332b5SXin Li 9898*088332b5SXin Li 9899*088332b5SXin Li 9900*088332b5SXin Li<p> 9901*088332b5SXin Li<hr><h3><a name="pdf-table.move"><code>table.move (a1, f, e, t [,a2])</code></a></h3> 9902*088332b5SXin Li 9903*088332b5SXin Li 9904*088332b5SXin Li<p> 9905*088332b5SXin LiMoves elements from the table <code>a1</code> to the table <code>a2</code>, 9906*088332b5SXin Liperforming the equivalent to the following 9907*088332b5SXin Limultiple assignment: 9908*088332b5SXin Li<code>a2[t],··· = a1[f],···,a1[e]</code>. 9909*088332b5SXin LiThe default for <code>a2</code> is <code>a1</code>. 9910*088332b5SXin LiThe destination range can overlap with the source range. 9911*088332b5SXin LiThe number of elements to be moved must fit in a Lua integer. 9912*088332b5SXin Li 9913*088332b5SXin Li 9914*088332b5SXin Li<p> 9915*088332b5SXin LiReturns the destination table <code>a2</code>. 9916*088332b5SXin Li 9917*088332b5SXin Li 9918*088332b5SXin Li 9919*088332b5SXin Li 9920*088332b5SXin Li<p> 9921*088332b5SXin Li<hr><h3><a name="pdf-table.pack"><code>table.pack (···)</code></a></h3> 9922*088332b5SXin Li 9923*088332b5SXin Li 9924*088332b5SXin Li<p> 9925*088332b5SXin LiReturns a new table with all arguments stored into keys 1, 2, etc. 9926*088332b5SXin Liand with a field "<code>n</code>" with the total number of arguments. 9927*088332b5SXin LiNote that the resulting table may not be a sequence, 9928*088332b5SXin Liif some arguments are <b>nil</b>. 9929*088332b5SXin Li 9930*088332b5SXin Li 9931*088332b5SXin Li 9932*088332b5SXin Li 9933*088332b5SXin Li<p> 9934*088332b5SXin Li<hr><h3><a name="pdf-table.remove"><code>table.remove (list [, pos])</code></a></h3> 9935*088332b5SXin Li 9936*088332b5SXin Li 9937*088332b5SXin Li<p> 9938*088332b5SXin LiRemoves from <code>list</code> the element at position <code>pos</code>, 9939*088332b5SXin Lireturning the value of the removed element. 9940*088332b5SXin LiWhen <code>pos</code> is an integer between 1 and <code>#list</code>, 9941*088332b5SXin Liit shifts down the elements 9942*088332b5SXin Li<code>list[pos+1], list[pos+2], ···, list[#list]</code> 9943*088332b5SXin Liand erases element <code>list[#list]</code>; 9944*088332b5SXin LiThe index <code>pos</code> can also be 0 when <code>#list</code> is 0, 9945*088332b5SXin Lior <code>#list + 1</code>. 9946*088332b5SXin Li 9947*088332b5SXin Li 9948*088332b5SXin Li<p> 9949*088332b5SXin LiThe default value for <code>pos</code> is <code>#list</code>, 9950*088332b5SXin Liso that a call <code>table.remove(l)</code> removes the last element 9951*088332b5SXin Liof the list <code>l</code>. 9952*088332b5SXin Li 9953*088332b5SXin Li 9954*088332b5SXin Li 9955*088332b5SXin Li 9956*088332b5SXin Li<p> 9957*088332b5SXin Li<hr><h3><a name="pdf-table.sort"><code>table.sort (list [, comp])</code></a></h3> 9958*088332b5SXin Li 9959*088332b5SXin Li 9960*088332b5SXin Li<p> 9961*088332b5SXin LiSorts the list elements in a given order, <em>in-place</em>, 9962*088332b5SXin Lifrom <code>list[1]</code> to <code>list[#list]</code>. 9963*088332b5SXin LiIf <code>comp</code> is given, 9964*088332b5SXin Lithen it must be a function that receives two list elements 9965*088332b5SXin Liand returns true when the first element must come 9966*088332b5SXin Libefore the second in the final order 9967*088332b5SXin Li(so that, after the sort, 9968*088332b5SXin Li<code>i < j</code> implies <code>not comp(list[j],list[i])</code>). 9969*088332b5SXin LiIf <code>comp</code> is not given, 9970*088332b5SXin Lithen the standard Lua operator <code><</code> is used instead. 9971*088332b5SXin Li 9972*088332b5SXin Li 9973*088332b5SXin Li<p> 9974*088332b5SXin LiNote that the <code>comp</code> function must define 9975*088332b5SXin Lia strict partial order over the elements in the list; 9976*088332b5SXin Lithat is, it must be asymmetric and transitive. 9977*088332b5SXin LiOtherwise, no valid sort may be possible. 9978*088332b5SXin Li 9979*088332b5SXin Li 9980*088332b5SXin Li<p> 9981*088332b5SXin LiThe sort algorithm is not stable: 9982*088332b5SXin Lielements considered equal by the given order 9983*088332b5SXin Limay have their relative positions changed by the sort. 9984*088332b5SXin Li 9985*088332b5SXin Li 9986*088332b5SXin Li 9987*088332b5SXin Li 9988*088332b5SXin Li<p> 9989*088332b5SXin Li<hr><h3><a name="pdf-table.unpack"><code>table.unpack (list [, i [, j]])</code></a></h3> 9990*088332b5SXin Li 9991*088332b5SXin Li 9992*088332b5SXin Li<p> 9993*088332b5SXin LiReturns the elements from the given list. 9994*088332b5SXin LiThis function is equivalent to 9995*088332b5SXin Li 9996*088332b5SXin Li<pre> 9997*088332b5SXin Li return list[i], list[i+1], ···, list[j] 9998*088332b5SXin Li</pre><p> 9999*088332b5SXin LiBy default, <code>i</code> is 1 and <code>j</code> is <code>#list</code>. 10000*088332b5SXin Li 10001*088332b5SXin Li 10002*088332b5SXin Li 10003*088332b5SXin Li 10004*088332b5SXin Li 10005*088332b5SXin Li 10006*088332b5SXin Li 10007*088332b5SXin Li<h2>6.7 – <a name="6.7">Mathematical Functions</a></h2> 10008*088332b5SXin Li 10009*088332b5SXin Li<p> 10010*088332b5SXin LiThis library provides basic mathematical functions. 10011*088332b5SXin LiIt provides all its functions and constants inside the table <a name="pdf-math"><code>math</code></a>. 10012*088332b5SXin LiFunctions with the annotation "<code>integer/float</code>" give 10013*088332b5SXin Liinteger results for integer arguments 10014*088332b5SXin Liand float results for non-integer arguments. 10015*088332b5SXin LiThe rounding functions 10016*088332b5SXin Li<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> 10017*088332b5SXin Lireturn an integer when the result fits in the range of an integer, 10018*088332b5SXin Lior a float otherwise. 10019*088332b5SXin Li 10020*088332b5SXin Li 10021*088332b5SXin Li<p> 10022*088332b5SXin Li<hr><h3><a name="pdf-math.abs"><code>math.abs (x)</code></a></h3> 10023*088332b5SXin Li 10024*088332b5SXin Li 10025*088332b5SXin Li<p> 10026*088332b5SXin LiReturns the maximum value between <code>x</code> and <code>-x</code>. (integer/float) 10027*088332b5SXin Li 10028*088332b5SXin Li 10029*088332b5SXin Li 10030*088332b5SXin Li 10031*088332b5SXin Li<p> 10032*088332b5SXin Li<hr><h3><a name="pdf-math.acos"><code>math.acos (x)</code></a></h3> 10033*088332b5SXin Li 10034*088332b5SXin Li 10035*088332b5SXin Li<p> 10036*088332b5SXin LiReturns the arc cosine of <code>x</code> (in radians). 10037*088332b5SXin Li 10038*088332b5SXin Li 10039*088332b5SXin Li 10040*088332b5SXin Li 10041*088332b5SXin Li<p> 10042*088332b5SXin Li<hr><h3><a name="pdf-math.asin"><code>math.asin (x)</code></a></h3> 10043*088332b5SXin Li 10044*088332b5SXin Li 10045*088332b5SXin Li<p> 10046*088332b5SXin LiReturns the arc sine of <code>x</code> (in radians). 10047*088332b5SXin Li 10048*088332b5SXin Li 10049*088332b5SXin Li 10050*088332b5SXin Li 10051*088332b5SXin Li<p> 10052*088332b5SXin Li<hr><h3><a name="pdf-math.atan"><code>math.atan (y [, x])</code></a></h3> 10053*088332b5SXin Li 10054*088332b5SXin Li 10055*088332b5SXin Li<p> 10056*088332b5SXin Li 10057*088332b5SXin LiReturns the arc tangent of <code>y/x</code> (in radians), 10058*088332b5SXin Libut uses the signs of both arguments to find the 10059*088332b5SXin Liquadrant of the result. 10060*088332b5SXin LiIt also handles correctly the case of <code>x</code> being zero. 10061*088332b5SXin Li 10062*088332b5SXin Li 10063*088332b5SXin Li<p> 10064*088332b5SXin LiThe default value for <code>x</code> is 1, 10065*088332b5SXin Liso that the call <code>math.atan(y)</code> 10066*088332b5SXin Lireturns the arc tangent of <code>y</code>. 10067*088332b5SXin Li 10068*088332b5SXin Li 10069*088332b5SXin Li 10070*088332b5SXin Li 10071*088332b5SXin Li<p> 10072*088332b5SXin Li<hr><h3><a name="pdf-math.ceil"><code>math.ceil (x)</code></a></h3> 10073*088332b5SXin Li 10074*088332b5SXin Li 10075*088332b5SXin Li<p> 10076*088332b5SXin LiReturns the smallest integral value greater than or equal to <code>x</code>. 10077*088332b5SXin Li 10078*088332b5SXin Li 10079*088332b5SXin Li 10080*088332b5SXin Li 10081*088332b5SXin Li<p> 10082*088332b5SXin Li<hr><h3><a name="pdf-math.cos"><code>math.cos (x)</code></a></h3> 10083*088332b5SXin Li 10084*088332b5SXin Li 10085*088332b5SXin Li<p> 10086*088332b5SXin LiReturns the cosine of <code>x</code> (assumed to be in radians). 10087*088332b5SXin Li 10088*088332b5SXin Li 10089*088332b5SXin Li 10090*088332b5SXin Li 10091*088332b5SXin Li<p> 10092*088332b5SXin Li<hr><h3><a name="pdf-math.deg"><code>math.deg (x)</code></a></h3> 10093*088332b5SXin Li 10094*088332b5SXin Li 10095*088332b5SXin Li<p> 10096*088332b5SXin LiConverts the angle <code>x</code> from radians to degrees. 10097*088332b5SXin Li 10098*088332b5SXin Li 10099*088332b5SXin Li 10100*088332b5SXin Li 10101*088332b5SXin Li<p> 10102*088332b5SXin Li<hr><h3><a name="pdf-math.exp"><code>math.exp (x)</code></a></h3> 10103*088332b5SXin Li 10104*088332b5SXin Li 10105*088332b5SXin Li<p> 10106*088332b5SXin LiReturns the value <em>e<sup>x</sup></em> 10107*088332b5SXin Li(where <code>e</code> is the base of natural logarithms). 10108*088332b5SXin Li 10109*088332b5SXin Li 10110*088332b5SXin Li 10111*088332b5SXin Li 10112*088332b5SXin Li<p> 10113*088332b5SXin Li<hr><h3><a name="pdf-math.floor"><code>math.floor (x)</code></a></h3> 10114*088332b5SXin Li 10115*088332b5SXin Li 10116*088332b5SXin Li<p> 10117*088332b5SXin LiReturns the largest integral value less than or equal to <code>x</code>. 10118*088332b5SXin Li 10119*088332b5SXin Li 10120*088332b5SXin Li 10121*088332b5SXin Li 10122*088332b5SXin Li<p> 10123*088332b5SXin Li<hr><h3><a name="pdf-math.fmod"><code>math.fmod (x, y)</code></a></h3> 10124*088332b5SXin Li 10125*088332b5SXin Li 10126*088332b5SXin Li<p> 10127*088332b5SXin LiReturns the remainder of the division of <code>x</code> by <code>y</code> 10128*088332b5SXin Lithat rounds the quotient towards zero. (integer/float) 10129*088332b5SXin Li 10130*088332b5SXin Li 10131*088332b5SXin Li 10132*088332b5SXin Li 10133*088332b5SXin Li<p> 10134*088332b5SXin Li<hr><h3><a name="pdf-math.huge"><code>math.huge</code></a></h3> 10135*088332b5SXin Li 10136*088332b5SXin Li 10137*088332b5SXin Li<p> 10138*088332b5SXin LiThe float value <code>HUGE_VAL</code>, 10139*088332b5SXin Lia value greater than any other numeric value. 10140*088332b5SXin Li 10141*088332b5SXin Li 10142*088332b5SXin Li 10143*088332b5SXin Li 10144*088332b5SXin Li<p> 10145*088332b5SXin Li<hr><h3><a name="pdf-math.log"><code>math.log (x [, base])</code></a></h3> 10146*088332b5SXin Li 10147*088332b5SXin Li 10148*088332b5SXin Li<p> 10149*088332b5SXin LiReturns the logarithm of <code>x</code> in the given base. 10150*088332b5SXin LiThe default for <code>base</code> is <em>e</em> 10151*088332b5SXin Li(so that the function returns the natural logarithm of <code>x</code>). 10152*088332b5SXin Li 10153*088332b5SXin Li 10154*088332b5SXin Li 10155*088332b5SXin Li 10156*088332b5SXin Li<p> 10157*088332b5SXin Li<hr><h3><a name="pdf-math.max"><code>math.max (x, ···)</code></a></h3> 10158*088332b5SXin Li 10159*088332b5SXin Li 10160*088332b5SXin Li<p> 10161*088332b5SXin LiReturns the argument with the maximum value, 10162*088332b5SXin Liaccording to the Lua operator <code><</code>. 10163*088332b5SXin Li 10164*088332b5SXin Li 10165*088332b5SXin Li 10166*088332b5SXin Li 10167*088332b5SXin Li<p> 10168*088332b5SXin Li<hr><h3><a name="pdf-math.maxinteger"><code>math.maxinteger</code></a></h3> 10169*088332b5SXin LiAn integer with the maximum value for an integer. 10170*088332b5SXin Li 10171*088332b5SXin Li 10172*088332b5SXin Li 10173*088332b5SXin Li 10174*088332b5SXin Li<p> 10175*088332b5SXin Li<hr><h3><a name="pdf-math.min"><code>math.min (x, ···)</code></a></h3> 10176*088332b5SXin Li 10177*088332b5SXin Li 10178*088332b5SXin Li<p> 10179*088332b5SXin LiReturns the argument with the minimum value, 10180*088332b5SXin Liaccording to the Lua operator <code><</code>. 10181*088332b5SXin Li 10182*088332b5SXin Li 10183*088332b5SXin Li 10184*088332b5SXin Li 10185*088332b5SXin Li<p> 10186*088332b5SXin Li<hr><h3><a name="pdf-math.mininteger"><code>math.mininteger</code></a></h3> 10187*088332b5SXin LiAn integer with the minimum value for an integer. 10188*088332b5SXin Li 10189*088332b5SXin Li 10190*088332b5SXin Li 10191*088332b5SXin Li 10192*088332b5SXin Li<p> 10193*088332b5SXin Li<hr><h3><a name="pdf-math.modf"><code>math.modf (x)</code></a></h3> 10194*088332b5SXin Li 10195*088332b5SXin Li 10196*088332b5SXin Li<p> 10197*088332b5SXin LiReturns the integral part of <code>x</code> and the fractional part of <code>x</code>. 10198*088332b5SXin LiIts second result is always a float. 10199*088332b5SXin Li 10200*088332b5SXin Li 10201*088332b5SXin Li 10202*088332b5SXin Li 10203*088332b5SXin Li<p> 10204*088332b5SXin Li<hr><h3><a name="pdf-math.pi"><code>math.pi</code></a></h3> 10205*088332b5SXin Li 10206*088332b5SXin Li 10207*088332b5SXin Li<p> 10208*088332b5SXin LiThe value of <em>π</em>. 10209*088332b5SXin Li 10210*088332b5SXin Li 10211*088332b5SXin Li 10212*088332b5SXin Li 10213*088332b5SXin Li<p> 10214*088332b5SXin Li<hr><h3><a name="pdf-math.rad"><code>math.rad (x)</code></a></h3> 10215*088332b5SXin Li 10216*088332b5SXin Li 10217*088332b5SXin Li<p> 10218*088332b5SXin LiConverts the angle <code>x</code> from degrees to radians. 10219*088332b5SXin Li 10220*088332b5SXin Li 10221*088332b5SXin Li 10222*088332b5SXin Li 10223*088332b5SXin Li<p> 10224*088332b5SXin Li<hr><h3><a name="pdf-math.random"><code>math.random ([m [, n]])</code></a></h3> 10225*088332b5SXin Li 10226*088332b5SXin Li 10227*088332b5SXin Li<p> 10228*088332b5SXin LiWhen called without arguments, 10229*088332b5SXin Lireturns a pseudo-random float with uniform distribution 10230*088332b5SXin Liin the range <em>[0,1)</em>. 10231*088332b5SXin LiWhen called with two integers <code>m</code> and <code>n</code>, 10232*088332b5SXin Li<code>math.random</code> returns a pseudo-random integer 10233*088332b5SXin Liwith uniform distribution in the range <em>[m, n]</em>. 10234*088332b5SXin LiThe call <code>math.random(n)</code>, for a positive <code>n</code>, 10235*088332b5SXin Liis equivalent to <code>math.random(1,n)</code>. 10236*088332b5SXin LiThe call <code>math.random(0)</code> produces an integer with 10237*088332b5SXin Liall bits (pseudo)random. 10238*088332b5SXin Li 10239*088332b5SXin Li 10240*088332b5SXin Li<p> 10241*088332b5SXin LiThis function uses the <code>xoshiro256**</code> algorithm to produce 10242*088332b5SXin Lipseudo-random 64-bit integers, 10243*088332b5SXin Liwhich are the results of calls with argument 0. 10244*088332b5SXin LiOther results (ranges and floats) 10245*088332b5SXin Liare unbiased extracted from these integers. 10246*088332b5SXin Li 10247*088332b5SXin Li 10248*088332b5SXin Li<p> 10249*088332b5SXin LiLua initializes its pseudo-random generator with the equivalent of 10250*088332b5SXin Lia call to <a href="#pdf-math.randomseed"><code>math.randomseed</code></a> with no arguments, 10251*088332b5SXin Liso that <code>math.random</code> should generate 10252*088332b5SXin Lidifferent sequences of results each time the program runs. 10253*088332b5SXin Li 10254*088332b5SXin Li 10255*088332b5SXin Li 10256*088332b5SXin Li 10257*088332b5SXin Li<p> 10258*088332b5SXin Li<hr><h3><a name="pdf-math.randomseed"><code>math.randomseed ([x [, y]])</code></a></h3> 10259*088332b5SXin Li 10260*088332b5SXin Li 10261*088332b5SXin Li<p> 10262*088332b5SXin LiWhen called with at least one argument, 10263*088332b5SXin Lithe integer parameters <code>x</code> and <code>y</code> are 10264*088332b5SXin Lijoined into a 128-bit <em>seed</em> that 10265*088332b5SXin Liis used to reinitialize the pseudo-random generator; 10266*088332b5SXin Liequal seeds produce equal sequences of numbers. 10267*088332b5SXin LiThe default for <code>y</code> is zero. 10268*088332b5SXin Li 10269*088332b5SXin Li 10270*088332b5SXin Li<p> 10271*088332b5SXin LiWhen called with no arguments, 10272*088332b5SXin LiLua generates a seed with 10273*088332b5SXin Lia weak attempt for randomness. 10274*088332b5SXin Li 10275*088332b5SXin Li 10276*088332b5SXin Li<p> 10277*088332b5SXin LiThis function returns the two seed components 10278*088332b5SXin Lithat were effectively used, 10279*088332b5SXin Liso that setting them again repeats the sequence. 10280*088332b5SXin Li 10281*088332b5SXin Li 10282*088332b5SXin Li<p> 10283*088332b5SXin LiTo ensure a required level of randomness to the initial state 10284*088332b5SXin Li(or contrarily, to have a deterministic sequence, 10285*088332b5SXin Lifor instance when debugging a program), 10286*088332b5SXin Liyou should call <a href="#pdf-math.randomseed"><code>math.randomseed</code></a> with explicit arguments. 10287*088332b5SXin Li 10288*088332b5SXin Li 10289*088332b5SXin Li 10290*088332b5SXin Li 10291*088332b5SXin Li<p> 10292*088332b5SXin Li<hr><h3><a name="pdf-math.sin"><code>math.sin (x)</code></a></h3> 10293*088332b5SXin Li 10294*088332b5SXin Li 10295*088332b5SXin Li<p> 10296*088332b5SXin LiReturns the sine of <code>x</code> (assumed to be in radians). 10297*088332b5SXin Li 10298*088332b5SXin Li 10299*088332b5SXin Li 10300*088332b5SXin Li 10301*088332b5SXin Li<p> 10302*088332b5SXin Li<hr><h3><a name="pdf-math.sqrt"><code>math.sqrt (x)</code></a></h3> 10303*088332b5SXin Li 10304*088332b5SXin Li 10305*088332b5SXin Li<p> 10306*088332b5SXin LiReturns the square root of <code>x</code>. 10307*088332b5SXin Li(You can also use the expression <code>x^0.5</code> to compute this value.) 10308*088332b5SXin Li 10309*088332b5SXin Li 10310*088332b5SXin Li 10311*088332b5SXin Li 10312*088332b5SXin Li<p> 10313*088332b5SXin Li<hr><h3><a name="pdf-math.tan"><code>math.tan (x)</code></a></h3> 10314*088332b5SXin Li 10315*088332b5SXin Li 10316*088332b5SXin Li<p> 10317*088332b5SXin LiReturns the tangent of <code>x</code> (assumed to be in radians). 10318*088332b5SXin Li 10319*088332b5SXin Li 10320*088332b5SXin Li 10321*088332b5SXin Li 10322*088332b5SXin Li<p> 10323*088332b5SXin Li<hr><h3><a name="pdf-math.tointeger"><code>math.tointeger (x)</code></a></h3> 10324*088332b5SXin Li 10325*088332b5SXin Li 10326*088332b5SXin Li<p> 10327*088332b5SXin LiIf the value <code>x</code> is convertible to an integer, 10328*088332b5SXin Lireturns that integer. 10329*088332b5SXin LiOtherwise, returns <b>fail</b>. 10330*088332b5SXin Li 10331*088332b5SXin Li 10332*088332b5SXin Li 10333*088332b5SXin Li 10334*088332b5SXin Li<p> 10335*088332b5SXin Li<hr><h3><a name="pdf-math.type"><code>math.type (x)</code></a></h3> 10336*088332b5SXin Li 10337*088332b5SXin Li 10338*088332b5SXin Li<p> 10339*088332b5SXin LiReturns "<code>integer</code>" if <code>x</code> is an integer, 10340*088332b5SXin Li"<code>float</code>" if it is a float, 10341*088332b5SXin Lior <b>fail</b> if <code>x</code> is not a number. 10342*088332b5SXin Li 10343*088332b5SXin Li 10344*088332b5SXin Li 10345*088332b5SXin Li 10346*088332b5SXin Li<p> 10347*088332b5SXin Li<hr><h3><a name="pdf-math.ult"><code>math.ult (m, n)</code></a></h3> 10348*088332b5SXin Li 10349*088332b5SXin Li 10350*088332b5SXin Li<p> 10351*088332b5SXin LiReturns a boolean, 10352*088332b5SXin Litrue if and only if integer <code>m</code> is below integer <code>n</code> when 10353*088332b5SXin Lithey are compared as unsigned integers. 10354*088332b5SXin Li 10355*088332b5SXin Li 10356*088332b5SXin Li 10357*088332b5SXin Li 10358*088332b5SXin Li 10359*088332b5SXin Li 10360*088332b5SXin Li 10361*088332b5SXin Li<h2>6.8 – <a name="6.8">Input and Output Facilities</a></h2> 10362*088332b5SXin Li 10363*088332b5SXin Li<p> 10364*088332b5SXin LiThe I/O library provides two different styles for file manipulation. 10365*088332b5SXin LiThe first one uses implicit file handles; 10366*088332b5SXin Lithat is, there are operations to set a default input file and a 10367*088332b5SXin Lidefault output file, 10368*088332b5SXin Liand all input/output operations are done over these default files. 10369*088332b5SXin LiThe second style uses explicit file handles. 10370*088332b5SXin Li 10371*088332b5SXin Li 10372*088332b5SXin Li<p> 10373*088332b5SXin LiWhen using implicit file handles, 10374*088332b5SXin Liall operations are supplied by table <a name="pdf-io"><code>io</code></a>. 10375*088332b5SXin LiWhen using explicit file handles, 10376*088332b5SXin Lithe operation <a href="#pdf-io.open"><code>io.open</code></a> returns a file handle 10377*088332b5SXin Liand then all operations are supplied as methods of the file handle. 10378*088332b5SXin Li 10379*088332b5SXin Li 10380*088332b5SXin Li<p> 10381*088332b5SXin LiThe metatable for file handles provides metamethods 10382*088332b5SXin Lifor <code>__gc</code> and <code>__close</code> that try 10383*088332b5SXin Lito close the file when called. 10384*088332b5SXin Li 10385*088332b5SXin Li 10386*088332b5SXin Li<p> 10387*088332b5SXin LiThe table <code>io</code> also provides 10388*088332b5SXin Lithree predefined file handles with their usual meanings from C: 10389*088332b5SXin Li<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>. 10390*088332b5SXin LiThe I/O library never closes these files. 10391*088332b5SXin Li 10392*088332b5SXin Li 10393*088332b5SXin Li<p> 10394*088332b5SXin LiUnless otherwise stated, 10395*088332b5SXin Liall I/O functions return <b>fail</b> on failure, 10396*088332b5SXin Liplus an error message as a second result and 10397*088332b5SXin Lia system-dependent error code as a third result, 10398*088332b5SXin Liand some non-false value on success. 10399*088332b5SXin LiOn non-POSIX systems, 10400*088332b5SXin Lithe computation of the error message and error code 10401*088332b5SXin Liin case of errors 10402*088332b5SXin Limay be not thread safe, 10403*088332b5SXin Libecause they rely on the global C variable <code>errno</code>. 10404*088332b5SXin Li 10405*088332b5SXin Li 10406*088332b5SXin Li<p> 10407*088332b5SXin Li<hr><h3><a name="pdf-io.close"><code>io.close ([file])</code></a></h3> 10408*088332b5SXin Li 10409*088332b5SXin Li 10410*088332b5SXin Li<p> 10411*088332b5SXin LiEquivalent to <code>file:close()</code>. 10412*088332b5SXin LiWithout a <code>file</code>, closes the default output file. 10413*088332b5SXin Li 10414*088332b5SXin Li 10415*088332b5SXin Li 10416*088332b5SXin Li 10417*088332b5SXin Li<p> 10418*088332b5SXin Li<hr><h3><a name="pdf-io.flush"><code>io.flush ()</code></a></h3> 10419*088332b5SXin Li 10420*088332b5SXin Li 10421*088332b5SXin Li<p> 10422*088332b5SXin LiEquivalent to <code>io.output():flush()</code>. 10423*088332b5SXin Li 10424*088332b5SXin Li 10425*088332b5SXin Li 10426*088332b5SXin Li 10427*088332b5SXin Li<p> 10428*088332b5SXin Li<hr><h3><a name="pdf-io.input"><code>io.input ([file])</code></a></h3> 10429*088332b5SXin Li 10430*088332b5SXin Li 10431*088332b5SXin Li<p> 10432*088332b5SXin LiWhen called with a file name, it opens the named file (in text mode), 10433*088332b5SXin Liand sets its handle as the default input file. 10434*088332b5SXin LiWhen called with a file handle, 10435*088332b5SXin Liit simply sets this file handle as the default input file. 10436*088332b5SXin LiWhen called without arguments, 10437*088332b5SXin Liit returns the current default input file. 10438*088332b5SXin Li 10439*088332b5SXin Li 10440*088332b5SXin Li<p> 10441*088332b5SXin LiIn case of errors this function raises the error, 10442*088332b5SXin Liinstead of returning an error code. 10443*088332b5SXin Li 10444*088332b5SXin Li 10445*088332b5SXin Li 10446*088332b5SXin Li 10447*088332b5SXin Li<p> 10448*088332b5SXin Li<hr><h3><a name="pdf-io.lines"><code>io.lines ([filename, ···])</code></a></h3> 10449*088332b5SXin Li 10450*088332b5SXin Li 10451*088332b5SXin Li<p> 10452*088332b5SXin LiOpens the given file name in read mode 10453*088332b5SXin Liand returns an iterator function that 10454*088332b5SXin Liworks like <code>file:lines(···)</code> over the opened file. 10455*088332b5SXin LiWhen the iterator function fails to read any value, 10456*088332b5SXin Liit automatically closes the file. 10457*088332b5SXin LiBesides the iterator function, 10458*088332b5SXin Li<code>io.lines</code> returns three other values: 10459*088332b5SXin Litwo <b>nil</b> values as placeholders, 10460*088332b5SXin Liplus the created file handle. 10461*088332b5SXin LiTherefore, when used in a generic <b>for</b> loop, 10462*088332b5SXin Lithe file is closed also if the loop is interrupted by an 10463*088332b5SXin Lierror or a <b>break</b>. 10464*088332b5SXin Li 10465*088332b5SXin Li 10466*088332b5SXin Li<p> 10467*088332b5SXin LiThe call <code>io.lines()</code> (with no file name) is equivalent 10468*088332b5SXin Lito <code>io.input():lines("l")</code>; 10469*088332b5SXin Lithat is, it iterates over the lines of the default input file. 10470*088332b5SXin LiIn this case, the iterator does not close the file when the loop ends. 10471*088332b5SXin Li 10472*088332b5SXin Li 10473*088332b5SXin Li<p> 10474*088332b5SXin LiIn case of errors opening the file, 10475*088332b5SXin Lithis function raises the error, 10476*088332b5SXin Liinstead of returning an error code. 10477*088332b5SXin Li 10478*088332b5SXin Li 10479*088332b5SXin Li 10480*088332b5SXin Li 10481*088332b5SXin Li<p> 10482*088332b5SXin Li<hr><h3><a name="pdf-io.open"><code>io.open (filename [, mode])</code></a></h3> 10483*088332b5SXin Li 10484*088332b5SXin Li 10485*088332b5SXin Li<p> 10486*088332b5SXin LiThis function opens a file, 10487*088332b5SXin Liin the mode specified in the string <code>mode</code>. 10488*088332b5SXin LiIn case of success, 10489*088332b5SXin Liit returns a new file handle. 10490*088332b5SXin Li 10491*088332b5SXin Li 10492*088332b5SXin Li<p> 10493*088332b5SXin LiThe <code>mode</code> string can be any of the following: 10494*088332b5SXin Li 10495*088332b5SXin Li<ul> 10496*088332b5SXin Li<li><b>"<code>r</code>": </b> read mode (the default);</li> 10497*088332b5SXin Li<li><b>"<code>w</code>": </b> write mode;</li> 10498*088332b5SXin Li<li><b>"<code>a</code>": </b> append mode;</li> 10499*088332b5SXin Li<li><b>"<code>r+</code>": </b> update mode, all previous data is preserved;</li> 10500*088332b5SXin Li<li><b>"<code>w+</code>": </b> update mode, all previous data is erased;</li> 10501*088332b5SXin Li<li><b>"<code>a+</code>": </b> append update mode, previous data is preserved, 10502*088332b5SXin Li writing is only allowed at the end of file.</li> 10503*088332b5SXin Li</ul><p> 10504*088332b5SXin LiThe <code>mode</code> string can also have a '<code>b</code>' at the end, 10505*088332b5SXin Liwhich is needed in some systems to open the file in binary mode. 10506*088332b5SXin Li 10507*088332b5SXin Li 10508*088332b5SXin Li 10509*088332b5SXin Li 10510*088332b5SXin Li<p> 10511*088332b5SXin Li<hr><h3><a name="pdf-io.output"><code>io.output ([file])</code></a></h3> 10512*088332b5SXin Li 10513*088332b5SXin Li 10514*088332b5SXin Li<p> 10515*088332b5SXin LiSimilar to <a href="#pdf-io.input"><code>io.input</code></a>, but operates over the default output file. 10516*088332b5SXin Li 10517*088332b5SXin Li 10518*088332b5SXin Li 10519*088332b5SXin Li 10520*088332b5SXin Li<p> 10521*088332b5SXin Li<hr><h3><a name="pdf-io.popen"><code>io.popen (prog [, mode])</code></a></h3> 10522*088332b5SXin Li 10523*088332b5SXin Li 10524*088332b5SXin Li<p> 10525*088332b5SXin LiThis function is system dependent and is not available 10526*088332b5SXin Lion all platforms. 10527*088332b5SXin Li 10528*088332b5SXin Li 10529*088332b5SXin Li<p> 10530*088332b5SXin LiStarts the program <code>prog</code> in a separated process and returns 10531*088332b5SXin Lia file handle that you can use to read data from this program 10532*088332b5SXin Li(if <code>mode</code> is <code>"r"</code>, the default) 10533*088332b5SXin Lior to write data to this program 10534*088332b5SXin Li(if <code>mode</code> is <code>"w"</code>). 10535*088332b5SXin Li 10536*088332b5SXin Li 10537*088332b5SXin Li 10538*088332b5SXin Li 10539*088332b5SXin Li<p> 10540*088332b5SXin Li<hr><h3><a name="pdf-io.read"><code>io.read (···)</code></a></h3> 10541*088332b5SXin Li 10542*088332b5SXin Li 10543*088332b5SXin Li<p> 10544*088332b5SXin LiEquivalent to <code>io.input():read(···)</code>. 10545*088332b5SXin Li 10546*088332b5SXin Li 10547*088332b5SXin Li 10548*088332b5SXin Li 10549*088332b5SXin Li<p> 10550*088332b5SXin Li<hr><h3><a name="pdf-io.tmpfile"><code>io.tmpfile ()</code></a></h3> 10551*088332b5SXin Li 10552*088332b5SXin Li 10553*088332b5SXin Li<p> 10554*088332b5SXin LiIn case of success, 10555*088332b5SXin Lireturns a handle for a temporary file. 10556*088332b5SXin LiThis file is opened in update mode 10557*088332b5SXin Liand it is automatically removed when the program ends. 10558*088332b5SXin Li 10559*088332b5SXin Li 10560*088332b5SXin Li 10561*088332b5SXin Li 10562*088332b5SXin Li<p> 10563*088332b5SXin Li<hr><h3><a name="pdf-io.type"><code>io.type (obj)</code></a></h3> 10564*088332b5SXin Li 10565*088332b5SXin Li 10566*088332b5SXin Li<p> 10567*088332b5SXin LiChecks whether <code>obj</code> is a valid file handle. 10568*088332b5SXin LiReturns the string <code>"file"</code> if <code>obj</code> is an open file handle, 10569*088332b5SXin Li<code>"closed file"</code> if <code>obj</code> is a closed file handle, 10570*088332b5SXin Lior <b>fail</b> if <code>obj</code> is not a file handle. 10571*088332b5SXin Li 10572*088332b5SXin Li 10573*088332b5SXin Li 10574*088332b5SXin Li 10575*088332b5SXin Li<p> 10576*088332b5SXin Li<hr><h3><a name="pdf-io.write"><code>io.write (···)</code></a></h3> 10577*088332b5SXin Li 10578*088332b5SXin Li 10579*088332b5SXin Li<p> 10580*088332b5SXin LiEquivalent to <code>io.output():write(···)</code>. 10581*088332b5SXin Li 10582*088332b5SXin Li 10583*088332b5SXin Li 10584*088332b5SXin Li 10585*088332b5SXin Li<p> 10586*088332b5SXin Li<hr><h3><a name="pdf-file:close"><code>file:close ()</code></a></h3> 10587*088332b5SXin Li 10588*088332b5SXin Li 10589*088332b5SXin Li<p> 10590*088332b5SXin LiCloses <code>file</code>. 10591*088332b5SXin LiNote that files are automatically closed when 10592*088332b5SXin Litheir handles are garbage collected, 10593*088332b5SXin Libut that takes an unpredictable amount of time to happen. 10594*088332b5SXin Li 10595*088332b5SXin Li 10596*088332b5SXin Li<p> 10597*088332b5SXin LiWhen closing a file handle created with <a href="#pdf-io.popen"><code>io.popen</code></a>, 10598*088332b5SXin Li<a href="#pdf-file:close"><code>file:close</code></a> returns the same values 10599*088332b5SXin Lireturned by <a href="#pdf-os.execute"><code>os.execute</code></a>. 10600*088332b5SXin Li 10601*088332b5SXin Li 10602*088332b5SXin Li 10603*088332b5SXin Li 10604*088332b5SXin Li<p> 10605*088332b5SXin Li<hr><h3><a name="pdf-file:flush"><code>file:flush ()</code></a></h3> 10606*088332b5SXin Li 10607*088332b5SXin Li 10608*088332b5SXin Li<p> 10609*088332b5SXin LiSaves any written data to <code>file</code>. 10610*088332b5SXin Li 10611*088332b5SXin Li 10612*088332b5SXin Li 10613*088332b5SXin Li 10614*088332b5SXin Li<p> 10615*088332b5SXin Li<hr><h3><a name="pdf-file:lines"><code>file:lines (···)</code></a></h3> 10616*088332b5SXin Li 10617*088332b5SXin Li 10618*088332b5SXin Li<p> 10619*088332b5SXin LiReturns an iterator function that, 10620*088332b5SXin Lieach time it is called, 10621*088332b5SXin Lireads the file according to the given formats. 10622*088332b5SXin LiWhen no format is given, 10623*088332b5SXin Liuses "<code>l</code>" as a default. 10624*088332b5SXin LiAs an example, the construction 10625*088332b5SXin Li 10626*088332b5SXin Li<pre> 10627*088332b5SXin Li for c in file:lines(1) do <em>body</em> end 10628*088332b5SXin Li</pre><p> 10629*088332b5SXin Liwill iterate over all characters of the file, 10630*088332b5SXin Listarting at the current position. 10631*088332b5SXin LiUnlike <a href="#pdf-io.lines"><code>io.lines</code></a>, this function does not close the file 10632*088332b5SXin Liwhen the loop ends. 10633*088332b5SXin Li 10634*088332b5SXin Li 10635*088332b5SXin Li 10636*088332b5SXin Li 10637*088332b5SXin Li<p> 10638*088332b5SXin Li<hr><h3><a name="pdf-file:read"><code>file:read (···)</code></a></h3> 10639*088332b5SXin Li 10640*088332b5SXin Li 10641*088332b5SXin Li<p> 10642*088332b5SXin LiReads the file <code>file</code>, 10643*088332b5SXin Liaccording to the given formats, which specify what to read. 10644*088332b5SXin LiFor each format, 10645*088332b5SXin Lithe function returns a string or a number with the characters read, 10646*088332b5SXin Lior <b>fail</b> if it cannot read data with the specified format. 10647*088332b5SXin Li(In this latter case, 10648*088332b5SXin Lithe function does not read subsequent formats.) 10649*088332b5SXin LiWhen called without arguments, 10650*088332b5SXin Liit uses a default format that reads the next line 10651*088332b5SXin Li(see below). 10652*088332b5SXin Li 10653*088332b5SXin Li 10654*088332b5SXin Li<p> 10655*088332b5SXin LiThe available formats are 10656*088332b5SXin Li 10657*088332b5SXin Li<ul> 10658*088332b5SXin Li 10659*088332b5SXin Li<li><b>"<code>n</code>": </b> 10660*088332b5SXin Lireads a numeral and returns it as a float or an integer, 10661*088332b5SXin Lifollowing the lexical conventions of Lua. 10662*088332b5SXin Li(The numeral may have leading whitespaces and a sign.) 10663*088332b5SXin LiThis format always reads the longest input sequence that 10664*088332b5SXin Liis a valid prefix for a numeral; 10665*088332b5SXin Liif that prefix does not form a valid numeral 10666*088332b5SXin Li(e.g., an empty string, "<code>0x</code>", or "<code>3.4e-</code>") 10667*088332b5SXin Lior it is too long (more than 200 characters), 10668*088332b5SXin Liit is discarded and the format returns <b>fail</b>. 10669*088332b5SXin Li</li> 10670*088332b5SXin Li 10671*088332b5SXin Li<li><b>"<code>a</code>": </b> 10672*088332b5SXin Lireads the whole file, starting at the current position. 10673*088332b5SXin LiOn end of file, it returns the empty string; 10674*088332b5SXin Lithis format never fails. 10675*088332b5SXin Li</li> 10676*088332b5SXin Li 10677*088332b5SXin Li<li><b>"<code>l</code>": </b> 10678*088332b5SXin Lireads the next line skipping the end of line, 10679*088332b5SXin Lireturning <b>fail</b> on end of file. 10680*088332b5SXin LiThis is the default format. 10681*088332b5SXin Li</li> 10682*088332b5SXin Li 10683*088332b5SXin Li<li><b>"<code>L</code>": </b> 10684*088332b5SXin Lireads the next line keeping the end-of-line character (if present), 10685*088332b5SXin Lireturning <b>fail</b> on end of file. 10686*088332b5SXin Li</li> 10687*088332b5SXin Li 10688*088332b5SXin Li<li><b><em>number</em>: </b> 10689*088332b5SXin Lireads a string with up to this number of bytes, 10690*088332b5SXin Lireturning <b>fail</b> on end of file. 10691*088332b5SXin LiIf <code>number</code> is zero, 10692*088332b5SXin Liit reads nothing and returns an empty string, 10693*088332b5SXin Lior <b>fail</b> on end of file. 10694*088332b5SXin Li</li> 10695*088332b5SXin Li 10696*088332b5SXin Li</ul><p> 10697*088332b5SXin LiThe formats "<code>l</code>" and "<code>L</code>" should be used only for text files. 10698*088332b5SXin Li 10699*088332b5SXin Li 10700*088332b5SXin Li 10701*088332b5SXin Li 10702*088332b5SXin Li<p> 10703*088332b5SXin Li<hr><h3><a name="pdf-file:seek"><code>file:seek ([whence [, offset]])</code></a></h3> 10704*088332b5SXin Li 10705*088332b5SXin Li 10706*088332b5SXin Li<p> 10707*088332b5SXin LiSets and gets the file position, 10708*088332b5SXin Limeasured from the beginning of the file, 10709*088332b5SXin Lito the position given by <code>offset</code> plus a base 10710*088332b5SXin Lispecified by the string <code>whence</code>, as follows: 10711*088332b5SXin Li 10712*088332b5SXin Li<ul> 10713*088332b5SXin Li<li><b>"<code>set</code>": </b> base is position 0 (beginning of the file);</li> 10714*088332b5SXin Li<li><b>"<code>cur</code>": </b> base is current position;</li> 10715*088332b5SXin Li<li><b>"<code>end</code>": </b> base is end of file;</li> 10716*088332b5SXin Li</ul><p> 10717*088332b5SXin LiIn case of success, <code>seek</code> returns the final file position, 10718*088332b5SXin Limeasured in bytes from the beginning of the file. 10719*088332b5SXin LiIf <code>seek</code> fails, it returns <b>fail</b>, 10720*088332b5SXin Liplus a string describing the error. 10721*088332b5SXin Li 10722*088332b5SXin Li 10723*088332b5SXin Li<p> 10724*088332b5SXin LiThe default value for <code>whence</code> is <code>"cur"</code>, 10725*088332b5SXin Liand for <code>offset</code> is 0. 10726*088332b5SXin LiTherefore, the call <code>file:seek()</code> returns the current 10727*088332b5SXin Lifile position, without changing it; 10728*088332b5SXin Lithe call <code>file:seek("set")</code> sets the position to the 10729*088332b5SXin Libeginning of the file (and returns 0); 10730*088332b5SXin Liand the call <code>file:seek("end")</code> sets the position to the 10731*088332b5SXin Liend of the file, and returns its size. 10732*088332b5SXin Li 10733*088332b5SXin Li 10734*088332b5SXin Li 10735*088332b5SXin Li 10736*088332b5SXin Li<p> 10737*088332b5SXin Li<hr><h3><a name="pdf-file:setvbuf"><code>file:setvbuf (mode [, size])</code></a></h3> 10738*088332b5SXin Li 10739*088332b5SXin Li 10740*088332b5SXin Li<p> 10741*088332b5SXin LiSets the buffering mode for a file. 10742*088332b5SXin LiThere are three available modes: 10743*088332b5SXin Li 10744*088332b5SXin Li<ul> 10745*088332b5SXin Li<li><b>"<code>no</code>": </b> no buffering.</li> 10746*088332b5SXin Li<li><b>"<code>full</code>": </b> full buffering.</li> 10747*088332b5SXin Li<li><b>"<code>line</code>": </b> line buffering.</li> 10748*088332b5SXin Li</ul> 10749*088332b5SXin Li 10750*088332b5SXin Li<p> 10751*088332b5SXin LiFor the last two cases, 10752*088332b5SXin Li<code>size</code> is a hint for the size of the buffer, in bytes. 10753*088332b5SXin LiThe default is an appropriate size. 10754*088332b5SXin Li 10755*088332b5SXin Li 10756*088332b5SXin Li<p> 10757*088332b5SXin LiThe specific behavior of each mode is non portable; 10758*088332b5SXin Licheck the underlying ISO C function <code>setvbuf</code> in your platform for 10759*088332b5SXin Limore details. 10760*088332b5SXin Li 10761*088332b5SXin Li 10762*088332b5SXin Li 10763*088332b5SXin Li 10764*088332b5SXin Li<p> 10765*088332b5SXin Li<hr><h3><a name="pdf-file:write"><code>file:write (···)</code></a></h3> 10766*088332b5SXin Li 10767*088332b5SXin Li 10768*088332b5SXin Li<p> 10769*088332b5SXin LiWrites the value of each of its arguments to <code>file</code>. 10770*088332b5SXin LiThe arguments must be strings or numbers. 10771*088332b5SXin Li 10772*088332b5SXin Li 10773*088332b5SXin Li<p> 10774*088332b5SXin LiIn case of success, this function returns <code>file</code>. 10775*088332b5SXin Li 10776*088332b5SXin Li 10777*088332b5SXin Li 10778*088332b5SXin Li 10779*088332b5SXin Li 10780*088332b5SXin Li 10781*088332b5SXin Li 10782*088332b5SXin Li<h2>6.9 – <a name="6.9">Operating System Facilities</a></h2> 10783*088332b5SXin Li 10784*088332b5SXin Li<p> 10785*088332b5SXin LiThis library is implemented through table <a name="pdf-os"><code>os</code></a>. 10786*088332b5SXin Li 10787*088332b5SXin Li 10788*088332b5SXin Li<p> 10789*088332b5SXin Li<hr><h3><a name="pdf-os.clock"><code>os.clock ()</code></a></h3> 10790*088332b5SXin Li 10791*088332b5SXin Li 10792*088332b5SXin Li<p> 10793*088332b5SXin LiReturns an approximation of the amount in seconds of CPU time 10794*088332b5SXin Liused by the program, 10795*088332b5SXin Lias returned by the underlying ISO C function <code>clock</code>. 10796*088332b5SXin Li 10797*088332b5SXin Li 10798*088332b5SXin Li 10799*088332b5SXin Li 10800*088332b5SXin Li<p> 10801*088332b5SXin Li<hr><h3><a name="pdf-os.date"><code>os.date ([format [, time]])</code></a></h3> 10802*088332b5SXin Li 10803*088332b5SXin Li 10804*088332b5SXin Li<p> 10805*088332b5SXin LiReturns a string or a table containing date and time, 10806*088332b5SXin Liformatted according to the given string <code>format</code>. 10807*088332b5SXin Li 10808*088332b5SXin Li 10809*088332b5SXin Li<p> 10810*088332b5SXin LiIf the <code>time</code> argument is present, 10811*088332b5SXin Lithis is the time to be formatted 10812*088332b5SXin Li(see the <a href="#pdf-os.time"><code>os.time</code></a> function for a description of this value). 10813*088332b5SXin LiOtherwise, <code>date</code> formats the current time. 10814*088332b5SXin Li 10815*088332b5SXin Li 10816*088332b5SXin Li<p> 10817*088332b5SXin LiIf <code>format</code> starts with '<code>!</code>', 10818*088332b5SXin Lithen the date is formatted in Coordinated Universal Time. 10819*088332b5SXin LiAfter this optional character, 10820*088332b5SXin Liif <code>format</code> is the string "<code>*t</code>", 10821*088332b5SXin Lithen <code>date</code> returns a table with the following fields: 10822*088332b5SXin Li<code>year</code>, <code>month</code> (1–12), <code>day</code> (1–31), 10823*088332b5SXin Li<code>hour</code> (0–23), <code>min</code> (0–59), 10824*088332b5SXin Li<code>sec</code> (0–61, due to leap seconds), 10825*088332b5SXin Li<code>wday</code> (weekday, 1–7, Sunday is 1), 10826*088332b5SXin Li<code>yday</code> (day of the year, 1–366), 10827*088332b5SXin Liand <code>isdst</code> (daylight saving flag, a boolean). 10828*088332b5SXin LiThis last field may be absent 10829*088332b5SXin Liif the information is not available. 10830*088332b5SXin Li 10831*088332b5SXin Li 10832*088332b5SXin Li<p> 10833*088332b5SXin LiIf <code>format</code> is not "<code>*t</code>", 10834*088332b5SXin Lithen <code>date</code> returns the date as a string, 10835*088332b5SXin Liformatted according to the same rules as the ISO C function <code>strftime</code>. 10836*088332b5SXin Li 10837*088332b5SXin Li 10838*088332b5SXin Li<p> 10839*088332b5SXin LiIf <code>format</code> is absent, it defaults to "<code>%c</code>", 10840*088332b5SXin Liwhich gives a human-readable date and time representation 10841*088332b5SXin Liusing the current locale. 10842*088332b5SXin Li 10843*088332b5SXin Li 10844*088332b5SXin Li<p> 10845*088332b5SXin LiOn non-POSIX systems, 10846*088332b5SXin Lithis function may be not thread safe 10847*088332b5SXin Libecause of its reliance on C function <code>gmtime</code> and C function <code>localtime</code>. 10848*088332b5SXin Li 10849*088332b5SXin Li 10850*088332b5SXin Li 10851*088332b5SXin Li 10852*088332b5SXin Li<p> 10853*088332b5SXin Li<hr><h3><a name="pdf-os.difftime"><code>os.difftime (t2, t1)</code></a></h3> 10854*088332b5SXin Li 10855*088332b5SXin Li 10856*088332b5SXin Li<p> 10857*088332b5SXin LiReturns the difference, in seconds, 10858*088332b5SXin Lifrom time <code>t1</code> to time <code>t2</code> 10859*088332b5SXin Li(where the times are values returned by <a href="#pdf-os.time"><code>os.time</code></a>). 10860*088332b5SXin LiIn POSIX, Windows, and some other systems, 10861*088332b5SXin Lithis value is exactly <code>t2</code><em>-</em><code>t1</code>. 10862*088332b5SXin Li 10863*088332b5SXin Li 10864*088332b5SXin Li 10865*088332b5SXin Li 10866*088332b5SXin Li<p> 10867*088332b5SXin Li<hr><h3><a name="pdf-os.execute"><code>os.execute ([command])</code></a></h3> 10868*088332b5SXin Li 10869*088332b5SXin Li 10870*088332b5SXin Li<p> 10871*088332b5SXin LiThis function is equivalent to the ISO C function <code>system</code>. 10872*088332b5SXin LiIt passes <code>command</code> to be executed by an operating system shell. 10873*088332b5SXin LiIts first result is <b>true</b> 10874*088332b5SXin Liif the command terminated successfully, 10875*088332b5SXin Lior <b>fail</b> otherwise. 10876*088332b5SXin LiAfter this first result 10877*088332b5SXin Lithe function returns a string plus a number, 10878*088332b5SXin Lias follows: 10879*088332b5SXin Li 10880*088332b5SXin Li<ul> 10881*088332b5SXin Li 10882*088332b5SXin Li<li><b>"<code>exit</code>": </b> 10883*088332b5SXin Lithe command terminated normally; 10884*088332b5SXin Lithe following number is the exit status of the command. 10885*088332b5SXin Li</li> 10886*088332b5SXin Li 10887*088332b5SXin Li<li><b>"<code>signal</code>": </b> 10888*088332b5SXin Lithe command was terminated by a signal; 10889*088332b5SXin Lithe following number is the signal that terminated the command. 10890*088332b5SXin Li</li> 10891*088332b5SXin Li 10892*088332b5SXin Li</ul> 10893*088332b5SXin Li 10894*088332b5SXin Li<p> 10895*088332b5SXin LiWhen called without a <code>command</code>, 10896*088332b5SXin Li<code>os.execute</code> returns a boolean that is true if a shell is available. 10897*088332b5SXin Li 10898*088332b5SXin Li 10899*088332b5SXin Li 10900*088332b5SXin Li 10901*088332b5SXin Li<p> 10902*088332b5SXin Li<hr><h3><a name="pdf-os.exit"><code>os.exit ([code [, close]])</code></a></h3> 10903*088332b5SXin Li 10904*088332b5SXin Li 10905*088332b5SXin Li<p> 10906*088332b5SXin LiCalls the ISO C function <code>exit</code> to terminate the host program. 10907*088332b5SXin LiIf <code>code</code> is <b>true</b>, 10908*088332b5SXin Lithe returned status is <code>EXIT_SUCCESS</code>; 10909*088332b5SXin Liif <code>code</code> is <b>false</b>, 10910*088332b5SXin Lithe returned status is <code>EXIT_FAILURE</code>; 10911*088332b5SXin Liif <code>code</code> is a number, 10912*088332b5SXin Lithe returned status is this number. 10913*088332b5SXin LiThe default value for <code>code</code> is <b>true</b>. 10914*088332b5SXin Li 10915*088332b5SXin Li 10916*088332b5SXin Li<p> 10917*088332b5SXin LiIf the optional second argument <code>close</code> is true, 10918*088332b5SXin Licloses the Lua state before exiting. 10919*088332b5SXin Li 10920*088332b5SXin Li 10921*088332b5SXin Li 10922*088332b5SXin Li 10923*088332b5SXin Li<p> 10924*088332b5SXin Li<hr><h3><a name="pdf-os.getenv"><code>os.getenv (varname)</code></a></h3> 10925*088332b5SXin Li 10926*088332b5SXin Li 10927*088332b5SXin Li<p> 10928*088332b5SXin LiReturns the value of the process environment variable <code>varname</code> 10929*088332b5SXin Lior <b>fail</b> if the variable is not defined. 10930*088332b5SXin Li 10931*088332b5SXin Li 10932*088332b5SXin Li 10933*088332b5SXin Li 10934*088332b5SXin Li<p> 10935*088332b5SXin Li<hr><h3><a name="pdf-os.remove"><code>os.remove (filename)</code></a></h3> 10936*088332b5SXin Li 10937*088332b5SXin Li 10938*088332b5SXin Li<p> 10939*088332b5SXin LiDeletes the file (or empty directory, on POSIX systems) 10940*088332b5SXin Liwith the given name. 10941*088332b5SXin LiIf this function fails, it returns <b>fail</b> 10942*088332b5SXin Liplus a string describing the error and the error code. 10943*088332b5SXin LiOtherwise, it returns true. 10944*088332b5SXin Li 10945*088332b5SXin Li 10946*088332b5SXin Li 10947*088332b5SXin Li 10948*088332b5SXin Li<p> 10949*088332b5SXin Li<hr><h3><a name="pdf-os.rename"><code>os.rename (oldname, newname)</code></a></h3> 10950*088332b5SXin Li 10951*088332b5SXin Li 10952*088332b5SXin Li<p> 10953*088332b5SXin LiRenames the file or directory named <code>oldname</code> to <code>newname</code>. 10954*088332b5SXin LiIf this function fails, it returns <b>fail</b>, 10955*088332b5SXin Liplus a string describing the error and the error code. 10956*088332b5SXin LiOtherwise, it returns true. 10957*088332b5SXin Li 10958*088332b5SXin Li 10959*088332b5SXin Li 10960*088332b5SXin Li 10961*088332b5SXin Li<p> 10962*088332b5SXin Li<hr><h3><a name="pdf-os.setlocale"><code>os.setlocale (locale [, category])</code></a></h3> 10963*088332b5SXin Li 10964*088332b5SXin Li 10965*088332b5SXin Li<p> 10966*088332b5SXin LiSets the current locale of the program. 10967*088332b5SXin Li<code>locale</code> is a system-dependent string specifying a locale; 10968*088332b5SXin Li<code>category</code> is an optional string describing which category to change: 10969*088332b5SXin Li<code>"all"</code>, <code>"collate"</code>, <code>"ctype"</code>, 10970*088332b5SXin Li<code>"monetary"</code>, <code>"numeric"</code>, or <code>"time"</code>; 10971*088332b5SXin Lithe default category is <code>"all"</code>. 10972*088332b5SXin LiThe function returns the name of the new locale, 10973*088332b5SXin Lior <b>fail</b> if the request cannot be honored. 10974*088332b5SXin Li 10975*088332b5SXin Li 10976*088332b5SXin Li<p> 10977*088332b5SXin LiIf <code>locale</code> is the empty string, 10978*088332b5SXin Lithe current locale is set to an implementation-defined native locale. 10979*088332b5SXin LiIf <code>locale</code> is the string "<code>C</code>", 10980*088332b5SXin Lithe current locale is set to the standard C locale. 10981*088332b5SXin Li 10982*088332b5SXin Li 10983*088332b5SXin Li<p> 10984*088332b5SXin LiWhen called with <b>nil</b> as the first argument, 10985*088332b5SXin Lithis function only returns the name of the current locale 10986*088332b5SXin Lifor the given category. 10987*088332b5SXin Li 10988*088332b5SXin Li 10989*088332b5SXin Li<p> 10990*088332b5SXin LiThis function may be not thread safe 10991*088332b5SXin Libecause of its reliance on C function <code>setlocale</code>. 10992*088332b5SXin Li 10993*088332b5SXin Li 10994*088332b5SXin Li 10995*088332b5SXin Li 10996*088332b5SXin Li<p> 10997*088332b5SXin Li<hr><h3><a name="pdf-os.time"><code>os.time ([table])</code></a></h3> 10998*088332b5SXin Li 10999*088332b5SXin Li 11000*088332b5SXin Li<p> 11001*088332b5SXin LiReturns the current time when called without arguments, 11002*088332b5SXin Lior a time representing the local date and time specified by the given table. 11003*088332b5SXin LiThis table must have fields <code>year</code>, <code>month</code>, and <code>day</code>, 11004*088332b5SXin Liand may have fields 11005*088332b5SXin Li<code>hour</code> (default is 12), 11006*088332b5SXin Li<code>min</code> (default is 0), 11007*088332b5SXin Li<code>sec</code> (default is 0), 11008*088332b5SXin Liand <code>isdst</code> (default is <b>nil</b>). 11009*088332b5SXin LiOther fields are ignored. 11010*088332b5SXin LiFor a description of these fields, see the <a href="#pdf-os.date"><code>os.date</code></a> function. 11011*088332b5SXin Li 11012*088332b5SXin Li 11013*088332b5SXin Li<p> 11014*088332b5SXin LiWhen the function is called, 11015*088332b5SXin Lithe values in these fields do not need to be inside their valid ranges. 11016*088332b5SXin LiFor instance, if <code>sec</code> is -10, 11017*088332b5SXin Liit means 10 seconds before the time specified by the other fields; 11018*088332b5SXin Liif <code>hour</code> is 1000, 11019*088332b5SXin Liit means 1000 hours after the time specified by the other fields. 11020*088332b5SXin Li 11021*088332b5SXin Li 11022*088332b5SXin Li<p> 11023*088332b5SXin LiThe returned value is a number, whose meaning depends on your system. 11024*088332b5SXin LiIn POSIX, Windows, and some other systems, 11025*088332b5SXin Lithis number counts the number 11026*088332b5SXin Liof seconds since some given start time (the "epoch"). 11027*088332b5SXin LiIn other systems, the meaning is not specified, 11028*088332b5SXin Liand the number returned by <code>time</code> can be used only as an argument to 11029*088332b5SXin Li<a href="#pdf-os.date"><code>os.date</code></a> and <a href="#pdf-os.difftime"><code>os.difftime</code></a>. 11030*088332b5SXin Li 11031*088332b5SXin Li 11032*088332b5SXin Li<p> 11033*088332b5SXin LiWhen called with a table, 11034*088332b5SXin Li<code>os.time</code> also normalizes all the fields 11035*088332b5SXin Lidocumented in the <a href="#pdf-os.date"><code>os.date</code></a> function, 11036*088332b5SXin Liso that they represent the same time as before the call 11037*088332b5SXin Libut with values inside their valid ranges. 11038*088332b5SXin Li 11039*088332b5SXin Li 11040*088332b5SXin Li 11041*088332b5SXin Li 11042*088332b5SXin Li<p> 11043*088332b5SXin Li<hr><h3><a name="pdf-os.tmpname"><code>os.tmpname ()</code></a></h3> 11044*088332b5SXin Li 11045*088332b5SXin Li 11046*088332b5SXin Li<p> 11047*088332b5SXin LiReturns a string with a file name that can 11048*088332b5SXin Libe used for a temporary file. 11049*088332b5SXin LiThe file must be explicitly opened before its use 11050*088332b5SXin Liand explicitly removed when no longer needed. 11051*088332b5SXin Li 11052*088332b5SXin Li 11053*088332b5SXin Li<p> 11054*088332b5SXin LiIn POSIX systems, 11055*088332b5SXin Lithis function also creates a file with that name, 11056*088332b5SXin Lito avoid security risks. 11057*088332b5SXin Li(Someone else might create the file with wrong permissions 11058*088332b5SXin Liin the time between getting the name and creating the file.) 11059*088332b5SXin LiYou still have to open the file to use it 11060*088332b5SXin Liand to remove it (even if you do not use it). 11061*088332b5SXin Li 11062*088332b5SXin Li 11063*088332b5SXin Li<p> 11064*088332b5SXin LiWhen possible, 11065*088332b5SXin Liyou may prefer to use <a href="#pdf-io.tmpfile"><code>io.tmpfile</code></a>, 11066*088332b5SXin Liwhich automatically removes the file when the program ends. 11067*088332b5SXin Li 11068*088332b5SXin Li 11069*088332b5SXin Li 11070*088332b5SXin Li 11071*088332b5SXin Li 11072*088332b5SXin Li 11073*088332b5SXin Li 11074*088332b5SXin Li<h2>6.10 – <a name="6.10">The Debug Library</a></h2> 11075*088332b5SXin Li 11076*088332b5SXin Li<p> 11077*088332b5SXin LiThis library provides 11078*088332b5SXin Lithe functionality of the debug interface (<a href="#4.7">§4.7</a>) to Lua programs. 11079*088332b5SXin LiYou should exert care when using this library. 11080*088332b5SXin LiSeveral of its functions 11081*088332b5SXin Liviolate basic assumptions about Lua code 11082*088332b5SXin Li(e.g., that variables local to a function 11083*088332b5SXin Licannot be accessed from outside; 11084*088332b5SXin Lithat userdata metatables cannot be changed by Lua code; 11085*088332b5SXin Lithat Lua programs do not crash) 11086*088332b5SXin Liand therefore can compromise otherwise secure code. 11087*088332b5SXin LiMoreover, some functions in this library may be slow. 11088*088332b5SXin Li 11089*088332b5SXin Li 11090*088332b5SXin Li<p> 11091*088332b5SXin LiAll functions in this library are provided 11092*088332b5SXin Liinside the <a name="pdf-debug"><code>debug</code></a> table. 11093*088332b5SXin LiAll functions that operate over a thread 11094*088332b5SXin Lihave an optional first argument which is the 11095*088332b5SXin Lithread to operate over. 11096*088332b5SXin LiThe default is always the current thread. 11097*088332b5SXin Li 11098*088332b5SXin Li 11099*088332b5SXin Li<p> 11100*088332b5SXin Li<hr><h3><a name="pdf-debug.debug"><code>debug.debug ()</code></a></h3> 11101*088332b5SXin Li 11102*088332b5SXin Li 11103*088332b5SXin Li<p> 11104*088332b5SXin LiEnters an interactive mode with the user, 11105*088332b5SXin Lirunning each string that the user enters. 11106*088332b5SXin LiUsing simple commands and other debug facilities, 11107*088332b5SXin Lithe user can inspect global and local variables, 11108*088332b5SXin Lichange their values, evaluate expressions, and so on. 11109*088332b5SXin LiA line containing only the word <code>cont</code> finishes this function, 11110*088332b5SXin Liso that the caller continues its execution. 11111*088332b5SXin Li 11112*088332b5SXin Li 11113*088332b5SXin Li<p> 11114*088332b5SXin LiNote that commands for <code>debug.debug</code> are not lexically nested 11115*088332b5SXin Liwithin any function and so have no direct access to local variables. 11116*088332b5SXin Li 11117*088332b5SXin Li 11118*088332b5SXin Li 11119*088332b5SXin Li 11120*088332b5SXin Li<p> 11121*088332b5SXin Li<hr><h3><a name="pdf-debug.gethook"><code>debug.gethook ([thread])</code></a></h3> 11122*088332b5SXin Li 11123*088332b5SXin Li 11124*088332b5SXin Li<p> 11125*088332b5SXin LiReturns the current hook settings of the thread, as three values: 11126*088332b5SXin Lithe current hook function, the current hook mask, 11127*088332b5SXin Liand the current hook count, 11128*088332b5SXin Lias set by the <a href="#pdf-debug.sethook"><code>debug.sethook</code></a> function. 11129*088332b5SXin Li 11130*088332b5SXin Li 11131*088332b5SXin Li<p> 11132*088332b5SXin LiReturns <b>fail</b> if there is no active hook. 11133*088332b5SXin Li 11134*088332b5SXin Li 11135*088332b5SXin Li 11136*088332b5SXin Li 11137*088332b5SXin Li<p> 11138*088332b5SXin Li<hr><h3><a name="pdf-debug.getinfo"><code>debug.getinfo ([thread,] f [, what])</code></a></h3> 11139*088332b5SXin Li 11140*088332b5SXin Li 11141*088332b5SXin Li<p> 11142*088332b5SXin LiReturns a table with information about a function. 11143*088332b5SXin LiYou can give the function directly 11144*088332b5SXin Lior you can give a number as the value of <code>f</code>, 11145*088332b5SXin Liwhich means the function running at level <code>f</code> of the call stack 11146*088332b5SXin Liof the given thread: 11147*088332b5SXin Lilevel 0 is the current function (<code>getinfo</code> itself); 11148*088332b5SXin Lilevel 1 is the function that called <code>getinfo</code> 11149*088332b5SXin Li(except for tail calls, which do not count in the stack); 11150*088332b5SXin Liand so on. 11151*088332b5SXin LiIf <code>f</code> is a number greater than the number of active functions, 11152*088332b5SXin Lithen <code>getinfo</code> returns <b>fail</b>. 11153*088332b5SXin Li 11154*088332b5SXin Li 11155*088332b5SXin Li<p> 11156*088332b5SXin LiThe returned table can contain all the fields returned by <a href="#lua_getinfo"><code>lua_getinfo</code></a>, 11157*088332b5SXin Liwith the string <code>what</code> describing which fields to fill in. 11158*088332b5SXin LiThe default for <code>what</code> is to get all information available, 11159*088332b5SXin Liexcept the table of valid lines. 11160*088332b5SXin LiIf present, 11161*088332b5SXin Lithe option '<code>f</code>' 11162*088332b5SXin Liadds a field named <code>func</code> with the function itself. 11163*088332b5SXin LiIf present, 11164*088332b5SXin Lithe option '<code>L</code>' 11165*088332b5SXin Liadds a field named <code>activelines</code> with the table of 11166*088332b5SXin Livalid lines. 11167*088332b5SXin Li 11168*088332b5SXin Li 11169*088332b5SXin Li<p> 11170*088332b5SXin LiFor instance, the expression <code>debug.getinfo(1,"n").name</code> returns 11171*088332b5SXin Lia name for the current function, 11172*088332b5SXin Liif a reasonable name can be found, 11173*088332b5SXin Liand the expression <code>debug.getinfo(print)</code> 11174*088332b5SXin Lireturns a table with all available information 11175*088332b5SXin Liabout the <a href="#pdf-print"><code>print</code></a> function. 11176*088332b5SXin Li 11177*088332b5SXin Li 11178*088332b5SXin Li 11179*088332b5SXin Li 11180*088332b5SXin Li<p> 11181*088332b5SXin Li<hr><h3><a name="pdf-debug.getlocal"><code>debug.getlocal ([thread,] f, local)</code></a></h3> 11182*088332b5SXin Li 11183*088332b5SXin Li 11184*088332b5SXin Li<p> 11185*088332b5SXin LiThis function returns the name and the value of the local variable 11186*088332b5SXin Liwith index <code>local</code> of the function at level <code>f</code> of the stack. 11187*088332b5SXin LiThis function accesses not only explicit local variables, 11188*088332b5SXin Libut also parameters and temporary values. 11189*088332b5SXin Li 11190*088332b5SXin Li 11191*088332b5SXin Li<p> 11192*088332b5SXin LiThe first parameter or local variable has index 1, and so on, 11193*088332b5SXin Lifollowing the order that they are declared in the code, 11194*088332b5SXin Licounting only the variables that are active 11195*088332b5SXin Liin the current scope of the function. 11196*088332b5SXin LiCompile-time constants may not appear in this listing, 11197*088332b5SXin Liif they were optimized away by the compiler. 11198*088332b5SXin LiNegative indices refer to vararg arguments; 11199*088332b5SXin Li-1 is the first vararg argument. 11200*088332b5SXin LiThe function returns <b>fail</b> 11201*088332b5SXin Liif there is no variable with the given index, 11202*088332b5SXin Liand raises an error when called with a level out of range. 11203*088332b5SXin Li(You can call <a href="#pdf-debug.getinfo"><code>debug.getinfo</code></a> to check whether the level is valid.) 11204*088332b5SXin Li 11205*088332b5SXin Li 11206*088332b5SXin Li<p> 11207*088332b5SXin LiVariable names starting with '<code>(</code>' (open parenthesis) 11208*088332b5SXin Lirepresent variables with no known names 11209*088332b5SXin Li(internal variables such as loop control variables, 11210*088332b5SXin Liand variables from chunks saved without debug information). 11211*088332b5SXin Li 11212*088332b5SXin Li 11213*088332b5SXin Li<p> 11214*088332b5SXin LiThe parameter <code>f</code> may also be a function. 11215*088332b5SXin LiIn that case, <code>getlocal</code> returns only the name of function parameters. 11216*088332b5SXin Li 11217*088332b5SXin Li 11218*088332b5SXin Li 11219*088332b5SXin Li 11220*088332b5SXin Li<p> 11221*088332b5SXin Li<hr><h3><a name="pdf-debug.getmetatable"><code>debug.getmetatable (value)</code></a></h3> 11222*088332b5SXin Li 11223*088332b5SXin Li 11224*088332b5SXin Li<p> 11225*088332b5SXin LiReturns the metatable of the given <code>value</code> 11226*088332b5SXin Lior <b>nil</b> if it does not have a metatable. 11227*088332b5SXin Li 11228*088332b5SXin Li 11229*088332b5SXin Li 11230*088332b5SXin Li 11231*088332b5SXin Li<p> 11232*088332b5SXin Li<hr><h3><a name="pdf-debug.getregistry"><code>debug.getregistry ()</code></a></h3> 11233*088332b5SXin Li 11234*088332b5SXin Li 11235*088332b5SXin Li<p> 11236*088332b5SXin LiReturns the registry table (see <a href="#4.3">§4.3</a>). 11237*088332b5SXin Li 11238*088332b5SXin Li 11239*088332b5SXin Li 11240*088332b5SXin Li 11241*088332b5SXin Li<p> 11242*088332b5SXin Li<hr><h3><a name="pdf-debug.getupvalue"><code>debug.getupvalue (f, up)</code></a></h3> 11243*088332b5SXin Li 11244*088332b5SXin Li 11245*088332b5SXin Li<p> 11246*088332b5SXin LiThis function returns the name and the value of the upvalue 11247*088332b5SXin Liwith index <code>up</code> of the function <code>f</code>. 11248*088332b5SXin LiThe function returns <b>fail</b> 11249*088332b5SXin Liif there is no upvalue with the given index. 11250*088332b5SXin Li 11251*088332b5SXin Li 11252*088332b5SXin Li<p> 11253*088332b5SXin Li(For Lua functions, 11254*088332b5SXin Liupvalues are the external local variables that the function uses, 11255*088332b5SXin Liand that are consequently included in its closure.) 11256*088332b5SXin Li 11257*088332b5SXin Li 11258*088332b5SXin Li<p> 11259*088332b5SXin LiFor C functions, this function uses the empty string <code>""</code> 11260*088332b5SXin Lias a name for all upvalues. 11261*088332b5SXin Li 11262*088332b5SXin Li 11263*088332b5SXin Li<p> 11264*088332b5SXin LiVariable name '<code>?</code>' (interrogation mark) 11265*088332b5SXin Lirepresents variables with no known names 11266*088332b5SXin Li(variables from chunks saved without debug information). 11267*088332b5SXin Li 11268*088332b5SXin Li 11269*088332b5SXin Li 11270*088332b5SXin Li 11271*088332b5SXin Li<p> 11272*088332b5SXin Li<hr><h3><a name="pdf-debug.getuservalue"><code>debug.getuservalue (u, n)</code></a></h3> 11273*088332b5SXin Li 11274*088332b5SXin Li 11275*088332b5SXin Li<p> 11276*088332b5SXin LiReturns the <code>n</code>-th user value associated 11277*088332b5SXin Lito the userdata <code>u</code> plus a boolean, 11278*088332b5SXin Li<b>false</b> if the userdata does not have that value. 11279*088332b5SXin Li 11280*088332b5SXin Li 11281*088332b5SXin Li 11282*088332b5SXin Li 11283*088332b5SXin Li<p> 11284*088332b5SXin Li<hr><h3><a name="pdf-debug.setcstacklimit"><code>debug.setcstacklimit (limit)</code></a></h3> 11285*088332b5SXin Li 11286*088332b5SXin Li 11287*088332b5SXin Li<p> 11288*088332b5SXin LiSets a new limit for the C stack. 11289*088332b5SXin LiThis limit controls how deeply nested calls can go in Lua, 11290*088332b5SXin Liwith the intent of avoiding a stack overflow. 11291*088332b5SXin LiA limit too small restricts recursive calls pointlessly; 11292*088332b5SXin Lia limit too large exposes the interpreter to stack-overflow crashes. 11293*088332b5SXin LiUnfortunately, there is no way to know a priori 11294*088332b5SXin Lithe maximum safe limit for a platform. 11295*088332b5SXin Li 11296*088332b5SXin Li 11297*088332b5SXin Li<p> 11298*088332b5SXin LiEach call made from Lua code counts one unit. 11299*088332b5SXin LiOther operations (e.g., calls made from C to Lua or resuming a coroutine) 11300*088332b5SXin Limay have a higher cost. 11301*088332b5SXin Li 11302*088332b5SXin Li 11303*088332b5SXin Li<p> 11304*088332b5SXin LiThis function has the following restrictions: 11305*088332b5SXin Li 11306*088332b5SXin Li<ul> 11307*088332b5SXin Li<li>It can only be called from the main coroutine (thread);</li> 11308*088332b5SXin Li<li>It cannot be called while handling a stack-overflow error;</li> 11309*088332b5SXin Li<li><code>limit</code> must be less than 40000;</li> 11310*088332b5SXin Li<li><code>limit</code> cannot be less than the amount of C stack in use.</li> 11311*088332b5SXin Li</ul><p> 11312*088332b5SXin LiIf a call does not respect some restriction, 11313*088332b5SXin Liit returns a false value. 11314*088332b5SXin LiOtherwise, 11315*088332b5SXin Lithe call returns the old limit. 11316*088332b5SXin Li 11317*088332b5SXin Li 11318*088332b5SXin Li 11319*088332b5SXin Li 11320*088332b5SXin Li<p> 11321*088332b5SXin Li<hr><h3><a name="pdf-debug.sethook"><code>debug.sethook ([thread,] hook, mask [, count])</code></a></h3> 11322*088332b5SXin Li 11323*088332b5SXin Li 11324*088332b5SXin Li<p> 11325*088332b5SXin LiSets the given function as the debug hook. 11326*088332b5SXin LiThe string <code>mask</code> and the number <code>count</code> describe 11327*088332b5SXin Liwhen the hook will be called. 11328*088332b5SXin LiThe string mask may have any combination of the following characters, 11329*088332b5SXin Liwith the given meaning: 11330*088332b5SXin Li 11331*088332b5SXin Li<ul> 11332*088332b5SXin Li<li><b>'<code>c</code>': </b> the hook is called every time Lua calls a function;</li> 11333*088332b5SXin Li<li><b>'<code>r</code>': </b> the hook is called every time Lua returns from a function;</li> 11334*088332b5SXin Li<li><b>'<code>l</code>': </b> the hook is called every time Lua enters a new line of code.</li> 11335*088332b5SXin Li</ul><p> 11336*088332b5SXin LiMoreover, 11337*088332b5SXin Liwith a <code>count</code> different from zero, 11338*088332b5SXin Lithe hook is called also after every <code>count</code> instructions. 11339*088332b5SXin Li 11340*088332b5SXin Li 11341*088332b5SXin Li<p> 11342*088332b5SXin LiWhen called without arguments, 11343*088332b5SXin Li<a href="#pdf-debug.sethook"><code>debug.sethook</code></a> turns off the hook. 11344*088332b5SXin Li 11345*088332b5SXin Li 11346*088332b5SXin Li<p> 11347*088332b5SXin LiWhen the hook is called, its first parameter is a string 11348*088332b5SXin Lidescribing the event that has triggered its call: 11349*088332b5SXin Li<code>"call"</code>, <code>"tail call"</code>, <code>"return"</code>, 11350*088332b5SXin Li<code>"line"</code>, and <code>"count"</code>. 11351*088332b5SXin LiFor line events, 11352*088332b5SXin Lithe hook also gets the new line number as its second parameter. 11353*088332b5SXin LiInside a hook, 11354*088332b5SXin Liyou can call <code>getinfo</code> with level 2 to get more information about 11355*088332b5SXin Lithe running function. 11356*088332b5SXin Li(Level 0 is the <code>getinfo</code> function, 11357*088332b5SXin Liand level 1 is the hook function.) 11358*088332b5SXin Li 11359*088332b5SXin Li 11360*088332b5SXin Li 11361*088332b5SXin Li 11362*088332b5SXin Li<p> 11363*088332b5SXin Li<hr><h3><a name="pdf-debug.setlocal"><code>debug.setlocal ([thread,] level, local, value)</code></a></h3> 11364*088332b5SXin Li 11365*088332b5SXin Li 11366*088332b5SXin Li<p> 11367*088332b5SXin LiThis function assigns the value <code>value</code> to the local variable 11368*088332b5SXin Liwith index <code>local</code> of the function at level <code>level</code> of the stack. 11369*088332b5SXin LiThe function returns <b>fail</b> if there is no local 11370*088332b5SXin Livariable with the given index, 11371*088332b5SXin Liand raises an error when called with a <code>level</code> out of range. 11372*088332b5SXin Li(You can call <code>getinfo</code> to check whether the level is valid.) 11373*088332b5SXin LiOtherwise, it returns the name of the local variable. 11374*088332b5SXin Li 11375*088332b5SXin Li 11376*088332b5SXin Li<p> 11377*088332b5SXin LiSee <a href="#pdf-debug.getlocal"><code>debug.getlocal</code></a> for more information about 11378*088332b5SXin Livariable indices and names. 11379*088332b5SXin Li 11380*088332b5SXin Li 11381*088332b5SXin Li 11382*088332b5SXin Li 11383*088332b5SXin Li<p> 11384*088332b5SXin Li<hr><h3><a name="pdf-debug.setmetatable"><code>debug.setmetatable (value, table)</code></a></h3> 11385*088332b5SXin Li 11386*088332b5SXin Li 11387*088332b5SXin Li<p> 11388*088332b5SXin LiSets the metatable for the given <code>value</code> to the given <code>table</code> 11389*088332b5SXin Li(which can be <b>nil</b>). 11390*088332b5SXin LiReturns <code>value</code>. 11391*088332b5SXin Li 11392*088332b5SXin Li 11393*088332b5SXin Li 11394*088332b5SXin Li 11395*088332b5SXin Li<p> 11396*088332b5SXin Li<hr><h3><a name="pdf-debug.setupvalue"><code>debug.setupvalue (f, up, value)</code></a></h3> 11397*088332b5SXin Li 11398*088332b5SXin Li 11399*088332b5SXin Li<p> 11400*088332b5SXin LiThis function assigns the value <code>value</code> to the upvalue 11401*088332b5SXin Liwith index <code>up</code> of the function <code>f</code>. 11402*088332b5SXin LiThe function returns <b>fail</b> if there is no upvalue 11403*088332b5SXin Liwith the given index. 11404*088332b5SXin LiOtherwise, it returns the name of the upvalue. 11405*088332b5SXin Li 11406*088332b5SXin Li 11407*088332b5SXin Li<p> 11408*088332b5SXin LiSee <a href="#pdf-debug.getupvalue"><code>debug.getupvalue</code></a> for more information about upvalues. 11409*088332b5SXin Li 11410*088332b5SXin Li 11411*088332b5SXin Li 11412*088332b5SXin Li 11413*088332b5SXin Li<p> 11414*088332b5SXin Li<hr><h3><a name="pdf-debug.setuservalue"><code>debug.setuservalue (udata, value, n)</code></a></h3> 11415*088332b5SXin Li 11416*088332b5SXin Li 11417*088332b5SXin Li<p> 11418*088332b5SXin LiSets the given <code>value</code> as 11419*088332b5SXin Lithe <code>n</code>-th user value associated to the given <code>udata</code>. 11420*088332b5SXin Li<code>udata</code> must be a full userdata. 11421*088332b5SXin Li 11422*088332b5SXin Li 11423*088332b5SXin Li<p> 11424*088332b5SXin LiReturns <code>udata</code>, 11425*088332b5SXin Lior <b>fail</b> if the userdata does not have that value. 11426*088332b5SXin Li 11427*088332b5SXin Li 11428*088332b5SXin Li 11429*088332b5SXin Li 11430*088332b5SXin Li<p> 11431*088332b5SXin Li<hr><h3><a name="pdf-debug.traceback"><code>debug.traceback ([thread,] [message [, level]])</code></a></h3> 11432*088332b5SXin Li 11433*088332b5SXin Li 11434*088332b5SXin Li<p> 11435*088332b5SXin LiIf <code>message</code> is present but is neither a string nor <b>nil</b>, 11436*088332b5SXin Lithis function returns <code>message</code> without further processing. 11437*088332b5SXin LiOtherwise, 11438*088332b5SXin Liit returns a string with a traceback of the call stack. 11439*088332b5SXin LiThe optional <code>message</code> string is appended 11440*088332b5SXin Liat the beginning of the traceback. 11441*088332b5SXin LiAn optional <code>level</code> number tells at which level 11442*088332b5SXin Lito start the traceback 11443*088332b5SXin Li(default is 1, the function calling <code>traceback</code>). 11444*088332b5SXin Li 11445*088332b5SXin Li 11446*088332b5SXin Li 11447*088332b5SXin Li 11448*088332b5SXin Li<p> 11449*088332b5SXin Li<hr><h3><a name="pdf-debug.upvalueid"><code>debug.upvalueid (f, n)</code></a></h3> 11450*088332b5SXin Li 11451*088332b5SXin Li 11452*088332b5SXin Li<p> 11453*088332b5SXin LiReturns a unique identifier (as a light userdata) 11454*088332b5SXin Lifor the upvalue numbered <code>n</code> 11455*088332b5SXin Lifrom the given function. 11456*088332b5SXin Li 11457*088332b5SXin Li 11458*088332b5SXin Li<p> 11459*088332b5SXin LiThese unique identifiers allow a program to check whether different 11460*088332b5SXin Liclosures share upvalues. 11461*088332b5SXin LiLua closures that share an upvalue 11462*088332b5SXin Li(that is, that access a same external local variable) 11463*088332b5SXin Liwill return identical ids for those upvalue indices. 11464*088332b5SXin Li 11465*088332b5SXin Li 11466*088332b5SXin Li 11467*088332b5SXin Li 11468*088332b5SXin Li<p> 11469*088332b5SXin Li<hr><h3><a name="pdf-debug.upvaluejoin"><code>debug.upvaluejoin (f1, n1, f2, n2)</code></a></h3> 11470*088332b5SXin Li 11471*088332b5SXin Li 11472*088332b5SXin Li<p> 11473*088332b5SXin LiMake the <code>n1</code>-th upvalue of the Lua closure <code>f1</code> 11474*088332b5SXin Lirefer to the <code>n2</code>-th upvalue of the Lua closure <code>f2</code>. 11475*088332b5SXin Li 11476*088332b5SXin Li 11477*088332b5SXin Li 11478*088332b5SXin Li 11479*088332b5SXin Li 11480*088332b5SXin Li 11481*088332b5SXin Li 11482*088332b5SXin Li<h1>7 – <a name="7">Lua Standalone</a></h1> 11483*088332b5SXin Li 11484*088332b5SXin Li<p> 11485*088332b5SXin LiAlthough Lua has been designed as an extension language, 11486*088332b5SXin Lito be embedded in a host C program, 11487*088332b5SXin Liit is also frequently used as a standalone language. 11488*088332b5SXin LiAn interpreter for Lua as a standalone language, 11489*088332b5SXin Licalled simply <code>lua</code>, 11490*088332b5SXin Liis provided with the standard distribution. 11491*088332b5SXin LiThe standalone interpreter includes 11492*088332b5SXin Liall standard libraries. 11493*088332b5SXin LiIts usage is: 11494*088332b5SXin Li 11495*088332b5SXin Li<pre> 11496*088332b5SXin Li lua [options] [script [args]] 11497*088332b5SXin Li</pre><p> 11498*088332b5SXin LiThe options are: 11499*088332b5SXin Li 11500*088332b5SXin Li<ul> 11501*088332b5SXin Li<li><b><code>-e <em>stat</em></code>: </b> execute string <em>stat</em>;</li> 11502*088332b5SXin Li<li><b><code>-i</code>: </b> enter interactive mode after running <em>script</em>;</li> 11503*088332b5SXin Li<li><b><code>-l <em>mod</em></code>: </b> "require" <em>mod</em> and assign the 11504*088332b5SXin Li result to global <em>mod</em>;</li> 11505*088332b5SXin Li<li><b><code>-v</code>: </b> print version information;</li> 11506*088332b5SXin Li<li><b><code>-E</code>: </b> ignore environment variables;</li> 11507*088332b5SXin Li<li><b><code>-W</code>: </b> turn warnings on;</li> 11508*088332b5SXin Li<li><b><code>--</code>: </b> stop handling options;</li> 11509*088332b5SXin Li<li><b><code>-</code>: </b> execute <code>stdin</code> as a file and stop handling options.</li> 11510*088332b5SXin Li</ul><p> 11511*088332b5SXin LiAfter handling its options, <code>lua</code> runs the given <em>script</em>. 11512*088332b5SXin LiWhen called without arguments, 11513*088332b5SXin Li<code>lua</code> behaves as <code>lua -v -i</code> 11514*088332b5SXin Liwhen the standard input (<code>stdin</code>) is a terminal, 11515*088332b5SXin Liand as <code>lua -</code> otherwise. 11516*088332b5SXin Li 11517*088332b5SXin Li 11518*088332b5SXin Li<p> 11519*088332b5SXin LiWhen called without the option <code>-E</code>, 11520*088332b5SXin Lithe interpreter checks for an environment variable <a name="pdf-LUA_INIT_5_4"><code>LUA_INIT_5_4</code></a> 11521*088332b5SXin Li(or <a name="pdf-LUA_INIT"><code>LUA_INIT</code></a> if the versioned name is not defined) 11522*088332b5SXin Libefore running any argument. 11523*088332b5SXin LiIf the variable content has the format <code>@<em>filename</em></code>, 11524*088332b5SXin Lithen <code>lua</code> executes the file. 11525*088332b5SXin LiOtherwise, <code>lua</code> executes the string itself. 11526*088332b5SXin Li 11527*088332b5SXin Li 11528*088332b5SXin Li<p> 11529*088332b5SXin LiWhen called with the option <code>-E</code>, 11530*088332b5SXin LiLua does not consult any environment variables. 11531*088332b5SXin LiIn particular, 11532*088332b5SXin Lithe values of <a href="#pdf-package.path"><code>package.path</code></a> and <a href="#pdf-package.cpath"><code>package.cpath</code></a> 11533*088332b5SXin Liare set with the default paths defined in <code>luaconf.h</code>. 11534*088332b5SXin Li 11535*088332b5SXin Li 11536*088332b5SXin Li<p> 11537*088332b5SXin LiThe options <code>-e</code>, <code>-l</code>, and <code>-W</code> are handled in 11538*088332b5SXin Lithe order they appear. 11539*088332b5SXin LiFor instance, an invocation like 11540*088332b5SXin Li 11541*088332b5SXin Li<pre> 11542*088332b5SXin Li $ lua -e 'a=1' -llib1 script.lua 11543*088332b5SXin Li</pre><p> 11544*088332b5SXin Liwill first set <code>a</code> to 1, then require the library <code>lib1</code>, 11545*088332b5SXin Liand finally run the file <code>script.lua</code> with no arguments. 11546*088332b5SXin Li(Here <code>$</code> is the shell prompt. Your prompt may be different.) 11547*088332b5SXin Li 11548*088332b5SXin Li 11549*088332b5SXin Li<p> 11550*088332b5SXin LiBefore running any code, 11551*088332b5SXin Li<code>lua</code> collects all command-line arguments 11552*088332b5SXin Liin a global table called <code>arg</code>. 11553*088332b5SXin LiThe script name goes to index 0, 11554*088332b5SXin Lithe first argument after the script name goes to index 1, 11555*088332b5SXin Liand so on. 11556*088332b5SXin LiAny arguments before the script name 11557*088332b5SXin Li(that is, the interpreter name plus its options) 11558*088332b5SXin Ligo to negative indices. 11559*088332b5SXin LiFor instance, in the call 11560*088332b5SXin Li 11561*088332b5SXin Li<pre> 11562*088332b5SXin Li $ lua -la b.lua t1 t2 11563*088332b5SXin Li</pre><p> 11564*088332b5SXin Lithe table is like this: 11565*088332b5SXin Li 11566*088332b5SXin Li<pre> 11567*088332b5SXin Li arg = { [-2] = "lua", [-1] = "-la", 11568*088332b5SXin Li [0] = "b.lua", 11569*088332b5SXin Li [1] = "t1", [2] = "t2" } 11570*088332b5SXin Li</pre><p> 11571*088332b5SXin LiIf there is no script in the call, 11572*088332b5SXin Lithe interpreter name goes to index 0, 11573*088332b5SXin Lifollowed by the other arguments. 11574*088332b5SXin LiFor instance, the call 11575*088332b5SXin Li 11576*088332b5SXin Li<pre> 11577*088332b5SXin Li $ lua -e "print(arg[1])" 11578*088332b5SXin Li</pre><p> 11579*088332b5SXin Liwill print "<code>-e</code>". 11580*088332b5SXin LiIf there is a script, 11581*088332b5SXin Lithe script is called with arguments 11582*088332b5SXin Li<code>arg[1]</code>, ···, <code>arg[#arg]</code>. 11583*088332b5SXin LiLike all chunks in Lua, 11584*088332b5SXin Lithe script is compiled as a vararg function. 11585*088332b5SXin Li 11586*088332b5SXin Li 11587*088332b5SXin Li<p> 11588*088332b5SXin LiIn interactive mode, 11589*088332b5SXin LiLua repeatedly prompts and waits for a line. 11590*088332b5SXin LiAfter reading a line, 11591*088332b5SXin LiLua first try to interpret the line as an expression. 11592*088332b5SXin LiIf it succeeds, it prints its value. 11593*088332b5SXin LiOtherwise, it interprets the line as a statement. 11594*088332b5SXin LiIf you write an incomplete statement, 11595*088332b5SXin Lithe interpreter waits for its completion 11596*088332b5SXin Liby issuing a different prompt. 11597*088332b5SXin Li 11598*088332b5SXin Li 11599*088332b5SXin Li<p> 11600*088332b5SXin LiIf the global variable <a name="pdf-_PROMPT"><code>_PROMPT</code></a> contains a string, 11601*088332b5SXin Lithen its value is used as the prompt. 11602*088332b5SXin LiSimilarly, if the global variable <a name="pdf-_PROMPT2"><code>_PROMPT2</code></a> contains a string, 11603*088332b5SXin Liits value is used as the secondary prompt 11604*088332b5SXin Li(issued during incomplete statements). 11605*088332b5SXin Li 11606*088332b5SXin Li 11607*088332b5SXin Li<p> 11608*088332b5SXin LiIn case of unprotected errors in the script, 11609*088332b5SXin Lithe interpreter reports the error to the standard error stream. 11610*088332b5SXin LiIf the error object is not a string but 11611*088332b5SXin Lihas a metamethod <code>__tostring</code>, 11612*088332b5SXin Lithe interpreter calls this metamethod to produce the final message. 11613*088332b5SXin LiOtherwise, the interpreter converts the error object to a string 11614*088332b5SXin Liand adds a stack traceback to it. 11615*088332b5SXin LiWhen warnings are on, 11616*088332b5SXin Lithey are simply printed in the standard error output. 11617*088332b5SXin Li 11618*088332b5SXin Li 11619*088332b5SXin Li<p> 11620*088332b5SXin LiWhen finishing normally, 11621*088332b5SXin Lithe interpreter closes its main Lua state 11622*088332b5SXin Li(see <a href="#lua_close"><code>lua_close</code></a>). 11623*088332b5SXin LiThe script can avoid this step by 11624*088332b5SXin Licalling <a href="#pdf-os.exit"><code>os.exit</code></a> to terminate. 11625*088332b5SXin Li 11626*088332b5SXin Li 11627*088332b5SXin Li<p> 11628*088332b5SXin LiTo allow the use of Lua as a 11629*088332b5SXin Liscript interpreter in Unix systems, 11630*088332b5SXin LiLua skips the first line of a file chunk if it starts with <code>#</code>. 11631*088332b5SXin LiTherefore, Lua scripts can be made into executable programs 11632*088332b5SXin Liby using <code>chmod +x</code> and the <code>#!</code> form, 11633*088332b5SXin Lias in 11634*088332b5SXin Li 11635*088332b5SXin Li<pre> 11636*088332b5SXin Li #!/usr/local/bin/lua 11637*088332b5SXin Li</pre><p> 11638*088332b5SXin LiOf course, 11639*088332b5SXin Lithe location of the Lua interpreter may be different in your machine. 11640*088332b5SXin LiIf <code>lua</code> is in your <code>PATH</code>, 11641*088332b5SXin Lithen 11642*088332b5SXin Li 11643*088332b5SXin Li<pre> 11644*088332b5SXin Li #!/usr/bin/env lua 11645*088332b5SXin Li</pre><p> 11646*088332b5SXin Liis a more portable solution. 11647*088332b5SXin Li 11648*088332b5SXin Li 11649*088332b5SXin Li 11650*088332b5SXin Li<h1>8 – <a name="8">Incompatibilities with the Previous Version</a></h1> 11651*088332b5SXin Li 11652*088332b5SXin Li 11653*088332b5SXin Li 11654*088332b5SXin Li<p> 11655*088332b5SXin LiHere we list the incompatibilities that you may find when moving a program 11656*088332b5SXin Lifrom Lua 5.3 to Lua 5.4. 11657*088332b5SXin Li 11658*088332b5SXin Li 11659*088332b5SXin Li<p> 11660*088332b5SXin LiYou can avoid some incompatibilities by compiling Lua with 11661*088332b5SXin Liappropriate options (see file <code>luaconf.h</code>). 11662*088332b5SXin LiHowever, 11663*088332b5SXin Liall these compatibility options will be removed in the future. 11664*088332b5SXin LiMore often than not, 11665*088332b5SXin Licompatibility issues arise when these compatibility options 11666*088332b5SXin Liare removed. 11667*088332b5SXin LiSo, whenever you have the chance, 11668*088332b5SXin Liyou should try to test your code with a version of Lua compiled 11669*088332b5SXin Liwith all compatibility options turned off. 11670*088332b5SXin LiThat will ease transitions to newer versions of Lua. 11671*088332b5SXin Li 11672*088332b5SXin Li 11673*088332b5SXin Li<p> 11674*088332b5SXin LiLua versions can always change the C API in ways that 11675*088332b5SXin Lido not imply source-code changes in a program, 11676*088332b5SXin Lisuch as the numeric values for constants 11677*088332b5SXin Lior the implementation of functions as macros. 11678*088332b5SXin LiTherefore, 11679*088332b5SXin Liyou should never assume that binaries are compatible between 11680*088332b5SXin Lidifferent Lua versions. 11681*088332b5SXin LiAlways recompile clients of the Lua API when 11682*088332b5SXin Liusing a new version. 11683*088332b5SXin Li 11684*088332b5SXin Li 11685*088332b5SXin Li<p> 11686*088332b5SXin LiSimilarly, Lua versions can always change the internal representation 11687*088332b5SXin Liof precompiled chunks; 11688*088332b5SXin Liprecompiled chunks are not compatible between different Lua versions. 11689*088332b5SXin Li 11690*088332b5SXin Li 11691*088332b5SXin Li<p> 11692*088332b5SXin LiThe standard paths in the official distribution may 11693*088332b5SXin Lichange between versions. 11694*088332b5SXin Li 11695*088332b5SXin Li 11696*088332b5SXin Li 11697*088332b5SXin Li 11698*088332b5SXin Li 11699*088332b5SXin Li<h2>8.1 – <a name="8.1">Incompatibilities in the Language</a></h2> 11700*088332b5SXin Li<ul> 11701*088332b5SXin Li 11702*088332b5SXin Li<li> 11703*088332b5SXin LiThe coercion of strings to numbers in 11704*088332b5SXin Liarithmetic and bitwise operations 11705*088332b5SXin Lihas been removed from the core language. 11706*088332b5SXin LiThe string library does a similar job 11707*088332b5SXin Lifor arithmetic (but not for bitwise) operations 11708*088332b5SXin Liusing the string metamethods. 11709*088332b5SXin LiHowever, unlike in previous versions, 11710*088332b5SXin Lithe new implementation preserves the implicit type of the numeral 11711*088332b5SXin Liin the string. 11712*088332b5SXin LiFor instance, the result of <code>"1" + "2"</code> now is an integer, 11713*088332b5SXin Linot a float. 11714*088332b5SXin Li</li> 11715*088332b5SXin Li 11716*088332b5SXin Li<li> 11717*088332b5SXin LiLiteral decimal integer constants that overflow are read as floats, 11718*088332b5SXin Liinstead of wrapping around. 11719*088332b5SXin LiYou can use hexadecimal notation for such constants if you 11720*088332b5SXin Liwant the old behavior 11721*088332b5SXin Li(reading them as integers with wrap around). 11722*088332b5SXin Li</li> 11723*088332b5SXin Li 11724*088332b5SXin Li<li> 11725*088332b5SXin LiThe use of the <code>__lt</code> metamethod to emulate <code>__le</code> 11726*088332b5SXin Lihas been removed. 11727*088332b5SXin LiWhen needed, this metamethod must be explicitly defined. 11728*088332b5SXin Li</li> 11729*088332b5SXin Li 11730*088332b5SXin Li<li> 11731*088332b5SXin LiThe semantics of the numerical <b>for</b> loop 11732*088332b5SXin Liover integers changed in some details. 11733*088332b5SXin LiIn particular, the control variable never wraps around. 11734*088332b5SXin Li</li> 11735*088332b5SXin Li 11736*088332b5SXin Li<li> 11737*088332b5SXin LiA label for a <b>goto</b> cannot be declared where a label with the same 11738*088332b5SXin Liname is visible, even if this other label is declared in an enclosing 11739*088332b5SXin Liblock. 11740*088332b5SXin Li</li> 11741*088332b5SXin Li 11742*088332b5SXin Li<li> 11743*088332b5SXin LiWhen finalizing an object, 11744*088332b5SXin LiLua does not ignore <code>__gc</code> metamethods that are not functions. 11745*088332b5SXin LiAny value will be called, if present. 11746*088332b5SXin Li(Non-callable values will generate a warning, 11747*088332b5SXin Lilike any other error when calling a finalizer.) 11748*088332b5SXin Li</li> 11749*088332b5SXin Li 11750*088332b5SXin Li</ul> 11751*088332b5SXin Li 11752*088332b5SXin Li 11753*088332b5SXin Li 11754*088332b5SXin Li 11755*088332b5SXin Li<h2>8.2 – <a name="8.2">Incompatibilities in the Libraries</a></h2> 11756*088332b5SXin Li<ul> 11757*088332b5SXin Li 11758*088332b5SXin Li<li> 11759*088332b5SXin LiThe function <a href="#pdf-print"><code>print</code></a> does not call <a href="#pdf-tostring"><code>tostring</code></a> 11760*088332b5SXin Lito format its arguments; 11761*088332b5SXin Liinstead, it has this functionality hardwired. 11762*088332b5SXin LiYou should use <code>__tostring</code> to modify how values are printed. 11763*088332b5SXin Li</li> 11764*088332b5SXin Li 11765*088332b5SXin Li<li> 11766*088332b5SXin LiThe pseudo-random number generator used by the function <a href="#pdf-math.random"><code>math.random</code></a> 11767*088332b5SXin Linow starts with a somewhat random seed. 11768*088332b5SXin LiMoreover, it uses a different algorithm. 11769*088332b5SXin Li</li> 11770*088332b5SXin Li 11771*088332b5SXin Li<li> 11772*088332b5SXin LiBy default, the decoding functions in the <a href="#pdf-utf8"><code>utf8</code></a> library 11773*088332b5SXin Lido not accept surrogates as valid code points. 11774*088332b5SXin LiAn extra parameter in these functions makes them more permissive. 11775*088332b5SXin Li</li> 11776*088332b5SXin Li 11777*088332b5SXin Li<li> 11778*088332b5SXin LiThe options "<code>setpause</code>" and "<code>setstepmul</code>" 11779*088332b5SXin Liof the function <a href="#pdf-collectgarbage"><code>collectgarbage</code></a> are deprecated. 11780*088332b5SXin LiYou should use the new option "<code>incremental</code>" to set them. 11781*088332b5SXin Li</li> 11782*088332b5SXin Li 11783*088332b5SXin Li<li> 11784*088332b5SXin LiThe function <a href="#pdf-io.lines"><code>io.lines</code></a> now returns four values, 11785*088332b5SXin Liinstead of just one. 11786*088332b5SXin LiThat can be a problem when it is used as the sole 11787*088332b5SXin Liargument to another function that has optional parameters, 11788*088332b5SXin Lisuch as in <code>load(io.lines(filename, "L"))</code>. 11789*088332b5SXin LiTo fix that issue, 11790*088332b5SXin Liyou can wrap the call into parentheses, 11791*088332b5SXin Lito adjust its number of results to one. 11792*088332b5SXin Li</li> 11793*088332b5SXin Li 11794*088332b5SXin Li</ul> 11795*088332b5SXin Li 11796*088332b5SXin Li 11797*088332b5SXin Li 11798*088332b5SXin Li 11799*088332b5SXin Li<h2>8.3 – <a name="8.3">Incompatibilities in the API</a></h2> 11800*088332b5SXin Li 11801*088332b5SXin Li 11802*088332b5SXin Li<ul> 11803*088332b5SXin Li 11804*088332b5SXin Li<li> 11805*088332b5SXin LiFull userdata now has an arbitrary number of associated user values. 11806*088332b5SXin LiTherefore, the functions <code>lua_newuserdata</code>, 11807*088332b5SXin Li<code>lua_setuservalue</code>, and <code>lua_getuservalue</code> were 11808*088332b5SXin Lireplaced by <a href="#lua_newuserdatauv"><code>lua_newuserdatauv</code></a>, 11809*088332b5SXin Li<a href="#lua_setiuservalue"><code>lua_setiuservalue</code></a>, and <a href="#lua_getiuservalue"><code>lua_getiuservalue</code></a>, 11810*088332b5SXin Liwhich have an extra argument. 11811*088332b5SXin Li 11812*088332b5SXin Li 11813*088332b5SXin Li<p> 11814*088332b5SXin LiFor compatibility, the old names still work as macros assuming 11815*088332b5SXin Lione single user value. 11816*088332b5SXin LiNote, however, that userdata with zero user values 11817*088332b5SXin Liare more efficient memory-wise. 11818*088332b5SXin Li</li> 11819*088332b5SXin Li 11820*088332b5SXin Li<li> 11821*088332b5SXin LiThe function <a href="#lua_resume"><code>lua_resume</code></a> has an extra parameter. 11822*088332b5SXin LiThis out parameter returns the number of values on 11823*088332b5SXin Lithe top of the stack that were yielded or returned by the coroutine. 11824*088332b5SXin Li(In previous versions, 11825*088332b5SXin Lithose values were the entire stack.) 11826*088332b5SXin Li</li> 11827*088332b5SXin Li 11828*088332b5SXin Li<li> 11829*088332b5SXin LiThe function <a href="#lua_version"><code>lua_version</code></a> returns the version number, 11830*088332b5SXin Liinstead of an address of the version number. 11831*088332b5SXin LiThe Lua core should work correctly with libraries using their 11832*088332b5SXin Liown static copies of the same core, 11833*088332b5SXin Liso there is no need to check whether they are using the same 11834*088332b5SXin Liaddress space. 11835*088332b5SXin Li</li> 11836*088332b5SXin Li 11837*088332b5SXin Li<li> 11838*088332b5SXin LiThe constant <code>LUA_ERRGCMM</code> was removed. 11839*088332b5SXin LiErrors in finalizers are never propagated; 11840*088332b5SXin Liinstead, they generate a warning. 11841*088332b5SXin Li</li> 11842*088332b5SXin Li 11843*088332b5SXin Li<li> 11844*088332b5SXin LiThe options <code>LUA_GCSETPAUSE</code> and <code>LUA_GCSETSTEPMUL</code> 11845*088332b5SXin Liof the function <a href="#lua_gc"><code>lua_gc</code></a> are deprecated. 11846*088332b5SXin LiYou should use the new option <code>LUA_GCINC</code> to set them. 11847*088332b5SXin Li</li> 11848*088332b5SXin Li 11849*088332b5SXin Li</ul> 11850*088332b5SXin Li 11851*088332b5SXin Li 11852*088332b5SXin Li 11853*088332b5SXin Li 11854*088332b5SXin Li<h1>9 – <a name="9">The Complete Syntax of Lua</a></h1> 11855*088332b5SXin Li 11856*088332b5SXin Li<p> 11857*088332b5SXin LiHere is the complete syntax of Lua in extended BNF. 11858*088332b5SXin LiAs usual in extended BNF, 11859*088332b5SXin Li{A} means 0 or more As, 11860*088332b5SXin Liand [A] means an optional A. 11861*088332b5SXin Li(For operator precedences, see <a href="#3.4.8">§3.4.8</a>; 11862*088332b5SXin Lifor a description of the terminals 11863*088332b5SXin LiName, Numeral, 11864*088332b5SXin Liand LiteralString, see <a href="#3.1">§3.1</a>.) 11865*088332b5SXin Li 11866*088332b5SXin Li 11867*088332b5SXin Li 11868*088332b5SXin Li 11869*088332b5SXin Li<pre> 11870*088332b5SXin Li 11871*088332b5SXin Li chunk ::= block 11872*088332b5SXin Li 11873*088332b5SXin Li block ::= {stat} [retstat] 11874*088332b5SXin Li 11875*088332b5SXin Li stat ::= ‘<b>;</b>’ | 11876*088332b5SXin Li varlist ‘<b>=</b>’ explist | 11877*088332b5SXin Li functioncall | 11878*088332b5SXin Li label | 11879*088332b5SXin Li <b>break</b> | 11880*088332b5SXin Li <b>goto</b> Name | 11881*088332b5SXin Li <b>do</b> block <b>end</b> | 11882*088332b5SXin Li <b>while</b> exp <b>do</b> block <b>end</b> | 11883*088332b5SXin Li <b>repeat</b> block <b>until</b> exp | 11884*088332b5SXin Li <b>if</b> exp <b>then</b> block {<b>elseif</b> exp <b>then</b> block} [<b>else</b> block] <b>end</b> | 11885*088332b5SXin Li <b>for</b> Name ‘<b>=</b>’ exp ‘<b>,</b>’ exp [‘<b>,</b>’ exp] <b>do</b> block <b>end</b> | 11886*088332b5SXin Li <b>for</b> namelist <b>in</b> explist <b>do</b> block <b>end</b> | 11887*088332b5SXin Li <b>function</b> funcname funcbody | 11888*088332b5SXin Li <b>local</b> <b>function</b> Name funcbody | 11889*088332b5SXin Li <b>local</b> attnamelist [‘<b>=</b>’ explist] 11890*088332b5SXin Li 11891*088332b5SXin Li attnamelist ::= Name attrib {‘<b>,</b>’ Name attrib} 11892*088332b5SXin Li 11893*088332b5SXin Li attrib ::= [‘<b><</b>’ Name ‘<b>></b>’] 11894*088332b5SXin Li 11895*088332b5SXin Li retstat ::= <b>return</b> [explist] [‘<b>;</b>’] 11896*088332b5SXin Li 11897*088332b5SXin Li label ::= ‘<b>::</b>’ Name ‘<b>::</b>’ 11898*088332b5SXin Li 11899*088332b5SXin Li funcname ::= Name {‘<b>.</b>’ Name} [‘<b>:</b>’ Name] 11900*088332b5SXin Li 11901*088332b5SXin Li varlist ::= var {‘<b>,</b>’ var} 11902*088332b5SXin Li 11903*088332b5SXin Li var ::= Name | prefixexp ‘<b>[</b>’ exp ‘<b>]</b>’ | prefixexp ‘<b>.</b>’ Name 11904*088332b5SXin Li 11905*088332b5SXin Li namelist ::= Name {‘<b>,</b>’ Name} 11906*088332b5SXin Li 11907*088332b5SXin Li explist ::= exp {‘<b>,</b>’ exp} 11908*088332b5SXin Li 11909*088332b5SXin Li exp ::= <b>nil</b> | <b>false</b> | <b>true</b> | Numeral | LiteralString | ‘<b>...</b>’ | functiondef | 11910*088332b5SXin Li prefixexp | tableconstructor | exp binop exp | unop exp 11911*088332b5SXin Li 11912*088332b5SXin Li prefixexp ::= var | functioncall | ‘<b>(</b>’ exp ‘<b>)</b>’ 11913*088332b5SXin Li 11914*088332b5SXin Li functioncall ::= prefixexp args | prefixexp ‘<b>:</b>’ Name args 11915*088332b5SXin Li 11916*088332b5SXin Li args ::= ‘<b>(</b>’ [explist] ‘<b>)</b>’ | tableconstructor | LiteralString 11917*088332b5SXin Li 11918*088332b5SXin Li functiondef ::= <b>function</b> funcbody 11919*088332b5SXin Li 11920*088332b5SXin Li funcbody ::= ‘<b>(</b>’ [parlist] ‘<b>)</b>’ block <b>end</b> 11921*088332b5SXin Li 11922*088332b5SXin Li parlist ::= namelist [‘<b>,</b>’ ‘<b>...</b>’] | ‘<b>...</b>’ 11923*088332b5SXin Li 11924*088332b5SXin Li tableconstructor ::= ‘<b>{</b>’ [fieldlist] ‘<b>}</b>’ 11925*088332b5SXin Li 11926*088332b5SXin Li fieldlist ::= field {fieldsep field} [fieldsep] 11927*088332b5SXin Li 11928*088332b5SXin Li field ::= ‘<b>[</b>’ exp ‘<b>]</b>’ ‘<b>=</b>’ exp | Name ‘<b>=</b>’ exp | exp 11929*088332b5SXin Li 11930*088332b5SXin Li fieldsep ::= ‘<b>,</b>’ | ‘<b>;</b>’ 11931*088332b5SXin Li 11932*088332b5SXin Li binop ::= ‘<b>+</b>’ | ‘<b>-</b>’ | ‘<b>*</b>’ | ‘<b>/</b>’ | ‘<b>//</b>’ | ‘<b>^</b>’ | ‘<b>%</b>’ | 11933*088332b5SXin Li ‘<b>&</b>’ | ‘<b>~</b>’ | ‘<b>|</b>’ | ‘<b>>></b>’ | ‘<b><<</b>’ | ‘<b>..</b>’ | 11934*088332b5SXin Li ‘<b><</b>’ | ‘<b><=</b>’ | ‘<b>></b>’ | ‘<b>>=</b>’ | ‘<b>==</b>’ | ‘<b>~=</b>’ | 11935*088332b5SXin Li <b>and</b> | <b>or</b> 11936*088332b5SXin Li 11937*088332b5SXin Li unop ::= ‘<b>-</b>’ | <b>not</b> | ‘<b>#</b>’ | ‘<b>~</b>’ 11938*088332b5SXin Li 11939*088332b5SXin Li</pre> 11940*088332b5SXin Li 11941*088332b5SXin Li<p> 11942*088332b5SXin Li 11943*088332b5SXin Li 11944*088332b5SXin Li 11945*088332b5SXin Li 11946*088332b5SXin Li 11947*088332b5SXin Li 11948*088332b5SXin Li 11949*088332b5SXin Li<P CLASS="footer"> 11950*088332b5SXin LiLast update: 11951*088332b5SXin LiWed Sep 30 09:46:30 UTC 2020 11952*088332b5SXin Li</P> 11953*088332b5SXin Li<!-- 11954*088332b5SXin LiLast change: revised for Lua 5.4.1 11955*088332b5SXin Li--> 11956*088332b5SXin Li 11957*088332b5SXin Li</body></html> 11958*088332b5SXin Li 11959