111be35a1SLionel Sambuc<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 2*0a6a1f1dSLionel Sambuc<HTML> 3*0a6a1f1dSLionel Sambuc<HEAD> 4*0a6a1f1dSLionel Sambuc<TITLE>Lua 5.3 Reference Manual</TITLE> 5*0a6a1f1dSLionel Sambuc<LINK REL="stylesheet" TYPE="text/css" HREF="lua.css"> 6*0a6a1f1dSLionel Sambuc<LINK REL="stylesheet" TYPE="text/css" HREF="manual.css"> 711be35a1SLionel Sambuc<META HTTP-EQUIV="content-type" CONTENT="text/html; charset=iso-8859-1"> 8*0a6a1f1dSLionel Sambuc</HEAD> 911be35a1SLionel Sambuc 10*0a6a1f1dSLionel Sambuc<BODY> 1111be35a1SLionel Sambuc 12*0a6a1f1dSLionel Sambuc<H1> 13*0a6a1f1dSLionel Sambuc<A HREF="http://www.lua.org/"><IMG SRC="logo.gif" ALT="Lua"></A> 14*0a6a1f1dSLionel SambucLua 5.3 Reference Manual 15*0a6a1f1dSLionel Sambuc</H1> 1611be35a1SLionel Sambuc 17*0a6a1f1dSLionel Sambuc<P> 1811be35a1SLionel Sambucby Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes 19*0a6a1f1dSLionel Sambuc 20*0a6a1f1dSLionel Sambuc<P> 21*0a6a1f1dSLionel Sambuc<SMALL> 22*0a6a1f1dSLionel SambucCopyright © 2015 Lua.org, PUC-Rio. 2311be35a1SLionel SambucFreely available under the terms of the 2411be35a1SLionel Sambuc<a href="http://www.lua.org/license.html">Lua license</a>. 25*0a6a1f1dSLionel Sambuc</SMALL> 2611be35a1SLionel Sambuc 27*0a6a1f1dSLionel Sambuc<DIV CLASS="menubar"> 28*0a6a1f1dSLionel Sambuc<A HREF="contents.html#contents">contents</A> 2911be35a1SLionel Sambuc· 30*0a6a1f1dSLionel Sambuc<A HREF="contents.html#index">index</A> 3111be35a1SLionel Sambuc· 3211be35a1SLionel Sambuc<A HREF="http://www.lua.org/manual/">other versions</A> 33*0a6a1f1dSLionel Sambuc</DIV> 3411be35a1SLionel Sambuc 3511be35a1SLionel Sambuc<!-- ====================================================================== --> 3611be35a1SLionel Sambuc<p> 3711be35a1SLionel Sambuc 38*0a6a1f1dSLionel Sambuc<!-- Id: manual.of,v 1.151 2015/06/10 21:08:57 roberto Exp --> 3911be35a1SLionel Sambuc 4011be35a1SLionel Sambuc 4111be35a1SLionel Sambuc 4211be35a1SLionel Sambuc 43*0a6a1f1dSLionel Sambuc<h1>1 – <a name="1">Introduction</a></h1> 4411be35a1SLionel Sambuc 4511be35a1SLionel Sambuc<p> 4611be35a1SLionel SambucLua is an extension programming language designed to support 4711be35a1SLionel Sambucgeneral procedural programming with data description 4811be35a1SLionel Sambucfacilities. 49*0a6a1f1dSLionel SambucLua also offers good support for object-oriented programming, 5011be35a1SLionel Sambucfunctional programming, and data-driven programming. 51*0a6a1f1dSLionel SambucLua is intended to be used as a powerful, lightweight, 52*0a6a1f1dSLionel Sambucembeddable scripting language for any program that needs one. 53*0a6a1f1dSLionel SambucLua is implemented as a library, written in <em>clean C</em>, 54*0a6a1f1dSLionel Sambucthe common subset of Standard C and C++. 5511be35a1SLionel Sambuc 5611be35a1SLionel Sambuc 5711be35a1SLionel Sambuc<p> 58*0a6a1f1dSLionel SambucAs an extension language, Lua has no notion of a "main" program: 5911be35a1SLionel Sambucit only works <em>embedded</em> in a host client, 6011be35a1SLionel Sambuccalled the <em>embedding program</em> or simply the <em>host</em>. 61*0a6a1f1dSLionel SambucThe host program can invoke functions to execute a piece of Lua code, 6211be35a1SLionel Sambuccan write and read Lua variables, 6311be35a1SLionel Sambucand can register C functions to be called by Lua code. 6411be35a1SLionel SambucThrough the use of C functions, Lua can be augmented to cope with 6511be35a1SLionel Sambuca wide range of different domains, 6611be35a1SLionel Sambucthus creating customized programming languages sharing a syntactical framework. 6711be35a1SLionel SambucThe Lua distribution includes a sample host program called <code>lua</code>, 68*0a6a1f1dSLionel Sambucwhich uses the Lua library to offer a complete, standalone Lua interpreter, 69*0a6a1f1dSLionel Sambucfor interactive or batch use. 7011be35a1SLionel Sambuc 7111be35a1SLionel Sambuc 7211be35a1SLionel Sambuc<p> 7311be35a1SLionel SambucLua is free software, 7411be35a1SLionel Sambucand is provided as usual with no guarantees, 7511be35a1SLionel Sambucas stated in its license. 7611be35a1SLionel SambucThe implementation described in this manual is available 7711be35a1SLionel Sambucat Lua's official web site, <code>www.lua.org</code>. 7811be35a1SLionel Sambuc 7911be35a1SLionel Sambuc 8011be35a1SLionel Sambuc<p> 8111be35a1SLionel SambucLike any other reference manual, 8211be35a1SLionel Sambucthis document is dry in places. 8311be35a1SLionel SambucFor a discussion of the decisions behind the design of Lua, 8411be35a1SLionel Sambucsee the technical papers available at Lua's web site. 8511be35a1SLionel SambucFor a detailed introduction to programming in Lua, 86*0a6a1f1dSLionel Sambucsee Roberto's book, <em>Programming in Lua</em>. 8711be35a1SLionel Sambuc 8811be35a1SLionel Sambuc 8911be35a1SLionel Sambuc 90*0a6a1f1dSLionel Sambuc<h1>2 – <a name="2">Basic Concepts</a></h1> 91*0a6a1f1dSLionel Sambuc 92*0a6a1f1dSLionel Sambuc<p> 93*0a6a1f1dSLionel SambucThis section describes the basic concepts of the language. 94*0a6a1f1dSLionel Sambuc 95*0a6a1f1dSLionel Sambuc 96*0a6a1f1dSLionel Sambuc 97*0a6a1f1dSLionel Sambuc<h2>2.1 – <a name="2.1">Values and Types</a></h2> 98*0a6a1f1dSLionel Sambuc 99*0a6a1f1dSLionel Sambuc<p> 100*0a6a1f1dSLionel SambucLua is a <em>dynamically typed language</em>. 101*0a6a1f1dSLionel SambucThis means that 102*0a6a1f1dSLionel Sambucvariables do not have types; only values do. 103*0a6a1f1dSLionel SambucThere are no type definitions in the language. 104*0a6a1f1dSLionel SambucAll values carry their own type. 105*0a6a1f1dSLionel Sambuc 106*0a6a1f1dSLionel Sambuc 107*0a6a1f1dSLionel Sambuc<p> 108*0a6a1f1dSLionel SambucAll values in Lua are <em>first-class values</em>. 109*0a6a1f1dSLionel SambucThis means that all values can be stored in variables, 110*0a6a1f1dSLionel Sambucpassed as arguments to other functions, and returned as results. 111*0a6a1f1dSLionel Sambuc 112*0a6a1f1dSLionel Sambuc 113*0a6a1f1dSLionel Sambuc<p> 114*0a6a1f1dSLionel SambucThere are eight basic types in Lua: 115*0a6a1f1dSLionel Sambuc<em>nil</em>, <em>boolean</em>, <em>number</em>, 116*0a6a1f1dSLionel Sambuc<em>string</em>, <em>function</em>, <em>userdata</em>, 117*0a6a1f1dSLionel Sambuc<em>thread</em>, and <em>table</em>. 118*0a6a1f1dSLionel SambucThe type <em>nil</em> has one single value, <b>nil</b>, 119*0a6a1f1dSLionel Sambucwhose main property is to be different from any other value; 120*0a6a1f1dSLionel Sambucit usually represents the absence of a useful value. 121*0a6a1f1dSLionel SambucThe type <em>boolean</em> has two values, <b>false</b> and <b>true</b>. 122*0a6a1f1dSLionel SambucBoth <b>nil</b> and <b>false</b> make a condition false; 123*0a6a1f1dSLionel Sambucany other value makes it true. 124*0a6a1f1dSLionel SambucThe type <em>number</em> represents both 125*0a6a1f1dSLionel Sambucinteger numbers and real (floating-point) numbers. 126*0a6a1f1dSLionel SambucThe type <em>string</em> represents immutable sequences of bytes. 127*0a6a1f1dSLionel Sambuc 128*0a6a1f1dSLionel SambucLua is 8-bit clean: 129*0a6a1f1dSLionel Sambucstrings can contain any 8-bit value, 130*0a6a1f1dSLionel Sambucincluding embedded zeros ('<code>\0</code>'). 131*0a6a1f1dSLionel SambucLua is also encoding-agnostic; 132*0a6a1f1dSLionel Sambucit makes no assumptions about the contents of a string. 133*0a6a1f1dSLionel Sambuc 134*0a6a1f1dSLionel Sambuc 135*0a6a1f1dSLionel Sambuc<p> 136*0a6a1f1dSLionel SambucThe type <em>number</em> uses two internal representations, 137*0a6a1f1dSLionel Sambucor two subtypes, 138*0a6a1f1dSLionel Sambucone called <em>integer</em> and the other called <em>float</em>. 139*0a6a1f1dSLionel SambucLua has explicit rules about when each representation is used, 140*0a6a1f1dSLionel Sambucbut it also converts between them automatically as needed (see <a href="#3.4.3">§3.4.3</a>). 141*0a6a1f1dSLionel SambucTherefore, 142*0a6a1f1dSLionel Sambucthe programmer may choose to mostly ignore the difference 143*0a6a1f1dSLionel Sambucbetween integers and floats 144*0a6a1f1dSLionel Sambucor to assume complete control over the representation of each number. 145*0a6a1f1dSLionel SambucStandard Lua uses 64-bit integers and double-precision (64-bit) floats, 146*0a6a1f1dSLionel Sambucbut you can also compile Lua so that it 147*0a6a1f1dSLionel Sambucuses 32-bit integers and/or single-precision (32-bit) floats. 148*0a6a1f1dSLionel SambucThe option with 32 bits for both integers and floats 149*0a6a1f1dSLionel Sambucis particularly attractive 150*0a6a1f1dSLionel Sambucfor small machines and embedded systems. 151*0a6a1f1dSLionel Sambuc(See macro <code>LUA_32BITS</code> in file <code>luaconf.h</code>.) 152*0a6a1f1dSLionel Sambuc 153*0a6a1f1dSLionel Sambuc 154*0a6a1f1dSLionel Sambuc<p> 155*0a6a1f1dSLionel SambucLua can call (and manipulate) functions written in Lua and 156*0a6a1f1dSLionel Sambucfunctions written in C (see <a href="#3.4.10">§3.4.10</a>). 157*0a6a1f1dSLionel SambucBoth are represented by the type <em>function</em>. 158*0a6a1f1dSLionel Sambuc 159*0a6a1f1dSLionel Sambuc 160*0a6a1f1dSLionel Sambuc<p> 161*0a6a1f1dSLionel SambucThe type <em>userdata</em> is provided to allow arbitrary C data to 162*0a6a1f1dSLionel Sambucbe stored in Lua variables. 163*0a6a1f1dSLionel SambucA userdata value represents a block of raw memory. 164*0a6a1f1dSLionel SambucThere are two kinds of userdata: 165*0a6a1f1dSLionel Sambuc<em>full userdata</em>, 166*0a6a1f1dSLionel Sambucwhich is an object with a block of memory managed by Lua, 167*0a6a1f1dSLionel Sambucand <em>light userdata</em>, 168*0a6a1f1dSLionel Sambucwhich is simply a C pointer value. 169*0a6a1f1dSLionel SambucUserdata has no predefined operations in Lua, 170*0a6a1f1dSLionel Sambucexcept assignment and identity test. 171*0a6a1f1dSLionel SambucBy using <em>metatables</em>, 172*0a6a1f1dSLionel Sambucthe programmer can define operations for full userdata values 173*0a6a1f1dSLionel Sambuc(see <a href="#2.4">§2.4</a>). 174*0a6a1f1dSLionel SambucUserdata values cannot be created or modified in Lua, 175*0a6a1f1dSLionel Sambuconly through the C API. 176*0a6a1f1dSLionel SambucThis guarantees the integrity of data owned by the host program. 177*0a6a1f1dSLionel Sambuc 178*0a6a1f1dSLionel Sambuc 179*0a6a1f1dSLionel Sambuc<p> 180*0a6a1f1dSLionel SambucThe type <em>thread</em> represents independent threads of execution 181*0a6a1f1dSLionel Sambucand it is used to implement coroutines (see <a href="#2.6">§2.6</a>). 182*0a6a1f1dSLionel SambucLua threads are not related to operating-system threads. 183*0a6a1f1dSLionel SambucLua supports coroutines on all systems, 184*0a6a1f1dSLionel Sambuceven those that do not support threads natively. 185*0a6a1f1dSLionel Sambuc 186*0a6a1f1dSLionel Sambuc 187*0a6a1f1dSLionel Sambuc<p> 188*0a6a1f1dSLionel SambucThe type <em>table</em> implements associative arrays, 189*0a6a1f1dSLionel Sambucthat is, arrays that can be indexed not only with numbers, 190*0a6a1f1dSLionel Sambucbut with any Lua value except <b>nil</b> and NaN. 191*0a6a1f1dSLionel Sambuc(<em>Not a Number</em> is a special value used to represent 192*0a6a1f1dSLionel Sambucundefined or unrepresentable numerical results, such as <code>0/0</code>.) 193*0a6a1f1dSLionel SambucTables can be <em>heterogeneous</em>; 194*0a6a1f1dSLionel Sambucthat is, they can contain values of all types (except <b>nil</b>). 195*0a6a1f1dSLionel SambucAny key with value <b>nil</b> is not considered part of the table. 196*0a6a1f1dSLionel SambucConversely, any key that is not part of a table has 197*0a6a1f1dSLionel Sambucan associated value <b>nil</b>. 198*0a6a1f1dSLionel Sambuc 199*0a6a1f1dSLionel Sambuc 200*0a6a1f1dSLionel Sambuc<p> 201*0a6a1f1dSLionel SambucTables are the sole data-structuring mechanism in Lua; 202*0a6a1f1dSLionel Sambucthey can be used to represent ordinary arrays, sequences, 203*0a6a1f1dSLionel Sambucsymbol tables, sets, records, graphs, trees, etc. 204*0a6a1f1dSLionel SambucTo represent records, Lua uses the field name as an index. 205*0a6a1f1dSLionel SambucThe language supports this representation by 206*0a6a1f1dSLionel Sambucproviding <code>a.name</code> as syntactic sugar for <code>a["name"]</code>. 207*0a6a1f1dSLionel SambucThere are several convenient ways to create tables in Lua 208*0a6a1f1dSLionel Sambuc(see <a href="#3.4.9">§3.4.9</a>). 209*0a6a1f1dSLionel Sambuc 210*0a6a1f1dSLionel Sambuc 211*0a6a1f1dSLionel Sambuc<p> 212*0a6a1f1dSLionel SambucWe use the term <em>sequence</em> to denote a table where 213*0a6a1f1dSLionel Sambucthe set of all positive numeric keys is equal to {1..<em>n</em>} 214*0a6a1f1dSLionel Sambucfor some non-negative integer <em>n</em>, 215*0a6a1f1dSLionel Sambucwhich is called the length of the sequence (see <a href="#3.4.7">§3.4.7</a>). 216*0a6a1f1dSLionel Sambuc 217*0a6a1f1dSLionel Sambuc 218*0a6a1f1dSLionel Sambuc<p> 219*0a6a1f1dSLionel SambucLike indices, 220*0a6a1f1dSLionel Sambucthe values of table fields can be of any type. 221*0a6a1f1dSLionel SambucIn particular, 222*0a6a1f1dSLionel Sambucbecause functions are first-class values, 223*0a6a1f1dSLionel Sambuctable fields can contain functions. 224*0a6a1f1dSLionel SambucThus tables can also carry <em>methods</em> (see <a href="#3.4.11">§3.4.11</a>). 225*0a6a1f1dSLionel Sambuc 226*0a6a1f1dSLionel Sambuc 227*0a6a1f1dSLionel Sambuc<p> 228*0a6a1f1dSLionel SambucThe indexing of tables follows 229*0a6a1f1dSLionel Sambucthe definition of raw equality in the language. 230*0a6a1f1dSLionel SambucThe expressions <code>a[i]</code> and <code>a[j]</code> 231*0a6a1f1dSLionel Sambucdenote the same table element 232*0a6a1f1dSLionel Sambucif and only if <code>i</code> and <code>j</code> are raw equal 233*0a6a1f1dSLionel Sambuc(that is, equal without metamethods). 234*0a6a1f1dSLionel SambucIn particular, floats with integral values 235*0a6a1f1dSLionel Sambucare equal to their respective integers 236*0a6a1f1dSLionel Sambuc(e.g., <code>1.0 == 1</code>). 237*0a6a1f1dSLionel SambucTo avoid ambiguities, 238*0a6a1f1dSLionel Sambucany float with integral value used as a key 239*0a6a1f1dSLionel Sambucis converted to its respective integer. 240*0a6a1f1dSLionel SambucFor instance, if you write <code>a[2.0] = true</code>, 241*0a6a1f1dSLionel Sambucthe actual key inserted into the table will be the 242*0a6a1f1dSLionel Sambucinteger <code>2</code>. 243*0a6a1f1dSLionel Sambuc(On the other hand, 244*0a6a1f1dSLionel Sambuc2 and "<code>2</code>" are different Lua values and therefore 245*0a6a1f1dSLionel Sambucdenote different table entries.) 246*0a6a1f1dSLionel Sambuc 247*0a6a1f1dSLionel Sambuc 248*0a6a1f1dSLionel Sambuc<p> 249*0a6a1f1dSLionel SambucTables, functions, threads, and (full) userdata values are <em>objects</em>: 250*0a6a1f1dSLionel Sambucvariables do not actually <em>contain</em> these values, 251*0a6a1f1dSLionel Sambuconly <em>references</em> to them. 252*0a6a1f1dSLionel SambucAssignment, parameter passing, and function returns 253*0a6a1f1dSLionel Sambucalways manipulate references to such values; 254*0a6a1f1dSLionel Sambucthese operations do not imply any kind of copy. 255*0a6a1f1dSLionel Sambuc 256*0a6a1f1dSLionel Sambuc 257*0a6a1f1dSLionel Sambuc<p> 258*0a6a1f1dSLionel SambucThe library function <a href="#pdf-type"><code>type</code></a> returns a string describing the type 259*0a6a1f1dSLionel Sambucof a given value (see <a href="#6.1">§6.1</a>). 260*0a6a1f1dSLionel Sambuc 261*0a6a1f1dSLionel Sambuc 262*0a6a1f1dSLionel Sambuc 263*0a6a1f1dSLionel Sambuc 264*0a6a1f1dSLionel Sambuc 265*0a6a1f1dSLionel Sambuc<h2>2.2 – <a name="2.2">Environments and the Global Environment</a></h2> 266*0a6a1f1dSLionel Sambuc 267*0a6a1f1dSLionel Sambuc<p> 268*0a6a1f1dSLionel SambucAs will be discussed in <a href="#3.2">§3.2</a> and <a href="#3.3.3">§3.3.3</a>, 269*0a6a1f1dSLionel Sambucany reference to a free name 270*0a6a1f1dSLionel Sambuc(that is, a name not bound to any declaration) <code>var</code> 271*0a6a1f1dSLionel Sambucis syntactically translated to <code>_ENV.var</code>. 272*0a6a1f1dSLionel SambucMoreover, every chunk is compiled in the scope of 273*0a6a1f1dSLionel Sambucan external local variable named <code>_ENV</code> (see <a href="#3.3.2">§3.3.2</a>), 274*0a6a1f1dSLionel Sambucso <code>_ENV</code> itself is never a free name in a chunk. 275*0a6a1f1dSLionel Sambuc 276*0a6a1f1dSLionel Sambuc 277*0a6a1f1dSLionel Sambuc<p> 278*0a6a1f1dSLionel SambucDespite the existence of this external <code>_ENV</code> variable and 279*0a6a1f1dSLionel Sambucthe translation of free names, 280*0a6a1f1dSLionel Sambuc<code>_ENV</code> is a completely regular name. 281*0a6a1f1dSLionel SambucIn particular, 282*0a6a1f1dSLionel Sambucyou can define new variables and parameters with that name. 283*0a6a1f1dSLionel SambucEach reference to a free name uses the <code>_ENV</code> that is 284*0a6a1f1dSLionel Sambucvisible at that point in the program, 285*0a6a1f1dSLionel Sambucfollowing the usual visibility rules of Lua (see <a href="#3.5">§3.5</a>). 286*0a6a1f1dSLionel Sambuc 287*0a6a1f1dSLionel Sambuc 288*0a6a1f1dSLionel Sambuc<p> 289*0a6a1f1dSLionel SambucAny table used as the value of <code>_ENV</code> is called an <em>environment</em>. 290*0a6a1f1dSLionel Sambuc 291*0a6a1f1dSLionel Sambuc 292*0a6a1f1dSLionel Sambuc<p> 293*0a6a1f1dSLionel SambucLua keeps a distinguished environment called the <em>global environment</em>. 294*0a6a1f1dSLionel SambucThis value is kept at a special index in the C registry (see <a href="#4.5">§4.5</a>). 295*0a6a1f1dSLionel SambucIn Lua, the global variable <a href="#pdf-_G"><code>_G</code></a> is initialized with this same value. 296*0a6a1f1dSLionel Sambuc(<a href="#pdf-_G"><code>_G</code></a> is never used internally.) 297*0a6a1f1dSLionel Sambuc 298*0a6a1f1dSLionel Sambuc 299*0a6a1f1dSLionel Sambuc<p> 300*0a6a1f1dSLionel SambucWhen Lua loads a chunk, 301*0a6a1f1dSLionel Sambucthe default value for its <code>_ENV</code> upvalue 302*0a6a1f1dSLionel Sambucis the global environment (see <a href="#pdf-load"><code>load</code></a>). 303*0a6a1f1dSLionel SambucTherefore, by default, 304*0a6a1f1dSLionel Sambucfree names in Lua code refer to entries in the global environment 305*0a6a1f1dSLionel Sambuc(and, therefore, they are also called <em>global variables</em>). 306*0a6a1f1dSLionel SambucMoreover, all standard libraries are loaded in the global environment 307*0a6a1f1dSLionel Sambucand some functions there operate on that environment. 308*0a6a1f1dSLionel SambucYou can use <a href="#pdf-load"><code>load</code></a> (or <a href="#pdf-loadfile"><code>loadfile</code></a>) 309*0a6a1f1dSLionel Sambucto load a chunk with a different environment. 310*0a6a1f1dSLionel Sambuc(In C, you have to load the chunk and then change the value 311*0a6a1f1dSLionel Sambucof its first upvalue.) 312*0a6a1f1dSLionel Sambuc 313*0a6a1f1dSLionel Sambuc 314*0a6a1f1dSLionel Sambuc 315*0a6a1f1dSLionel Sambuc 316*0a6a1f1dSLionel Sambuc 317*0a6a1f1dSLionel Sambuc<h2>2.3 – <a name="2.3">Error Handling</a></h2> 318*0a6a1f1dSLionel Sambuc 319*0a6a1f1dSLionel Sambuc<p> 320*0a6a1f1dSLionel SambucBecause Lua is an embedded extension language, 321*0a6a1f1dSLionel Sambucall Lua actions start from C code in the host program 322*0a6a1f1dSLionel Sambuccalling a function from the Lua library. 323*0a6a1f1dSLionel Sambuc(When you use Lua standalone, 324*0a6a1f1dSLionel Sambucthe <code>lua</code> application is the host program.) 325*0a6a1f1dSLionel SambucWhenever an error occurs during 326*0a6a1f1dSLionel Sambucthe compilation or execution of a Lua chunk, 327*0a6a1f1dSLionel Sambuccontrol returns to the host, 328*0a6a1f1dSLionel Sambucwhich can take appropriate measures 329*0a6a1f1dSLionel Sambuc(such as printing an error message). 330*0a6a1f1dSLionel Sambuc 331*0a6a1f1dSLionel Sambuc 332*0a6a1f1dSLionel Sambuc<p> 333*0a6a1f1dSLionel SambucLua code can explicitly generate an error by calling the 334*0a6a1f1dSLionel Sambuc<a href="#pdf-error"><code>error</code></a> function. 335*0a6a1f1dSLionel SambucIf you need to catch errors in Lua, 336*0a6a1f1dSLionel Sambucyou can use <a href="#pdf-pcall"><code>pcall</code></a> or <a href="#pdf-xpcall"><code>xpcall</code></a> 337*0a6a1f1dSLionel Sambucto call a given function in <em>protected mode</em>. 338*0a6a1f1dSLionel Sambuc 339*0a6a1f1dSLionel Sambuc 340*0a6a1f1dSLionel Sambuc<p> 341*0a6a1f1dSLionel SambucWhenever there is an error, 342*0a6a1f1dSLionel Sambucan <em>error object</em> (also called an <em>error message</em>) 343*0a6a1f1dSLionel Sambucis propagated with information about the error. 344*0a6a1f1dSLionel SambucLua itself only generates errors whose error object is a string, 345*0a6a1f1dSLionel Sambucbut programs may generate errors with 346*0a6a1f1dSLionel Sambucany value as the error object. 347*0a6a1f1dSLionel SambucIt is up to the Lua program or its host to handle such error objects. 348*0a6a1f1dSLionel Sambuc 349*0a6a1f1dSLionel Sambuc 350*0a6a1f1dSLionel Sambuc<p> 351*0a6a1f1dSLionel SambucWhen you use <a href="#pdf-xpcall"><code>xpcall</code></a> or <a href="#lua_pcall"><code>lua_pcall</code></a>, 352*0a6a1f1dSLionel Sambucyou may give a <em>message handler</em> 353*0a6a1f1dSLionel Sambucto be called in case of errors. 354*0a6a1f1dSLionel SambucThis function is called with the original error message 355*0a6a1f1dSLionel Sambucand returns a new error message. 356*0a6a1f1dSLionel SambucIt is called before the error unwinds the stack, 357*0a6a1f1dSLionel Sambucso that it can gather more information about the error, 358*0a6a1f1dSLionel Sambucfor instance by inspecting the stack and creating a stack traceback. 359*0a6a1f1dSLionel SambucThis message handler is still protected by the protected call; 360*0a6a1f1dSLionel Sambucso, an error inside the message handler 361*0a6a1f1dSLionel Sambucwill call the message handler again. 362*0a6a1f1dSLionel SambucIf this loop goes on for too long, 363*0a6a1f1dSLionel SambucLua breaks it and returns an appropriate message. 364*0a6a1f1dSLionel Sambuc 365*0a6a1f1dSLionel Sambuc 366*0a6a1f1dSLionel Sambuc 367*0a6a1f1dSLionel Sambuc 368*0a6a1f1dSLionel Sambuc 369*0a6a1f1dSLionel Sambuc<h2>2.4 – <a name="2.4">Metatables and Metamethods</a></h2> 370*0a6a1f1dSLionel Sambuc 371*0a6a1f1dSLionel Sambuc<p> 372*0a6a1f1dSLionel SambucEvery value in Lua can have a <em>metatable</em>. 373*0a6a1f1dSLionel SambucThis <em>metatable</em> is an ordinary Lua table 374*0a6a1f1dSLionel Sambucthat defines the behavior of the original value 375*0a6a1f1dSLionel Sambucunder certain special operations. 376*0a6a1f1dSLionel SambucYou can change several aspects of the behavior 377*0a6a1f1dSLionel Sambucof operations over a value by setting specific fields in its metatable. 378*0a6a1f1dSLionel SambucFor instance, when a non-numeric value is the operand of an addition, 379*0a6a1f1dSLionel SambucLua checks for a function in the field "<code>__add</code>" of the value's metatable. 380*0a6a1f1dSLionel SambucIf it finds one, 381*0a6a1f1dSLionel SambucLua calls this function to perform the addition. 382*0a6a1f1dSLionel Sambuc 383*0a6a1f1dSLionel Sambuc 384*0a6a1f1dSLionel Sambuc<p> 385*0a6a1f1dSLionel SambucThe keys in a metatable are derived from the <em>event</em> names; 386*0a6a1f1dSLionel Sambucthe corresponding values are called <em>metamethods</em>. 387*0a6a1f1dSLionel SambucIn the previous example, the event is <code>"add"</code> 388*0a6a1f1dSLionel Sambucand the metamethod is the function that performs the addition. 389*0a6a1f1dSLionel Sambuc 390*0a6a1f1dSLionel Sambuc 391*0a6a1f1dSLionel Sambuc<p> 392*0a6a1f1dSLionel SambucYou can query the metatable of any value 393*0a6a1f1dSLionel Sambucusing the <a href="#pdf-getmetatable"><code>getmetatable</code></a> function. 394*0a6a1f1dSLionel Sambuc 395*0a6a1f1dSLionel Sambuc 396*0a6a1f1dSLionel Sambuc<p> 397*0a6a1f1dSLionel SambucYou can replace the metatable of tables 398*0a6a1f1dSLionel Sambucusing the <a href="#pdf-setmetatable"><code>setmetatable</code></a> function. 399*0a6a1f1dSLionel SambucYou cannot change the metatable of other types from Lua code 400*0a6a1f1dSLionel Sambuc(except by using the debug library (<a href="#6.10">§6.10</a>)); 401*0a6a1f1dSLionel Sambucyou must use the C API for that. 402*0a6a1f1dSLionel Sambuc 403*0a6a1f1dSLionel Sambuc 404*0a6a1f1dSLionel Sambuc<p> 405*0a6a1f1dSLionel SambucTables and full userdata have individual metatables 406*0a6a1f1dSLionel Sambuc(although multiple tables and userdata can share their metatables). 407*0a6a1f1dSLionel SambucValues of all other types share one single metatable per type; 408*0a6a1f1dSLionel Sambucthat is, there is one single metatable for all numbers, 409*0a6a1f1dSLionel Sambucone for all strings, etc. 410*0a6a1f1dSLionel SambucBy default, a value has no metatable, 411*0a6a1f1dSLionel Sambucbut the string library sets a metatable for the string type (see <a href="#6.4">§6.4</a>). 412*0a6a1f1dSLionel Sambuc 413*0a6a1f1dSLionel Sambuc 414*0a6a1f1dSLionel Sambuc<p> 415*0a6a1f1dSLionel SambucA metatable controls how an object behaves in 416*0a6a1f1dSLionel Sambucarithmetic operations, bitwise operations, 417*0a6a1f1dSLionel Sambucorder comparisons, concatenation, length operation, calls, and indexing. 418*0a6a1f1dSLionel SambucA metatable also can define a function to be called 419*0a6a1f1dSLionel Sambucwhen a userdata or a table is garbage collected (<a href="#2.5">§2.5</a>). 420*0a6a1f1dSLionel Sambuc 421*0a6a1f1dSLionel Sambuc 422*0a6a1f1dSLionel Sambuc<p> 423*0a6a1f1dSLionel SambucA detailed list of events controlled by metatables is given next. 424*0a6a1f1dSLionel SambucEach operation is identified by its corresponding event name. 425*0a6a1f1dSLionel SambucThe key for each event is a string with its name prefixed by 426*0a6a1f1dSLionel Sambuctwo underscores, '<code>__</code>'; 427*0a6a1f1dSLionel Sambucfor instance, the key for operation "add" is the 428*0a6a1f1dSLionel Sambucstring "<code>__add</code>". 429*0a6a1f1dSLionel SambucNote that queries for metamethods are always raw; 430*0a6a1f1dSLionel Sambucthe access to a metamethod does not invoke other metamethods. 431*0a6a1f1dSLionel Sambuc 432*0a6a1f1dSLionel Sambuc 433*0a6a1f1dSLionel Sambuc<p> 434*0a6a1f1dSLionel SambucFor the unary operators (negation, length, and bitwise not), 435*0a6a1f1dSLionel Sambucthe metamethod is computed and called with a dummy second operand, 436*0a6a1f1dSLionel Sambucequal to the first one. 437*0a6a1f1dSLionel SambucThis extra operand is only to simplify Lua's internals 438*0a6a1f1dSLionel Sambuc(by making these operators behave like a binary operation) 439*0a6a1f1dSLionel Sambucand may be removed in future versions. 440*0a6a1f1dSLionel Sambuc(For most uses this extra operand is irrelevant.) 441*0a6a1f1dSLionel Sambuc 442*0a6a1f1dSLionel Sambuc 443*0a6a1f1dSLionel Sambuc 444*0a6a1f1dSLionel Sambuc<ul> 445*0a6a1f1dSLionel Sambuc 446*0a6a1f1dSLionel Sambuc<li><b>"add": </b> 447*0a6a1f1dSLionel Sambucthe <code>+</code> operation. 448*0a6a1f1dSLionel Sambuc 449*0a6a1f1dSLionel SambucIf any operand for an addition is not a number 450*0a6a1f1dSLionel Sambuc(nor a string coercible to a number), 451*0a6a1f1dSLionel SambucLua will try to call a metamethod. 452*0a6a1f1dSLionel SambucFirst, Lua will check the first operand (even if it is valid). 453*0a6a1f1dSLionel SambucIf that operand does not define a metamethod for the "<code>__add</code>" event, 454*0a6a1f1dSLionel Sambucthen Lua will check the second operand. 455*0a6a1f1dSLionel SambucIf Lua can find a metamethod, 456*0a6a1f1dSLionel Sambucit calls the metamethod with the two operands as arguments, 457*0a6a1f1dSLionel Sambucand the result of the call 458*0a6a1f1dSLionel Sambuc(adjusted to one value) 459*0a6a1f1dSLionel Sambucis the result of the operation. 460*0a6a1f1dSLionel SambucOtherwise, 461*0a6a1f1dSLionel Sambucit raises an error. 462*0a6a1f1dSLionel Sambuc</li> 463*0a6a1f1dSLionel Sambuc 464*0a6a1f1dSLionel Sambuc<li><b>"sub": </b> 465*0a6a1f1dSLionel Sambucthe <code>-</code> operation. 466*0a6a1f1dSLionel Sambuc 467*0a6a1f1dSLionel SambucBehavior similar to the "add" operation. 468*0a6a1f1dSLionel Sambuc</li> 469*0a6a1f1dSLionel Sambuc 470*0a6a1f1dSLionel Sambuc<li><b>"mul": </b> 471*0a6a1f1dSLionel Sambucthe <code>*</code> operation. 472*0a6a1f1dSLionel Sambuc 473*0a6a1f1dSLionel SambucBehavior similar to the "add" operation. 474*0a6a1f1dSLionel Sambuc</li> 475*0a6a1f1dSLionel Sambuc 476*0a6a1f1dSLionel Sambuc<li><b>"div": </b> 477*0a6a1f1dSLionel Sambucthe <code>/</code> operation. 478*0a6a1f1dSLionel Sambuc 479*0a6a1f1dSLionel SambucBehavior similar to the "add" operation. 480*0a6a1f1dSLionel Sambuc</li> 481*0a6a1f1dSLionel Sambuc 482*0a6a1f1dSLionel Sambuc<li><b>"mod": </b> 483*0a6a1f1dSLionel Sambucthe <code>%</code> operation. 484*0a6a1f1dSLionel Sambuc 485*0a6a1f1dSLionel SambucBehavior similar to the "add" operation. 486*0a6a1f1dSLionel Sambuc</li> 487*0a6a1f1dSLionel Sambuc 488*0a6a1f1dSLionel Sambuc<li><b>"pow": </b> 489*0a6a1f1dSLionel Sambucthe <code>^</code> (exponentiation) operation. 490*0a6a1f1dSLionel Sambuc 491*0a6a1f1dSLionel SambucBehavior similar to the "add" operation. 492*0a6a1f1dSLionel Sambuc</li> 493*0a6a1f1dSLionel Sambuc 494*0a6a1f1dSLionel Sambuc<li><b>"unm": </b> 495*0a6a1f1dSLionel Sambucthe <code>-</code> (unary minus) operation. 496*0a6a1f1dSLionel Sambuc 497*0a6a1f1dSLionel SambucBehavior similar to the "add" operation. 498*0a6a1f1dSLionel Sambuc</li> 499*0a6a1f1dSLionel Sambuc 500*0a6a1f1dSLionel Sambuc<li><b>"idiv": </b> 501*0a6a1f1dSLionel Sambucthe <code>//</code> (floor division) operation. 502*0a6a1f1dSLionel Sambuc 503*0a6a1f1dSLionel SambucBehavior similar to the "add" operation. 504*0a6a1f1dSLionel Sambuc</li> 505*0a6a1f1dSLionel Sambuc 506*0a6a1f1dSLionel Sambuc<li><b>"band": </b> 507*0a6a1f1dSLionel Sambucthe <code>&</code> (bitwise and) operation. 508*0a6a1f1dSLionel Sambuc 509*0a6a1f1dSLionel SambucBehavior similar to the "add" operation, 510*0a6a1f1dSLionel Sambucexcept that Lua will try a metamethod 511*0a6a1f1dSLionel Sambucif any operand is neither an integer 512*0a6a1f1dSLionel Sambucnor a value coercible to an integer (see <a href="#3.4.3">§3.4.3</a>). 513*0a6a1f1dSLionel Sambuc</li> 514*0a6a1f1dSLionel Sambuc 515*0a6a1f1dSLionel Sambuc<li><b>"bor": </b> 516*0a6a1f1dSLionel Sambucthe <code>|</code> (bitwise or) operation. 517*0a6a1f1dSLionel Sambuc 518*0a6a1f1dSLionel SambucBehavior similar to the "band" operation. 519*0a6a1f1dSLionel Sambuc</li> 520*0a6a1f1dSLionel Sambuc 521*0a6a1f1dSLionel Sambuc<li><b>"bxor": </b> 522*0a6a1f1dSLionel Sambucthe <code>~</code> (bitwise exclusive or) operation. 523*0a6a1f1dSLionel Sambuc 524*0a6a1f1dSLionel SambucBehavior similar to the "band" operation. 525*0a6a1f1dSLionel Sambuc</li> 526*0a6a1f1dSLionel Sambuc 527*0a6a1f1dSLionel Sambuc<li><b>"bnot": </b> 528*0a6a1f1dSLionel Sambucthe <code>~</code> (bitwise unary not) operation. 529*0a6a1f1dSLionel Sambuc 530*0a6a1f1dSLionel SambucBehavior similar to the "band" operation. 531*0a6a1f1dSLionel Sambuc</li> 532*0a6a1f1dSLionel Sambuc 533*0a6a1f1dSLionel Sambuc<li><b>"shl": </b> 534*0a6a1f1dSLionel Sambucthe <code><<</code> (bitwise left shift) operation. 535*0a6a1f1dSLionel Sambuc 536*0a6a1f1dSLionel SambucBehavior similar to the "band" operation. 537*0a6a1f1dSLionel Sambuc</li> 538*0a6a1f1dSLionel Sambuc 539*0a6a1f1dSLionel Sambuc<li><b>"shr": </b> 540*0a6a1f1dSLionel Sambucthe <code>>></code> (bitwise right shift) operation. 541*0a6a1f1dSLionel Sambuc 542*0a6a1f1dSLionel SambucBehavior similar to the "band" operation. 543*0a6a1f1dSLionel Sambuc</li> 544*0a6a1f1dSLionel Sambuc 545*0a6a1f1dSLionel Sambuc<li><b>"concat": </b> 546*0a6a1f1dSLionel Sambucthe <code>..</code> (concatenation) operation. 547*0a6a1f1dSLionel Sambuc 548*0a6a1f1dSLionel SambucBehavior similar to the "add" operation, 549*0a6a1f1dSLionel Sambucexcept that Lua will try a metamethod 550*0a6a1f1dSLionel Sambucif any operand is neither a string nor a number 551*0a6a1f1dSLionel Sambuc(which is always coercible to a string). 552*0a6a1f1dSLionel Sambuc</li> 553*0a6a1f1dSLionel Sambuc 554*0a6a1f1dSLionel Sambuc<li><b>"len": </b> 555*0a6a1f1dSLionel Sambucthe <code>#</code> (length) operation. 556*0a6a1f1dSLionel Sambuc 557*0a6a1f1dSLionel SambucIf the object is not a string, 558*0a6a1f1dSLionel SambucLua will try its metamethod. 559*0a6a1f1dSLionel SambucIf there is a metamethod, 560*0a6a1f1dSLionel SambucLua calls it with the object as argument, 561*0a6a1f1dSLionel Sambucand the result of the call 562*0a6a1f1dSLionel Sambuc(always adjusted to one value) 563*0a6a1f1dSLionel Sambucis the result of the operation. 564*0a6a1f1dSLionel SambucIf there is no metamethod but the object is a table, 565*0a6a1f1dSLionel Sambucthen Lua uses the table length operation (see <a href="#3.4.7">§3.4.7</a>). 566*0a6a1f1dSLionel SambucOtherwise, Lua raises an error. 567*0a6a1f1dSLionel Sambuc</li> 568*0a6a1f1dSLionel Sambuc 569*0a6a1f1dSLionel Sambuc<li><b>"eq": </b> 570*0a6a1f1dSLionel Sambucthe <code>==</code> (equal) operation. 571*0a6a1f1dSLionel Sambuc 572*0a6a1f1dSLionel SambucBehavior similar to the "add" operation, 573*0a6a1f1dSLionel Sambucexcept that Lua will try a metamethod only when the values 574*0a6a1f1dSLionel Sambucbeing compared are either both tables or both full userdata 575*0a6a1f1dSLionel Sambucand they are not primitively equal. 576*0a6a1f1dSLionel SambucThe result of the call is always converted to a boolean. 577*0a6a1f1dSLionel Sambuc</li> 578*0a6a1f1dSLionel Sambuc 579*0a6a1f1dSLionel Sambuc<li><b>"lt": </b> 580*0a6a1f1dSLionel Sambucthe <code><</code> (less than) operation. 581*0a6a1f1dSLionel Sambuc 582*0a6a1f1dSLionel SambucBehavior similar to the "add" operation, 583*0a6a1f1dSLionel Sambucexcept that Lua will try a metamethod only when the values 584*0a6a1f1dSLionel Sambucbeing compared are neither both numbers nor both strings. 585*0a6a1f1dSLionel SambucThe result of the call is always converted to a boolean. 586*0a6a1f1dSLionel Sambuc</li> 587*0a6a1f1dSLionel Sambuc 588*0a6a1f1dSLionel Sambuc<li><b>"le": </b> 589*0a6a1f1dSLionel Sambucthe <code><=</code> (less equal) operation. 590*0a6a1f1dSLionel Sambuc 591*0a6a1f1dSLionel SambucUnlike other operations, 592*0a6a1f1dSLionel SambucThe less-equal operation can use two different events. 593*0a6a1f1dSLionel SambucFirst, Lua looks for the "<code>__le</code>" metamethod in both operands, 594*0a6a1f1dSLionel Sambuclike in the "lt" operation. 595*0a6a1f1dSLionel SambucIf it cannot find such a metamethod, 596*0a6a1f1dSLionel Sambucthen it will try the "<code>__lt</code>" event, 597*0a6a1f1dSLionel Sambucassuming that <code>a <= b</code> is equivalent to <code>not (b < a)</code>. 598*0a6a1f1dSLionel SambucAs with the other comparison operators, 599*0a6a1f1dSLionel Sambucthe result is always a boolean. 600*0a6a1f1dSLionel Sambuc(This use of the "<code>__lt</code>" event can be removed in future versions; 601*0a6a1f1dSLionel Sambucit is also slower than a real "<code>__le</code>" metamethod.) 602*0a6a1f1dSLionel Sambuc</li> 603*0a6a1f1dSLionel Sambuc 604*0a6a1f1dSLionel Sambuc<li><b>"index": </b> 605*0a6a1f1dSLionel SambucThe indexing access <code>table[key]</code>. 606*0a6a1f1dSLionel Sambuc 607*0a6a1f1dSLionel SambucThis event happens when <code>table</code> is not a table or 608*0a6a1f1dSLionel Sambucwhen <code>key</code> is not present in <code>table</code>. 609*0a6a1f1dSLionel SambucThe metamethod is looked up in <code>table</code>. 610*0a6a1f1dSLionel Sambuc 611*0a6a1f1dSLionel Sambuc 612*0a6a1f1dSLionel Sambuc<p> 613*0a6a1f1dSLionel SambucDespite the name, 614*0a6a1f1dSLionel Sambucthe metamethod for this event can be either a function or a table. 615*0a6a1f1dSLionel SambucIf it is a function, 616*0a6a1f1dSLionel Sambucit is called with <code>table</code> and <code>key</code> as arguments. 617*0a6a1f1dSLionel SambucIf it is a table, 618*0a6a1f1dSLionel Sambucthe final result is the result of indexing this table with <code>key</code>. 619*0a6a1f1dSLionel Sambuc(This indexing is regular, not raw, 620*0a6a1f1dSLionel Sambucand therefore can trigger another metamethod.) 621*0a6a1f1dSLionel Sambuc</li> 622*0a6a1f1dSLionel Sambuc 623*0a6a1f1dSLionel Sambuc<li><b>"newindex": </b> 624*0a6a1f1dSLionel SambucThe indexing assignment <code>table[key] = value</code>. 625*0a6a1f1dSLionel Sambuc 626*0a6a1f1dSLionel SambucLike the index event, 627*0a6a1f1dSLionel Sambucthis event happens when <code>table</code> is not a table or 628*0a6a1f1dSLionel Sambucwhen <code>key</code> is not present in <code>table</code>. 629*0a6a1f1dSLionel SambucThe metamethod is looked up in <code>table</code>. 630*0a6a1f1dSLionel Sambuc 631*0a6a1f1dSLionel Sambuc 632*0a6a1f1dSLionel Sambuc<p> 633*0a6a1f1dSLionel SambucLike with indexing, 634*0a6a1f1dSLionel Sambucthe metamethod for this event can be either a function or a table. 635*0a6a1f1dSLionel SambucIf it is a function, 636*0a6a1f1dSLionel Sambucit is called with <code>table</code>, <code>key</code>, and <code>value</code> as arguments. 637*0a6a1f1dSLionel SambucIf it is a table, 638*0a6a1f1dSLionel SambucLua does an indexing assignment to this table with the same key and value. 639*0a6a1f1dSLionel Sambuc(This assignment is regular, not raw, 640*0a6a1f1dSLionel Sambucand therefore can trigger another metamethod.) 641*0a6a1f1dSLionel Sambuc 642*0a6a1f1dSLionel Sambuc 643*0a6a1f1dSLionel Sambuc<p> 644*0a6a1f1dSLionel SambucWhenever there is a "newindex" metamethod, 645*0a6a1f1dSLionel SambucLua does not perform the primitive assignment. 646*0a6a1f1dSLionel Sambuc(If necessary, 647*0a6a1f1dSLionel Sambucthe metamethod itself can call <a href="#pdf-rawset"><code>rawset</code></a> 648*0a6a1f1dSLionel Sambucto do the assignment.) 649*0a6a1f1dSLionel Sambuc</li> 650*0a6a1f1dSLionel Sambuc 651*0a6a1f1dSLionel Sambuc<li><b>"call": </b> 652*0a6a1f1dSLionel SambucThe call operation <code>func(args)</code>. 653*0a6a1f1dSLionel Sambuc 654*0a6a1f1dSLionel SambucThis event happens when Lua tries to call a non-function value 655*0a6a1f1dSLionel Sambuc(that is, <code>func</code> is not a function). 656*0a6a1f1dSLionel SambucThe metamethod is looked up in <code>func</code>. 657*0a6a1f1dSLionel SambucIf present, 658*0a6a1f1dSLionel Sambucthe metamethod is called with <code>func</code> as its first argument, 659*0a6a1f1dSLionel Sambucfollowed by the arguments of the original call (<code>args</code>). 660*0a6a1f1dSLionel Sambuc</li> 661*0a6a1f1dSLionel Sambuc 662*0a6a1f1dSLionel Sambuc</ul> 663*0a6a1f1dSLionel Sambuc 664*0a6a1f1dSLionel Sambuc<p> 665*0a6a1f1dSLionel SambucIt is a good practice to add all needed metamethods to a table 666*0a6a1f1dSLionel Sambucbefore setting it as a metatable of some object. 667*0a6a1f1dSLionel SambucIn particular, the "<code>__gc</code>" metamethod works only when this order 668*0a6a1f1dSLionel Sambucis followed (see <a href="#2.5.1">§2.5.1</a>). 669*0a6a1f1dSLionel Sambuc 670*0a6a1f1dSLionel Sambuc 671*0a6a1f1dSLionel Sambuc 672*0a6a1f1dSLionel Sambuc 673*0a6a1f1dSLionel Sambuc 674*0a6a1f1dSLionel Sambuc<h2>2.5 – <a name="2.5">Garbage Collection</a></h2> 675*0a6a1f1dSLionel Sambuc 676*0a6a1f1dSLionel Sambuc<p> 677*0a6a1f1dSLionel SambucLua performs automatic memory management. 678*0a6a1f1dSLionel SambucThis means that 679*0a6a1f1dSLionel Sambucyou do not have to worry about allocating memory for new objects 680*0a6a1f1dSLionel Sambucor freeing it when the objects are no longer needed. 681*0a6a1f1dSLionel SambucLua manages memory automatically by running 682*0a6a1f1dSLionel Sambuca <em>garbage collector</em> to collect all <em>dead objects</em> 683*0a6a1f1dSLionel Sambuc(that is, objects that are no longer accessible from Lua). 684*0a6a1f1dSLionel SambucAll memory used by Lua is subject to automatic management: 685*0a6a1f1dSLionel Sambucstrings, tables, userdata, functions, threads, internal structures, etc. 686*0a6a1f1dSLionel Sambuc 687*0a6a1f1dSLionel Sambuc 688*0a6a1f1dSLionel Sambuc<p> 689*0a6a1f1dSLionel SambucLua implements an incremental mark-and-sweep collector. 690*0a6a1f1dSLionel SambucIt uses two numbers to control its garbage-collection cycles: 691*0a6a1f1dSLionel Sambucthe <em>garbage-collector pause</em> and 692*0a6a1f1dSLionel Sambucthe <em>garbage-collector step multiplier</em>. 693*0a6a1f1dSLionel SambucBoth use percentage points as units 694*0a6a1f1dSLionel Sambuc(e.g., a value of 100 means an internal value of 1). 695*0a6a1f1dSLionel Sambuc 696*0a6a1f1dSLionel Sambuc 697*0a6a1f1dSLionel Sambuc<p> 698*0a6a1f1dSLionel SambucThe garbage-collector pause 699*0a6a1f1dSLionel Sambuccontrols how long the collector waits before starting a new cycle. 700*0a6a1f1dSLionel SambucLarger values make the collector less aggressive. 701*0a6a1f1dSLionel SambucValues smaller than 100 mean the collector will not wait to 702*0a6a1f1dSLionel Sambucstart a new cycle. 703*0a6a1f1dSLionel SambucA value of 200 means that the collector waits for the total memory in use 704*0a6a1f1dSLionel Sambucto double before starting a new cycle. 705*0a6a1f1dSLionel Sambuc 706*0a6a1f1dSLionel Sambuc 707*0a6a1f1dSLionel Sambuc<p> 708*0a6a1f1dSLionel SambucThe garbage-collector step multiplier 709*0a6a1f1dSLionel Sambuccontrols the relative speed of the collector relative to 710*0a6a1f1dSLionel Sambucmemory allocation. 711*0a6a1f1dSLionel SambucLarger values make the collector more aggressive but also increase 712*0a6a1f1dSLionel Sambucthe size of each incremental step. 713*0a6a1f1dSLionel SambucYou should not use values smaller than 100, 714*0a6a1f1dSLionel Sambucbecause they make the collector too slow and 715*0a6a1f1dSLionel Sambuccan result in the collector never finishing a cycle. 716*0a6a1f1dSLionel SambucThe default is 200, 717*0a6a1f1dSLionel Sambucwhich means that the collector runs at "twice" 718*0a6a1f1dSLionel Sambucthe speed of memory allocation. 719*0a6a1f1dSLionel Sambuc 720*0a6a1f1dSLionel Sambuc 721*0a6a1f1dSLionel Sambuc<p> 722*0a6a1f1dSLionel SambucIf you set the step multiplier to a very large number 723*0a6a1f1dSLionel Sambuc(larger than 10% of the maximum number of 724*0a6a1f1dSLionel Sambucbytes that the program may use), 725*0a6a1f1dSLionel Sambucthe collector behaves like a stop-the-world collector. 726*0a6a1f1dSLionel SambucIf you then set the pause to 200, 727*0a6a1f1dSLionel Sambucthe collector behaves as in old Lua versions, 728*0a6a1f1dSLionel Sambucdoing a complete collection every time Lua doubles its 729*0a6a1f1dSLionel Sambucmemory usage. 730*0a6a1f1dSLionel Sambuc 731*0a6a1f1dSLionel Sambuc 732*0a6a1f1dSLionel Sambuc<p> 733*0a6a1f1dSLionel SambucYou can change these numbers by calling <a href="#lua_gc"><code>lua_gc</code></a> in C 734*0a6a1f1dSLionel Sambucor <a href="#pdf-collectgarbage"><code>collectgarbage</code></a> in Lua. 735*0a6a1f1dSLionel SambucYou can also use these functions to control 736*0a6a1f1dSLionel Sambucthe collector directly (e.g., stop and restart it). 737*0a6a1f1dSLionel Sambuc 738*0a6a1f1dSLionel Sambuc 739*0a6a1f1dSLionel Sambuc 740*0a6a1f1dSLionel Sambuc<h3>2.5.1 – <a name="2.5.1">Garbage-Collection Metamethods</a></h3> 741*0a6a1f1dSLionel Sambuc 742*0a6a1f1dSLionel Sambuc<p> 743*0a6a1f1dSLionel SambucYou can set garbage-collector metamethods for tables 744*0a6a1f1dSLionel Sambucand, using the C API, 745*0a6a1f1dSLionel Sambucfor full userdata (see <a href="#2.4">§2.4</a>). 746*0a6a1f1dSLionel SambucThese metamethods are also called <em>finalizers</em>. 747*0a6a1f1dSLionel SambucFinalizers allow you to coordinate Lua's garbage collection 748*0a6a1f1dSLionel Sambucwith external resource management 749*0a6a1f1dSLionel Sambuc(such as closing files, network or database connections, 750*0a6a1f1dSLionel Sambucor freeing your own memory). 751*0a6a1f1dSLionel Sambuc 752*0a6a1f1dSLionel Sambuc 753*0a6a1f1dSLionel Sambuc<p> 754*0a6a1f1dSLionel SambucFor an object (table or userdata) to be finalized when collected, 755*0a6a1f1dSLionel Sambucyou must <em>mark</em> it for finalization. 756*0a6a1f1dSLionel Sambuc 757*0a6a1f1dSLionel SambucYou mark an object for finalization when you set its metatable 758*0a6a1f1dSLionel Sambucand the metatable has a field indexed by the string "<code>__gc</code>". 759*0a6a1f1dSLionel SambucNote that if you set a metatable without a <code>__gc</code> field 760*0a6a1f1dSLionel Sambucand later create that field in the metatable, 761*0a6a1f1dSLionel Sambucthe object will not be marked for finalization. 762*0a6a1f1dSLionel Sambuc 763*0a6a1f1dSLionel Sambuc 764*0a6a1f1dSLionel Sambuc<p> 765*0a6a1f1dSLionel SambucWhen a marked object becomes garbage, 766*0a6a1f1dSLionel Sambucit is not collected immediately by the garbage collector. 767*0a6a1f1dSLionel SambucInstead, Lua puts it in a list. 768*0a6a1f1dSLionel SambucAfter the collection, 769*0a6a1f1dSLionel SambucLua goes through that list. 770*0a6a1f1dSLionel SambucFor each object in the list, 771*0a6a1f1dSLionel Sambucit checks the object's <code>__gc</code> metamethod: 772*0a6a1f1dSLionel SambucIf it is a function, 773*0a6a1f1dSLionel SambucLua calls it with the object as its single argument; 774*0a6a1f1dSLionel Sambucif the metamethod is not a function, 775*0a6a1f1dSLionel SambucLua simply ignores it. 776*0a6a1f1dSLionel Sambuc 777*0a6a1f1dSLionel Sambuc 778*0a6a1f1dSLionel Sambuc<p> 779*0a6a1f1dSLionel SambucAt the end of each garbage-collection cycle, 780*0a6a1f1dSLionel Sambucthe finalizers for objects are called in 781*0a6a1f1dSLionel Sambucthe reverse order that the objects were marked for finalization, 782*0a6a1f1dSLionel Sambucamong those collected in that cycle; 783*0a6a1f1dSLionel Sambucthat is, the first finalizer to be called is the one associated 784*0a6a1f1dSLionel Sambucwith the object marked last in the program. 785*0a6a1f1dSLionel SambucThe execution of each finalizer may occur at any point during 786*0a6a1f1dSLionel Sambucthe execution of the regular code. 787*0a6a1f1dSLionel Sambuc 788*0a6a1f1dSLionel Sambuc 789*0a6a1f1dSLionel Sambuc<p> 790*0a6a1f1dSLionel SambucBecause the object being collected must still be used by the finalizer, 791*0a6a1f1dSLionel Sambucthat object (and other objects accessible only through it) 792*0a6a1f1dSLionel Sambucmust be <em>resurrected</em> by Lua. 793*0a6a1f1dSLionel SambucUsually, this resurrection is transient, 794*0a6a1f1dSLionel Sambucand the object memory is freed in the next garbage-collection cycle. 795*0a6a1f1dSLionel SambucHowever, if the finalizer stores the object in some global place 796*0a6a1f1dSLionel Sambuc(e.g., a global variable), 797*0a6a1f1dSLionel Sambucthen the resurrection is permanent. 798*0a6a1f1dSLionel SambucMoreover, if the finalizer marks a finalizing object for finalization again, 799*0a6a1f1dSLionel Sambucits finalizer will be called again in the next cycle where the 800*0a6a1f1dSLionel Sambucobject is unreachable. 801*0a6a1f1dSLionel SambucIn any case, 802*0a6a1f1dSLionel Sambucthe object memory is freed only in a GC cycle where 803*0a6a1f1dSLionel Sambucthe object is unreachable and not marked for finalization. 804*0a6a1f1dSLionel Sambuc 805*0a6a1f1dSLionel Sambuc 806*0a6a1f1dSLionel Sambuc<p> 807*0a6a1f1dSLionel SambucWhen you close a state (see <a href="#lua_close"><code>lua_close</code></a>), 808*0a6a1f1dSLionel SambucLua calls the finalizers of all objects marked for finalization, 809*0a6a1f1dSLionel Sambucfollowing the reverse order that they were marked. 810*0a6a1f1dSLionel SambucIf any finalizer marks objects for collection during that phase, 811*0a6a1f1dSLionel Sambucthese marks have no effect. 812*0a6a1f1dSLionel Sambuc 813*0a6a1f1dSLionel Sambuc 814*0a6a1f1dSLionel Sambuc 815*0a6a1f1dSLionel Sambuc 816*0a6a1f1dSLionel Sambuc 817*0a6a1f1dSLionel Sambuc<h3>2.5.2 – <a name="2.5.2">Weak Tables</a></h3> 818*0a6a1f1dSLionel Sambuc 819*0a6a1f1dSLionel Sambuc<p> 820*0a6a1f1dSLionel SambucA <em>weak table</em> is a table whose elements are 821*0a6a1f1dSLionel Sambuc<em>weak references</em>. 822*0a6a1f1dSLionel SambucA weak reference is ignored by the garbage collector. 823*0a6a1f1dSLionel SambucIn other words, 824*0a6a1f1dSLionel Sambucif the only references to an object are weak references, 825*0a6a1f1dSLionel Sambucthen the garbage collector will collect that object. 826*0a6a1f1dSLionel Sambuc 827*0a6a1f1dSLionel Sambuc 828*0a6a1f1dSLionel Sambuc<p> 829*0a6a1f1dSLionel SambucA weak table can have weak keys, weak values, or both. 830*0a6a1f1dSLionel SambucA table with weak values allows the collection of its values, 831*0a6a1f1dSLionel Sambucbut prevents the collection of its keys. 832*0a6a1f1dSLionel SambucA table with both weak keys and weak values allows the collection of 833*0a6a1f1dSLionel Sambucboth keys and values. 834*0a6a1f1dSLionel SambucIn any case, if either the key or the value is collected, 835*0a6a1f1dSLionel Sambucthe whole pair is removed from the table. 836*0a6a1f1dSLionel SambucThe weakness of a table is controlled by the 837*0a6a1f1dSLionel Sambuc<code>__mode</code> field of its metatable. 838*0a6a1f1dSLionel SambucIf the <code>__mode</code> field is a string containing the character '<code>k</code>', 839*0a6a1f1dSLionel Sambucthe keys in the table are weak. 840*0a6a1f1dSLionel SambucIf <code>__mode</code> contains '<code>v</code>', 841*0a6a1f1dSLionel Sambucthe values in the table are weak. 842*0a6a1f1dSLionel Sambuc 843*0a6a1f1dSLionel Sambuc 844*0a6a1f1dSLionel Sambuc<p> 845*0a6a1f1dSLionel SambucA table with weak keys and strong values 846*0a6a1f1dSLionel Sambucis also called an <em>ephemeron table</em>. 847*0a6a1f1dSLionel SambucIn an ephemeron table, 848*0a6a1f1dSLionel Sambuca value is considered reachable only if its key is reachable. 849*0a6a1f1dSLionel SambucIn particular, 850*0a6a1f1dSLionel Sambucif the only reference to a key comes through its value, 851*0a6a1f1dSLionel Sambucthe pair is removed. 852*0a6a1f1dSLionel Sambuc 853*0a6a1f1dSLionel Sambuc 854*0a6a1f1dSLionel Sambuc<p> 855*0a6a1f1dSLionel SambucAny change in the weakness of a table may take effect only 856*0a6a1f1dSLionel Sambucat the next collect cycle. 857*0a6a1f1dSLionel SambucIn particular, if you change the weakness to a stronger mode, 858*0a6a1f1dSLionel SambucLua may still collect some items from that table 859*0a6a1f1dSLionel Sambucbefore the change takes effect. 860*0a6a1f1dSLionel Sambuc 861*0a6a1f1dSLionel Sambuc 862*0a6a1f1dSLionel Sambuc<p> 863*0a6a1f1dSLionel SambucOnly objects that have an explicit construction 864*0a6a1f1dSLionel Sambucare removed from weak tables. 865*0a6a1f1dSLionel SambucValues, such as numbers and light C functions, 866*0a6a1f1dSLionel Sambucare not subject to garbage collection, 867*0a6a1f1dSLionel Sambucand therefore are not removed from weak tables 868*0a6a1f1dSLionel Sambuc(unless their associated values are collected). 869*0a6a1f1dSLionel SambucAlthough strings are subject to garbage collection, 870*0a6a1f1dSLionel Sambucthey do not have an explicit construction, 871*0a6a1f1dSLionel Sambucand therefore are not removed from weak tables. 872*0a6a1f1dSLionel Sambuc 873*0a6a1f1dSLionel Sambuc 874*0a6a1f1dSLionel Sambuc<p> 875*0a6a1f1dSLionel SambucResurrected objects 876*0a6a1f1dSLionel Sambuc(that is, objects being finalized 877*0a6a1f1dSLionel Sambucand objects accessible only through objects being finalized) 878*0a6a1f1dSLionel Sambuchave a special behavior in weak tables. 879*0a6a1f1dSLionel SambucThey are removed from weak values before running their finalizers, 880*0a6a1f1dSLionel Sambucbut are removed from weak keys only in the next collection 881*0a6a1f1dSLionel Sambucafter running their finalizers, when such objects are actually freed. 882*0a6a1f1dSLionel SambucThis behavior allows the finalizer to access properties 883*0a6a1f1dSLionel Sambucassociated with the object through weak tables. 884*0a6a1f1dSLionel Sambuc 885*0a6a1f1dSLionel Sambuc 886*0a6a1f1dSLionel Sambuc<p> 887*0a6a1f1dSLionel SambucIf a weak table is among the resurrected objects in a collection cycle, 888*0a6a1f1dSLionel Sambucit may not be properly cleared until the next cycle. 889*0a6a1f1dSLionel Sambuc 890*0a6a1f1dSLionel Sambuc 891*0a6a1f1dSLionel Sambuc 892*0a6a1f1dSLionel Sambuc 893*0a6a1f1dSLionel Sambuc 894*0a6a1f1dSLionel Sambuc 895*0a6a1f1dSLionel Sambuc 896*0a6a1f1dSLionel Sambuc<h2>2.6 – <a name="2.6">Coroutines</a></h2> 897*0a6a1f1dSLionel Sambuc 898*0a6a1f1dSLionel Sambuc<p> 899*0a6a1f1dSLionel SambucLua supports coroutines, 900*0a6a1f1dSLionel Sambucalso called <em>collaborative multithreading</em>. 901*0a6a1f1dSLionel SambucA coroutine in Lua represents an independent thread of execution. 902*0a6a1f1dSLionel SambucUnlike threads in multithread systems, however, 903*0a6a1f1dSLionel Sambuca coroutine only suspends its execution by explicitly calling 904*0a6a1f1dSLionel Sambuca yield function. 905*0a6a1f1dSLionel Sambuc 906*0a6a1f1dSLionel Sambuc 907*0a6a1f1dSLionel Sambuc<p> 908*0a6a1f1dSLionel SambucYou create a coroutine by calling <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>. 909*0a6a1f1dSLionel SambucIts sole argument is a function 910*0a6a1f1dSLionel Sambucthat is the main function of the coroutine. 911*0a6a1f1dSLionel SambucThe <code>create</code> function only creates a new coroutine and 912*0a6a1f1dSLionel Sambucreturns a handle to it (an object of type <em>thread</em>); 913*0a6a1f1dSLionel Sambucit does not start the coroutine. 914*0a6a1f1dSLionel Sambuc 915*0a6a1f1dSLionel Sambuc 916*0a6a1f1dSLionel Sambuc<p> 917*0a6a1f1dSLionel SambucYou execute a coroutine by calling <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>. 918*0a6a1f1dSLionel SambucWhen you first call <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>, 919*0a6a1f1dSLionel Sambucpassing as its first argument 920*0a6a1f1dSLionel Sambuca thread returned by <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>, 921*0a6a1f1dSLionel Sambucthe coroutine starts its execution by 922*0a6a1f1dSLionel Sambuccalling its main function. 923*0a6a1f1dSLionel SambucExtra arguments passed to <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> are passed 924*0a6a1f1dSLionel Sambucas arguments to that function. 925*0a6a1f1dSLionel SambucAfter the coroutine starts running, 926*0a6a1f1dSLionel Sambucit runs until it terminates or <em>yields</em>. 927*0a6a1f1dSLionel Sambuc 928*0a6a1f1dSLionel Sambuc 929*0a6a1f1dSLionel Sambuc<p> 930*0a6a1f1dSLionel SambucA coroutine can terminate its execution in two ways: 931*0a6a1f1dSLionel Sambucnormally, when its main function returns 932*0a6a1f1dSLionel Sambuc(explicitly or implicitly, after the last instruction); 933*0a6a1f1dSLionel Sambucand abnormally, if there is an unprotected error. 934*0a6a1f1dSLionel SambucIn case of normal termination, 935*0a6a1f1dSLionel Sambuc<a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> returns <b>true</b>, 936*0a6a1f1dSLionel Sambucplus any values returned by the coroutine main function. 937*0a6a1f1dSLionel SambucIn case of errors, <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> returns <b>false</b> 938*0a6a1f1dSLionel Sambucplus an error message. 939*0a6a1f1dSLionel Sambuc 940*0a6a1f1dSLionel Sambuc 941*0a6a1f1dSLionel Sambuc<p> 942*0a6a1f1dSLionel SambucA coroutine yields by calling <a href="#pdf-coroutine.yield"><code>coroutine.yield</code></a>. 943*0a6a1f1dSLionel SambucWhen a coroutine yields, 944*0a6a1f1dSLionel Sambucthe corresponding <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> returns immediately, 945*0a6a1f1dSLionel Sambuceven if the yield happens inside nested function calls 946*0a6a1f1dSLionel Sambuc(that is, not in the main function, 947*0a6a1f1dSLionel Sambucbut in a function directly or indirectly called by the main function). 948*0a6a1f1dSLionel SambucIn the case of a yield, <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> also returns <b>true</b>, 949*0a6a1f1dSLionel Sambucplus any values passed to <a href="#pdf-coroutine.yield"><code>coroutine.yield</code></a>. 950*0a6a1f1dSLionel SambucThe next time you resume the same coroutine, 951*0a6a1f1dSLionel Sambucit continues its execution from the point where it yielded, 952*0a6a1f1dSLionel Sambucwith the call to <a href="#pdf-coroutine.yield"><code>coroutine.yield</code></a> returning any extra 953*0a6a1f1dSLionel Sambucarguments passed to <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>. 954*0a6a1f1dSLionel Sambuc 955*0a6a1f1dSLionel Sambuc 956*0a6a1f1dSLionel Sambuc<p> 957*0a6a1f1dSLionel SambucLike <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>, 958*0a6a1f1dSLionel Sambucthe <a href="#pdf-coroutine.wrap"><code>coroutine.wrap</code></a> function also creates a coroutine, 959*0a6a1f1dSLionel Sambucbut instead of returning the coroutine itself, 960*0a6a1f1dSLionel Sambucit returns a function that, when called, resumes the coroutine. 961*0a6a1f1dSLionel SambucAny arguments passed to this function 962*0a6a1f1dSLionel Sambucgo as extra arguments to <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>. 963*0a6a1f1dSLionel Sambuc<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>, 964*0a6a1f1dSLionel Sambucexcept the first one (the boolean error code). 965*0a6a1f1dSLionel SambucUnlike <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>, 966*0a6a1f1dSLionel Sambuc<a href="#pdf-coroutine.wrap"><code>coroutine.wrap</code></a> does not catch errors; 967*0a6a1f1dSLionel Sambucany error is propagated to the caller. 968*0a6a1f1dSLionel Sambuc 969*0a6a1f1dSLionel Sambuc 970*0a6a1f1dSLionel Sambuc<p> 971*0a6a1f1dSLionel SambucAs an example of how coroutines work, 972*0a6a1f1dSLionel Sambucconsider the following code: 973*0a6a1f1dSLionel Sambuc 974*0a6a1f1dSLionel Sambuc<pre> 975*0a6a1f1dSLionel Sambuc function foo (a) 976*0a6a1f1dSLionel Sambuc print("foo", a) 977*0a6a1f1dSLionel Sambuc return coroutine.yield(2*a) 978*0a6a1f1dSLionel Sambuc end 979*0a6a1f1dSLionel Sambuc 980*0a6a1f1dSLionel Sambuc co = coroutine.create(function (a,b) 981*0a6a1f1dSLionel Sambuc print("co-body", a, b) 982*0a6a1f1dSLionel Sambuc local r = foo(a+1) 983*0a6a1f1dSLionel Sambuc print("co-body", r) 984*0a6a1f1dSLionel Sambuc local r, s = coroutine.yield(a+b, a-b) 985*0a6a1f1dSLionel Sambuc print("co-body", r, s) 986*0a6a1f1dSLionel Sambuc return b, "end" 987*0a6a1f1dSLionel Sambuc end) 988*0a6a1f1dSLionel Sambuc 989*0a6a1f1dSLionel Sambuc print("main", coroutine.resume(co, 1, 10)) 990*0a6a1f1dSLionel Sambuc print("main", coroutine.resume(co, "r")) 991*0a6a1f1dSLionel Sambuc print("main", coroutine.resume(co, "x", "y")) 992*0a6a1f1dSLionel Sambuc print("main", coroutine.resume(co, "x", "y")) 993*0a6a1f1dSLionel Sambuc</pre><p> 994*0a6a1f1dSLionel SambucWhen you run it, it produces the following output: 995*0a6a1f1dSLionel Sambuc 996*0a6a1f1dSLionel Sambuc<pre> 997*0a6a1f1dSLionel Sambuc co-body 1 10 998*0a6a1f1dSLionel Sambuc foo 2 999*0a6a1f1dSLionel Sambuc main true 4 1000*0a6a1f1dSLionel Sambuc co-body r 1001*0a6a1f1dSLionel Sambuc main true 11 -9 1002*0a6a1f1dSLionel Sambuc co-body x y 1003*0a6a1f1dSLionel Sambuc main true 10 end 1004*0a6a1f1dSLionel Sambuc main false cannot resume dead coroutine 1005*0a6a1f1dSLionel Sambuc</pre> 1006*0a6a1f1dSLionel Sambuc 1007*0a6a1f1dSLionel Sambuc<p> 1008*0a6a1f1dSLionel SambucYou can also create and manipulate coroutines through the C API: 1009*0a6a1f1dSLionel Sambucsee functions <a href="#lua_newthread"><code>lua_newthread</code></a>, <a href="#lua_resume"><code>lua_resume</code></a>, 1010*0a6a1f1dSLionel Sambucand <a href="#lua_yield"><code>lua_yield</code></a>. 1011*0a6a1f1dSLionel Sambuc 1012*0a6a1f1dSLionel Sambuc 1013*0a6a1f1dSLionel Sambuc 1014*0a6a1f1dSLionel Sambuc 1015*0a6a1f1dSLionel Sambuc 1016*0a6a1f1dSLionel Sambuc<h1>3 – <a name="3">The Language</a></h1> 101711be35a1SLionel Sambuc 101811be35a1SLionel Sambuc<p> 101911be35a1SLionel SambucThis section describes the lexis, the syntax, and the semantics of Lua. 102011be35a1SLionel SambucIn other words, 102111be35a1SLionel Sambucthis section describes 102211be35a1SLionel Sambucwhich tokens are valid, 102311be35a1SLionel Sambuchow they can be combined, 102411be35a1SLionel Sambucand what their combinations mean. 102511be35a1SLionel Sambuc 102611be35a1SLionel Sambuc 102711be35a1SLionel Sambuc<p> 1028*0a6a1f1dSLionel SambucLanguage constructs will be explained using the usual extended BNF notation, 102911be35a1SLionel Sambucin which 103011be35a1SLionel Sambuc{<em>a</em>} means 0 or more <em>a</em>'s, and 103111be35a1SLionel Sambuc[<em>a</em>] means an optional <em>a</em>. 103211be35a1SLionel SambucNon-terminals are shown like non-terminal, 103311be35a1SLionel Sambuckeywords are shown like <b>kword</b>, 1034*0a6a1f1dSLionel Sambucand other terminal symbols are shown like ‘<b>=</b>’. 1035*0a6a1f1dSLionel SambucThe complete syntax of Lua can be found in <a href="#9">§9</a> 103611be35a1SLionel Sambucat the end of this manual. 103711be35a1SLionel Sambuc 103811be35a1SLionel Sambuc 103911be35a1SLionel Sambuc 1040*0a6a1f1dSLionel Sambuc<h2>3.1 – <a name="3.1">Lexical Conventions</a></h2> 1041*0a6a1f1dSLionel Sambuc 1042*0a6a1f1dSLionel Sambuc<p> 1043*0a6a1f1dSLionel SambucLua is a free-form language. 1044*0a6a1f1dSLionel SambucIt ignores spaces (including new lines) and comments 1045*0a6a1f1dSLionel Sambucbetween lexical elements (tokens), 1046*0a6a1f1dSLionel Sambucexcept as delimiters between names and keywords. 1047*0a6a1f1dSLionel Sambuc 104811be35a1SLionel Sambuc 104911be35a1SLionel Sambuc<p> 105011be35a1SLionel Sambuc<em>Names</em> 105111be35a1SLionel Sambuc(also called <em>identifiers</em>) 105211be35a1SLionel Sambucin Lua can be any string of letters, 105311be35a1SLionel Sambucdigits, and underscores, 105411be35a1SLionel Sambucnot beginning with a digit. 1055*0a6a1f1dSLionel SambucIdentifiers are used to name variables, table fields, and labels. 105611be35a1SLionel Sambuc 105711be35a1SLionel Sambuc 105811be35a1SLionel Sambuc<p> 105911be35a1SLionel SambucThe following <em>keywords</em> are reserved 106011be35a1SLionel Sambucand cannot be used as names: 106111be35a1SLionel Sambuc 106211be35a1SLionel Sambuc 106311be35a1SLionel Sambuc<pre> 1064*0a6a1f1dSLionel Sambuc and break do else elseif end 1065*0a6a1f1dSLionel Sambuc false for function goto if in 1066*0a6a1f1dSLionel Sambuc local nil not or repeat return 1067*0a6a1f1dSLionel Sambuc then true until while 106811be35a1SLionel Sambuc</pre> 106911be35a1SLionel Sambuc 107011be35a1SLionel Sambuc<p> 107111be35a1SLionel SambucLua is a case-sensitive language: 107211be35a1SLionel Sambuc<code>and</code> is a reserved word, but <code>And</code> and <code>AND</code> 107311be35a1SLionel Sambucare two different, valid names. 1074*0a6a1f1dSLionel SambucAs a convention, 1075*0a6a1f1dSLionel Sambucprograms should avoid creating 1076*0a6a1f1dSLionel Sambucnames that start with an underscore followed by 1077*0a6a1f1dSLionel Sambucone or more uppercase letters (such as <a href="#pdf-_VERSION"><code>_VERSION</code></a>). 107811be35a1SLionel Sambuc 107911be35a1SLionel Sambuc 108011be35a1SLionel Sambuc<p> 108111be35a1SLionel SambucThe following strings denote other tokens: 108211be35a1SLionel Sambuc 108311be35a1SLionel Sambuc<pre> 108411be35a1SLionel Sambuc + - * / % ^ # 1085*0a6a1f1dSLionel Sambuc & ~ | << >> // 108611be35a1SLionel Sambuc == ~= <= >= < > = 1087*0a6a1f1dSLionel Sambuc ( ) { } [ ] :: 108811be35a1SLionel Sambuc ; : , . .. ... 108911be35a1SLionel Sambuc</pre> 109011be35a1SLionel Sambuc 109111be35a1SLionel Sambuc<p> 109211be35a1SLionel Sambuc<em>Literal strings</em> 109311be35a1SLionel Sambuccan be delimited by matching single or double quotes, 109411be35a1SLionel Sambucand can contain the following C-like escape sequences: 109511be35a1SLionel Sambuc'<code>\a</code>' (bell), 109611be35a1SLionel Sambuc'<code>\b</code>' (backspace), 109711be35a1SLionel Sambuc'<code>\f</code>' (form feed), 109811be35a1SLionel Sambuc'<code>\n</code>' (newline), 109911be35a1SLionel Sambuc'<code>\r</code>' (carriage return), 110011be35a1SLionel Sambuc'<code>\t</code>' (horizontal tab), 110111be35a1SLionel Sambuc'<code>\v</code>' (vertical tab), 110211be35a1SLionel Sambuc'<code>\\</code>' (backslash), 110311be35a1SLionel Sambuc'<code>\"</code>' (quotation mark [double quote]), 110411be35a1SLionel Sambucand '<code>\'</code>' (apostrophe [single quote]). 1105*0a6a1f1dSLionel SambucA backslash followed by a real newline 110611be35a1SLionel Sambucresults in a newline in the string. 1107*0a6a1f1dSLionel SambucThe escape sequence '<code>\z</code>' skips the following span 1108*0a6a1f1dSLionel Sambucof white-space characters, 1109*0a6a1f1dSLionel Sambucincluding line breaks; 1110*0a6a1f1dSLionel Sambucit is particularly useful to break and indent a long literal string 1111*0a6a1f1dSLionel Sambucinto multiple lines without adding the newlines and spaces 1112*0a6a1f1dSLionel Sambucinto the string contents. 1113*0a6a1f1dSLionel Sambuc 1114*0a6a1f1dSLionel Sambuc 1115*0a6a1f1dSLionel Sambuc<p> 111611be35a1SLionel SambucStrings in Lua can contain any 8-bit value, including embedded zeros, 111711be35a1SLionel Sambucwhich can be specified as '<code>\0</code>'. 1118*0a6a1f1dSLionel SambucMore generally, 1119*0a6a1f1dSLionel Sambucwe can specify any byte in a literal string by its numeric value. 1120*0a6a1f1dSLionel SambucThis can be done 1121*0a6a1f1dSLionel Sambucwith the escape sequence <code>\x<em>XX</em></code>, 1122*0a6a1f1dSLionel Sambucwhere <em>XX</em> is a sequence of exactly two hexadecimal digits, 1123*0a6a1f1dSLionel Sambucor with the escape sequence <code>\<em>ddd</em></code>, 1124*0a6a1f1dSLionel Sambucwhere <em>ddd</em> is a sequence of up to three decimal digits. 1125*0a6a1f1dSLionel Sambuc(Note that if a decimal escape sequence is to be followed by a digit, 1126*0a6a1f1dSLionel Sambucit must be expressed using exactly three digits.) 1127*0a6a1f1dSLionel Sambuc 1128*0a6a1f1dSLionel Sambuc 1129*0a6a1f1dSLionel Sambuc<p> 1130*0a6a1f1dSLionel SambucThe UTF-8 encoding of a Unicode character 1131*0a6a1f1dSLionel Sambuccan be inserted in a literal string with 1132*0a6a1f1dSLionel Sambucthe escape sequence <code>\u{<em>XXX</em>}</code> 1133*0a6a1f1dSLionel Sambuc(note the mandatory enclosing brackets), 1134*0a6a1f1dSLionel Sambucwhere <em>XXX</em> is a sequence of one or more hexadecimal digits 1135*0a6a1f1dSLionel Sambucrepresenting the character code point. 113611be35a1SLionel Sambuc 113711be35a1SLionel Sambuc 113811be35a1SLionel Sambuc<p> 113911be35a1SLionel SambucLiteral strings can also be defined using a long format 114011be35a1SLionel Sambucenclosed by <em>long brackets</em>. 114111be35a1SLionel SambucWe define an <em>opening long bracket of level <em>n</em></em> as an opening 114211be35a1SLionel Sambucsquare bracket followed by <em>n</em> equal signs followed by another 114311be35a1SLionel Sambucopening square bracket. 114411be35a1SLionel SambucSo, an opening long bracket of level 0 is written as <code>[[</code>, 114511be35a1SLionel Sambucan opening long bracket of level 1 is written as <code>[=[</code>, 114611be35a1SLionel Sambucand so on. 114711be35a1SLionel SambucA <em>closing long bracket</em> is defined similarly; 1148*0a6a1f1dSLionel Sambucfor instance, 1149*0a6a1f1dSLionel Sambuca closing long bracket of level 4 is written as <code>]====]</code>. 1150*0a6a1f1dSLionel SambucA <em>long literal</em> starts with an opening long bracket of any level and 115111be35a1SLionel Sambucends at the first closing long bracket of the same level. 1152*0a6a1f1dSLionel SambucIt can contain any text except a closing bracket of the same level. 115311be35a1SLionel SambucLiterals in this bracketed form can run for several lines, 115411be35a1SLionel Sambucdo not interpret any escape sequences, 115511be35a1SLionel Sambucand ignore long brackets of any other level. 1156*0a6a1f1dSLionel SambucAny kind of end-of-line sequence 1157*0a6a1f1dSLionel Sambuc(carriage return, newline, carriage return followed by newline, 1158*0a6a1f1dSLionel Sambucor newline followed by carriage return) 1159*0a6a1f1dSLionel Sambucis converted to a simple newline. 1160*0a6a1f1dSLionel Sambuc 1161*0a6a1f1dSLionel Sambuc 1162*0a6a1f1dSLionel Sambuc<p> 1163*0a6a1f1dSLionel SambucAny byte in a literal string not 1164*0a6a1f1dSLionel Sambucexplicitly affected by the previous rules represents itself. 1165*0a6a1f1dSLionel SambucHowever, Lua opens files for parsing in text mode, 1166*0a6a1f1dSLionel Sambucand the system file functions may have problems with 1167*0a6a1f1dSLionel Sambucsome control characters. 1168*0a6a1f1dSLionel SambucSo, it is safer to represent 1169*0a6a1f1dSLionel Sambucnon-text data as a quoted literal with 1170*0a6a1f1dSLionel Sambucexplicit escape sequences for non-text characters. 117111be35a1SLionel Sambuc 117211be35a1SLionel Sambuc 117311be35a1SLionel Sambuc<p> 117411be35a1SLionel SambucFor convenience, 117511be35a1SLionel Sambucwhen the opening long bracket is immediately followed by a newline, 117611be35a1SLionel Sambucthe newline is not included in the string. 117711be35a1SLionel SambucAs an example, in a system using ASCII 117811be35a1SLionel Sambuc(in which '<code>a</code>' is coded as 97, 117911be35a1SLionel Sambucnewline is coded as 10, and '<code>1</code>' is coded as 49), 118011be35a1SLionel Sambucthe five literal strings below denote the same string: 118111be35a1SLionel Sambuc 118211be35a1SLionel Sambuc<pre> 118311be35a1SLionel Sambuc a = 'alo\n123"' 118411be35a1SLionel Sambuc a = "alo\n123\"" 118511be35a1SLionel Sambuc a = '\97lo\10\04923"' 118611be35a1SLionel Sambuc a = [[alo 118711be35a1SLionel Sambuc 123"]] 118811be35a1SLionel Sambuc a = [==[ 118911be35a1SLionel Sambuc alo 119011be35a1SLionel Sambuc 123"]==] 119111be35a1SLionel Sambuc</pre> 119211be35a1SLionel Sambuc 119311be35a1SLionel Sambuc<p> 1194*0a6a1f1dSLionel SambucA <em>numeric constant</em> (or <em>numeral</em>) 1195*0a6a1f1dSLionel Sambuccan be written with an optional fractional part 1196*0a6a1f1dSLionel Sambucand an optional decimal exponent, 1197*0a6a1f1dSLionel Sambucmarked by a letter '<code>e</code>' or '<code>E</code>'. 1198*0a6a1f1dSLionel SambucLua also accepts hexadecimal constants, 1199*0a6a1f1dSLionel Sambucwhich start with <code>0x</code> or <code>0X</code>. 1200*0a6a1f1dSLionel SambucHexadecimal constants also accept an optional fractional part 1201*0a6a1f1dSLionel Sambucplus an optional binary exponent, 1202*0a6a1f1dSLionel Sambucmarked by a letter '<code>p</code>' or '<code>P</code>'. 1203*0a6a1f1dSLionel SambucA numeric constant with a fractional dot or an exponent 1204*0a6a1f1dSLionel Sambucdenotes a float; 1205*0a6a1f1dSLionel Sambucotherwise it denotes an integer. 1206*0a6a1f1dSLionel SambucExamples of valid integer constants are 120711be35a1SLionel Sambuc 120811be35a1SLionel Sambuc<pre> 1209*0a6a1f1dSLionel Sambuc 3 345 0xff 0xBEBADA 1210*0a6a1f1dSLionel Sambuc</pre><p> 1211*0a6a1f1dSLionel SambucExamples of valid float constants are 1212*0a6a1f1dSLionel Sambuc 1213*0a6a1f1dSLionel Sambuc<pre> 1214*0a6a1f1dSLionel Sambuc 3.0 3.1416 314.16e-2 0.31416E1 34e1 1215*0a6a1f1dSLionel Sambuc 0x0.1E 0xA23p-4 0X1.921FB54442D18P+1 121611be35a1SLionel Sambuc</pre> 121711be35a1SLionel Sambuc 121811be35a1SLionel Sambuc<p> 121911be35a1SLionel SambucA <em>comment</em> starts with a double hyphen (<code>--</code>) 122011be35a1SLionel Sambucanywhere outside a string. 122111be35a1SLionel SambucIf the text immediately after <code>--</code> is not an opening long bracket, 122211be35a1SLionel Sambucthe comment is a <em>short comment</em>, 122311be35a1SLionel Sambucwhich runs until the end of the line. 122411be35a1SLionel SambucOtherwise, it is a <em>long comment</em>, 122511be35a1SLionel Sambucwhich runs until the corresponding closing long bracket. 122611be35a1SLionel SambucLong comments are frequently used to disable code temporarily. 122711be35a1SLionel Sambuc 122811be35a1SLionel Sambuc 122911be35a1SLionel Sambuc 123011be35a1SLionel Sambuc 123111be35a1SLionel Sambuc 1232*0a6a1f1dSLionel Sambuc<h2>3.2 – <a name="3.2">Variables</a></h2> 123311be35a1SLionel Sambuc 123411be35a1SLionel Sambuc<p> 123511be35a1SLionel SambucVariables are places that store values. 123611be35a1SLionel SambucThere are three kinds of variables in Lua: 123711be35a1SLionel Sambucglobal variables, local variables, and table fields. 123811be35a1SLionel Sambuc 123911be35a1SLionel Sambuc 124011be35a1SLionel Sambuc<p> 124111be35a1SLionel SambucA single name can denote a global variable or a local variable 124211be35a1SLionel Sambuc(or a function's formal parameter, 124311be35a1SLionel Sambucwhich is a particular kind of local variable): 124411be35a1SLionel Sambuc 124511be35a1SLionel Sambuc<pre> 124611be35a1SLionel Sambuc var ::= Name 124711be35a1SLionel Sambuc</pre><p> 1248*0a6a1f1dSLionel SambucName denotes identifiers, as defined in <a href="#3.1">§3.1</a>. 124911be35a1SLionel Sambuc 125011be35a1SLionel Sambuc 125111be35a1SLionel Sambuc<p> 1252*0a6a1f1dSLionel SambucAny variable name is assumed to be global unless explicitly declared 1253*0a6a1f1dSLionel Sambucas a local (see <a href="#3.3.7">§3.3.7</a>). 125411be35a1SLionel SambucLocal variables are <em>lexically scoped</em>: 125511be35a1SLionel Sambuclocal variables can be freely accessed by functions 1256*0a6a1f1dSLionel Sambucdefined inside their scope (see <a href="#3.5">§3.5</a>). 125711be35a1SLionel Sambuc 125811be35a1SLionel Sambuc 125911be35a1SLionel Sambuc<p> 126011be35a1SLionel SambucBefore the first assignment to a variable, its value is <b>nil</b>. 126111be35a1SLionel Sambuc 126211be35a1SLionel Sambuc 126311be35a1SLionel Sambuc<p> 126411be35a1SLionel SambucSquare brackets are used to index a table: 126511be35a1SLionel Sambuc 126611be35a1SLionel Sambuc<pre> 1267*0a6a1f1dSLionel Sambuc var ::= prefixexp ‘<b>[</b>’ exp ‘<b>]</b>’ 126811be35a1SLionel Sambuc</pre><p> 1269*0a6a1f1dSLionel SambucThe meaning of accesses to table fields can be changed via metatables. 127011be35a1SLionel SambucAn access to an indexed variable <code>t[i]</code> is equivalent to 127111be35a1SLionel Sambuca call <code>gettable_event(t,i)</code>. 1272*0a6a1f1dSLionel Sambuc(See <a href="#2.4">§2.4</a> for a complete description of the 127311be35a1SLionel Sambuc<code>gettable_event</code> function. 127411be35a1SLionel SambucThis function is not defined or callable in Lua. 127511be35a1SLionel SambucWe use it here only for explanatory purposes.) 127611be35a1SLionel Sambuc 127711be35a1SLionel Sambuc 127811be35a1SLionel Sambuc<p> 127911be35a1SLionel SambucThe syntax <code>var.Name</code> is just syntactic sugar for 128011be35a1SLionel Sambuc<code>var["Name"]</code>: 128111be35a1SLionel Sambuc 128211be35a1SLionel Sambuc<pre> 1283*0a6a1f1dSLionel Sambuc var ::= prefixexp ‘<b>.</b>’ Name 128411be35a1SLionel Sambuc</pre> 128511be35a1SLionel Sambuc 128611be35a1SLionel Sambuc<p> 128711be35a1SLionel SambucAn access to a global variable <code>x</code> 1288*0a6a1f1dSLionel Sambucis equivalent to <code>_ENV.x</code>. 1289*0a6a1f1dSLionel SambucDue to the way that chunks are compiled, 1290*0a6a1f1dSLionel Sambuc<code>_ENV</code> is never a global name (see <a href="#2.2">§2.2</a>). 129111be35a1SLionel Sambuc 129211be35a1SLionel Sambuc 129311be35a1SLionel Sambuc 129411be35a1SLionel Sambuc 129511be35a1SLionel Sambuc 1296*0a6a1f1dSLionel Sambuc<h2>3.3 – <a name="3.3">Statements</a></h2> 129711be35a1SLionel Sambuc 129811be35a1SLionel Sambuc<p> 129911be35a1SLionel SambucLua supports an almost conventional set of statements, 130011be35a1SLionel Sambucsimilar to those in Pascal or C. 130111be35a1SLionel SambucThis set includes 130211be35a1SLionel Sambucassignments, control structures, function calls, 130311be35a1SLionel Sambucand variable declarations. 130411be35a1SLionel Sambuc 130511be35a1SLionel Sambuc 130611be35a1SLionel Sambuc 1307*0a6a1f1dSLionel Sambuc<h3>3.3.1 – <a name="3.3.1">Blocks</a></h3> 130811be35a1SLionel Sambuc 130911be35a1SLionel Sambuc<p> 1310*0a6a1f1dSLionel SambucA block is a list of statements, 1311*0a6a1f1dSLionel Sambucwhich are executed sequentially: 131211be35a1SLionel Sambuc 131311be35a1SLionel Sambuc<pre> 1314*0a6a1f1dSLionel Sambuc block ::= {stat} 131511be35a1SLionel Sambuc</pre><p> 1316*0a6a1f1dSLionel SambucLua has <em>empty statements</em> 1317*0a6a1f1dSLionel Sambucthat allow you to separate statements with semicolons, 1318*0a6a1f1dSLionel Sambucstart a block with a semicolon 1319*0a6a1f1dSLionel Sambucor write two semicolons in sequence: 132011be35a1SLionel Sambuc 132111be35a1SLionel Sambuc<pre> 1322*0a6a1f1dSLionel Sambuc stat ::= ‘<b>;</b>’ 1323*0a6a1f1dSLionel Sambuc</pre> 1324*0a6a1f1dSLionel Sambuc 1325*0a6a1f1dSLionel Sambuc<p> 1326*0a6a1f1dSLionel SambucFunction calls and assignments 1327*0a6a1f1dSLionel Sambuccan start with an open parenthesis. 1328*0a6a1f1dSLionel SambucThis possibility leads to an ambiguity in Lua's grammar. 1329*0a6a1f1dSLionel SambucConsider the following fragment: 1330*0a6a1f1dSLionel Sambuc 1331*0a6a1f1dSLionel Sambuc<pre> 1332*0a6a1f1dSLionel Sambuc a = b + c 1333*0a6a1f1dSLionel Sambuc (print or io.write)('done') 1334*0a6a1f1dSLionel Sambuc</pre><p> 1335*0a6a1f1dSLionel SambucThe grammar could see it in two ways: 1336*0a6a1f1dSLionel Sambuc 1337*0a6a1f1dSLionel Sambuc<pre> 1338*0a6a1f1dSLionel Sambuc a = b + c(print or io.write)('done') 1339*0a6a1f1dSLionel Sambuc 1340*0a6a1f1dSLionel Sambuc a = b + c; (print or io.write)('done') 1341*0a6a1f1dSLionel Sambuc</pre><p> 1342*0a6a1f1dSLionel SambucThe current parser always sees such constructions 1343*0a6a1f1dSLionel Sambucin the first way, 1344*0a6a1f1dSLionel Sambucinterpreting the open parenthesis 1345*0a6a1f1dSLionel Sambucas the start of the arguments to a call. 1346*0a6a1f1dSLionel SambucTo avoid this ambiguity, 1347*0a6a1f1dSLionel Sambucit is a good practice to always precede with a semicolon 1348*0a6a1f1dSLionel Sambucstatements that start with a parenthesis: 1349*0a6a1f1dSLionel Sambuc 1350*0a6a1f1dSLionel Sambuc<pre> 1351*0a6a1f1dSLionel Sambuc ;(print or io.write)('done') 135211be35a1SLionel Sambuc</pre> 135311be35a1SLionel Sambuc 135411be35a1SLionel Sambuc<p> 135511be35a1SLionel SambucA block can be explicitly delimited to produce a single statement: 135611be35a1SLionel Sambuc 135711be35a1SLionel Sambuc<pre> 135811be35a1SLionel Sambuc stat ::= <b>do</b> block <b>end</b> 135911be35a1SLionel Sambuc</pre><p> 136011be35a1SLionel SambucExplicit blocks are useful 136111be35a1SLionel Sambucto control the scope of variable declarations. 136211be35a1SLionel SambucExplicit blocks are also sometimes used to 1363*0a6a1f1dSLionel Sambucadd a <b>return</b> statement in the middle 1364*0a6a1f1dSLionel Sambucof another block (see <a href="#3.3.4">§3.3.4</a>). 136511be35a1SLionel Sambuc 136611be35a1SLionel Sambuc 136711be35a1SLionel Sambuc 136811be35a1SLionel Sambuc 136911be35a1SLionel Sambuc 1370*0a6a1f1dSLionel Sambuc<h3>3.3.2 – <a name="3.3.2">Chunks</a></h3> 1371*0a6a1f1dSLionel Sambuc 1372*0a6a1f1dSLionel Sambuc<p> 1373*0a6a1f1dSLionel SambucThe unit of compilation of Lua is called a <em>chunk</em>. 1374*0a6a1f1dSLionel SambucSyntactically, 1375*0a6a1f1dSLionel Sambuca chunk is simply a block: 1376*0a6a1f1dSLionel Sambuc 1377*0a6a1f1dSLionel Sambuc<pre> 1378*0a6a1f1dSLionel Sambuc chunk ::= block 1379*0a6a1f1dSLionel Sambuc</pre> 1380*0a6a1f1dSLionel Sambuc 1381*0a6a1f1dSLionel Sambuc<p> 1382*0a6a1f1dSLionel SambucLua handles a chunk as the body of an anonymous function 1383*0a6a1f1dSLionel Sambucwith a variable number of arguments 1384*0a6a1f1dSLionel Sambuc(see <a href="#3.4.11">§3.4.11</a>). 1385*0a6a1f1dSLionel SambucAs such, chunks can define local variables, 1386*0a6a1f1dSLionel Sambucreceive arguments, and return values. 1387*0a6a1f1dSLionel SambucMoreover, such anonymous function is compiled as in the 1388*0a6a1f1dSLionel Sambucscope of an external local variable called <code>_ENV</code> (see <a href="#2.2">§2.2</a>). 1389*0a6a1f1dSLionel SambucThe resulting function always has <code>_ENV</code> as its only upvalue, 1390*0a6a1f1dSLionel Sambuceven if it does not use that variable. 1391*0a6a1f1dSLionel Sambuc 1392*0a6a1f1dSLionel Sambuc 1393*0a6a1f1dSLionel Sambuc<p> 1394*0a6a1f1dSLionel SambucA chunk can be stored in a file or in a string inside the host program. 1395*0a6a1f1dSLionel SambucTo execute a chunk, 1396*0a6a1f1dSLionel SambucLua first <em>loads</em> it, 1397*0a6a1f1dSLionel Sambucprecompiling the chunk's code into instructions for a virtual machine, 1398*0a6a1f1dSLionel Sambucand then Lua executes the compiled code 1399*0a6a1f1dSLionel Sambucwith an interpreter for the virtual machine. 1400*0a6a1f1dSLionel Sambuc 1401*0a6a1f1dSLionel Sambuc 1402*0a6a1f1dSLionel Sambuc<p> 1403*0a6a1f1dSLionel SambucChunks can also be precompiled into binary form; 1404*0a6a1f1dSLionel Sambucsee program <code>luac</code> and function <a href="#pdf-string.dump"><code>string.dump</code></a> for details. 1405*0a6a1f1dSLionel SambucPrograms in source and compiled forms are interchangeable; 1406*0a6a1f1dSLionel SambucLua automatically detects the file type and acts accordingly (see <a href="#pdf-load"><code>load</code></a>). 1407*0a6a1f1dSLionel Sambuc 1408*0a6a1f1dSLionel Sambuc 1409*0a6a1f1dSLionel Sambuc 1410*0a6a1f1dSLionel Sambuc 1411*0a6a1f1dSLionel Sambuc 1412*0a6a1f1dSLionel Sambuc<h3>3.3.3 – <a name="3.3.3">Assignment</a></h3> 141311be35a1SLionel Sambuc 141411be35a1SLionel Sambuc<p> 141511be35a1SLionel SambucLua allows multiple assignments. 141611be35a1SLionel SambucTherefore, the syntax for assignment 141711be35a1SLionel Sambucdefines a list of variables on the left side 141811be35a1SLionel Sambucand a list of expressions on the right side. 141911be35a1SLionel SambucThe elements in both lists are separated by commas: 142011be35a1SLionel Sambuc 142111be35a1SLionel Sambuc<pre> 1422*0a6a1f1dSLionel Sambuc stat ::= varlist ‘<b>=</b>’ explist 1423*0a6a1f1dSLionel Sambuc varlist ::= var {‘<b>,</b>’ var} 1424*0a6a1f1dSLionel Sambuc explist ::= exp {‘<b>,</b>’ exp} 142511be35a1SLionel Sambuc</pre><p> 1426*0a6a1f1dSLionel SambucExpressions are discussed in <a href="#3.4">§3.4</a>. 142711be35a1SLionel Sambuc 142811be35a1SLionel Sambuc 142911be35a1SLionel Sambuc<p> 143011be35a1SLionel SambucBefore the assignment, 143111be35a1SLionel Sambucthe list of values is <em>adjusted</em> to the length of 143211be35a1SLionel Sambucthe list of variables. 143311be35a1SLionel SambucIf there are more values than needed, 143411be35a1SLionel Sambucthe excess values are thrown away. 143511be35a1SLionel SambucIf there are fewer values than needed, 143611be35a1SLionel Sambucthe list is extended with as many <b>nil</b>'s as needed. 143711be35a1SLionel SambucIf the list of expressions ends with a function call, 143811be35a1SLionel Sambucthen all values returned by that call enter the list of values, 143911be35a1SLionel Sambucbefore the adjustment 1440*0a6a1f1dSLionel Sambuc(except when the call is enclosed in parentheses; see <a href="#3.4">§3.4</a>). 144111be35a1SLionel Sambuc 144211be35a1SLionel Sambuc 144311be35a1SLionel Sambuc<p> 144411be35a1SLionel SambucThe assignment statement first evaluates all its expressions 1445*0a6a1f1dSLionel Sambucand only then the assignments are performed. 144611be35a1SLionel SambucThus the code 144711be35a1SLionel Sambuc 144811be35a1SLionel Sambuc<pre> 144911be35a1SLionel Sambuc i = 3 145011be35a1SLionel Sambuc i, a[i] = i+1, 20 145111be35a1SLionel Sambuc</pre><p> 145211be35a1SLionel Sambucsets <code>a[3]</code> to 20, without affecting <code>a[4]</code> 145311be35a1SLionel Sambucbecause the <code>i</code> in <code>a[i]</code> is evaluated (to 3) 145411be35a1SLionel Sambucbefore it is assigned 4. 145511be35a1SLionel SambucSimilarly, the line 145611be35a1SLionel Sambuc 145711be35a1SLionel Sambuc<pre> 145811be35a1SLionel Sambuc x, y = y, x 145911be35a1SLionel Sambuc</pre><p> 146011be35a1SLionel Sambucexchanges the values of <code>x</code> and <code>y</code>, 146111be35a1SLionel Sambucand 146211be35a1SLionel Sambuc 146311be35a1SLionel Sambuc<pre> 146411be35a1SLionel Sambuc x, y, z = y, z, x 146511be35a1SLionel Sambuc</pre><p> 146611be35a1SLionel Sambuccyclically permutes the values of <code>x</code>, <code>y</code>, and <code>z</code>. 146711be35a1SLionel Sambuc 146811be35a1SLionel Sambuc 146911be35a1SLionel Sambuc<p> 147011be35a1SLionel SambucThe meaning of assignments to global variables 147111be35a1SLionel Sambucand table fields can be changed via metatables. 147211be35a1SLionel SambucAn assignment to an indexed variable <code>t[i] = val</code> is equivalent to 147311be35a1SLionel Sambuc<code>settable_event(t,i,val)</code>. 1474*0a6a1f1dSLionel Sambuc(See <a href="#2.4">§2.4</a> for a complete description of the 147511be35a1SLionel Sambuc<code>settable_event</code> function. 147611be35a1SLionel SambucThis function is not defined or callable in Lua. 147711be35a1SLionel SambucWe use it here only for explanatory purposes.) 147811be35a1SLionel Sambuc 147911be35a1SLionel Sambuc 148011be35a1SLionel Sambuc<p> 1481*0a6a1f1dSLionel SambucAn assignment to a global name <code>x = val</code> 148211be35a1SLionel Sambucis equivalent to the assignment 1483*0a6a1f1dSLionel Sambuc<code>_ENV.x = val</code> (see <a href="#2.2">§2.2</a>). 148411be35a1SLionel Sambuc 148511be35a1SLionel Sambuc 148611be35a1SLionel Sambuc 148711be35a1SLionel Sambuc 148811be35a1SLionel Sambuc 1489*0a6a1f1dSLionel Sambuc<h3>3.3.4 – <a name="3.3.4">Control Structures</a></h3><p> 149011be35a1SLionel SambucThe control structures 149111be35a1SLionel Sambuc<b>if</b>, <b>while</b>, and <b>repeat</b> have the usual meaning and 149211be35a1SLionel Sambucfamiliar syntax: 149311be35a1SLionel Sambuc 149411be35a1SLionel Sambuc 149511be35a1SLionel Sambuc 149611be35a1SLionel Sambuc 149711be35a1SLionel Sambuc<pre> 149811be35a1SLionel Sambuc stat ::= <b>while</b> exp <b>do</b> block <b>end</b> 149911be35a1SLionel Sambuc stat ::= <b>repeat</b> block <b>until</b> exp 150011be35a1SLionel Sambuc stat ::= <b>if</b> exp <b>then</b> block {<b>elseif</b> exp <b>then</b> block} [<b>else</b> block] <b>end</b> 150111be35a1SLionel Sambuc</pre><p> 1502*0a6a1f1dSLionel SambucLua also has a <b>for</b> statement, in two flavors (see <a href="#3.3.5">§3.3.5</a>). 150311be35a1SLionel Sambuc 150411be35a1SLionel Sambuc 150511be35a1SLionel Sambuc<p> 150611be35a1SLionel SambucThe condition expression of a 150711be35a1SLionel Sambuccontrol structure can return any value. 150811be35a1SLionel SambucBoth <b>false</b> and <b>nil</b> are considered false. 150911be35a1SLionel SambucAll values different from <b>nil</b> and <b>false</b> are considered true 151011be35a1SLionel Sambuc(in particular, the number 0 and the empty string are also true). 151111be35a1SLionel Sambuc 151211be35a1SLionel Sambuc 151311be35a1SLionel Sambuc<p> 151411be35a1SLionel SambucIn the <b>repeat</b>–<b>until</b> loop, 151511be35a1SLionel Sambucthe inner block does not end at the <b>until</b> keyword, 151611be35a1SLionel Sambucbut only after the condition. 151711be35a1SLionel SambucSo, the condition can refer to local variables 151811be35a1SLionel Sambucdeclared inside the loop block. 151911be35a1SLionel Sambuc 152011be35a1SLionel Sambuc 152111be35a1SLionel Sambuc<p> 1522*0a6a1f1dSLionel SambucThe <b>goto</b> statement transfers the program control to a label. 1523*0a6a1f1dSLionel SambucFor syntactical reasons, 1524*0a6a1f1dSLionel Sambuclabels in Lua are considered statements too: 152511be35a1SLionel Sambuc 1526*0a6a1f1dSLionel Sambuc 152711be35a1SLionel Sambuc 152811be35a1SLionel Sambuc<pre> 1529*0a6a1f1dSLionel Sambuc stat ::= <b>goto</b> Name 1530*0a6a1f1dSLionel Sambuc stat ::= label 1531*0a6a1f1dSLionel Sambuc label ::= ‘<b>::</b>’ Name ‘<b>::</b>’ 153211be35a1SLionel Sambuc</pre> 153311be35a1SLionel Sambuc 153411be35a1SLionel Sambuc<p> 1535*0a6a1f1dSLionel SambucA label is visible in the entire block where it is defined, 1536*0a6a1f1dSLionel Sambucexcept 1537*0a6a1f1dSLionel Sambucinside nested blocks where a label with the same name is defined and 1538*0a6a1f1dSLionel Sambucinside nested functions. 1539*0a6a1f1dSLionel SambucA goto may jump to any visible label as long as it does not 1540*0a6a1f1dSLionel Sambucenter into the scope of a local variable. 1541*0a6a1f1dSLionel Sambuc 1542*0a6a1f1dSLionel Sambuc 1543*0a6a1f1dSLionel Sambuc<p> 1544*0a6a1f1dSLionel SambucLabels and empty statements are called <em>void statements</em>, 1545*0a6a1f1dSLionel Sambucas they perform no actions. 1546*0a6a1f1dSLionel Sambuc 1547*0a6a1f1dSLionel Sambuc 1548*0a6a1f1dSLionel Sambuc<p> 1549*0a6a1f1dSLionel SambucThe <b>break</b> statement terminates the execution of a 155011be35a1SLionel Sambuc<b>while</b>, <b>repeat</b>, or <b>for</b> loop, 155111be35a1SLionel Sambucskipping to the next statement after the loop: 155211be35a1SLionel Sambuc 155311be35a1SLionel Sambuc 155411be35a1SLionel Sambuc<pre> 155511be35a1SLionel Sambuc stat ::= <b>break</b> 155611be35a1SLionel Sambuc</pre><p> 155711be35a1SLionel SambucA <b>break</b> ends the innermost enclosing loop. 155811be35a1SLionel Sambuc 155911be35a1SLionel Sambuc 156011be35a1SLionel Sambuc<p> 1561*0a6a1f1dSLionel SambucThe <b>return</b> statement is used to return values 1562*0a6a1f1dSLionel Sambucfrom a function or a chunk 1563*0a6a1f1dSLionel Sambuc(which is an anonymous function). 1564*0a6a1f1dSLionel Sambuc 1565*0a6a1f1dSLionel SambucFunctions can return more than one value, 1566*0a6a1f1dSLionel Sambucso the syntax for the <b>return</b> statement is 1567*0a6a1f1dSLionel Sambuc 1568*0a6a1f1dSLionel Sambuc<pre> 1569*0a6a1f1dSLionel Sambuc stat ::= <b>return</b> [explist] [‘<b>;</b>’] 1570*0a6a1f1dSLionel Sambuc</pre> 1571*0a6a1f1dSLionel Sambuc 1572*0a6a1f1dSLionel Sambuc<p> 1573*0a6a1f1dSLionel SambucThe <b>return</b> statement can only be written 1574*0a6a1f1dSLionel Sambucas the last statement of a block. 1575*0a6a1f1dSLionel SambucIf it is really necessary to <b>return</b> in the middle of a block, 157611be35a1SLionel Sambucthen an explicit inner block can be used, 1577*0a6a1f1dSLionel Sambucas in the idiom <code>do return end</code>, 1578*0a6a1f1dSLionel Sambucbecause now <b>return</b> is the last statement in its (inner) block. 157911be35a1SLionel Sambuc 158011be35a1SLionel Sambuc 158111be35a1SLionel Sambuc 158211be35a1SLionel Sambuc 158311be35a1SLionel Sambuc 1584*0a6a1f1dSLionel Sambuc<h3>3.3.5 – <a name="3.3.5">For Statement</a></h3> 158511be35a1SLionel Sambuc 158611be35a1SLionel Sambuc<p> 158711be35a1SLionel Sambuc 158811be35a1SLionel SambucThe <b>for</b> statement has two forms: 1589*0a6a1f1dSLionel Sambucone numerical and one generic. 159011be35a1SLionel Sambuc 159111be35a1SLionel Sambuc 159211be35a1SLionel Sambuc<p> 1593*0a6a1f1dSLionel SambucThe numerical <b>for</b> loop repeats a block of code while a 159411be35a1SLionel Sambuccontrol variable runs through an arithmetic progression. 159511be35a1SLionel SambucIt has the following syntax: 159611be35a1SLionel Sambuc 159711be35a1SLionel Sambuc<pre> 1598*0a6a1f1dSLionel Sambuc stat ::= <b>for</b> Name ‘<b>=</b>’ exp ‘<b>,</b>’ exp [‘<b>,</b>’ exp] <b>do</b> block <b>end</b> 159911be35a1SLionel Sambuc</pre><p> 160011be35a1SLionel SambucThe <em>block</em> is repeated for <em>name</em> starting at the value of 160111be35a1SLionel Sambucthe first <em>exp</em>, until it passes the second <em>exp</em> by steps of the 160211be35a1SLionel Sambucthird <em>exp</em>. 160311be35a1SLionel SambucMore precisely, a <b>for</b> statement like 160411be35a1SLionel Sambuc 160511be35a1SLionel Sambuc<pre> 160611be35a1SLionel Sambuc for v = <em>e1</em>, <em>e2</em>, <em>e3</em> do <em>block</em> end 160711be35a1SLionel Sambuc</pre><p> 160811be35a1SLionel Sambucis equivalent to the code: 160911be35a1SLionel Sambuc 161011be35a1SLionel Sambuc<pre> 161111be35a1SLionel Sambuc do 161211be35a1SLionel Sambuc local <em>var</em>, <em>limit</em>, <em>step</em> = tonumber(<em>e1</em>), tonumber(<em>e2</em>), tonumber(<em>e3</em>) 161311be35a1SLionel Sambuc if not (<em>var</em> and <em>limit</em> and <em>step</em>) then error() end 1614*0a6a1f1dSLionel Sambuc <em>var</em> = <em>var</em> - <em>step</em> 1615*0a6a1f1dSLionel Sambuc while true do 1616*0a6a1f1dSLionel Sambuc <em>var</em> = <em>var</em> + <em>step</em> 1617*0a6a1f1dSLionel Sambuc if (<em>step</em> >= 0 and <em>var</em> > <em>limit</em>) or (<em>step</em> < 0 and <em>var</em> < <em>limit</em>) then 1618*0a6a1f1dSLionel Sambuc break 1619*0a6a1f1dSLionel Sambuc end 162011be35a1SLionel Sambuc local v = <em>var</em> 162111be35a1SLionel Sambuc <em>block</em> 162211be35a1SLionel Sambuc end 162311be35a1SLionel Sambuc end 1624*0a6a1f1dSLionel Sambuc</pre> 1625*0a6a1f1dSLionel Sambuc 1626*0a6a1f1dSLionel Sambuc<p> 162711be35a1SLionel SambucNote the following: 162811be35a1SLionel Sambuc 162911be35a1SLionel Sambuc<ul> 163011be35a1SLionel Sambuc 163111be35a1SLionel Sambuc<li> 163211be35a1SLionel SambucAll three control expressions are evaluated only once, 163311be35a1SLionel Sambucbefore the loop starts. 163411be35a1SLionel SambucThey must all result in numbers. 163511be35a1SLionel Sambuc</li> 163611be35a1SLionel Sambuc 163711be35a1SLionel Sambuc<li> 163811be35a1SLionel Sambuc<code><em>var</em></code>, <code><em>limit</em></code>, and <code><em>step</em></code> are invisible variables. 163911be35a1SLionel SambucThe names shown here are for explanatory purposes only. 164011be35a1SLionel Sambuc</li> 164111be35a1SLionel Sambuc 164211be35a1SLionel Sambuc<li> 164311be35a1SLionel SambucIf the third expression (the step) is absent, 164411be35a1SLionel Sambucthen a step of 1 is used. 164511be35a1SLionel Sambuc</li> 164611be35a1SLionel Sambuc 164711be35a1SLionel Sambuc<li> 1648*0a6a1f1dSLionel SambucYou can use <b>break</b> and <b>goto</b> to exit a <b>for</b> loop. 164911be35a1SLionel Sambuc</li> 165011be35a1SLionel Sambuc 165111be35a1SLionel Sambuc<li> 1652*0a6a1f1dSLionel SambucThe loop variable <code>v</code> is local to the loop body. 1653*0a6a1f1dSLionel SambucIf you need its value after the loop, 1654*0a6a1f1dSLionel Sambucassign it to another variable before exiting the loop. 165511be35a1SLionel Sambuc</li> 165611be35a1SLionel Sambuc 165711be35a1SLionel Sambuc</ul> 165811be35a1SLionel Sambuc 165911be35a1SLionel Sambuc<p> 166011be35a1SLionel SambucThe generic <b>for</b> statement works over functions, 166111be35a1SLionel Sambuccalled <em>iterators</em>. 166211be35a1SLionel SambucOn each iteration, the iterator function is called to produce a new value, 166311be35a1SLionel Sambucstopping when this new value is <b>nil</b>. 166411be35a1SLionel SambucThe generic <b>for</b> loop has the following syntax: 166511be35a1SLionel Sambuc 166611be35a1SLionel Sambuc<pre> 166711be35a1SLionel Sambuc stat ::= <b>for</b> namelist <b>in</b> explist <b>do</b> block <b>end</b> 1668*0a6a1f1dSLionel Sambuc namelist ::= Name {‘<b>,</b>’ Name} 166911be35a1SLionel Sambuc</pre><p> 167011be35a1SLionel SambucA <b>for</b> statement like 167111be35a1SLionel Sambuc 167211be35a1SLionel Sambuc<pre> 167311be35a1SLionel Sambuc for <em>var_1</em>, ···, <em>var_n</em> in <em>explist</em> do <em>block</em> end 167411be35a1SLionel Sambuc</pre><p> 167511be35a1SLionel Sambucis equivalent to the code: 167611be35a1SLionel Sambuc 167711be35a1SLionel Sambuc<pre> 167811be35a1SLionel Sambuc do 167911be35a1SLionel Sambuc local <em>f</em>, <em>s</em>, <em>var</em> = <em>explist</em> 168011be35a1SLionel Sambuc while true do 168111be35a1SLionel Sambuc local <em>var_1</em>, ···, <em>var_n</em> = <em>f</em>(<em>s</em>, <em>var</em>) 1682*0a6a1f1dSLionel Sambuc if <em>var_1</em> == nil then break end 168311be35a1SLionel Sambuc <em>var</em> = <em>var_1</em> 168411be35a1SLionel Sambuc <em>block</em> 168511be35a1SLionel Sambuc end 168611be35a1SLionel Sambuc end 168711be35a1SLionel Sambuc</pre><p> 168811be35a1SLionel SambucNote the following: 168911be35a1SLionel Sambuc 169011be35a1SLionel Sambuc<ul> 169111be35a1SLionel Sambuc 169211be35a1SLionel Sambuc<li> 169311be35a1SLionel Sambuc<code><em>explist</em></code> is evaluated only once. 169411be35a1SLionel SambucIts results are an <em>iterator</em> function, 169511be35a1SLionel Sambuca <em>state</em>, 169611be35a1SLionel Sambucand an initial value for the first <em>iterator variable</em>. 169711be35a1SLionel Sambuc</li> 169811be35a1SLionel Sambuc 169911be35a1SLionel Sambuc<li> 170011be35a1SLionel Sambuc<code><em>f</em></code>, <code><em>s</em></code>, and <code><em>var</em></code> are invisible variables. 170111be35a1SLionel SambucThe names are here for explanatory purposes only. 170211be35a1SLionel Sambuc</li> 170311be35a1SLionel Sambuc 170411be35a1SLionel Sambuc<li> 170511be35a1SLionel SambucYou can use <b>break</b> to exit a <b>for</b> loop. 170611be35a1SLionel Sambuc</li> 170711be35a1SLionel Sambuc 170811be35a1SLionel Sambuc<li> 170911be35a1SLionel SambucThe loop variables <code><em>var_i</em></code> are local to the loop; 171011be35a1SLionel Sambucyou cannot use their values after the <b>for</b> ends. 171111be35a1SLionel SambucIf you need these values, 171211be35a1SLionel Sambucthen assign them to other variables before breaking or exiting the loop. 171311be35a1SLionel Sambuc</li> 171411be35a1SLionel Sambuc 171511be35a1SLionel Sambuc</ul> 171611be35a1SLionel Sambuc 171711be35a1SLionel Sambuc 171811be35a1SLionel Sambuc 171911be35a1SLionel Sambuc 1720*0a6a1f1dSLionel Sambuc<h3>3.3.6 – <a name="3.3.6">Function Calls as Statements</a></h3><p> 172111be35a1SLionel SambucTo allow possible side-effects, 172211be35a1SLionel Sambucfunction calls can be executed as statements: 172311be35a1SLionel Sambuc 172411be35a1SLionel Sambuc<pre> 172511be35a1SLionel Sambuc stat ::= functioncall 172611be35a1SLionel Sambuc</pre><p> 172711be35a1SLionel SambucIn this case, all returned values are thrown away. 1728*0a6a1f1dSLionel SambucFunction calls are explained in <a href="#3.4.10">§3.4.10</a>. 172911be35a1SLionel Sambuc 173011be35a1SLionel Sambuc 173111be35a1SLionel Sambuc 173211be35a1SLionel Sambuc 173311be35a1SLionel Sambuc 1734*0a6a1f1dSLionel Sambuc<h3>3.3.7 – <a name="3.3.7">Local Declarations</a></h3><p> 173511be35a1SLionel SambucLocal variables can be declared anywhere inside a block. 173611be35a1SLionel SambucThe declaration can include an initial assignment: 173711be35a1SLionel Sambuc 173811be35a1SLionel Sambuc<pre> 1739*0a6a1f1dSLionel Sambuc stat ::= <b>local</b> namelist [‘<b>=</b>’ explist] 174011be35a1SLionel Sambuc</pre><p> 174111be35a1SLionel SambucIf present, an initial assignment has the same semantics 1742*0a6a1f1dSLionel Sambucof a multiple assignment (see <a href="#3.3.3">§3.3.3</a>). 174311be35a1SLionel SambucOtherwise, all variables are initialized with <b>nil</b>. 174411be35a1SLionel Sambuc 174511be35a1SLionel Sambuc 174611be35a1SLionel Sambuc<p> 1747*0a6a1f1dSLionel SambucA chunk is also a block (see <a href="#3.3.2">§3.3.2</a>), 174811be35a1SLionel Sambucand so local variables can be declared in a chunk outside any explicit block. 174911be35a1SLionel Sambuc 175011be35a1SLionel Sambuc 175111be35a1SLionel Sambuc<p> 1752*0a6a1f1dSLionel SambucThe visibility rules for local variables are explained in <a href="#3.5">§3.5</a>. 175311be35a1SLionel Sambuc 175411be35a1SLionel Sambuc 175511be35a1SLionel Sambuc 175611be35a1SLionel Sambuc 175711be35a1SLionel Sambuc 175811be35a1SLionel Sambuc 175911be35a1SLionel Sambuc 1760*0a6a1f1dSLionel Sambuc<h2>3.4 – <a name="3.4">Expressions</a></h2> 176111be35a1SLionel Sambuc 176211be35a1SLionel Sambuc<p> 176311be35a1SLionel SambucThe basic expressions in Lua are the following: 176411be35a1SLionel Sambuc 176511be35a1SLionel Sambuc<pre> 176611be35a1SLionel Sambuc exp ::= prefixexp 176711be35a1SLionel Sambuc exp ::= <b>nil</b> | <b>false</b> | <b>true</b> 1768*0a6a1f1dSLionel Sambuc exp ::= Numeral 1769*0a6a1f1dSLionel Sambuc exp ::= LiteralString 1770*0a6a1f1dSLionel Sambuc exp ::= functiondef 177111be35a1SLionel Sambuc exp ::= tableconstructor 1772*0a6a1f1dSLionel Sambuc exp ::= ‘<b>...</b>’ 177311be35a1SLionel Sambuc exp ::= exp binop exp 177411be35a1SLionel Sambuc exp ::= unop exp 1775*0a6a1f1dSLionel Sambuc prefixexp ::= var | functioncall | ‘<b>(</b>’ exp ‘<b>)</b>’ 177611be35a1SLionel Sambuc</pre> 177711be35a1SLionel Sambuc 177811be35a1SLionel Sambuc<p> 1779*0a6a1f1dSLionel SambucNumerals and literal strings are explained in <a href="#3.1">§3.1</a>; 1780*0a6a1f1dSLionel Sambucvariables are explained in <a href="#3.2">§3.2</a>; 1781*0a6a1f1dSLionel Sambucfunction definitions are explained in <a href="#3.4.11">§3.4.11</a>; 1782*0a6a1f1dSLionel Sambucfunction calls are explained in <a href="#3.4.10">§3.4.10</a>; 1783*0a6a1f1dSLionel Sambuctable constructors are explained in <a href="#3.4.9">§3.4.9</a>. 178411be35a1SLionel SambucVararg expressions, 178511be35a1SLionel Sambucdenoted by three dots ('<code>...</code>'), can only be used when 178611be35a1SLionel Sambucdirectly inside a vararg function; 1787*0a6a1f1dSLionel Sambucthey are explained in <a href="#3.4.11">§3.4.11</a>. 178811be35a1SLionel Sambuc 178911be35a1SLionel Sambuc 179011be35a1SLionel Sambuc<p> 1791*0a6a1f1dSLionel SambucBinary operators comprise arithmetic operators (see <a href="#3.4.1">§3.4.1</a>), 1792*0a6a1f1dSLionel Sambucbitwise operators (see <a href="#3.4.2">§3.4.2</a>), 1793*0a6a1f1dSLionel Sambucrelational operators (see <a href="#3.4.4">§3.4.4</a>), logical operators (see <a href="#3.4.5">§3.4.5</a>), 1794*0a6a1f1dSLionel Sambucand the concatenation operator (see <a href="#3.4.6">§3.4.6</a>). 1795*0a6a1f1dSLionel SambucUnary operators comprise the unary minus (see <a href="#3.4.1">§3.4.1</a>), 1796*0a6a1f1dSLionel Sambucthe unary bitwise not (see <a href="#3.4.2">§3.4.2</a>), 1797*0a6a1f1dSLionel Sambucthe unary logical <b>not</b> (see <a href="#3.4.5">§3.4.5</a>), 1798*0a6a1f1dSLionel Sambucand the unary <em>length operator</em> (see <a href="#3.4.7">§3.4.7</a>). 179911be35a1SLionel Sambuc 180011be35a1SLionel Sambuc 180111be35a1SLionel Sambuc<p> 180211be35a1SLionel SambucBoth function calls and vararg expressions can result in multiple values. 1803*0a6a1f1dSLionel SambucIf a function call is used as a statement (see <a href="#3.3.6">§3.3.6</a>), 180411be35a1SLionel Sambucthen its return list is adjusted to zero elements, 180511be35a1SLionel Sambucthus discarding all returned values. 180611be35a1SLionel SambucIf an expression is used as the last (or the only) element 180711be35a1SLionel Sambucof a list of expressions, 180811be35a1SLionel Sambucthen no adjustment is made 1809*0a6a1f1dSLionel Sambuc(unless the expression is enclosed in parentheses). 181011be35a1SLionel SambucIn all other contexts, 181111be35a1SLionel SambucLua adjusts the result list to one element, 1812*0a6a1f1dSLionel Sambuceither discarding all values except the first one 1813*0a6a1f1dSLionel Sambucor adding a single <b>nil</b> if there are no values. 181411be35a1SLionel Sambuc 181511be35a1SLionel Sambuc 181611be35a1SLionel Sambuc<p> 181711be35a1SLionel SambucHere are some examples: 181811be35a1SLionel Sambuc 181911be35a1SLionel Sambuc<pre> 182011be35a1SLionel Sambuc f() -- adjusted to 0 results 182111be35a1SLionel Sambuc g(f(), x) -- f() is adjusted to 1 result 182211be35a1SLionel Sambuc g(x, f()) -- g gets x plus all results from f() 182311be35a1SLionel Sambuc a,b,c = f(), x -- f() is adjusted to 1 result (c gets nil) 182411be35a1SLionel Sambuc a,b = ... -- a gets the first vararg parameter, b gets 182511be35a1SLionel Sambuc -- the second (both a and b can get nil if there 182611be35a1SLionel Sambuc -- is no corresponding vararg parameter) 182711be35a1SLionel Sambuc 182811be35a1SLionel Sambuc a,b,c = x, f() -- f() is adjusted to 2 results 182911be35a1SLionel Sambuc a,b,c = f() -- f() is adjusted to 3 results 183011be35a1SLionel Sambuc return f() -- returns all results from f() 183111be35a1SLionel Sambuc return ... -- returns all received vararg parameters 183211be35a1SLionel Sambuc return x,y,f() -- returns x, y, and all results from f() 183311be35a1SLionel Sambuc {f()} -- creates a list with all results from f() 183411be35a1SLionel Sambuc {...} -- creates a list with all vararg parameters 183511be35a1SLionel Sambuc {f(), nil} -- f() is adjusted to 1 result 183611be35a1SLionel Sambuc</pre> 183711be35a1SLionel Sambuc 183811be35a1SLionel Sambuc<p> 183911be35a1SLionel SambucAny expression enclosed in parentheses always results in only one value. 184011be35a1SLionel SambucThus, 184111be35a1SLionel Sambuc<code>(f(x,y,z))</code> is always a single value, 184211be35a1SLionel Sambuceven if <code>f</code> returns several values. 184311be35a1SLionel Sambuc(The value of <code>(f(x,y,z))</code> is the first value returned by <code>f</code> 184411be35a1SLionel Sambucor <b>nil</b> if <code>f</code> does not return any values.) 184511be35a1SLionel Sambuc 184611be35a1SLionel Sambuc 184711be35a1SLionel Sambuc 1848*0a6a1f1dSLionel Sambuc<h3>3.4.1 – <a name="3.4.1">Arithmetic Operators</a></h3><p> 1849*0a6a1f1dSLionel SambucLua supports the following arithmetic operators: 185011be35a1SLionel Sambuc 1851*0a6a1f1dSLionel Sambuc<ul> 1852*0a6a1f1dSLionel Sambuc<li><b><code>+</code>: </b>addition</li> 1853*0a6a1f1dSLionel Sambuc<li><b><code>-</code>: </b>subtraction</li> 1854*0a6a1f1dSLionel Sambuc<li><b><code>*</code>: </b>multiplication</li> 1855*0a6a1f1dSLionel Sambuc<li><b><code>/</code>: </b>float division</li> 1856*0a6a1f1dSLionel Sambuc<li><b><code>//</code>: </b>floor division</li> 1857*0a6a1f1dSLionel Sambuc<li><b><code>%</code>: </b>modulo</li> 1858*0a6a1f1dSLionel Sambuc<li><b><code>^</code>: </b>exponentiation</li> 1859*0a6a1f1dSLionel Sambuc<li><b><code>-</code>: </b>unary minus</li> 1860*0a6a1f1dSLionel Sambuc</ul> 1861*0a6a1f1dSLionel Sambuc 1862*0a6a1f1dSLionel Sambuc<p> 1863*0a6a1f1dSLionel SambucWith the exception of exponentiation and float division, 1864*0a6a1f1dSLionel Sambucthe arithmetic operators work as follows: 1865*0a6a1f1dSLionel SambucIf both operands are integers, 1866*0a6a1f1dSLionel Sambucthe operation is performed over integers and the result is an integer. 1867*0a6a1f1dSLionel SambucOtherwise, if both operands are numbers 1868*0a6a1f1dSLionel Sambucor strings that can be converted to 1869*0a6a1f1dSLionel Sambucnumbers (see <a href="#3.4.3">§3.4.3</a>), 1870*0a6a1f1dSLionel Sambucthen they are converted to floats, 1871*0a6a1f1dSLionel Sambucthe operation is performed following the usual rules 1872*0a6a1f1dSLionel Sambucfor floating-point arithmetic 1873*0a6a1f1dSLionel Sambuc(usually the IEEE 754 standard), 1874*0a6a1f1dSLionel Sambucand the result is a float. 1875*0a6a1f1dSLionel Sambuc 1876*0a6a1f1dSLionel Sambuc 1877*0a6a1f1dSLionel Sambuc<p> 1878*0a6a1f1dSLionel SambucExponentiation and float division (<code>/</code>) 1879*0a6a1f1dSLionel Sambucalways convert their operands to floats 1880*0a6a1f1dSLionel Sambucand the result is always a float. 1881*0a6a1f1dSLionel SambucExponentiation uses the ISO C function <code>pow</code>, 1882*0a6a1f1dSLionel Sambucso that it works for non-integer exponents too. 1883*0a6a1f1dSLionel Sambuc 1884*0a6a1f1dSLionel Sambuc 1885*0a6a1f1dSLionel Sambuc<p> 1886*0a6a1f1dSLionel SambucFloor division (<code>//</code>) is a division 1887*0a6a1f1dSLionel Sambucthat rounds the quotient towards minus infinity, 1888*0a6a1f1dSLionel Sambucthat is, the floor of the division of its operands. 1889*0a6a1f1dSLionel Sambuc 1890*0a6a1f1dSLionel Sambuc 1891*0a6a1f1dSLionel Sambuc<p> 1892*0a6a1f1dSLionel SambucModulo is defined as the remainder of a division 1893*0a6a1f1dSLionel Sambucthat rounds the quotient towards minus infinity (floor division). 1894*0a6a1f1dSLionel Sambuc 1895*0a6a1f1dSLionel Sambuc 1896*0a6a1f1dSLionel Sambuc<p> 1897*0a6a1f1dSLionel SambucIn case of overflows in integer arithmetic, 1898*0a6a1f1dSLionel Sambucall operations <em>wrap around</em>, 1899*0a6a1f1dSLionel Sambucaccording to the usual rules of two-complement arithmetic. 1900*0a6a1f1dSLionel Sambuc(In other words, 1901*0a6a1f1dSLionel Sambucthey return the unique representable integer 1902*0a6a1f1dSLionel Sambucthat is equal modulo <em>2<sup>64</sup></em> to the mathematical result.) 1903*0a6a1f1dSLionel Sambuc 1904*0a6a1f1dSLionel Sambuc 1905*0a6a1f1dSLionel Sambuc 1906*0a6a1f1dSLionel Sambuc<h3>3.4.2 – <a name="3.4.2">Bitwise Operators</a></h3><p> 1907*0a6a1f1dSLionel SambucLua supports the following bitwise operators: 1908*0a6a1f1dSLionel Sambuc 1909*0a6a1f1dSLionel Sambuc<ul> 1910*0a6a1f1dSLionel Sambuc<li><b><code>&</code>: </b>bitwise and</li> 1911*0a6a1f1dSLionel Sambuc<li><b><code>|</code>: </b>bitwise or</li> 1912*0a6a1f1dSLionel Sambuc<li><b><code>~</code>: </b>bitwise exclusive or</li> 1913*0a6a1f1dSLionel Sambuc<li><b><code>>></code>: </b>right shift</li> 1914*0a6a1f1dSLionel Sambuc<li><b><code><<</code>: </b>left shift</li> 1915*0a6a1f1dSLionel Sambuc<li><b><code>~</code>: </b>unary bitwise not</li> 1916*0a6a1f1dSLionel Sambuc</ul> 1917*0a6a1f1dSLionel Sambuc 1918*0a6a1f1dSLionel Sambuc<p> 1919*0a6a1f1dSLionel SambucAll bitwise operations convert its operands to integers 1920*0a6a1f1dSLionel Sambuc(see <a href="#3.4.3">§3.4.3</a>), 1921*0a6a1f1dSLionel Sambucoperate on all bits of those integers, 1922*0a6a1f1dSLionel Sambucand result in an integer. 1923*0a6a1f1dSLionel Sambuc 1924*0a6a1f1dSLionel Sambuc 1925*0a6a1f1dSLionel Sambuc<p> 1926*0a6a1f1dSLionel SambucBoth right and left shifts fill the vacant bits with zeros. 1927*0a6a1f1dSLionel SambucNegative displacements shift to the other direction; 1928*0a6a1f1dSLionel Sambucdisplacements with absolute values equal to or higher than 1929*0a6a1f1dSLionel Sambucthe number of bits in an integer 1930*0a6a1f1dSLionel Sambucresult in zero (as all bits are shifted out). 193111be35a1SLionel Sambuc 193211be35a1SLionel Sambuc 193311be35a1SLionel Sambuc 193411be35a1SLionel Sambuc 193511be35a1SLionel Sambuc 1936*0a6a1f1dSLionel Sambuc<h3>3.4.3 – <a name="3.4.3">Coercions and Conversions</a></h3><p> 1937*0a6a1f1dSLionel SambucLua provides some automatic conversions between some 1938*0a6a1f1dSLionel Sambuctypes and representations at run time. 1939*0a6a1f1dSLionel SambucBitwise operators always convert float operands to integers. 1940*0a6a1f1dSLionel SambucExponentiation and float division 1941*0a6a1f1dSLionel Sambucalways convert integer operands to floats. 1942*0a6a1f1dSLionel SambucAll other arithmetic operations applied to mixed numbers 1943*0a6a1f1dSLionel Sambuc(integers and floats) convert the integer operand to a float; 1944*0a6a1f1dSLionel Sambucthis is called the <em>usual rule</em>. 1945*0a6a1f1dSLionel SambucThe C API also converts both integers to floats and 1946*0a6a1f1dSLionel Sambucfloats to integers, as needed. 1947*0a6a1f1dSLionel SambucMoreover, string concatenation accepts numbers as arguments, 1948*0a6a1f1dSLionel Sambucbesides strings. 194911be35a1SLionel Sambuc 1950*0a6a1f1dSLionel Sambuc 1951*0a6a1f1dSLionel Sambuc<p> 1952*0a6a1f1dSLionel SambucLua also converts strings to numbers, 1953*0a6a1f1dSLionel Sambucwhenever a number is expected. 1954*0a6a1f1dSLionel Sambuc 1955*0a6a1f1dSLionel Sambuc 1956*0a6a1f1dSLionel Sambuc<p> 1957*0a6a1f1dSLionel SambucIn a conversion from integer to float, 1958*0a6a1f1dSLionel Sambucif the integer value has an exact representation as a float, 1959*0a6a1f1dSLionel Sambucthat is the result. 1960*0a6a1f1dSLionel SambucOtherwise, 1961*0a6a1f1dSLionel Sambucthe conversion gets the nearest higher or 1962*0a6a1f1dSLionel Sambucthe nearest lower representable value. 1963*0a6a1f1dSLionel SambucThis kind of conversion never fails. 1964*0a6a1f1dSLionel Sambuc 1965*0a6a1f1dSLionel Sambuc 1966*0a6a1f1dSLionel Sambuc<p> 1967*0a6a1f1dSLionel SambucThe conversion from float to integer 1968*0a6a1f1dSLionel Sambucchecks whether the float has an exact representation as an integer 1969*0a6a1f1dSLionel Sambuc(that is, the float has an integral value and 1970*0a6a1f1dSLionel Sambucit is in the range of integer representation). 1971*0a6a1f1dSLionel SambucIf it does, that representation is the result. 1972*0a6a1f1dSLionel SambucOtherwise, the conversion fails. 1973*0a6a1f1dSLionel Sambuc 1974*0a6a1f1dSLionel Sambuc 1975*0a6a1f1dSLionel Sambuc<p> 1976*0a6a1f1dSLionel SambucThe conversion from strings to numbers goes as follows: 1977*0a6a1f1dSLionel SambucFirst, the string is converted to an integer or a float, 1978*0a6a1f1dSLionel Sambucfollowing its syntax and the rules of the Lua lexer. 1979*0a6a1f1dSLionel Sambuc(The string may have also leading and trailing spaces and a sign.) 1980*0a6a1f1dSLionel SambucThen, the resulting number (float or integer) 1981*0a6a1f1dSLionel Sambucis converted to the type (float or integer) required by the context 1982*0a6a1f1dSLionel Sambuc(e.g., the operation that forced the conversion). 1983*0a6a1f1dSLionel Sambuc 1984*0a6a1f1dSLionel Sambuc 1985*0a6a1f1dSLionel Sambuc<p> 1986*0a6a1f1dSLionel SambucThe conversion from numbers to strings uses a 1987*0a6a1f1dSLionel Sambucnon-specified human-readable format. 1988*0a6a1f1dSLionel SambucFor complete control over how numbers are converted to strings, 1989*0a6a1f1dSLionel Sambucuse the <code>format</code> function from the string library 1990*0a6a1f1dSLionel Sambuc(see <a href="#pdf-string.format"><code>string.format</code></a>). 1991*0a6a1f1dSLionel Sambuc 1992*0a6a1f1dSLionel Sambuc 1993*0a6a1f1dSLionel Sambuc 1994*0a6a1f1dSLionel Sambuc 1995*0a6a1f1dSLionel Sambuc 1996*0a6a1f1dSLionel Sambuc<h3>3.4.4 – <a name="3.4.4">Relational Operators</a></h3><p> 1997*0a6a1f1dSLionel SambucLua supports the following relational operators: 1998*0a6a1f1dSLionel Sambuc 1999*0a6a1f1dSLionel Sambuc<ul> 2000*0a6a1f1dSLionel Sambuc<li><b><code>==</code>: </b>equality</li> 2001*0a6a1f1dSLionel Sambuc<li><b><code>~=</code>: </b>inequality</li> 2002*0a6a1f1dSLionel Sambuc<li><b><code><</code>: </b>less than</li> 2003*0a6a1f1dSLionel Sambuc<li><b><code>></code>: </b>greater than</li> 2004*0a6a1f1dSLionel Sambuc<li><b><code><=</code>: </b>less or equal</li> 2005*0a6a1f1dSLionel Sambuc<li><b><code>>=</code>: </b>greater or equal</li> 2006*0a6a1f1dSLionel Sambuc</ul><p> 200711be35a1SLionel SambucThese operators always result in <b>false</b> or <b>true</b>. 200811be35a1SLionel Sambuc 200911be35a1SLionel Sambuc 201011be35a1SLionel Sambuc<p> 201111be35a1SLionel SambucEquality (<code>==</code>) first compares the type of its operands. 201211be35a1SLionel SambucIf the types are different, then the result is <b>false</b>. 201311be35a1SLionel SambucOtherwise, the values of the operands are compared. 2014*0a6a1f1dSLionel SambucStrings are compared in the obvious way. 2015*0a6a1f1dSLionel SambucNumbers are equal if they denote the same mathematical value. 2016*0a6a1f1dSLionel Sambuc 2017*0a6a1f1dSLionel Sambuc 2018*0a6a1f1dSLionel Sambuc<p> 2019*0a6a1f1dSLionel SambucTables, userdata, and threads 2020*0a6a1f1dSLionel Sambucare compared by reference: 2021*0a6a1f1dSLionel Sambuctwo objects are considered equal only if they are the same object. 202211be35a1SLionel SambucEvery time you create a new object 2023*0a6a1f1dSLionel Sambuc(a table, userdata, or thread), 202411be35a1SLionel Sambucthis new object is different from any previously existing object. 2025*0a6a1f1dSLionel SambucClosures with the same reference are always equal. 2026*0a6a1f1dSLionel SambucClosures with any detectable difference 2027*0a6a1f1dSLionel Sambuc(different behavior, different definition) are always different. 202811be35a1SLionel Sambuc 202911be35a1SLionel Sambuc 203011be35a1SLionel Sambuc<p> 203111be35a1SLionel SambucYou can change the way that Lua compares tables and userdata 2032*0a6a1f1dSLionel Sambucby using the "eq" metamethod (see <a href="#2.4">§2.4</a>). 203311be35a1SLionel Sambuc 203411be35a1SLionel Sambuc 203511be35a1SLionel Sambuc<p> 2036*0a6a1f1dSLionel SambucEquality comparisons do not convert strings to numbers 2037*0a6a1f1dSLionel Sambucor vice versa. 203811be35a1SLionel SambucThus, <code>"0"==0</code> evaluates to <b>false</b>, 203911be35a1SLionel Sambucand <code>t[0]</code> and <code>t["0"]</code> denote different 204011be35a1SLionel Sambucentries in a table. 204111be35a1SLionel Sambuc 204211be35a1SLionel Sambuc 204311be35a1SLionel Sambuc<p> 204411be35a1SLionel SambucThe operator <code>~=</code> is exactly the negation of equality (<code>==</code>). 204511be35a1SLionel Sambuc 204611be35a1SLionel Sambuc 204711be35a1SLionel Sambuc<p> 204811be35a1SLionel SambucThe order operators work as follows. 2049*0a6a1f1dSLionel SambucIf both arguments are numbers, 2050*0a6a1f1dSLionel Sambucthen they are compared according to their mathematical values 2051*0a6a1f1dSLionel Sambuc(regardless of their subtypes). 205211be35a1SLionel SambucOtherwise, if both arguments are strings, 205311be35a1SLionel Sambucthen their values are compared according to the current locale. 205411be35a1SLionel SambucOtherwise, Lua tries to call the "lt" or the "le" 2055*0a6a1f1dSLionel Sambucmetamethod (see <a href="#2.4">§2.4</a>). 205611be35a1SLionel SambucA comparison <code>a > b</code> is translated to <code>b < a</code> 205711be35a1SLionel Sambucand <code>a >= b</code> is translated to <code>b <= a</code>. 205811be35a1SLionel Sambuc 205911be35a1SLionel Sambuc 2060*0a6a1f1dSLionel Sambuc<p> 2061*0a6a1f1dSLionel SambucFollowing the IEEE 754 standard, 2062*0a6a1f1dSLionel SambucNaN is considered neither smaller than, 2063*0a6a1f1dSLionel Sambucnor equal to, nor greater than any value (including itself). 206411be35a1SLionel Sambuc 206511be35a1SLionel Sambuc 206611be35a1SLionel Sambuc 2067*0a6a1f1dSLionel Sambuc 2068*0a6a1f1dSLionel Sambuc 2069*0a6a1f1dSLionel Sambuc<h3>3.4.5 – <a name="3.4.5">Logical Operators</a></h3><p> 207011be35a1SLionel SambucThe logical operators in Lua are 207111be35a1SLionel Sambuc<b>and</b>, <b>or</b>, and <b>not</b>. 2072*0a6a1f1dSLionel SambucLike the control structures (see <a href="#3.3.4">§3.3.4</a>), 207311be35a1SLionel Sambucall logical operators consider both <b>false</b> and <b>nil</b> as false 207411be35a1SLionel Sambucand anything else as true. 207511be35a1SLionel Sambuc 207611be35a1SLionel Sambuc 207711be35a1SLionel Sambuc<p> 207811be35a1SLionel SambucThe negation operator <b>not</b> always returns <b>false</b> or <b>true</b>. 207911be35a1SLionel SambucThe conjunction operator <b>and</b> returns its first argument 208011be35a1SLionel Sambucif this value is <b>false</b> or <b>nil</b>; 208111be35a1SLionel Sambucotherwise, <b>and</b> returns its second argument. 208211be35a1SLionel SambucThe disjunction operator <b>or</b> returns its first argument 208311be35a1SLionel Sambucif this value is different from <b>nil</b> and <b>false</b>; 208411be35a1SLionel Sambucotherwise, <b>or</b> returns its second argument. 2085*0a6a1f1dSLionel SambucBoth <b>and</b> and <b>or</b> use short-circuit evaluation; 208611be35a1SLionel Sambucthat is, 208711be35a1SLionel Sambucthe second operand is evaluated only if necessary. 208811be35a1SLionel SambucHere are some examples: 208911be35a1SLionel Sambuc 209011be35a1SLionel Sambuc<pre> 209111be35a1SLionel Sambuc 10 or 20 --> 10 209211be35a1SLionel Sambuc 10 or error() --> 10 209311be35a1SLionel Sambuc nil or "a" --> "a" 209411be35a1SLionel Sambuc nil and 10 --> nil 209511be35a1SLionel Sambuc false and error() --> false 209611be35a1SLionel Sambuc false and nil --> false 209711be35a1SLionel Sambuc false or nil --> nil 209811be35a1SLionel Sambuc 10 and 20 --> 20 209911be35a1SLionel Sambuc</pre><p> 210011be35a1SLionel Sambuc(In this manual, 210111be35a1SLionel Sambuc<code>--></code> indicates the result of the preceding expression.) 210211be35a1SLionel Sambuc 210311be35a1SLionel Sambuc 210411be35a1SLionel Sambuc 210511be35a1SLionel Sambuc 210611be35a1SLionel Sambuc 2107*0a6a1f1dSLionel Sambuc<h3>3.4.6 – <a name="3.4.6">Concatenation</a></h3><p> 210811be35a1SLionel SambucThe string concatenation operator in Lua is 210911be35a1SLionel Sambucdenoted by two dots ('<code>..</code>'). 211011be35a1SLionel SambucIf both operands are strings or numbers, then they are converted to 2111*0a6a1f1dSLionel Sambucstrings according to the rules described in <a href="#3.4.3">§3.4.3</a>. 2112*0a6a1f1dSLionel SambucOtherwise, the <code>__concat</code> metamethod is called (see <a href="#2.4">§2.4</a>). 211311be35a1SLionel Sambuc 211411be35a1SLionel Sambuc 211511be35a1SLionel Sambuc 211611be35a1SLionel Sambuc 211711be35a1SLionel Sambuc 2118*0a6a1f1dSLionel Sambuc<h3>3.4.7 – <a name="3.4.7">The Length Operator</a></h3> 211911be35a1SLionel Sambuc 212011be35a1SLionel Sambuc<p> 2121*0a6a1f1dSLionel SambucThe length operator is denoted by the unary prefix operator <code>#</code>. 212211be35a1SLionel SambucThe length of a string is its number of bytes 212311be35a1SLionel Sambuc(that is, the usual meaning of string length when each 212411be35a1SLionel Sambuccharacter is one byte). 212511be35a1SLionel Sambuc 212611be35a1SLionel Sambuc 212711be35a1SLionel Sambuc<p> 2128*0a6a1f1dSLionel SambucA program can modify the behavior of the length operator for 2129*0a6a1f1dSLionel Sambucany value but strings through the <code>__len</code> metamethod (see <a href="#2.4">§2.4</a>). 2130*0a6a1f1dSLionel Sambuc 2131*0a6a1f1dSLionel Sambuc 2132*0a6a1f1dSLionel Sambuc<p> 2133*0a6a1f1dSLionel SambucUnless a <code>__len</code> metamethod is given, 2134*0a6a1f1dSLionel Sambucthe length of a table <code>t</code> is only defined if the 2135*0a6a1f1dSLionel Sambuctable is a <em>sequence</em>, 2136*0a6a1f1dSLionel Sambucthat is, 2137*0a6a1f1dSLionel Sambucthe set of its positive numeric keys is equal to <em>{1..n}</em> 2138*0a6a1f1dSLionel Sambucfor some non-negative integer <em>n</em>. 2139*0a6a1f1dSLionel SambucIn that case, <em>n</em> is its length. 2140*0a6a1f1dSLionel SambucNote that a table like 2141*0a6a1f1dSLionel Sambuc 2142*0a6a1f1dSLionel Sambuc<pre> 2143*0a6a1f1dSLionel Sambuc {10, 20, nil, 40} 2144*0a6a1f1dSLionel Sambuc</pre><p> 2145*0a6a1f1dSLionel Sambucis not a sequence, because it has the key <code>4</code> 2146*0a6a1f1dSLionel Sambucbut does not have the key <code>3</code>. 2147*0a6a1f1dSLionel Sambuc(So, there is no <em>n</em> such that the set <em>{1..n}</em> is equal 2148*0a6a1f1dSLionel Sambucto the set of positive numeric keys of that table.) 2149*0a6a1f1dSLionel SambucNote, however, that non-numeric keys do not interfere 2150*0a6a1f1dSLionel Sambucwith whether a table is a sequence. 215111be35a1SLionel Sambuc 215211be35a1SLionel Sambuc 215311be35a1SLionel Sambuc 215411be35a1SLionel Sambuc 215511be35a1SLionel Sambuc 2156*0a6a1f1dSLionel Sambuc<h3>3.4.8 – <a name="3.4.8">Precedence</a></h3><p> 215711be35a1SLionel SambucOperator precedence in Lua follows the table below, 215811be35a1SLionel Sambucfrom lower to higher priority: 215911be35a1SLionel Sambuc 216011be35a1SLionel Sambuc<pre> 216111be35a1SLionel Sambuc or 216211be35a1SLionel Sambuc and 216311be35a1SLionel Sambuc < > <= >= ~= == 2164*0a6a1f1dSLionel Sambuc | 2165*0a6a1f1dSLionel Sambuc ~ 2166*0a6a1f1dSLionel Sambuc & 2167*0a6a1f1dSLionel Sambuc << >> 216811be35a1SLionel Sambuc .. 216911be35a1SLionel Sambuc + - 2170*0a6a1f1dSLionel Sambuc * / // % 2171*0a6a1f1dSLionel Sambuc unary operators (not # - ~) 217211be35a1SLionel Sambuc ^ 217311be35a1SLionel Sambuc</pre><p> 217411be35a1SLionel SambucAs usual, 217511be35a1SLionel Sambucyou can use parentheses to change the precedences of an expression. 217611be35a1SLionel SambucThe concatenation ('<code>..</code>') and exponentiation ('<code>^</code>') 217711be35a1SLionel Sambucoperators are right associative. 217811be35a1SLionel SambucAll other binary operators are left associative. 217911be35a1SLionel Sambuc 218011be35a1SLionel Sambuc 218111be35a1SLionel Sambuc 218211be35a1SLionel Sambuc 218311be35a1SLionel Sambuc 2184*0a6a1f1dSLionel Sambuc<h3>3.4.9 – <a name="3.4.9">Table Constructors</a></h3><p> 218511be35a1SLionel SambucTable constructors are expressions that create tables. 218611be35a1SLionel SambucEvery time a constructor is evaluated, a new table is created. 218711be35a1SLionel SambucA constructor can be used to create an empty table 218811be35a1SLionel Sambucor to create a table and initialize some of its fields. 218911be35a1SLionel SambucThe general syntax for constructors is 219011be35a1SLionel Sambuc 219111be35a1SLionel Sambuc<pre> 2192*0a6a1f1dSLionel Sambuc tableconstructor ::= ‘<b>{</b>’ [fieldlist] ‘<b>}</b>’ 219311be35a1SLionel Sambuc fieldlist ::= field {fieldsep field} [fieldsep] 2194*0a6a1f1dSLionel Sambuc field ::= ‘<b>[</b>’ exp ‘<b>]</b>’ ‘<b>=</b>’ exp | Name ‘<b>=</b>’ exp | exp 2195*0a6a1f1dSLionel Sambuc fieldsep ::= ‘<b>,</b>’ | ‘<b>;</b>’ 219611be35a1SLionel Sambuc</pre> 219711be35a1SLionel Sambuc 219811be35a1SLionel Sambuc<p> 219911be35a1SLionel SambucEach field of the form <code>[exp1] = exp2</code> adds to the new table an entry 220011be35a1SLionel Sambucwith key <code>exp1</code> and value <code>exp2</code>. 220111be35a1SLionel SambucA field of the form <code>name = exp</code> is equivalent to 220211be35a1SLionel Sambuc<code>["name"] = exp</code>. 220311be35a1SLionel SambucFinally, fields of the form <code>exp</code> are equivalent to 2204*0a6a1f1dSLionel Sambuc<code>[i] = exp</code>, where <code>i</code> are consecutive integers 220511be35a1SLionel Sambucstarting with 1. 220611be35a1SLionel SambucFields in the other formats do not affect this counting. 220711be35a1SLionel SambucFor example, 220811be35a1SLionel Sambuc 220911be35a1SLionel Sambuc<pre> 221011be35a1SLionel Sambuc a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 } 221111be35a1SLionel Sambuc</pre><p> 221211be35a1SLionel Sambucis equivalent to 221311be35a1SLionel Sambuc 221411be35a1SLionel Sambuc<pre> 221511be35a1SLionel Sambuc do 221611be35a1SLionel Sambuc local t = {} 221711be35a1SLionel Sambuc t[f(1)] = g 221811be35a1SLionel Sambuc t[1] = "x" -- 1st exp 221911be35a1SLionel Sambuc t[2] = "y" -- 2nd exp 222011be35a1SLionel Sambuc t.x = 1 -- t["x"] = 1 222111be35a1SLionel Sambuc t[3] = f(x) -- 3rd exp 222211be35a1SLionel Sambuc t[30] = 23 222311be35a1SLionel Sambuc t[4] = 45 -- 4th exp 222411be35a1SLionel Sambuc a = t 222511be35a1SLionel Sambuc end 222611be35a1SLionel Sambuc</pre> 222711be35a1SLionel Sambuc 222811be35a1SLionel Sambuc<p> 2229*0a6a1f1dSLionel SambucThe order of the assignments in a constructor is undefined. 2230*0a6a1f1dSLionel Sambuc(This order would be relevant only when there are repeated keys.) 2231*0a6a1f1dSLionel Sambuc 2232*0a6a1f1dSLionel Sambuc 2233*0a6a1f1dSLionel Sambuc<p> 223411be35a1SLionel SambucIf the last field in the list has the form <code>exp</code> 223511be35a1SLionel Sambucand the expression is a function call or a vararg expression, 223611be35a1SLionel Sambucthen all values returned by this expression enter the list consecutively 2237*0a6a1f1dSLionel Sambuc(see <a href="#3.4.10">§3.4.10</a>). 223811be35a1SLionel Sambuc 223911be35a1SLionel Sambuc 224011be35a1SLionel Sambuc<p> 224111be35a1SLionel SambucThe field list can have an optional trailing separator, 224211be35a1SLionel Sambucas a convenience for machine-generated code. 224311be35a1SLionel Sambuc 224411be35a1SLionel Sambuc 224511be35a1SLionel Sambuc 224611be35a1SLionel Sambuc 224711be35a1SLionel Sambuc 2248*0a6a1f1dSLionel Sambuc<h3>3.4.10 – <a name="3.4.10">Function Calls</a></h3><p> 224911be35a1SLionel SambucA function call in Lua has the following syntax: 225011be35a1SLionel Sambuc 225111be35a1SLionel Sambuc<pre> 225211be35a1SLionel Sambuc functioncall ::= prefixexp args 225311be35a1SLionel Sambuc</pre><p> 225411be35a1SLionel SambucIn a function call, 225511be35a1SLionel Sambucfirst prefixexp and args are evaluated. 225611be35a1SLionel SambucIf the value of prefixexp has type <em>function</em>, 225711be35a1SLionel Sambucthen this function is called 225811be35a1SLionel Sambucwith the given arguments. 225911be35a1SLionel SambucOtherwise, the prefixexp "call" metamethod is called, 226011be35a1SLionel Sambuchaving as first parameter the value of prefixexp, 226111be35a1SLionel Sambucfollowed by the original call arguments 2262*0a6a1f1dSLionel Sambuc(see <a href="#2.4">§2.4</a>). 226311be35a1SLionel Sambuc 226411be35a1SLionel Sambuc 226511be35a1SLionel Sambuc<p> 226611be35a1SLionel SambucThe form 226711be35a1SLionel Sambuc 226811be35a1SLionel Sambuc<pre> 2269*0a6a1f1dSLionel Sambuc functioncall ::= prefixexp ‘<b>:</b>’ Name args 227011be35a1SLionel Sambuc</pre><p> 227111be35a1SLionel Sambuccan be used to call "methods". 227211be35a1SLionel SambucA call <code>v:name(<em>args</em>)</code> 227311be35a1SLionel Sambucis syntactic sugar for <code>v.name(v,<em>args</em>)</code>, 227411be35a1SLionel Sambucexcept that <code>v</code> is evaluated only once. 227511be35a1SLionel Sambuc 227611be35a1SLionel Sambuc 227711be35a1SLionel Sambuc<p> 227811be35a1SLionel SambucArguments have the following syntax: 227911be35a1SLionel Sambuc 228011be35a1SLionel Sambuc<pre> 2281*0a6a1f1dSLionel Sambuc args ::= ‘<b>(</b>’ [explist] ‘<b>)</b>’ 228211be35a1SLionel Sambuc args ::= tableconstructor 2283*0a6a1f1dSLionel Sambuc args ::= LiteralString 228411be35a1SLionel Sambuc</pre><p> 228511be35a1SLionel SambucAll argument expressions are evaluated before the call. 228611be35a1SLionel SambucA call of the form <code>f{<em>fields</em>}</code> is 228711be35a1SLionel Sambucsyntactic sugar for <code>f({<em>fields</em>})</code>; 228811be35a1SLionel Sambucthat is, the argument list is a single new table. 228911be35a1SLionel SambucA call of the form <code>f'<em>string</em>'</code> 229011be35a1SLionel Sambuc(or <code>f"<em>string</em>"</code> or <code>f[[<em>string</em>]]</code>) 229111be35a1SLionel Sambucis syntactic sugar for <code>f('<em>string</em>')</code>; 229211be35a1SLionel Sambucthat is, the argument list is a single literal string. 229311be35a1SLionel Sambuc 229411be35a1SLionel Sambuc 229511be35a1SLionel Sambuc<p> 2296*0a6a1f1dSLionel SambucA call of the form <code>return <em>functioncall</em></code> is called 229711be35a1SLionel Sambuca <em>tail call</em>. 229811be35a1SLionel SambucLua implements <em>proper tail calls</em> 229911be35a1SLionel Sambuc(or <em>proper tail recursion</em>): 230011be35a1SLionel Sambucin a tail call, 230111be35a1SLionel Sambucthe called function reuses the stack entry of the calling function. 230211be35a1SLionel SambucTherefore, there is no limit on the number of nested tail calls that 230311be35a1SLionel Sambuca program can execute. 230411be35a1SLionel SambucHowever, a tail call erases any debug information about the 230511be35a1SLionel Sambuccalling function. 230611be35a1SLionel SambucNote that a tail call only happens with a particular syntax, 230711be35a1SLionel Sambucwhere the <b>return</b> has one single function call as argument; 230811be35a1SLionel Sambucthis syntax makes the calling function return exactly 230911be35a1SLionel Sambucthe returns of the called function. 231011be35a1SLionel SambucSo, none of the following examples are tail calls: 231111be35a1SLionel Sambuc 231211be35a1SLionel Sambuc<pre> 231311be35a1SLionel Sambuc return (f(x)) -- results adjusted to 1 231411be35a1SLionel Sambuc return 2 * f(x) 231511be35a1SLionel Sambuc return x, f(x) -- additional results 231611be35a1SLionel Sambuc f(x); return -- results discarded 231711be35a1SLionel Sambuc return x or f(x) -- results adjusted to 1 231811be35a1SLionel Sambuc</pre> 231911be35a1SLionel Sambuc 232011be35a1SLionel Sambuc 232111be35a1SLionel Sambuc 232211be35a1SLionel Sambuc 2323*0a6a1f1dSLionel Sambuc<h3>3.4.11 – <a name="3.4.11">Function Definitions</a></h3> 232411be35a1SLionel Sambuc 232511be35a1SLionel Sambuc<p> 232611be35a1SLionel SambucThe syntax for function definition is 232711be35a1SLionel Sambuc 232811be35a1SLionel Sambuc<pre> 2329*0a6a1f1dSLionel Sambuc functiondef ::= <b>function</b> funcbody 2330*0a6a1f1dSLionel Sambuc funcbody ::= ‘<b>(</b>’ [parlist] ‘<b>)</b>’ block <b>end</b> 233111be35a1SLionel Sambuc</pre> 233211be35a1SLionel Sambuc 233311be35a1SLionel Sambuc<p> 233411be35a1SLionel SambucThe following syntactic sugar simplifies function definitions: 233511be35a1SLionel Sambuc 233611be35a1SLionel Sambuc<pre> 233711be35a1SLionel Sambuc stat ::= <b>function</b> funcname funcbody 233811be35a1SLionel Sambuc stat ::= <b>local</b> <b>function</b> Name funcbody 2339*0a6a1f1dSLionel Sambuc funcname ::= Name {‘<b>.</b>’ Name} [‘<b>:</b>’ Name] 234011be35a1SLionel Sambuc</pre><p> 234111be35a1SLionel SambucThe statement 234211be35a1SLionel Sambuc 234311be35a1SLionel Sambuc<pre> 234411be35a1SLionel Sambuc function f () <em>body</em> end 234511be35a1SLionel Sambuc</pre><p> 234611be35a1SLionel Sambuctranslates to 234711be35a1SLionel Sambuc 234811be35a1SLionel Sambuc<pre> 234911be35a1SLionel Sambuc f = function () <em>body</em> end 235011be35a1SLionel Sambuc</pre><p> 235111be35a1SLionel SambucThe statement 235211be35a1SLionel Sambuc 235311be35a1SLionel Sambuc<pre> 235411be35a1SLionel Sambuc function t.a.b.c.f () <em>body</em> end 235511be35a1SLionel Sambuc</pre><p> 235611be35a1SLionel Sambuctranslates to 235711be35a1SLionel Sambuc 235811be35a1SLionel Sambuc<pre> 235911be35a1SLionel Sambuc t.a.b.c.f = function () <em>body</em> end 236011be35a1SLionel Sambuc</pre><p> 236111be35a1SLionel SambucThe statement 236211be35a1SLionel Sambuc 236311be35a1SLionel Sambuc<pre> 236411be35a1SLionel Sambuc local function f () <em>body</em> end 236511be35a1SLionel Sambuc</pre><p> 236611be35a1SLionel Sambuctranslates to 236711be35a1SLionel Sambuc 236811be35a1SLionel Sambuc<pre> 236911be35a1SLionel Sambuc local f; f = function () <em>body</em> end 237011be35a1SLionel Sambuc</pre><p> 2371*0a6a1f1dSLionel Sambucnot to 237211be35a1SLionel Sambuc 237311be35a1SLionel Sambuc<pre> 237411be35a1SLionel Sambuc local f = function () <em>body</em> end 237511be35a1SLionel Sambuc</pre><p> 237611be35a1SLionel Sambuc(This only makes a difference when the body of the function 237711be35a1SLionel Sambuccontains references to <code>f</code>.) 237811be35a1SLionel Sambuc 237911be35a1SLionel Sambuc 238011be35a1SLionel Sambuc<p> 238111be35a1SLionel SambucA function definition is an executable expression, 238211be35a1SLionel Sambucwhose value has type <em>function</em>. 2383*0a6a1f1dSLionel SambucWhen Lua precompiles a chunk, 2384*0a6a1f1dSLionel Sambucall its function bodies are precompiled too. 238511be35a1SLionel SambucThen, whenever Lua executes the function definition, 238611be35a1SLionel Sambucthe function is <em>instantiated</em> (or <em>closed</em>). 238711be35a1SLionel SambucThis function instance (or <em>closure</em>) 238811be35a1SLionel Sambucis the final value of the expression. 238911be35a1SLionel Sambuc 239011be35a1SLionel Sambuc 239111be35a1SLionel Sambuc<p> 239211be35a1SLionel SambucParameters act as local variables that are 239311be35a1SLionel Sambucinitialized with the argument values: 239411be35a1SLionel Sambuc 239511be35a1SLionel Sambuc<pre> 2396*0a6a1f1dSLionel Sambuc parlist ::= namelist [‘<b>,</b>’ ‘<b>...</b>’] | ‘<b>...</b>’ 239711be35a1SLionel Sambuc</pre><p> 239811be35a1SLionel SambucWhen a function is called, 239911be35a1SLionel Sambucthe list of arguments is adjusted to 240011be35a1SLionel Sambucthe length of the list of parameters, 2401*0a6a1f1dSLionel Sambucunless the function is a <em>vararg function</em>, 2402*0a6a1f1dSLionel Sambucwhich is indicated by three dots ('<code>...</code>') 2403*0a6a1f1dSLionel Sambucat the end of its parameter list. 240411be35a1SLionel SambucA vararg function does not adjust its argument list; 240511be35a1SLionel Sambucinstead, it collects all extra arguments and supplies them 240611be35a1SLionel Sambucto the function through a <em>vararg expression</em>, 240711be35a1SLionel Sambucwhich is also written as three dots. 240811be35a1SLionel SambucThe value of this expression is a list of all actual extra arguments, 240911be35a1SLionel Sambucsimilar to a function with multiple results. 241011be35a1SLionel SambucIf a vararg expression is used inside another expression 241111be35a1SLionel Sambucor in the middle of a list of expressions, 241211be35a1SLionel Sambucthen its return list is adjusted to one element. 241311be35a1SLionel SambucIf the expression is used as the last element of a list of expressions, 241411be35a1SLionel Sambucthen no adjustment is made 241511be35a1SLionel Sambuc(unless that last expression is enclosed in parentheses). 241611be35a1SLionel Sambuc 241711be35a1SLionel Sambuc 241811be35a1SLionel Sambuc<p> 241911be35a1SLionel SambucAs an example, consider the following definitions: 242011be35a1SLionel Sambuc 242111be35a1SLionel Sambuc<pre> 242211be35a1SLionel Sambuc function f(a, b) end 242311be35a1SLionel Sambuc function g(a, b, ...) end 242411be35a1SLionel Sambuc function r() return 1,2,3 end 242511be35a1SLionel Sambuc</pre><p> 242611be35a1SLionel SambucThen, we have the following mapping from arguments to parameters and 242711be35a1SLionel Sambucto the vararg expression: 242811be35a1SLionel Sambuc 242911be35a1SLionel Sambuc<pre> 243011be35a1SLionel Sambuc CALL PARAMETERS 243111be35a1SLionel Sambuc 243211be35a1SLionel Sambuc f(3) a=3, b=nil 243311be35a1SLionel Sambuc f(3, 4) a=3, b=4 243411be35a1SLionel Sambuc f(3, 4, 5) a=3, b=4 243511be35a1SLionel Sambuc f(r(), 10) a=1, b=10 243611be35a1SLionel Sambuc f(r()) a=1, b=2 243711be35a1SLionel Sambuc 243811be35a1SLionel Sambuc g(3) a=3, b=nil, ... --> (nothing) 243911be35a1SLionel Sambuc g(3, 4) a=3, b=4, ... --> (nothing) 244011be35a1SLionel Sambuc g(3, 4, 5, 8) a=3, b=4, ... --> 5 8 244111be35a1SLionel Sambuc g(5, r()) a=5, b=1, ... --> 2 3 244211be35a1SLionel Sambuc</pre> 244311be35a1SLionel Sambuc 244411be35a1SLionel Sambuc<p> 2445*0a6a1f1dSLionel SambucResults are returned using the <b>return</b> statement (see <a href="#3.3.4">§3.3.4</a>). 244611be35a1SLionel SambucIf control reaches the end of a function 244711be35a1SLionel Sambucwithout encountering a <b>return</b> statement, 244811be35a1SLionel Sambucthen the function returns with no results. 244911be35a1SLionel Sambuc 245011be35a1SLionel Sambuc 245111be35a1SLionel Sambuc<p> 2452*0a6a1f1dSLionel Sambuc 2453*0a6a1f1dSLionel SambucThere is a system-dependent limit on the number of values 2454*0a6a1f1dSLionel Sambucthat a function may return. 2455*0a6a1f1dSLionel SambucThis limit is guaranteed to be larger than 1000. 2456*0a6a1f1dSLionel Sambuc 2457*0a6a1f1dSLionel Sambuc 2458*0a6a1f1dSLionel Sambuc<p> 245911be35a1SLionel SambucThe <em>colon</em> syntax 246011be35a1SLionel Sambucis used for defining <em>methods</em>, 246111be35a1SLionel Sambucthat is, functions that have an implicit extra parameter <code>self</code>. 246211be35a1SLionel SambucThus, the statement 246311be35a1SLionel Sambuc 246411be35a1SLionel Sambuc<pre> 246511be35a1SLionel Sambuc function t.a.b.c:f (<em>params</em>) <em>body</em> end 246611be35a1SLionel Sambuc</pre><p> 246711be35a1SLionel Sambucis syntactic sugar for 246811be35a1SLionel Sambuc 246911be35a1SLionel Sambuc<pre> 247011be35a1SLionel Sambuc t.a.b.c.f = function (self, <em>params</em>) <em>body</em> end 247111be35a1SLionel Sambuc</pre> 247211be35a1SLionel Sambuc 247311be35a1SLionel Sambuc 247411be35a1SLionel Sambuc 247511be35a1SLionel Sambuc 247611be35a1SLionel Sambuc 247711be35a1SLionel Sambuc 2478*0a6a1f1dSLionel Sambuc<h2>3.5 – <a name="3.5">Visibility Rules</a></h2> 247911be35a1SLionel Sambuc 248011be35a1SLionel Sambuc<p> 248111be35a1SLionel Sambuc 248211be35a1SLionel SambucLua is a lexically scoped language. 2483*0a6a1f1dSLionel SambucThe scope of a local variable begins at the first statement after 2484*0a6a1f1dSLionel Sambucits declaration and lasts until the last non-void statement 2485*0a6a1f1dSLionel Sambucof the innermost block that includes the declaration. 248611be35a1SLionel SambucConsider the following example: 248711be35a1SLionel Sambuc 248811be35a1SLionel Sambuc<pre> 248911be35a1SLionel Sambuc x = 10 -- global variable 249011be35a1SLionel Sambuc do -- new block 249111be35a1SLionel Sambuc local x = x -- new 'x', with value 10 249211be35a1SLionel Sambuc print(x) --> 10 249311be35a1SLionel Sambuc x = x+1 249411be35a1SLionel Sambuc do -- another block 249511be35a1SLionel Sambuc local x = x+1 -- another 'x' 249611be35a1SLionel Sambuc print(x) --> 12 249711be35a1SLionel Sambuc end 249811be35a1SLionel Sambuc print(x) --> 11 249911be35a1SLionel Sambuc end 250011be35a1SLionel Sambuc print(x) --> 10 (the global one) 250111be35a1SLionel Sambuc</pre> 250211be35a1SLionel Sambuc 250311be35a1SLionel Sambuc<p> 250411be35a1SLionel SambucNotice that, in a declaration like <code>local x = x</code>, 250511be35a1SLionel Sambucthe new <code>x</code> being declared is not in scope yet, 250611be35a1SLionel Sambucand so the second <code>x</code> refers to the outside variable. 250711be35a1SLionel Sambuc 250811be35a1SLionel Sambuc 250911be35a1SLionel Sambuc<p> 251011be35a1SLionel SambucBecause of the lexical scoping rules, 251111be35a1SLionel Sambuclocal variables can be freely accessed by functions 251211be35a1SLionel Sambucdefined inside their scope. 251311be35a1SLionel SambucA local variable used by an inner function is called 251411be35a1SLionel Sambucan <em>upvalue</em>, or <em>external local variable</em>, 251511be35a1SLionel Sambucinside the inner function. 251611be35a1SLionel Sambuc 251711be35a1SLionel Sambuc 251811be35a1SLionel Sambuc<p> 251911be35a1SLionel SambucNotice that each execution of a <b>local</b> statement 252011be35a1SLionel Sambucdefines new local variables. 252111be35a1SLionel SambucConsider the following example: 252211be35a1SLionel Sambuc 252311be35a1SLionel Sambuc<pre> 252411be35a1SLionel Sambuc a = {} 252511be35a1SLionel Sambuc local x = 20 252611be35a1SLionel Sambuc for i=1,10 do 252711be35a1SLionel Sambuc local y = 0 252811be35a1SLionel Sambuc a[i] = function () y=y+1; return x+y end 252911be35a1SLionel Sambuc end 253011be35a1SLionel Sambuc</pre><p> 253111be35a1SLionel SambucThe loop creates ten closures 253211be35a1SLionel Sambuc(that is, ten instances of the anonymous function). 253311be35a1SLionel SambucEach of these closures uses a different <code>y</code> variable, 253411be35a1SLionel Sambucwhile all of them share the same <code>x</code>. 253511be35a1SLionel Sambuc 253611be35a1SLionel Sambuc 253711be35a1SLionel Sambuc 253811be35a1SLionel Sambuc 253911be35a1SLionel Sambuc 2540*0a6a1f1dSLionel Sambuc<h1>4 – <a name="4">The Application Program Interface</a></h1> 254111be35a1SLionel Sambuc 254211be35a1SLionel Sambuc<p> 254311be35a1SLionel Sambuc 254411be35a1SLionel SambucThis section describes the C API for Lua, that is, 254511be35a1SLionel Sambucthe set of C functions available to the host program to communicate 254611be35a1SLionel Sambucwith Lua. 254711be35a1SLionel SambucAll API functions and related types and constants 254811be35a1SLionel Sambucare declared in the header file <a name="pdf-lua.h"><code>lua.h</code></a>. 254911be35a1SLionel Sambuc 255011be35a1SLionel Sambuc 255111be35a1SLionel Sambuc<p> 255211be35a1SLionel SambucEven when we use the term "function", 255311be35a1SLionel Sambucany facility in the API may be provided as a macro instead. 2554*0a6a1f1dSLionel SambucExcept where stated otherwise, 2555*0a6a1f1dSLionel Sambucall such macros use each of their arguments exactly once 255611be35a1SLionel Sambuc(except for the first argument, which is always a Lua state), 255711be35a1SLionel Sambucand so do not generate any hidden side-effects. 255811be35a1SLionel Sambuc 255911be35a1SLionel Sambuc 256011be35a1SLionel Sambuc<p> 256111be35a1SLionel SambucAs in most C libraries, 256211be35a1SLionel Sambucthe Lua API functions do not check their arguments for validity or consistency. 256311be35a1SLionel SambucHowever, you can change this behavior by compiling Lua 2564*0a6a1f1dSLionel Sambucwith the macro <a name="pdf-LUA_USE_APICHECK"><code>LUA_USE_APICHECK</code></a> defined. 256511be35a1SLionel Sambuc 256611be35a1SLionel Sambuc 256711be35a1SLionel Sambuc 2568*0a6a1f1dSLionel Sambuc<h2>4.1 – <a name="4.1">The Stack</a></h2> 256911be35a1SLionel Sambuc 257011be35a1SLionel Sambuc<p> 257111be35a1SLionel SambucLua uses a <em>virtual stack</em> to pass values to and from C. 257211be35a1SLionel SambucEach element in this stack represents a Lua value 257311be35a1SLionel Sambuc(<b>nil</b>, number, string, etc.). 257411be35a1SLionel Sambuc 257511be35a1SLionel Sambuc 257611be35a1SLionel Sambuc<p> 257711be35a1SLionel SambucWhenever Lua calls C, the called function gets a new stack, 257811be35a1SLionel Sambucwhich is independent of previous stacks and of stacks of 257911be35a1SLionel SambucC functions that are still active. 258011be35a1SLionel SambucThis stack initially contains any arguments to the C function 258111be35a1SLionel Sambucand it is where the C function pushes its results 258211be35a1SLionel Sambucto be returned to the caller (see <a href="#lua_CFunction"><code>lua_CFunction</code></a>). 258311be35a1SLionel Sambuc 258411be35a1SLionel Sambuc 258511be35a1SLionel Sambuc<p> 258611be35a1SLionel SambucFor convenience, 258711be35a1SLionel Sambucmost query operations in the API do not follow a strict stack discipline. 258811be35a1SLionel SambucInstead, they can refer to any element in the stack 258911be35a1SLionel Sambucby using an <em>index</em>: 2590*0a6a1f1dSLionel SambucA positive index represents an absolute stack position 259111be35a1SLionel Sambuc(starting at 1); 2592*0a6a1f1dSLionel Sambuca negative index represents an offset relative to the top of the stack. 259311be35a1SLionel SambucMore specifically, if the stack has <em>n</em> elements, 259411be35a1SLionel Sambucthen index 1 represents the first element 259511be35a1SLionel Sambuc(that is, the element that was pushed onto the stack first) 259611be35a1SLionel Sambucand 259711be35a1SLionel Sambucindex <em>n</em> represents the last element; 259811be35a1SLionel Sambucindex -1 also represents the last element 259911be35a1SLionel Sambuc(that is, the element at the top) 260011be35a1SLionel Sambucand index <em>-n</em> represents the first element. 260111be35a1SLionel Sambuc 260211be35a1SLionel Sambuc 260311be35a1SLionel Sambuc 260411be35a1SLionel Sambuc 260511be35a1SLionel Sambuc 2606*0a6a1f1dSLionel Sambuc<h2>4.2 – <a name="4.2">Stack Size</a></h2> 260711be35a1SLionel Sambuc 260811be35a1SLionel Sambuc<p> 2609*0a6a1f1dSLionel SambucWhen you interact with the Lua API, 261011be35a1SLionel Sambucyou are responsible for ensuring consistency. 261111be35a1SLionel SambucIn particular, 261211be35a1SLionel Sambuc<em>you are responsible for controlling stack overflow</em>. 261311be35a1SLionel SambucYou can use the function <a href="#lua_checkstack"><code>lua_checkstack</code></a> 2614*0a6a1f1dSLionel Sambucto ensure that the stack has enough space for pushing new elements. 261511be35a1SLionel Sambuc 261611be35a1SLionel Sambuc 261711be35a1SLionel Sambuc<p> 261811be35a1SLionel SambucWhenever Lua calls C, 2619*0a6a1f1dSLionel Sambucit ensures that the stack has space for 2620*0a6a1f1dSLionel Sambucat least <a name="pdf-LUA_MINSTACK"><code>LUA_MINSTACK</code></a> extra slots. 262111be35a1SLionel Sambuc<code>LUA_MINSTACK</code> is defined as 20, 262211be35a1SLionel Sambucso that usually you do not have to worry about stack space 262311be35a1SLionel Sambucunless your code has loops pushing elements onto the stack. 262411be35a1SLionel Sambuc 262511be35a1SLionel Sambuc 262611be35a1SLionel Sambuc<p> 2627*0a6a1f1dSLionel SambucWhen you call a Lua function 2628*0a6a1f1dSLionel Sambucwithout a fixed number of results (see <a href="#lua_call"><code>lua_call</code></a>), 2629*0a6a1f1dSLionel SambucLua ensures that the stack has enough space for all results, 2630*0a6a1f1dSLionel Sambucbut it does not ensure any extra space. 2631*0a6a1f1dSLionel SambucSo, before pushing anything in the stack after such a call 2632*0a6a1f1dSLionel Sambucyou should use <a href="#lua_checkstack"><code>lua_checkstack</code></a>. 263311be35a1SLionel Sambuc 263411be35a1SLionel Sambuc 263511be35a1SLionel Sambuc 263611be35a1SLionel Sambuc 263711be35a1SLionel Sambuc 2638*0a6a1f1dSLionel Sambuc<h2>4.3 – <a name="4.3">Valid and Acceptable Indices</a></h2> 263911be35a1SLionel Sambuc 264011be35a1SLionel Sambuc<p> 2641*0a6a1f1dSLionel SambucAny function in the API that receives stack indices 2642*0a6a1f1dSLionel Sambucworks only with <em>valid indices</em> or <em>acceptable indices</em>. 264311be35a1SLionel Sambuc 264411be35a1SLionel Sambuc 264511be35a1SLionel Sambuc<p> 2646*0a6a1f1dSLionel SambucA <em>valid index</em> is an index that refers to a 2647*0a6a1f1dSLionel Sambucposition that stores a modifiable Lua value. 2648*0a6a1f1dSLionel SambucIt comprises stack indices between 1 and the stack top 2649*0a6a1f1dSLionel Sambuc(<code>1 ≤ abs(index) ≤ top</code>) 2650*0a6a1f1dSLionel Sambuc 2651*0a6a1f1dSLionel Sambucplus <em>pseudo-indices</em>, 2652*0a6a1f1dSLionel Sambucwhich represent some positions that are accessible to C code 2653*0a6a1f1dSLionel Sambucbut that are not in the stack. 2654*0a6a1f1dSLionel SambucPseudo-indices are used to access the registry (see <a href="#4.5">§4.5</a>) 2655*0a6a1f1dSLionel Sambucand the upvalues of a C function (see <a href="#4.4">§4.4</a>). 265611be35a1SLionel Sambuc 265711be35a1SLionel Sambuc 265811be35a1SLionel Sambuc<p> 2659*0a6a1f1dSLionel SambucFunctions that do not need a specific mutable position, 2660*0a6a1f1dSLionel Sambucbut only a value (e.g., query functions), 2661*0a6a1f1dSLionel Sambuccan be called with acceptable indices. 2662*0a6a1f1dSLionel SambucAn <em>acceptable index</em> can be any valid index, 2663*0a6a1f1dSLionel Sambucbut it also can be any positive index after the stack top 2664*0a6a1f1dSLionel Sambucwithin the space allocated for the stack, 2665*0a6a1f1dSLionel Sambucthat is, indices up to the stack size. 2666*0a6a1f1dSLionel Sambuc(Note that 0 is never an acceptable index.) 2667*0a6a1f1dSLionel SambucExcept when noted otherwise, 2668*0a6a1f1dSLionel Sambucfunctions in the API work with acceptable indices. 266911be35a1SLionel Sambuc 2670*0a6a1f1dSLionel Sambuc 2671*0a6a1f1dSLionel Sambuc<p> 2672*0a6a1f1dSLionel SambucAcceptable indices serve to avoid extra tests 2673*0a6a1f1dSLionel Sambucagainst the stack top when querying the stack. 2674*0a6a1f1dSLionel SambucFor instance, a C function can query its third argument 2675*0a6a1f1dSLionel Sambucwithout the need to first check whether there is a third argument, 2676*0a6a1f1dSLionel Sambucthat is, without the need to check whether 3 is a valid index. 2677*0a6a1f1dSLionel Sambuc 2678*0a6a1f1dSLionel Sambuc 2679*0a6a1f1dSLionel Sambuc<p> 2680*0a6a1f1dSLionel SambucFor functions that can be called with acceptable indices, 2681*0a6a1f1dSLionel Sambucany non-valid index is treated as if it 2682*0a6a1f1dSLionel Sambuccontains a value of a virtual type <a name="pdf-LUA_TNONE"><code>LUA_TNONE</code></a>, 2683*0a6a1f1dSLionel Sambucwhich behaves like a nil value. 268411be35a1SLionel Sambuc 268511be35a1SLionel Sambuc 268611be35a1SLionel Sambuc 268711be35a1SLionel Sambuc 2688*0a6a1f1dSLionel Sambuc 2689*0a6a1f1dSLionel Sambuc<h2>4.4 – <a name="4.4">C Closures</a></h2> 269011be35a1SLionel Sambuc 269111be35a1SLionel Sambuc<p> 269211be35a1SLionel SambucWhen a C function is created, 269311be35a1SLionel Sambucit is possible to associate some values with it, 2694*0a6a1f1dSLionel Sambucthus creating a <em>C closure</em> 2695*0a6a1f1dSLionel Sambuc(see <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a>); 269611be35a1SLionel Sambucthese values are called <em>upvalues</em> and are 2697*0a6a1f1dSLionel Sambucaccessible to the function whenever it is called. 269811be35a1SLionel Sambuc 269911be35a1SLionel Sambuc 270011be35a1SLionel Sambuc<p> 270111be35a1SLionel SambucWhenever a C function is called, 270211be35a1SLionel Sambucits upvalues are located at specific pseudo-indices. 270311be35a1SLionel SambucThese pseudo-indices are produced by the macro 2704*0a6a1f1dSLionel Sambuc<a href="#lua_upvalueindex"><code>lua_upvalueindex</code></a>. 2705*0a6a1f1dSLionel SambucThe first upvalue associated with a function is at index 270611be35a1SLionel Sambuc<code>lua_upvalueindex(1)</code>, and so on. 270711be35a1SLionel SambucAny access to <code>lua_upvalueindex(<em>n</em>)</code>, 270811be35a1SLionel Sambucwhere <em>n</em> is greater than the number of upvalues of the 270911be35a1SLionel Sambuccurrent function (but not greater than 256), 2710*0a6a1f1dSLionel Sambucproduces an acceptable but invalid index. 271111be35a1SLionel Sambuc 271211be35a1SLionel Sambuc 271311be35a1SLionel Sambuc 271411be35a1SLionel Sambuc 271511be35a1SLionel Sambuc 2716*0a6a1f1dSLionel Sambuc<h2>4.5 – <a name="4.5">Registry</a></h2> 271711be35a1SLionel Sambuc 271811be35a1SLionel Sambuc<p> 271911be35a1SLionel SambucLua provides a <em>registry</em>, 2720*0a6a1f1dSLionel Sambuca predefined table that can be used by any C code to 2721*0a6a1f1dSLionel Sambucstore whatever Lua values it needs to store. 2722*0a6a1f1dSLionel SambucThe registry table is always located at pseudo-index 272311be35a1SLionel Sambuc<a name="pdf-LUA_REGISTRYINDEX"><code>LUA_REGISTRYINDEX</code></a>. 272411be35a1SLionel SambucAny C library can store data into this table, 2725*0a6a1f1dSLionel Sambucbut it must take care to choose keys 2726*0a6a1f1dSLionel Sambucthat are different from those used 272711be35a1SLionel Sambucby other libraries, to avoid collisions. 2728*0a6a1f1dSLionel SambucTypically, you should use as key a string containing your library name, 2729*0a6a1f1dSLionel Sambucor a light userdata with the address of a C object in your code, 2730*0a6a1f1dSLionel Sambucor any Lua object created by your code. 2731*0a6a1f1dSLionel SambucAs with variable names, 2732*0a6a1f1dSLionel Sambucstring keys starting with an underscore followed by 2733*0a6a1f1dSLionel Sambucuppercase letters are reserved for Lua. 273411be35a1SLionel Sambuc 273511be35a1SLionel Sambuc 273611be35a1SLionel Sambuc<p> 2737*0a6a1f1dSLionel SambucThe integer keys in the registry are used 2738*0a6a1f1dSLionel Sambucby the reference mechanism (see <a href="#luaL_ref"><code>luaL_ref</code></a>) 2739*0a6a1f1dSLionel Sambucand by some predefined values. 2740*0a6a1f1dSLionel SambucTherefore, integer keys must not be used for other purposes. 2741*0a6a1f1dSLionel Sambuc 2742*0a6a1f1dSLionel Sambuc 2743*0a6a1f1dSLionel Sambuc<p> 2744*0a6a1f1dSLionel SambucWhen you create a new Lua state, 2745*0a6a1f1dSLionel Sambucits registry comes with some predefined values. 2746*0a6a1f1dSLionel SambucThese predefined values are indexed with integer keys 2747*0a6a1f1dSLionel Sambucdefined as constants in <code>lua.h</code>. 2748*0a6a1f1dSLionel SambucThe following constants are defined: 2749*0a6a1f1dSLionel Sambuc 2750*0a6a1f1dSLionel Sambuc<ul> 2751*0a6a1f1dSLionel Sambuc<li><b><a name="pdf-LUA_RIDX_MAINTHREAD"><code>LUA_RIDX_MAINTHREAD</code></a>: </b> At this index the registry has 2752*0a6a1f1dSLionel Sambucthe main thread of the state. 2753*0a6a1f1dSLionel Sambuc(The main thread is the one created together with the state.) 2754*0a6a1f1dSLionel Sambuc</li> 2755*0a6a1f1dSLionel Sambuc 2756*0a6a1f1dSLionel Sambuc<li><b><a name="pdf-LUA_RIDX_GLOBALS"><code>LUA_RIDX_GLOBALS</code></a>: </b> At this index the registry has 2757*0a6a1f1dSLionel Sambucthe global environment. 2758*0a6a1f1dSLionel Sambuc</li> 2759*0a6a1f1dSLionel Sambuc</ul> 276011be35a1SLionel Sambuc 276111be35a1SLionel Sambuc 276211be35a1SLionel Sambuc 276311be35a1SLionel Sambuc 2764*0a6a1f1dSLionel Sambuc<h2>4.6 – <a name="4.6">Error Handling in C</a></h2> 276511be35a1SLionel Sambuc 276611be35a1SLionel Sambuc<p> 276711be35a1SLionel SambucInternally, Lua uses the C <code>longjmp</code> facility to handle errors. 2768*0a6a1f1dSLionel Sambuc(Lua will use exceptions if you compile it as C++; 2769*0a6a1f1dSLionel Sambucsearch for <code>LUAI_THROW</code> in the source code for details.) 277011be35a1SLionel SambucWhen Lua faces any error 2771*0a6a1f1dSLionel Sambuc(such as a memory allocation error, type errors, syntax errors, 277211be35a1SLionel Sambucand runtime errors) 277311be35a1SLionel Sambucit <em>raises</em> an error; 277411be35a1SLionel Sambucthat is, it does a long jump. 277511be35a1SLionel SambucA <em>protected environment</em> uses <code>setjmp</code> 2776*0a6a1f1dSLionel Sambucto set a recovery point; 2777*0a6a1f1dSLionel Sambucany error jumps to the most recent active recovery point. 277811be35a1SLionel Sambuc 277911be35a1SLionel Sambuc 278011be35a1SLionel Sambuc<p> 2781*0a6a1f1dSLionel SambucIf an error happens outside any protected environment, 2782*0a6a1f1dSLionel SambucLua calls a <em>panic function</em> (see <a href="#lua_atpanic"><code>lua_atpanic</code></a>) 2783*0a6a1f1dSLionel Sambucand then calls <code>abort</code>, 2784*0a6a1f1dSLionel Sambucthus exiting the host application. 2785*0a6a1f1dSLionel SambucYour panic function can avoid this exit by 2786*0a6a1f1dSLionel Sambucnever returning 2787*0a6a1f1dSLionel Sambuc(e.g., doing a long jump to your own recovery point outside Lua). 2788*0a6a1f1dSLionel Sambuc 2789*0a6a1f1dSLionel Sambuc 2790*0a6a1f1dSLionel Sambuc<p> 2791*0a6a1f1dSLionel SambucThe panic function runs as if it were a message handler (see <a href="#2.3">§2.3</a>); 2792*0a6a1f1dSLionel Sambucin particular, the error message is at the top of the stack. 2793*0a6a1f1dSLionel SambucHowever, there is no guarantee about stack space. 2794*0a6a1f1dSLionel SambucTo push anything on the stack, 2795*0a6a1f1dSLionel Sambucthe panic function must first check the available space (see <a href="#4.2">§4.2</a>). 2796*0a6a1f1dSLionel Sambuc 2797*0a6a1f1dSLionel Sambuc 2798*0a6a1f1dSLionel Sambuc<p> 2799*0a6a1f1dSLionel SambucMost functions in the API can raise an error, 280011be35a1SLionel Sambucfor instance due to a memory allocation error. 280111be35a1SLionel SambucThe documentation for each function indicates whether 2802*0a6a1f1dSLionel Sambucit can raise errors. 280311be35a1SLionel Sambuc 280411be35a1SLionel Sambuc 280511be35a1SLionel Sambuc<p> 2806*0a6a1f1dSLionel SambucInside a C function you can raise an error by calling <a href="#lua_error"><code>lua_error</code></a>. 280711be35a1SLionel Sambuc 280811be35a1SLionel Sambuc 280911be35a1SLionel Sambuc 281011be35a1SLionel Sambuc 281111be35a1SLionel Sambuc 2812*0a6a1f1dSLionel Sambuc<h2>4.7 – <a name="4.7">Handling Yields in C</a></h2> 2813*0a6a1f1dSLionel Sambuc 2814*0a6a1f1dSLionel Sambuc<p> 2815*0a6a1f1dSLionel SambucInternally, Lua uses the C <code>longjmp</code> facility to yield a coroutine. 2816*0a6a1f1dSLionel SambucTherefore, if a C function <code>foo</code> calls an API function 2817*0a6a1f1dSLionel Sambucand this API function yields 2818*0a6a1f1dSLionel Sambuc(directly or indirectly by calling another function that yields), 2819*0a6a1f1dSLionel SambucLua cannot return to <code>foo</code> any more, 2820*0a6a1f1dSLionel Sambucbecause the <code>longjmp</code> removes its frame from the C stack. 2821*0a6a1f1dSLionel Sambuc 2822*0a6a1f1dSLionel Sambuc 2823*0a6a1f1dSLionel Sambuc<p> 2824*0a6a1f1dSLionel SambucTo avoid this kind of problem, 2825*0a6a1f1dSLionel SambucLua raises an error whenever it tries to yield across an API call, 2826*0a6a1f1dSLionel Sambucexcept for three functions: 2827*0a6a1f1dSLionel Sambuc<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>. 2828*0a6a1f1dSLionel SambucAll those functions receive a <em>continuation function</em> 2829*0a6a1f1dSLionel Sambuc(as a parameter named <code>k</code>) to continue execution after a yield. 2830*0a6a1f1dSLionel Sambuc 2831*0a6a1f1dSLionel Sambuc 2832*0a6a1f1dSLionel Sambuc<p> 2833*0a6a1f1dSLionel SambucWe need to set some terminology to explain continuations. 2834*0a6a1f1dSLionel SambucWe have a C function called from Lua which we will call 2835*0a6a1f1dSLionel Sambucthe <em>original function</em>. 2836*0a6a1f1dSLionel SambucThis original function then calls one of those three functions in the C API, 2837*0a6a1f1dSLionel Sambucwhich we will call the <em>callee function</em>, 2838*0a6a1f1dSLionel Sambucthat then yields the current thread. 2839*0a6a1f1dSLionel Sambuc(This can happen when the callee function is <a href="#lua_yieldk"><code>lua_yieldk</code></a>, 2840*0a6a1f1dSLionel Sambucor 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> 2841*0a6a1f1dSLionel Sambucand the function called by them yields.) 2842*0a6a1f1dSLionel Sambuc 2843*0a6a1f1dSLionel Sambuc 2844*0a6a1f1dSLionel Sambuc<p> 2845*0a6a1f1dSLionel SambucSuppose the running thread yields while executing the callee function. 2846*0a6a1f1dSLionel SambucAfter the thread resumes, 2847*0a6a1f1dSLionel Sambucit eventually will finish running the callee function. 2848*0a6a1f1dSLionel SambucHowever, 2849*0a6a1f1dSLionel Sambucthe callee function cannot return to the original function, 2850*0a6a1f1dSLionel Sambucbecause its frame in the C stack was destroyed by the yield. 2851*0a6a1f1dSLionel SambucInstead, Lua calls a <em>continuation function</em>, 2852*0a6a1f1dSLionel Sambucwhich was given as an argument to the callee function. 2853*0a6a1f1dSLionel SambucAs the name implies, 2854*0a6a1f1dSLionel Sambucthe continuation function should continue the task 2855*0a6a1f1dSLionel Sambucof the original function. 2856*0a6a1f1dSLionel Sambuc 2857*0a6a1f1dSLionel Sambuc 2858*0a6a1f1dSLionel Sambuc<p> 2859*0a6a1f1dSLionel SambucAs an illustration, consider the following function: 2860*0a6a1f1dSLionel Sambuc 2861*0a6a1f1dSLionel Sambuc<pre> 2862*0a6a1f1dSLionel Sambuc int original_function (lua_State *L) { 2863*0a6a1f1dSLionel Sambuc ... /* code 1 */ 2864*0a6a1f1dSLionel Sambuc status = lua_pcall(L, n, m, h); /* calls Lua */ 2865*0a6a1f1dSLionel Sambuc ... /* code 2 */ 2866*0a6a1f1dSLionel Sambuc } 2867*0a6a1f1dSLionel Sambuc</pre><p> 2868*0a6a1f1dSLionel SambucNow we want to allow 2869*0a6a1f1dSLionel Sambucthe Lua code being run by <a href="#lua_pcall"><code>lua_pcall</code></a> to yield. 2870*0a6a1f1dSLionel SambucFirst, we can rewrite our function like here: 2871*0a6a1f1dSLionel Sambuc 2872*0a6a1f1dSLionel Sambuc<pre> 2873*0a6a1f1dSLionel Sambuc int k (lua_State *L, int status, lua_KContext ctx) { 2874*0a6a1f1dSLionel Sambuc ... /* code 2 */ 2875*0a6a1f1dSLionel Sambuc } 2876*0a6a1f1dSLionel Sambuc 2877*0a6a1f1dSLionel Sambuc int original_function (lua_State *L) { 2878*0a6a1f1dSLionel Sambuc ... /* code 1 */ 2879*0a6a1f1dSLionel Sambuc return k(L, lua_pcall(L, n, m, h), ctx); 2880*0a6a1f1dSLionel Sambuc } 2881*0a6a1f1dSLionel Sambuc</pre><p> 2882*0a6a1f1dSLionel SambucIn the above code, 2883*0a6a1f1dSLionel Sambucthe new function <code>k</code> is a 2884*0a6a1f1dSLionel Sambuc<em>continuation function</em> (with type <a href="#lua_KFunction"><code>lua_KFunction</code></a>), 2885*0a6a1f1dSLionel Sambucwhich should do all the work that the original function 2886*0a6a1f1dSLionel Sambucwas doing after calling <a href="#lua_pcall"><code>lua_pcall</code></a>. 2887*0a6a1f1dSLionel SambucNow, we must inform Lua that it must call <code>k</code> if the Lua code 2888*0a6a1f1dSLionel Sambucbeing executed by <a href="#lua_pcall"><code>lua_pcall</code></a> gets interrupted in some way 2889*0a6a1f1dSLionel Sambuc(errors or yielding), 2890*0a6a1f1dSLionel Sambucso we rewrite the code as here, 2891*0a6a1f1dSLionel Sambucreplacing <a href="#lua_pcall"><code>lua_pcall</code></a> by <a href="#lua_pcallk"><code>lua_pcallk</code></a>: 2892*0a6a1f1dSLionel Sambuc 2893*0a6a1f1dSLionel Sambuc<pre> 2894*0a6a1f1dSLionel Sambuc int original_function (lua_State *L) { 2895*0a6a1f1dSLionel Sambuc ... /* code 1 */ 2896*0a6a1f1dSLionel Sambuc return k(L, lua_pcallk(L, n, m, h, ctx2, k), ctx1); 2897*0a6a1f1dSLionel Sambuc } 2898*0a6a1f1dSLionel Sambuc</pre><p> 2899*0a6a1f1dSLionel SambucNote the external, explicit call to the continuation: 2900*0a6a1f1dSLionel SambucLua will call the continuation only if needed, that is, 2901*0a6a1f1dSLionel Sambucin case of errors or resuming after a yield. 2902*0a6a1f1dSLionel SambucIf the called function returns normally without ever yielding, 2903*0a6a1f1dSLionel Sambuc<a href="#lua_pcallk"><code>lua_pcallk</code></a> (and <a href="#lua_callk"><code>lua_callk</code></a>) will also return normally. 2904*0a6a1f1dSLionel Sambuc(Of course, instead of calling the continuation in that case, 2905*0a6a1f1dSLionel Sambucyou can do the equivalent work directly inside the original function.) 2906*0a6a1f1dSLionel Sambuc 2907*0a6a1f1dSLionel Sambuc 2908*0a6a1f1dSLionel Sambuc<p> 2909*0a6a1f1dSLionel SambucBesides the Lua state, 2910*0a6a1f1dSLionel Sambucthe continuation function has two other parameters: 2911*0a6a1f1dSLionel Sambucthe final status of the call plus the context value (<code>ctx</code>) that 2912*0a6a1f1dSLionel Sambucwas passed originally to <a href="#lua_pcallk"><code>lua_pcallk</code></a>. 2913*0a6a1f1dSLionel Sambuc(Lua does not use this context value; 2914*0a6a1f1dSLionel Sambucit only passes this value from the original function to the 2915*0a6a1f1dSLionel Sambuccontinuation function.) 2916*0a6a1f1dSLionel SambucFor <a href="#lua_pcallk"><code>lua_pcallk</code></a>, 2917*0a6a1f1dSLionel Sambucthe status is the same value that would be returned by <a href="#lua_pcallk"><code>lua_pcallk</code></a>, 2918*0a6a1f1dSLionel Sambucexcept that it is <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> when being executed after a yield 2919*0a6a1f1dSLionel Sambuc(instead of <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>). 2920*0a6a1f1dSLionel SambucFor <a href="#lua_yieldk"><code>lua_yieldk</code></a> and <a href="#lua_callk"><code>lua_callk</code></a>, 2921*0a6a1f1dSLionel Sambucthe status is always <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> when Lua calls the continuation. 2922*0a6a1f1dSLionel Sambuc(For these two functions, 2923*0a6a1f1dSLionel SambucLua will not call the continuation in case of errors, 2924*0a6a1f1dSLionel Sambucbecause they do not handle errors.) 2925*0a6a1f1dSLionel SambucSimilarly, when using <a href="#lua_callk"><code>lua_callk</code></a>, 2926*0a6a1f1dSLionel Sambucyou should call the continuation function 2927*0a6a1f1dSLionel Sambucwith <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> as the status. 2928*0a6a1f1dSLionel Sambuc(For <a href="#lua_yieldk"><code>lua_yieldk</code></a>, there is not much point in calling 2929*0a6a1f1dSLionel Sambucdirectly the continuation function, 2930*0a6a1f1dSLionel Sambucbecause <a href="#lua_yieldk"><code>lua_yieldk</code></a> usually does not return.) 2931*0a6a1f1dSLionel Sambuc 2932*0a6a1f1dSLionel Sambuc 2933*0a6a1f1dSLionel Sambuc<p> 2934*0a6a1f1dSLionel SambucLua treats the continuation function as if it were the original function. 2935*0a6a1f1dSLionel SambucThe continuation function receives the same Lua stack 2936*0a6a1f1dSLionel Sambucfrom the original function, 2937*0a6a1f1dSLionel Sambucin the same state it would be if the callee function had returned. 2938*0a6a1f1dSLionel Sambuc(For instance, 2939*0a6a1f1dSLionel Sambucafter a <a href="#lua_callk"><code>lua_callk</code></a> the function and its arguments are 2940*0a6a1f1dSLionel Sambucremoved from the stack and replaced by the results from the call.) 2941*0a6a1f1dSLionel SambucIt also has the same upvalues. 2942*0a6a1f1dSLionel SambucWhatever it returns is handled by Lua as if it were the return 2943*0a6a1f1dSLionel Sambucof the original function. 2944*0a6a1f1dSLionel Sambuc 2945*0a6a1f1dSLionel Sambuc 2946*0a6a1f1dSLionel Sambuc 2947*0a6a1f1dSLionel Sambuc 2948*0a6a1f1dSLionel Sambuc 2949*0a6a1f1dSLionel Sambuc<h2>4.8 – <a name="4.8">Functions and Types</a></h2> 295011be35a1SLionel Sambuc 295111be35a1SLionel Sambuc<p> 295211be35a1SLionel SambucHere we list all functions and types from the C API in 295311be35a1SLionel Sambucalphabetical order. 295411be35a1SLionel SambucEach function has an indicator like this: 295511be35a1SLionel Sambuc<span class="apii">[-o, +p, <em>x</em>]</span> 295611be35a1SLionel Sambuc 295711be35a1SLionel Sambuc 295811be35a1SLionel Sambuc<p> 295911be35a1SLionel SambucThe first field, <code>o</code>, 296011be35a1SLionel Sambucis how many elements the function pops from the stack. 296111be35a1SLionel SambucThe second field, <code>p</code>, 296211be35a1SLionel Sambucis how many elements the function pushes onto the stack. 296311be35a1SLionel Sambuc(Any function always pushes its results after popping its arguments.) 296411be35a1SLionel SambucA field in the form <code>x|y</code> means the function can push (or pop) 296511be35a1SLionel Sambuc<code>x</code> or <code>y</code> elements, 296611be35a1SLionel Sambucdepending on the situation; 296711be35a1SLionel Sambucan interrogation mark '<code>?</code>' means that 296811be35a1SLionel Sambucwe cannot know how many elements the function pops/pushes 296911be35a1SLionel Sambucby looking only at its arguments 297011be35a1SLionel Sambuc(e.g., they may depend on what is on the stack). 297111be35a1SLionel SambucThe third field, <code>x</code>, 2972*0a6a1f1dSLionel Sambuctells whether the function may raise errors: 2973*0a6a1f1dSLionel Sambuc'<code>-</code>' means the function never raises any error; 2974*0a6a1f1dSLionel Sambuc'<code>e</code>' means the function may raise errors; 2975*0a6a1f1dSLionel Sambuc'<code>v</code>' means the function may raise an error on purpose. 2976*0a6a1f1dSLionel Sambuc 2977*0a6a1f1dSLionel Sambuc 2978*0a6a1f1dSLionel Sambuc 2979*0a6a1f1dSLionel Sambuc<hr><h3><a name="lua_absindex"><code>lua_absindex</code></a></h3><p> 2980*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 2981*0a6a1f1dSLionel Sambuc<pre>int lua_absindex (lua_State *L, int idx);</pre> 2982*0a6a1f1dSLionel Sambuc 2983*0a6a1f1dSLionel Sambuc<p> 2984*0a6a1f1dSLionel SambucConverts the acceptable index <code>idx</code> 2985*0a6a1f1dSLionel Sambucinto an equivalent absolute index 2986*0a6a1f1dSLionel Sambuc(that is, one that does not depend on the stack top). 2987*0a6a1f1dSLionel Sambuc 2988*0a6a1f1dSLionel Sambuc 298911be35a1SLionel Sambuc 299011be35a1SLionel Sambuc 299111be35a1SLionel Sambuc 299211be35a1SLionel Sambuc<hr><h3><a name="lua_Alloc"><code>lua_Alloc</code></a></h3> 299311be35a1SLionel Sambuc<pre>typedef void * (*lua_Alloc) (void *ud, 299411be35a1SLionel Sambuc void *ptr, 299511be35a1SLionel Sambuc size_t osize, 299611be35a1SLionel Sambuc size_t nsize);</pre> 299711be35a1SLionel Sambuc 299811be35a1SLionel Sambuc<p> 299911be35a1SLionel SambucThe type of the memory-allocation function used by Lua states. 300011be35a1SLionel SambucThe allocator function must provide a 300111be35a1SLionel Sambucfunctionality similar to <code>realloc</code>, 300211be35a1SLionel Sambucbut not exactly the same. 300311be35a1SLionel SambucIts arguments are 300411be35a1SLionel Sambuc<code>ud</code>, an opaque pointer passed to <a href="#lua_newstate"><code>lua_newstate</code></a>; 300511be35a1SLionel Sambuc<code>ptr</code>, a pointer to the block being allocated/reallocated/freed; 3006*0a6a1f1dSLionel Sambuc<code>osize</code>, the original size of the block or some code about what 3007*0a6a1f1dSLionel Sambucis being allocated; 3008*0a6a1f1dSLionel Sambucand <code>nsize</code>, the new size of the block. 3009*0a6a1f1dSLionel Sambuc 3010*0a6a1f1dSLionel Sambuc 3011*0a6a1f1dSLionel Sambuc<p> 3012*0a6a1f1dSLionel SambucWhen <code>ptr</code> is not <code>NULL</code>, 3013*0a6a1f1dSLionel Sambuc<code>osize</code> is the size of the block pointed by <code>ptr</code>, 3014*0a6a1f1dSLionel Sambucthat is, the size given when it was allocated or reallocated. 3015*0a6a1f1dSLionel Sambuc 3016*0a6a1f1dSLionel Sambuc 3017*0a6a1f1dSLionel Sambuc<p> 3018*0a6a1f1dSLionel SambucWhen <code>ptr</code> is <code>NULL</code>, 3019*0a6a1f1dSLionel Sambuc<code>osize</code> encodes the kind of object that Lua is allocating. 3020*0a6a1f1dSLionel Sambuc<code>osize</code> is any of 3021*0a6a1f1dSLionel Sambuc<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>, 3022*0a6a1f1dSLionel Sambuc<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) 3023*0a6a1f1dSLionel SambucLua is creating a new object of that type. 3024*0a6a1f1dSLionel SambucWhen <code>osize</code> is some other value, 3025*0a6a1f1dSLionel SambucLua is allocating memory for something else. 3026*0a6a1f1dSLionel Sambuc 3027*0a6a1f1dSLionel Sambuc 3028*0a6a1f1dSLionel Sambuc<p> 3029*0a6a1f1dSLionel SambucLua assumes the following behavior from the allocator function: 3030*0a6a1f1dSLionel Sambuc 3031*0a6a1f1dSLionel Sambuc 3032*0a6a1f1dSLionel Sambuc<p> 3033*0a6a1f1dSLionel SambucWhen <code>nsize</code> is zero, 3034*0a6a1f1dSLionel Sambucthe allocator must behave like <code>free</code> 3035*0a6a1f1dSLionel Sambucand return <code>NULL</code>. 3036*0a6a1f1dSLionel Sambuc 3037*0a6a1f1dSLionel Sambuc 3038*0a6a1f1dSLionel Sambuc<p> 3039*0a6a1f1dSLionel SambucWhen <code>nsize</code> is not zero, 3040*0a6a1f1dSLionel Sambucthe allocator must behave like <code>realloc</code>. 3041*0a6a1f1dSLionel SambucThe allocator returns <code>NULL</code> 3042*0a6a1f1dSLionel Sambucif and only if it cannot fulfill the request. 304311be35a1SLionel SambucLua assumes that the allocator never fails when 304411be35a1SLionel Sambuc<code>osize >= nsize</code>. 304511be35a1SLionel Sambuc 304611be35a1SLionel Sambuc 304711be35a1SLionel Sambuc<p> 304811be35a1SLionel SambucHere is a simple implementation for the allocator function. 304911be35a1SLionel SambucIt is used in the auxiliary library by <a href="#luaL_newstate"><code>luaL_newstate</code></a>. 305011be35a1SLionel Sambuc 305111be35a1SLionel Sambuc<pre> 305211be35a1SLionel Sambuc static void *l_alloc (void *ud, void *ptr, size_t osize, 305311be35a1SLionel Sambuc size_t nsize) { 305411be35a1SLionel Sambuc (void)ud; (void)osize; /* not used */ 305511be35a1SLionel Sambuc if (nsize == 0) { 305611be35a1SLionel Sambuc free(ptr); 305711be35a1SLionel Sambuc return NULL; 305811be35a1SLionel Sambuc } 305911be35a1SLionel Sambuc else 306011be35a1SLionel Sambuc return realloc(ptr, nsize); 306111be35a1SLionel Sambuc } 306211be35a1SLionel Sambuc</pre><p> 3063*0a6a1f1dSLionel SambucNote that Standard C ensures 306411be35a1SLionel Sambucthat <code>free(NULL)</code> has no effect and that 306511be35a1SLionel Sambuc<code>realloc(NULL,size)</code> is equivalent to <code>malloc(size)</code>. 3066*0a6a1f1dSLionel SambucThis code assumes that <code>realloc</code> does not fail when shrinking a block. 3067*0a6a1f1dSLionel Sambuc(Although Standard C does not ensure this behavior, 3068*0a6a1f1dSLionel Sambucit seems to be a safe assumption.) 306911be35a1SLionel Sambuc 307011be35a1SLionel Sambuc 307111be35a1SLionel Sambuc 307211be35a1SLionel Sambuc 307311be35a1SLionel Sambuc 3074*0a6a1f1dSLionel Sambuc<hr><h3><a name="lua_arith"><code>lua_arith</code></a></h3><p> 3075*0a6a1f1dSLionel Sambuc<span class="apii">[-(2|1), +1, <em>e</em>]</span> 3076*0a6a1f1dSLionel Sambuc<pre>void lua_arith (lua_State *L, int op);</pre> 3077*0a6a1f1dSLionel Sambuc 3078*0a6a1f1dSLionel Sambuc<p> 3079*0a6a1f1dSLionel SambucPerforms an arithmetic or bitwise operation over the two values 3080*0a6a1f1dSLionel Sambuc(or one, in the case of negations) 3081*0a6a1f1dSLionel Sambucat the top of the stack, 3082*0a6a1f1dSLionel Sambucwith the value at the top being the second operand, 3083*0a6a1f1dSLionel Sambucpops these values, and pushes the result of the operation. 3084*0a6a1f1dSLionel SambucThe function follows the semantics of the corresponding Lua operator 3085*0a6a1f1dSLionel Sambuc(that is, it may call metamethods). 3086*0a6a1f1dSLionel Sambuc 3087*0a6a1f1dSLionel Sambuc 3088*0a6a1f1dSLionel Sambuc<p> 3089*0a6a1f1dSLionel SambucThe value of <code>op</code> must be one of the following constants: 3090*0a6a1f1dSLionel Sambuc 3091*0a6a1f1dSLionel Sambuc<ul> 3092*0a6a1f1dSLionel Sambuc 3093*0a6a1f1dSLionel Sambuc<li><b><a name="pdf-LUA_OPADD"><code>LUA_OPADD</code></a>: </b> performs addition (<code>+</code>)</li> 3094*0a6a1f1dSLionel Sambuc<li><b><a name="pdf-LUA_OPSUB"><code>LUA_OPSUB</code></a>: </b> performs subtraction (<code>-</code>)</li> 3095*0a6a1f1dSLionel Sambuc<li><b><a name="pdf-LUA_OPMUL"><code>LUA_OPMUL</code></a>: </b> performs multiplication (<code>*</code>)</li> 3096*0a6a1f1dSLionel Sambuc<li><b><a name="pdf-LUA_OPDIV"><code>LUA_OPDIV</code></a>: </b> performs float division (<code>/</code>)</li> 3097*0a6a1f1dSLionel Sambuc<li><b><a name="pdf-LUA_OPIDIV"><code>LUA_OPIDIV</code></a>: </b> performs floor division (<code>//</code>)</li> 3098*0a6a1f1dSLionel Sambuc<li><b><a name="pdf-LUA_OPMOD"><code>LUA_OPMOD</code></a>: </b> performs modulo (<code>%</code>)</li> 3099*0a6a1f1dSLionel Sambuc<li><b><a name="pdf-LUA_OPPOW"><code>LUA_OPPOW</code></a>: </b> performs exponentiation (<code>^</code>)</li> 3100*0a6a1f1dSLionel Sambuc<li><b><a name="pdf-LUA_OPUNM"><code>LUA_OPUNM</code></a>: </b> performs mathematical negation (unary <code>-</code>)</li> 3101*0a6a1f1dSLionel Sambuc<li><b><a name="pdf-LUA_OPBNOT"><code>LUA_OPBNOT</code></a>: </b> performs bitwise negation (<code>~</code>)</li> 3102*0a6a1f1dSLionel Sambuc<li><b><a name="pdf-LUA_OPBAND"><code>LUA_OPBAND</code></a>: </b> performs bitwise and (<code>&</code>)</li> 3103*0a6a1f1dSLionel Sambuc<li><b><a name="pdf-LUA_OPBOR"><code>LUA_OPBOR</code></a>: </b> performs bitwise or (<code>|</code>)</li> 3104*0a6a1f1dSLionel Sambuc<li><b><a name="pdf-LUA_OPBXOR"><code>LUA_OPBXOR</code></a>: </b> performs bitwise exclusive or (<code>~</code>)</li> 3105*0a6a1f1dSLionel Sambuc<li><b><a name="pdf-LUA_OPSHL"><code>LUA_OPSHL</code></a>: </b> performs left shift (<code><<</code>)</li> 3106*0a6a1f1dSLionel Sambuc<li><b><a name="pdf-LUA_OPSHR"><code>LUA_OPSHR</code></a>: </b> performs right shift (<code>>></code>)</li> 3107*0a6a1f1dSLionel Sambuc 3108*0a6a1f1dSLionel Sambuc</ul> 3109*0a6a1f1dSLionel Sambuc 3110*0a6a1f1dSLionel Sambuc 3111*0a6a1f1dSLionel Sambuc 3112*0a6a1f1dSLionel Sambuc 311311be35a1SLionel Sambuc<hr><h3><a name="lua_atpanic"><code>lua_atpanic</code></a></h3><p> 3114*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 311511be35a1SLionel Sambuc<pre>lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf);</pre> 311611be35a1SLionel Sambuc 311711be35a1SLionel Sambuc<p> 3118*0a6a1f1dSLionel SambucSets a new panic function and returns the old one (see <a href="#4.6">§4.6</a>). 311911be35a1SLionel Sambuc 312011be35a1SLionel Sambuc 312111be35a1SLionel Sambuc 312211be35a1SLionel Sambuc 312311be35a1SLionel Sambuc 312411be35a1SLionel Sambuc<hr><h3><a name="lua_call"><code>lua_call</code></a></h3><p> 312511be35a1SLionel Sambuc<span class="apii">[-(nargs+1), +nresults, <em>e</em>]</span> 312611be35a1SLionel Sambuc<pre>void lua_call (lua_State *L, int nargs, int nresults);</pre> 312711be35a1SLionel Sambuc 312811be35a1SLionel Sambuc<p> 312911be35a1SLionel SambucCalls a function. 313011be35a1SLionel Sambuc 313111be35a1SLionel Sambuc 313211be35a1SLionel Sambuc<p> 313311be35a1SLionel SambucTo call a function you must use the following protocol: 313411be35a1SLionel Sambucfirst, the function to be called is pushed onto the stack; 313511be35a1SLionel Sambucthen, the arguments to the function are pushed 313611be35a1SLionel Sambucin direct order; 313711be35a1SLionel Sambucthat is, the first argument is pushed first. 313811be35a1SLionel SambucFinally you call <a href="#lua_call"><code>lua_call</code></a>; 313911be35a1SLionel Sambuc<code>nargs</code> is the number of arguments that you pushed onto the stack. 314011be35a1SLionel SambucAll arguments and the function value are popped from the stack 314111be35a1SLionel Sambucwhen the function is called. 314211be35a1SLionel SambucThe function results are pushed onto the stack when the function returns. 314311be35a1SLionel SambucThe number of results is adjusted to <code>nresults</code>, 314411be35a1SLionel Sambucunless <code>nresults</code> is <a name="pdf-LUA_MULTRET"><code>LUA_MULTRET</code></a>. 3145*0a6a1f1dSLionel SambucIn this case, all results from the function are pushed. 314611be35a1SLionel SambucLua takes care that the returned values fit into the stack space. 314711be35a1SLionel SambucThe function results are pushed onto the stack in direct order 314811be35a1SLionel Sambuc(the first result is pushed first), 314911be35a1SLionel Sambucso that after the call the last result is on the top of the stack. 315011be35a1SLionel Sambuc 315111be35a1SLionel Sambuc 315211be35a1SLionel Sambuc<p> 315311be35a1SLionel SambucAny error inside the called function is propagated upwards 315411be35a1SLionel Sambuc(with a <code>longjmp</code>). 315511be35a1SLionel Sambuc 315611be35a1SLionel Sambuc 315711be35a1SLionel Sambuc<p> 315811be35a1SLionel SambucThe following example shows how the host program can do the 315911be35a1SLionel Sambucequivalent to this Lua code: 316011be35a1SLionel Sambuc 316111be35a1SLionel Sambuc<pre> 316211be35a1SLionel Sambuc a = f("how", t.x, 14) 316311be35a1SLionel Sambuc</pre><p> 316411be35a1SLionel SambucHere it is in C: 316511be35a1SLionel Sambuc 316611be35a1SLionel Sambuc<pre> 3167*0a6a1f1dSLionel Sambuc lua_getglobal(L, "f"); /* function to be called */ 3168*0a6a1f1dSLionel Sambuc lua_pushliteral(L, "how"); /* 1st argument */ 3169*0a6a1f1dSLionel Sambuc lua_getglobal(L, "t"); /* table to be indexed */ 317011be35a1SLionel Sambuc lua_getfield(L, -1, "x"); /* push result of t.x (2nd arg) */ 317111be35a1SLionel Sambuc lua_remove(L, -2); /* remove 't' from the stack */ 317211be35a1SLionel Sambuc lua_pushinteger(L, 14); /* 3rd argument */ 317311be35a1SLionel Sambuc lua_call(L, 3, 1); /* call 'f' with 3 arguments and 1 result */ 3174*0a6a1f1dSLionel Sambuc lua_setglobal(L, "a"); /* set global 'a' */ 317511be35a1SLionel Sambuc</pre><p> 3176*0a6a1f1dSLionel SambucNote that the code above is <em>balanced</em>: 317711be35a1SLionel Sambucat its end, the stack is back to its original configuration. 317811be35a1SLionel SambucThis is considered good programming practice. 317911be35a1SLionel Sambuc 318011be35a1SLionel Sambuc 318111be35a1SLionel Sambuc 318211be35a1SLionel Sambuc 318311be35a1SLionel Sambuc 3184*0a6a1f1dSLionel Sambuc<hr><h3><a name="lua_callk"><code>lua_callk</code></a></h3><p> 3185*0a6a1f1dSLionel Sambuc<span class="apii">[-(nargs + 1), +nresults, <em>e</em>]</span> 3186*0a6a1f1dSLionel Sambuc<pre>void lua_callk (lua_State *L, 3187*0a6a1f1dSLionel Sambuc int nargs, 3188*0a6a1f1dSLionel Sambuc int nresults, 3189*0a6a1f1dSLionel Sambuc lua_KContext ctx, 3190*0a6a1f1dSLionel Sambuc lua_KFunction k);</pre> 3191*0a6a1f1dSLionel Sambuc 3192*0a6a1f1dSLionel Sambuc<p> 3193*0a6a1f1dSLionel SambucThis function behaves exactly like <a href="#lua_call"><code>lua_call</code></a>, 3194*0a6a1f1dSLionel Sambucbut allows the called function to yield (see <a href="#4.7">§4.7</a>). 3195*0a6a1f1dSLionel Sambuc 3196*0a6a1f1dSLionel Sambuc 3197*0a6a1f1dSLionel Sambuc 3198*0a6a1f1dSLionel Sambuc 3199*0a6a1f1dSLionel Sambuc 320011be35a1SLionel Sambuc<hr><h3><a name="lua_CFunction"><code>lua_CFunction</code></a></h3> 320111be35a1SLionel Sambuc<pre>typedef int (*lua_CFunction) (lua_State *L);</pre> 320211be35a1SLionel Sambuc 320311be35a1SLionel Sambuc<p> 320411be35a1SLionel SambucType for C functions. 320511be35a1SLionel Sambuc 320611be35a1SLionel Sambuc 320711be35a1SLionel Sambuc<p> 320811be35a1SLionel SambucIn order to communicate properly with Lua, 320911be35a1SLionel Sambuca C function must use the following protocol, 321011be35a1SLionel Sambucwhich defines the way parameters and results are passed: 321111be35a1SLionel Sambuca C function receives its arguments from Lua in its stack 321211be35a1SLionel Sambucin direct order (the first argument is pushed first). 321311be35a1SLionel SambucSo, when the function starts, 321411be35a1SLionel Sambuc<code>lua_gettop(L)</code> returns the number of arguments received by the function. 321511be35a1SLionel SambucThe first argument (if any) is at index 1 321611be35a1SLionel Sambucand its last argument is at index <code>lua_gettop(L)</code>. 321711be35a1SLionel SambucTo return values to Lua, a C function just pushes them onto the stack, 321811be35a1SLionel Sambucin direct order (the first result is pushed first), 321911be35a1SLionel Sambucand returns the number of results. 322011be35a1SLionel SambucAny other value in the stack below the results will be properly 322111be35a1SLionel Sambucdiscarded by Lua. 322211be35a1SLionel SambucLike a Lua function, a C function called by Lua can also return 322311be35a1SLionel Sambucmany results. 322411be35a1SLionel Sambuc 322511be35a1SLionel Sambuc 322611be35a1SLionel Sambuc<p> 322711be35a1SLionel SambucAs an example, the following function receives a variable number 3228*0a6a1f1dSLionel Sambucof numeric arguments and returns their average and their sum: 322911be35a1SLionel Sambuc 323011be35a1SLionel Sambuc<pre> 323111be35a1SLionel Sambuc static int foo (lua_State *L) { 323211be35a1SLionel Sambuc int n = lua_gettop(L); /* number of arguments */ 3233*0a6a1f1dSLionel Sambuc lua_Number sum = 0.0; 323411be35a1SLionel Sambuc int i; 323511be35a1SLionel Sambuc for (i = 1; i <= n; i++) { 323611be35a1SLionel Sambuc if (!lua_isnumber(L, i)) { 3237*0a6a1f1dSLionel Sambuc lua_pushliteral(L, "incorrect argument"); 323811be35a1SLionel Sambuc lua_error(L); 323911be35a1SLionel Sambuc } 324011be35a1SLionel Sambuc sum += lua_tonumber(L, i); 324111be35a1SLionel Sambuc } 324211be35a1SLionel Sambuc lua_pushnumber(L, sum/n); /* first result */ 324311be35a1SLionel Sambuc lua_pushnumber(L, sum); /* second result */ 324411be35a1SLionel Sambuc return 2; /* number of results */ 324511be35a1SLionel Sambuc } 324611be35a1SLionel Sambuc</pre> 324711be35a1SLionel Sambuc 324811be35a1SLionel Sambuc 324911be35a1SLionel Sambuc 325011be35a1SLionel Sambuc 325111be35a1SLionel Sambuc<hr><h3><a name="lua_checkstack"><code>lua_checkstack</code></a></h3><p> 3252*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 3253*0a6a1f1dSLionel Sambuc<pre>int lua_checkstack (lua_State *L, int n);</pre> 325411be35a1SLionel Sambuc 325511be35a1SLionel Sambuc<p> 3256*0a6a1f1dSLionel SambucEnsures that the stack has space for at least <code>n</code> extra slots. 3257*0a6a1f1dSLionel SambucIt returns false if it cannot fulfill the request, 3258*0a6a1f1dSLionel Sambuceither because it would cause the stack 3259*0a6a1f1dSLionel Sambucto be larger than a fixed maximum size 3260*0a6a1f1dSLionel Sambuc(typically at least several thousand elements) or 3261*0a6a1f1dSLionel Sambucbecause it cannot allocate memory for the extra space. 326211be35a1SLionel SambucThis function never shrinks the stack; 326311be35a1SLionel Sambucif the stack is already larger than the new size, 326411be35a1SLionel Sambucit is left unchanged. 326511be35a1SLionel Sambuc 326611be35a1SLionel Sambuc 326711be35a1SLionel Sambuc 326811be35a1SLionel Sambuc 326911be35a1SLionel Sambuc 327011be35a1SLionel Sambuc<hr><h3><a name="lua_close"><code>lua_close</code></a></h3><p> 3271*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 327211be35a1SLionel Sambuc<pre>void lua_close (lua_State *L);</pre> 327311be35a1SLionel Sambuc 327411be35a1SLionel Sambuc<p> 327511be35a1SLionel SambucDestroys all objects in the given Lua state 327611be35a1SLionel Sambuc(calling the corresponding garbage-collection metamethods, if any) 327711be35a1SLionel Sambucand frees all dynamic memory used by this state. 327811be35a1SLionel SambucOn several platforms, you may not need to call this function, 327911be35a1SLionel Sambucbecause all resources are naturally released when the host program ends. 3280*0a6a1f1dSLionel SambucOn the other hand, long-running programs that create multiple states, 3281*0a6a1f1dSLionel Sambucsuch as daemons or web servers, 3282*0a6a1f1dSLionel Sambucwill probably need to close states as soon as they are not needed. 328311be35a1SLionel Sambuc 328411be35a1SLionel Sambuc 328511be35a1SLionel Sambuc 328611be35a1SLionel Sambuc 328711be35a1SLionel Sambuc 3288*0a6a1f1dSLionel Sambuc<hr><h3><a name="lua_compare"><code>lua_compare</code></a></h3><p> 3289*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, <em>e</em>]</span> 3290*0a6a1f1dSLionel Sambuc<pre>int lua_compare (lua_State *L, int index1, int index2, int op);</pre> 3291*0a6a1f1dSLionel Sambuc 3292*0a6a1f1dSLionel Sambuc<p> 3293*0a6a1f1dSLionel SambucCompares two Lua values. 3294*0a6a1f1dSLionel SambucReturns 1 if the value at index <code>index1</code> satisfies <code>op</code> 3295*0a6a1f1dSLionel Sambucwhen compared with the value at index <code>index2</code>, 3296*0a6a1f1dSLionel Sambucfollowing the semantics of the corresponding Lua operator 3297*0a6a1f1dSLionel Sambuc(that is, it may call metamethods). 3298*0a6a1f1dSLionel SambucOtherwise returns 0. 3299*0a6a1f1dSLionel SambucAlso returns 0 if any of the indices is not valid. 3300*0a6a1f1dSLionel Sambuc 3301*0a6a1f1dSLionel Sambuc 3302*0a6a1f1dSLionel Sambuc<p> 3303*0a6a1f1dSLionel SambucThe value of <code>op</code> must be one of the following constants: 3304*0a6a1f1dSLionel Sambuc 3305*0a6a1f1dSLionel Sambuc<ul> 3306*0a6a1f1dSLionel Sambuc 3307*0a6a1f1dSLionel Sambuc<li><b><a name="pdf-LUA_OPEQ"><code>LUA_OPEQ</code></a>: </b> compares for equality (<code>==</code>)</li> 3308*0a6a1f1dSLionel Sambuc<li><b><a name="pdf-LUA_OPLT"><code>LUA_OPLT</code></a>: </b> compares for less than (<code><</code>)</li> 3309*0a6a1f1dSLionel Sambuc<li><b><a name="pdf-LUA_OPLE"><code>LUA_OPLE</code></a>: </b> compares for less or equal (<code><=</code>)</li> 3310*0a6a1f1dSLionel Sambuc 3311*0a6a1f1dSLionel Sambuc</ul> 3312*0a6a1f1dSLionel Sambuc 3313*0a6a1f1dSLionel Sambuc 3314*0a6a1f1dSLionel Sambuc 3315*0a6a1f1dSLionel Sambuc 331611be35a1SLionel Sambuc<hr><h3><a name="lua_concat"><code>lua_concat</code></a></h3><p> 331711be35a1SLionel Sambuc<span class="apii">[-n, +1, <em>e</em>]</span> 331811be35a1SLionel Sambuc<pre>void lua_concat (lua_State *L, int n);</pre> 331911be35a1SLionel Sambuc 332011be35a1SLionel Sambuc<p> 332111be35a1SLionel SambucConcatenates the <code>n</code> values at the top of the stack, 332211be35a1SLionel Sambucpops them, and leaves the result at the top. 332311be35a1SLionel SambucIf <code>n</code> is 1, the result is the single value on the stack 332411be35a1SLionel Sambuc(that is, the function does nothing); 332511be35a1SLionel Sambucif <code>n</code> is 0, the result is the empty string. 332611be35a1SLionel SambucConcatenation is performed following the usual semantics of Lua 3327*0a6a1f1dSLionel Sambuc(see <a href="#3.4.6">§3.4.6</a>). 332811be35a1SLionel Sambuc 332911be35a1SLionel Sambuc 333011be35a1SLionel Sambuc 333111be35a1SLionel Sambuc 333211be35a1SLionel Sambuc 3333*0a6a1f1dSLionel Sambuc<hr><h3><a name="lua_copy"><code>lua_copy</code></a></h3><p> 3334*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 3335*0a6a1f1dSLionel Sambuc<pre>void lua_copy (lua_State *L, int fromidx, int toidx);</pre> 333611be35a1SLionel Sambuc 333711be35a1SLionel Sambuc<p> 3338*0a6a1f1dSLionel SambucCopies the element at index <code>fromidx</code> 3339*0a6a1f1dSLionel Sambucinto the valid index <code>toidx</code>, 3340*0a6a1f1dSLionel Sambucreplacing the value at that position. 3341*0a6a1f1dSLionel SambucValues at other positions are not affected. 334211be35a1SLionel Sambuc 334311be35a1SLionel Sambuc 334411be35a1SLionel Sambuc 334511be35a1SLionel Sambuc 334611be35a1SLionel Sambuc 334711be35a1SLionel Sambuc<hr><h3><a name="lua_createtable"><code>lua_createtable</code></a></h3><p> 3348*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, <em>e</em>]</span> 334911be35a1SLionel Sambuc<pre>void lua_createtable (lua_State *L, int narr, int nrec);</pre> 335011be35a1SLionel Sambuc 335111be35a1SLionel Sambuc<p> 335211be35a1SLionel SambucCreates a new empty table and pushes it onto the stack. 3353*0a6a1f1dSLionel SambucParameter <code>narr</code> is a hint for how many elements the table 3354*0a6a1f1dSLionel Sambucwill have as a sequence; 3355*0a6a1f1dSLionel Sambucparameter <code>nrec</code> is a hint for how many other elements 335611be35a1SLionel Sambucthe table will have. 3357*0a6a1f1dSLionel SambucLua may use these hints to preallocate memory for the new table. 3358*0a6a1f1dSLionel SambucThis pre-allocation is useful for performance when you know in advance 3359*0a6a1f1dSLionel Sambuchow many elements the table will have. 336011be35a1SLionel SambucOtherwise you can use the function <a href="#lua_newtable"><code>lua_newtable</code></a>. 336111be35a1SLionel Sambuc 336211be35a1SLionel Sambuc 336311be35a1SLionel Sambuc 336411be35a1SLionel Sambuc 336511be35a1SLionel Sambuc 336611be35a1SLionel Sambuc<hr><h3><a name="lua_dump"><code>lua_dump</code></a></h3><p> 3367*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, <em>e</em>]</span> 3368*0a6a1f1dSLionel Sambuc<pre>int lua_dump (lua_State *L, 3369*0a6a1f1dSLionel Sambuc lua_Writer writer, 3370*0a6a1f1dSLionel Sambuc void *data, 3371*0a6a1f1dSLionel Sambuc int strip);</pre> 337211be35a1SLionel Sambuc 337311be35a1SLionel Sambuc<p> 337411be35a1SLionel SambucDumps a function as a binary chunk. 337511be35a1SLionel SambucReceives a Lua function on the top of the stack 337611be35a1SLionel Sambucand produces a binary chunk that, 337711be35a1SLionel Sambucif loaded again, 337811be35a1SLionel Sambucresults in a function equivalent to the one dumped. 337911be35a1SLionel SambucAs it produces parts of the chunk, 338011be35a1SLionel Sambuc<a href="#lua_dump"><code>lua_dump</code></a> calls function <code>writer</code> (see <a href="#lua_Writer"><code>lua_Writer</code></a>) 338111be35a1SLionel Sambucwith the given <code>data</code> 338211be35a1SLionel Sambucto write them. 338311be35a1SLionel Sambuc 338411be35a1SLionel Sambuc 338511be35a1SLionel Sambuc<p> 3386*0a6a1f1dSLionel SambucIf <code>strip</code> is true, 3387*0a6a1f1dSLionel Sambucthe binary representation may not include all debug information 3388*0a6a1f1dSLionel Sambucabout the function, 3389*0a6a1f1dSLionel Sambucto save space. 3390*0a6a1f1dSLionel Sambuc 3391*0a6a1f1dSLionel Sambuc 3392*0a6a1f1dSLionel Sambuc<p> 339311be35a1SLionel SambucThe value returned is the error code returned by the last 339411be35a1SLionel Sambuccall to the writer; 339511be35a1SLionel Sambuc0 means no errors. 339611be35a1SLionel Sambuc 339711be35a1SLionel Sambuc 339811be35a1SLionel Sambuc<p> 339911be35a1SLionel SambucThis function does not pop the Lua function from the stack. 340011be35a1SLionel Sambuc 340111be35a1SLionel Sambuc 340211be35a1SLionel Sambuc 340311be35a1SLionel Sambuc 340411be35a1SLionel Sambuc 340511be35a1SLionel Sambuc<hr><h3><a name="lua_error"><code>lua_error</code></a></h3><p> 340611be35a1SLionel Sambuc<span class="apii">[-1, +0, <em>v</em>]</span> 340711be35a1SLionel Sambuc<pre>int lua_error (lua_State *L);</pre> 340811be35a1SLionel Sambuc 340911be35a1SLionel Sambuc<p> 3410*0a6a1f1dSLionel SambucGenerates a Lua error, 3411*0a6a1f1dSLionel Sambucusing the value at the top of the stack as the error object. 341211be35a1SLionel SambucThis function does a long jump, 3413*0a6a1f1dSLionel Sambucand therefore never returns 341411be35a1SLionel Sambuc(see <a href="#luaL_error"><code>luaL_error</code></a>). 341511be35a1SLionel Sambuc 341611be35a1SLionel Sambuc 341711be35a1SLionel Sambuc 341811be35a1SLionel Sambuc 341911be35a1SLionel Sambuc 342011be35a1SLionel Sambuc<hr><h3><a name="lua_gc"><code>lua_gc</code></a></h3><p> 342111be35a1SLionel Sambuc<span class="apii">[-0, +0, <em>e</em>]</span> 342211be35a1SLionel Sambuc<pre>int lua_gc (lua_State *L, int what, int data);</pre> 342311be35a1SLionel Sambuc 342411be35a1SLionel Sambuc<p> 342511be35a1SLionel SambucControls the garbage collector. 342611be35a1SLionel Sambuc 342711be35a1SLionel Sambuc 342811be35a1SLionel Sambuc<p> 342911be35a1SLionel SambucThis function performs several tasks, 343011be35a1SLionel Sambucaccording to the value of the parameter <code>what</code>: 343111be35a1SLionel Sambuc 343211be35a1SLionel Sambuc<ul> 343311be35a1SLionel Sambuc 343411be35a1SLionel Sambuc<li><b><code>LUA_GCSTOP</code>: </b> 343511be35a1SLionel Sambucstops the garbage collector. 343611be35a1SLionel Sambuc</li> 343711be35a1SLionel Sambuc 343811be35a1SLionel Sambuc<li><b><code>LUA_GCRESTART</code>: </b> 343911be35a1SLionel Sambucrestarts the garbage collector. 344011be35a1SLionel Sambuc</li> 344111be35a1SLionel Sambuc 344211be35a1SLionel Sambuc<li><b><code>LUA_GCCOLLECT</code>: </b> 344311be35a1SLionel Sambucperforms a full garbage-collection cycle. 344411be35a1SLionel Sambuc</li> 344511be35a1SLionel Sambuc 344611be35a1SLionel Sambuc<li><b><code>LUA_GCCOUNT</code>: </b> 344711be35a1SLionel Sambucreturns the current amount of memory (in Kbytes) in use by Lua. 344811be35a1SLionel Sambuc</li> 344911be35a1SLionel Sambuc 345011be35a1SLionel Sambuc<li><b><code>LUA_GCCOUNTB</code>: </b> 345111be35a1SLionel Sambucreturns the remainder of dividing the current amount of bytes of 345211be35a1SLionel Sambucmemory in use by Lua by 1024. 345311be35a1SLionel Sambuc</li> 345411be35a1SLionel Sambuc 345511be35a1SLionel Sambuc<li><b><code>LUA_GCSTEP</code>: </b> 345611be35a1SLionel Sambucperforms an incremental step of garbage collection. 345711be35a1SLionel Sambuc</li> 345811be35a1SLionel Sambuc 345911be35a1SLionel Sambuc<li><b><code>LUA_GCSETPAUSE</code>: </b> 346011be35a1SLionel Sambucsets <code>data</code> as the new value 3461*0a6a1f1dSLionel Sambucfor the <em>pause</em> of the collector (see <a href="#2.5">§2.5</a>) 3462*0a6a1f1dSLionel Sambucand returns the previous value of the pause. 346311be35a1SLionel Sambuc</li> 346411be35a1SLionel Sambuc 346511be35a1SLionel Sambuc<li><b><code>LUA_GCSETSTEPMUL</code>: </b> 346611be35a1SLionel Sambucsets <code>data</code> as the new value for the <em>step multiplier</em> of 3467*0a6a1f1dSLionel Sambucthe collector (see <a href="#2.5">§2.5</a>) 3468*0a6a1f1dSLionel Sambucand returns the previous value of the step multiplier. 3469*0a6a1f1dSLionel Sambuc</li> 3470*0a6a1f1dSLionel Sambuc 3471*0a6a1f1dSLionel Sambuc<li><b><code>LUA_GCISRUNNING</code>: </b> 3472*0a6a1f1dSLionel Sambucreturns a boolean that tells whether the collector is running 3473*0a6a1f1dSLionel Sambuc(i.e., not stopped). 347411be35a1SLionel Sambuc</li> 347511be35a1SLionel Sambuc 347611be35a1SLionel Sambuc</ul> 347711be35a1SLionel Sambuc 3478*0a6a1f1dSLionel Sambuc<p> 3479*0a6a1f1dSLionel SambucFor more details about these options, 3480*0a6a1f1dSLionel Sambucsee <a href="#pdf-collectgarbage"><code>collectgarbage</code></a>. 3481*0a6a1f1dSLionel Sambuc 3482*0a6a1f1dSLionel Sambuc 348311be35a1SLionel Sambuc 348411be35a1SLionel Sambuc 348511be35a1SLionel Sambuc 348611be35a1SLionel Sambuc<hr><h3><a name="lua_getallocf"><code>lua_getallocf</code></a></h3><p> 3487*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 348811be35a1SLionel Sambuc<pre>lua_Alloc lua_getallocf (lua_State *L, void **ud);</pre> 348911be35a1SLionel Sambuc 349011be35a1SLionel Sambuc<p> 349111be35a1SLionel SambucReturns the memory-allocation function of a given state. 349211be35a1SLionel SambucIf <code>ud</code> is not <code>NULL</code>, Lua stores in <code>*ud</code> the 3493*0a6a1f1dSLionel Sambucopaque pointer given when the memory-allocator function was set. 349411be35a1SLionel Sambuc 349511be35a1SLionel Sambuc 349611be35a1SLionel Sambuc 349711be35a1SLionel Sambuc 349811be35a1SLionel Sambuc 349911be35a1SLionel Sambuc<hr><h3><a name="lua_getfield"><code>lua_getfield</code></a></h3><p> 350011be35a1SLionel Sambuc<span class="apii">[-0, +1, <em>e</em>]</span> 3501*0a6a1f1dSLionel Sambuc<pre>int lua_getfield (lua_State *L, int index, const char *k);</pre> 350211be35a1SLionel Sambuc 350311be35a1SLionel Sambuc<p> 350411be35a1SLionel SambucPushes onto the stack the value <code>t[k]</code>, 3505*0a6a1f1dSLionel Sambucwhere <code>t</code> is the value at the given index. 350611be35a1SLionel SambucAs in Lua, this function may trigger a metamethod 3507*0a6a1f1dSLionel Sambucfor the "index" event (see <a href="#2.4">§2.4</a>). 3508*0a6a1f1dSLionel Sambuc 3509*0a6a1f1dSLionel Sambuc 3510*0a6a1f1dSLionel Sambuc<p> 3511*0a6a1f1dSLionel SambucReturns the type of the pushed value. 3512*0a6a1f1dSLionel Sambuc 3513*0a6a1f1dSLionel Sambuc 3514*0a6a1f1dSLionel Sambuc 3515*0a6a1f1dSLionel Sambuc 3516*0a6a1f1dSLionel Sambuc 3517*0a6a1f1dSLionel Sambuc<hr><h3><a name="lua_getextraspace"><code>lua_getextraspace</code></a></h3><p> 3518*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 3519*0a6a1f1dSLionel Sambuc<pre>void *lua_getextraspace (lua_State *L);</pre> 3520*0a6a1f1dSLionel Sambuc 3521*0a6a1f1dSLionel Sambuc<p> 3522*0a6a1f1dSLionel SambucReturns a pointer to a raw memory area associated with the 3523*0a6a1f1dSLionel Sambucgiven Lua state. 3524*0a6a1f1dSLionel SambucThe application can use this area for any purpose; 3525*0a6a1f1dSLionel SambucLua does not use it for anything. 3526*0a6a1f1dSLionel Sambuc 3527*0a6a1f1dSLionel Sambuc 3528*0a6a1f1dSLionel Sambuc<p> 3529*0a6a1f1dSLionel SambucEach new thread has this area initialized with a copy 3530*0a6a1f1dSLionel Sambucof the area of the main thread. 3531*0a6a1f1dSLionel Sambuc 3532*0a6a1f1dSLionel Sambuc 3533*0a6a1f1dSLionel Sambuc<p> 3534*0a6a1f1dSLionel SambucBy default, this area has the size of a pointer to void, 3535*0a6a1f1dSLionel Sambucbut you can recompile Lua with a different size for this area. 3536*0a6a1f1dSLionel Sambuc(See <code>LUA_EXTRASPACE</code> in <code>luaconf.h</code>.) 353711be35a1SLionel Sambuc 353811be35a1SLionel Sambuc 353911be35a1SLionel Sambuc 354011be35a1SLionel Sambuc 354111be35a1SLionel Sambuc 354211be35a1SLionel Sambuc<hr><h3><a name="lua_getglobal"><code>lua_getglobal</code></a></h3><p> 354311be35a1SLionel Sambuc<span class="apii">[-0, +1, <em>e</em>]</span> 3544*0a6a1f1dSLionel Sambuc<pre>int lua_getglobal (lua_State *L, const char *name);</pre> 354511be35a1SLionel Sambuc 354611be35a1SLionel Sambuc<p> 354711be35a1SLionel SambucPushes onto the stack the value of the global <code>name</code>. 3548*0a6a1f1dSLionel SambucReturns the type of that value. 354911be35a1SLionel Sambuc 3550*0a6a1f1dSLionel Sambuc 3551*0a6a1f1dSLionel Sambuc 3552*0a6a1f1dSLionel Sambuc 3553*0a6a1f1dSLionel Sambuc 3554*0a6a1f1dSLionel Sambuc<hr><h3><a name="lua_geti"><code>lua_geti</code></a></h3><p> 3555*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, <em>e</em>]</span> 3556*0a6a1f1dSLionel Sambuc<pre>int lua_geti (lua_State *L, int index, lua_Integer i);</pre> 3557*0a6a1f1dSLionel Sambuc 3558*0a6a1f1dSLionel Sambuc<p> 3559*0a6a1f1dSLionel SambucPushes onto the stack the value <code>t[i]</code>, 3560*0a6a1f1dSLionel Sambucwhere <code>t</code> is the value at the given index. 3561*0a6a1f1dSLionel SambucAs in Lua, this function may trigger a metamethod 3562*0a6a1f1dSLionel Sambucfor the "index" event (see <a href="#2.4">§2.4</a>). 3563*0a6a1f1dSLionel Sambuc 3564*0a6a1f1dSLionel Sambuc 3565*0a6a1f1dSLionel Sambuc<p> 3566*0a6a1f1dSLionel SambucReturns the type of the pushed value. 3567*0a6a1f1dSLionel Sambuc 356811be35a1SLionel Sambuc 356911be35a1SLionel Sambuc 357011be35a1SLionel Sambuc 357111be35a1SLionel Sambuc 357211be35a1SLionel Sambuc<hr><h3><a name="lua_getmetatable"><code>lua_getmetatable</code></a></h3><p> 3573*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +(0|1), –]</span> 357411be35a1SLionel Sambuc<pre>int lua_getmetatable (lua_State *L, int index);</pre> 357511be35a1SLionel Sambuc 357611be35a1SLionel Sambuc<p> 3577*0a6a1f1dSLionel SambucIf the value at the given index has a metatable, 3578*0a6a1f1dSLionel Sambucthe function pushes that metatable onto the stack and returns 1. 3579*0a6a1f1dSLionel SambucOtherwise, 358011be35a1SLionel Sambucthe function returns 0 and pushes nothing on the stack. 358111be35a1SLionel Sambuc 358211be35a1SLionel Sambuc 358311be35a1SLionel Sambuc 358411be35a1SLionel Sambuc 358511be35a1SLionel Sambuc 358611be35a1SLionel Sambuc<hr><h3><a name="lua_gettable"><code>lua_gettable</code></a></h3><p> 358711be35a1SLionel Sambuc<span class="apii">[-1, +1, <em>e</em>]</span> 3588*0a6a1f1dSLionel Sambuc<pre>int lua_gettable (lua_State *L, int index);</pre> 358911be35a1SLionel Sambuc 359011be35a1SLionel Sambuc<p> 359111be35a1SLionel SambucPushes onto the stack the value <code>t[k]</code>, 3592*0a6a1f1dSLionel Sambucwhere <code>t</code> is the value at the given index 359311be35a1SLionel Sambucand <code>k</code> is the value at the top of the stack. 359411be35a1SLionel Sambuc 359511be35a1SLionel Sambuc 359611be35a1SLionel Sambuc<p> 3597*0a6a1f1dSLionel SambucThis function pops the key from the stack, 3598*0a6a1f1dSLionel Sambucpushing the resulting value in its place. 359911be35a1SLionel SambucAs in Lua, this function may trigger a metamethod 3600*0a6a1f1dSLionel Sambucfor the "index" event (see <a href="#2.4">§2.4</a>). 3601*0a6a1f1dSLionel Sambuc 3602*0a6a1f1dSLionel Sambuc 3603*0a6a1f1dSLionel Sambuc<p> 3604*0a6a1f1dSLionel SambucReturns the type of the pushed value. 360511be35a1SLionel Sambuc 360611be35a1SLionel Sambuc 360711be35a1SLionel Sambuc 360811be35a1SLionel Sambuc 360911be35a1SLionel Sambuc 361011be35a1SLionel Sambuc<hr><h3><a name="lua_gettop"><code>lua_gettop</code></a></h3><p> 3611*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 361211be35a1SLionel Sambuc<pre>int lua_gettop (lua_State *L);</pre> 361311be35a1SLionel Sambuc 361411be35a1SLionel Sambuc<p> 361511be35a1SLionel SambucReturns the index of the top element in the stack. 361611be35a1SLionel SambucBecause indices start at 1, 3617*0a6a1f1dSLionel Sambucthis result is equal to the number of elements in the stack; 3618*0a6a1f1dSLionel Sambucin particular, 0 means an empty stack. 3619*0a6a1f1dSLionel Sambuc 3620*0a6a1f1dSLionel Sambuc 3621*0a6a1f1dSLionel Sambuc 3622*0a6a1f1dSLionel Sambuc 3623*0a6a1f1dSLionel Sambuc 3624*0a6a1f1dSLionel Sambuc<hr><h3><a name="lua_getuservalue"><code>lua_getuservalue</code></a></h3><p> 3625*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, –]</span> 3626*0a6a1f1dSLionel Sambuc<pre>int lua_getuservalue (lua_State *L, int index);</pre> 3627*0a6a1f1dSLionel Sambuc 3628*0a6a1f1dSLionel Sambuc<p> 3629*0a6a1f1dSLionel SambucPushes onto the stack the Lua value associated with the userdata 3630*0a6a1f1dSLionel Sambucat the given index. 3631*0a6a1f1dSLionel Sambuc 3632*0a6a1f1dSLionel Sambuc 3633*0a6a1f1dSLionel Sambuc<p> 3634*0a6a1f1dSLionel SambucReturns the type of the pushed value. 363511be35a1SLionel Sambuc 363611be35a1SLionel Sambuc 363711be35a1SLionel Sambuc 363811be35a1SLionel Sambuc 363911be35a1SLionel Sambuc 364011be35a1SLionel Sambuc<hr><h3><a name="lua_insert"><code>lua_insert</code></a></h3><p> 3641*0a6a1f1dSLionel Sambuc<span class="apii">[-1, +1, –]</span> 364211be35a1SLionel Sambuc<pre>void lua_insert (lua_State *L, int index);</pre> 364311be35a1SLionel Sambuc 364411be35a1SLionel Sambuc<p> 364511be35a1SLionel SambucMoves the top element into the given valid index, 364611be35a1SLionel Sambucshifting up the elements above this index to open space. 3647*0a6a1f1dSLionel SambucThis function cannot be called with a pseudo-index, 364811be35a1SLionel Sambucbecause a pseudo-index is not an actual stack position. 364911be35a1SLionel Sambuc 365011be35a1SLionel Sambuc 365111be35a1SLionel Sambuc 365211be35a1SLionel Sambuc 365311be35a1SLionel Sambuc 365411be35a1SLionel Sambuc<hr><h3><a name="lua_Integer"><code>lua_Integer</code></a></h3> 3655*0a6a1f1dSLionel Sambuc<pre>typedef ... lua_Integer;</pre> 365611be35a1SLionel Sambuc 365711be35a1SLionel Sambuc<p> 3658*0a6a1f1dSLionel SambucThe type of integers in Lua. 365911be35a1SLionel Sambuc 366011be35a1SLionel Sambuc 366111be35a1SLionel Sambuc<p> 3662*0a6a1f1dSLionel SambucBy default this type is <code>long long</code>, 3663*0a6a1f1dSLionel Sambuc(usually a 64-bit two-complement integer), 3664*0a6a1f1dSLionel Sambucbut that can be changed to <code>long</code> or <code>int</code> 3665*0a6a1f1dSLionel Sambuc(usually a 32-bit two-complement integer). 3666*0a6a1f1dSLionel Sambuc(See <code>LUA_INT_TYPE</code> in <code>luaconf.h</code>.) 3667*0a6a1f1dSLionel Sambuc 3668*0a6a1f1dSLionel Sambuc 3669*0a6a1f1dSLionel Sambuc<p> 3670*0a6a1f1dSLionel SambucLua also defines the constants 3671*0a6a1f1dSLionel Sambuc<a name="pdf-LUA_MININTEGER"><code>LUA_MININTEGER</code></a> and <a name="pdf-LUA_MAXINTEGER"><code>LUA_MAXINTEGER</code></a>, 3672*0a6a1f1dSLionel Sambucwith the minimum and the maximum values that fit in this type. 367311be35a1SLionel Sambuc 367411be35a1SLionel Sambuc 367511be35a1SLionel Sambuc 367611be35a1SLionel Sambuc 367711be35a1SLionel Sambuc 367811be35a1SLionel Sambuc<hr><h3><a name="lua_isboolean"><code>lua_isboolean</code></a></h3><p> 3679*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 368011be35a1SLionel Sambuc<pre>int lua_isboolean (lua_State *L, int index);</pre> 368111be35a1SLionel Sambuc 368211be35a1SLionel Sambuc<p> 3683*0a6a1f1dSLionel SambucReturns 1 if the value at the given index is a boolean, 368411be35a1SLionel Sambucand 0 otherwise. 368511be35a1SLionel Sambuc 368611be35a1SLionel Sambuc 368711be35a1SLionel Sambuc 368811be35a1SLionel Sambuc 368911be35a1SLionel Sambuc 369011be35a1SLionel Sambuc<hr><h3><a name="lua_iscfunction"><code>lua_iscfunction</code></a></h3><p> 3691*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 369211be35a1SLionel Sambuc<pre>int lua_iscfunction (lua_State *L, int index);</pre> 369311be35a1SLionel Sambuc 369411be35a1SLionel Sambuc<p> 3695*0a6a1f1dSLionel SambucReturns 1 if the value at the given index is a C function, 369611be35a1SLionel Sambucand 0 otherwise. 369711be35a1SLionel Sambuc 369811be35a1SLionel Sambuc 369911be35a1SLionel Sambuc 370011be35a1SLionel Sambuc 370111be35a1SLionel Sambuc 370211be35a1SLionel Sambuc<hr><h3><a name="lua_isfunction"><code>lua_isfunction</code></a></h3><p> 3703*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 370411be35a1SLionel Sambuc<pre>int lua_isfunction (lua_State *L, int index);</pre> 370511be35a1SLionel Sambuc 370611be35a1SLionel Sambuc<p> 3707*0a6a1f1dSLionel SambucReturns 1 if the value at the given index is a function 370811be35a1SLionel Sambuc(either C or Lua), and 0 otherwise. 370911be35a1SLionel Sambuc 371011be35a1SLionel Sambuc 371111be35a1SLionel Sambuc 371211be35a1SLionel Sambuc 371311be35a1SLionel Sambuc 3714*0a6a1f1dSLionel Sambuc<hr><h3><a name="lua_isinteger"><code>lua_isinteger</code></a></h3><p> 3715*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 3716*0a6a1f1dSLionel Sambuc<pre>int lua_isinteger (lua_State *L, int index);</pre> 3717*0a6a1f1dSLionel Sambuc 3718*0a6a1f1dSLionel Sambuc<p> 3719*0a6a1f1dSLionel SambucReturns 1 if the value at the given index is an integer 3720*0a6a1f1dSLionel Sambuc(that is, the value is a number and is represented as an integer), 3721*0a6a1f1dSLionel Sambucand 0 otherwise. 3722*0a6a1f1dSLionel Sambuc 3723*0a6a1f1dSLionel Sambuc 3724*0a6a1f1dSLionel Sambuc 3725*0a6a1f1dSLionel Sambuc 3726*0a6a1f1dSLionel Sambuc 372711be35a1SLionel Sambuc<hr><h3><a name="lua_islightuserdata"><code>lua_islightuserdata</code></a></h3><p> 3728*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 372911be35a1SLionel Sambuc<pre>int lua_islightuserdata (lua_State *L, int index);</pre> 373011be35a1SLionel Sambuc 373111be35a1SLionel Sambuc<p> 3732*0a6a1f1dSLionel SambucReturns 1 if the value at the given index is a light userdata, 373311be35a1SLionel Sambucand 0 otherwise. 373411be35a1SLionel Sambuc 373511be35a1SLionel Sambuc 373611be35a1SLionel Sambuc 373711be35a1SLionel Sambuc 373811be35a1SLionel Sambuc 373911be35a1SLionel Sambuc<hr><h3><a name="lua_isnil"><code>lua_isnil</code></a></h3><p> 3740*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 374111be35a1SLionel Sambuc<pre>int lua_isnil (lua_State *L, int index);</pre> 374211be35a1SLionel Sambuc 374311be35a1SLionel Sambuc<p> 3744*0a6a1f1dSLionel SambucReturns 1 if the value at the given index is <b>nil</b>, 374511be35a1SLionel Sambucand 0 otherwise. 374611be35a1SLionel Sambuc 374711be35a1SLionel Sambuc 374811be35a1SLionel Sambuc 374911be35a1SLionel Sambuc 375011be35a1SLionel Sambuc 375111be35a1SLionel Sambuc<hr><h3><a name="lua_isnone"><code>lua_isnone</code></a></h3><p> 3752*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 375311be35a1SLionel Sambuc<pre>int lua_isnone (lua_State *L, int index);</pre> 375411be35a1SLionel Sambuc 375511be35a1SLionel Sambuc<p> 3756*0a6a1f1dSLionel SambucReturns 1 if the given index is not valid, 375711be35a1SLionel Sambucand 0 otherwise. 375811be35a1SLionel Sambuc 375911be35a1SLionel Sambuc 376011be35a1SLionel Sambuc 376111be35a1SLionel Sambuc 376211be35a1SLionel Sambuc 376311be35a1SLionel Sambuc<hr><h3><a name="lua_isnoneornil"><code>lua_isnoneornil</code></a></h3><p> 3764*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 376511be35a1SLionel Sambuc<pre>int lua_isnoneornil (lua_State *L, int index);</pre> 376611be35a1SLionel Sambuc 376711be35a1SLionel Sambuc<p> 3768*0a6a1f1dSLionel SambucReturns 1 if the given index is not valid 376911be35a1SLionel Sambucor if the value at this index is <b>nil</b>, 377011be35a1SLionel Sambucand 0 otherwise. 377111be35a1SLionel Sambuc 377211be35a1SLionel Sambuc 377311be35a1SLionel Sambuc 377411be35a1SLionel Sambuc 377511be35a1SLionel Sambuc 377611be35a1SLionel Sambuc<hr><h3><a name="lua_isnumber"><code>lua_isnumber</code></a></h3><p> 3777*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 377811be35a1SLionel Sambuc<pre>int lua_isnumber (lua_State *L, int index);</pre> 377911be35a1SLionel Sambuc 378011be35a1SLionel Sambuc<p> 3781*0a6a1f1dSLionel SambucReturns 1 if the value at the given index is a number 378211be35a1SLionel Sambucor a string convertible to a number, 378311be35a1SLionel Sambucand 0 otherwise. 378411be35a1SLionel Sambuc 378511be35a1SLionel Sambuc 378611be35a1SLionel Sambuc 378711be35a1SLionel Sambuc 378811be35a1SLionel Sambuc 378911be35a1SLionel Sambuc<hr><h3><a name="lua_isstring"><code>lua_isstring</code></a></h3><p> 3790*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 379111be35a1SLionel Sambuc<pre>int lua_isstring (lua_State *L, int index);</pre> 379211be35a1SLionel Sambuc 379311be35a1SLionel Sambuc<p> 3794*0a6a1f1dSLionel SambucReturns 1 if the value at the given index is a string 379511be35a1SLionel Sambucor a number (which is always convertible to a string), 379611be35a1SLionel Sambucand 0 otherwise. 379711be35a1SLionel Sambuc 379811be35a1SLionel Sambuc 379911be35a1SLionel Sambuc 380011be35a1SLionel Sambuc 380111be35a1SLionel Sambuc 380211be35a1SLionel Sambuc<hr><h3><a name="lua_istable"><code>lua_istable</code></a></h3><p> 3803*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 380411be35a1SLionel Sambuc<pre>int lua_istable (lua_State *L, int index);</pre> 380511be35a1SLionel Sambuc 380611be35a1SLionel Sambuc<p> 3807*0a6a1f1dSLionel SambucReturns 1 if the value at the given index is a table, 380811be35a1SLionel Sambucand 0 otherwise. 380911be35a1SLionel Sambuc 381011be35a1SLionel Sambuc 381111be35a1SLionel Sambuc 381211be35a1SLionel Sambuc 381311be35a1SLionel Sambuc 381411be35a1SLionel Sambuc<hr><h3><a name="lua_isthread"><code>lua_isthread</code></a></h3><p> 3815*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 381611be35a1SLionel Sambuc<pre>int lua_isthread (lua_State *L, int index);</pre> 381711be35a1SLionel Sambuc 381811be35a1SLionel Sambuc<p> 3819*0a6a1f1dSLionel SambucReturns 1 if the value at the given index is a thread, 382011be35a1SLionel Sambucand 0 otherwise. 382111be35a1SLionel Sambuc 382211be35a1SLionel Sambuc 382311be35a1SLionel Sambuc 382411be35a1SLionel Sambuc 382511be35a1SLionel Sambuc 382611be35a1SLionel Sambuc<hr><h3><a name="lua_isuserdata"><code>lua_isuserdata</code></a></h3><p> 3827*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 382811be35a1SLionel Sambuc<pre>int lua_isuserdata (lua_State *L, int index);</pre> 382911be35a1SLionel Sambuc 383011be35a1SLionel Sambuc<p> 3831*0a6a1f1dSLionel SambucReturns 1 if the value at the given index is a userdata 383211be35a1SLionel Sambuc(either full or light), and 0 otherwise. 383311be35a1SLionel Sambuc 383411be35a1SLionel Sambuc 383511be35a1SLionel Sambuc 383611be35a1SLionel Sambuc 383711be35a1SLionel Sambuc 3838*0a6a1f1dSLionel Sambuc<hr><h3><a name="lua_isyieldable"><code>lua_isyieldable</code></a></h3><p> 3839*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 3840*0a6a1f1dSLionel Sambuc<pre>int lua_isyieldable (lua_State *L);</pre> 384111be35a1SLionel Sambuc 384211be35a1SLionel Sambuc<p> 3843*0a6a1f1dSLionel SambucReturns 1 if the given coroutine can yield, 3844*0a6a1f1dSLionel Sambucand 0 otherwise. 3845*0a6a1f1dSLionel Sambuc 3846*0a6a1f1dSLionel Sambuc 3847*0a6a1f1dSLionel Sambuc 3848*0a6a1f1dSLionel Sambuc 3849*0a6a1f1dSLionel Sambuc 3850*0a6a1f1dSLionel Sambuc<hr><h3><a name="lua_KContext"><code>lua_KContext</code></a></h3> 3851*0a6a1f1dSLionel Sambuc<pre>typedef ... lua_KContext;</pre> 3852*0a6a1f1dSLionel Sambuc 3853*0a6a1f1dSLionel Sambuc<p> 3854*0a6a1f1dSLionel SambucThe type for continuation-function contexts. 3855*0a6a1f1dSLionel SambucIt must be a numeric type. 3856*0a6a1f1dSLionel SambucThis type is defined as <code>intptr_t</code> 3857*0a6a1f1dSLionel Sambucwhen <code>intptr_t</code> is available, 3858*0a6a1f1dSLionel Sambucso that it can store pointers too. 3859*0a6a1f1dSLionel SambucOtherwise, it is defined as <code>ptrdiff_t</code>. 3860*0a6a1f1dSLionel Sambuc 3861*0a6a1f1dSLionel Sambuc 3862*0a6a1f1dSLionel Sambuc 3863*0a6a1f1dSLionel Sambuc 3864*0a6a1f1dSLionel Sambuc 3865*0a6a1f1dSLionel Sambuc<hr><h3><a name="lua_KFunction"><code>lua_KFunction</code></a></h3> 3866*0a6a1f1dSLionel Sambuc<pre>typedef int (*lua_KFunction) (lua_State *L, int status, lua_KContext ctx);</pre> 3867*0a6a1f1dSLionel Sambuc 3868*0a6a1f1dSLionel Sambuc<p> 3869*0a6a1f1dSLionel SambucType for continuation functions (see <a href="#4.7">§4.7</a>). 3870*0a6a1f1dSLionel Sambuc 3871*0a6a1f1dSLionel Sambuc 3872*0a6a1f1dSLionel Sambuc 3873*0a6a1f1dSLionel Sambuc 3874*0a6a1f1dSLionel Sambuc 3875*0a6a1f1dSLionel Sambuc<hr><h3><a name="lua_len"><code>lua_len</code></a></h3><p> 3876*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, <em>e</em>]</span> 3877*0a6a1f1dSLionel Sambuc<pre>void lua_len (lua_State *L, int index);</pre> 3878*0a6a1f1dSLionel Sambuc 3879*0a6a1f1dSLionel Sambuc<p> 3880*0a6a1f1dSLionel SambucReturns the length of the value at the given index. 3881*0a6a1f1dSLionel SambucIt is equivalent to the '<code>#</code>' operator in Lua (see <a href="#3.4.7">§3.4.7</a>) and 3882*0a6a1f1dSLionel Sambucmay trigger a metamethod for the "length" event (see <a href="#2.4">§2.4</a>). 3883*0a6a1f1dSLionel SambucThe result is pushed on the stack. 388411be35a1SLionel Sambuc 388511be35a1SLionel Sambuc 388611be35a1SLionel Sambuc 388711be35a1SLionel Sambuc 388811be35a1SLionel Sambuc 388911be35a1SLionel Sambuc<hr><h3><a name="lua_load"><code>lua_load</code></a></h3><p> 3890*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, –]</span> 389111be35a1SLionel Sambuc<pre>int lua_load (lua_State *L, 389211be35a1SLionel Sambuc lua_Reader reader, 389311be35a1SLionel Sambuc void *data, 3894*0a6a1f1dSLionel Sambuc const char *chunkname, 3895*0a6a1f1dSLionel Sambuc const char *mode);</pre> 389611be35a1SLionel Sambuc 389711be35a1SLionel Sambuc<p> 3898*0a6a1f1dSLionel SambucLoads a Lua chunk without running it. 389911be35a1SLionel SambucIf there are no errors, 3900*0a6a1f1dSLionel Sambuc<code>lua_load</code> pushes the compiled chunk as a Lua 390111be35a1SLionel Sambucfunction on top of the stack. 390211be35a1SLionel SambucOtherwise, it pushes an error message. 3903*0a6a1f1dSLionel Sambuc 3904*0a6a1f1dSLionel Sambuc 3905*0a6a1f1dSLionel Sambuc<p> 3906*0a6a1f1dSLionel SambucThe return values of <code>lua_load</code> are: 390711be35a1SLionel Sambuc 390811be35a1SLionel Sambuc<ul> 390911be35a1SLionel Sambuc 3910*0a6a1f1dSLionel Sambuc<li><b><a href="#pdf-LUA_OK"><code>LUA_OK</code></a>: </b> no errors;</li> 391111be35a1SLionel Sambuc 391211be35a1SLionel Sambuc<li><b><a name="pdf-LUA_ERRSYNTAX"><code>LUA_ERRSYNTAX</code></a>: </b> 3913*0a6a1f1dSLionel Sambucsyntax error during precompilation;</li> 391411be35a1SLionel Sambuc 391511be35a1SLionel Sambuc<li><b><a href="#pdf-LUA_ERRMEM"><code>LUA_ERRMEM</code></a>: </b> 3916*0a6a1f1dSLionel Sambucmemory allocation error;</li> 3917*0a6a1f1dSLionel Sambuc 3918*0a6a1f1dSLionel Sambuc<li><b><a href="#pdf-LUA_ERRGCMM"><code>LUA_ERRGCMM</code></a>: </b> 3919*0a6a1f1dSLionel Sambucerror while running a <code>__gc</code> metamethod. 3920*0a6a1f1dSLionel Sambuc(This error has no relation with the chunk being loaded. 3921*0a6a1f1dSLionel SambucIt is generated by the garbage collector.) 3922*0a6a1f1dSLionel Sambuc</li> 392311be35a1SLionel Sambuc 392411be35a1SLionel Sambuc</ul> 392511be35a1SLionel Sambuc 392611be35a1SLionel Sambuc<p> 3927*0a6a1f1dSLionel SambucThe <code>lua_load</code> function uses a user-supplied <code>reader</code> function 392811be35a1SLionel Sambucto read the chunk (see <a href="#lua_Reader"><code>lua_Reader</code></a>). 392911be35a1SLionel SambucThe <code>data</code> argument is an opaque value passed to the reader function. 393011be35a1SLionel Sambuc 393111be35a1SLionel Sambuc 393211be35a1SLionel Sambuc<p> 393311be35a1SLionel SambucThe <code>chunkname</code> argument gives a name to the chunk, 3934*0a6a1f1dSLionel Sambucwhich is used for error messages and in debug information (see <a href="#4.9">§4.9</a>). 3935*0a6a1f1dSLionel Sambuc 3936*0a6a1f1dSLionel Sambuc 3937*0a6a1f1dSLionel Sambuc<p> 3938*0a6a1f1dSLionel Sambuc<code>lua_load</code> automatically detects whether the chunk is text or binary 3939*0a6a1f1dSLionel Sambucand loads it accordingly (see program <code>luac</code>). 3940*0a6a1f1dSLionel SambucThe string <code>mode</code> works as in function <a href="#pdf-load"><code>load</code></a>, 3941*0a6a1f1dSLionel Sambucwith the addition that 3942*0a6a1f1dSLionel Sambuca <code>NULL</code> value is equivalent to the string "<code>bt</code>". 3943*0a6a1f1dSLionel Sambuc 3944*0a6a1f1dSLionel Sambuc 3945*0a6a1f1dSLionel Sambuc<p> 3946*0a6a1f1dSLionel Sambuc<code>lua_load</code> uses the stack internally, 3947*0a6a1f1dSLionel Sambucso the reader function must always leave the stack 3948*0a6a1f1dSLionel Sambucunmodified when returning. 3949*0a6a1f1dSLionel Sambuc 3950*0a6a1f1dSLionel Sambuc 3951*0a6a1f1dSLionel Sambuc<p> 3952*0a6a1f1dSLionel SambucIf the resulting function has upvalues, 3953*0a6a1f1dSLionel Sambucits first upvalue is set to the value of the global environment 3954*0a6a1f1dSLionel Sambucstored at index <code>LUA_RIDX_GLOBALS</code> in the registry (see <a href="#4.5">§4.5</a>). 3955*0a6a1f1dSLionel SambucWhen loading main chunks, 3956*0a6a1f1dSLionel Sambucthis upvalue will be the <code>_ENV</code> variable (see <a href="#2.2">§2.2</a>). 3957*0a6a1f1dSLionel SambucOther upvalues are initialized with <b>nil</b>. 395811be35a1SLionel Sambuc 395911be35a1SLionel Sambuc 396011be35a1SLionel Sambuc 396111be35a1SLionel Sambuc 396211be35a1SLionel Sambuc 396311be35a1SLionel Sambuc<hr><h3><a name="lua_newstate"><code>lua_newstate</code></a></h3><p> 3964*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 396511be35a1SLionel Sambuc<pre>lua_State *lua_newstate (lua_Alloc f, void *ud);</pre> 396611be35a1SLionel Sambuc 396711be35a1SLionel Sambuc<p> 3968*0a6a1f1dSLionel SambucCreates a new thread running in a new, independent state. 3969*0a6a1f1dSLionel SambucReturns <code>NULL</code> if it cannot create the thread or the state 397011be35a1SLionel Sambuc(due to lack of memory). 397111be35a1SLionel SambucThe argument <code>f</code> is the allocator function; 397211be35a1SLionel SambucLua does all memory allocation for this state through this function. 397311be35a1SLionel SambucThe second argument, <code>ud</code>, is an opaque pointer that Lua 3974*0a6a1f1dSLionel Sambucpasses to the allocator in every call. 397511be35a1SLionel Sambuc 397611be35a1SLionel Sambuc 397711be35a1SLionel Sambuc 397811be35a1SLionel Sambuc 397911be35a1SLionel Sambuc 398011be35a1SLionel Sambuc<hr><h3><a name="lua_newtable"><code>lua_newtable</code></a></h3><p> 3981*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, <em>e</em>]</span> 398211be35a1SLionel Sambuc<pre>void lua_newtable (lua_State *L);</pre> 398311be35a1SLionel Sambuc 398411be35a1SLionel Sambuc<p> 398511be35a1SLionel SambucCreates a new empty table and pushes it onto the stack. 398611be35a1SLionel SambucIt is equivalent to <code>lua_createtable(L, 0, 0)</code>. 398711be35a1SLionel Sambuc 398811be35a1SLionel Sambuc 398911be35a1SLionel Sambuc 399011be35a1SLionel Sambuc 399111be35a1SLionel Sambuc 399211be35a1SLionel Sambuc<hr><h3><a name="lua_newthread"><code>lua_newthread</code></a></h3><p> 3993*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, <em>e</em>]</span> 399411be35a1SLionel Sambuc<pre>lua_State *lua_newthread (lua_State *L);</pre> 399511be35a1SLionel Sambuc 399611be35a1SLionel Sambuc<p> 399711be35a1SLionel SambucCreates a new thread, pushes it on the stack, 399811be35a1SLionel Sambucand returns a pointer to a <a href="#lua_State"><code>lua_State</code></a> that represents this new thread. 3999*0a6a1f1dSLionel SambucThe new thread returned by this function shares with the original thread 4000*0a6a1f1dSLionel Sambucits global environment, 400111be35a1SLionel Sambucbut has an independent execution stack. 400211be35a1SLionel Sambuc 400311be35a1SLionel Sambuc 400411be35a1SLionel Sambuc<p> 400511be35a1SLionel SambucThere is no explicit function to close or to destroy a thread. 400611be35a1SLionel SambucThreads are subject to garbage collection, 400711be35a1SLionel Sambuclike any Lua object. 400811be35a1SLionel Sambuc 400911be35a1SLionel Sambuc 401011be35a1SLionel Sambuc 401111be35a1SLionel Sambuc 401211be35a1SLionel Sambuc 401311be35a1SLionel Sambuc<hr><h3><a name="lua_newuserdata"><code>lua_newuserdata</code></a></h3><p> 4014*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, <em>e</em>]</span> 401511be35a1SLionel Sambuc<pre>void *lua_newuserdata (lua_State *L, size_t size);</pre> 401611be35a1SLionel Sambuc 401711be35a1SLionel Sambuc<p> 401811be35a1SLionel SambucThis function allocates a new block of memory with the given size, 401911be35a1SLionel Sambucpushes onto the stack a new full userdata with the block address, 402011be35a1SLionel Sambucand returns this address. 4021*0a6a1f1dSLionel SambucThe host program can freely use this memory. 402211be35a1SLionel Sambuc 402311be35a1SLionel Sambuc 402411be35a1SLionel Sambuc 402511be35a1SLionel Sambuc 402611be35a1SLionel Sambuc 402711be35a1SLionel Sambuc<hr><h3><a name="lua_next"><code>lua_next</code></a></h3><p> 402811be35a1SLionel Sambuc<span class="apii">[-1, +(2|0), <em>e</em>]</span> 402911be35a1SLionel Sambuc<pre>int lua_next (lua_State *L, int index);</pre> 403011be35a1SLionel Sambuc 403111be35a1SLionel Sambuc<p> 403211be35a1SLionel SambucPops a key from the stack, 4033*0a6a1f1dSLionel Sambucand pushes a key–value pair from the table at the given index 403411be35a1SLionel Sambuc(the "next" pair after the given key). 403511be35a1SLionel SambucIf there are no more elements in the table, 403611be35a1SLionel Sambucthen <a href="#lua_next"><code>lua_next</code></a> returns 0 (and pushes nothing). 403711be35a1SLionel Sambuc 403811be35a1SLionel Sambuc 403911be35a1SLionel Sambuc<p> 404011be35a1SLionel SambucA typical traversal looks like this: 404111be35a1SLionel Sambuc 404211be35a1SLionel Sambuc<pre> 404311be35a1SLionel Sambuc /* table is in the stack at index 't' */ 404411be35a1SLionel Sambuc lua_pushnil(L); /* first key */ 404511be35a1SLionel Sambuc while (lua_next(L, t) != 0) { 404611be35a1SLionel Sambuc /* uses 'key' (at index -2) and 'value' (at index -1) */ 404711be35a1SLionel Sambuc printf("%s - %s\n", 404811be35a1SLionel Sambuc lua_typename(L, lua_type(L, -2)), 404911be35a1SLionel Sambuc lua_typename(L, lua_type(L, -1))); 405011be35a1SLionel Sambuc /* removes 'value'; keeps 'key' for next iteration */ 405111be35a1SLionel Sambuc lua_pop(L, 1); 405211be35a1SLionel Sambuc } 405311be35a1SLionel Sambuc</pre> 405411be35a1SLionel Sambuc 405511be35a1SLionel Sambuc<p> 405611be35a1SLionel SambucWhile traversing a table, 405711be35a1SLionel Sambucdo not call <a href="#lua_tolstring"><code>lua_tolstring</code></a> directly on a key, 405811be35a1SLionel Sambucunless you know that the key is actually a string. 4059*0a6a1f1dSLionel SambucRecall that <a href="#lua_tolstring"><code>lua_tolstring</code></a> may change 406011be35a1SLionel Sambucthe value at the given index; 406111be35a1SLionel Sambucthis confuses the next call to <a href="#lua_next"><code>lua_next</code></a>. 406211be35a1SLionel Sambuc 406311be35a1SLionel Sambuc 4064*0a6a1f1dSLionel Sambuc<p> 4065*0a6a1f1dSLionel SambucSee function <a href="#pdf-next"><code>next</code></a> for the caveats of modifying 4066*0a6a1f1dSLionel Sambucthe table during its traversal. 4067*0a6a1f1dSLionel Sambuc 4068*0a6a1f1dSLionel Sambuc 406911be35a1SLionel Sambuc 407011be35a1SLionel Sambuc 407111be35a1SLionel Sambuc 407211be35a1SLionel Sambuc<hr><h3><a name="lua_Number"><code>lua_Number</code></a></h3> 4073*0a6a1f1dSLionel Sambuc<pre>typedef ... lua_Number;</pre> 407411be35a1SLionel Sambuc 407511be35a1SLionel Sambuc<p> 4076*0a6a1f1dSLionel SambucThe type of floats in Lua. 407711be35a1SLionel Sambuc 407811be35a1SLionel Sambuc 407911be35a1SLionel Sambuc<p> 4080*0a6a1f1dSLionel SambucBy default this type is double, 4081*0a6a1f1dSLionel Sambucbut that can be changed to a single float or a long double. 4082*0a6a1f1dSLionel Sambuc(See <code>LUA_FLOAT_TYPE</code> in <code>luaconf.h</code>.) 408311be35a1SLionel Sambuc 408411be35a1SLionel Sambuc 408511be35a1SLionel Sambuc 408611be35a1SLionel Sambuc 408711be35a1SLionel Sambuc 4088*0a6a1f1dSLionel Sambuc<hr><h3><a name="lua_numbertointeger"><code>lua_numbertointeger</code></a></h3> 4089*0a6a1f1dSLionel Sambuc<pre>int lua_numbertointeger (lua_Number n, lua_Integer *p);</pre> 409011be35a1SLionel Sambuc 409111be35a1SLionel Sambuc<p> 4092*0a6a1f1dSLionel SambucConverts a Lua float to a Lua integer. 4093*0a6a1f1dSLionel SambucThis macro assumes that <code>n</code> has an integral value. 4094*0a6a1f1dSLionel SambucIf that value is within the range of Lua integers, 4095*0a6a1f1dSLionel Sambucit is converted to an integer and assigned to <code>*p</code>. 4096*0a6a1f1dSLionel SambucThe macro results in a boolean indicating whether the 4097*0a6a1f1dSLionel Sambucconversion was successful. 4098*0a6a1f1dSLionel Sambuc(Note that this range test can be tricky to do 4099*0a6a1f1dSLionel Sambuccorrectly without this macro, 4100*0a6a1f1dSLionel Sambucdue to roundings.) 4101*0a6a1f1dSLionel Sambuc 4102*0a6a1f1dSLionel Sambuc 4103*0a6a1f1dSLionel Sambuc<p> 4104*0a6a1f1dSLionel SambucThis macro may evaluate its arguments more than once. 410511be35a1SLionel Sambuc 410611be35a1SLionel Sambuc 410711be35a1SLionel Sambuc 410811be35a1SLionel Sambuc 410911be35a1SLionel Sambuc 411011be35a1SLionel Sambuc<hr><h3><a name="lua_pcall"><code>lua_pcall</code></a></h3><p> 4111*0a6a1f1dSLionel Sambuc<span class="apii">[-(nargs + 1), +(nresults|1), –]</span> 4112*0a6a1f1dSLionel Sambuc<pre>int lua_pcall (lua_State *L, int nargs, int nresults, int msgh);</pre> 411311be35a1SLionel Sambuc 411411be35a1SLionel Sambuc<p> 411511be35a1SLionel SambucCalls a function in protected mode. 411611be35a1SLionel Sambuc 411711be35a1SLionel Sambuc 411811be35a1SLionel Sambuc<p> 411911be35a1SLionel SambucBoth <code>nargs</code> and <code>nresults</code> have the same meaning as 412011be35a1SLionel Sambucin <a href="#lua_call"><code>lua_call</code></a>. 412111be35a1SLionel SambucIf there are no errors during the call, 412211be35a1SLionel Sambuc<a href="#lua_pcall"><code>lua_pcall</code></a> behaves exactly like <a href="#lua_call"><code>lua_call</code></a>. 412311be35a1SLionel SambucHowever, if there is any error, 412411be35a1SLionel Sambuc<a href="#lua_pcall"><code>lua_pcall</code></a> catches it, 412511be35a1SLionel Sambucpushes a single value on the stack (the error message), 412611be35a1SLionel Sambucand returns an error code. 412711be35a1SLionel SambucLike <a href="#lua_call"><code>lua_call</code></a>, 412811be35a1SLionel Sambuc<a href="#lua_pcall"><code>lua_pcall</code></a> always removes the function 412911be35a1SLionel Sambucand its arguments from the stack. 413011be35a1SLionel Sambuc 413111be35a1SLionel Sambuc 413211be35a1SLionel Sambuc<p> 4133*0a6a1f1dSLionel SambucIf <code>msgh</code> is 0, 413411be35a1SLionel Sambucthen the error message returned on the stack 413511be35a1SLionel Sambucis exactly the original error message. 4136*0a6a1f1dSLionel SambucOtherwise, <code>msgh</code> is the stack index of a 4137*0a6a1f1dSLionel Sambuc<em>message handler</em>. 4138*0a6a1f1dSLionel Sambuc(This index cannot be a pseudo-index.) 413911be35a1SLionel SambucIn case of runtime errors, 414011be35a1SLionel Sambucthis function will be called with the error message 4141*0a6a1f1dSLionel Sambucand its return value will be the message 4142*0a6a1f1dSLionel Sambucreturned on the stack by <a href="#lua_pcall"><code>lua_pcall</code></a>. 414311be35a1SLionel Sambuc 414411be35a1SLionel Sambuc 414511be35a1SLionel Sambuc<p> 4146*0a6a1f1dSLionel SambucTypically, the message handler is used to add more debug 414711be35a1SLionel Sambucinformation to the error message, such as a stack traceback. 414811be35a1SLionel SambucSuch information cannot be gathered after the return of <a href="#lua_pcall"><code>lua_pcall</code></a>, 414911be35a1SLionel Sambucsince by then the stack has unwound. 415011be35a1SLionel Sambuc 415111be35a1SLionel Sambuc 415211be35a1SLionel Sambuc<p> 4153*0a6a1f1dSLionel SambucThe <a href="#lua_pcall"><code>lua_pcall</code></a> function returns one of the following constants 415411be35a1SLionel Sambuc(defined in <code>lua.h</code>): 415511be35a1SLionel Sambuc 415611be35a1SLionel Sambuc<ul> 415711be35a1SLionel Sambuc 4158*0a6a1f1dSLionel Sambuc<li><b><a name="pdf-LUA_OK"><code>LUA_OK</code></a> (0): </b> 4159*0a6a1f1dSLionel Sambucsuccess.</li> 4160*0a6a1f1dSLionel Sambuc 416111be35a1SLionel Sambuc<li><b><a name="pdf-LUA_ERRRUN"><code>LUA_ERRRUN</code></a>: </b> 416211be35a1SLionel Sambuca runtime error. 416311be35a1SLionel Sambuc</li> 416411be35a1SLionel Sambuc 416511be35a1SLionel Sambuc<li><b><a name="pdf-LUA_ERRMEM"><code>LUA_ERRMEM</code></a>: </b> 416611be35a1SLionel Sambucmemory allocation error. 4167*0a6a1f1dSLionel SambucFor such errors, Lua does not call the message handler. 416811be35a1SLionel Sambuc</li> 416911be35a1SLionel Sambuc 417011be35a1SLionel Sambuc<li><b><a name="pdf-LUA_ERRERR"><code>LUA_ERRERR</code></a>: </b> 4171*0a6a1f1dSLionel Sambucerror while running the message handler. 4172*0a6a1f1dSLionel Sambuc</li> 4173*0a6a1f1dSLionel Sambuc 4174*0a6a1f1dSLionel Sambuc<li><b><a name="pdf-LUA_ERRGCMM"><code>LUA_ERRGCMM</code></a>: </b> 4175*0a6a1f1dSLionel Sambucerror while running a <code>__gc</code> metamethod. 4176*0a6a1f1dSLionel Sambuc(This error typically has no relation with the function being called.) 417711be35a1SLionel Sambuc</li> 417811be35a1SLionel Sambuc 417911be35a1SLionel Sambuc</ul> 418011be35a1SLionel Sambuc 418111be35a1SLionel Sambuc 418211be35a1SLionel Sambuc 418311be35a1SLionel Sambuc 4184*0a6a1f1dSLionel Sambuc<hr><h3><a name="lua_pcallk"><code>lua_pcallk</code></a></h3><p> 4185*0a6a1f1dSLionel Sambuc<span class="apii">[-(nargs + 1), +(nresults|1), –]</span> 4186*0a6a1f1dSLionel Sambuc<pre>int lua_pcallk (lua_State *L, 4187*0a6a1f1dSLionel Sambuc int nargs, 4188*0a6a1f1dSLionel Sambuc int nresults, 4189*0a6a1f1dSLionel Sambuc int msgh, 4190*0a6a1f1dSLionel Sambuc lua_KContext ctx, 4191*0a6a1f1dSLionel Sambuc lua_KFunction k);</pre> 4192*0a6a1f1dSLionel Sambuc 4193*0a6a1f1dSLionel Sambuc<p> 4194*0a6a1f1dSLionel SambucThis function behaves exactly like <a href="#lua_pcall"><code>lua_pcall</code></a>, 4195*0a6a1f1dSLionel Sambucbut allows the called function to yield (see <a href="#4.7">§4.7</a>). 4196*0a6a1f1dSLionel Sambuc 4197*0a6a1f1dSLionel Sambuc 4198*0a6a1f1dSLionel Sambuc 4199*0a6a1f1dSLionel Sambuc 4200*0a6a1f1dSLionel Sambuc 420111be35a1SLionel Sambuc<hr><h3><a name="lua_pop"><code>lua_pop</code></a></h3><p> 4202*0a6a1f1dSLionel Sambuc<span class="apii">[-n, +0, –]</span> 420311be35a1SLionel Sambuc<pre>void lua_pop (lua_State *L, int n);</pre> 420411be35a1SLionel Sambuc 420511be35a1SLionel Sambuc<p> 420611be35a1SLionel SambucPops <code>n</code> elements from the stack. 420711be35a1SLionel Sambuc 420811be35a1SLionel Sambuc 420911be35a1SLionel Sambuc 421011be35a1SLionel Sambuc 421111be35a1SLionel Sambuc 421211be35a1SLionel Sambuc<hr><h3><a name="lua_pushboolean"><code>lua_pushboolean</code></a></h3><p> 4213*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, –]</span> 421411be35a1SLionel Sambuc<pre>void lua_pushboolean (lua_State *L, int b);</pre> 421511be35a1SLionel Sambuc 421611be35a1SLionel Sambuc<p> 421711be35a1SLionel SambucPushes a boolean value with value <code>b</code> onto the stack. 421811be35a1SLionel Sambuc 421911be35a1SLionel Sambuc 422011be35a1SLionel Sambuc 422111be35a1SLionel Sambuc 422211be35a1SLionel Sambuc 422311be35a1SLionel Sambuc<hr><h3><a name="lua_pushcclosure"><code>lua_pushcclosure</code></a></h3><p> 4224*0a6a1f1dSLionel Sambuc<span class="apii">[-n, +1, <em>e</em>]</span> 422511be35a1SLionel Sambuc<pre>void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);</pre> 422611be35a1SLionel Sambuc 422711be35a1SLionel Sambuc<p> 422811be35a1SLionel SambucPushes a new C closure onto the stack. 422911be35a1SLionel Sambuc 423011be35a1SLionel Sambuc 423111be35a1SLionel Sambuc<p> 423211be35a1SLionel SambucWhen a C function is created, 423311be35a1SLionel Sambucit is possible to associate some values with it, 4234*0a6a1f1dSLionel Sambucthus creating a C closure (see <a href="#4.4">§4.4</a>); 423511be35a1SLionel Sambucthese values are then accessible to the function whenever it is called. 423611be35a1SLionel SambucTo associate values with a C function, 4237*0a6a1f1dSLionel Sambucfirst these values must be pushed onto the stack 423811be35a1SLionel Sambuc(when there are multiple values, the first value is pushed first). 423911be35a1SLionel SambucThen <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a> 424011be35a1SLionel Sambucis called to create and push the C function onto the stack, 4241*0a6a1f1dSLionel Sambucwith the argument <code>n</code> telling how many values will be 424211be35a1SLionel Sambucassociated with the function. 424311be35a1SLionel Sambuc<a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a> also pops these values from the stack. 424411be35a1SLionel Sambuc 424511be35a1SLionel Sambuc 424611be35a1SLionel Sambuc<p> 424711be35a1SLionel SambucThe maximum value for <code>n</code> is 255. 424811be35a1SLionel Sambuc 424911be35a1SLionel Sambuc 4250*0a6a1f1dSLionel Sambuc<p> 4251*0a6a1f1dSLionel SambucWhen <code>n</code> is zero, 4252*0a6a1f1dSLionel Sambucthis function creates a <em>light C function</em>, 4253*0a6a1f1dSLionel Sambucwhich is just a pointer to the C function. 4254*0a6a1f1dSLionel SambucIn that case, it never raises a memory error. 4255*0a6a1f1dSLionel Sambuc 4256*0a6a1f1dSLionel Sambuc 425711be35a1SLionel Sambuc 425811be35a1SLionel Sambuc 425911be35a1SLionel Sambuc 426011be35a1SLionel Sambuc<hr><h3><a name="lua_pushcfunction"><code>lua_pushcfunction</code></a></h3><p> 4261*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, –]</span> 426211be35a1SLionel Sambuc<pre>void lua_pushcfunction (lua_State *L, lua_CFunction f);</pre> 426311be35a1SLionel Sambuc 426411be35a1SLionel Sambuc<p> 426511be35a1SLionel SambucPushes a C function onto the stack. 426611be35a1SLionel SambucThis function receives a pointer to a C function 426711be35a1SLionel Sambucand pushes onto the stack a Lua value of type <code>function</code> that, 426811be35a1SLionel Sambucwhen called, invokes the corresponding C function. 426911be35a1SLionel Sambuc 427011be35a1SLionel Sambuc 427111be35a1SLionel Sambuc<p> 4272*0a6a1f1dSLionel SambucAny function to be callable by Lua must 427311be35a1SLionel Sambucfollow the correct protocol to receive its parameters 427411be35a1SLionel Sambucand return its results (see <a href="#lua_CFunction"><code>lua_CFunction</code></a>). 427511be35a1SLionel Sambuc 427611be35a1SLionel Sambuc 427711be35a1SLionel Sambuc 427811be35a1SLionel Sambuc 427911be35a1SLionel Sambuc 428011be35a1SLionel Sambuc<hr><h3><a name="lua_pushfstring"><code>lua_pushfstring</code></a></h3><p> 4281*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, <em>e</em>]</span> 428211be35a1SLionel Sambuc<pre>const char *lua_pushfstring (lua_State *L, const char *fmt, ...);</pre> 428311be35a1SLionel Sambuc 428411be35a1SLionel Sambuc<p> 428511be35a1SLionel SambucPushes onto the stack a formatted string 428611be35a1SLionel Sambucand returns a pointer to this string. 4287*0a6a1f1dSLionel SambucIt is similar to the ISO C function <code>sprintf</code>, 428811be35a1SLionel Sambucbut has some important differences: 428911be35a1SLionel Sambuc 429011be35a1SLionel Sambuc<ul> 429111be35a1SLionel Sambuc 429211be35a1SLionel Sambuc<li> 429311be35a1SLionel SambucYou do not have to allocate space for the result: 429411be35a1SLionel Sambucthe result is a Lua string and Lua takes care of memory allocation 429511be35a1SLionel Sambuc(and deallocation, through garbage collection). 429611be35a1SLionel Sambuc</li> 429711be35a1SLionel Sambuc 429811be35a1SLionel Sambuc<li> 429911be35a1SLionel SambucThe conversion specifiers are quite restricted. 430011be35a1SLionel SambucThere are no flags, widths, or precisions. 430111be35a1SLionel SambucThe conversion specifiers can only be 4302*0a6a1f1dSLionel Sambuc'<code>%%</code>' (inserts the character '<code>%</code>'), 430311be35a1SLionel Sambuc'<code>%s</code>' (inserts a zero-terminated string, with no size restrictions), 430411be35a1SLionel Sambuc'<code>%f</code>' (inserts a <a href="#lua_Number"><code>lua_Number</code></a>), 4305*0a6a1f1dSLionel Sambuc'<code>%I</code>' (inserts a <a href="#lua_Integer"><code>lua_Integer</code></a>), 430611be35a1SLionel Sambuc'<code>%p</code>' (inserts a pointer as a hexadecimal numeral), 4307*0a6a1f1dSLionel Sambuc'<code>%d</code>' (inserts an <code>int</code>), 4308*0a6a1f1dSLionel Sambuc'<code>%c</code>' (inserts an <code>int</code> as a one-byte character), and 4309*0a6a1f1dSLionel Sambuc'<code>%U</code>' (inserts a <code>long int</code> as a UTF-8 byte sequence). 431011be35a1SLionel Sambuc</li> 431111be35a1SLionel Sambuc 431211be35a1SLionel Sambuc</ul> 431311be35a1SLionel Sambuc 431411be35a1SLionel Sambuc 431511be35a1SLionel Sambuc 431611be35a1SLionel Sambuc 4317*0a6a1f1dSLionel Sambuc<hr><h3><a name="lua_pushglobaltable"><code>lua_pushglobaltable</code></a></h3><p> 4318*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, –]</span> 4319*0a6a1f1dSLionel Sambuc<pre>void lua_pushglobaltable (lua_State *L);</pre> 4320*0a6a1f1dSLionel Sambuc 4321*0a6a1f1dSLionel Sambuc<p> 4322*0a6a1f1dSLionel SambucPushes the global environment onto the stack. 4323*0a6a1f1dSLionel Sambuc 4324*0a6a1f1dSLionel Sambuc 4325*0a6a1f1dSLionel Sambuc 4326*0a6a1f1dSLionel Sambuc 4327*0a6a1f1dSLionel Sambuc 432811be35a1SLionel Sambuc<hr><h3><a name="lua_pushinteger"><code>lua_pushinteger</code></a></h3><p> 4329*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, –]</span> 433011be35a1SLionel Sambuc<pre>void lua_pushinteger (lua_State *L, lua_Integer n);</pre> 433111be35a1SLionel Sambuc 433211be35a1SLionel Sambuc<p> 4333*0a6a1f1dSLionel SambucPushes an integer with value <code>n</code> onto the stack. 433411be35a1SLionel Sambuc 433511be35a1SLionel Sambuc 433611be35a1SLionel Sambuc 433711be35a1SLionel Sambuc 433811be35a1SLionel Sambuc 433911be35a1SLionel Sambuc<hr><h3><a name="lua_pushlightuserdata"><code>lua_pushlightuserdata</code></a></h3><p> 4340*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, –]</span> 434111be35a1SLionel Sambuc<pre>void lua_pushlightuserdata (lua_State *L, void *p);</pre> 434211be35a1SLionel Sambuc 434311be35a1SLionel Sambuc<p> 434411be35a1SLionel SambucPushes a light userdata onto the stack. 434511be35a1SLionel Sambuc 434611be35a1SLionel Sambuc 434711be35a1SLionel Sambuc<p> 434811be35a1SLionel SambucUserdata represent C values in Lua. 4349*0a6a1f1dSLionel SambucA <em>light userdata</em> represents a pointer, a <code>void*</code>. 435011be35a1SLionel SambucIt is a value (like a number): 435111be35a1SLionel Sambucyou do not create it, it has no individual metatable, 435211be35a1SLionel Sambucand it is not collected (as it was never created). 435311be35a1SLionel SambucA light userdata is equal to "any" 435411be35a1SLionel Sambuclight userdata with the same C address. 435511be35a1SLionel Sambuc 435611be35a1SLionel Sambuc 435711be35a1SLionel Sambuc 435811be35a1SLionel Sambuc 435911be35a1SLionel Sambuc 436011be35a1SLionel Sambuc<hr><h3><a name="lua_pushliteral"><code>lua_pushliteral</code></a></h3><p> 4361*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, <em>e</em>]</span> 4362*0a6a1f1dSLionel Sambuc<pre>const char *lua_pushliteral (lua_State *L, const char *s);</pre> 436311be35a1SLionel Sambuc 436411be35a1SLionel Sambuc<p> 4365*0a6a1f1dSLionel SambucThis macro is equivalent to <a href="#lua_pushstring"><code>lua_pushstring</code></a>, 4366*0a6a1f1dSLionel Sambucbut should be used only when <code>s</code> is a literal string. 436711be35a1SLionel Sambuc 436811be35a1SLionel Sambuc 436911be35a1SLionel Sambuc 437011be35a1SLionel Sambuc 437111be35a1SLionel Sambuc 437211be35a1SLionel Sambuc<hr><h3><a name="lua_pushlstring"><code>lua_pushlstring</code></a></h3><p> 4373*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, <em>e</em>]</span> 4374*0a6a1f1dSLionel Sambuc<pre>const char *lua_pushlstring (lua_State *L, const char *s, size_t len);</pre> 437511be35a1SLionel Sambuc 437611be35a1SLionel Sambuc<p> 437711be35a1SLionel SambucPushes the string pointed to by <code>s</code> with size <code>len</code> 437811be35a1SLionel Sambuconto the stack. 437911be35a1SLionel SambucLua makes (or reuses) an internal copy of the given string, 438011be35a1SLionel Sambucso the memory at <code>s</code> can be freed or reused immediately after 438111be35a1SLionel Sambucthe function returns. 4382*0a6a1f1dSLionel SambucThe string can contain any binary data, 4383*0a6a1f1dSLionel Sambucincluding embedded zeros. 4384*0a6a1f1dSLionel Sambuc 4385*0a6a1f1dSLionel Sambuc 4386*0a6a1f1dSLionel Sambuc<p> 4387*0a6a1f1dSLionel SambucReturns a pointer to the internal copy of the string. 438811be35a1SLionel Sambuc 438911be35a1SLionel Sambuc 439011be35a1SLionel Sambuc 439111be35a1SLionel Sambuc 439211be35a1SLionel Sambuc 439311be35a1SLionel Sambuc<hr><h3><a name="lua_pushnil"><code>lua_pushnil</code></a></h3><p> 4394*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, –]</span> 439511be35a1SLionel Sambuc<pre>void lua_pushnil (lua_State *L);</pre> 439611be35a1SLionel Sambuc 439711be35a1SLionel Sambuc<p> 439811be35a1SLionel SambucPushes a nil value onto the stack. 439911be35a1SLionel Sambuc 440011be35a1SLionel Sambuc 440111be35a1SLionel Sambuc 440211be35a1SLionel Sambuc 440311be35a1SLionel Sambuc 440411be35a1SLionel Sambuc<hr><h3><a name="lua_pushnumber"><code>lua_pushnumber</code></a></h3><p> 4405*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, –]</span> 440611be35a1SLionel Sambuc<pre>void lua_pushnumber (lua_State *L, lua_Number n);</pre> 440711be35a1SLionel Sambuc 440811be35a1SLionel Sambuc<p> 4409*0a6a1f1dSLionel SambucPushes a float with value <code>n</code> onto the stack. 441011be35a1SLionel Sambuc 441111be35a1SLionel Sambuc 441211be35a1SLionel Sambuc 441311be35a1SLionel Sambuc 441411be35a1SLionel Sambuc 441511be35a1SLionel Sambuc<hr><h3><a name="lua_pushstring"><code>lua_pushstring</code></a></h3><p> 4416*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, <em>e</em>]</span> 4417*0a6a1f1dSLionel Sambuc<pre>const char *lua_pushstring (lua_State *L, const char *s);</pre> 441811be35a1SLionel Sambuc 441911be35a1SLionel Sambuc<p> 442011be35a1SLionel SambucPushes the zero-terminated string pointed to by <code>s</code> 442111be35a1SLionel Sambuconto the stack. 442211be35a1SLionel SambucLua makes (or reuses) an internal copy of the given string, 442311be35a1SLionel Sambucso the memory at <code>s</code> can be freed or reused immediately after 442411be35a1SLionel Sambucthe function returns. 4425*0a6a1f1dSLionel Sambuc 4426*0a6a1f1dSLionel Sambuc 4427*0a6a1f1dSLionel Sambuc<p> 4428*0a6a1f1dSLionel SambucReturns a pointer to the internal copy of the string. 4429*0a6a1f1dSLionel Sambuc 4430*0a6a1f1dSLionel Sambuc 4431*0a6a1f1dSLionel Sambuc<p> 4432*0a6a1f1dSLionel SambucIf <code>s</code> is <code>NULL</code>, pushes <b>nil</b> and returns <code>NULL</code>. 443311be35a1SLionel Sambuc 443411be35a1SLionel Sambuc 443511be35a1SLionel Sambuc 443611be35a1SLionel Sambuc 443711be35a1SLionel Sambuc 443811be35a1SLionel Sambuc<hr><h3><a name="lua_pushthread"><code>lua_pushthread</code></a></h3><p> 4439*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, –]</span> 444011be35a1SLionel Sambuc<pre>int lua_pushthread (lua_State *L);</pre> 444111be35a1SLionel Sambuc 444211be35a1SLionel Sambuc<p> 444311be35a1SLionel SambucPushes the thread represented by <code>L</code> onto the stack. 444411be35a1SLionel SambucReturns 1 if this thread is the main thread of its state. 444511be35a1SLionel Sambuc 444611be35a1SLionel Sambuc 444711be35a1SLionel Sambuc 444811be35a1SLionel Sambuc 444911be35a1SLionel Sambuc 445011be35a1SLionel Sambuc<hr><h3><a name="lua_pushvalue"><code>lua_pushvalue</code></a></h3><p> 4451*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, –]</span> 445211be35a1SLionel Sambuc<pre>void lua_pushvalue (lua_State *L, int index);</pre> 445311be35a1SLionel Sambuc 445411be35a1SLionel Sambuc<p> 4455*0a6a1f1dSLionel SambucPushes a copy of the element at the given index 445611be35a1SLionel Sambuconto the stack. 445711be35a1SLionel Sambuc 445811be35a1SLionel Sambuc 445911be35a1SLionel Sambuc 446011be35a1SLionel Sambuc 446111be35a1SLionel Sambuc 446211be35a1SLionel Sambuc<hr><h3><a name="lua_pushvfstring"><code>lua_pushvfstring</code></a></h3><p> 4463*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, <em>e</em>]</span> 446411be35a1SLionel Sambuc<pre>const char *lua_pushvfstring (lua_State *L, 446511be35a1SLionel Sambuc const char *fmt, 446611be35a1SLionel Sambuc va_list argp);</pre> 446711be35a1SLionel Sambuc 446811be35a1SLionel Sambuc<p> 446911be35a1SLionel SambucEquivalent to <a href="#lua_pushfstring"><code>lua_pushfstring</code></a>, except that it receives a <code>va_list</code> 447011be35a1SLionel Sambucinstead of a variable number of arguments. 447111be35a1SLionel Sambuc 447211be35a1SLionel Sambuc 447311be35a1SLionel Sambuc 447411be35a1SLionel Sambuc 447511be35a1SLionel Sambuc 447611be35a1SLionel Sambuc<hr><h3><a name="lua_rawequal"><code>lua_rawequal</code></a></h3><p> 4477*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 447811be35a1SLionel Sambuc<pre>int lua_rawequal (lua_State *L, int index1, int index2);</pre> 447911be35a1SLionel Sambuc 448011be35a1SLionel Sambuc<p> 4481*0a6a1f1dSLionel SambucReturns 1 if the two values in indices <code>index1</code> and 448211be35a1SLionel Sambuc<code>index2</code> are primitively equal 448311be35a1SLionel Sambuc(that is, without calling metamethods). 448411be35a1SLionel SambucOtherwise returns 0. 4485*0a6a1f1dSLionel SambucAlso returns 0 if any of the indices are not valid. 448611be35a1SLionel Sambuc 448711be35a1SLionel Sambuc 448811be35a1SLionel Sambuc 448911be35a1SLionel Sambuc 449011be35a1SLionel Sambuc 449111be35a1SLionel Sambuc<hr><h3><a name="lua_rawget"><code>lua_rawget</code></a></h3><p> 4492*0a6a1f1dSLionel Sambuc<span class="apii">[-1, +1, –]</span> 4493*0a6a1f1dSLionel Sambuc<pre>int lua_rawget (lua_State *L, int index);</pre> 449411be35a1SLionel Sambuc 449511be35a1SLionel Sambuc<p> 449611be35a1SLionel SambucSimilar to <a href="#lua_gettable"><code>lua_gettable</code></a>, but does a raw access 449711be35a1SLionel Sambuc(i.e., without metamethods). 449811be35a1SLionel Sambuc 449911be35a1SLionel Sambuc 450011be35a1SLionel Sambuc 450111be35a1SLionel Sambuc 450211be35a1SLionel Sambuc 450311be35a1SLionel Sambuc<hr><h3><a name="lua_rawgeti"><code>lua_rawgeti</code></a></h3><p> 4504*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, –]</span> 4505*0a6a1f1dSLionel Sambuc<pre>int lua_rawgeti (lua_State *L, int index, lua_Integer n);</pre> 450611be35a1SLionel Sambuc 450711be35a1SLionel Sambuc<p> 450811be35a1SLionel SambucPushes onto the stack the value <code>t[n]</code>, 4509*0a6a1f1dSLionel Sambucwhere <code>t</code> is the table at the given index. 451011be35a1SLionel SambucThe access is raw; 451111be35a1SLionel Sambucthat is, it does not invoke metamethods. 451211be35a1SLionel Sambuc 451311be35a1SLionel Sambuc 4514*0a6a1f1dSLionel Sambuc<p> 4515*0a6a1f1dSLionel SambucReturns the type of the pushed value. 4516*0a6a1f1dSLionel Sambuc 4517*0a6a1f1dSLionel Sambuc 4518*0a6a1f1dSLionel Sambuc 4519*0a6a1f1dSLionel Sambuc 4520*0a6a1f1dSLionel Sambuc 4521*0a6a1f1dSLionel Sambuc<hr><h3><a name="lua_rawgetp"><code>lua_rawgetp</code></a></h3><p> 4522*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, –]</span> 4523*0a6a1f1dSLionel Sambuc<pre>int lua_rawgetp (lua_State *L, int index, const void *p);</pre> 4524*0a6a1f1dSLionel Sambuc 4525*0a6a1f1dSLionel Sambuc<p> 4526*0a6a1f1dSLionel SambucPushes onto the stack the value <code>t[k]</code>, 4527*0a6a1f1dSLionel Sambucwhere <code>t</code> is the table at the given index and 4528*0a6a1f1dSLionel Sambuc<code>k</code> is the pointer <code>p</code> represented as a light userdata. 4529*0a6a1f1dSLionel SambucThe access is raw; 4530*0a6a1f1dSLionel Sambucthat is, it does not invoke metamethods. 4531*0a6a1f1dSLionel Sambuc 4532*0a6a1f1dSLionel Sambuc 4533*0a6a1f1dSLionel Sambuc<p> 4534*0a6a1f1dSLionel SambucReturns the type of the pushed value. 4535*0a6a1f1dSLionel Sambuc 4536*0a6a1f1dSLionel Sambuc 4537*0a6a1f1dSLionel Sambuc 4538*0a6a1f1dSLionel Sambuc 4539*0a6a1f1dSLionel Sambuc 4540*0a6a1f1dSLionel Sambuc<hr><h3><a name="lua_rawlen"><code>lua_rawlen</code></a></h3><p> 4541*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 4542*0a6a1f1dSLionel Sambuc<pre>size_t lua_rawlen (lua_State *L, int index);</pre> 4543*0a6a1f1dSLionel Sambuc 4544*0a6a1f1dSLionel Sambuc<p> 4545*0a6a1f1dSLionel SambucReturns the raw "length" of the value at the given index: 4546*0a6a1f1dSLionel Sambucfor strings, this is the string length; 4547*0a6a1f1dSLionel Sambucfor tables, this is the result of the length operator ('<code>#</code>') 4548*0a6a1f1dSLionel Sambucwith no metamethods; 4549*0a6a1f1dSLionel Sambucfor userdata, this is the size of the block of memory allocated 4550*0a6a1f1dSLionel Sambucfor the userdata; 4551*0a6a1f1dSLionel Sambucfor other values, it is 0. 4552*0a6a1f1dSLionel Sambuc 4553*0a6a1f1dSLionel Sambuc 455411be35a1SLionel Sambuc 455511be35a1SLionel Sambuc 455611be35a1SLionel Sambuc 455711be35a1SLionel Sambuc<hr><h3><a name="lua_rawset"><code>lua_rawset</code></a></h3><p> 4558*0a6a1f1dSLionel Sambuc<span class="apii">[-2, +0, <em>e</em>]</span> 455911be35a1SLionel Sambuc<pre>void lua_rawset (lua_State *L, int index);</pre> 456011be35a1SLionel Sambuc 456111be35a1SLionel Sambuc<p> 456211be35a1SLionel SambucSimilar to <a href="#lua_settable"><code>lua_settable</code></a>, but does a raw assignment 456311be35a1SLionel Sambuc(i.e., without metamethods). 456411be35a1SLionel Sambuc 456511be35a1SLionel Sambuc 456611be35a1SLionel Sambuc 456711be35a1SLionel Sambuc 456811be35a1SLionel Sambuc 456911be35a1SLionel Sambuc<hr><h3><a name="lua_rawseti"><code>lua_rawseti</code></a></h3><p> 4570*0a6a1f1dSLionel Sambuc<span class="apii">[-1, +0, <em>e</em>]</span> 4571*0a6a1f1dSLionel Sambuc<pre>void lua_rawseti (lua_State *L, int index, lua_Integer i);</pre> 457211be35a1SLionel Sambuc 457311be35a1SLionel Sambuc<p> 4574*0a6a1f1dSLionel SambucDoes the equivalent of <code>t[i] = v</code>, 4575*0a6a1f1dSLionel Sambucwhere <code>t</code> is the table at the given index 4576*0a6a1f1dSLionel Sambucand <code>v</code> is the value at the top of the stack. 4577*0a6a1f1dSLionel Sambuc 4578*0a6a1f1dSLionel Sambuc 4579*0a6a1f1dSLionel Sambuc<p> 4580*0a6a1f1dSLionel SambucThis function pops the value from the stack. 4581*0a6a1f1dSLionel SambucThe assignment is raw; 4582*0a6a1f1dSLionel Sambucthat is, it does not invoke metamethods. 4583*0a6a1f1dSLionel Sambuc 4584*0a6a1f1dSLionel Sambuc 4585*0a6a1f1dSLionel Sambuc 4586*0a6a1f1dSLionel Sambuc 4587*0a6a1f1dSLionel Sambuc 4588*0a6a1f1dSLionel Sambuc<hr><h3><a name="lua_rawsetp"><code>lua_rawsetp</code></a></h3><p> 4589*0a6a1f1dSLionel Sambuc<span class="apii">[-1, +0, <em>e</em>]</span> 4590*0a6a1f1dSLionel Sambuc<pre>void lua_rawsetp (lua_State *L, int index, const void *p);</pre> 4591*0a6a1f1dSLionel Sambuc 4592*0a6a1f1dSLionel Sambuc<p> 4593*0a6a1f1dSLionel SambucDoes the equivalent of <code>t[p] = v</code>, 4594*0a6a1f1dSLionel Sambucwhere <code>t</code> is the table at the given index, 4595*0a6a1f1dSLionel Sambuc<code>p</code> is encoded as a light userdata, 459611be35a1SLionel Sambucand <code>v</code> is the value at the top of the stack. 459711be35a1SLionel Sambuc 459811be35a1SLionel Sambuc 459911be35a1SLionel Sambuc<p> 460011be35a1SLionel SambucThis function pops the value from the stack. 460111be35a1SLionel SambucThe assignment is raw; 460211be35a1SLionel Sambucthat is, it does not invoke metamethods. 460311be35a1SLionel Sambuc 460411be35a1SLionel Sambuc 460511be35a1SLionel Sambuc 460611be35a1SLionel Sambuc 460711be35a1SLionel Sambuc 460811be35a1SLionel Sambuc<hr><h3><a name="lua_Reader"><code>lua_Reader</code></a></h3> 460911be35a1SLionel Sambuc<pre>typedef const char * (*lua_Reader) (lua_State *L, 461011be35a1SLionel Sambuc void *data, 461111be35a1SLionel Sambuc size_t *size);</pre> 461211be35a1SLionel Sambuc 461311be35a1SLionel Sambuc<p> 461411be35a1SLionel SambucThe reader function used by <a href="#lua_load"><code>lua_load</code></a>. 461511be35a1SLionel SambucEvery time it needs another piece of the chunk, 461611be35a1SLionel Sambuc<a href="#lua_load"><code>lua_load</code></a> calls the reader, 461711be35a1SLionel Sambucpassing along its <code>data</code> parameter. 461811be35a1SLionel SambucThe reader must return a pointer to a block of memory 461911be35a1SLionel Sambucwith a new piece of the chunk 462011be35a1SLionel Sambucand set <code>size</code> to the block size. 462111be35a1SLionel SambucThe block must exist until the reader function is called again. 462211be35a1SLionel SambucTo signal the end of the chunk, 462311be35a1SLionel Sambucthe reader must return <code>NULL</code> or set <code>size</code> to zero. 462411be35a1SLionel SambucThe reader function may return pieces of any size greater than zero. 462511be35a1SLionel Sambuc 462611be35a1SLionel Sambuc 462711be35a1SLionel Sambuc 462811be35a1SLionel Sambuc 462911be35a1SLionel Sambuc 463011be35a1SLionel Sambuc<hr><h3><a name="lua_register"><code>lua_register</code></a></h3><p> 463111be35a1SLionel Sambuc<span class="apii">[-0, +0, <em>e</em>]</span> 4632*0a6a1f1dSLionel Sambuc<pre>void lua_register (lua_State *L, const char *name, lua_CFunction f);</pre> 463311be35a1SLionel Sambuc 463411be35a1SLionel Sambuc<p> 463511be35a1SLionel SambucSets the C function <code>f</code> as the new value of global <code>name</code>. 463611be35a1SLionel SambucIt is defined as a macro: 463711be35a1SLionel Sambuc 463811be35a1SLionel Sambuc<pre> 463911be35a1SLionel Sambuc #define lua_register(L,n,f) \ 464011be35a1SLionel Sambuc (lua_pushcfunction(L, f), lua_setglobal(L, n)) 464111be35a1SLionel Sambuc</pre> 464211be35a1SLionel Sambuc 464311be35a1SLionel Sambuc 464411be35a1SLionel Sambuc 464511be35a1SLionel Sambuc 464611be35a1SLionel Sambuc<hr><h3><a name="lua_remove"><code>lua_remove</code></a></h3><p> 4647*0a6a1f1dSLionel Sambuc<span class="apii">[-1, +0, –]</span> 464811be35a1SLionel Sambuc<pre>void lua_remove (lua_State *L, int index);</pre> 464911be35a1SLionel Sambuc 465011be35a1SLionel Sambuc<p> 465111be35a1SLionel SambucRemoves the element at the given valid index, 465211be35a1SLionel Sambucshifting down the elements above this index to fill the gap. 4653*0a6a1f1dSLionel SambucThis function cannot be called with a pseudo-index, 465411be35a1SLionel Sambucbecause a pseudo-index is not an actual stack position. 465511be35a1SLionel Sambuc 465611be35a1SLionel Sambuc 465711be35a1SLionel Sambuc 465811be35a1SLionel Sambuc 465911be35a1SLionel Sambuc 466011be35a1SLionel Sambuc<hr><h3><a name="lua_replace"><code>lua_replace</code></a></h3><p> 4661*0a6a1f1dSLionel Sambuc<span class="apii">[-1, +0, –]</span> 466211be35a1SLionel Sambuc<pre>void lua_replace (lua_State *L, int index);</pre> 466311be35a1SLionel Sambuc 466411be35a1SLionel Sambuc<p> 4665*0a6a1f1dSLionel SambucMoves the top element into the given valid index 466611be35a1SLionel Sambucwithout shifting any element 4667*0a6a1f1dSLionel Sambuc(therefore replacing the value at that given index), 4668*0a6a1f1dSLionel Sambucand then pops the top element. 466911be35a1SLionel Sambuc 467011be35a1SLionel Sambuc 467111be35a1SLionel Sambuc 467211be35a1SLionel Sambuc 467311be35a1SLionel Sambuc 467411be35a1SLionel Sambuc<hr><h3><a name="lua_resume"><code>lua_resume</code></a></h3><p> 4675*0a6a1f1dSLionel Sambuc<span class="apii">[-?, +?, –]</span> 4676*0a6a1f1dSLionel Sambuc<pre>int lua_resume (lua_State *L, lua_State *from, int nargs);</pre> 467711be35a1SLionel Sambuc 467811be35a1SLionel Sambuc<p> 4679*0a6a1f1dSLionel SambucStarts and resumes a coroutine in the given thread <code>L</code>. 468011be35a1SLionel Sambuc 468111be35a1SLionel Sambuc 468211be35a1SLionel Sambuc<p> 4683*0a6a1f1dSLionel SambucTo start a coroutine, 4684*0a6a1f1dSLionel Sambucyou push onto the thread stack the main function plus any arguments; 468511be35a1SLionel Sambucthen you call <a href="#lua_resume"><code>lua_resume</code></a>, 4686*0a6a1f1dSLionel Sambucwith <code>nargs</code> being the number of arguments. 468711be35a1SLionel SambucThis call returns when the coroutine suspends or finishes its execution. 468811be35a1SLionel SambucWhen it returns, the stack contains all values passed to <a href="#lua_yield"><code>lua_yield</code></a>, 468911be35a1SLionel Sambucor all values returned by the body function. 469011be35a1SLionel Sambuc<a href="#lua_resume"><code>lua_resume</code></a> returns 469111be35a1SLionel Sambuc<a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> if the coroutine yields, 4692*0a6a1f1dSLionel Sambuc<a href="#pdf-LUA_OK"><code>LUA_OK</code></a> if the coroutine finishes its execution 469311be35a1SLionel Sambucwithout errors, 469411be35a1SLionel Sambucor an error code in case of errors (see <a href="#lua_pcall"><code>lua_pcall</code></a>). 4695*0a6a1f1dSLionel Sambuc 4696*0a6a1f1dSLionel Sambuc 4697*0a6a1f1dSLionel Sambuc<p> 469811be35a1SLionel SambucIn case of errors, 469911be35a1SLionel Sambucthe stack is not unwound, 470011be35a1SLionel Sambucso you can use the debug API over it. 470111be35a1SLionel SambucThe error message is on the top of the stack. 4702*0a6a1f1dSLionel Sambuc 4703*0a6a1f1dSLionel Sambuc 4704*0a6a1f1dSLionel Sambuc<p> 4705*0a6a1f1dSLionel SambucTo resume a coroutine, 4706*0a6a1f1dSLionel Sambucyou remove any results from the last <a href="#lua_yield"><code>lua_yield</code></a>, 4707*0a6a1f1dSLionel Sambucput on its stack only the values to 470811be35a1SLionel Sambucbe passed as results from <code>yield</code>, 470911be35a1SLionel Sambucand then call <a href="#lua_resume"><code>lua_resume</code></a>. 471011be35a1SLionel Sambuc 471111be35a1SLionel Sambuc 4712*0a6a1f1dSLionel Sambuc<p> 4713*0a6a1f1dSLionel SambucThe parameter <code>from</code> represents the coroutine that is resuming <code>L</code>. 4714*0a6a1f1dSLionel SambucIf there is no such coroutine, 4715*0a6a1f1dSLionel Sambucthis parameter can be <code>NULL</code>. 4716*0a6a1f1dSLionel Sambuc 4717*0a6a1f1dSLionel Sambuc 4718*0a6a1f1dSLionel Sambuc 4719*0a6a1f1dSLionel Sambuc 4720*0a6a1f1dSLionel Sambuc 4721*0a6a1f1dSLionel Sambuc<hr><h3><a name="lua_rotate"><code>lua_rotate</code></a></h3><p> 4722*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 4723*0a6a1f1dSLionel Sambuc<pre>void lua_rotate (lua_State *L, int idx, int n);</pre> 4724*0a6a1f1dSLionel Sambuc 4725*0a6a1f1dSLionel Sambuc<p> 4726*0a6a1f1dSLionel SambucRotates the stack elements between the valid index <code>idx</code> 4727*0a6a1f1dSLionel Sambucand the top of the stack. 4728*0a6a1f1dSLionel SambucThe elements are rotated <code>n</code> positions in the direction of the top, 4729*0a6a1f1dSLionel Sambucfor a positive <code>n</code>, 4730*0a6a1f1dSLionel Sambucor <code>-n</code> positions in the direction of the bottom, 4731*0a6a1f1dSLionel Sambucfor a negative <code>n</code>. 4732*0a6a1f1dSLionel SambucThe absolute value of <code>n</code> must not be greater than the size 4733*0a6a1f1dSLionel Sambucof the slice being rotated. 4734*0a6a1f1dSLionel SambucThis function cannot be called with a pseudo-index, 4735*0a6a1f1dSLionel Sambucbecause a pseudo-index is not an actual stack position. 4736*0a6a1f1dSLionel Sambuc 4737*0a6a1f1dSLionel Sambuc 473811be35a1SLionel Sambuc 473911be35a1SLionel Sambuc 474011be35a1SLionel Sambuc 474111be35a1SLionel Sambuc<hr><h3><a name="lua_setallocf"><code>lua_setallocf</code></a></h3><p> 4742*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 474311be35a1SLionel Sambuc<pre>void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);</pre> 474411be35a1SLionel Sambuc 474511be35a1SLionel Sambuc<p> 474611be35a1SLionel SambucChanges the allocator function of a given state to <code>f</code> 474711be35a1SLionel Sambucwith user data <code>ud</code>. 474811be35a1SLionel Sambuc 474911be35a1SLionel Sambuc 475011be35a1SLionel Sambuc 475111be35a1SLionel Sambuc 475211be35a1SLionel Sambuc 475311be35a1SLionel Sambuc<hr><h3><a name="lua_setfield"><code>lua_setfield</code></a></h3><p> 475411be35a1SLionel Sambuc<span class="apii">[-1, +0, <em>e</em>]</span> 475511be35a1SLionel Sambuc<pre>void lua_setfield (lua_State *L, int index, const char *k);</pre> 475611be35a1SLionel Sambuc 475711be35a1SLionel Sambuc<p> 475811be35a1SLionel SambucDoes the equivalent to <code>t[k] = v</code>, 4759*0a6a1f1dSLionel Sambucwhere <code>t</code> is the value at the given index 476011be35a1SLionel Sambucand <code>v</code> is the value at the top of the stack. 476111be35a1SLionel Sambuc 476211be35a1SLionel Sambuc 476311be35a1SLionel Sambuc<p> 476411be35a1SLionel SambucThis function pops the value from the stack. 476511be35a1SLionel SambucAs in Lua, this function may trigger a metamethod 4766*0a6a1f1dSLionel Sambucfor the "newindex" event (see <a href="#2.4">§2.4</a>). 476711be35a1SLionel Sambuc 476811be35a1SLionel Sambuc 476911be35a1SLionel Sambuc 477011be35a1SLionel Sambuc 477111be35a1SLionel Sambuc 477211be35a1SLionel Sambuc<hr><h3><a name="lua_setglobal"><code>lua_setglobal</code></a></h3><p> 477311be35a1SLionel Sambuc<span class="apii">[-1, +0, <em>e</em>]</span> 477411be35a1SLionel Sambuc<pre>void lua_setglobal (lua_State *L, const char *name);</pre> 477511be35a1SLionel Sambuc 477611be35a1SLionel Sambuc<p> 477711be35a1SLionel SambucPops a value from the stack and 477811be35a1SLionel Sambucsets it as the new value of global <code>name</code>. 477911be35a1SLionel Sambuc 4780*0a6a1f1dSLionel Sambuc 4781*0a6a1f1dSLionel Sambuc 4782*0a6a1f1dSLionel Sambuc 4783*0a6a1f1dSLionel Sambuc 4784*0a6a1f1dSLionel Sambuc<hr><h3><a name="lua_seti"><code>lua_seti</code></a></h3><p> 4785*0a6a1f1dSLionel Sambuc<span class="apii">[-1, +0, <em>e</em>]</span> 4786*0a6a1f1dSLionel Sambuc<pre>void lua_seti (lua_State *L, int index, lua_Integer n);</pre> 4787*0a6a1f1dSLionel Sambuc 4788*0a6a1f1dSLionel Sambuc<p> 4789*0a6a1f1dSLionel SambucDoes the equivalent to <code>t[n] = v</code>, 4790*0a6a1f1dSLionel Sambucwhere <code>t</code> is the value at the given index 4791*0a6a1f1dSLionel Sambucand <code>v</code> is the value at the top of the stack. 4792*0a6a1f1dSLionel Sambuc 4793*0a6a1f1dSLionel Sambuc 4794*0a6a1f1dSLionel Sambuc<p> 4795*0a6a1f1dSLionel SambucThis function pops the value from the stack. 4796*0a6a1f1dSLionel SambucAs in Lua, this function may trigger a metamethod 4797*0a6a1f1dSLionel Sambucfor the "newindex" event (see <a href="#2.4">§2.4</a>). 4798*0a6a1f1dSLionel Sambuc 479911be35a1SLionel Sambuc 480011be35a1SLionel Sambuc 480111be35a1SLionel Sambuc 480211be35a1SLionel Sambuc 480311be35a1SLionel Sambuc<hr><h3><a name="lua_setmetatable"><code>lua_setmetatable</code></a></h3><p> 4804*0a6a1f1dSLionel Sambuc<span class="apii">[-1, +0, –]</span> 4805*0a6a1f1dSLionel Sambuc<pre>void lua_setmetatable (lua_State *L, int index);</pre> 480611be35a1SLionel Sambuc 480711be35a1SLionel Sambuc<p> 480811be35a1SLionel SambucPops a table from the stack and 4809*0a6a1f1dSLionel Sambucsets it as the new metatable for the value at the given index. 481011be35a1SLionel Sambuc 481111be35a1SLionel Sambuc 481211be35a1SLionel Sambuc 481311be35a1SLionel Sambuc 481411be35a1SLionel Sambuc 481511be35a1SLionel Sambuc<hr><h3><a name="lua_settable"><code>lua_settable</code></a></h3><p> 481611be35a1SLionel Sambuc<span class="apii">[-2, +0, <em>e</em>]</span> 481711be35a1SLionel Sambuc<pre>void lua_settable (lua_State *L, int index);</pre> 481811be35a1SLionel Sambuc 481911be35a1SLionel Sambuc<p> 482011be35a1SLionel SambucDoes the equivalent to <code>t[k] = v</code>, 4821*0a6a1f1dSLionel Sambucwhere <code>t</code> is the value at the given index, 482211be35a1SLionel Sambuc<code>v</code> is the value at the top of the stack, 482311be35a1SLionel Sambucand <code>k</code> is the value just below the top. 482411be35a1SLionel Sambuc 482511be35a1SLionel Sambuc 482611be35a1SLionel Sambuc<p> 482711be35a1SLionel SambucThis function pops both the key and the value from the stack. 482811be35a1SLionel SambucAs in Lua, this function may trigger a metamethod 4829*0a6a1f1dSLionel Sambucfor the "newindex" event (see <a href="#2.4">§2.4</a>). 483011be35a1SLionel Sambuc 483111be35a1SLionel Sambuc 483211be35a1SLionel Sambuc 483311be35a1SLionel Sambuc 483411be35a1SLionel Sambuc 483511be35a1SLionel Sambuc<hr><h3><a name="lua_settop"><code>lua_settop</code></a></h3><p> 4836*0a6a1f1dSLionel Sambuc<span class="apii">[-?, +?, –]</span> 483711be35a1SLionel Sambuc<pre>void lua_settop (lua_State *L, int index);</pre> 483811be35a1SLionel Sambuc 483911be35a1SLionel Sambuc<p> 4840*0a6a1f1dSLionel SambucAccepts any index, or 0, 484111be35a1SLionel Sambucand sets the stack top to this index. 484211be35a1SLionel SambucIf the new top is larger than the old one, 484311be35a1SLionel Sambucthen the new elements are filled with <b>nil</b>. 484411be35a1SLionel SambucIf <code>index</code> is 0, then all stack elements are removed. 484511be35a1SLionel Sambuc 484611be35a1SLionel Sambuc 484711be35a1SLionel Sambuc 484811be35a1SLionel Sambuc 484911be35a1SLionel Sambuc 4850*0a6a1f1dSLionel Sambuc<hr><h3><a name="lua_setuservalue"><code>lua_setuservalue</code></a></h3><p> 4851*0a6a1f1dSLionel Sambuc<span class="apii">[-1, +0, –]</span> 4852*0a6a1f1dSLionel Sambuc<pre>void lua_setuservalue (lua_State *L, int index);</pre> 4853*0a6a1f1dSLionel Sambuc 4854*0a6a1f1dSLionel Sambuc<p> 4855*0a6a1f1dSLionel SambucPops a value from the stack and sets it as 4856*0a6a1f1dSLionel Sambucthe new value associated to the userdata at the given index. 4857*0a6a1f1dSLionel Sambuc 4858*0a6a1f1dSLionel Sambuc 4859*0a6a1f1dSLionel Sambuc 4860*0a6a1f1dSLionel Sambuc 4861*0a6a1f1dSLionel Sambuc 486211be35a1SLionel Sambuc<hr><h3><a name="lua_State"><code>lua_State</code></a></h3> 486311be35a1SLionel Sambuc<pre>typedef struct lua_State lua_State;</pre> 486411be35a1SLionel Sambuc 486511be35a1SLionel Sambuc<p> 4866*0a6a1f1dSLionel SambucAn opaque structure that points to a thread and indirectly 4867*0a6a1f1dSLionel Sambuc(through the thread) to the whole state of a Lua interpreter. 486811be35a1SLionel SambucThe Lua library is fully reentrant: 486911be35a1SLionel Sambucit has no global variables. 4870*0a6a1f1dSLionel SambucAll information about a state is accessible through this structure. 487111be35a1SLionel Sambuc 487211be35a1SLionel Sambuc 487311be35a1SLionel Sambuc<p> 4874*0a6a1f1dSLionel SambucA pointer to this structure must be passed as the first argument to 487511be35a1SLionel Sambucevery function in the library, except to <a href="#lua_newstate"><code>lua_newstate</code></a>, 487611be35a1SLionel Sambucwhich creates a Lua state from scratch. 487711be35a1SLionel Sambuc 487811be35a1SLionel Sambuc 487911be35a1SLionel Sambuc 488011be35a1SLionel Sambuc 488111be35a1SLionel Sambuc 488211be35a1SLionel Sambuc<hr><h3><a name="lua_status"><code>lua_status</code></a></h3><p> 4883*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 488411be35a1SLionel Sambuc<pre>int lua_status (lua_State *L);</pre> 488511be35a1SLionel Sambuc 488611be35a1SLionel Sambuc<p> 488711be35a1SLionel SambucReturns the status of the thread <code>L</code>. 488811be35a1SLionel Sambuc 488911be35a1SLionel Sambuc 489011be35a1SLionel Sambuc<p> 4891*0a6a1f1dSLionel SambucThe status can be 0 (<a href="#pdf-LUA_OK"><code>LUA_OK</code></a>) for a normal thread, 4892*0a6a1f1dSLionel Sambucan error code if the thread finished the execution 4893*0a6a1f1dSLionel Sambucof a <a href="#lua_resume"><code>lua_resume</code></a> with an error, 489411be35a1SLionel Sambucor <a name="pdf-LUA_YIELD"><code>LUA_YIELD</code></a> if the thread is suspended. 489511be35a1SLionel Sambuc 489611be35a1SLionel Sambuc 4897*0a6a1f1dSLionel Sambuc<p> 4898*0a6a1f1dSLionel SambucYou can only call functions in threads with status <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>. 4899*0a6a1f1dSLionel SambucYou can resume threads with status <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> 4900*0a6a1f1dSLionel Sambuc(to start a new coroutine) or <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> 4901*0a6a1f1dSLionel Sambuc(to resume a coroutine). 4902*0a6a1f1dSLionel Sambuc 4903*0a6a1f1dSLionel Sambuc 4904*0a6a1f1dSLionel Sambuc 4905*0a6a1f1dSLionel Sambuc 4906*0a6a1f1dSLionel Sambuc 4907*0a6a1f1dSLionel Sambuc<hr><h3><a name="lua_stringtonumber"><code>lua_stringtonumber</code></a></h3><p> 4908*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, –]</span> 4909*0a6a1f1dSLionel Sambuc<pre>size_t lua_stringtonumber (lua_State *L, const char *s);</pre> 4910*0a6a1f1dSLionel Sambuc 4911*0a6a1f1dSLionel Sambuc<p> 4912*0a6a1f1dSLionel SambucConverts the zero-terminated string <code>s</code> to a number, 4913*0a6a1f1dSLionel Sambucpushes that number into the stack, 4914*0a6a1f1dSLionel Sambucand returns the total size of the string, 4915*0a6a1f1dSLionel Sambucthat is, its length plus one. 4916*0a6a1f1dSLionel SambucThe conversion can result in an integer or a float, 4917*0a6a1f1dSLionel Sambucaccording to the lexical conventions of Lua (see <a href="#3.1">§3.1</a>). 4918*0a6a1f1dSLionel SambucThe string may have leading and trailing spaces and a sign. 4919*0a6a1f1dSLionel SambucIf the string is not a valid numeral, 4920*0a6a1f1dSLionel Sambucreturns 0 and pushes nothing. 4921*0a6a1f1dSLionel Sambuc(Note that the result can be used as a boolean, 4922*0a6a1f1dSLionel Sambuctrue if the conversion succeeds.) 4923*0a6a1f1dSLionel Sambuc 4924*0a6a1f1dSLionel Sambuc 492511be35a1SLionel Sambuc 492611be35a1SLionel Sambuc 492711be35a1SLionel Sambuc 492811be35a1SLionel Sambuc<hr><h3><a name="lua_toboolean"><code>lua_toboolean</code></a></h3><p> 4929*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 493011be35a1SLionel Sambuc<pre>int lua_toboolean (lua_State *L, int index);</pre> 493111be35a1SLionel Sambuc 493211be35a1SLionel Sambuc<p> 4933*0a6a1f1dSLionel SambucConverts the Lua value at the given index to a C boolean 493411be35a1SLionel Sambucvalue (0 or 1). 493511be35a1SLionel SambucLike all tests in Lua, 4936*0a6a1f1dSLionel Sambuc<a href="#lua_toboolean"><code>lua_toboolean</code></a> returns true for any Lua value 493711be35a1SLionel Sambucdifferent from <b>false</b> and <b>nil</b>; 4938*0a6a1f1dSLionel Sambucotherwise it returns false. 493911be35a1SLionel Sambuc(If you want to accept only actual boolean values, 494011be35a1SLionel Sambucuse <a href="#lua_isboolean"><code>lua_isboolean</code></a> to test the value's type.) 494111be35a1SLionel Sambuc 494211be35a1SLionel Sambuc 494311be35a1SLionel Sambuc 494411be35a1SLionel Sambuc 494511be35a1SLionel Sambuc 494611be35a1SLionel Sambuc<hr><h3><a name="lua_tocfunction"><code>lua_tocfunction</code></a></h3><p> 4947*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 494811be35a1SLionel Sambuc<pre>lua_CFunction lua_tocfunction (lua_State *L, int index);</pre> 494911be35a1SLionel Sambuc 495011be35a1SLionel Sambuc<p> 4951*0a6a1f1dSLionel SambucConverts a value at the given index to a C function. 495211be35a1SLionel SambucThat value must be a C function; 495311be35a1SLionel Sambucotherwise, returns <code>NULL</code>. 495411be35a1SLionel Sambuc 495511be35a1SLionel Sambuc 495611be35a1SLionel Sambuc 495711be35a1SLionel Sambuc 495811be35a1SLionel Sambuc 495911be35a1SLionel Sambuc<hr><h3><a name="lua_tointeger"><code>lua_tointeger</code></a></h3><p> 4960*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 496111be35a1SLionel Sambuc<pre>lua_Integer lua_tointeger (lua_State *L, int index);</pre> 496211be35a1SLionel Sambuc 496311be35a1SLionel Sambuc<p> 4964*0a6a1f1dSLionel SambucEquivalent to <a href="#lua_tointegerx"><code>lua_tointegerx</code></a> with <code>isnum</code> equal to <code>NULL</code>. 4965*0a6a1f1dSLionel Sambuc 4966*0a6a1f1dSLionel Sambuc 4967*0a6a1f1dSLionel Sambuc 4968*0a6a1f1dSLionel Sambuc 4969*0a6a1f1dSLionel Sambuc 4970*0a6a1f1dSLionel Sambuc<hr><h3><a name="lua_tointegerx"><code>lua_tointegerx</code></a></h3><p> 4971*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 4972*0a6a1f1dSLionel Sambuc<pre>lua_Integer lua_tointegerx (lua_State *L, int index, int *isnum);</pre> 4973*0a6a1f1dSLionel Sambuc 4974*0a6a1f1dSLionel Sambuc<p> 4975*0a6a1f1dSLionel SambucConverts the Lua value at the given index 497611be35a1SLionel Sambucto the signed integral type <a href="#lua_Integer"><code>lua_Integer</code></a>. 4977*0a6a1f1dSLionel SambucThe Lua value must be an integer, 4978*0a6a1f1dSLionel Sambucor a number or string convertible to an integer (see <a href="#3.4.3">§3.4.3</a>); 4979*0a6a1f1dSLionel Sambucotherwise, <code>lua_tointegerx</code> returns 0. 498011be35a1SLionel Sambuc 498111be35a1SLionel Sambuc 498211be35a1SLionel Sambuc<p> 4983*0a6a1f1dSLionel SambucIf <code>isnum</code> is not <code>NULL</code>, 4984*0a6a1f1dSLionel Sambucits referent is assigned a boolean value that 4985*0a6a1f1dSLionel Sambucindicates whether the operation succeeded. 498611be35a1SLionel Sambuc 498711be35a1SLionel Sambuc 498811be35a1SLionel Sambuc 498911be35a1SLionel Sambuc 499011be35a1SLionel Sambuc 499111be35a1SLionel Sambuc<hr><h3><a name="lua_tolstring"><code>lua_tolstring</code></a></h3><p> 4992*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, <em>e</em>]</span> 499311be35a1SLionel Sambuc<pre>const char *lua_tolstring (lua_State *L, int index, size_t *len);</pre> 499411be35a1SLionel Sambuc 499511be35a1SLionel Sambuc<p> 4996*0a6a1f1dSLionel SambucConverts the Lua value at the given index to a C string. 499711be35a1SLionel SambucIf <code>len</code> is not <code>NULL</code>, 499811be35a1SLionel Sambucit also sets <code>*len</code> with the string length. 499911be35a1SLionel SambucThe Lua value must be a string or a number; 500011be35a1SLionel Sambucotherwise, the function returns <code>NULL</code>. 500111be35a1SLionel SambucIf the value is a number, 5002*0a6a1f1dSLionel Sambucthen <code>lua_tolstring</code> also 500311be35a1SLionel Sambuc<em>changes the actual value in the stack to a string</em>. 500411be35a1SLionel Sambuc(This change confuses <a href="#lua_next"><code>lua_next</code></a> 5005*0a6a1f1dSLionel Sambucwhen <code>lua_tolstring</code> is applied to keys during a table traversal.) 500611be35a1SLionel Sambuc 500711be35a1SLionel Sambuc 500811be35a1SLionel Sambuc<p> 5009*0a6a1f1dSLionel Sambuc<code>lua_tolstring</code> returns a fully aligned pointer 501011be35a1SLionel Sambucto a string inside the Lua state. 501111be35a1SLionel SambucThis string always has a zero ('<code>\0</code>') 501211be35a1SLionel Sambucafter its last character (as in C), 501311be35a1SLionel Sambucbut can contain other zeros in its body. 5014*0a6a1f1dSLionel Sambuc 5015*0a6a1f1dSLionel Sambuc 5016*0a6a1f1dSLionel Sambuc<p> 501711be35a1SLionel SambucBecause Lua has garbage collection, 5018*0a6a1f1dSLionel Sambucthere is no guarantee that the pointer returned by <code>lua_tolstring</code> 5019*0a6a1f1dSLionel Sambucwill be valid after the corresponding Lua value is removed from the stack. 502011be35a1SLionel Sambuc 502111be35a1SLionel Sambuc 502211be35a1SLionel Sambuc 502311be35a1SLionel Sambuc 502411be35a1SLionel Sambuc 502511be35a1SLionel Sambuc<hr><h3><a name="lua_tonumber"><code>lua_tonumber</code></a></h3><p> 5026*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 502711be35a1SLionel Sambuc<pre>lua_Number lua_tonumber (lua_State *L, int index);</pre> 502811be35a1SLionel Sambuc 502911be35a1SLionel Sambuc<p> 5030*0a6a1f1dSLionel SambucEquivalent to <a href="#lua_tonumberx"><code>lua_tonumberx</code></a> with <code>isnum</code> equal to <code>NULL</code>. 5031*0a6a1f1dSLionel Sambuc 5032*0a6a1f1dSLionel Sambuc 5033*0a6a1f1dSLionel Sambuc 5034*0a6a1f1dSLionel Sambuc 5035*0a6a1f1dSLionel Sambuc 5036*0a6a1f1dSLionel Sambuc<hr><h3><a name="lua_tonumberx"><code>lua_tonumberx</code></a></h3><p> 5037*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 5038*0a6a1f1dSLionel Sambuc<pre>lua_Number lua_tonumberx (lua_State *L, int index, int *isnum);</pre> 5039*0a6a1f1dSLionel Sambuc 5040*0a6a1f1dSLionel Sambuc<p> 5041*0a6a1f1dSLionel SambucConverts the Lua value at the given index 504211be35a1SLionel Sambucto the C type <a href="#lua_Number"><code>lua_Number</code></a> (see <a href="#lua_Number"><code>lua_Number</code></a>). 504311be35a1SLionel SambucThe Lua value must be a number or a string convertible to a number 5044*0a6a1f1dSLionel Sambuc(see <a href="#3.4.3">§3.4.3</a>); 5045*0a6a1f1dSLionel Sambucotherwise, <a href="#lua_tonumberx"><code>lua_tonumberx</code></a> returns 0. 5046*0a6a1f1dSLionel Sambuc 5047*0a6a1f1dSLionel Sambuc 5048*0a6a1f1dSLionel Sambuc<p> 5049*0a6a1f1dSLionel SambucIf <code>isnum</code> is not <code>NULL</code>, 5050*0a6a1f1dSLionel Sambucits referent is assigned a boolean value that 5051*0a6a1f1dSLionel Sambucindicates whether the operation succeeded. 505211be35a1SLionel Sambuc 505311be35a1SLionel Sambuc 505411be35a1SLionel Sambuc 505511be35a1SLionel Sambuc 505611be35a1SLionel Sambuc 505711be35a1SLionel Sambuc<hr><h3><a name="lua_topointer"><code>lua_topointer</code></a></h3><p> 5058*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 505911be35a1SLionel Sambuc<pre>const void *lua_topointer (lua_State *L, int index);</pre> 506011be35a1SLionel Sambuc 506111be35a1SLionel Sambuc<p> 5062*0a6a1f1dSLionel SambucConverts the value at the given index to a generic 506311be35a1SLionel SambucC pointer (<code>void*</code>). 506411be35a1SLionel SambucThe value can be a userdata, a table, a thread, or a function; 5065*0a6a1f1dSLionel Sambucotherwise, <code>lua_topointer</code> returns <code>NULL</code>. 506611be35a1SLionel SambucDifferent objects will give different pointers. 506711be35a1SLionel SambucThere is no way to convert the pointer back to its original value. 506811be35a1SLionel Sambuc 506911be35a1SLionel Sambuc 507011be35a1SLionel Sambuc<p> 5071*0a6a1f1dSLionel SambucTypically this function is used only for hashing and debug information. 507211be35a1SLionel Sambuc 507311be35a1SLionel Sambuc 507411be35a1SLionel Sambuc 507511be35a1SLionel Sambuc 507611be35a1SLionel Sambuc 507711be35a1SLionel Sambuc<hr><h3><a name="lua_tostring"><code>lua_tostring</code></a></h3><p> 5078*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, <em>e</em>]</span> 507911be35a1SLionel Sambuc<pre>const char *lua_tostring (lua_State *L, int index);</pre> 508011be35a1SLionel Sambuc 508111be35a1SLionel Sambuc<p> 508211be35a1SLionel SambucEquivalent to <a href="#lua_tolstring"><code>lua_tolstring</code></a> with <code>len</code> equal to <code>NULL</code>. 508311be35a1SLionel Sambuc 508411be35a1SLionel Sambuc 508511be35a1SLionel Sambuc 508611be35a1SLionel Sambuc 508711be35a1SLionel Sambuc 508811be35a1SLionel Sambuc<hr><h3><a name="lua_tothread"><code>lua_tothread</code></a></h3><p> 5089*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 509011be35a1SLionel Sambuc<pre>lua_State *lua_tothread (lua_State *L, int index);</pre> 509111be35a1SLionel Sambuc 509211be35a1SLionel Sambuc<p> 5093*0a6a1f1dSLionel SambucConverts the value at the given index to a Lua thread 509411be35a1SLionel Sambuc(represented as <code>lua_State*</code>). 509511be35a1SLionel SambucThis value must be a thread; 509611be35a1SLionel Sambucotherwise, the function returns <code>NULL</code>. 509711be35a1SLionel Sambuc 509811be35a1SLionel Sambuc 509911be35a1SLionel Sambuc 510011be35a1SLionel Sambuc 510111be35a1SLionel Sambuc 510211be35a1SLionel Sambuc<hr><h3><a name="lua_touserdata"><code>lua_touserdata</code></a></h3><p> 5103*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 510411be35a1SLionel Sambuc<pre>void *lua_touserdata (lua_State *L, int index);</pre> 510511be35a1SLionel Sambuc 510611be35a1SLionel Sambuc<p> 5107*0a6a1f1dSLionel SambucIf the value at the given index is a full userdata, 510811be35a1SLionel Sambucreturns its block address. 510911be35a1SLionel SambucIf the value is a light userdata, 511011be35a1SLionel Sambucreturns its pointer. 511111be35a1SLionel SambucOtherwise, returns <code>NULL</code>. 511211be35a1SLionel Sambuc 511311be35a1SLionel Sambuc 511411be35a1SLionel Sambuc 511511be35a1SLionel Sambuc 511611be35a1SLionel Sambuc 511711be35a1SLionel Sambuc<hr><h3><a name="lua_type"><code>lua_type</code></a></h3><p> 5118*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 511911be35a1SLionel Sambuc<pre>int lua_type (lua_State *L, int index);</pre> 512011be35a1SLionel Sambuc 512111be35a1SLionel Sambuc<p> 5122*0a6a1f1dSLionel SambucReturns the type of the value in the given valid index, 5123*0a6a1f1dSLionel Sambucor <code>LUA_TNONE</code> for a non-valid (but acceptable) index. 512411be35a1SLionel SambucThe types returned by <a href="#lua_type"><code>lua_type</code></a> are coded by the following constants 512511be35a1SLionel Sambucdefined in <code>lua.h</code>: 5126*0a6a1f1dSLionel Sambuc<a name="pdf-LUA_TNIL"><code>LUA_TNIL</code></a> (0), 5127*0a6a1f1dSLionel Sambuc<a name="pdf-LUA_TNUMBER"><code>LUA_TNUMBER</code></a>, 5128*0a6a1f1dSLionel Sambuc<a name="pdf-LUA_TBOOLEAN"><code>LUA_TBOOLEAN</code></a>, 5129*0a6a1f1dSLionel Sambuc<a name="pdf-LUA_TSTRING"><code>LUA_TSTRING</code></a>, 5130*0a6a1f1dSLionel Sambuc<a name="pdf-LUA_TTABLE"><code>LUA_TTABLE</code></a>, 5131*0a6a1f1dSLionel Sambuc<a name="pdf-LUA_TFUNCTION"><code>LUA_TFUNCTION</code></a>, 5132*0a6a1f1dSLionel Sambuc<a name="pdf-LUA_TUSERDATA"><code>LUA_TUSERDATA</code></a>, 5133*0a6a1f1dSLionel Sambuc<a name="pdf-LUA_TTHREAD"><code>LUA_TTHREAD</code></a>, 513411be35a1SLionel Sambucand 5135*0a6a1f1dSLionel Sambuc<a name="pdf-LUA_TLIGHTUSERDATA"><code>LUA_TLIGHTUSERDATA</code></a>. 513611be35a1SLionel Sambuc 513711be35a1SLionel Sambuc 513811be35a1SLionel Sambuc 513911be35a1SLionel Sambuc 514011be35a1SLionel Sambuc 514111be35a1SLionel Sambuc<hr><h3><a name="lua_typename"><code>lua_typename</code></a></h3><p> 5142*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 514311be35a1SLionel Sambuc<pre>const char *lua_typename (lua_State *L, int tp);</pre> 514411be35a1SLionel Sambuc 514511be35a1SLionel Sambuc<p> 514611be35a1SLionel SambucReturns the name of the type encoded by the value <code>tp</code>, 514711be35a1SLionel Sambucwhich must be one the values returned by <a href="#lua_type"><code>lua_type</code></a>. 514811be35a1SLionel Sambuc 514911be35a1SLionel Sambuc 515011be35a1SLionel Sambuc 515111be35a1SLionel Sambuc 515211be35a1SLionel Sambuc 5153*0a6a1f1dSLionel Sambuc<hr><h3><a name="lua_Unsigned"><code>lua_Unsigned</code></a></h3> 5154*0a6a1f1dSLionel Sambuc<pre>typedef ... lua_Unsigned;</pre> 5155*0a6a1f1dSLionel Sambuc 5156*0a6a1f1dSLionel Sambuc<p> 5157*0a6a1f1dSLionel SambucThe unsigned version of <a href="#lua_Integer"><code>lua_Integer</code></a>. 5158*0a6a1f1dSLionel Sambuc 5159*0a6a1f1dSLionel Sambuc 5160*0a6a1f1dSLionel Sambuc 5161*0a6a1f1dSLionel Sambuc 5162*0a6a1f1dSLionel Sambuc 5163*0a6a1f1dSLionel Sambuc<hr><h3><a name="lua_upvalueindex"><code>lua_upvalueindex</code></a></h3><p> 5164*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 5165*0a6a1f1dSLionel Sambuc<pre>int lua_upvalueindex (int i);</pre> 5166*0a6a1f1dSLionel Sambuc 5167*0a6a1f1dSLionel Sambuc<p> 5168*0a6a1f1dSLionel SambucReturns the pseudo-index that represents the <code>i</code>-th upvalue of 5169*0a6a1f1dSLionel Sambucthe running function (see <a href="#4.4">§4.4</a>). 5170*0a6a1f1dSLionel Sambuc 5171*0a6a1f1dSLionel Sambuc 5172*0a6a1f1dSLionel Sambuc 5173*0a6a1f1dSLionel Sambuc 5174*0a6a1f1dSLionel Sambuc 5175*0a6a1f1dSLionel Sambuc<hr><h3><a name="lua_version"><code>lua_version</code></a></h3><p> 5176*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, <em>v</em>]</span> 5177*0a6a1f1dSLionel Sambuc<pre>const lua_Number *lua_version (lua_State *L);</pre> 5178*0a6a1f1dSLionel Sambuc 5179*0a6a1f1dSLionel Sambuc<p> 5180*0a6a1f1dSLionel SambucReturns the address of the version number stored in the Lua core. 5181*0a6a1f1dSLionel SambucWhen called with a valid <a href="#lua_State"><code>lua_State</code></a>, 5182*0a6a1f1dSLionel Sambucreturns the address of the version used to create that state. 5183*0a6a1f1dSLionel SambucWhen called with <code>NULL</code>, 5184*0a6a1f1dSLionel Sambucreturns the address of the version running the call. 5185*0a6a1f1dSLionel Sambuc 5186*0a6a1f1dSLionel Sambuc 5187*0a6a1f1dSLionel Sambuc 5188*0a6a1f1dSLionel Sambuc 5189*0a6a1f1dSLionel Sambuc 519011be35a1SLionel Sambuc<hr><h3><a name="lua_Writer"><code>lua_Writer</code></a></h3> 519111be35a1SLionel Sambuc<pre>typedef int (*lua_Writer) (lua_State *L, 519211be35a1SLionel Sambuc const void* p, 519311be35a1SLionel Sambuc size_t sz, 519411be35a1SLionel Sambuc void* ud);</pre> 519511be35a1SLionel Sambuc 519611be35a1SLionel Sambuc<p> 519711be35a1SLionel SambucThe type of the writer function used by <a href="#lua_dump"><code>lua_dump</code></a>. 519811be35a1SLionel SambucEvery time it produces another piece of chunk, 519911be35a1SLionel Sambuc<a href="#lua_dump"><code>lua_dump</code></a> calls the writer, 520011be35a1SLionel Sambucpassing along the buffer to be written (<code>p</code>), 520111be35a1SLionel Sambucits size (<code>sz</code>), 520211be35a1SLionel Sambucand the <code>data</code> parameter supplied to <a href="#lua_dump"><code>lua_dump</code></a>. 520311be35a1SLionel Sambuc 520411be35a1SLionel Sambuc 520511be35a1SLionel Sambuc<p> 520611be35a1SLionel SambucThe writer returns an error code: 520711be35a1SLionel Sambuc0 means no errors; 520811be35a1SLionel Sambucany other value means an error and stops <a href="#lua_dump"><code>lua_dump</code></a> from 520911be35a1SLionel Sambuccalling the writer again. 521011be35a1SLionel Sambuc 521111be35a1SLionel Sambuc 521211be35a1SLionel Sambuc 521311be35a1SLionel Sambuc 521411be35a1SLionel Sambuc 521511be35a1SLionel Sambuc<hr><h3><a name="lua_xmove"><code>lua_xmove</code></a></h3><p> 5216*0a6a1f1dSLionel Sambuc<span class="apii">[-?, +?, –]</span> 521711be35a1SLionel Sambuc<pre>void lua_xmove (lua_State *from, lua_State *to, int n);</pre> 521811be35a1SLionel Sambuc 521911be35a1SLionel Sambuc<p> 5220*0a6a1f1dSLionel SambucExchange values between different threads of the same state. 522111be35a1SLionel Sambuc 522211be35a1SLionel Sambuc 522311be35a1SLionel Sambuc<p> 522411be35a1SLionel SambucThis function pops <code>n</code> values from the stack <code>from</code>, 522511be35a1SLionel Sambucand pushes them onto the stack <code>to</code>. 522611be35a1SLionel Sambuc 522711be35a1SLionel Sambuc 522811be35a1SLionel Sambuc 522911be35a1SLionel Sambuc 523011be35a1SLionel Sambuc 523111be35a1SLionel Sambuc<hr><h3><a name="lua_yield"><code>lua_yield</code></a></h3><p> 5232*0a6a1f1dSLionel Sambuc<span class="apii">[-?, +?, <em>e</em>]</span> 523311be35a1SLionel Sambuc<pre>int lua_yield (lua_State *L, int nresults);</pre> 523411be35a1SLionel Sambuc 523511be35a1SLionel Sambuc<p> 5236*0a6a1f1dSLionel SambucThis function is equivalent to <a href="#lua_yieldk"><code>lua_yieldk</code></a>, 5237*0a6a1f1dSLionel Sambucbut it has no continuation (see <a href="#4.7">§4.7</a>). 5238*0a6a1f1dSLionel SambucTherefore, when the thread resumes, 5239*0a6a1f1dSLionel Sambucit continues the function that called 5240*0a6a1f1dSLionel Sambucthe function calling <code>lua_yield</code>. 5241*0a6a1f1dSLionel Sambuc 5242*0a6a1f1dSLionel Sambuc 5243*0a6a1f1dSLionel Sambuc 5244*0a6a1f1dSLionel Sambuc 5245*0a6a1f1dSLionel Sambuc 5246*0a6a1f1dSLionel Sambuc<hr><h3><a name="lua_yieldk"><code>lua_yieldk</code></a></h3><p> 5247*0a6a1f1dSLionel Sambuc<span class="apii">[-?, +?, <em>e</em>]</span> 5248*0a6a1f1dSLionel Sambuc<pre>int lua_yieldk (lua_State *L, 5249*0a6a1f1dSLionel Sambuc int nresults, 5250*0a6a1f1dSLionel Sambuc lua_KContext ctx, 5251*0a6a1f1dSLionel Sambuc lua_KFunction k);</pre> 5252*0a6a1f1dSLionel Sambuc 5253*0a6a1f1dSLionel Sambuc<p> 5254*0a6a1f1dSLionel SambucYields a coroutine (thread). 525511be35a1SLionel Sambuc 525611be35a1SLionel Sambuc 525711be35a1SLionel Sambuc<p> 5258*0a6a1f1dSLionel SambucWhen a C function calls <a href="#lua_yieldk"><code>lua_yieldk</code></a>, 525911be35a1SLionel Sambucthe running coroutine suspends its execution, 526011be35a1SLionel Sambucand the call to <a href="#lua_resume"><code>lua_resume</code></a> that started this coroutine returns. 526111be35a1SLionel SambucThe parameter <code>nresults</code> is the number of values from the stack 5262*0a6a1f1dSLionel Sambucthat will be passed as results to <a href="#lua_resume"><code>lua_resume</code></a>. 5263*0a6a1f1dSLionel Sambuc 5264*0a6a1f1dSLionel Sambuc 5265*0a6a1f1dSLionel Sambuc<p> 5266*0a6a1f1dSLionel SambucWhen the coroutine is resumed again, 5267*0a6a1f1dSLionel SambucLua calls the given continuation function <code>k</code> to continue 5268*0a6a1f1dSLionel Sambucthe execution of the C function that yielded (see <a href="#4.7">§4.7</a>). 5269*0a6a1f1dSLionel SambucThis continuation function receives the same stack 5270*0a6a1f1dSLionel Sambucfrom the previous function, 5271*0a6a1f1dSLionel Sambucwith the <code>n</code> results removed and 5272*0a6a1f1dSLionel Sambucreplaced by the arguments passed to <a href="#lua_resume"><code>lua_resume</code></a>. 5273*0a6a1f1dSLionel SambucMoreover, 5274*0a6a1f1dSLionel Sambucthe continuation function receives the value <code>ctx</code> 5275*0a6a1f1dSLionel Sambucthat was passed to <a href="#lua_yieldk"><code>lua_yieldk</code></a>. 5276*0a6a1f1dSLionel Sambuc 5277*0a6a1f1dSLionel Sambuc 5278*0a6a1f1dSLionel Sambuc<p> 5279*0a6a1f1dSLionel SambucUsually, this function does not return; 5280*0a6a1f1dSLionel Sambucwhen the coroutine eventually resumes, 5281*0a6a1f1dSLionel Sambucit continues executing the continuation function. 5282*0a6a1f1dSLionel SambucHowever, there is one special case, 5283*0a6a1f1dSLionel Sambucwhich is when this function is called 5284*0a6a1f1dSLionel Sambucfrom inside a line hook (see <a href="#4.9">§4.9</a>). 5285*0a6a1f1dSLionel SambucIn that case, <code>lua_yieldk</code> should be called with no continuation 5286*0a6a1f1dSLionel Sambuc(probably in the form of <a href="#lua_yield"><code>lua_yield</code></a>), 5287*0a6a1f1dSLionel Sambucand the hook should return immediately after the call. 5288*0a6a1f1dSLionel SambucLua will yield and, 5289*0a6a1f1dSLionel Sambucwhen the coroutine resumes again, 5290*0a6a1f1dSLionel Sambucit will continue the normal execution 5291*0a6a1f1dSLionel Sambucof the (Lua) function that triggered the hook. 5292*0a6a1f1dSLionel Sambuc 5293*0a6a1f1dSLionel Sambuc 5294*0a6a1f1dSLionel Sambuc<p> 5295*0a6a1f1dSLionel SambucThis function can raise an error if it is called from a thread 5296*0a6a1f1dSLionel Sambucwith a pending C call with no continuation function, 5297*0a6a1f1dSLionel Sambucor it is called from a thread that is not running inside a resume 5298*0a6a1f1dSLionel Sambuc(e.g., the main thread). 529911be35a1SLionel Sambuc 530011be35a1SLionel Sambuc 530111be35a1SLionel Sambuc 530211be35a1SLionel Sambuc 530311be35a1SLionel Sambuc 530411be35a1SLionel Sambuc 530511be35a1SLionel Sambuc 5306*0a6a1f1dSLionel Sambuc<h2>4.9 – <a name="4.9">The Debug Interface</a></h2> 530711be35a1SLionel Sambuc 530811be35a1SLionel Sambuc<p> 530911be35a1SLionel SambucLua has no built-in debugging facilities. 531011be35a1SLionel SambucInstead, it offers a special interface 531111be35a1SLionel Sambucby means of functions and <em>hooks</em>. 531211be35a1SLionel SambucThis interface allows the construction of different 531311be35a1SLionel Sambuckinds of debuggers, profilers, and other tools 531411be35a1SLionel Sambucthat need "inside information" from the interpreter. 531511be35a1SLionel Sambuc 531611be35a1SLionel Sambuc 531711be35a1SLionel Sambuc 531811be35a1SLionel Sambuc<hr><h3><a name="lua_Debug"><code>lua_Debug</code></a></h3> 531911be35a1SLionel Sambuc<pre>typedef struct lua_Debug { 532011be35a1SLionel Sambuc int event; 532111be35a1SLionel Sambuc const char *name; /* (n) */ 532211be35a1SLionel Sambuc const char *namewhat; /* (n) */ 532311be35a1SLionel Sambuc const char *what; /* (S) */ 532411be35a1SLionel Sambuc const char *source; /* (S) */ 532511be35a1SLionel Sambuc int currentline; /* (l) */ 532611be35a1SLionel Sambuc int linedefined; /* (S) */ 532711be35a1SLionel Sambuc int lastlinedefined; /* (S) */ 5328*0a6a1f1dSLionel Sambuc unsigned char nups; /* (u) number of upvalues */ 5329*0a6a1f1dSLionel Sambuc unsigned char nparams; /* (u) number of parameters */ 5330*0a6a1f1dSLionel Sambuc char isvararg; /* (u) */ 5331*0a6a1f1dSLionel Sambuc char istailcall; /* (t) */ 533211be35a1SLionel Sambuc char short_src[LUA_IDSIZE]; /* (S) */ 533311be35a1SLionel Sambuc /* private part */ 533411be35a1SLionel Sambuc <em>other fields</em> 533511be35a1SLionel Sambuc} lua_Debug;</pre> 533611be35a1SLionel Sambuc 533711be35a1SLionel Sambuc<p> 533811be35a1SLionel SambucA structure used to carry different pieces of 5339*0a6a1f1dSLionel Sambucinformation about a function or an activation record. 534011be35a1SLionel Sambuc<a href="#lua_getstack"><code>lua_getstack</code></a> fills only the private part 534111be35a1SLionel Sambucof this structure, for later use. 534211be35a1SLionel SambucTo fill the other fields of <a href="#lua_Debug"><code>lua_Debug</code></a> with useful information, 534311be35a1SLionel Sambuccall <a href="#lua_getinfo"><code>lua_getinfo</code></a>. 534411be35a1SLionel Sambuc 534511be35a1SLionel Sambuc 534611be35a1SLionel Sambuc<p> 534711be35a1SLionel SambucThe fields of <a href="#lua_Debug"><code>lua_Debug</code></a> have the following meaning: 534811be35a1SLionel Sambuc 534911be35a1SLionel Sambuc<ul> 535011be35a1SLionel Sambuc 535111be35a1SLionel Sambuc<li><b><code>source</code>: </b> 5352*0a6a1f1dSLionel Sambucthe name of the chunk that created the function. 5353*0a6a1f1dSLionel SambucIf <code>source</code> starts with a '<code>@</code>', 5354*0a6a1f1dSLionel Sambucit means that the function was defined in a file where 5355*0a6a1f1dSLionel Sambucthe file name follows the '<code>@</code>'. 5356*0a6a1f1dSLionel SambucIf <code>source</code> starts with a '<code>=</code>', 5357*0a6a1f1dSLionel Sambucthe remainder of its contents describe the source in a user-dependent manner. 5358*0a6a1f1dSLionel SambucOtherwise, 5359*0a6a1f1dSLionel Sambucthe function was defined in a string where 5360*0a6a1f1dSLionel Sambuc<code>source</code> is that string. 536111be35a1SLionel Sambuc</li> 536211be35a1SLionel Sambuc 536311be35a1SLionel Sambuc<li><b><code>short_src</code>: </b> 536411be35a1SLionel Sambuca "printable" version of <code>source</code>, to be used in error messages. 536511be35a1SLionel Sambuc</li> 536611be35a1SLionel Sambuc 536711be35a1SLionel Sambuc<li><b><code>linedefined</code>: </b> 536811be35a1SLionel Sambucthe line number where the definition of the function starts. 536911be35a1SLionel Sambuc</li> 537011be35a1SLionel Sambuc 537111be35a1SLionel Sambuc<li><b><code>lastlinedefined</code>: </b> 537211be35a1SLionel Sambucthe line number where the definition of the function ends. 537311be35a1SLionel Sambuc</li> 537411be35a1SLionel Sambuc 537511be35a1SLionel Sambuc<li><b><code>what</code>: </b> 537611be35a1SLionel Sambucthe string <code>"Lua"</code> if the function is a Lua function, 537711be35a1SLionel Sambuc<code>"C"</code> if it is a C function, 5378*0a6a1f1dSLionel Sambuc<code>"main"</code> if it is the main part of a chunk. 537911be35a1SLionel Sambuc</li> 538011be35a1SLionel Sambuc 538111be35a1SLionel Sambuc<li><b><code>currentline</code>: </b> 538211be35a1SLionel Sambucthe current line where the given function is executing. 538311be35a1SLionel SambucWhen no line information is available, 538411be35a1SLionel Sambuc<code>currentline</code> is set to -1. 538511be35a1SLionel Sambuc</li> 538611be35a1SLionel Sambuc 538711be35a1SLionel Sambuc<li><b><code>name</code>: </b> 538811be35a1SLionel Sambuca reasonable name for the given function. 538911be35a1SLionel SambucBecause functions in Lua are first-class values, 539011be35a1SLionel Sambucthey do not have a fixed name: 539111be35a1SLionel Sambucsome functions can be the value of multiple global variables, 539211be35a1SLionel Sambucwhile others can be stored only in a table field. 539311be35a1SLionel SambucThe <code>lua_getinfo</code> function checks how the function was 539411be35a1SLionel Sambuccalled to find a suitable name. 539511be35a1SLionel SambucIf it cannot find a name, 539611be35a1SLionel Sambucthen <code>name</code> is set to <code>NULL</code>. 539711be35a1SLionel Sambuc</li> 539811be35a1SLionel Sambuc 539911be35a1SLionel Sambuc<li><b><code>namewhat</code>: </b> 540011be35a1SLionel Sambucexplains the <code>name</code> field. 540111be35a1SLionel SambucThe value of <code>namewhat</code> can be 540211be35a1SLionel Sambuc<code>"global"</code>, <code>"local"</code>, <code>"method"</code>, 540311be35a1SLionel Sambuc<code>"field"</code>, <code>"upvalue"</code>, or <code>""</code> (the empty string), 540411be35a1SLionel Sambucaccording to how the function was called. 540511be35a1SLionel Sambuc(Lua uses the empty string when no other option seems to apply.) 540611be35a1SLionel Sambuc</li> 540711be35a1SLionel Sambuc 5408*0a6a1f1dSLionel Sambuc<li><b><code>istailcall</code>: </b> 5409*0a6a1f1dSLionel Sambuctrue if this function invocation was called by a tail call. 5410*0a6a1f1dSLionel SambucIn this case, the caller of this level is not in the stack. 5411*0a6a1f1dSLionel Sambuc</li> 5412*0a6a1f1dSLionel Sambuc 541311be35a1SLionel Sambuc<li><b><code>nups</code>: </b> 541411be35a1SLionel Sambucthe number of upvalues of the function. 541511be35a1SLionel Sambuc</li> 541611be35a1SLionel Sambuc 5417*0a6a1f1dSLionel Sambuc<li><b><code>nparams</code>: </b> 5418*0a6a1f1dSLionel Sambucthe number of fixed parameters of the function 5419*0a6a1f1dSLionel Sambuc(always 0 for C functions). 5420*0a6a1f1dSLionel Sambuc</li> 5421*0a6a1f1dSLionel Sambuc 5422*0a6a1f1dSLionel Sambuc<li><b><code>isvararg</code>: </b> 5423*0a6a1f1dSLionel Sambuctrue if the function is a vararg function 5424*0a6a1f1dSLionel Sambuc(always true for C functions). 5425*0a6a1f1dSLionel Sambuc</li> 5426*0a6a1f1dSLionel Sambuc 542711be35a1SLionel Sambuc</ul> 542811be35a1SLionel Sambuc 542911be35a1SLionel Sambuc 543011be35a1SLionel Sambuc 543111be35a1SLionel Sambuc 543211be35a1SLionel Sambuc<hr><h3><a name="lua_gethook"><code>lua_gethook</code></a></h3><p> 5433*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 543411be35a1SLionel Sambuc<pre>lua_Hook lua_gethook (lua_State *L);</pre> 543511be35a1SLionel Sambuc 543611be35a1SLionel Sambuc<p> 543711be35a1SLionel SambucReturns the current hook function. 543811be35a1SLionel Sambuc 543911be35a1SLionel Sambuc 544011be35a1SLionel Sambuc 544111be35a1SLionel Sambuc 544211be35a1SLionel Sambuc 544311be35a1SLionel Sambuc<hr><h3><a name="lua_gethookcount"><code>lua_gethookcount</code></a></h3><p> 5444*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 544511be35a1SLionel Sambuc<pre>int lua_gethookcount (lua_State *L);</pre> 544611be35a1SLionel Sambuc 544711be35a1SLionel Sambuc<p> 544811be35a1SLionel SambucReturns the current hook count. 544911be35a1SLionel Sambuc 545011be35a1SLionel Sambuc 545111be35a1SLionel Sambuc 545211be35a1SLionel Sambuc 545311be35a1SLionel Sambuc 545411be35a1SLionel Sambuc<hr><h3><a name="lua_gethookmask"><code>lua_gethookmask</code></a></h3><p> 5455*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 545611be35a1SLionel Sambuc<pre>int lua_gethookmask (lua_State *L);</pre> 545711be35a1SLionel Sambuc 545811be35a1SLionel Sambuc<p> 545911be35a1SLionel SambucReturns the current hook mask. 546011be35a1SLionel Sambuc 546111be35a1SLionel Sambuc 546211be35a1SLionel Sambuc 546311be35a1SLionel Sambuc 546411be35a1SLionel Sambuc 546511be35a1SLionel Sambuc<hr><h3><a name="lua_getinfo"><code>lua_getinfo</code></a></h3><p> 5466*0a6a1f1dSLionel Sambuc<span class="apii">[-(0|1), +(0|1|2), <em>e</em>]</span> 546711be35a1SLionel Sambuc<pre>int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);</pre> 546811be35a1SLionel Sambuc 546911be35a1SLionel Sambuc<p> 5470*0a6a1f1dSLionel SambucGets information about a specific function or function invocation. 547111be35a1SLionel Sambuc 547211be35a1SLionel Sambuc 547311be35a1SLionel Sambuc<p> 547411be35a1SLionel SambucTo get information about a function invocation, 547511be35a1SLionel Sambucthe parameter <code>ar</code> must be a valid activation record that was 547611be35a1SLionel Sambucfilled by a previous call to <a href="#lua_getstack"><code>lua_getstack</code></a> or 547711be35a1SLionel Sambucgiven as argument to a hook (see <a href="#lua_Hook"><code>lua_Hook</code></a>). 547811be35a1SLionel Sambuc 547911be35a1SLionel Sambuc 548011be35a1SLionel Sambuc<p> 548111be35a1SLionel SambucTo get information about a function you push it onto the stack 548211be35a1SLionel Sambucand start the <code>what</code> string with the character '<code>></code>'. 548311be35a1SLionel Sambuc(In that case, 5484*0a6a1f1dSLionel Sambuc<code>lua_getinfo</code> pops the function from the top of the stack.) 548511be35a1SLionel SambucFor instance, to know in which line a function <code>f</code> was defined, 548611be35a1SLionel Sambucyou can write the following code: 548711be35a1SLionel Sambuc 548811be35a1SLionel Sambuc<pre> 548911be35a1SLionel Sambuc lua_Debug ar; 5490*0a6a1f1dSLionel Sambuc lua_getglobal(L, "f"); /* get global 'f' */ 549111be35a1SLionel Sambuc lua_getinfo(L, ">S", &ar); 549211be35a1SLionel Sambuc printf("%d\n", ar.linedefined); 549311be35a1SLionel Sambuc</pre> 549411be35a1SLionel Sambuc 549511be35a1SLionel Sambuc<p> 549611be35a1SLionel SambucEach character in the string <code>what</code> 549711be35a1SLionel Sambucselects some fields of the structure <code>ar</code> to be filled or 549811be35a1SLionel Sambuca value to be pushed on the stack: 549911be35a1SLionel Sambuc 550011be35a1SLionel Sambuc<ul> 550111be35a1SLionel Sambuc 550211be35a1SLionel Sambuc<li><b>'<code>n</code>': </b> fills in the field <code>name</code> and <code>namewhat</code>; 550311be35a1SLionel Sambuc</li> 550411be35a1SLionel Sambuc 550511be35a1SLionel Sambuc<li><b>'<code>S</code>': </b> 550611be35a1SLionel Sambucfills in the fields <code>source</code>, <code>short_src</code>, 550711be35a1SLionel Sambuc<code>linedefined</code>, <code>lastlinedefined</code>, and <code>what</code>; 550811be35a1SLionel Sambuc</li> 550911be35a1SLionel Sambuc 551011be35a1SLionel Sambuc<li><b>'<code>l</code>': </b> fills in the field <code>currentline</code>; 551111be35a1SLionel Sambuc</li> 551211be35a1SLionel Sambuc 5513*0a6a1f1dSLionel Sambuc<li><b>'<code>t</code>': </b> fills in the field <code>istailcall</code>; 5514*0a6a1f1dSLionel Sambuc</li> 5515*0a6a1f1dSLionel Sambuc 5516*0a6a1f1dSLionel Sambuc<li><b>'<code>u</code>': </b> fills in the fields 5517*0a6a1f1dSLionel Sambuc<code>nups</code>, <code>nparams</code>, and <code>isvararg</code>; 551811be35a1SLionel Sambuc</li> 551911be35a1SLionel Sambuc 552011be35a1SLionel Sambuc<li><b>'<code>f</code>': </b> 552111be35a1SLionel Sambucpushes onto the stack the function that is 552211be35a1SLionel Sambucrunning at the given level; 552311be35a1SLionel Sambuc</li> 552411be35a1SLionel Sambuc 552511be35a1SLionel Sambuc<li><b>'<code>L</code>': </b> 552611be35a1SLionel Sambucpushes onto the stack a table whose indices are the 552711be35a1SLionel Sambucnumbers of the lines that are valid on the function. 552811be35a1SLionel Sambuc(A <em>valid line</em> is a line with some associated code, 552911be35a1SLionel Sambucthat is, a line where you can put a break point. 553011be35a1SLionel SambucNon-valid lines include empty lines and comments.) 5531*0a6a1f1dSLionel Sambuc 5532*0a6a1f1dSLionel Sambuc 5533*0a6a1f1dSLionel Sambuc<p> 5534*0a6a1f1dSLionel SambucIf this option is given together with option '<code>f</code>', 5535*0a6a1f1dSLionel Sambucits table is pushed after the function. 553611be35a1SLionel Sambuc</li> 553711be35a1SLionel Sambuc 553811be35a1SLionel Sambuc</ul> 553911be35a1SLionel Sambuc 554011be35a1SLionel Sambuc<p> 554111be35a1SLionel SambucThis function returns 0 on error 554211be35a1SLionel Sambuc(for instance, an invalid option in <code>what</code>). 554311be35a1SLionel Sambuc 554411be35a1SLionel Sambuc 554511be35a1SLionel Sambuc 554611be35a1SLionel Sambuc 554711be35a1SLionel Sambuc 554811be35a1SLionel Sambuc<hr><h3><a name="lua_getlocal"><code>lua_getlocal</code></a></h3><p> 5549*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +(0|1), –]</span> 5550*0a6a1f1dSLionel Sambuc<pre>const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n);</pre> 555111be35a1SLionel Sambuc 555211be35a1SLionel Sambuc<p> 5553*0a6a1f1dSLionel SambucGets information about a local variable of 5554*0a6a1f1dSLionel Sambuca given activation record or a given function. 5555*0a6a1f1dSLionel Sambuc 5556*0a6a1f1dSLionel Sambuc 5557*0a6a1f1dSLionel Sambuc<p> 5558*0a6a1f1dSLionel SambucIn the first case, 5559*0a6a1f1dSLionel Sambucthe parameter <code>ar</code> must be a valid activation record that was 556011be35a1SLionel Sambucfilled by a previous call to <a href="#lua_getstack"><code>lua_getstack</code></a> or 556111be35a1SLionel Sambucgiven as argument to a hook (see <a href="#lua_Hook"><code>lua_Hook</code></a>). 5562*0a6a1f1dSLionel SambucThe index <code>n</code> selects which local variable to inspect; 5563*0a6a1f1dSLionel Sambucsee <a href="#pdf-debug.getlocal"><code>debug.getlocal</code></a> for details about variable indices 5564*0a6a1f1dSLionel Sambucand names. 5565*0a6a1f1dSLionel Sambuc 5566*0a6a1f1dSLionel Sambuc 5567*0a6a1f1dSLionel Sambuc<p> 556811be35a1SLionel Sambuc<a href="#lua_getlocal"><code>lua_getlocal</code></a> pushes the variable's value onto the stack 556911be35a1SLionel Sambucand returns its name. 557011be35a1SLionel Sambuc 557111be35a1SLionel Sambuc 557211be35a1SLionel Sambuc<p> 5573*0a6a1f1dSLionel SambucIn the second case, <code>ar</code> must be <code>NULL</code> and the function 5574*0a6a1f1dSLionel Sambucto be inspected must be at the top of the stack. 5575*0a6a1f1dSLionel SambucIn this case, only parameters of Lua functions are visible 5576*0a6a1f1dSLionel Sambuc(as there is no information about what variables are active) 5577*0a6a1f1dSLionel Sambucand no values are pushed onto the stack. 557811be35a1SLionel Sambuc 557911be35a1SLionel Sambuc 558011be35a1SLionel Sambuc<p> 558111be35a1SLionel SambucReturns <code>NULL</code> (and pushes nothing) 558211be35a1SLionel Sambucwhen the index is greater than 558311be35a1SLionel Sambucthe number of active local variables. 558411be35a1SLionel Sambuc 558511be35a1SLionel Sambuc 558611be35a1SLionel Sambuc 558711be35a1SLionel Sambuc 558811be35a1SLionel Sambuc 558911be35a1SLionel Sambuc<hr><h3><a name="lua_getstack"><code>lua_getstack</code></a></h3><p> 5590*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 559111be35a1SLionel Sambuc<pre>int lua_getstack (lua_State *L, int level, lua_Debug *ar);</pre> 559211be35a1SLionel Sambuc 559311be35a1SLionel Sambuc<p> 5594*0a6a1f1dSLionel SambucGets information about the interpreter runtime stack. 559511be35a1SLionel Sambuc 559611be35a1SLionel Sambuc 559711be35a1SLionel Sambuc<p> 559811be35a1SLionel SambucThis function fills parts of a <a href="#lua_Debug"><code>lua_Debug</code></a> structure with 559911be35a1SLionel Sambucan identification of the <em>activation record</em> 560011be35a1SLionel Sambucof the function executing at a given level. 560111be35a1SLionel SambucLevel 0 is the current running function, 5602*0a6a1f1dSLionel Sambucwhereas level <em>n+1</em> is the function that has called level <em>n</em> 5603*0a6a1f1dSLionel Sambuc(except for tail calls, which do not count on the stack). 560411be35a1SLionel SambucWhen there are no errors, <a href="#lua_getstack"><code>lua_getstack</code></a> returns 1; 560511be35a1SLionel Sambucwhen called with a level greater than the stack depth, 560611be35a1SLionel Sambucit returns 0. 560711be35a1SLionel Sambuc 560811be35a1SLionel Sambuc 560911be35a1SLionel Sambuc 561011be35a1SLionel Sambuc 561111be35a1SLionel Sambuc 561211be35a1SLionel Sambuc<hr><h3><a name="lua_getupvalue"><code>lua_getupvalue</code></a></h3><p> 5613*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +(0|1), –]</span> 561411be35a1SLionel Sambuc<pre>const char *lua_getupvalue (lua_State *L, int funcindex, int n);</pre> 561511be35a1SLionel Sambuc 561611be35a1SLionel Sambuc<p> 5617*0a6a1f1dSLionel SambucGets information about the <code>n</code>-th upvalue 5618*0a6a1f1dSLionel Sambucof the closure at index <code>funcindex</code>. 5619*0a6a1f1dSLionel SambucIt pushes the upvalue's value onto the stack 562011be35a1SLionel Sambucand returns its name. 5621*0a6a1f1dSLionel SambucReturns <code>NULL</code> (and pushes nothing) 5622*0a6a1f1dSLionel Sambucwhen the index <code>n</code> is greater than the number of upvalues. 562311be35a1SLionel Sambuc 562411be35a1SLionel Sambuc 562511be35a1SLionel Sambuc<p> 562611be35a1SLionel SambucFor C functions, this function uses the empty string <code>""</code> 562711be35a1SLionel Sambucas a name for all upvalues. 5628*0a6a1f1dSLionel Sambuc(For Lua functions, 5629*0a6a1f1dSLionel Sambucupvalues are the external local variables that the function uses, 5630*0a6a1f1dSLionel Sambucand that are consequently included in its closure.) 5631*0a6a1f1dSLionel Sambuc 5632*0a6a1f1dSLionel Sambuc 5633*0a6a1f1dSLionel Sambuc<p> 5634*0a6a1f1dSLionel SambucUpvalues have no particular order, 5635*0a6a1f1dSLionel Sambucas they are active through the whole function. 5636*0a6a1f1dSLionel SambucThey are numbered in an arbitrary order. 563711be35a1SLionel Sambuc 563811be35a1SLionel Sambuc 563911be35a1SLionel Sambuc 564011be35a1SLionel Sambuc 564111be35a1SLionel Sambuc 564211be35a1SLionel Sambuc<hr><h3><a name="lua_Hook"><code>lua_Hook</code></a></h3> 564311be35a1SLionel Sambuc<pre>typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);</pre> 564411be35a1SLionel Sambuc 564511be35a1SLionel Sambuc<p> 564611be35a1SLionel SambucType for debugging hook functions. 564711be35a1SLionel Sambuc 564811be35a1SLionel Sambuc 564911be35a1SLionel Sambuc<p> 565011be35a1SLionel SambucWhenever a hook is called, its <code>ar</code> argument has its field 565111be35a1SLionel Sambuc<code>event</code> set to the specific event that triggered the hook. 565211be35a1SLionel SambucLua identifies these events with the following constants: 565311be35a1SLionel Sambuc<a name="pdf-LUA_HOOKCALL"><code>LUA_HOOKCALL</code></a>, <a name="pdf-LUA_HOOKRET"><code>LUA_HOOKRET</code></a>, 5654*0a6a1f1dSLionel Sambuc<a name="pdf-LUA_HOOKTAILCALL"><code>LUA_HOOKTAILCALL</code></a>, <a name="pdf-LUA_HOOKLINE"><code>LUA_HOOKLINE</code></a>, 565511be35a1SLionel Sambucand <a name="pdf-LUA_HOOKCOUNT"><code>LUA_HOOKCOUNT</code></a>. 565611be35a1SLionel SambucMoreover, for line events, the field <code>currentline</code> is also set. 565711be35a1SLionel SambucTo get the value of any other field in <code>ar</code>, 565811be35a1SLionel Sambucthe hook must call <a href="#lua_getinfo"><code>lua_getinfo</code></a>. 5659*0a6a1f1dSLionel Sambuc 5660*0a6a1f1dSLionel Sambuc 5661*0a6a1f1dSLionel Sambuc<p> 5662*0a6a1f1dSLionel SambucFor call events, <code>event</code> can be <code>LUA_HOOKCALL</code>, 5663*0a6a1f1dSLionel Sambucthe normal value, or <code>LUA_HOOKTAILCALL</code>, for a tail call; 5664*0a6a1f1dSLionel Sambucin this case, there will be no corresponding return event. 566511be35a1SLionel Sambuc 566611be35a1SLionel Sambuc 566711be35a1SLionel Sambuc<p> 566811be35a1SLionel SambucWhile Lua is running a hook, it disables other calls to hooks. 566911be35a1SLionel SambucTherefore, if a hook calls back Lua to execute a function or a chunk, 567011be35a1SLionel Sambucthis execution occurs without any calls to hooks. 567111be35a1SLionel Sambuc 567211be35a1SLionel Sambuc 5673*0a6a1f1dSLionel Sambuc<p> 5674*0a6a1f1dSLionel SambucHook functions cannot have continuations, 5675*0a6a1f1dSLionel Sambucthat is, they cannot call <a href="#lua_yieldk"><code>lua_yieldk</code></a>, 5676*0a6a1f1dSLionel Sambuc<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>. 5677*0a6a1f1dSLionel Sambuc 5678*0a6a1f1dSLionel Sambuc 5679*0a6a1f1dSLionel Sambuc<p> 5680*0a6a1f1dSLionel SambucHook functions can yield under the following conditions: 5681*0a6a1f1dSLionel SambucOnly count and line events can yield; 5682*0a6a1f1dSLionel Sambucto yield, a hook function must finish its execution 5683*0a6a1f1dSLionel Sambuccalling <a href="#lua_yield"><code>lua_yield</code></a> with <code>nresults</code> equal to zero 5684*0a6a1f1dSLionel Sambuc(that is, with no values). 5685*0a6a1f1dSLionel Sambuc 5686*0a6a1f1dSLionel Sambuc 568711be35a1SLionel Sambuc 568811be35a1SLionel Sambuc 568911be35a1SLionel Sambuc 569011be35a1SLionel Sambuc<hr><h3><a name="lua_sethook"><code>lua_sethook</code></a></h3><p> 5691*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 5692*0a6a1f1dSLionel Sambuc<pre>void lua_sethook (lua_State *L, lua_Hook f, int mask, int count);</pre> 569311be35a1SLionel Sambuc 569411be35a1SLionel Sambuc<p> 569511be35a1SLionel SambucSets the debugging hook function. 569611be35a1SLionel Sambuc 569711be35a1SLionel Sambuc 569811be35a1SLionel Sambuc<p> 569911be35a1SLionel SambucArgument <code>f</code> is the hook function. 570011be35a1SLionel Sambuc<code>mask</code> specifies on which events the hook will be called: 570111be35a1SLionel Sambucit is formed by a bitwise or of the constants 570211be35a1SLionel Sambuc<a name="pdf-LUA_MASKCALL"><code>LUA_MASKCALL</code></a>, 570311be35a1SLionel Sambuc<a name="pdf-LUA_MASKRET"><code>LUA_MASKRET</code></a>, 570411be35a1SLionel Sambuc<a name="pdf-LUA_MASKLINE"><code>LUA_MASKLINE</code></a>, 570511be35a1SLionel Sambucand <a name="pdf-LUA_MASKCOUNT"><code>LUA_MASKCOUNT</code></a>. 570611be35a1SLionel SambucThe <code>count</code> argument is only meaningful when the mask 570711be35a1SLionel Sambucincludes <code>LUA_MASKCOUNT</code>. 570811be35a1SLionel SambucFor each event, the hook is called as explained below: 570911be35a1SLionel Sambuc 571011be35a1SLionel Sambuc<ul> 571111be35a1SLionel Sambuc 571211be35a1SLionel Sambuc<li><b>The call hook: </b> is called when the interpreter calls a function. 571311be35a1SLionel SambucThe hook is called just after Lua enters the new function, 571411be35a1SLionel Sambucbefore the function gets its arguments. 571511be35a1SLionel Sambuc</li> 571611be35a1SLionel Sambuc 571711be35a1SLionel Sambuc<li><b>The return hook: </b> is called when the interpreter returns from a function. 571811be35a1SLionel SambucThe hook is called just before Lua leaves the function. 5719*0a6a1f1dSLionel SambucThere is no standard way to access the values 5720*0a6a1f1dSLionel Sambucto be returned by the function. 572111be35a1SLionel Sambuc</li> 572211be35a1SLionel Sambuc 572311be35a1SLionel Sambuc<li><b>The line hook: </b> is called when the interpreter is about to 572411be35a1SLionel Sambucstart the execution of a new line of code, 572511be35a1SLionel Sambucor when it jumps back in the code (even to the same line). 572611be35a1SLionel Sambuc(This event only happens while Lua is executing a Lua function.) 572711be35a1SLionel Sambuc</li> 572811be35a1SLionel Sambuc 572911be35a1SLionel Sambuc<li><b>The count hook: </b> is called after the interpreter executes every 573011be35a1SLionel Sambuc<code>count</code> instructions. 573111be35a1SLionel Sambuc(This event only happens while Lua is executing a Lua function.) 573211be35a1SLionel Sambuc</li> 573311be35a1SLionel Sambuc 573411be35a1SLionel Sambuc</ul> 573511be35a1SLionel Sambuc 573611be35a1SLionel Sambuc<p> 573711be35a1SLionel SambucA hook is disabled by setting <code>mask</code> to zero. 573811be35a1SLionel Sambuc 573911be35a1SLionel Sambuc 574011be35a1SLionel Sambuc 574111be35a1SLionel Sambuc 574211be35a1SLionel Sambuc 574311be35a1SLionel Sambuc<hr><h3><a name="lua_setlocal"><code>lua_setlocal</code></a></h3><p> 5744*0a6a1f1dSLionel Sambuc<span class="apii">[-(0|1), +0, –]</span> 5745*0a6a1f1dSLionel Sambuc<pre>const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n);</pre> 574611be35a1SLionel Sambuc 574711be35a1SLionel Sambuc<p> 574811be35a1SLionel SambucSets the value of a local variable of a given activation record. 5749*0a6a1f1dSLionel SambucIt assigns the value at the top of the stack 575011be35a1SLionel Sambucto the variable and returns its name. 575111be35a1SLionel SambucIt also pops the value from the stack. 575211be35a1SLionel Sambuc 575311be35a1SLionel Sambuc 575411be35a1SLionel Sambuc<p> 575511be35a1SLionel SambucReturns <code>NULL</code> (and pops nothing) 575611be35a1SLionel Sambucwhen the index is greater than 575711be35a1SLionel Sambucthe number of active local variables. 575811be35a1SLionel Sambuc 575911be35a1SLionel Sambuc 5760*0a6a1f1dSLionel Sambuc<p> 5761*0a6a1f1dSLionel SambucParameters <code>ar</code> and <code>n</code> are as in function <a href="#lua_getlocal"><code>lua_getlocal</code></a>. 5762*0a6a1f1dSLionel Sambuc 5763*0a6a1f1dSLionel Sambuc 576411be35a1SLionel Sambuc 576511be35a1SLionel Sambuc 576611be35a1SLionel Sambuc 576711be35a1SLionel Sambuc<hr><h3><a name="lua_setupvalue"><code>lua_setupvalue</code></a></h3><p> 5768*0a6a1f1dSLionel Sambuc<span class="apii">[-(0|1), +0, –]</span> 576911be35a1SLionel Sambuc<pre>const char *lua_setupvalue (lua_State *L, int funcindex, int n);</pre> 577011be35a1SLionel Sambuc 577111be35a1SLionel Sambuc<p> 577211be35a1SLionel SambucSets the value of a closure's upvalue. 577311be35a1SLionel SambucIt assigns the value at the top of the stack 577411be35a1SLionel Sambucto the upvalue and returns its name. 577511be35a1SLionel SambucIt also pops the value from the stack. 577611be35a1SLionel Sambuc 577711be35a1SLionel Sambuc 577811be35a1SLionel Sambuc<p> 577911be35a1SLionel SambucReturns <code>NULL</code> (and pops nothing) 5780*0a6a1f1dSLionel Sambucwhen the index <code>n</code> is greater than the number of upvalues. 5781*0a6a1f1dSLionel Sambuc 5782*0a6a1f1dSLionel Sambuc 5783*0a6a1f1dSLionel Sambuc<p> 5784*0a6a1f1dSLionel SambucParameters <code>funcindex</code> and <code>n</code> are as in function <a href="#lua_getupvalue"><code>lua_getupvalue</code></a>. 5785*0a6a1f1dSLionel Sambuc 5786*0a6a1f1dSLionel Sambuc 5787*0a6a1f1dSLionel Sambuc 5788*0a6a1f1dSLionel Sambuc 5789*0a6a1f1dSLionel Sambuc 5790*0a6a1f1dSLionel Sambuc<hr><h3><a name="lua_upvalueid"><code>lua_upvalueid</code></a></h3><p> 5791*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 5792*0a6a1f1dSLionel Sambuc<pre>void *lua_upvalueid (lua_State *L, int funcindex, int n);</pre> 5793*0a6a1f1dSLionel Sambuc 5794*0a6a1f1dSLionel Sambuc<p> 5795*0a6a1f1dSLionel SambucReturns a unique identifier for the upvalue numbered <code>n</code> 5796*0a6a1f1dSLionel Sambucfrom the closure at index <code>funcindex</code>. 5797*0a6a1f1dSLionel Sambuc 5798*0a6a1f1dSLionel Sambuc 5799*0a6a1f1dSLionel Sambuc<p> 5800*0a6a1f1dSLionel SambucThese unique identifiers allow a program to check whether different 5801*0a6a1f1dSLionel Sambucclosures share upvalues. 5802*0a6a1f1dSLionel SambucLua closures that share an upvalue 5803*0a6a1f1dSLionel Sambuc(that is, that access a same external local variable) 5804*0a6a1f1dSLionel Sambucwill return identical ids for those upvalue indices. 5805*0a6a1f1dSLionel Sambuc 5806*0a6a1f1dSLionel Sambuc 5807*0a6a1f1dSLionel Sambuc<p> 5808*0a6a1f1dSLionel SambucParameters <code>funcindex</code> and <code>n</code> are as in function <a href="#lua_getupvalue"><code>lua_getupvalue</code></a>, 5809*0a6a1f1dSLionel Sambucbut <code>n</code> cannot be greater than the number of upvalues. 5810*0a6a1f1dSLionel Sambuc 5811*0a6a1f1dSLionel Sambuc 5812*0a6a1f1dSLionel Sambuc 5813*0a6a1f1dSLionel Sambuc 5814*0a6a1f1dSLionel Sambuc 5815*0a6a1f1dSLionel Sambuc<hr><h3><a name="lua_upvaluejoin"><code>lua_upvaluejoin</code></a></h3><p> 5816*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 5817*0a6a1f1dSLionel Sambuc<pre>void lua_upvaluejoin (lua_State *L, int funcindex1, int n1, 5818*0a6a1f1dSLionel Sambuc int funcindex2, int n2);</pre> 5819*0a6a1f1dSLionel Sambuc 5820*0a6a1f1dSLionel Sambuc<p> 5821*0a6a1f1dSLionel SambucMake the <code>n1</code>-th upvalue of the Lua closure at index <code>funcindex1</code> 5822*0a6a1f1dSLionel Sambucrefer to the <code>n2</code>-th upvalue of the Lua closure at index <code>funcindex2</code>. 582311be35a1SLionel Sambuc 582411be35a1SLionel Sambuc 582511be35a1SLionel Sambuc 582611be35a1SLionel Sambuc 582711be35a1SLionel Sambuc 582811be35a1SLionel Sambuc 582911be35a1SLionel Sambuc 5830*0a6a1f1dSLionel Sambuc<h1>5 – <a name="5">The Auxiliary Library</a></h1> 583111be35a1SLionel Sambuc 583211be35a1SLionel Sambuc<p> 583311be35a1SLionel Sambuc 583411be35a1SLionel SambucThe <em>auxiliary library</em> provides several convenient functions 583511be35a1SLionel Sambucto interface C with Lua. 583611be35a1SLionel SambucWhile the basic API provides the primitive functions for all 583711be35a1SLionel Sambucinteractions between C and Lua, 583811be35a1SLionel Sambucthe auxiliary library provides higher-level functions for some 583911be35a1SLionel Sambuccommon tasks. 584011be35a1SLionel Sambuc 584111be35a1SLionel Sambuc 584211be35a1SLionel Sambuc<p> 5843*0a6a1f1dSLionel SambucAll functions and types from the auxiliary library 584411be35a1SLionel Sambucare defined in header file <code>lauxlib.h</code> and 584511be35a1SLionel Sambuchave a prefix <code>luaL_</code>. 584611be35a1SLionel Sambuc 584711be35a1SLionel Sambuc 584811be35a1SLionel Sambuc<p> 584911be35a1SLionel SambucAll functions in the auxiliary library are built on 585011be35a1SLionel Sambuctop of the basic API, 5851*0a6a1f1dSLionel Sambucand so they provide nothing that cannot be done with that API. 5852*0a6a1f1dSLionel SambucNevertheless, the use of the auxiliary library ensures 5853*0a6a1f1dSLionel Sambucmore consistency to your code. 5854*0a6a1f1dSLionel Sambuc 5855*0a6a1f1dSLionel Sambuc 5856*0a6a1f1dSLionel Sambuc<p> 5857*0a6a1f1dSLionel SambucSeveral functions in the auxiliary library use internally some 5858*0a6a1f1dSLionel Sambucextra stack slots. 5859*0a6a1f1dSLionel SambucWhen a function in the auxiliary library uses less than five slots, 5860*0a6a1f1dSLionel Sambucit does not check the stack size; 5861*0a6a1f1dSLionel Sambucit simply assumes that there are enough slots. 586211be35a1SLionel Sambuc 586311be35a1SLionel Sambuc 586411be35a1SLionel Sambuc<p> 586511be35a1SLionel SambucSeveral functions in the auxiliary library are used to 586611be35a1SLionel Sambuccheck C function arguments. 586711be35a1SLionel SambucBecause the error message is formatted for arguments 586811be35a1SLionel Sambuc(e.g., "<code>bad argument #1</code>"), 586911be35a1SLionel Sambucyou should not use these functions for other stack values. 587011be35a1SLionel Sambuc 587111be35a1SLionel Sambuc 5872*0a6a1f1dSLionel Sambuc<p> 5873*0a6a1f1dSLionel SambucFunctions called <code>luaL_check*</code> 5874*0a6a1f1dSLionel Sambucalways raise an error if the check is not satisfied. 587511be35a1SLionel Sambuc 5876*0a6a1f1dSLionel Sambuc 5877*0a6a1f1dSLionel Sambuc 5878*0a6a1f1dSLionel Sambuc<h2>5.1 – <a name="5.1">Functions and Types</a></h2> 587911be35a1SLionel Sambuc 588011be35a1SLionel Sambuc<p> 588111be35a1SLionel SambucHere we list all functions and types from the auxiliary library 588211be35a1SLionel Sambucin alphabetical order. 588311be35a1SLionel Sambuc 588411be35a1SLionel Sambuc 588511be35a1SLionel Sambuc 588611be35a1SLionel Sambuc<hr><h3><a name="luaL_addchar"><code>luaL_addchar</code></a></h3><p> 5887*0a6a1f1dSLionel Sambuc<span class="apii">[-?, +?, <em>e</em>]</span> 588811be35a1SLionel Sambuc<pre>void luaL_addchar (luaL_Buffer *B, char c);</pre> 588911be35a1SLionel Sambuc 589011be35a1SLionel Sambuc<p> 5891*0a6a1f1dSLionel SambucAdds the byte <code>c</code> to the buffer <code>B</code> 589211be35a1SLionel Sambuc(see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>). 589311be35a1SLionel Sambuc 589411be35a1SLionel Sambuc 589511be35a1SLionel Sambuc 589611be35a1SLionel Sambuc 589711be35a1SLionel Sambuc 589811be35a1SLionel Sambuc<hr><h3><a name="luaL_addlstring"><code>luaL_addlstring</code></a></h3><p> 5899*0a6a1f1dSLionel Sambuc<span class="apii">[-?, +?, <em>e</em>]</span> 590011be35a1SLionel Sambuc<pre>void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l);</pre> 590111be35a1SLionel Sambuc 590211be35a1SLionel Sambuc<p> 590311be35a1SLionel SambucAdds the string pointed to by <code>s</code> with length <code>l</code> to 590411be35a1SLionel Sambucthe buffer <code>B</code> 590511be35a1SLionel Sambuc(see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>). 5906*0a6a1f1dSLionel SambucThe string can contain embedded zeros. 590711be35a1SLionel Sambuc 590811be35a1SLionel Sambuc 590911be35a1SLionel Sambuc 591011be35a1SLionel Sambuc 591111be35a1SLionel Sambuc 591211be35a1SLionel Sambuc<hr><h3><a name="luaL_addsize"><code>luaL_addsize</code></a></h3><p> 5913*0a6a1f1dSLionel Sambuc<span class="apii">[-?, +?, <em>e</em>]</span> 591411be35a1SLionel Sambuc<pre>void luaL_addsize (luaL_Buffer *B, size_t n);</pre> 591511be35a1SLionel Sambuc 591611be35a1SLionel Sambuc<p> 591711be35a1SLionel SambucAdds to the buffer <code>B</code> (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>) 591811be35a1SLionel Sambuca string of length <code>n</code> previously copied to the 591911be35a1SLionel Sambucbuffer area (see <a href="#luaL_prepbuffer"><code>luaL_prepbuffer</code></a>). 592011be35a1SLionel Sambuc 592111be35a1SLionel Sambuc 592211be35a1SLionel Sambuc 592311be35a1SLionel Sambuc 592411be35a1SLionel Sambuc 592511be35a1SLionel Sambuc<hr><h3><a name="luaL_addstring"><code>luaL_addstring</code></a></h3><p> 5926*0a6a1f1dSLionel Sambuc<span class="apii">[-?, +?, <em>e</em>]</span> 592711be35a1SLionel Sambuc<pre>void luaL_addstring (luaL_Buffer *B, const char *s);</pre> 592811be35a1SLionel Sambuc 592911be35a1SLionel Sambuc<p> 593011be35a1SLionel SambucAdds the zero-terminated string pointed to by <code>s</code> 593111be35a1SLionel Sambucto the buffer <code>B</code> 593211be35a1SLionel Sambuc(see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>). 593311be35a1SLionel Sambuc 593411be35a1SLionel Sambuc 593511be35a1SLionel Sambuc 593611be35a1SLionel Sambuc 593711be35a1SLionel Sambuc 593811be35a1SLionel Sambuc<hr><h3><a name="luaL_addvalue"><code>luaL_addvalue</code></a></h3><p> 5939*0a6a1f1dSLionel Sambuc<span class="apii">[-1, +?, <em>e</em>]</span> 594011be35a1SLionel Sambuc<pre>void luaL_addvalue (luaL_Buffer *B);</pre> 594111be35a1SLionel Sambuc 594211be35a1SLionel Sambuc<p> 594311be35a1SLionel SambucAdds the value at the top of the stack 594411be35a1SLionel Sambucto the buffer <code>B</code> 594511be35a1SLionel Sambuc(see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>). 594611be35a1SLionel SambucPops the value. 594711be35a1SLionel Sambuc 594811be35a1SLionel Sambuc 594911be35a1SLionel Sambuc<p> 595011be35a1SLionel SambucThis is the only function on string buffers that can (and must) 595111be35a1SLionel Sambucbe called with an extra element on the stack, 595211be35a1SLionel Sambucwhich is the value to be added to the buffer. 595311be35a1SLionel Sambuc 595411be35a1SLionel Sambuc 595511be35a1SLionel Sambuc 595611be35a1SLionel Sambuc 595711be35a1SLionel Sambuc 595811be35a1SLionel Sambuc<hr><h3><a name="luaL_argcheck"><code>luaL_argcheck</code></a></h3><p> 595911be35a1SLionel Sambuc<span class="apii">[-0, +0, <em>v</em>]</span> 596011be35a1SLionel Sambuc<pre>void luaL_argcheck (lua_State *L, 596111be35a1SLionel Sambuc int cond, 5962*0a6a1f1dSLionel Sambuc int arg, 596311be35a1SLionel Sambuc const char *extramsg);</pre> 596411be35a1SLionel Sambuc 596511be35a1SLionel Sambuc<p> 596611be35a1SLionel SambucChecks whether <code>cond</code> is true. 5967*0a6a1f1dSLionel SambucIf it is not, raises an error with a standard message (see <a href="#luaL_argerror"><code>luaL_argerror</code></a>). 596811be35a1SLionel Sambuc 596911be35a1SLionel Sambuc 597011be35a1SLionel Sambuc 597111be35a1SLionel Sambuc 597211be35a1SLionel Sambuc 597311be35a1SLionel Sambuc<hr><h3><a name="luaL_argerror"><code>luaL_argerror</code></a></h3><p> 597411be35a1SLionel Sambuc<span class="apii">[-0, +0, <em>v</em>]</span> 5975*0a6a1f1dSLionel Sambuc<pre>int luaL_argerror (lua_State *L, int arg, const char *extramsg);</pre> 597611be35a1SLionel Sambuc 597711be35a1SLionel Sambuc<p> 5978*0a6a1f1dSLionel SambucRaises an error reporting a problem with argument <code>arg</code> 5979*0a6a1f1dSLionel Sambucof the C function that called it, 5980*0a6a1f1dSLionel Sambucusing a standard message 5981*0a6a1f1dSLionel Sambucthat includes <code>extramsg</code> as a comment: 598211be35a1SLionel Sambuc 598311be35a1SLionel Sambuc<pre> 5984*0a6a1f1dSLionel Sambuc bad argument #<em>arg</em> to '<em>funcname</em>' (<em>extramsg</em>) 5985*0a6a1f1dSLionel Sambuc</pre><p> 5986*0a6a1f1dSLionel SambucThis function never returns. 598711be35a1SLionel Sambuc 598811be35a1SLionel Sambuc 598911be35a1SLionel Sambuc 599011be35a1SLionel Sambuc 599111be35a1SLionel Sambuc 599211be35a1SLionel Sambuc<hr><h3><a name="luaL_Buffer"><code>luaL_Buffer</code></a></h3> 599311be35a1SLionel Sambuc<pre>typedef struct luaL_Buffer luaL_Buffer;</pre> 599411be35a1SLionel Sambuc 599511be35a1SLionel Sambuc<p> 599611be35a1SLionel SambucType for a <em>string buffer</em>. 599711be35a1SLionel Sambuc 599811be35a1SLionel Sambuc 599911be35a1SLionel Sambuc<p> 600011be35a1SLionel SambucA string buffer allows C code to build Lua strings piecemeal. 600111be35a1SLionel SambucIts pattern of use is as follows: 600211be35a1SLionel Sambuc 600311be35a1SLionel Sambuc<ul> 600411be35a1SLionel Sambuc 6005*0a6a1f1dSLionel Sambuc<li>First declare a variable <code>b</code> of type <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>.</li> 600611be35a1SLionel Sambuc 6007*0a6a1f1dSLionel Sambuc<li>Then initialize it with a call <code>luaL_buffinit(L, &b)</code>.</li> 600811be35a1SLionel Sambuc 600911be35a1SLionel Sambuc<li> 6010*0a6a1f1dSLionel SambucThen add string pieces to the buffer calling any of 601111be35a1SLionel Sambucthe <code>luaL_add*</code> functions. 601211be35a1SLionel Sambuc</li> 601311be35a1SLionel Sambuc 601411be35a1SLionel Sambuc<li> 6015*0a6a1f1dSLionel SambucFinish by calling <code>luaL_pushresult(&b)</code>. 601611be35a1SLionel SambucThis call leaves the final string on the top of the stack. 601711be35a1SLionel Sambuc</li> 601811be35a1SLionel Sambuc 601911be35a1SLionel Sambuc</ul> 602011be35a1SLionel Sambuc 602111be35a1SLionel Sambuc<p> 6022*0a6a1f1dSLionel SambucIf you know beforehand the total size of the resulting string, 6023*0a6a1f1dSLionel Sambucyou can use the buffer like this: 6024*0a6a1f1dSLionel Sambuc 6025*0a6a1f1dSLionel Sambuc<ul> 6026*0a6a1f1dSLionel Sambuc 6027*0a6a1f1dSLionel Sambuc<li>First declare a variable <code>b</code> of type <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>.</li> 6028*0a6a1f1dSLionel Sambuc 6029*0a6a1f1dSLionel Sambuc<li>Then initialize it and preallocate a space of 6030*0a6a1f1dSLionel Sambucsize <code>sz</code> with a call <code>luaL_buffinitsize(L, &b, sz)</code>.</li> 6031*0a6a1f1dSLionel Sambuc 6032*0a6a1f1dSLionel Sambuc<li>Then copy the string into that space.</li> 6033*0a6a1f1dSLionel Sambuc 6034*0a6a1f1dSLionel Sambuc<li> 6035*0a6a1f1dSLionel SambucFinish by calling <code>luaL_pushresultsize(&b, sz)</code>, 6036*0a6a1f1dSLionel Sambucwhere <code>sz</code> is the total size of the resulting string 6037*0a6a1f1dSLionel Sambuccopied into that space. 6038*0a6a1f1dSLionel Sambuc</li> 6039*0a6a1f1dSLionel Sambuc 6040*0a6a1f1dSLionel Sambuc</ul> 6041*0a6a1f1dSLionel Sambuc 6042*0a6a1f1dSLionel Sambuc<p> 604311be35a1SLionel SambucDuring its normal operation, 604411be35a1SLionel Sambuca string buffer uses a variable number of stack slots. 604511be35a1SLionel SambucSo, while using a buffer, you cannot assume that you know where 604611be35a1SLionel Sambucthe top of the stack is. 604711be35a1SLionel SambucYou can use the stack between successive calls to buffer operations 604811be35a1SLionel Sambucas long as that use is balanced; 604911be35a1SLionel Sambucthat is, 605011be35a1SLionel Sambucwhen you call a buffer operation, 605111be35a1SLionel Sambucthe stack is at the same level 605211be35a1SLionel Sambucit was immediately after the previous buffer operation. 605311be35a1SLionel Sambuc(The only exception to this rule is <a href="#luaL_addvalue"><code>luaL_addvalue</code></a>.) 605411be35a1SLionel SambucAfter calling <a href="#luaL_pushresult"><code>luaL_pushresult</code></a> the stack is back to its 605511be35a1SLionel Sambuclevel when the buffer was initialized, 605611be35a1SLionel Sambucplus the final string on its top. 605711be35a1SLionel Sambuc 605811be35a1SLionel Sambuc 605911be35a1SLionel Sambuc 606011be35a1SLionel Sambuc 606111be35a1SLionel Sambuc 606211be35a1SLionel Sambuc<hr><h3><a name="luaL_buffinit"><code>luaL_buffinit</code></a></h3><p> 6063*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 606411be35a1SLionel Sambuc<pre>void luaL_buffinit (lua_State *L, luaL_Buffer *B);</pre> 606511be35a1SLionel Sambuc 606611be35a1SLionel Sambuc<p> 606711be35a1SLionel SambucInitializes a buffer <code>B</code>. 606811be35a1SLionel SambucThis function does not allocate any space; 606911be35a1SLionel Sambucthe buffer must be declared as a variable 607011be35a1SLionel Sambuc(see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>). 607111be35a1SLionel Sambuc 607211be35a1SLionel Sambuc 607311be35a1SLionel Sambuc 607411be35a1SLionel Sambuc 607511be35a1SLionel Sambuc 6076*0a6a1f1dSLionel Sambuc<hr><h3><a name="luaL_buffinitsize"><code>luaL_buffinitsize</code></a></h3><p> 6077*0a6a1f1dSLionel Sambuc<span class="apii">[-?, +?, <em>e</em>]</span> 6078*0a6a1f1dSLionel Sambuc<pre>char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz);</pre> 6079*0a6a1f1dSLionel Sambuc 6080*0a6a1f1dSLionel Sambuc<p> 6081*0a6a1f1dSLionel SambucEquivalent to the sequence 6082*0a6a1f1dSLionel Sambuc<a href="#luaL_buffinit"><code>luaL_buffinit</code></a>, <a href="#luaL_prepbuffsize"><code>luaL_prepbuffsize</code></a>. 6083*0a6a1f1dSLionel Sambuc 6084*0a6a1f1dSLionel Sambuc 6085*0a6a1f1dSLionel Sambuc 6086*0a6a1f1dSLionel Sambuc 6087*0a6a1f1dSLionel Sambuc 608811be35a1SLionel Sambuc<hr><h3><a name="luaL_callmeta"><code>luaL_callmeta</code></a></h3><p> 608911be35a1SLionel Sambuc<span class="apii">[-0, +(0|1), <em>e</em>]</span> 609011be35a1SLionel Sambuc<pre>int luaL_callmeta (lua_State *L, int obj, const char *e);</pre> 609111be35a1SLionel Sambuc 609211be35a1SLionel Sambuc<p> 609311be35a1SLionel SambucCalls a metamethod. 609411be35a1SLionel Sambuc 609511be35a1SLionel Sambuc 609611be35a1SLionel Sambuc<p> 609711be35a1SLionel SambucIf the object at index <code>obj</code> has a metatable and this 609811be35a1SLionel Sambucmetatable has a field <code>e</code>, 6099*0a6a1f1dSLionel Sambucthis function calls this field passing the object as its only argument. 6100*0a6a1f1dSLionel SambucIn this case this function returns true and pushes onto the 610111be35a1SLionel Sambucstack the value returned by the call. 610211be35a1SLionel SambucIf there is no metatable or no metamethod, 6103*0a6a1f1dSLionel Sambucthis function returns false (without pushing any value on the stack). 610411be35a1SLionel Sambuc 610511be35a1SLionel Sambuc 610611be35a1SLionel Sambuc 610711be35a1SLionel Sambuc 610811be35a1SLionel Sambuc 610911be35a1SLionel Sambuc<hr><h3><a name="luaL_checkany"><code>luaL_checkany</code></a></h3><p> 611011be35a1SLionel Sambuc<span class="apii">[-0, +0, <em>v</em>]</span> 6111*0a6a1f1dSLionel Sambuc<pre>void luaL_checkany (lua_State *L, int arg);</pre> 611211be35a1SLionel Sambuc 611311be35a1SLionel Sambuc<p> 611411be35a1SLionel SambucChecks whether the function has an argument 6115*0a6a1f1dSLionel Sambucof any type (including <b>nil</b>) at position <code>arg</code>. 611611be35a1SLionel Sambuc 611711be35a1SLionel Sambuc 611811be35a1SLionel Sambuc 611911be35a1SLionel Sambuc 612011be35a1SLionel Sambuc 612111be35a1SLionel Sambuc<hr><h3><a name="luaL_checkinteger"><code>luaL_checkinteger</code></a></h3><p> 612211be35a1SLionel Sambuc<span class="apii">[-0, +0, <em>v</em>]</span> 6123*0a6a1f1dSLionel Sambuc<pre>lua_Integer luaL_checkinteger (lua_State *L, int arg);</pre> 612411be35a1SLionel Sambuc 612511be35a1SLionel Sambuc<p> 6126*0a6a1f1dSLionel SambucChecks whether the function argument <code>arg</code> is an integer 6127*0a6a1f1dSLionel Sambuc(or can be converted to an integer) 6128*0a6a1f1dSLionel Sambucand returns this integer cast to a <a href="#lua_Integer"><code>lua_Integer</code></a>. 612911be35a1SLionel Sambuc 613011be35a1SLionel Sambuc 613111be35a1SLionel Sambuc 613211be35a1SLionel Sambuc 613311be35a1SLionel Sambuc 613411be35a1SLionel Sambuc<hr><h3><a name="luaL_checklstring"><code>luaL_checklstring</code></a></h3><p> 613511be35a1SLionel Sambuc<span class="apii">[-0, +0, <em>v</em>]</span> 6136*0a6a1f1dSLionel Sambuc<pre>const char *luaL_checklstring (lua_State *L, int arg, size_t *l);</pre> 613711be35a1SLionel Sambuc 613811be35a1SLionel Sambuc<p> 6139*0a6a1f1dSLionel SambucChecks whether the function argument <code>arg</code> is a string 614011be35a1SLionel Sambucand returns this string; 614111be35a1SLionel Sambucif <code>l</code> is not <code>NULL</code> fills <code>*l</code> 614211be35a1SLionel Sambucwith the string's length. 614311be35a1SLionel Sambuc 614411be35a1SLionel Sambuc 614511be35a1SLionel Sambuc<p> 614611be35a1SLionel SambucThis function uses <a href="#lua_tolstring"><code>lua_tolstring</code></a> to get its result, 614711be35a1SLionel Sambucso all conversions and caveats of that function apply here. 614811be35a1SLionel Sambuc 614911be35a1SLionel Sambuc 615011be35a1SLionel Sambuc 615111be35a1SLionel Sambuc 615211be35a1SLionel Sambuc 615311be35a1SLionel Sambuc<hr><h3><a name="luaL_checknumber"><code>luaL_checknumber</code></a></h3><p> 615411be35a1SLionel Sambuc<span class="apii">[-0, +0, <em>v</em>]</span> 6155*0a6a1f1dSLionel Sambuc<pre>lua_Number luaL_checknumber (lua_State *L, int arg);</pre> 615611be35a1SLionel Sambuc 615711be35a1SLionel Sambuc<p> 6158*0a6a1f1dSLionel SambucChecks whether the function argument <code>arg</code> is a number 615911be35a1SLionel Sambucand returns this number. 616011be35a1SLionel Sambuc 616111be35a1SLionel Sambuc 616211be35a1SLionel Sambuc 616311be35a1SLionel Sambuc 616411be35a1SLionel Sambuc 616511be35a1SLionel Sambuc<hr><h3><a name="luaL_checkoption"><code>luaL_checkoption</code></a></h3><p> 616611be35a1SLionel Sambuc<span class="apii">[-0, +0, <em>v</em>]</span> 616711be35a1SLionel Sambuc<pre>int luaL_checkoption (lua_State *L, 6168*0a6a1f1dSLionel Sambuc int arg, 616911be35a1SLionel Sambuc const char *def, 617011be35a1SLionel Sambuc const char *const lst[]);</pre> 617111be35a1SLionel Sambuc 617211be35a1SLionel Sambuc<p> 6173*0a6a1f1dSLionel SambucChecks whether the function argument <code>arg</code> is a string and 617411be35a1SLionel Sambucsearches for this string in the array <code>lst</code> 617511be35a1SLionel Sambuc(which must be NULL-terminated). 617611be35a1SLionel SambucReturns the index in the array where the string was found. 617711be35a1SLionel SambucRaises an error if the argument is not a string or 617811be35a1SLionel Sambucif the string cannot be found. 617911be35a1SLionel Sambuc 618011be35a1SLionel Sambuc 618111be35a1SLionel Sambuc<p> 618211be35a1SLionel SambucIf <code>def</code> is not <code>NULL</code>, 618311be35a1SLionel Sambucthe function uses <code>def</code> as a default value when 6184*0a6a1f1dSLionel Sambucthere is no argument <code>arg</code> or when this argument is <b>nil</b>. 618511be35a1SLionel Sambuc 618611be35a1SLionel Sambuc 618711be35a1SLionel Sambuc<p> 618811be35a1SLionel SambucThis is a useful function for mapping strings to C enums. 618911be35a1SLionel Sambuc(The usual convention in Lua libraries is 619011be35a1SLionel Sambucto use strings instead of numbers to select options.) 619111be35a1SLionel Sambuc 619211be35a1SLionel Sambuc 619311be35a1SLionel Sambuc 619411be35a1SLionel Sambuc 619511be35a1SLionel Sambuc 619611be35a1SLionel Sambuc<hr><h3><a name="luaL_checkstack"><code>luaL_checkstack</code></a></h3><p> 619711be35a1SLionel Sambuc<span class="apii">[-0, +0, <em>v</em>]</span> 619811be35a1SLionel Sambuc<pre>void luaL_checkstack (lua_State *L, int sz, const char *msg);</pre> 619911be35a1SLionel Sambuc 620011be35a1SLionel Sambuc<p> 620111be35a1SLionel SambucGrows the stack size to <code>top + sz</code> elements, 620211be35a1SLionel Sambucraising an error if the stack cannot grow to that size. 6203*0a6a1f1dSLionel Sambuc<code>msg</code> is an additional text to go into the error message 6204*0a6a1f1dSLionel Sambuc(or <code>NULL</code> for no additional text). 620511be35a1SLionel Sambuc 620611be35a1SLionel Sambuc 620711be35a1SLionel Sambuc 620811be35a1SLionel Sambuc 620911be35a1SLionel Sambuc 621011be35a1SLionel Sambuc<hr><h3><a name="luaL_checkstring"><code>luaL_checkstring</code></a></h3><p> 621111be35a1SLionel Sambuc<span class="apii">[-0, +0, <em>v</em>]</span> 6212*0a6a1f1dSLionel Sambuc<pre>const char *luaL_checkstring (lua_State *L, int arg);</pre> 621311be35a1SLionel Sambuc 621411be35a1SLionel Sambuc<p> 6215*0a6a1f1dSLionel SambucChecks whether the function argument <code>arg</code> is a string 621611be35a1SLionel Sambucand returns this string. 621711be35a1SLionel Sambuc 621811be35a1SLionel Sambuc 621911be35a1SLionel Sambuc<p> 622011be35a1SLionel SambucThis function uses <a href="#lua_tolstring"><code>lua_tolstring</code></a> to get its result, 622111be35a1SLionel Sambucso all conversions and caveats of that function apply here. 622211be35a1SLionel Sambuc 622311be35a1SLionel Sambuc 622411be35a1SLionel Sambuc 622511be35a1SLionel Sambuc 622611be35a1SLionel Sambuc 622711be35a1SLionel Sambuc<hr><h3><a name="luaL_checktype"><code>luaL_checktype</code></a></h3><p> 622811be35a1SLionel Sambuc<span class="apii">[-0, +0, <em>v</em>]</span> 6229*0a6a1f1dSLionel Sambuc<pre>void luaL_checktype (lua_State *L, int arg, int t);</pre> 623011be35a1SLionel Sambuc 623111be35a1SLionel Sambuc<p> 6232*0a6a1f1dSLionel SambucChecks whether the function argument <code>arg</code> has type <code>t</code>. 623311be35a1SLionel SambucSee <a href="#lua_type"><code>lua_type</code></a> for the encoding of types for <code>t</code>. 623411be35a1SLionel Sambuc 623511be35a1SLionel Sambuc 623611be35a1SLionel Sambuc 623711be35a1SLionel Sambuc 623811be35a1SLionel Sambuc 623911be35a1SLionel Sambuc<hr><h3><a name="luaL_checkudata"><code>luaL_checkudata</code></a></h3><p> 624011be35a1SLionel Sambuc<span class="apii">[-0, +0, <em>v</em>]</span> 6241*0a6a1f1dSLionel Sambuc<pre>void *luaL_checkudata (lua_State *L, int arg, const char *tname);</pre> 624211be35a1SLionel Sambuc 624311be35a1SLionel Sambuc<p> 6244*0a6a1f1dSLionel SambucChecks whether the function argument <code>arg</code> is a userdata 6245*0a6a1f1dSLionel Sambucof the type <code>tname</code> (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>) and 6246*0a6a1f1dSLionel Sambucreturns the userdata address (see <a href="#lua_touserdata"><code>lua_touserdata</code></a>). 6247*0a6a1f1dSLionel Sambuc 6248*0a6a1f1dSLionel Sambuc 6249*0a6a1f1dSLionel Sambuc 6250*0a6a1f1dSLionel Sambuc 6251*0a6a1f1dSLionel Sambuc 6252*0a6a1f1dSLionel Sambuc<hr><h3><a name="luaL_checkversion"><code>luaL_checkversion</code></a></h3><p> 6253*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 6254*0a6a1f1dSLionel Sambuc<pre>void luaL_checkversion (lua_State *L);</pre> 6255*0a6a1f1dSLionel Sambuc 6256*0a6a1f1dSLionel Sambuc<p> 6257*0a6a1f1dSLionel SambucChecks whether the core running the call, 6258*0a6a1f1dSLionel Sambucthe core that created the Lua state, 6259*0a6a1f1dSLionel Sambucand the code making the call are all using the same version of Lua. 6260*0a6a1f1dSLionel SambucAlso checks whether the core running the call 6261*0a6a1f1dSLionel Sambucand the core that created the Lua state 6262*0a6a1f1dSLionel Sambucare using the same address space. 626311be35a1SLionel Sambuc 626411be35a1SLionel Sambuc 626511be35a1SLionel Sambuc 626611be35a1SLionel Sambuc 626711be35a1SLionel Sambuc 626811be35a1SLionel Sambuc<hr><h3><a name="luaL_dofile"><code>luaL_dofile</code></a></h3><p> 6269*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +?, <em>e</em>]</span> 627011be35a1SLionel Sambuc<pre>int luaL_dofile (lua_State *L, const char *filename);</pre> 627111be35a1SLionel Sambuc 627211be35a1SLionel Sambuc<p> 627311be35a1SLionel SambucLoads and runs the given file. 627411be35a1SLionel SambucIt is defined as the following macro: 627511be35a1SLionel Sambuc 627611be35a1SLionel Sambuc<pre> 627711be35a1SLionel Sambuc (luaL_loadfile(L, filename) || lua_pcall(L, 0, LUA_MULTRET, 0)) 627811be35a1SLionel Sambuc</pre><p> 6279*0a6a1f1dSLionel SambucIt returns false if there are no errors 6280*0a6a1f1dSLionel Sambucor true in case of errors. 628111be35a1SLionel Sambuc 628211be35a1SLionel Sambuc 628311be35a1SLionel Sambuc 628411be35a1SLionel Sambuc 628511be35a1SLionel Sambuc 628611be35a1SLionel Sambuc<hr><h3><a name="luaL_dostring"><code>luaL_dostring</code></a></h3><p> 6287*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +?, –]</span> 628811be35a1SLionel Sambuc<pre>int luaL_dostring (lua_State *L, const char *str);</pre> 628911be35a1SLionel Sambuc 629011be35a1SLionel Sambuc<p> 629111be35a1SLionel SambucLoads and runs the given string. 629211be35a1SLionel SambucIt is defined as the following macro: 629311be35a1SLionel Sambuc 629411be35a1SLionel Sambuc<pre> 629511be35a1SLionel Sambuc (luaL_loadstring(L, str) || lua_pcall(L, 0, LUA_MULTRET, 0)) 629611be35a1SLionel Sambuc</pre><p> 6297*0a6a1f1dSLionel SambucIt returns false if there are no errors 6298*0a6a1f1dSLionel Sambucor true in case of errors. 629911be35a1SLionel Sambuc 630011be35a1SLionel Sambuc 630111be35a1SLionel Sambuc 630211be35a1SLionel Sambuc 630311be35a1SLionel Sambuc 630411be35a1SLionel Sambuc<hr><h3><a name="luaL_error"><code>luaL_error</code></a></h3><p> 630511be35a1SLionel Sambuc<span class="apii">[-0, +0, <em>v</em>]</span> 630611be35a1SLionel Sambuc<pre>int luaL_error (lua_State *L, const char *fmt, ...);</pre> 630711be35a1SLionel Sambuc 630811be35a1SLionel Sambuc<p> 630911be35a1SLionel SambucRaises an error. 631011be35a1SLionel SambucThe error message format is given by <code>fmt</code> 631111be35a1SLionel Sambucplus any extra arguments, 631211be35a1SLionel Sambucfollowing the same rules of <a href="#lua_pushfstring"><code>lua_pushfstring</code></a>. 631311be35a1SLionel SambucIt also adds at the beginning of the message the file name and 631411be35a1SLionel Sambucthe line number where the error occurred, 631511be35a1SLionel Sambucif this information is available. 631611be35a1SLionel Sambuc 631711be35a1SLionel Sambuc 631811be35a1SLionel Sambuc<p> 631911be35a1SLionel SambucThis function never returns, 632011be35a1SLionel Sambucbut it is an idiom to use it in C functions 632111be35a1SLionel Sambucas <code>return luaL_error(<em>args</em>)</code>. 632211be35a1SLionel Sambuc 632311be35a1SLionel Sambuc 632411be35a1SLionel Sambuc 632511be35a1SLionel Sambuc 632611be35a1SLionel Sambuc 6327*0a6a1f1dSLionel Sambuc<hr><h3><a name="luaL_execresult"><code>luaL_execresult</code></a></h3><p> 6328*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +3, <em>e</em>]</span> 6329*0a6a1f1dSLionel Sambuc<pre>int luaL_execresult (lua_State *L, int stat);</pre> 6330*0a6a1f1dSLionel Sambuc 6331*0a6a1f1dSLionel Sambuc<p> 6332*0a6a1f1dSLionel SambucThis function produces the return values for 6333*0a6a1f1dSLionel Sambucprocess-related functions in the standard library 6334*0a6a1f1dSLionel Sambuc(<a href="#pdf-os.execute"><code>os.execute</code></a> and <a href="#pdf-io.close"><code>io.close</code></a>). 6335*0a6a1f1dSLionel Sambuc 6336*0a6a1f1dSLionel Sambuc 6337*0a6a1f1dSLionel Sambuc 6338*0a6a1f1dSLionel Sambuc 6339*0a6a1f1dSLionel Sambuc 6340*0a6a1f1dSLionel Sambuc<hr><h3><a name="luaL_fileresult"><code>luaL_fileresult</code></a></h3><p> 6341*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +(1|3), <em>e</em>]</span> 6342*0a6a1f1dSLionel Sambuc<pre>int luaL_fileresult (lua_State *L, int stat, const char *fname);</pre> 6343*0a6a1f1dSLionel Sambuc 6344*0a6a1f1dSLionel Sambuc<p> 6345*0a6a1f1dSLionel SambucThis function produces the return values for 6346*0a6a1f1dSLionel Sambucfile-related functions in the standard library 6347*0a6a1f1dSLionel Sambuc(<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.). 6348*0a6a1f1dSLionel Sambuc 6349*0a6a1f1dSLionel Sambuc 6350*0a6a1f1dSLionel Sambuc 6351*0a6a1f1dSLionel Sambuc 6352*0a6a1f1dSLionel Sambuc 635311be35a1SLionel Sambuc<hr><h3><a name="luaL_getmetafield"><code>luaL_getmetafield</code></a></h3><p> 6354*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +(0|1), <em>e</em>]</span> 635511be35a1SLionel Sambuc<pre>int luaL_getmetafield (lua_State *L, int obj, const char *e);</pre> 635611be35a1SLionel Sambuc 635711be35a1SLionel Sambuc<p> 635811be35a1SLionel SambucPushes onto the stack the field <code>e</code> from the metatable 6359*0a6a1f1dSLionel Sambucof the object at index <code>obj</code> and returns the type of pushed value. 636011be35a1SLionel SambucIf the object does not have a metatable, 636111be35a1SLionel Sambucor if the metatable does not have this field, 6362*0a6a1f1dSLionel Sambucpushes nothing and returns <code>LUA_TNIL</code>. 636311be35a1SLionel Sambuc 636411be35a1SLionel Sambuc 636511be35a1SLionel Sambuc 636611be35a1SLionel Sambuc 636711be35a1SLionel Sambuc 636811be35a1SLionel Sambuc<hr><h3><a name="luaL_getmetatable"><code>luaL_getmetatable</code></a></h3><p> 6369*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, –]</span> 6370*0a6a1f1dSLionel Sambuc<pre>int luaL_getmetatable (lua_State *L, const char *tname);</pre> 637111be35a1SLionel Sambuc 637211be35a1SLionel Sambuc<p> 637311be35a1SLionel SambucPushes onto the stack the metatable associated with name <code>tname</code> 6374*0a6a1f1dSLionel Sambucin the registry (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>) 6375*0a6a1f1dSLionel Sambuc(<b>nil</b> if there is no metatable associated with that name). 6376*0a6a1f1dSLionel SambucReturns the type of the pushed value. 6377*0a6a1f1dSLionel Sambuc 6378*0a6a1f1dSLionel Sambuc 6379*0a6a1f1dSLionel Sambuc 6380*0a6a1f1dSLionel Sambuc 6381*0a6a1f1dSLionel Sambuc 6382*0a6a1f1dSLionel Sambuc<hr><h3><a name="luaL_getsubtable"><code>luaL_getsubtable</code></a></h3><p> 6383*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, <em>e</em>]</span> 6384*0a6a1f1dSLionel Sambuc<pre>int luaL_getsubtable (lua_State *L, int idx, const char *fname);</pre> 6385*0a6a1f1dSLionel Sambuc 6386*0a6a1f1dSLionel Sambuc<p> 6387*0a6a1f1dSLionel SambucEnsures that the value <code>t[fname]</code>, 6388*0a6a1f1dSLionel Sambucwhere <code>t</code> is the value at index <code>idx</code>, 6389*0a6a1f1dSLionel Sambucis a table, 6390*0a6a1f1dSLionel Sambucand pushes that table onto the stack. 6391*0a6a1f1dSLionel SambucReturns true if it finds a previous table there 6392*0a6a1f1dSLionel Sambucand false if it creates a new table. 639311be35a1SLionel Sambuc 639411be35a1SLionel Sambuc 639511be35a1SLionel Sambuc 639611be35a1SLionel Sambuc 639711be35a1SLionel Sambuc 639811be35a1SLionel Sambuc<hr><h3><a name="luaL_gsub"><code>luaL_gsub</code></a></h3><p> 6399*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, <em>e</em>]</span> 640011be35a1SLionel Sambuc<pre>const char *luaL_gsub (lua_State *L, 640111be35a1SLionel Sambuc const char *s, 640211be35a1SLionel Sambuc const char *p, 640311be35a1SLionel Sambuc const char *r);</pre> 640411be35a1SLionel Sambuc 640511be35a1SLionel Sambuc<p> 640611be35a1SLionel SambucCreates a copy of string <code>s</code> by replacing 640711be35a1SLionel Sambucany occurrence of the string <code>p</code> 640811be35a1SLionel Sambucwith the string <code>r</code>. 640911be35a1SLionel SambucPushes the resulting string on the stack and returns it. 641011be35a1SLionel Sambuc 641111be35a1SLionel Sambuc 641211be35a1SLionel Sambuc 641311be35a1SLionel Sambuc 641411be35a1SLionel Sambuc 6415*0a6a1f1dSLionel Sambuc<hr><h3><a name="luaL_len"><code>luaL_len</code></a></h3><p> 6416*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, <em>e</em>]</span> 6417*0a6a1f1dSLionel Sambuc<pre>lua_Integer luaL_len (lua_State *L, int index);</pre> 6418*0a6a1f1dSLionel Sambuc 6419*0a6a1f1dSLionel Sambuc<p> 6420*0a6a1f1dSLionel SambucReturns the "length" of the value at the given index 6421*0a6a1f1dSLionel Sambucas a number; 6422*0a6a1f1dSLionel Sambucit is equivalent to the '<code>#</code>' operator in Lua (see <a href="#3.4.7">§3.4.7</a>). 6423*0a6a1f1dSLionel SambucRaises an error if the result of the operation is not an integer. 6424*0a6a1f1dSLionel Sambuc(This case only can happen through metamethods.) 6425*0a6a1f1dSLionel Sambuc 6426*0a6a1f1dSLionel Sambuc 6427*0a6a1f1dSLionel Sambuc 6428*0a6a1f1dSLionel Sambuc 6429*0a6a1f1dSLionel Sambuc 643011be35a1SLionel Sambuc<hr><h3><a name="luaL_loadbuffer"><code>luaL_loadbuffer</code></a></h3><p> 6431*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, –]</span> 643211be35a1SLionel Sambuc<pre>int luaL_loadbuffer (lua_State *L, 643311be35a1SLionel Sambuc const char *buff, 643411be35a1SLionel Sambuc size_t sz, 643511be35a1SLionel Sambuc const char *name);</pre> 643611be35a1SLionel Sambuc 643711be35a1SLionel Sambuc<p> 6438*0a6a1f1dSLionel SambucEquivalent to <a href="#luaL_loadbufferx"><code>luaL_loadbufferx</code></a> with <code>mode</code> equal to <code>NULL</code>. 6439*0a6a1f1dSLionel Sambuc 6440*0a6a1f1dSLionel Sambuc 6441*0a6a1f1dSLionel Sambuc 6442*0a6a1f1dSLionel Sambuc 6443*0a6a1f1dSLionel Sambuc 6444*0a6a1f1dSLionel Sambuc<hr><h3><a name="luaL_loadbufferx"><code>luaL_loadbufferx</code></a></h3><p> 6445*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, –]</span> 6446*0a6a1f1dSLionel Sambuc<pre>int luaL_loadbufferx (lua_State *L, 6447*0a6a1f1dSLionel Sambuc const char *buff, 6448*0a6a1f1dSLionel Sambuc size_t sz, 6449*0a6a1f1dSLionel Sambuc const char *name, 6450*0a6a1f1dSLionel Sambuc const char *mode);</pre> 6451*0a6a1f1dSLionel Sambuc 6452*0a6a1f1dSLionel Sambuc<p> 645311be35a1SLionel SambucLoads a buffer as a Lua chunk. 645411be35a1SLionel SambucThis function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in the 645511be35a1SLionel Sambucbuffer pointed to by <code>buff</code> with size <code>sz</code>. 645611be35a1SLionel Sambuc 645711be35a1SLionel Sambuc 645811be35a1SLionel Sambuc<p> 645911be35a1SLionel SambucThis function returns the same results as <a href="#lua_load"><code>lua_load</code></a>. 646011be35a1SLionel Sambuc<code>name</code> is the chunk name, 646111be35a1SLionel Sambucused for debug information and error messages. 6462*0a6a1f1dSLionel SambucThe string <code>mode</code> works as in function <a href="#lua_load"><code>lua_load</code></a>. 646311be35a1SLionel Sambuc 646411be35a1SLionel Sambuc 646511be35a1SLionel Sambuc 646611be35a1SLionel Sambuc 646711be35a1SLionel Sambuc 646811be35a1SLionel Sambuc<hr><h3><a name="luaL_loadfile"><code>luaL_loadfile</code></a></h3><p> 6469*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, <em>e</em>]</span> 647011be35a1SLionel Sambuc<pre>int luaL_loadfile (lua_State *L, const char *filename);</pre> 647111be35a1SLionel Sambuc 647211be35a1SLionel Sambuc<p> 6473*0a6a1f1dSLionel SambucEquivalent to <a href="#luaL_loadfilex"><code>luaL_loadfilex</code></a> with <code>mode</code> equal to <code>NULL</code>. 6474*0a6a1f1dSLionel Sambuc 6475*0a6a1f1dSLionel Sambuc 6476*0a6a1f1dSLionel Sambuc 6477*0a6a1f1dSLionel Sambuc 6478*0a6a1f1dSLionel Sambuc 6479*0a6a1f1dSLionel Sambuc<hr><h3><a name="luaL_loadfilex"><code>luaL_loadfilex</code></a></h3><p> 6480*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, <em>e</em>]</span> 6481*0a6a1f1dSLionel Sambuc<pre>int luaL_loadfilex (lua_State *L, const char *filename, 6482*0a6a1f1dSLionel Sambuc const char *mode);</pre> 6483*0a6a1f1dSLionel Sambuc 6484*0a6a1f1dSLionel Sambuc<p> 648511be35a1SLionel SambucLoads a file as a Lua chunk. 648611be35a1SLionel SambucThis function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in the file 648711be35a1SLionel Sambucnamed <code>filename</code>. 648811be35a1SLionel SambucIf <code>filename</code> is <code>NULL</code>, 648911be35a1SLionel Sambucthen it loads from the standard input. 649011be35a1SLionel SambucThe first line in the file is ignored if it starts with a <code>#</code>. 649111be35a1SLionel Sambuc 649211be35a1SLionel Sambuc 649311be35a1SLionel Sambuc<p> 6494*0a6a1f1dSLionel SambucThe string <code>mode</code> works as in function <a href="#lua_load"><code>lua_load</code></a>. 6495*0a6a1f1dSLionel Sambuc 6496*0a6a1f1dSLionel Sambuc 6497*0a6a1f1dSLionel Sambuc<p> 649811be35a1SLionel SambucThis function returns the same results as <a href="#lua_load"><code>lua_load</code></a>, 649911be35a1SLionel Sambucbut it has an extra error code <a name="pdf-LUA_ERRFILE"><code>LUA_ERRFILE</code></a> 6500*0a6a1f1dSLionel Sambucif it cannot open/read the file or the file has a wrong mode. 650111be35a1SLionel Sambuc 650211be35a1SLionel Sambuc 650311be35a1SLionel Sambuc<p> 650411be35a1SLionel SambucAs <a href="#lua_load"><code>lua_load</code></a>, this function only loads the chunk; 650511be35a1SLionel Sambucit does not run it. 650611be35a1SLionel Sambuc 650711be35a1SLionel Sambuc 650811be35a1SLionel Sambuc 650911be35a1SLionel Sambuc 651011be35a1SLionel Sambuc 651111be35a1SLionel Sambuc<hr><h3><a name="luaL_loadstring"><code>luaL_loadstring</code></a></h3><p> 6512*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, –]</span> 651311be35a1SLionel Sambuc<pre>int luaL_loadstring (lua_State *L, const char *s);</pre> 651411be35a1SLionel Sambuc 651511be35a1SLionel Sambuc<p> 651611be35a1SLionel SambucLoads a string as a Lua chunk. 651711be35a1SLionel SambucThis function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in 651811be35a1SLionel Sambucthe zero-terminated string <code>s</code>. 651911be35a1SLionel Sambuc 652011be35a1SLionel Sambuc 652111be35a1SLionel Sambuc<p> 652211be35a1SLionel SambucThis function returns the same results as <a href="#lua_load"><code>lua_load</code></a>. 652311be35a1SLionel Sambuc 652411be35a1SLionel Sambuc 652511be35a1SLionel Sambuc<p> 652611be35a1SLionel SambucAlso as <a href="#lua_load"><code>lua_load</code></a>, this function only loads the chunk; 652711be35a1SLionel Sambucit does not run it. 652811be35a1SLionel Sambuc 652911be35a1SLionel Sambuc 653011be35a1SLionel Sambuc 653111be35a1SLionel Sambuc 653211be35a1SLionel Sambuc 6533*0a6a1f1dSLionel Sambuc<hr><h3><a name="luaL_newlib"><code>luaL_newlib</code></a></h3><p> 6534*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, <em>e</em>]</span> 6535*0a6a1f1dSLionel Sambuc<pre>void luaL_newlib (lua_State *L, const luaL_Reg l[]);</pre> 6536*0a6a1f1dSLionel Sambuc 6537*0a6a1f1dSLionel Sambuc<p> 6538*0a6a1f1dSLionel SambucCreates a new table and registers there 6539*0a6a1f1dSLionel Sambucthe functions in list <code>l</code>. 6540*0a6a1f1dSLionel Sambuc 6541*0a6a1f1dSLionel Sambuc 6542*0a6a1f1dSLionel Sambuc<p> 6543*0a6a1f1dSLionel SambucIt is implemented as the following macro: 6544*0a6a1f1dSLionel Sambuc 6545*0a6a1f1dSLionel Sambuc<pre> 6546*0a6a1f1dSLionel Sambuc (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) 6547*0a6a1f1dSLionel Sambuc</pre><p> 6548*0a6a1f1dSLionel SambucThe array <code>l</code> must be the actual array, 6549*0a6a1f1dSLionel Sambucnot a pointer to it. 6550*0a6a1f1dSLionel Sambuc 6551*0a6a1f1dSLionel Sambuc 6552*0a6a1f1dSLionel Sambuc 6553*0a6a1f1dSLionel Sambuc 6554*0a6a1f1dSLionel Sambuc 6555*0a6a1f1dSLionel Sambuc<hr><h3><a name="luaL_newlibtable"><code>luaL_newlibtable</code></a></h3><p> 6556*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, <em>e</em>]</span> 6557*0a6a1f1dSLionel Sambuc<pre>void luaL_newlibtable (lua_State *L, const luaL_Reg l[]);</pre> 6558*0a6a1f1dSLionel Sambuc 6559*0a6a1f1dSLionel Sambuc<p> 6560*0a6a1f1dSLionel SambucCreates a new table with a size optimized 6561*0a6a1f1dSLionel Sambucto store all entries in the array <code>l</code> 6562*0a6a1f1dSLionel Sambuc(but does not actually store them). 6563*0a6a1f1dSLionel SambucIt is intended to be used in conjunction with <a href="#luaL_setfuncs"><code>luaL_setfuncs</code></a> 6564*0a6a1f1dSLionel Sambuc(see <a href="#luaL_newlib"><code>luaL_newlib</code></a>). 6565*0a6a1f1dSLionel Sambuc 6566*0a6a1f1dSLionel Sambuc 6567*0a6a1f1dSLionel Sambuc<p> 6568*0a6a1f1dSLionel SambucIt is implemented as a macro. 6569*0a6a1f1dSLionel SambucThe array <code>l</code> must be the actual array, 6570*0a6a1f1dSLionel Sambucnot a pointer to it. 6571*0a6a1f1dSLionel Sambuc 6572*0a6a1f1dSLionel Sambuc 6573*0a6a1f1dSLionel Sambuc 6574*0a6a1f1dSLionel Sambuc 6575*0a6a1f1dSLionel Sambuc 657611be35a1SLionel Sambuc<hr><h3><a name="luaL_newmetatable"><code>luaL_newmetatable</code></a></h3><p> 6577*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, <em>e</em>]</span> 657811be35a1SLionel Sambuc<pre>int luaL_newmetatable (lua_State *L, const char *tname);</pre> 657911be35a1SLionel Sambuc 658011be35a1SLionel Sambuc<p> 658111be35a1SLionel SambucIf the registry already has the key <code>tname</code>, 658211be35a1SLionel Sambucreturns 0. 658311be35a1SLionel SambucOtherwise, 658411be35a1SLionel Sambuccreates a new table to be used as a metatable for userdata, 6585*0a6a1f1dSLionel Sambucadds to this new table the pair <code>__name = tname</code>, 6586*0a6a1f1dSLionel Sambucadds to the registry the pair <code>[tname] = new table</code>, 658711be35a1SLionel Sambucand returns 1. 6588*0a6a1f1dSLionel Sambuc(The entry <code>__name</code> is used by some error-reporting functions.) 658911be35a1SLionel Sambuc 659011be35a1SLionel Sambuc 659111be35a1SLionel Sambuc<p> 659211be35a1SLionel SambucIn both cases pushes onto the stack the final value associated 659311be35a1SLionel Sambucwith <code>tname</code> in the registry. 659411be35a1SLionel Sambuc 659511be35a1SLionel Sambuc 659611be35a1SLionel Sambuc 659711be35a1SLionel Sambuc 659811be35a1SLionel Sambuc 659911be35a1SLionel Sambuc<hr><h3><a name="luaL_newstate"><code>luaL_newstate</code></a></h3><p> 6600*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 660111be35a1SLionel Sambuc<pre>lua_State *luaL_newstate (void);</pre> 660211be35a1SLionel Sambuc 660311be35a1SLionel Sambuc<p> 660411be35a1SLionel SambucCreates a new Lua state. 660511be35a1SLionel SambucIt calls <a href="#lua_newstate"><code>lua_newstate</code></a> with an 660611be35a1SLionel Sambucallocator based on the standard C <code>realloc</code> function 6607*0a6a1f1dSLionel Sambucand then sets a panic function (see <a href="#4.6">§4.6</a>) that prints 660811be35a1SLionel Sambucan error message to the standard error output in case of fatal 660911be35a1SLionel Sambucerrors. 661011be35a1SLionel Sambuc 661111be35a1SLionel Sambuc 661211be35a1SLionel Sambuc<p> 661311be35a1SLionel SambucReturns the new state, 661411be35a1SLionel Sambucor <code>NULL</code> if there is a memory allocation error. 661511be35a1SLionel Sambuc 661611be35a1SLionel Sambuc 661711be35a1SLionel Sambuc 661811be35a1SLionel Sambuc 661911be35a1SLionel Sambuc 662011be35a1SLionel Sambuc<hr><h3><a name="luaL_openlibs"><code>luaL_openlibs</code></a></h3><p> 6621*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, <em>e</em>]</span> 662211be35a1SLionel Sambuc<pre>void luaL_openlibs (lua_State *L);</pre> 662311be35a1SLionel Sambuc 662411be35a1SLionel Sambuc<p> 662511be35a1SLionel SambucOpens all standard Lua libraries into the given state. 662611be35a1SLionel Sambuc 662711be35a1SLionel Sambuc 662811be35a1SLionel Sambuc 662911be35a1SLionel Sambuc 663011be35a1SLionel Sambuc 663111be35a1SLionel Sambuc<hr><h3><a name="luaL_optinteger"><code>luaL_optinteger</code></a></h3><p> 663211be35a1SLionel Sambuc<span class="apii">[-0, +0, <em>v</em>]</span> 663311be35a1SLionel Sambuc<pre>lua_Integer luaL_optinteger (lua_State *L, 6634*0a6a1f1dSLionel Sambuc int arg, 663511be35a1SLionel Sambuc lua_Integer d);</pre> 663611be35a1SLionel Sambuc 663711be35a1SLionel Sambuc<p> 6638*0a6a1f1dSLionel SambucIf the function argument <code>arg</code> is an integer 6639*0a6a1f1dSLionel Sambuc(or convertible to an integer), 6640*0a6a1f1dSLionel Sambucreturns this integer. 664111be35a1SLionel SambucIf this argument is absent or is <b>nil</b>, 664211be35a1SLionel Sambucreturns <code>d</code>. 664311be35a1SLionel SambucOtherwise, raises an error. 664411be35a1SLionel Sambuc 664511be35a1SLionel Sambuc 664611be35a1SLionel Sambuc 664711be35a1SLionel Sambuc 664811be35a1SLionel Sambuc 664911be35a1SLionel Sambuc<hr><h3><a name="luaL_optlstring"><code>luaL_optlstring</code></a></h3><p> 665011be35a1SLionel Sambuc<span class="apii">[-0, +0, <em>v</em>]</span> 665111be35a1SLionel Sambuc<pre>const char *luaL_optlstring (lua_State *L, 6652*0a6a1f1dSLionel Sambuc int arg, 665311be35a1SLionel Sambuc const char *d, 665411be35a1SLionel Sambuc size_t *l);</pre> 665511be35a1SLionel Sambuc 665611be35a1SLionel Sambuc<p> 6657*0a6a1f1dSLionel SambucIf the function argument <code>arg</code> is a string, 665811be35a1SLionel Sambucreturns this string. 665911be35a1SLionel SambucIf this argument is absent or is <b>nil</b>, 666011be35a1SLionel Sambucreturns <code>d</code>. 666111be35a1SLionel SambucOtherwise, raises an error. 666211be35a1SLionel Sambuc 666311be35a1SLionel Sambuc 666411be35a1SLionel Sambuc<p> 666511be35a1SLionel SambucIf <code>l</code> is not <code>NULL</code>, 6666*0a6a1f1dSLionel Sambucfills the position <code>*l</code> with the result's length. 666711be35a1SLionel Sambuc 666811be35a1SLionel Sambuc 666911be35a1SLionel Sambuc 667011be35a1SLionel Sambuc 667111be35a1SLionel Sambuc 667211be35a1SLionel Sambuc<hr><h3><a name="luaL_optnumber"><code>luaL_optnumber</code></a></h3><p> 667311be35a1SLionel Sambuc<span class="apii">[-0, +0, <em>v</em>]</span> 6674*0a6a1f1dSLionel Sambuc<pre>lua_Number luaL_optnumber (lua_State *L, int arg, lua_Number d);</pre> 667511be35a1SLionel Sambuc 667611be35a1SLionel Sambuc<p> 6677*0a6a1f1dSLionel SambucIf the function argument <code>arg</code> is a number, 667811be35a1SLionel Sambucreturns this number. 667911be35a1SLionel SambucIf this argument is absent or is <b>nil</b>, 668011be35a1SLionel Sambucreturns <code>d</code>. 668111be35a1SLionel SambucOtherwise, raises an error. 668211be35a1SLionel Sambuc 668311be35a1SLionel Sambuc 668411be35a1SLionel Sambuc 668511be35a1SLionel Sambuc 668611be35a1SLionel Sambuc 668711be35a1SLionel Sambuc<hr><h3><a name="luaL_optstring"><code>luaL_optstring</code></a></h3><p> 668811be35a1SLionel Sambuc<span class="apii">[-0, +0, <em>v</em>]</span> 668911be35a1SLionel Sambuc<pre>const char *luaL_optstring (lua_State *L, 6690*0a6a1f1dSLionel Sambuc int arg, 669111be35a1SLionel Sambuc const char *d);</pre> 669211be35a1SLionel Sambuc 669311be35a1SLionel Sambuc<p> 6694*0a6a1f1dSLionel SambucIf the function argument <code>arg</code> is a string, 669511be35a1SLionel Sambucreturns this string. 669611be35a1SLionel SambucIf this argument is absent or is <b>nil</b>, 669711be35a1SLionel Sambucreturns <code>d</code>. 669811be35a1SLionel SambucOtherwise, raises an error. 669911be35a1SLionel Sambuc 670011be35a1SLionel Sambuc 670111be35a1SLionel Sambuc 670211be35a1SLionel Sambuc 670311be35a1SLionel Sambuc 670411be35a1SLionel Sambuc<hr><h3><a name="luaL_prepbuffer"><code>luaL_prepbuffer</code></a></h3><p> 6705*0a6a1f1dSLionel Sambuc<span class="apii">[-?, +?, <em>e</em>]</span> 670611be35a1SLionel Sambuc<pre>char *luaL_prepbuffer (luaL_Buffer *B);</pre> 670711be35a1SLionel Sambuc 670811be35a1SLionel Sambuc<p> 6709*0a6a1f1dSLionel SambucEquivalent to <a href="#luaL_prepbuffsize"><code>luaL_prepbuffsize</code></a> 6710*0a6a1f1dSLionel Sambucwith the predefined size <a name="pdf-LUAL_BUFFERSIZE"><code>LUAL_BUFFERSIZE</code></a>. 6711*0a6a1f1dSLionel Sambuc 6712*0a6a1f1dSLionel Sambuc 6713*0a6a1f1dSLionel Sambuc 6714*0a6a1f1dSLionel Sambuc 6715*0a6a1f1dSLionel Sambuc 6716*0a6a1f1dSLionel Sambuc<hr><h3><a name="luaL_prepbuffsize"><code>luaL_prepbuffsize</code></a></h3><p> 6717*0a6a1f1dSLionel Sambuc<span class="apii">[-?, +?, <em>e</em>]</span> 6718*0a6a1f1dSLionel Sambuc<pre>char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz);</pre> 6719*0a6a1f1dSLionel Sambuc 6720*0a6a1f1dSLionel Sambuc<p> 6721*0a6a1f1dSLionel SambucReturns an address to a space of size <code>sz</code> 672211be35a1SLionel Sambucwhere you can copy a string to be added to buffer <code>B</code> 672311be35a1SLionel Sambuc(see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>). 672411be35a1SLionel SambucAfter copying the string into this space you must call 672511be35a1SLionel Sambuc<a href="#luaL_addsize"><code>luaL_addsize</code></a> with the size of the string to actually add 672611be35a1SLionel Sambucit to the buffer. 672711be35a1SLionel Sambuc 672811be35a1SLionel Sambuc 672911be35a1SLionel Sambuc 673011be35a1SLionel Sambuc 673111be35a1SLionel Sambuc 673211be35a1SLionel Sambuc<hr><h3><a name="luaL_pushresult"><code>luaL_pushresult</code></a></h3><p> 6733*0a6a1f1dSLionel Sambuc<span class="apii">[-?, +1, <em>e</em>]</span> 673411be35a1SLionel Sambuc<pre>void luaL_pushresult (luaL_Buffer *B);</pre> 673511be35a1SLionel Sambuc 673611be35a1SLionel Sambuc<p> 673711be35a1SLionel SambucFinishes the use of buffer <code>B</code> leaving the final string on 673811be35a1SLionel Sambucthe top of the stack. 673911be35a1SLionel Sambuc 674011be35a1SLionel Sambuc 674111be35a1SLionel Sambuc 674211be35a1SLionel Sambuc 674311be35a1SLionel Sambuc 6744*0a6a1f1dSLionel Sambuc<hr><h3><a name="luaL_pushresultsize"><code>luaL_pushresultsize</code></a></h3><p> 6745*0a6a1f1dSLionel Sambuc<span class="apii">[-?, +1, <em>e</em>]</span> 6746*0a6a1f1dSLionel Sambuc<pre>void luaL_pushresultsize (luaL_Buffer *B, size_t sz);</pre> 6747*0a6a1f1dSLionel Sambuc 6748*0a6a1f1dSLionel Sambuc<p> 6749*0a6a1f1dSLionel SambucEquivalent to the sequence <a href="#luaL_addsize"><code>luaL_addsize</code></a>, <a href="#luaL_pushresult"><code>luaL_pushresult</code></a>. 6750*0a6a1f1dSLionel Sambuc 6751*0a6a1f1dSLionel Sambuc 6752*0a6a1f1dSLionel Sambuc 6753*0a6a1f1dSLionel Sambuc 6754*0a6a1f1dSLionel Sambuc 675511be35a1SLionel Sambuc<hr><h3><a name="luaL_ref"><code>luaL_ref</code></a></h3><p> 6756*0a6a1f1dSLionel Sambuc<span class="apii">[-1, +0, <em>e</em>]</span> 675711be35a1SLionel Sambuc<pre>int luaL_ref (lua_State *L, int t);</pre> 675811be35a1SLionel Sambuc 675911be35a1SLionel Sambuc<p> 676011be35a1SLionel SambucCreates and returns a <em>reference</em>, 676111be35a1SLionel Sambucin the table at index <code>t</code>, 676211be35a1SLionel Sambucfor the object at the top of the stack (and pops the object). 676311be35a1SLionel Sambuc 676411be35a1SLionel Sambuc 676511be35a1SLionel Sambuc<p> 676611be35a1SLionel SambucA reference is a unique integer key. 676711be35a1SLionel SambucAs long as you do not manually add integer keys into table <code>t</code>, 676811be35a1SLionel Sambuc<a href="#luaL_ref"><code>luaL_ref</code></a> ensures the uniqueness of the key it returns. 676911be35a1SLionel SambucYou can retrieve an object referred by reference <code>r</code> 677011be35a1SLionel Sambucby calling <code>lua_rawgeti(L, t, r)</code>. 677111be35a1SLionel SambucFunction <a href="#luaL_unref"><code>luaL_unref</code></a> frees a reference and its associated object. 677211be35a1SLionel Sambuc 677311be35a1SLionel Sambuc 677411be35a1SLionel Sambuc<p> 677511be35a1SLionel SambucIf the object at the top of the stack is <b>nil</b>, 677611be35a1SLionel Sambuc<a href="#luaL_ref"><code>luaL_ref</code></a> returns the constant <a name="pdf-LUA_REFNIL"><code>LUA_REFNIL</code></a>. 677711be35a1SLionel SambucThe constant <a name="pdf-LUA_NOREF"><code>LUA_NOREF</code></a> is guaranteed to be different 677811be35a1SLionel Sambucfrom any reference returned by <a href="#luaL_ref"><code>luaL_ref</code></a>. 677911be35a1SLionel Sambuc 678011be35a1SLionel Sambuc 678111be35a1SLionel Sambuc 678211be35a1SLionel Sambuc 678311be35a1SLionel Sambuc 678411be35a1SLionel Sambuc<hr><h3><a name="luaL_Reg"><code>luaL_Reg</code></a></h3> 678511be35a1SLionel Sambuc<pre>typedef struct luaL_Reg { 678611be35a1SLionel Sambuc const char *name; 678711be35a1SLionel Sambuc lua_CFunction func; 678811be35a1SLionel Sambuc} luaL_Reg;</pre> 678911be35a1SLionel Sambuc 679011be35a1SLionel Sambuc<p> 679111be35a1SLionel SambucType for arrays of functions to be registered by 6792*0a6a1f1dSLionel Sambuc<a href="#luaL_setfuncs"><code>luaL_setfuncs</code></a>. 679311be35a1SLionel Sambuc<code>name</code> is the function name and <code>func</code> is a pointer to 679411be35a1SLionel Sambucthe function. 6795*0a6a1f1dSLionel SambucAny array of <a href="#luaL_Reg"><code>luaL_Reg</code></a> must end with a sentinel entry 679611be35a1SLionel Sambucin which both <code>name</code> and <code>func</code> are <code>NULL</code>. 679711be35a1SLionel Sambuc 679811be35a1SLionel Sambuc 679911be35a1SLionel Sambuc 680011be35a1SLionel Sambuc 680111be35a1SLionel Sambuc 6802*0a6a1f1dSLionel Sambuc<hr><h3><a name="luaL_requiref"><code>luaL_requiref</code></a></h3><p> 6803*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, <em>e</em>]</span> 6804*0a6a1f1dSLionel Sambuc<pre>void luaL_requiref (lua_State *L, const char *modname, 6805*0a6a1f1dSLionel Sambuc lua_CFunction openf, int glb);</pre> 680611be35a1SLionel Sambuc 680711be35a1SLionel Sambuc<p> 6808*0a6a1f1dSLionel SambucIf <code>modname</code> is not already present in <a href="#pdf-package.loaded"><code>package.loaded</code></a>, 6809*0a6a1f1dSLionel Sambuccalls function <code>openf</code> with string <code>modname</code> as an argument 6810*0a6a1f1dSLionel Sambucand sets the call result in <code>package.loaded[modname]</code>, 6811*0a6a1f1dSLionel Sambucas if that function has been called through <a href="#pdf-require"><code>require</code></a>. 681211be35a1SLionel Sambuc 681311be35a1SLionel Sambuc 681411be35a1SLionel Sambuc<p> 6815*0a6a1f1dSLionel SambucIf <code>glb</code> is true, 6816*0a6a1f1dSLionel Sambucalso stores the module into global <code>modname</code>. 681711be35a1SLionel Sambuc 681811be35a1SLionel Sambuc 681911be35a1SLionel Sambuc<p> 6820*0a6a1f1dSLionel SambucLeaves a copy of the module on the stack. 6821*0a6a1f1dSLionel Sambuc 6822*0a6a1f1dSLionel Sambuc 6823*0a6a1f1dSLionel Sambuc 6824*0a6a1f1dSLionel Sambuc 6825*0a6a1f1dSLionel Sambuc 6826*0a6a1f1dSLionel Sambuc<hr><h3><a name="luaL_setfuncs"><code>luaL_setfuncs</code></a></h3><p> 6827*0a6a1f1dSLionel Sambuc<span class="apii">[-nup, +0, <em>e</em>]</span> 6828*0a6a1f1dSLionel Sambuc<pre>void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup);</pre> 6829*0a6a1f1dSLionel Sambuc 6830*0a6a1f1dSLionel Sambuc<p> 6831*0a6a1f1dSLionel SambucRegisters all functions in the array <code>l</code> 6832*0a6a1f1dSLionel Sambuc(see <a href="#luaL_Reg"><code>luaL_Reg</code></a>) into the table on the top of the stack 6833*0a6a1f1dSLionel Sambuc(below optional upvalues, see next). 683411be35a1SLionel Sambuc 683511be35a1SLionel Sambuc 683611be35a1SLionel Sambuc<p> 6837*0a6a1f1dSLionel SambucWhen <code>nup</code> is not zero, 6838*0a6a1f1dSLionel Sambucall functions are created sharing <code>nup</code> upvalues, 6839*0a6a1f1dSLionel Sambucwhich must be previously pushed on the stack 6840*0a6a1f1dSLionel Sambucon top of the library table. 6841*0a6a1f1dSLionel SambucThese values are popped from the stack after the registration. 6842*0a6a1f1dSLionel Sambuc 6843*0a6a1f1dSLionel Sambuc 6844*0a6a1f1dSLionel Sambuc 6845*0a6a1f1dSLionel Sambuc 6846*0a6a1f1dSLionel Sambuc 6847*0a6a1f1dSLionel Sambuc<hr><h3><a name="luaL_setmetatable"><code>luaL_setmetatable</code></a></h3><p> 6848*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 6849*0a6a1f1dSLionel Sambuc<pre>void luaL_setmetatable (lua_State *L, const char *tname);</pre> 6850*0a6a1f1dSLionel Sambuc 6851*0a6a1f1dSLionel Sambuc<p> 6852*0a6a1f1dSLionel SambucSets the metatable of the object at the top of the stack 6853*0a6a1f1dSLionel Sambucas the metatable associated with name <code>tname</code> 6854*0a6a1f1dSLionel Sambucin the registry (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>). 6855*0a6a1f1dSLionel Sambuc 6856*0a6a1f1dSLionel Sambuc 6857*0a6a1f1dSLionel Sambuc 6858*0a6a1f1dSLionel Sambuc 6859*0a6a1f1dSLionel Sambuc 6860*0a6a1f1dSLionel Sambuc<hr><h3><a name="luaL_Stream"><code>luaL_Stream</code></a></h3> 6861*0a6a1f1dSLionel Sambuc<pre>typedef struct luaL_Stream { 6862*0a6a1f1dSLionel Sambuc FILE *f; 6863*0a6a1f1dSLionel Sambuc lua_CFunction closef; 6864*0a6a1f1dSLionel Sambuc} luaL_Stream;</pre> 6865*0a6a1f1dSLionel Sambuc 6866*0a6a1f1dSLionel Sambuc<p> 6867*0a6a1f1dSLionel SambucThe standard representation for file handles, 6868*0a6a1f1dSLionel Sambucwhich is used by the standard I/O library. 6869*0a6a1f1dSLionel Sambuc 6870*0a6a1f1dSLionel Sambuc 6871*0a6a1f1dSLionel Sambuc<p> 6872*0a6a1f1dSLionel SambucA file handle is implemented as a full userdata, 6873*0a6a1f1dSLionel Sambucwith a metatable called <code>LUA_FILEHANDLE</code> 6874*0a6a1f1dSLionel Sambuc(where <code>LUA_FILEHANDLE</code> is a macro with the actual metatable's name). 6875*0a6a1f1dSLionel SambucThe metatable is created by the I/O library 6876*0a6a1f1dSLionel Sambuc(see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>). 6877*0a6a1f1dSLionel Sambuc 6878*0a6a1f1dSLionel Sambuc 6879*0a6a1f1dSLionel Sambuc<p> 6880*0a6a1f1dSLionel SambucThis userdata must start with the structure <code>luaL_Stream</code>; 6881*0a6a1f1dSLionel Sambucit can contain other data after this initial structure. 6882*0a6a1f1dSLionel SambucField <code>f</code> points to the corresponding C stream 6883*0a6a1f1dSLionel Sambuc(or it can be <code>NULL</code> to indicate an incompletely created handle). 6884*0a6a1f1dSLionel SambucField <code>closef</code> points to a Lua function 6885*0a6a1f1dSLionel Sambucthat will be called to close the stream 6886*0a6a1f1dSLionel Sambucwhen the handle is closed or collected; 6887*0a6a1f1dSLionel Sambucthis function receives the file handle as its sole argument and 6888*0a6a1f1dSLionel Sambucmust return either <b>true</b> (in case of success) 6889*0a6a1f1dSLionel Sambucor <b>nil</b> plus an error message (in case of error). 6890*0a6a1f1dSLionel SambucOnce Lua calls this field, 6891*0a6a1f1dSLionel Sambucthe field value is changed to <code>NULL</code> 6892*0a6a1f1dSLionel Sambucto signal that the handle is closed. 6893*0a6a1f1dSLionel Sambuc 6894*0a6a1f1dSLionel Sambuc 6895*0a6a1f1dSLionel Sambuc 6896*0a6a1f1dSLionel Sambuc 6897*0a6a1f1dSLionel Sambuc 6898*0a6a1f1dSLionel Sambuc<hr><h3><a name="luaL_testudata"><code>luaL_testudata</code></a></h3><p> 6899*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, <em>e</em>]</span> 6900*0a6a1f1dSLionel Sambuc<pre>void *luaL_testudata (lua_State *L, int arg, const char *tname);</pre> 6901*0a6a1f1dSLionel Sambuc 6902*0a6a1f1dSLionel Sambuc<p> 6903*0a6a1f1dSLionel SambucThis function works like <a href="#luaL_checkudata"><code>luaL_checkudata</code></a>, 6904*0a6a1f1dSLionel Sambucexcept that, when the test fails, 6905*0a6a1f1dSLionel Sambucit returns <code>NULL</code> instead of raising an error. 6906*0a6a1f1dSLionel Sambuc 6907*0a6a1f1dSLionel Sambuc 6908*0a6a1f1dSLionel Sambuc 6909*0a6a1f1dSLionel Sambuc 6910*0a6a1f1dSLionel Sambuc 6911*0a6a1f1dSLionel Sambuc<hr><h3><a name="luaL_tolstring"><code>luaL_tolstring</code></a></h3><p> 6912*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, <em>e</em>]</span> 6913*0a6a1f1dSLionel Sambuc<pre>const char *luaL_tolstring (lua_State *L, int idx, size_t *len);</pre> 6914*0a6a1f1dSLionel Sambuc 6915*0a6a1f1dSLionel Sambuc<p> 6916*0a6a1f1dSLionel SambucConverts any Lua value at the given index to a C string 6917*0a6a1f1dSLionel Sambucin a reasonable format. 6918*0a6a1f1dSLionel SambucThe resulting string is pushed onto the stack and also 6919*0a6a1f1dSLionel Sambucreturned by the function. 6920*0a6a1f1dSLionel SambucIf <code>len</code> is not <code>NULL</code>, 6921*0a6a1f1dSLionel Sambucthe function also sets <code>*len</code> with the string length. 6922*0a6a1f1dSLionel Sambuc 6923*0a6a1f1dSLionel Sambuc 6924*0a6a1f1dSLionel Sambuc<p> 6925*0a6a1f1dSLionel SambucIf the value has a metatable with a <code>"__tostring"</code> field, 6926*0a6a1f1dSLionel Sambucthen <code>luaL_tolstring</code> calls the corresponding metamethod 6927*0a6a1f1dSLionel Sambucwith the value as argument, 6928*0a6a1f1dSLionel Sambucand uses the result of the call as its result. 6929*0a6a1f1dSLionel Sambuc 6930*0a6a1f1dSLionel Sambuc 6931*0a6a1f1dSLionel Sambuc 6932*0a6a1f1dSLionel Sambuc 6933*0a6a1f1dSLionel Sambuc 6934*0a6a1f1dSLionel Sambuc<hr><h3><a name="luaL_traceback"><code>luaL_traceback</code></a></h3><p> 6935*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, <em>e</em>]</span> 6936*0a6a1f1dSLionel Sambuc<pre>void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, 6937*0a6a1f1dSLionel Sambuc int level);</pre> 6938*0a6a1f1dSLionel Sambuc 6939*0a6a1f1dSLionel Sambuc<p> 6940*0a6a1f1dSLionel SambucCreates and pushes a traceback of the stack <code>L1</code>. 6941*0a6a1f1dSLionel SambucIf <code>msg</code> is not <code>NULL</code> it is appended 6942*0a6a1f1dSLionel Sambucat the beginning of the traceback. 6943*0a6a1f1dSLionel SambucThe <code>level</code> parameter tells at which level 6944*0a6a1f1dSLionel Sambucto start the traceback. 694511be35a1SLionel Sambuc 694611be35a1SLionel Sambuc 694711be35a1SLionel Sambuc 694811be35a1SLionel Sambuc 694911be35a1SLionel Sambuc 695011be35a1SLionel Sambuc<hr><h3><a name="luaL_typename"><code>luaL_typename</code></a></h3><p> 6951*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 695211be35a1SLionel Sambuc<pre>const char *luaL_typename (lua_State *L, int index);</pre> 695311be35a1SLionel Sambuc 695411be35a1SLionel Sambuc<p> 695511be35a1SLionel SambucReturns the name of the type of the value at the given index. 695611be35a1SLionel Sambuc 695711be35a1SLionel Sambuc 695811be35a1SLionel Sambuc 695911be35a1SLionel Sambuc 696011be35a1SLionel Sambuc 696111be35a1SLionel Sambuc<hr><h3><a name="luaL_unref"><code>luaL_unref</code></a></h3><p> 6962*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +0, –]</span> 696311be35a1SLionel Sambuc<pre>void luaL_unref (lua_State *L, int t, int ref);</pre> 696411be35a1SLionel Sambuc 696511be35a1SLionel Sambuc<p> 696611be35a1SLionel SambucReleases reference <code>ref</code> from the table at index <code>t</code> 696711be35a1SLionel Sambuc(see <a href="#luaL_ref"><code>luaL_ref</code></a>). 696811be35a1SLionel SambucThe entry is removed from the table, 696911be35a1SLionel Sambucso that the referred object can be collected. 697011be35a1SLionel SambucThe reference <code>ref</code> is also freed to be used again. 697111be35a1SLionel Sambuc 697211be35a1SLionel Sambuc 697311be35a1SLionel Sambuc<p> 697411be35a1SLionel SambucIf <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>, 697511be35a1SLionel Sambuc<a href="#luaL_unref"><code>luaL_unref</code></a> does nothing. 697611be35a1SLionel Sambuc 697711be35a1SLionel Sambuc 697811be35a1SLionel Sambuc 697911be35a1SLionel Sambuc 698011be35a1SLionel Sambuc 698111be35a1SLionel Sambuc<hr><h3><a name="luaL_where"><code>luaL_where</code></a></h3><p> 6982*0a6a1f1dSLionel Sambuc<span class="apii">[-0, +1, <em>e</em>]</span> 698311be35a1SLionel Sambuc<pre>void luaL_where (lua_State *L, int lvl);</pre> 698411be35a1SLionel Sambuc 698511be35a1SLionel Sambuc<p> 698611be35a1SLionel SambucPushes onto the stack a string identifying the current position 698711be35a1SLionel Sambucof the control at level <code>lvl</code> in the call stack. 698811be35a1SLionel SambucTypically this string has the following format: 698911be35a1SLionel Sambuc 699011be35a1SLionel Sambuc<pre> 699111be35a1SLionel Sambuc <em>chunkname</em>:<em>currentline</em>: 699211be35a1SLionel Sambuc</pre><p> 699311be35a1SLionel SambucLevel 0 is the running function, 699411be35a1SLionel Sambuclevel 1 is the function that called the running function, 699511be35a1SLionel Sambucetc. 699611be35a1SLionel Sambuc 699711be35a1SLionel Sambuc 699811be35a1SLionel Sambuc<p> 699911be35a1SLionel SambucThis function is used to build a prefix for error messages. 700011be35a1SLionel Sambuc 700111be35a1SLionel Sambuc 700211be35a1SLionel Sambuc 700311be35a1SLionel Sambuc 700411be35a1SLionel Sambuc 700511be35a1SLionel Sambuc 700611be35a1SLionel Sambuc 7007*0a6a1f1dSLionel Sambuc<h1>6 – <a name="6">Standard Libraries</a></h1> 700811be35a1SLionel Sambuc 700911be35a1SLionel Sambuc<p> 701011be35a1SLionel SambucThe standard Lua libraries provide useful functions 701111be35a1SLionel Sambucthat are implemented directly through the C API. 701211be35a1SLionel SambucSome of these functions provide essential services to the language 701311be35a1SLionel Sambuc(e.g., <a href="#pdf-type"><code>type</code></a> and <a href="#pdf-getmetatable"><code>getmetatable</code></a>); 701411be35a1SLionel Sambucothers provide access to "outside" services (e.g., I/O); 701511be35a1SLionel Sambucand others could be implemented in Lua itself, 701611be35a1SLionel Sambucbut are quite useful or have critical performance requirements that 701711be35a1SLionel Sambucdeserve an implementation in C (e.g., <a href="#pdf-table.sort"><code>table.sort</code></a>). 701811be35a1SLionel Sambuc 701911be35a1SLionel Sambuc 702011be35a1SLionel Sambuc<p> 702111be35a1SLionel SambucAll libraries are implemented through the official C API 702211be35a1SLionel Sambucand are provided as separate C modules. 702311be35a1SLionel SambucCurrently, Lua has the following standard libraries: 702411be35a1SLionel Sambuc 702511be35a1SLionel Sambuc<ul> 702611be35a1SLionel Sambuc 7027*0a6a1f1dSLionel Sambuc<li>basic library (<a href="#6.1">§6.1</a>);</li> 702811be35a1SLionel Sambuc 7029*0a6a1f1dSLionel Sambuc<li>coroutine library (<a href="#6.2">§6.2</a>);</li> 703011be35a1SLionel Sambuc 7031*0a6a1f1dSLionel Sambuc<li>package library (<a href="#6.3">§6.3</a>);</li> 703211be35a1SLionel Sambuc 7033*0a6a1f1dSLionel Sambuc<li>string manipulation (<a href="#6.4">§6.4</a>);</li> 703411be35a1SLionel Sambuc 7035*0a6a1f1dSLionel Sambuc<li>basic UTF-8 support (<a href="#6.5">§6.5</a>);</li> 703611be35a1SLionel Sambuc 7037*0a6a1f1dSLionel Sambuc<li>table manipulation (<a href="#6.6">§6.6</a>);</li> 703811be35a1SLionel Sambuc 7039*0a6a1f1dSLionel Sambuc<li>mathematical functions (<a href="#6.7">§6.7</a>) (sin, log, etc.);</li> 704011be35a1SLionel Sambuc 7041*0a6a1f1dSLionel Sambuc<li>input and output (<a href="#6.8">§6.8</a>);</li> 7042*0a6a1f1dSLionel Sambuc 7043*0a6a1f1dSLionel Sambuc<li>operating system facilities (<a href="#6.9">§6.9</a>);</li> 7044*0a6a1f1dSLionel Sambuc 7045*0a6a1f1dSLionel Sambuc<li>debug facilities (<a href="#6.10">§6.10</a>).</li> 704611be35a1SLionel Sambuc 704711be35a1SLionel Sambuc</ul><p> 7048*0a6a1f1dSLionel SambucExcept for the basic and the package libraries, 704911be35a1SLionel Sambuceach library provides all its functions as fields of a global table 705011be35a1SLionel Sambucor as methods of its objects. 705111be35a1SLionel Sambuc 705211be35a1SLionel Sambuc 705311be35a1SLionel Sambuc<p> 705411be35a1SLionel SambucTo have access to these libraries, 705511be35a1SLionel Sambucthe C host program should call the <a href="#luaL_openlibs"><code>luaL_openlibs</code></a> function, 705611be35a1SLionel Sambucwhich opens all standard libraries. 705711be35a1SLionel SambucAlternatively, 7058*0a6a1f1dSLionel Sambucthe host program can open them individually by using 7059*0a6a1f1dSLionel Sambuc<a href="#luaL_requiref"><code>luaL_requiref</code></a> to call 706011be35a1SLionel Sambuc<a name="pdf-luaopen_base"><code>luaopen_base</code></a> (for the basic library), 706111be35a1SLionel Sambuc<a name="pdf-luaopen_package"><code>luaopen_package</code></a> (for the package library), 7062*0a6a1f1dSLionel Sambuc<a name="pdf-luaopen_coroutine"><code>luaopen_coroutine</code></a> (for the coroutine library), 706311be35a1SLionel Sambuc<a name="pdf-luaopen_string"><code>luaopen_string</code></a> (for the string library), 7064*0a6a1f1dSLionel Sambuc<a name="pdf-luaopen_utf8"><code>luaopen_utf8</code></a> (for the UTF8 library), 706511be35a1SLionel Sambuc<a name="pdf-luaopen_table"><code>luaopen_table</code></a> (for the table library), 706611be35a1SLionel Sambuc<a name="pdf-luaopen_math"><code>luaopen_math</code></a> (for the mathematical library), 706711be35a1SLionel Sambuc<a name="pdf-luaopen_io"><code>luaopen_io</code></a> (for the I/O library), 7068*0a6a1f1dSLionel Sambuc<a name="pdf-luaopen_os"><code>luaopen_os</code></a> (for the operating system library), 706911be35a1SLionel Sambucand <a name="pdf-luaopen_debug"><code>luaopen_debug</code></a> (for the debug library). 7070*0a6a1f1dSLionel SambucThese functions are declared in <a name="pdf-lualib.h"><code>lualib.h</code></a>. 707111be35a1SLionel Sambuc 707211be35a1SLionel Sambuc 707311be35a1SLionel Sambuc 7074*0a6a1f1dSLionel Sambuc<h2>6.1 – <a name="6.1">Basic Functions</a></h2> 707511be35a1SLionel Sambuc 707611be35a1SLionel Sambuc<p> 7077*0a6a1f1dSLionel SambucThe basic library provides core functions to Lua. 707811be35a1SLionel SambucIf you do not include this library in your application, 707911be35a1SLionel Sambucyou should check carefully whether you need to provide 708011be35a1SLionel Sambucimplementations for some of its facilities. 708111be35a1SLionel Sambuc 708211be35a1SLionel Sambuc 708311be35a1SLionel Sambuc<p> 708411be35a1SLionel Sambuc<hr><h3><a name="pdf-assert"><code>assert (v [, message])</code></a></h3> 7085*0a6a1f1dSLionel Sambuc 7086*0a6a1f1dSLionel Sambuc 7087*0a6a1f1dSLionel Sambuc<p> 7088*0a6a1f1dSLionel SambucCalls <a href="#pdf-error"><code>error</code></a> if 708911be35a1SLionel Sambucthe value of its argument <code>v</code> is false (i.e., <b>nil</b> or <b>false</b>); 709011be35a1SLionel Sambucotherwise, returns all its arguments. 7091*0a6a1f1dSLionel SambucIn case of error, 7092*0a6a1f1dSLionel Sambuc<code>message</code> is the error object; 7093*0a6a1f1dSLionel Sambucwhen absent, it defaults to "<code>assertion failed!</code>" 709411be35a1SLionel Sambuc 709511be35a1SLionel Sambuc 709611be35a1SLionel Sambuc 709711be35a1SLionel Sambuc 709811be35a1SLionel Sambuc<p> 709911be35a1SLionel Sambuc<hr><h3><a name="pdf-collectgarbage"><code>collectgarbage ([opt [, arg]])</code></a></h3> 710011be35a1SLionel Sambuc 710111be35a1SLionel Sambuc 710211be35a1SLionel Sambuc<p> 710311be35a1SLionel SambucThis function is a generic interface to the garbage collector. 710411be35a1SLionel SambucIt performs different functions according to its first argument, <code>opt</code>: 710511be35a1SLionel Sambuc 710611be35a1SLionel Sambuc<ul> 710711be35a1SLionel Sambuc 7108*0a6a1f1dSLionel Sambuc<li><b>"<code>collect</code>": </b> 710911be35a1SLionel Sambucperforms a full garbage-collection cycle. 711011be35a1SLionel SambucThis is the default option. 711111be35a1SLionel Sambuc</li> 711211be35a1SLionel Sambuc 7113*0a6a1f1dSLionel Sambuc<li><b>"<code>stop</code>": </b> 7114*0a6a1f1dSLionel Sambucstops automatic execution of the garbage collector. 7115*0a6a1f1dSLionel SambucThe collector will run only when explicitly invoked, 7116*0a6a1f1dSLionel Sambucuntil a call to restart it. 711711be35a1SLionel Sambuc</li> 711811be35a1SLionel Sambuc 7119*0a6a1f1dSLionel Sambuc<li><b>"<code>restart</code>": </b> 7120*0a6a1f1dSLionel Sambucrestarts automatic execution of the garbage collector. 712111be35a1SLionel Sambuc</li> 712211be35a1SLionel Sambuc 7123*0a6a1f1dSLionel Sambuc<li><b>"<code>count</code>": </b> 7124*0a6a1f1dSLionel Sambucreturns the total memory in use by Lua in Kbytes. 7125*0a6a1f1dSLionel SambucThe value has a fractional part, 7126*0a6a1f1dSLionel Sambucso that it multiplied by 1024 7127*0a6a1f1dSLionel Sambucgives the exact number of bytes in use by Lua 7128*0a6a1f1dSLionel Sambuc(except for overflows). 712911be35a1SLionel Sambuc</li> 713011be35a1SLionel Sambuc 7131*0a6a1f1dSLionel Sambuc<li><b>"<code>step</code>": </b> 713211be35a1SLionel Sambucperforms a garbage-collection step. 7133*0a6a1f1dSLionel SambucThe step "size" is controlled by <code>arg</code>. 7134*0a6a1f1dSLionel SambucWith a zero value, 7135*0a6a1f1dSLionel Sambucthe collector will perform one basic (indivisible) step. 7136*0a6a1f1dSLionel SambucFor non-zero values, 7137*0a6a1f1dSLionel Sambucthe collector will perform as if that amount of memory 7138*0a6a1f1dSLionel Sambuc(in KBytes) had been allocated by Lua. 713911be35a1SLionel SambucReturns <b>true</b> if the step finished a collection cycle. 714011be35a1SLionel Sambuc</li> 714111be35a1SLionel Sambuc 7142*0a6a1f1dSLionel Sambuc<li><b>"<code>setpause</code>": </b> 714311be35a1SLionel Sambucsets <code>arg</code> as the new value for the <em>pause</em> of 7144*0a6a1f1dSLionel Sambucthe collector (see <a href="#2.5">§2.5</a>). 714511be35a1SLionel SambucReturns the previous value for <em>pause</em>. 714611be35a1SLionel Sambuc</li> 714711be35a1SLionel Sambuc 7148*0a6a1f1dSLionel Sambuc<li><b>"<code>setstepmul</code>": </b> 714911be35a1SLionel Sambucsets <code>arg</code> as the new value for the <em>step multiplier</em> of 7150*0a6a1f1dSLionel Sambucthe collector (see <a href="#2.5">§2.5</a>). 715111be35a1SLionel SambucReturns the previous value for <em>step</em>. 715211be35a1SLionel Sambuc</li> 715311be35a1SLionel Sambuc 7154*0a6a1f1dSLionel Sambuc<li><b>"<code>isrunning</code>": </b> 7155*0a6a1f1dSLionel Sambucreturns a boolean that tells whether the collector is running 7156*0a6a1f1dSLionel Sambuc(i.e., not stopped). 7157*0a6a1f1dSLionel Sambuc</li> 7158*0a6a1f1dSLionel Sambuc 715911be35a1SLionel Sambuc</ul> 716011be35a1SLionel Sambuc 716111be35a1SLionel Sambuc 716211be35a1SLionel Sambuc 716311be35a1SLionel Sambuc<p> 716411be35a1SLionel Sambuc<hr><h3><a name="pdf-dofile"><code>dofile ([filename])</code></a></h3> 716511be35a1SLionel SambucOpens the named file and executes its contents as a Lua chunk. 716611be35a1SLionel SambucWhen called without arguments, 716711be35a1SLionel Sambuc<code>dofile</code> executes the contents of the standard input (<code>stdin</code>). 716811be35a1SLionel SambucReturns all values returned by the chunk. 716911be35a1SLionel SambucIn case of errors, <code>dofile</code> propagates the error 717011be35a1SLionel Sambucto its caller (that is, <code>dofile</code> does not run in protected mode). 717111be35a1SLionel Sambuc 717211be35a1SLionel Sambuc 717311be35a1SLionel Sambuc 717411be35a1SLionel Sambuc 717511be35a1SLionel Sambuc<p> 717611be35a1SLionel Sambuc<hr><h3><a name="pdf-error"><code>error (message [, level])</code></a></h3> 717711be35a1SLionel SambucTerminates the last protected function called 7178*0a6a1f1dSLionel Sambucand returns <code>message</code> as the error object. 717911be35a1SLionel SambucFunction <code>error</code> never returns. 718011be35a1SLionel Sambuc 718111be35a1SLionel Sambuc 718211be35a1SLionel Sambuc<p> 718311be35a1SLionel SambucUsually, <code>error</code> adds some information about the error position 7184*0a6a1f1dSLionel Sambucat the beginning of the message, if the message is a string. 718511be35a1SLionel SambucThe <code>level</code> argument specifies how to get the error position. 718611be35a1SLionel SambucWith level 1 (the default), the error position is where the 718711be35a1SLionel Sambuc<code>error</code> function was called. 718811be35a1SLionel SambucLevel 2 points the error to where the function 718911be35a1SLionel Sambucthat called <code>error</code> was called; and so on. 719011be35a1SLionel SambucPassing a level 0 avoids the addition of error position information 719111be35a1SLionel Sambucto the message. 719211be35a1SLionel Sambuc 719311be35a1SLionel Sambuc 719411be35a1SLionel Sambuc 719511be35a1SLionel Sambuc 719611be35a1SLionel Sambuc<p> 719711be35a1SLionel Sambuc<hr><h3><a name="pdf-_G"><code>_G</code></a></h3> 719811be35a1SLionel SambucA global variable (not a function) that 7199*0a6a1f1dSLionel Sambucholds the global environment (see <a href="#2.2">§2.2</a>). 720011be35a1SLionel SambucLua itself does not use this variable; 720111be35a1SLionel Sambucchanging its value does not affect any environment, 7202*0a6a1f1dSLionel Sambucnor vice versa. 720311be35a1SLionel Sambuc 720411be35a1SLionel Sambuc 720511be35a1SLionel Sambuc 720611be35a1SLionel Sambuc 720711be35a1SLionel Sambuc<p> 720811be35a1SLionel Sambuc<hr><h3><a name="pdf-getmetatable"><code>getmetatable (object)</code></a></h3> 720911be35a1SLionel Sambuc 721011be35a1SLionel Sambuc 721111be35a1SLionel Sambuc<p> 721211be35a1SLionel SambucIf <code>object</code> does not have a metatable, returns <b>nil</b>. 721311be35a1SLionel SambucOtherwise, 721411be35a1SLionel Sambucif the object's metatable has a <code>"__metatable"</code> field, 721511be35a1SLionel Sambucreturns the associated value. 721611be35a1SLionel SambucOtherwise, returns the metatable of the given object. 721711be35a1SLionel Sambuc 721811be35a1SLionel Sambuc 721911be35a1SLionel Sambuc 722011be35a1SLionel Sambuc 722111be35a1SLionel Sambuc<p> 722211be35a1SLionel Sambuc<hr><h3><a name="pdf-ipairs"><code>ipairs (t)</code></a></h3> 722311be35a1SLionel Sambuc 722411be35a1SLionel Sambuc 722511be35a1SLionel Sambuc<p> 7226*0a6a1f1dSLionel SambucReturns three values (an iterator function, the table <code>t</code>, and 0) 722711be35a1SLionel Sambucso that the construction 722811be35a1SLionel Sambuc 722911be35a1SLionel Sambuc<pre> 723011be35a1SLionel Sambuc for i,v in ipairs(t) do <em>body</em> end 723111be35a1SLionel Sambuc</pre><p> 7232*0a6a1f1dSLionel Sambucwill iterate over the key–value pairs 7233*0a6a1f1dSLionel Sambuc(<code>1,t[1]</code>), (<code>2,t[2]</code>), ..., 7234*0a6a1f1dSLionel Sambucup to the first nil value. 723511be35a1SLionel Sambuc 723611be35a1SLionel Sambuc 723711be35a1SLionel Sambuc 723811be35a1SLionel Sambuc 723911be35a1SLionel Sambuc<p> 7240*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-load"><code>load (chunk [, chunkname [, mode [, env]]])</code></a></h3> 724111be35a1SLionel Sambuc 724211be35a1SLionel Sambuc 724311be35a1SLionel Sambuc<p> 7244*0a6a1f1dSLionel SambucLoads a chunk. 7245*0a6a1f1dSLionel Sambuc 7246*0a6a1f1dSLionel Sambuc 7247*0a6a1f1dSLionel Sambuc<p> 7248*0a6a1f1dSLionel SambucIf <code>chunk</code> is a string, the chunk is this string. 7249*0a6a1f1dSLionel SambucIf <code>chunk</code> is a function, 7250*0a6a1f1dSLionel Sambuc<code>load</code> calls it repeatedly to get the chunk pieces. 7251*0a6a1f1dSLionel SambucEach call to <code>chunk</code> must return a string that concatenates 725211be35a1SLionel Sambucwith previous results. 725311be35a1SLionel SambucA return of an empty string, <b>nil</b>, or no value signals the end of the chunk. 725411be35a1SLionel Sambuc 725511be35a1SLionel Sambuc 725611be35a1SLionel Sambuc<p> 7257*0a6a1f1dSLionel SambucIf there are no syntactic errors, 725811be35a1SLionel Sambucreturns the compiled chunk as a function; 725911be35a1SLionel Sambucotherwise, returns <b>nil</b> plus the error message. 726011be35a1SLionel Sambuc 726111be35a1SLionel Sambuc 726211be35a1SLionel Sambuc<p> 7263*0a6a1f1dSLionel SambucIf the resulting function has upvalues, 7264*0a6a1f1dSLionel Sambucthe first upvalue is set to the value of <code>env</code>, 7265*0a6a1f1dSLionel Sambucif that parameter is given, 7266*0a6a1f1dSLionel Sambucor to the value of the global environment. 7267*0a6a1f1dSLionel SambucOther upvalues are initialized with <b>nil</b>. 7268*0a6a1f1dSLionel Sambuc(When you load a main chunk, 7269*0a6a1f1dSLionel Sambucthe resulting function will always have exactly one upvalue, 7270*0a6a1f1dSLionel Sambucthe <code>_ENV</code> variable (see <a href="#2.2">§2.2</a>). 7271*0a6a1f1dSLionel SambucHowever, 7272*0a6a1f1dSLionel Sambucwhen you load a binary chunk created from a function (see <a href="#pdf-string.dump"><code>string.dump</code></a>), 7273*0a6a1f1dSLionel Sambucthe resulting function can have an arbitrary number of upvalues.) 7274*0a6a1f1dSLionel SambucAll upvalues are fresh, that is, 7275*0a6a1f1dSLionel Sambucthey are not shared with any other function. 7276*0a6a1f1dSLionel Sambuc 7277*0a6a1f1dSLionel Sambuc 7278*0a6a1f1dSLionel Sambuc<p> 7279*0a6a1f1dSLionel Sambuc<code>chunkname</code> is used as the name of the chunk for error messages 7280*0a6a1f1dSLionel Sambucand debug information (see <a href="#4.9">§4.9</a>). 728111be35a1SLionel SambucWhen absent, 7282*0a6a1f1dSLionel Sambucit defaults to <code>chunk</code>, if <code>chunk</code> is a string, 7283*0a6a1f1dSLionel Sambucor to "<code>=(load)</code>" otherwise. 7284*0a6a1f1dSLionel Sambuc 7285*0a6a1f1dSLionel Sambuc 7286*0a6a1f1dSLionel Sambuc<p> 7287*0a6a1f1dSLionel SambucThe string <code>mode</code> controls whether the chunk can be text or binary 7288*0a6a1f1dSLionel Sambuc(that is, a precompiled chunk). 7289*0a6a1f1dSLionel SambucIt may be the string "<code>b</code>" (only binary chunks), 7290*0a6a1f1dSLionel Sambuc"<code>t</code>" (only text chunks), 7291*0a6a1f1dSLionel Sambucor "<code>bt</code>" (both binary and text). 7292*0a6a1f1dSLionel SambucThe default is "<code>bt</code>". 7293*0a6a1f1dSLionel Sambuc 7294*0a6a1f1dSLionel Sambuc 7295*0a6a1f1dSLionel Sambuc<p> 7296*0a6a1f1dSLionel SambucLua does not check the consistency of binary chunks. 7297*0a6a1f1dSLionel SambucMaliciously crafted binary chunks can crash 7298*0a6a1f1dSLionel Sambucthe interpreter. 729911be35a1SLionel Sambuc 730011be35a1SLionel Sambuc 730111be35a1SLionel Sambuc 730211be35a1SLionel Sambuc 730311be35a1SLionel Sambuc<p> 7304*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-loadfile"><code>loadfile ([filename [, mode [, env]]])</code></a></h3> 730511be35a1SLionel Sambuc 730611be35a1SLionel Sambuc 730711be35a1SLionel Sambuc<p> 730811be35a1SLionel SambucSimilar to <a href="#pdf-load"><code>load</code></a>, 730911be35a1SLionel Sambucbut gets the chunk from file <code>filename</code> 731011be35a1SLionel Sambucor from the standard input, 731111be35a1SLionel Sambucif no file name is given. 731211be35a1SLionel Sambuc 731311be35a1SLionel Sambuc 731411be35a1SLionel Sambuc 731511be35a1SLionel Sambuc 731611be35a1SLionel Sambuc<p> 731711be35a1SLionel Sambuc<hr><h3><a name="pdf-next"><code>next (table [, index])</code></a></h3> 731811be35a1SLionel Sambuc 731911be35a1SLionel Sambuc 732011be35a1SLionel Sambuc<p> 732111be35a1SLionel SambucAllows a program to traverse all fields of a table. 732211be35a1SLionel SambucIts first argument is a table and its second argument 732311be35a1SLionel Sambucis an index in this table. 732411be35a1SLionel Sambuc<code>next</code> returns the next index of the table 732511be35a1SLionel Sambucand its associated value. 732611be35a1SLionel SambucWhen called with <b>nil</b> as its second argument, 732711be35a1SLionel Sambuc<code>next</code> returns an initial index 732811be35a1SLionel Sambucand its associated value. 732911be35a1SLionel SambucWhen called with the last index, 733011be35a1SLionel Sambucor with <b>nil</b> in an empty table, 733111be35a1SLionel Sambuc<code>next</code> returns <b>nil</b>. 733211be35a1SLionel SambucIf the second argument is absent, then it is interpreted as <b>nil</b>. 733311be35a1SLionel SambucIn particular, 733411be35a1SLionel Sambucyou can use <code>next(t)</code> to check whether a table is empty. 733511be35a1SLionel Sambuc 733611be35a1SLionel Sambuc 733711be35a1SLionel Sambuc<p> 733811be35a1SLionel SambucThe order in which the indices are enumerated is not specified, 733911be35a1SLionel Sambuc<em>even for numeric indices</em>. 7340*0a6a1f1dSLionel Sambuc(To traverse a table in numerical order, 7341*0a6a1f1dSLionel Sambucuse a numerical <b>for</b>.) 734211be35a1SLionel Sambuc 734311be35a1SLionel Sambuc 734411be35a1SLionel Sambuc<p> 7345*0a6a1f1dSLionel SambucThe behavior of <code>next</code> is undefined if, 734611be35a1SLionel Sambucduring the traversal, 734711be35a1SLionel Sambucyou assign any value to a non-existent field in the table. 734811be35a1SLionel SambucYou may however modify existing fields. 734911be35a1SLionel SambucIn particular, you may clear existing fields. 735011be35a1SLionel Sambuc 735111be35a1SLionel Sambuc 735211be35a1SLionel Sambuc 735311be35a1SLionel Sambuc 735411be35a1SLionel Sambuc<p> 735511be35a1SLionel Sambuc<hr><h3><a name="pdf-pairs"><code>pairs (t)</code></a></h3> 735611be35a1SLionel Sambuc 735711be35a1SLionel Sambuc 735811be35a1SLionel Sambuc<p> 7359*0a6a1f1dSLionel SambucIf <code>t</code> has a metamethod <code>__pairs</code>, 7360*0a6a1f1dSLionel Sambuccalls it with <code>t</code> as argument and returns the first three 7361*0a6a1f1dSLionel Sambucresults from the call. 7362*0a6a1f1dSLionel Sambuc 7363*0a6a1f1dSLionel Sambuc 7364*0a6a1f1dSLionel Sambuc<p> 7365*0a6a1f1dSLionel SambucOtherwise, 7366*0a6a1f1dSLionel Sambucreturns three values: the <a href="#pdf-next"><code>next</code></a> function, the table <code>t</code>, and <b>nil</b>, 736711be35a1SLionel Sambucso that the construction 736811be35a1SLionel Sambuc 736911be35a1SLionel Sambuc<pre> 737011be35a1SLionel Sambuc for k,v in pairs(t) do <em>body</em> end 737111be35a1SLionel Sambuc</pre><p> 737211be35a1SLionel Sambucwill iterate over all key–value pairs of table <code>t</code>. 737311be35a1SLionel Sambuc 737411be35a1SLionel Sambuc 737511be35a1SLionel Sambuc<p> 737611be35a1SLionel SambucSee function <a href="#pdf-next"><code>next</code></a> for the caveats of modifying 737711be35a1SLionel Sambucthe table during its traversal. 737811be35a1SLionel Sambuc 737911be35a1SLionel Sambuc 738011be35a1SLionel Sambuc 738111be35a1SLionel Sambuc 738211be35a1SLionel Sambuc<p> 7383*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-pcall"><code>pcall (f [, arg1, ···])</code></a></h3> 738411be35a1SLionel Sambuc 738511be35a1SLionel Sambuc 738611be35a1SLionel Sambuc<p> 738711be35a1SLionel SambucCalls function <code>f</code> with 738811be35a1SLionel Sambucthe given arguments in <em>protected mode</em>. 738911be35a1SLionel SambucThis means that any error inside <code>f</code> is not propagated; 739011be35a1SLionel Sambucinstead, <code>pcall</code> catches the error 739111be35a1SLionel Sambucand returns a status code. 739211be35a1SLionel SambucIts first result is the status code (a boolean), 739311be35a1SLionel Sambucwhich is true if the call succeeds without errors. 739411be35a1SLionel SambucIn such case, <code>pcall</code> also returns all results from the call, 739511be35a1SLionel Sambucafter this first result. 739611be35a1SLionel SambucIn case of any error, <code>pcall</code> returns <b>false</b> plus the error message. 739711be35a1SLionel Sambuc 739811be35a1SLionel Sambuc 739911be35a1SLionel Sambuc 740011be35a1SLionel Sambuc 740111be35a1SLionel Sambuc<p> 740211be35a1SLionel Sambuc<hr><h3><a name="pdf-print"><code>print (···)</code></a></h3> 7403*0a6a1f1dSLionel SambucReceives any number of arguments 740411be35a1SLionel Sambucand prints their values to <code>stdout</code>, 7405*0a6a1f1dSLionel Sambucusing the <a href="#pdf-tostring"><code>tostring</code></a> function to convert each argument to a string. 740611be35a1SLionel Sambuc<code>print</code> is not intended for formatted output, 740711be35a1SLionel Sambucbut only as a quick way to show a value, 7408*0a6a1f1dSLionel Sambucfor instance for debugging. 7409*0a6a1f1dSLionel SambucFor complete control over the output, 7410*0a6a1f1dSLionel Sambucuse <a href="#pdf-string.format"><code>string.format</code></a> and <a href="#pdf-io.write"><code>io.write</code></a>. 741111be35a1SLionel Sambuc 741211be35a1SLionel Sambuc 741311be35a1SLionel Sambuc 741411be35a1SLionel Sambuc 741511be35a1SLionel Sambuc<p> 741611be35a1SLionel Sambuc<hr><h3><a name="pdf-rawequal"><code>rawequal (v1, v2)</code></a></h3> 741711be35a1SLionel SambucChecks whether <code>v1</code> is equal to <code>v2</code>, 741811be35a1SLionel Sambucwithout invoking any metamethod. 741911be35a1SLionel SambucReturns a boolean. 742011be35a1SLionel Sambuc 742111be35a1SLionel Sambuc 742211be35a1SLionel Sambuc 742311be35a1SLionel Sambuc 742411be35a1SLionel Sambuc<p> 742511be35a1SLionel Sambuc<hr><h3><a name="pdf-rawget"><code>rawget (table, index)</code></a></h3> 742611be35a1SLionel SambucGets the real value of <code>table[index]</code>, 742711be35a1SLionel Sambucwithout invoking any metamethod. 742811be35a1SLionel Sambuc<code>table</code> must be a table; 742911be35a1SLionel Sambuc<code>index</code> may be any value. 743011be35a1SLionel Sambuc 743111be35a1SLionel Sambuc 743211be35a1SLionel Sambuc 743311be35a1SLionel Sambuc 743411be35a1SLionel Sambuc<p> 7435*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-rawlen"><code>rawlen (v)</code></a></h3> 7436*0a6a1f1dSLionel SambucReturns the length of the object <code>v</code>, 7437*0a6a1f1dSLionel Sambucwhich must be a table or a string, 7438*0a6a1f1dSLionel Sambucwithout invoking any metamethod. 7439*0a6a1f1dSLionel SambucReturns an integer. 7440*0a6a1f1dSLionel Sambuc 7441*0a6a1f1dSLionel Sambuc 7442*0a6a1f1dSLionel Sambuc 7443*0a6a1f1dSLionel Sambuc 7444*0a6a1f1dSLionel Sambuc<p> 744511be35a1SLionel Sambuc<hr><h3><a name="pdf-rawset"><code>rawset (table, index, value)</code></a></h3> 744611be35a1SLionel SambucSets the real value of <code>table[index]</code> to <code>value</code>, 744711be35a1SLionel Sambucwithout invoking any metamethod. 744811be35a1SLionel Sambuc<code>table</code> must be a table, 7449*0a6a1f1dSLionel Sambuc<code>index</code> any value different from <b>nil</b> and NaN, 745011be35a1SLionel Sambucand <code>value</code> any Lua value. 745111be35a1SLionel Sambuc 745211be35a1SLionel Sambuc 745311be35a1SLionel Sambuc<p> 745411be35a1SLionel SambucThis function returns <code>table</code>. 745511be35a1SLionel Sambuc 745611be35a1SLionel Sambuc 745711be35a1SLionel Sambuc 745811be35a1SLionel Sambuc 745911be35a1SLionel Sambuc<p> 746011be35a1SLionel Sambuc<hr><h3><a name="pdf-select"><code>select (index, ···)</code></a></h3> 746111be35a1SLionel Sambuc 746211be35a1SLionel Sambuc 746311be35a1SLionel Sambuc<p> 746411be35a1SLionel SambucIf <code>index</code> is a number, 7465*0a6a1f1dSLionel Sambucreturns all arguments after argument number <code>index</code>; 7466*0a6a1f1dSLionel Sambuca negative number indexes from the end (-1 is the last argument). 746711be35a1SLionel SambucOtherwise, <code>index</code> must be the string <code>"#"</code>, 746811be35a1SLionel Sambucand <code>select</code> returns the total number of extra arguments it received. 746911be35a1SLionel Sambuc 747011be35a1SLionel Sambuc 747111be35a1SLionel Sambuc 747211be35a1SLionel Sambuc 747311be35a1SLionel Sambuc<p> 747411be35a1SLionel Sambuc<hr><h3><a name="pdf-setmetatable"><code>setmetatable (table, metatable)</code></a></h3> 747511be35a1SLionel Sambuc 747611be35a1SLionel Sambuc 747711be35a1SLionel Sambuc<p> 747811be35a1SLionel SambucSets the metatable for the given table. 747911be35a1SLionel Sambuc(You cannot change the metatable of other types from Lua, only from C.) 748011be35a1SLionel SambucIf <code>metatable</code> is <b>nil</b>, 748111be35a1SLionel Sambucremoves the metatable of the given table. 748211be35a1SLionel SambucIf the original metatable has a <code>"__metatable"</code> field, 748311be35a1SLionel Sambucraises an error. 748411be35a1SLionel Sambuc 748511be35a1SLionel Sambuc 748611be35a1SLionel Sambuc<p> 748711be35a1SLionel SambucThis function returns <code>table</code>. 748811be35a1SLionel Sambuc 748911be35a1SLionel Sambuc 749011be35a1SLionel Sambuc 749111be35a1SLionel Sambuc 749211be35a1SLionel Sambuc<p> 749311be35a1SLionel Sambuc<hr><h3><a name="pdf-tonumber"><code>tonumber (e [, base])</code></a></h3> 7494*0a6a1f1dSLionel Sambuc 7495*0a6a1f1dSLionel Sambuc 7496*0a6a1f1dSLionel Sambuc<p> 7497*0a6a1f1dSLionel SambucWhen called with no <code>base</code>, 7498*0a6a1f1dSLionel Sambuc<code>tonumber</code> tries to convert its argument to a number. 7499*0a6a1f1dSLionel SambucIf the argument is already a number or 7500*0a6a1f1dSLionel Sambuca string convertible to a number, 7501*0a6a1f1dSLionel Sambucthen <code>tonumber</code> returns this number; 750211be35a1SLionel Sambucotherwise, it returns <b>nil</b>. 750311be35a1SLionel Sambuc 750411be35a1SLionel Sambuc 750511be35a1SLionel Sambuc<p> 7506*0a6a1f1dSLionel SambucThe conversion of strings can result in integers or floats, 7507*0a6a1f1dSLionel Sambucaccording to the lexical conventions of Lua (see <a href="#3.1">§3.1</a>). 7508*0a6a1f1dSLionel Sambuc(The string may have leading and trailing spaces and a sign.) 7509*0a6a1f1dSLionel Sambuc 7510*0a6a1f1dSLionel Sambuc 7511*0a6a1f1dSLionel Sambuc<p> 7512*0a6a1f1dSLionel SambucWhen called with <code>base</code>, 7513*0a6a1f1dSLionel Sambucthen <code>e</code> must be a string to be interpreted as 7514*0a6a1f1dSLionel Sambucan integer numeral in that base. 751511be35a1SLionel SambucThe base may be any integer between 2 and 36, inclusive. 751611be35a1SLionel SambucIn bases above 10, the letter '<code>A</code>' (in either upper or lower case) 751711be35a1SLionel Sambucrepresents 10, '<code>B</code>' represents 11, and so forth, 751811be35a1SLionel Sambucwith '<code>Z</code>' representing 35. 7519*0a6a1f1dSLionel SambucIf the string <code>e</code> is not a valid numeral in the given base, 7520*0a6a1f1dSLionel Sambucthe function returns <b>nil</b>. 752111be35a1SLionel Sambuc 752211be35a1SLionel Sambuc 752311be35a1SLionel Sambuc 752411be35a1SLionel Sambuc 752511be35a1SLionel Sambuc<p> 7526*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-tostring"><code>tostring (v)</code></a></h3> 7527*0a6a1f1dSLionel SambucReceives a value of any type and 7528*0a6a1f1dSLionel Sambucconverts it to a string in a human-readable format. 7529*0a6a1f1dSLionel Sambuc(For complete control of how numbers are converted, 7530*0a6a1f1dSLionel Sambucuse <a href="#pdf-string.format"><code>string.format</code></a>.) 753111be35a1SLionel Sambuc 753211be35a1SLionel Sambuc 753311be35a1SLionel Sambuc<p> 7534*0a6a1f1dSLionel SambucIf the metatable of <code>v</code> has a <code>"__tostring"</code> field, 753511be35a1SLionel Sambucthen <code>tostring</code> calls the corresponding value 7536*0a6a1f1dSLionel Sambucwith <code>v</code> as argument, 753711be35a1SLionel Sambucand uses the result of the call as its result. 753811be35a1SLionel Sambuc 753911be35a1SLionel Sambuc 754011be35a1SLionel Sambuc 754111be35a1SLionel Sambuc 754211be35a1SLionel Sambuc<p> 754311be35a1SLionel Sambuc<hr><h3><a name="pdf-type"><code>type (v)</code></a></h3> 754411be35a1SLionel SambucReturns the type of its only argument, coded as a string. 754511be35a1SLionel SambucThe possible results of this function are 754611be35a1SLionel Sambuc"<code>nil</code>" (a string, not the value <b>nil</b>), 754711be35a1SLionel Sambuc"<code>number</code>", 754811be35a1SLionel Sambuc"<code>string</code>", 754911be35a1SLionel Sambuc"<code>boolean</code>", 755011be35a1SLionel Sambuc"<code>table</code>", 755111be35a1SLionel Sambuc"<code>function</code>", 755211be35a1SLionel Sambuc"<code>thread</code>", 755311be35a1SLionel Sambucand "<code>userdata</code>". 755411be35a1SLionel Sambuc 755511be35a1SLionel Sambuc 755611be35a1SLionel Sambuc 755711be35a1SLionel Sambuc 755811be35a1SLionel Sambuc<p> 755911be35a1SLionel Sambuc<hr><h3><a name="pdf-_VERSION"><code>_VERSION</code></a></h3> 756011be35a1SLionel SambucA global variable (not a function) that 756111be35a1SLionel Sambucholds a string containing the current interpreter version. 7562*0a6a1f1dSLionel SambucThe current value of this variable is "<code>Lua 5.3</code>". 756311be35a1SLionel Sambuc 756411be35a1SLionel Sambuc 756511be35a1SLionel Sambuc 756611be35a1SLionel Sambuc 756711be35a1SLionel Sambuc<p> 7568*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-xpcall"><code>xpcall (f, msgh [, arg1, ···])</code></a></h3> 756911be35a1SLionel Sambuc 757011be35a1SLionel Sambuc 757111be35a1SLionel Sambuc<p> 757211be35a1SLionel SambucThis function is similar to <a href="#pdf-pcall"><code>pcall</code></a>, 7573*0a6a1f1dSLionel Sambucexcept that it sets a new message handler <code>msgh</code>. 757411be35a1SLionel Sambuc 757511be35a1SLionel Sambuc 7576*0a6a1f1dSLionel Sambuc 7577*0a6a1f1dSLionel Sambuc 7578*0a6a1f1dSLionel Sambuc 7579*0a6a1f1dSLionel Sambuc 7580*0a6a1f1dSLionel Sambuc 7581*0a6a1f1dSLionel Sambuc<h2>6.2 – <a name="6.2">Coroutine Manipulation</a></h2> 7582*0a6a1f1dSLionel Sambuc 758311be35a1SLionel Sambuc<p> 7584*0a6a1f1dSLionel SambucThis library comprises the operations to manipulate coroutines, 7585*0a6a1f1dSLionel Sambucwhich come inside the table <a name="pdf-coroutine"><code>coroutine</code></a>. 7586*0a6a1f1dSLionel SambucSee <a href="#2.6">§2.6</a> for a general description of coroutines. 758711be35a1SLionel Sambuc 758811be35a1SLionel Sambuc 758911be35a1SLionel Sambuc<p> 759011be35a1SLionel Sambuc<hr><h3><a name="pdf-coroutine.create"><code>coroutine.create (f)</code></a></h3> 759111be35a1SLionel Sambuc 759211be35a1SLionel Sambuc 759311be35a1SLionel Sambuc<p> 759411be35a1SLionel SambucCreates a new coroutine, with body <code>f</code>. 7595*0a6a1f1dSLionel Sambuc<code>f</code> must be a function. 759611be35a1SLionel SambucReturns this new coroutine, 759711be35a1SLionel Sambucan object with type <code>"thread"</code>. 759811be35a1SLionel Sambuc 759911be35a1SLionel Sambuc 760011be35a1SLionel Sambuc 760111be35a1SLionel Sambuc 760211be35a1SLionel Sambuc<p> 7603*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-coroutine.isyieldable"><code>coroutine.isyieldable ()</code></a></h3> 7604*0a6a1f1dSLionel Sambuc 7605*0a6a1f1dSLionel Sambuc 7606*0a6a1f1dSLionel Sambuc<p> 7607*0a6a1f1dSLionel SambucReturns true when the running coroutine can yield. 7608*0a6a1f1dSLionel Sambuc 7609*0a6a1f1dSLionel Sambuc 7610*0a6a1f1dSLionel Sambuc<p> 7611*0a6a1f1dSLionel SambucA running coroutine is yieldable if it is not the main thread and 7612*0a6a1f1dSLionel Sambucit is not inside a non-yieldable C function. 7613*0a6a1f1dSLionel Sambuc 7614*0a6a1f1dSLionel Sambuc 7615*0a6a1f1dSLionel Sambuc 7616*0a6a1f1dSLionel Sambuc 7617*0a6a1f1dSLionel Sambuc<p> 761811be35a1SLionel Sambuc<hr><h3><a name="pdf-coroutine.resume"><code>coroutine.resume (co [, val1, ···])</code></a></h3> 761911be35a1SLionel Sambuc 762011be35a1SLionel Sambuc 762111be35a1SLionel Sambuc<p> 762211be35a1SLionel SambucStarts or continues the execution of coroutine <code>co</code>. 762311be35a1SLionel SambucThe first time you resume a coroutine, 762411be35a1SLionel Sambucit starts running its body. 7625*0a6a1f1dSLionel SambucThe values <code>val1</code>, ... are passed 762611be35a1SLionel Sambucas the arguments to the body function. 762711be35a1SLionel SambucIf the coroutine has yielded, 762811be35a1SLionel Sambuc<code>resume</code> restarts it; 7629*0a6a1f1dSLionel Sambucthe values <code>val1</code>, ... are passed 763011be35a1SLionel Sambucas the results from the yield. 763111be35a1SLionel Sambuc 763211be35a1SLionel Sambuc 763311be35a1SLionel Sambuc<p> 763411be35a1SLionel SambucIf the coroutine runs without any errors, 763511be35a1SLionel Sambuc<code>resume</code> returns <b>true</b> plus any values passed to <code>yield</code> 7636*0a6a1f1dSLionel Sambuc(when the coroutine yields) or any values returned by the body function 7637*0a6a1f1dSLionel Sambuc(when the coroutine terminates). 763811be35a1SLionel SambucIf there is any error, 763911be35a1SLionel Sambuc<code>resume</code> returns <b>false</b> plus the error message. 764011be35a1SLionel Sambuc 764111be35a1SLionel Sambuc 764211be35a1SLionel Sambuc 764311be35a1SLionel Sambuc 764411be35a1SLionel Sambuc<p> 764511be35a1SLionel Sambuc<hr><h3><a name="pdf-coroutine.running"><code>coroutine.running ()</code></a></h3> 764611be35a1SLionel Sambuc 764711be35a1SLionel Sambuc 764811be35a1SLionel Sambuc<p> 7649*0a6a1f1dSLionel SambucReturns the running coroutine plus a boolean, 7650*0a6a1f1dSLionel Sambuctrue when the running coroutine is the main one. 765111be35a1SLionel Sambuc 765211be35a1SLionel Sambuc 765311be35a1SLionel Sambuc 765411be35a1SLionel Sambuc 765511be35a1SLionel Sambuc<p> 765611be35a1SLionel Sambuc<hr><h3><a name="pdf-coroutine.status"><code>coroutine.status (co)</code></a></h3> 765711be35a1SLionel Sambuc 765811be35a1SLionel Sambuc 765911be35a1SLionel Sambuc<p> 766011be35a1SLionel SambucReturns the status of coroutine <code>co</code>, as a string: 766111be35a1SLionel Sambuc<code>"running"</code>, 766211be35a1SLionel Sambucif the coroutine is running (that is, it called <code>status</code>); 766311be35a1SLionel Sambuc<code>"suspended"</code>, if the coroutine is suspended in a call to <code>yield</code>, 766411be35a1SLionel Sambucor if it has not started running yet; 766511be35a1SLionel Sambuc<code>"normal"</code> if the coroutine is active but not running 766611be35a1SLionel Sambuc(that is, it has resumed another coroutine); 766711be35a1SLionel Sambucand <code>"dead"</code> if the coroutine has finished its body function, 766811be35a1SLionel Sambucor if it has stopped with an error. 766911be35a1SLionel Sambuc 767011be35a1SLionel Sambuc 767111be35a1SLionel Sambuc 767211be35a1SLionel Sambuc 767311be35a1SLionel Sambuc<p> 767411be35a1SLionel Sambuc<hr><h3><a name="pdf-coroutine.wrap"><code>coroutine.wrap (f)</code></a></h3> 767511be35a1SLionel Sambuc 767611be35a1SLionel Sambuc 767711be35a1SLionel Sambuc<p> 767811be35a1SLionel SambucCreates a new coroutine, with body <code>f</code>. 7679*0a6a1f1dSLionel Sambuc<code>f</code> must be a function. 768011be35a1SLionel SambucReturns a function that resumes the coroutine each time it is called. 768111be35a1SLionel SambucAny arguments passed to the function behave as the 768211be35a1SLionel Sambucextra arguments to <code>resume</code>. 768311be35a1SLionel SambucReturns the same values returned by <code>resume</code>, 768411be35a1SLionel Sambucexcept the first boolean. 768511be35a1SLionel SambucIn case of error, propagates the error. 768611be35a1SLionel Sambuc 768711be35a1SLionel Sambuc 768811be35a1SLionel Sambuc 768911be35a1SLionel Sambuc 769011be35a1SLionel Sambuc<p> 769111be35a1SLionel Sambuc<hr><h3><a name="pdf-coroutine.yield"><code>coroutine.yield (···)</code></a></h3> 769211be35a1SLionel Sambuc 769311be35a1SLionel Sambuc 769411be35a1SLionel Sambuc<p> 769511be35a1SLionel SambucSuspends the execution of the calling coroutine. 769611be35a1SLionel SambucAny arguments to <code>yield</code> are passed as extra results to <code>resume</code>. 769711be35a1SLionel Sambuc 769811be35a1SLionel Sambuc 769911be35a1SLionel Sambuc 770011be35a1SLionel Sambuc 770111be35a1SLionel Sambuc 770211be35a1SLionel Sambuc 770311be35a1SLionel Sambuc 7704*0a6a1f1dSLionel Sambuc<h2>6.3 – <a name="6.3">Modules</a></h2> 770511be35a1SLionel Sambuc 770611be35a1SLionel Sambuc<p> 770711be35a1SLionel SambucThe package library provides basic 7708*0a6a1f1dSLionel Sambucfacilities for loading modules in Lua. 7709*0a6a1f1dSLionel SambucIt exports one function directly in the global environment: 7710*0a6a1f1dSLionel Sambuc<a href="#pdf-require"><code>require</code></a>. 771111be35a1SLionel SambucEverything else is exported in a table <a name="pdf-package"><code>package</code></a>. 771211be35a1SLionel Sambuc 771311be35a1SLionel Sambuc 771411be35a1SLionel Sambuc<p> 771511be35a1SLionel Sambuc<hr><h3><a name="pdf-require"><code>require (modname)</code></a></h3> 771611be35a1SLionel Sambuc 771711be35a1SLionel Sambuc 771811be35a1SLionel Sambuc<p> 771911be35a1SLionel SambucLoads the given module. 772011be35a1SLionel SambucThe function starts by looking into the <a href="#pdf-package.loaded"><code>package.loaded</code></a> table 772111be35a1SLionel Sambucto determine whether <code>modname</code> is already loaded. 772211be35a1SLionel SambucIf it is, then <code>require</code> returns the value stored 772311be35a1SLionel Sambucat <code>package.loaded[modname]</code>. 772411be35a1SLionel SambucOtherwise, it tries to find a <em>loader</em> for the module. 772511be35a1SLionel Sambuc 772611be35a1SLionel Sambuc 772711be35a1SLionel Sambuc<p> 772811be35a1SLionel SambucTo find a loader, 7729*0a6a1f1dSLionel Sambuc<code>require</code> is guided by the <a href="#pdf-package.searchers"><code>package.searchers</code></a> sequence. 7730*0a6a1f1dSLionel SambucBy changing this sequence, 773111be35a1SLionel Sambucwe can change how <code>require</code> looks for a module. 773211be35a1SLionel SambucThe following explanation is based on the default configuration 7733*0a6a1f1dSLionel Sambucfor <a href="#pdf-package.searchers"><code>package.searchers</code></a>. 773411be35a1SLionel Sambuc 773511be35a1SLionel Sambuc 773611be35a1SLionel Sambuc<p> 773711be35a1SLionel SambucFirst <code>require</code> queries <code>package.preload[modname]</code>. 773811be35a1SLionel SambucIf it has a value, 7739*0a6a1f1dSLionel Sambucthis value (which must be a function) is the loader. 774011be35a1SLionel SambucOtherwise <code>require</code> searches for a Lua loader using the 774111be35a1SLionel Sambucpath stored in <a href="#pdf-package.path"><code>package.path</code></a>. 774211be35a1SLionel SambucIf that also fails, it searches for a C loader using the 774311be35a1SLionel Sambucpath stored in <a href="#pdf-package.cpath"><code>package.cpath</code></a>. 774411be35a1SLionel SambucIf that also fails, 7745*0a6a1f1dSLionel Sambucit tries an <em>all-in-one</em> loader (see <a href="#pdf-package.searchers"><code>package.searchers</code></a>). 774611be35a1SLionel Sambuc 774711be35a1SLionel Sambuc 774811be35a1SLionel Sambuc<p> 774911be35a1SLionel SambucOnce a loader is found, 7750*0a6a1f1dSLionel Sambuc<code>require</code> calls the loader with two arguments: 7751*0a6a1f1dSLionel Sambuc<code>modname</code> and an extra value dependent on how it got the loader. 7752*0a6a1f1dSLionel Sambuc(If the loader came from a file, 7753*0a6a1f1dSLionel Sambucthis extra value is the file name.) 7754*0a6a1f1dSLionel SambucIf the loader returns any non-nil value, 775511be35a1SLionel Sambuc<code>require</code> assigns the returned value to <code>package.loaded[modname]</code>. 7756*0a6a1f1dSLionel SambucIf the loader does not return a non-nil value and 775711be35a1SLionel Sambuchas not assigned any value to <code>package.loaded[modname]</code>, 775811be35a1SLionel Sambucthen <code>require</code> assigns <b>true</b> to this entry. 775911be35a1SLionel SambucIn any case, <code>require</code> returns the 776011be35a1SLionel Sambucfinal value of <code>package.loaded[modname]</code>. 776111be35a1SLionel Sambuc 776211be35a1SLionel Sambuc 776311be35a1SLionel Sambuc<p> 776411be35a1SLionel SambucIf there is any error loading or running the module, 776511be35a1SLionel Sambucor if it cannot find any loader for the module, 7766*0a6a1f1dSLionel Sambucthen <code>require</code> raises an error. 776711be35a1SLionel Sambuc 776811be35a1SLionel Sambuc 776911be35a1SLionel Sambuc 777011be35a1SLionel Sambuc 777111be35a1SLionel Sambuc<p> 7772*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-package.config"><code>package.config</code></a></h3> 7773*0a6a1f1dSLionel Sambuc 7774*0a6a1f1dSLionel Sambuc 7775*0a6a1f1dSLionel Sambuc<p> 7776*0a6a1f1dSLionel SambucA string describing some compile-time configurations for packages. 7777*0a6a1f1dSLionel SambucThis string is a sequence of lines: 7778*0a6a1f1dSLionel Sambuc 7779*0a6a1f1dSLionel Sambuc<ul> 7780*0a6a1f1dSLionel Sambuc 7781*0a6a1f1dSLionel Sambuc<li>The first line is the directory separator string. 7782*0a6a1f1dSLionel SambucDefault is '<code>\</code>' for Windows and '<code>/</code>' for all other systems.</li> 7783*0a6a1f1dSLionel Sambuc 7784*0a6a1f1dSLionel Sambuc<li>The second line is the character that separates templates in a path. 7785*0a6a1f1dSLionel SambucDefault is '<code>;</code>'.</li> 7786*0a6a1f1dSLionel Sambuc 7787*0a6a1f1dSLionel Sambuc<li>The third line is the string that marks the 7788*0a6a1f1dSLionel Sambucsubstitution points in a template. 7789*0a6a1f1dSLionel SambucDefault is '<code>?</code>'.</li> 7790*0a6a1f1dSLionel Sambuc 7791*0a6a1f1dSLionel Sambuc<li>The fourth line is a string that, in a path in Windows, 7792*0a6a1f1dSLionel Sambucis replaced by the executable's directory. 7793*0a6a1f1dSLionel SambucDefault is '<code>!</code>'.</li> 7794*0a6a1f1dSLionel Sambuc 7795*0a6a1f1dSLionel Sambuc<li>The fifth line is a mark to ignore all text after it 7796*0a6a1f1dSLionel Sambucwhen building the <code>luaopen_</code> function name. 7797*0a6a1f1dSLionel SambucDefault is '<code>-</code>'.</li> 7798*0a6a1f1dSLionel Sambuc 7799*0a6a1f1dSLionel Sambuc</ul> 7800*0a6a1f1dSLionel Sambuc 7801*0a6a1f1dSLionel Sambuc 7802*0a6a1f1dSLionel Sambuc 7803*0a6a1f1dSLionel Sambuc<p> 780411be35a1SLionel Sambuc<hr><h3><a name="pdf-package.cpath"><code>package.cpath</code></a></h3> 780511be35a1SLionel Sambuc 780611be35a1SLionel Sambuc 780711be35a1SLionel Sambuc<p> 780811be35a1SLionel SambucThe path used by <a href="#pdf-require"><code>require</code></a> to search for a C loader. 780911be35a1SLionel Sambuc 781011be35a1SLionel Sambuc 781111be35a1SLionel Sambuc<p> 781211be35a1SLionel SambucLua initializes the C path <a href="#pdf-package.cpath"><code>package.cpath</code></a> in the same way 781311be35a1SLionel Sambucit initializes the Lua path <a href="#pdf-package.path"><code>package.path</code></a>, 7814*0a6a1f1dSLionel Sambucusing the environment variable <a name="pdf-LUA_CPATH_5_3"><code>LUA_CPATH_5_3</code></a> 7815*0a6a1f1dSLionel Sambucor the environment variable <a name="pdf-LUA_CPATH"><code>LUA_CPATH</code></a> 781611be35a1SLionel Sambucor a default path defined in <code>luaconf.h</code>. 781711be35a1SLionel Sambuc 781811be35a1SLionel Sambuc 781911be35a1SLionel Sambuc 782011be35a1SLionel Sambuc 782111be35a1SLionel Sambuc<p> 782211be35a1SLionel Sambuc<hr><h3><a name="pdf-package.loaded"><code>package.loaded</code></a></h3> 782311be35a1SLionel Sambuc 782411be35a1SLionel Sambuc 782511be35a1SLionel Sambuc<p> 782611be35a1SLionel SambucA table used by <a href="#pdf-require"><code>require</code></a> to control which 782711be35a1SLionel Sambucmodules are already loaded. 782811be35a1SLionel SambucWhen you require a module <code>modname</code> and 782911be35a1SLionel Sambuc<code>package.loaded[modname]</code> is not false, 783011be35a1SLionel Sambuc<a href="#pdf-require"><code>require</code></a> simply returns the value stored there. 783111be35a1SLionel Sambuc 783211be35a1SLionel Sambuc 7833*0a6a1f1dSLionel Sambuc<p> 7834*0a6a1f1dSLionel SambucThis variable is only a reference to the real table; 7835*0a6a1f1dSLionel Sambucassignments to this variable do not change the 7836*0a6a1f1dSLionel Sambuctable used by <a href="#pdf-require"><code>require</code></a>. 7837*0a6a1f1dSLionel Sambuc 7838*0a6a1f1dSLionel Sambuc 783911be35a1SLionel Sambuc 784011be35a1SLionel Sambuc 784111be35a1SLionel Sambuc<p> 7842*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-package.loadlib"><code>package.loadlib (libname, funcname)</code></a></h3> 7843*0a6a1f1dSLionel Sambuc 7844*0a6a1f1dSLionel Sambuc 7845*0a6a1f1dSLionel Sambuc<p> 7846*0a6a1f1dSLionel SambucDynamically links the host program with the C library <code>libname</code>. 7847*0a6a1f1dSLionel Sambuc 7848*0a6a1f1dSLionel Sambuc 7849*0a6a1f1dSLionel Sambuc<p> 7850*0a6a1f1dSLionel SambucIf <code>funcname</code> is "<code>*</code>", 7851*0a6a1f1dSLionel Sambucthen it only links with the library, 7852*0a6a1f1dSLionel Sambucmaking the symbols exported by the library 7853*0a6a1f1dSLionel Sambucavailable to other dynamically linked libraries. 7854*0a6a1f1dSLionel SambucOtherwise, 7855*0a6a1f1dSLionel Sambucit looks for a function <code>funcname</code> inside the library 7856*0a6a1f1dSLionel Sambucand returns this function as a C function. 7857*0a6a1f1dSLionel SambucSo, <code>funcname</code> must follow the <a href="#lua_CFunction"><code>lua_CFunction</code></a> prototype 7858*0a6a1f1dSLionel Sambuc(see <a href="#lua_CFunction"><code>lua_CFunction</code></a>). 7859*0a6a1f1dSLionel Sambuc 7860*0a6a1f1dSLionel Sambuc 7861*0a6a1f1dSLionel Sambuc<p> 7862*0a6a1f1dSLionel SambucThis is a low-level function. 7863*0a6a1f1dSLionel SambucIt completely bypasses the package and module system. 7864*0a6a1f1dSLionel SambucUnlike <a href="#pdf-require"><code>require</code></a>, 7865*0a6a1f1dSLionel Sambucit does not perform any path searching and 7866*0a6a1f1dSLionel Sambucdoes not automatically adds extensions. 7867*0a6a1f1dSLionel Sambuc<code>libname</code> must be the complete file name of the C library, 7868*0a6a1f1dSLionel Sambucincluding if necessary a path and an extension. 7869*0a6a1f1dSLionel Sambuc<code>funcname</code> must be the exact name exported by the C library 7870*0a6a1f1dSLionel Sambuc(which may depend on the C compiler and linker used). 7871*0a6a1f1dSLionel Sambuc 7872*0a6a1f1dSLionel Sambuc 7873*0a6a1f1dSLionel Sambuc<p> 7874*0a6a1f1dSLionel SambucThis function is not supported by Standard C. 7875*0a6a1f1dSLionel SambucAs such, it is only available on some platforms 7876*0a6a1f1dSLionel Sambuc(Windows, Linux, Mac OS X, Solaris, BSD, 7877*0a6a1f1dSLionel Sambucplus other Unix systems that support the <code>dlfcn</code> standard). 7878*0a6a1f1dSLionel Sambuc 7879*0a6a1f1dSLionel Sambuc 7880*0a6a1f1dSLionel Sambuc 7881*0a6a1f1dSLionel Sambuc 7882*0a6a1f1dSLionel Sambuc<p> 7883*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-package.path"><code>package.path</code></a></h3> 7884*0a6a1f1dSLionel Sambuc 7885*0a6a1f1dSLionel Sambuc 7886*0a6a1f1dSLionel Sambuc<p> 7887*0a6a1f1dSLionel SambucThe path used by <a href="#pdf-require"><code>require</code></a> to search for a Lua loader. 7888*0a6a1f1dSLionel Sambuc 7889*0a6a1f1dSLionel Sambuc 7890*0a6a1f1dSLionel Sambuc<p> 7891*0a6a1f1dSLionel SambucAt start-up, Lua initializes this variable with 7892*0a6a1f1dSLionel Sambucthe value of the environment variable <a name="pdf-LUA_PATH_5_3"><code>LUA_PATH_5_3</code></a> or 7893*0a6a1f1dSLionel Sambucthe environment variable <a name="pdf-LUA_PATH"><code>LUA_PATH</code></a> or 7894*0a6a1f1dSLionel Sambucwith a default path defined in <code>luaconf.h</code>, 7895*0a6a1f1dSLionel Sambucif those environment variables are not defined. 7896*0a6a1f1dSLionel SambucAny "<code>;;</code>" in the value of the environment variable 7897*0a6a1f1dSLionel Sambucis replaced by the default path. 7898*0a6a1f1dSLionel Sambuc 7899*0a6a1f1dSLionel Sambuc 7900*0a6a1f1dSLionel Sambuc 7901*0a6a1f1dSLionel Sambuc 7902*0a6a1f1dSLionel Sambuc<p> 7903*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-package.preload"><code>package.preload</code></a></h3> 7904*0a6a1f1dSLionel Sambuc 7905*0a6a1f1dSLionel Sambuc 7906*0a6a1f1dSLionel Sambuc<p> 7907*0a6a1f1dSLionel SambucA table to store loaders for specific modules 7908*0a6a1f1dSLionel Sambuc(see <a href="#pdf-require"><code>require</code></a>). 7909*0a6a1f1dSLionel Sambuc 7910*0a6a1f1dSLionel Sambuc 7911*0a6a1f1dSLionel Sambuc<p> 7912*0a6a1f1dSLionel SambucThis variable is only a reference to the real table; 7913*0a6a1f1dSLionel Sambucassignments to this variable do not change the 7914*0a6a1f1dSLionel Sambuctable used by <a href="#pdf-require"><code>require</code></a>. 7915*0a6a1f1dSLionel Sambuc 7916*0a6a1f1dSLionel Sambuc 7917*0a6a1f1dSLionel Sambuc 7918*0a6a1f1dSLionel Sambuc 7919*0a6a1f1dSLionel Sambuc<p> 7920*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-package.searchers"><code>package.searchers</code></a></h3> 792111be35a1SLionel Sambuc 792211be35a1SLionel Sambuc 792311be35a1SLionel Sambuc<p> 792411be35a1SLionel SambucA table used by <a href="#pdf-require"><code>require</code></a> to control how to load modules. 792511be35a1SLionel Sambuc 792611be35a1SLionel Sambuc 792711be35a1SLionel Sambuc<p> 792811be35a1SLionel SambucEach entry in this table is a <em>searcher function</em>. 792911be35a1SLionel SambucWhen looking for a module, 793011be35a1SLionel Sambuc<a href="#pdf-require"><code>require</code></a> calls each of these searchers in ascending order, 793111be35a1SLionel Sambucwith the module name (the argument given to <a href="#pdf-require"><code>require</code></a>) as its 793211be35a1SLionel Sambucsole parameter. 793311be35a1SLionel SambucThe function can return another function (the module <em>loader</em>) 7934*0a6a1f1dSLionel Sambucplus an extra value that will be passed to that loader, 793511be35a1SLionel Sambucor a string explaining why it did not find that module 793611be35a1SLionel Sambuc(or <b>nil</b> if it has nothing to say). 7937*0a6a1f1dSLionel Sambuc 7938*0a6a1f1dSLionel Sambuc 7939*0a6a1f1dSLionel Sambuc<p> 7940*0a6a1f1dSLionel SambucLua initializes this table with four searcher functions. 794111be35a1SLionel Sambuc 794211be35a1SLionel Sambuc 794311be35a1SLionel Sambuc<p> 794411be35a1SLionel SambucThe first searcher simply looks for a loader in the 794511be35a1SLionel Sambuc<a href="#pdf-package.preload"><code>package.preload</code></a> table. 794611be35a1SLionel Sambuc 794711be35a1SLionel Sambuc 794811be35a1SLionel Sambuc<p> 794911be35a1SLionel SambucThe second searcher looks for a loader as a Lua library, 795011be35a1SLionel Sambucusing the path stored at <a href="#pdf-package.path"><code>package.path</code></a>. 7951*0a6a1f1dSLionel SambucThe search is done as described in function <a href="#pdf-package.searchpath"><code>package.searchpath</code></a>. 795211be35a1SLionel Sambuc 795311be35a1SLionel Sambuc 795411be35a1SLionel Sambuc<p> 795511be35a1SLionel SambucThe third searcher looks for a loader as a C library, 795611be35a1SLionel Sambucusing the path given by the variable <a href="#pdf-package.cpath"><code>package.cpath</code></a>. 7957*0a6a1f1dSLionel SambucAgain, 7958*0a6a1f1dSLionel Sambucthe search is done as described in function <a href="#pdf-package.searchpath"><code>package.searchpath</code></a>. 795911be35a1SLionel SambucFor instance, 796011be35a1SLionel Sambucif the C path is the string 796111be35a1SLionel Sambuc 796211be35a1SLionel Sambuc<pre> 796311be35a1SLionel Sambuc "./?.so;./?.dll;/usr/local/?/init.so" 796411be35a1SLionel Sambuc</pre><p> 796511be35a1SLionel Sambucthe searcher for module <code>foo</code> 796611be35a1SLionel Sambucwill try to open the files <code>./foo.so</code>, <code>./foo.dll</code>, 796711be35a1SLionel Sambucand <code>/usr/local/foo/init.so</code>, in that order. 796811be35a1SLionel SambucOnce it finds a C library, 796911be35a1SLionel Sambucthis searcher first uses a dynamic link facility to link the 797011be35a1SLionel Sambucapplication with the library. 797111be35a1SLionel SambucThen it tries to find a C function inside the library to 797211be35a1SLionel Sambucbe used as the loader. 797311be35a1SLionel SambucThe name of this C function is the string "<code>luaopen_</code>" 797411be35a1SLionel Sambucconcatenated with a copy of the module name where each dot 797511be35a1SLionel Sambucis replaced by an underscore. 797611be35a1SLionel SambucMoreover, if the module name has a hyphen, 7977*0a6a1f1dSLionel Sambucits suffix after (and including) the first hyphen is removed. 7978*0a6a1f1dSLionel SambucFor instance, if the module name is <code>a.b.c-v2.1</code>, 7979*0a6a1f1dSLionel Sambucthe function name will be <code>luaopen_a_b_c</code>. 798011be35a1SLionel Sambuc 798111be35a1SLionel Sambuc 798211be35a1SLionel Sambuc<p> 798311be35a1SLionel SambucThe fourth searcher tries an <em>all-in-one loader</em>. 798411be35a1SLionel SambucIt searches the C path for a library for 798511be35a1SLionel Sambucthe root name of the given module. 798611be35a1SLionel SambucFor instance, when requiring <code>a.b.c</code>, 798711be35a1SLionel Sambucit will search for a C library for <code>a</code>. 798811be35a1SLionel SambucIf found, it looks into it for an open function for 798911be35a1SLionel Sambucthe submodule; 799011be35a1SLionel Sambucin our example, that would be <code>luaopen_a_b_c</code>. 799111be35a1SLionel SambucWith this facility, a package can pack several C submodules 799211be35a1SLionel Sambucinto one single library, 799311be35a1SLionel Sambucwith each submodule keeping its original open function. 799411be35a1SLionel Sambuc 799511be35a1SLionel Sambuc 799611be35a1SLionel Sambuc<p> 7997*0a6a1f1dSLionel SambucAll searchers except the first one (preload) return as the extra value 7998*0a6a1f1dSLionel Sambucthe file name where the module was found, 7999*0a6a1f1dSLionel Sambucas returned by <a href="#pdf-package.searchpath"><code>package.searchpath</code></a>. 8000*0a6a1f1dSLionel SambucThe first searcher returns no extra value. 800111be35a1SLionel Sambuc 800211be35a1SLionel Sambuc 800311be35a1SLionel Sambuc 800411be35a1SLionel Sambuc 800511be35a1SLionel Sambuc<p> 8006*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-package.searchpath"><code>package.searchpath (name, path [, sep [, rep]])</code></a></h3> 800711be35a1SLionel Sambuc 800811be35a1SLionel Sambuc 800911be35a1SLionel Sambuc<p> 8010*0a6a1f1dSLionel SambucSearches for the given <code>name</code> in the given <code>path</code>. 801111be35a1SLionel Sambuc 801211be35a1SLionel Sambuc 801311be35a1SLionel Sambuc<p> 8014*0a6a1f1dSLionel SambucA path is a string containing a sequence of 8015*0a6a1f1dSLionel Sambuc<em>templates</em> separated by semicolons. 8016*0a6a1f1dSLionel SambucFor each template, 8017*0a6a1f1dSLionel Sambucthe function replaces each interrogation mark (if any) 8018*0a6a1f1dSLionel Sambucin the template with a copy of <code>name</code> 8019*0a6a1f1dSLionel Sambucwherein all occurrences of <code>sep</code> 8020*0a6a1f1dSLionel Sambuc(a dot, by default) 8021*0a6a1f1dSLionel Sambucwere replaced by <code>rep</code> 8022*0a6a1f1dSLionel Sambuc(the system's directory separator, by default), 8023*0a6a1f1dSLionel Sambucand then tries to open the resulting file name. 802411be35a1SLionel Sambuc 802511be35a1SLionel Sambuc 802611be35a1SLionel Sambuc<p> 8027*0a6a1f1dSLionel SambucFor instance, if the path is the string 8028*0a6a1f1dSLionel Sambuc 8029*0a6a1f1dSLionel Sambuc<pre> 8030*0a6a1f1dSLionel Sambuc "./?.lua;./?.lc;/usr/local/?/init.lua" 8031*0a6a1f1dSLionel Sambuc</pre><p> 8032*0a6a1f1dSLionel Sambucthe search for the name <code>foo.a</code> 8033*0a6a1f1dSLionel Sambucwill try to open the files 8034*0a6a1f1dSLionel Sambuc<code>./foo/a.lua</code>, <code>./foo/a.lc</code>, and 8035*0a6a1f1dSLionel Sambuc<code>/usr/local/foo/a/init.lua</code>, in that order. 803611be35a1SLionel Sambuc 803711be35a1SLionel Sambuc 803811be35a1SLionel Sambuc<p> 8039*0a6a1f1dSLionel SambucReturns the resulting name of the first file that it can 8040*0a6a1f1dSLionel Sambucopen in read mode (after closing the file), 8041*0a6a1f1dSLionel Sambucor <b>nil</b> plus an error message if none succeeds. 8042*0a6a1f1dSLionel Sambuc(This error message lists all file names it tried to open.) 804311be35a1SLionel Sambuc 804411be35a1SLionel Sambuc 804511be35a1SLionel Sambuc 804611be35a1SLionel Sambuc 804711be35a1SLionel Sambuc 804811be35a1SLionel Sambuc 804911be35a1SLionel Sambuc 8050*0a6a1f1dSLionel Sambuc<h2>6.4 – <a name="6.4">String Manipulation</a></h2> 805111be35a1SLionel Sambuc 805211be35a1SLionel Sambuc<p> 805311be35a1SLionel SambucThis library provides generic functions for string manipulation, 805411be35a1SLionel Sambucsuch as finding and extracting substrings, and pattern matching. 805511be35a1SLionel SambucWhen indexing a string in Lua, the first character is at position 1 805611be35a1SLionel Sambuc(not at 0, as in C). 805711be35a1SLionel SambucIndices are allowed to be negative and are interpreted as indexing backwards, 805811be35a1SLionel Sambucfrom the end of the string. 805911be35a1SLionel SambucThus, the last character is at position -1, and so on. 806011be35a1SLionel Sambuc 806111be35a1SLionel Sambuc 806211be35a1SLionel Sambuc<p> 806311be35a1SLionel SambucThe string library provides all its functions inside the table 806411be35a1SLionel Sambuc<a name="pdf-string"><code>string</code></a>. 806511be35a1SLionel SambucIt also sets a metatable for strings 806611be35a1SLionel Sambucwhere the <code>__index</code> field points to the <code>string</code> table. 806711be35a1SLionel SambucTherefore, you can use the string functions in object-oriented style. 806811be35a1SLionel SambucFor instance, <code>string.byte(s,i)</code> 806911be35a1SLionel Sambuccan be written as <code>s:byte(i)</code>. 807011be35a1SLionel Sambuc 807111be35a1SLionel Sambuc 807211be35a1SLionel Sambuc<p> 807311be35a1SLionel SambucThe string library assumes one-byte character encodings. 807411be35a1SLionel Sambuc 807511be35a1SLionel Sambuc 807611be35a1SLionel Sambuc<p> 807711be35a1SLionel Sambuc<hr><h3><a name="pdf-string.byte"><code>string.byte (s [, i [, j]])</code></a></h3> 8078*0a6a1f1dSLionel SambucReturns the internal numeric codes of the characters <code>s[i]</code>, 8079*0a6a1f1dSLionel Sambuc<code>s[i+1]</code>, ..., <code>s[j]</code>. 808011be35a1SLionel SambucThe default value for <code>i</code> is 1; 808111be35a1SLionel Sambucthe default value for <code>j</code> is <code>i</code>. 8082*0a6a1f1dSLionel SambucThese indices are corrected 8083*0a6a1f1dSLionel Sambucfollowing the same rules of function <a href="#pdf-string.sub"><code>string.sub</code></a>. 808411be35a1SLionel Sambuc 808511be35a1SLionel Sambuc 808611be35a1SLionel Sambuc<p> 8087*0a6a1f1dSLionel SambucNumeric codes are not necessarily portable across platforms. 808811be35a1SLionel Sambuc 808911be35a1SLionel Sambuc 809011be35a1SLionel Sambuc 809111be35a1SLionel Sambuc 809211be35a1SLionel Sambuc<p> 809311be35a1SLionel Sambuc<hr><h3><a name="pdf-string.char"><code>string.char (···)</code></a></h3> 809411be35a1SLionel SambucReceives zero or more integers. 809511be35a1SLionel SambucReturns a string with length equal to the number of arguments, 8096*0a6a1f1dSLionel Sambucin which each character has the internal numeric code equal 809711be35a1SLionel Sambucto its corresponding argument. 809811be35a1SLionel Sambuc 809911be35a1SLionel Sambuc 810011be35a1SLionel Sambuc<p> 8101*0a6a1f1dSLionel SambucNumeric codes are not necessarily portable across platforms. 810211be35a1SLionel Sambuc 810311be35a1SLionel Sambuc 810411be35a1SLionel Sambuc 810511be35a1SLionel Sambuc 810611be35a1SLionel Sambuc<p> 8107*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-string.dump"><code>string.dump (function [, strip])</code></a></h3> 810811be35a1SLionel Sambuc 810911be35a1SLionel Sambuc 811011be35a1SLionel Sambuc<p> 8111*0a6a1f1dSLionel SambucReturns a string containing a binary representation 8112*0a6a1f1dSLionel Sambuc(a <em>binary chunk</em>) 8113*0a6a1f1dSLionel Sambucof the given function, 8114*0a6a1f1dSLionel Sambucso that a later <a href="#pdf-load"><code>load</code></a> on this string returns 8115*0a6a1f1dSLionel Sambuca copy of the function (but with new upvalues). 8116*0a6a1f1dSLionel SambucIf <code>strip</code> is a true value, 8117*0a6a1f1dSLionel Sambucthe binary representation may not include all debug information 8118*0a6a1f1dSLionel Sambucabout the function, 8119*0a6a1f1dSLionel Sambucto save space. 8120*0a6a1f1dSLionel Sambuc 8121*0a6a1f1dSLionel Sambuc 8122*0a6a1f1dSLionel Sambuc<p> 8123*0a6a1f1dSLionel SambucFunctions with upvalues have only their number of upvalues saved. 8124*0a6a1f1dSLionel SambucWhen (re)loaded, 8125*0a6a1f1dSLionel Sambucthose upvalues receive fresh instances containing <b>nil</b>. 8126*0a6a1f1dSLionel Sambuc(You can use the debug library to serialize 8127*0a6a1f1dSLionel Sambucand reload the upvalues of a function 8128*0a6a1f1dSLionel Sambucin a way adequate to your needs.) 812911be35a1SLionel Sambuc 813011be35a1SLionel Sambuc 813111be35a1SLionel Sambuc 813211be35a1SLionel Sambuc 813311be35a1SLionel Sambuc<p> 813411be35a1SLionel Sambuc<hr><h3><a name="pdf-string.find"><code>string.find (s, pattern [, init [, plain]])</code></a></h3> 8135*0a6a1f1dSLionel Sambuc 8136*0a6a1f1dSLionel Sambuc 8137*0a6a1f1dSLionel Sambuc<p> 813811be35a1SLionel SambucLooks for the first match of 8139*0a6a1f1dSLionel Sambuc<code>pattern</code> (see <a href="#6.4.1">§6.4.1</a>) in the string <code>s</code>. 814011be35a1SLionel SambucIf it finds a match, then <code>find</code> returns the indices of <code>s</code> 814111be35a1SLionel Sambucwhere this occurrence starts and ends; 814211be35a1SLionel Sambucotherwise, it returns <b>nil</b>. 8143*0a6a1f1dSLionel SambucA third, optional numeric argument <code>init</code> specifies 814411be35a1SLionel Sambucwhere to start the search; 814511be35a1SLionel Sambucits default value is 1 and can be negative. 814611be35a1SLionel SambucA value of <b>true</b> as a fourth, optional argument <code>plain</code> 814711be35a1SLionel Sambucturns off the pattern matching facilities, 814811be35a1SLionel Sambucso the function does a plain "find substring" operation, 8149*0a6a1f1dSLionel Sambucwith no characters in <code>pattern</code> being considered magic. 815011be35a1SLionel SambucNote that if <code>plain</code> is given, then <code>init</code> must be given as well. 815111be35a1SLionel Sambuc 815211be35a1SLionel Sambuc 815311be35a1SLionel Sambuc<p> 815411be35a1SLionel SambucIf the pattern has captures, 815511be35a1SLionel Sambucthen in a successful match 815611be35a1SLionel Sambucthe captured values are also returned, 815711be35a1SLionel Sambucafter the two indices. 815811be35a1SLionel Sambuc 815911be35a1SLionel Sambuc 816011be35a1SLionel Sambuc 816111be35a1SLionel Sambuc 816211be35a1SLionel Sambuc<p> 816311be35a1SLionel Sambuc<hr><h3><a name="pdf-string.format"><code>string.format (formatstring, ···)</code></a></h3> 8164*0a6a1f1dSLionel Sambuc 8165*0a6a1f1dSLionel Sambuc 8166*0a6a1f1dSLionel Sambuc<p> 816711be35a1SLionel SambucReturns a formatted version of its variable number of arguments 816811be35a1SLionel Sambucfollowing the description given in its first argument (which must be a string). 8169*0a6a1f1dSLionel SambucThe format string follows the same rules as the ISO C function <code>sprintf</code>. 817011be35a1SLionel SambucThe only differences are that the options/modifiers 8171*0a6a1f1dSLionel Sambuc<code>*</code>, <code>h</code>, <code>L</code>, <code>l</code>, <code>n</code>, 8172*0a6a1f1dSLionel Sambucand <code>p</code> are not supported 817311be35a1SLionel Sambucand that there is an extra option, <code>q</code>. 8174*0a6a1f1dSLionel SambucThe <code>q</code> option formats a string between double quotes, 8175*0a6a1f1dSLionel Sambucusing escape sequences when necessary to ensure that 8176*0a6a1f1dSLionel Sambucit can safely be read back by the Lua interpreter. 817711be35a1SLionel SambucFor instance, the call 817811be35a1SLionel Sambuc 817911be35a1SLionel Sambuc<pre> 818011be35a1SLionel Sambuc string.format('%q', 'a string with "quotes" and \n new line') 818111be35a1SLionel Sambuc</pre><p> 8182*0a6a1f1dSLionel Sambucmay produce the string: 818311be35a1SLionel Sambuc 818411be35a1SLionel Sambuc<pre> 818511be35a1SLionel Sambuc "a string with \"quotes\" and \ 818611be35a1SLionel Sambuc new line" 818711be35a1SLionel Sambuc</pre> 818811be35a1SLionel Sambuc 818911be35a1SLionel Sambuc<p> 8190*0a6a1f1dSLionel SambucOptions 8191*0a6a1f1dSLionel Sambuc<code>A</code>, <code>a</code>, <code>E</code>, <code>e</code>, <code>f</code>, 8192*0a6a1f1dSLionel Sambuc<code>G</code>, and <code>g</code> all expect a number as argument. 8193*0a6a1f1dSLionel SambucOptions <code>c</code>, <code>d</code>, 8194*0a6a1f1dSLionel Sambuc<code>i</code>, <code>o</code>, <code>u</code>, <code>X</code>, and <code>x</code> 8195*0a6a1f1dSLionel Sambucexpect an integer. 8196*0a6a1f1dSLionel SambucOption <code>q</code> expects a string. 8197*0a6a1f1dSLionel SambucOption <code>s</code> expects a string without embedded zeros; 8198*0a6a1f1dSLionel Sambucif its argument is not a string, 8199*0a6a1f1dSLionel Sambucit is converted to one following the same rules of <a href="#pdf-tostring"><code>tostring</code></a>. 820011be35a1SLionel Sambuc 820111be35a1SLionel Sambuc 820211be35a1SLionel Sambuc<p> 8203*0a6a1f1dSLionel SambucWhen Lua is compiled with a non-C99 compiler, 8204*0a6a1f1dSLionel Sambucoptions <code>A</code> and <code>a</code> (hexadecimal floats) 8205*0a6a1f1dSLionel Sambucdo not support any modifier (flags, width, length). 820611be35a1SLionel Sambuc 820711be35a1SLionel Sambuc 820811be35a1SLionel Sambuc 820911be35a1SLionel Sambuc 821011be35a1SLionel Sambuc<p> 821111be35a1SLionel Sambuc<hr><h3><a name="pdf-string.gmatch"><code>string.gmatch (s, pattern)</code></a></h3> 821211be35a1SLionel SambucReturns an iterator function that, 821311be35a1SLionel Sambuceach time it is called, 8214*0a6a1f1dSLionel Sambucreturns the next captures from <code>pattern</code> (see <a href="#6.4.1">§6.4.1</a>) 8215*0a6a1f1dSLionel Sambucover the string <code>s</code>. 821611be35a1SLionel SambucIf <code>pattern</code> specifies no captures, 821711be35a1SLionel Sambucthen the whole match is produced in each call. 821811be35a1SLionel Sambuc 821911be35a1SLionel Sambuc 822011be35a1SLionel Sambuc<p> 822111be35a1SLionel SambucAs an example, the following loop 8222*0a6a1f1dSLionel Sambucwill iterate over all the words from string <code>s</code>, 8223*0a6a1f1dSLionel Sambucprinting one per line: 822411be35a1SLionel Sambuc 822511be35a1SLionel Sambuc<pre> 822611be35a1SLionel Sambuc s = "hello world from Lua" 822711be35a1SLionel Sambuc for w in string.gmatch(s, "%a+") do 822811be35a1SLionel Sambuc print(w) 822911be35a1SLionel Sambuc end 823011be35a1SLionel Sambuc</pre><p> 823111be35a1SLionel SambucThe next example collects all pairs <code>key=value</code> from the 823211be35a1SLionel Sambucgiven string into a table: 823311be35a1SLionel Sambuc 823411be35a1SLionel Sambuc<pre> 823511be35a1SLionel Sambuc t = {} 823611be35a1SLionel Sambuc s = "from=world, to=Lua" 823711be35a1SLionel Sambuc for k, v in string.gmatch(s, "(%w+)=(%w+)") do 823811be35a1SLionel Sambuc t[k] = v 823911be35a1SLionel Sambuc end 824011be35a1SLionel Sambuc</pre> 824111be35a1SLionel Sambuc 824211be35a1SLionel Sambuc<p> 8243*0a6a1f1dSLionel SambucFor this function, a caret '<code>^</code>' at the start of a pattern does not 824411be35a1SLionel Sambucwork as an anchor, as this would prevent the iteration. 824511be35a1SLionel Sambuc 824611be35a1SLionel Sambuc 824711be35a1SLionel Sambuc 824811be35a1SLionel Sambuc 824911be35a1SLionel Sambuc<p> 825011be35a1SLionel Sambuc<hr><h3><a name="pdf-string.gsub"><code>string.gsub (s, pattern, repl [, n])</code></a></h3> 825111be35a1SLionel SambucReturns a copy of <code>s</code> 825211be35a1SLionel Sambucin which all (or the first <code>n</code>, if given) 8253*0a6a1f1dSLionel Sambucoccurrences of the <code>pattern</code> (see <a href="#6.4.1">§6.4.1</a>) have been 825411be35a1SLionel Sambucreplaced by a replacement string specified by <code>repl</code>, 825511be35a1SLionel Sambucwhich can be a string, a table, or a function. 825611be35a1SLionel Sambuc<code>gsub</code> also returns, as its second value, 825711be35a1SLionel Sambucthe total number of matches that occurred. 8258*0a6a1f1dSLionel SambucThe name <code>gsub</code> comes from <em>Global SUBstitution</em>. 825911be35a1SLionel Sambuc 826011be35a1SLionel Sambuc 826111be35a1SLionel Sambuc<p> 826211be35a1SLionel SambucIf <code>repl</code> is a string, then its value is used for replacement. 826311be35a1SLionel SambucThe character <code>%</code> works as an escape character: 8264*0a6a1f1dSLionel Sambucany sequence in <code>repl</code> of the form <code>%<em>d</em></code>, 8265*0a6a1f1dSLionel Sambucwith <em>d</em> between 1 and 9, 8266*0a6a1f1dSLionel Sambucstands for the value of the <em>d</em>-th captured substring. 826711be35a1SLionel SambucThe sequence <code>%0</code> stands for the whole match. 826811be35a1SLionel SambucThe sequence <code>%%</code> stands for a single <code>%</code>. 826911be35a1SLionel Sambuc 827011be35a1SLionel Sambuc 827111be35a1SLionel Sambuc<p> 827211be35a1SLionel SambucIf <code>repl</code> is a table, then the table is queried for every match, 8273*0a6a1f1dSLionel Sambucusing the first capture as the key. 827411be35a1SLionel Sambuc 827511be35a1SLionel Sambuc 827611be35a1SLionel Sambuc<p> 827711be35a1SLionel SambucIf <code>repl</code> is a function, then this function is called every time a 827811be35a1SLionel Sambucmatch occurs, with all captured substrings passed as arguments, 8279*0a6a1f1dSLionel Sambucin order. 8280*0a6a1f1dSLionel Sambuc 8281*0a6a1f1dSLionel Sambuc 8282*0a6a1f1dSLionel Sambuc<p> 8283*0a6a1f1dSLionel SambucIn any case, 828411be35a1SLionel Sambucif the pattern specifies no captures, 8285*0a6a1f1dSLionel Sambucthen it behaves as if the whole pattern was inside a capture. 828611be35a1SLionel Sambuc 828711be35a1SLionel Sambuc 828811be35a1SLionel Sambuc<p> 828911be35a1SLionel SambucIf the value returned by the table query or by the function call 829011be35a1SLionel Sambucis a string or a number, 829111be35a1SLionel Sambucthen it is used as the replacement string; 829211be35a1SLionel Sambucotherwise, if it is <b>false</b> or <b>nil</b>, 829311be35a1SLionel Sambucthen there is no replacement 829411be35a1SLionel Sambuc(that is, the original match is kept in the string). 829511be35a1SLionel Sambuc 829611be35a1SLionel Sambuc 829711be35a1SLionel Sambuc<p> 829811be35a1SLionel SambucHere are some examples: 829911be35a1SLionel Sambuc 830011be35a1SLionel Sambuc<pre> 830111be35a1SLionel Sambuc x = string.gsub("hello world", "(%w+)", "%1 %1") 830211be35a1SLionel Sambuc --> x="hello hello world world" 830311be35a1SLionel Sambuc 830411be35a1SLionel Sambuc x = string.gsub("hello world", "%w+", "%0 %0", 1) 830511be35a1SLionel Sambuc --> x="hello hello world" 830611be35a1SLionel Sambuc 830711be35a1SLionel Sambuc x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1") 830811be35a1SLionel Sambuc --> x="world hello Lua from" 830911be35a1SLionel Sambuc 831011be35a1SLionel Sambuc x = string.gsub("home = $HOME, user = $USER", "%$(%w+)", os.getenv) 831111be35a1SLionel Sambuc --> x="home = /home/roberto, user = roberto" 831211be35a1SLionel Sambuc 831311be35a1SLionel Sambuc x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s) 8314*0a6a1f1dSLionel Sambuc return load(s)() 831511be35a1SLionel Sambuc end) 831611be35a1SLionel Sambuc --> x="4+5 = 9" 831711be35a1SLionel Sambuc 8318*0a6a1f1dSLionel Sambuc local t = {name="lua", version="5.3"} 831911be35a1SLionel Sambuc x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t) 8320*0a6a1f1dSLionel Sambuc --> x="lua-5.3.tar.gz" 832111be35a1SLionel Sambuc</pre> 832211be35a1SLionel Sambuc 832311be35a1SLionel Sambuc 832411be35a1SLionel Sambuc 832511be35a1SLionel Sambuc<p> 832611be35a1SLionel Sambuc<hr><h3><a name="pdf-string.len"><code>string.len (s)</code></a></h3> 832711be35a1SLionel SambucReceives a string and returns its length. 832811be35a1SLionel SambucThe empty string <code>""</code> has length 0. 832911be35a1SLionel SambucEmbedded zeros are counted, 833011be35a1SLionel Sambucso <code>"a\000bc\000"</code> has length 5. 833111be35a1SLionel Sambuc 833211be35a1SLionel Sambuc 833311be35a1SLionel Sambuc 833411be35a1SLionel Sambuc 833511be35a1SLionel Sambuc<p> 833611be35a1SLionel Sambuc<hr><h3><a name="pdf-string.lower"><code>string.lower (s)</code></a></h3> 833711be35a1SLionel SambucReceives a string and returns a copy of this string with all 833811be35a1SLionel Sambucuppercase letters changed to lowercase. 833911be35a1SLionel SambucAll other characters are left unchanged. 834011be35a1SLionel SambucThe definition of what an uppercase letter is depends on the current locale. 834111be35a1SLionel Sambuc 834211be35a1SLionel Sambuc 834311be35a1SLionel Sambuc 834411be35a1SLionel Sambuc 834511be35a1SLionel Sambuc<p> 834611be35a1SLionel Sambuc<hr><h3><a name="pdf-string.match"><code>string.match (s, pattern [, init])</code></a></h3> 834711be35a1SLionel SambucLooks for the first <em>match</em> of 8348*0a6a1f1dSLionel Sambuc<code>pattern</code> (see <a href="#6.4.1">§6.4.1</a>) in the string <code>s</code>. 834911be35a1SLionel SambucIf it finds one, then <code>match</code> returns 835011be35a1SLionel Sambucthe captures from the pattern; 835111be35a1SLionel Sambucotherwise it returns <b>nil</b>. 835211be35a1SLionel SambucIf <code>pattern</code> specifies no captures, 835311be35a1SLionel Sambucthen the whole match is returned. 8354*0a6a1f1dSLionel SambucA third, optional numeric argument <code>init</code> specifies 835511be35a1SLionel Sambucwhere to start the search; 835611be35a1SLionel Sambucits default value is 1 and can be negative. 835711be35a1SLionel Sambuc 835811be35a1SLionel Sambuc 835911be35a1SLionel Sambuc 836011be35a1SLionel Sambuc 836111be35a1SLionel Sambuc<p> 8362*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-string.pack"><code>string.pack (fmt, v1, v2, ···)</code></a></h3> 8363*0a6a1f1dSLionel Sambuc 8364*0a6a1f1dSLionel Sambuc 8365*0a6a1f1dSLionel Sambuc<p> 8366*0a6a1f1dSLionel SambucReturns a binary string containing the values <code>v1</code>, <code>v2</code>, etc. 8367*0a6a1f1dSLionel Sambucpacked (that is, serialized in binary form) 8368*0a6a1f1dSLionel Sambucaccording to the format string <code>fmt</code> (see <a href="#6.4.2">§6.4.2</a>). 8369*0a6a1f1dSLionel Sambuc 8370*0a6a1f1dSLionel Sambuc 8371*0a6a1f1dSLionel Sambuc 8372*0a6a1f1dSLionel Sambuc 8373*0a6a1f1dSLionel Sambuc<p> 8374*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-string.packsize"><code>string.packsize (fmt)</code></a></h3> 8375*0a6a1f1dSLionel Sambuc 8376*0a6a1f1dSLionel Sambuc 8377*0a6a1f1dSLionel Sambuc<p> 8378*0a6a1f1dSLionel SambucReturns the size of a string resulting from <a href="#pdf-string.pack"><code>string.pack</code></a> 8379*0a6a1f1dSLionel Sambucwith the given format. 8380*0a6a1f1dSLionel SambucThe format string cannot have the variable-length options 8381*0a6a1f1dSLionel Sambuc'<code>s</code>' or '<code>z</code>' (see <a href="#6.4.2">§6.4.2</a>). 8382*0a6a1f1dSLionel Sambuc 8383*0a6a1f1dSLionel Sambuc 8384*0a6a1f1dSLionel Sambuc 8385*0a6a1f1dSLionel Sambuc 8386*0a6a1f1dSLionel Sambuc<p> 8387*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-string.rep"><code>string.rep (s, n [, sep])</code></a></h3> 838811be35a1SLionel SambucReturns a string that is the concatenation of <code>n</code> copies of 8389*0a6a1f1dSLionel Sambucthe string <code>s</code> separated by the string <code>sep</code>. 8390*0a6a1f1dSLionel SambucThe default value for <code>sep</code> is the empty string 8391*0a6a1f1dSLionel Sambuc(that is, no separator). 8392*0a6a1f1dSLionel SambucReturns the empty string if <code>n</code> is not positive. 839311be35a1SLionel Sambuc 839411be35a1SLionel Sambuc 839511be35a1SLionel Sambuc 839611be35a1SLionel Sambuc 839711be35a1SLionel Sambuc<p> 839811be35a1SLionel Sambuc<hr><h3><a name="pdf-string.reverse"><code>string.reverse (s)</code></a></h3> 839911be35a1SLionel SambucReturns a string that is the string <code>s</code> reversed. 840011be35a1SLionel Sambuc 840111be35a1SLionel Sambuc 840211be35a1SLionel Sambuc 840311be35a1SLionel Sambuc 840411be35a1SLionel Sambuc<p> 840511be35a1SLionel Sambuc<hr><h3><a name="pdf-string.sub"><code>string.sub (s, i [, j])</code></a></h3> 840611be35a1SLionel SambucReturns the substring of <code>s</code> that 840711be35a1SLionel Sambucstarts at <code>i</code> and continues until <code>j</code>; 840811be35a1SLionel Sambuc<code>i</code> and <code>j</code> can be negative. 840911be35a1SLionel SambucIf <code>j</code> is absent, then it is assumed to be equal to -1 841011be35a1SLionel Sambuc(which is the same as the string length). 841111be35a1SLionel SambucIn particular, 841211be35a1SLionel Sambucthe call <code>string.sub(s,1,j)</code> returns a prefix of <code>s</code> 841311be35a1SLionel Sambucwith length <code>j</code>, 841411be35a1SLionel Sambucand <code>string.sub(s, -i)</code> returns a suffix of <code>s</code> 841511be35a1SLionel Sambucwith length <code>i</code>. 841611be35a1SLionel Sambuc 841711be35a1SLionel Sambuc 8418*0a6a1f1dSLionel Sambuc<p> 8419*0a6a1f1dSLionel SambucIf, after the translation of negative indices, 8420*0a6a1f1dSLionel Sambuc<code>i</code> is less than 1, 8421*0a6a1f1dSLionel Sambucit is corrected to 1. 8422*0a6a1f1dSLionel SambucIf <code>j</code> is greater than the string length, 8423*0a6a1f1dSLionel Sambucit is corrected to that length. 8424*0a6a1f1dSLionel SambucIf, after these corrections, 8425*0a6a1f1dSLionel Sambuc<code>i</code> is greater than <code>j</code>, 8426*0a6a1f1dSLionel Sambucthe function returns the empty string. 8427*0a6a1f1dSLionel Sambuc 8428*0a6a1f1dSLionel Sambuc 8429*0a6a1f1dSLionel Sambuc 8430*0a6a1f1dSLionel Sambuc 8431*0a6a1f1dSLionel Sambuc<p> 8432*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-string.unpack"><code>string.unpack (fmt, s [, pos])</code></a></h3> 8433*0a6a1f1dSLionel Sambuc 8434*0a6a1f1dSLionel Sambuc 8435*0a6a1f1dSLionel Sambuc<p> 8436*0a6a1f1dSLionel SambucReturns the values packed in string <code>s</code> (see <a href="#pdf-string.pack"><code>string.pack</code></a>) 8437*0a6a1f1dSLionel Sambucaccording to the format string <code>fmt</code> (see <a href="#6.4.2">§6.4.2</a>). 8438*0a6a1f1dSLionel SambucAn optional <code>pos</code> marks where 8439*0a6a1f1dSLionel Sambucto start reading in <code>s</code> (default is 1). 8440*0a6a1f1dSLionel SambucAfter the read values, 8441*0a6a1f1dSLionel Sambucthis function also returns the index of the first unread byte in <code>s</code>. 8442*0a6a1f1dSLionel Sambuc 8443*0a6a1f1dSLionel Sambuc 844411be35a1SLionel Sambuc 844511be35a1SLionel Sambuc 844611be35a1SLionel Sambuc<p> 844711be35a1SLionel Sambuc<hr><h3><a name="pdf-string.upper"><code>string.upper (s)</code></a></h3> 844811be35a1SLionel SambucReceives a string and returns a copy of this string with all 844911be35a1SLionel Sambuclowercase letters changed to uppercase. 845011be35a1SLionel SambucAll other characters are left unchanged. 845111be35a1SLionel SambucThe definition of what a lowercase letter is depends on the current locale. 845211be35a1SLionel Sambuc 845311be35a1SLionel Sambuc 845411be35a1SLionel Sambuc 8455*0a6a1f1dSLionel Sambuc 8456*0a6a1f1dSLionel Sambuc 8457*0a6a1f1dSLionel Sambuc<h3>6.4.1 – <a name="6.4.1">Patterns</a></h3> 8458*0a6a1f1dSLionel Sambuc 8459*0a6a1f1dSLionel Sambuc<p> 8460*0a6a1f1dSLionel SambucPatterns in Lua are described by regular strings, 8461*0a6a1f1dSLionel Sambucwhich are interpreted as patterns by the pattern-matching functions 8462*0a6a1f1dSLionel Sambuc<a href="#pdf-string.find"><code>string.find</code></a>, 8463*0a6a1f1dSLionel Sambuc<a href="#pdf-string.gmatch"><code>string.gmatch</code></a>, 8464*0a6a1f1dSLionel Sambuc<a href="#pdf-string.gsub"><code>string.gsub</code></a>, 8465*0a6a1f1dSLionel Sambucand <a href="#pdf-string.match"><code>string.match</code></a>. 8466*0a6a1f1dSLionel SambucThis section describes the syntax and the meaning 8467*0a6a1f1dSLionel Sambuc(that is, what they match) of these strings. 8468*0a6a1f1dSLionel Sambuc 846911be35a1SLionel Sambuc 847011be35a1SLionel Sambuc 847111be35a1SLionel Sambuc<h4>Character Class:</h4><p> 847211be35a1SLionel SambucA <em>character class</em> is used to represent a set of characters. 847311be35a1SLionel SambucThe following combinations are allowed in describing a character class: 847411be35a1SLionel Sambuc 847511be35a1SLionel Sambuc<ul> 847611be35a1SLionel Sambuc 847711be35a1SLionel Sambuc<li><b><em>x</em>: </b> 847811be35a1SLionel Sambuc(where <em>x</em> is not one of the <em>magic characters</em> 847911be35a1SLionel Sambuc<code>^$()%.[]*+-?</code>) 848011be35a1SLionel Sambucrepresents the character <em>x</em> itself. 848111be35a1SLionel Sambuc</li> 848211be35a1SLionel Sambuc 848311be35a1SLionel Sambuc<li><b><code>.</code>: </b> (a dot) represents all characters.</li> 848411be35a1SLionel Sambuc 848511be35a1SLionel Sambuc<li><b><code>%a</code>: </b> represents all letters.</li> 848611be35a1SLionel Sambuc 848711be35a1SLionel Sambuc<li><b><code>%c</code>: </b> represents all control characters.</li> 848811be35a1SLionel Sambuc 848911be35a1SLionel Sambuc<li><b><code>%d</code>: </b> represents all digits.</li> 849011be35a1SLionel Sambuc 8491*0a6a1f1dSLionel Sambuc<li><b><code>%g</code>: </b> represents all printable characters except space.</li> 8492*0a6a1f1dSLionel Sambuc 849311be35a1SLionel Sambuc<li><b><code>%l</code>: </b> represents all lowercase letters.</li> 849411be35a1SLionel Sambuc 849511be35a1SLionel Sambuc<li><b><code>%p</code>: </b> represents all punctuation characters.</li> 849611be35a1SLionel Sambuc 849711be35a1SLionel Sambuc<li><b><code>%s</code>: </b> represents all space characters.</li> 849811be35a1SLionel Sambuc 849911be35a1SLionel Sambuc<li><b><code>%u</code>: </b> represents all uppercase letters.</li> 850011be35a1SLionel Sambuc 850111be35a1SLionel Sambuc<li><b><code>%w</code>: </b> represents all alphanumeric characters.</li> 850211be35a1SLionel Sambuc 850311be35a1SLionel Sambuc<li><b><code>%x</code>: </b> represents all hexadecimal digits.</li> 850411be35a1SLionel Sambuc 850511be35a1SLionel Sambuc<li><b><code>%<em>x</em></code>: </b> (where <em>x</em> is any non-alphanumeric character) 850611be35a1SLionel Sambucrepresents the character <em>x</em>. 850711be35a1SLionel SambucThis is the standard way to escape the magic characters. 8508*0a6a1f1dSLionel SambucAny non-alphanumeric character 8509*0a6a1f1dSLionel Sambuc(including all punctuation characters, even the non-magical) 851011be35a1SLionel Sambuccan be preceded by a '<code>%</code>' 851111be35a1SLionel Sambucwhen used to represent itself in a pattern. 851211be35a1SLionel Sambuc</li> 851311be35a1SLionel Sambuc 851411be35a1SLionel Sambuc<li><b><code>[<em>set</em>]</code>: </b> 851511be35a1SLionel Sambucrepresents the class which is the union of all 851611be35a1SLionel Sambuccharacters in <em>set</em>. 851711be35a1SLionel SambucA range of characters can be specified by 8518*0a6a1f1dSLionel Sambucseparating the end characters of the range, 8519*0a6a1f1dSLionel Sambucin ascending order, with a '<code>-</code>'. 852011be35a1SLionel SambucAll classes <code>%</code><em>x</em> described above can also be used as 852111be35a1SLionel Sambuccomponents in <em>set</em>. 852211be35a1SLionel SambucAll other characters in <em>set</em> represent themselves. 852311be35a1SLionel SambucFor example, <code>[%w_]</code> (or <code>[_%w]</code>) 852411be35a1SLionel Sambucrepresents all alphanumeric characters plus the underscore, 852511be35a1SLionel Sambuc<code>[0-7]</code> represents the octal digits, 852611be35a1SLionel Sambucand <code>[0-7%l%-]</code> represents the octal digits plus 852711be35a1SLionel Sambucthe lowercase letters plus the '<code>-</code>' character. 852811be35a1SLionel Sambuc 852911be35a1SLionel Sambuc 853011be35a1SLionel Sambuc<p> 853111be35a1SLionel SambucThe interaction between ranges and classes is not defined. 853211be35a1SLionel SambucTherefore, patterns like <code>[%a-z]</code> or <code>[a-%%]</code> 853311be35a1SLionel Sambuchave no meaning. 853411be35a1SLionel Sambuc</li> 853511be35a1SLionel Sambuc 853611be35a1SLionel Sambuc<li><b><code>[^<em>set</em>]</code>: </b> 853711be35a1SLionel Sambucrepresents the complement of <em>set</em>, 853811be35a1SLionel Sambucwhere <em>set</em> is interpreted as above. 853911be35a1SLionel Sambuc</li> 854011be35a1SLionel Sambuc 854111be35a1SLionel Sambuc</ul><p> 854211be35a1SLionel SambucFor all classes represented by single letters (<code>%a</code>, <code>%c</code>, etc.), 854311be35a1SLionel Sambucthe corresponding uppercase letter represents the complement of the class. 854411be35a1SLionel SambucFor instance, <code>%S</code> represents all non-space characters. 854511be35a1SLionel Sambuc 854611be35a1SLionel Sambuc 854711be35a1SLionel Sambuc<p> 854811be35a1SLionel SambucThe definitions of letter, space, and other character groups 854911be35a1SLionel Sambucdepend on the current locale. 855011be35a1SLionel SambucIn particular, the class <code>[a-z]</code> may not be equivalent to <code>%l</code>. 855111be35a1SLionel Sambuc 855211be35a1SLionel Sambuc 855311be35a1SLionel Sambuc 855411be35a1SLionel Sambuc 855511be35a1SLionel Sambuc 855611be35a1SLionel Sambuc<h4>Pattern Item:</h4><p> 855711be35a1SLionel SambucA <em>pattern item</em> can be 855811be35a1SLionel Sambuc 855911be35a1SLionel Sambuc<ul> 856011be35a1SLionel Sambuc 856111be35a1SLionel Sambuc<li> 856211be35a1SLionel Sambuca single character class, 856311be35a1SLionel Sambucwhich matches any single character in the class; 856411be35a1SLionel Sambuc</li> 856511be35a1SLionel Sambuc 856611be35a1SLionel Sambuc<li> 856711be35a1SLionel Sambuca single character class followed by '<code>*</code>', 8568*0a6a1f1dSLionel Sambucwhich matches zero or more repetitions of characters in the class. 856911be35a1SLionel SambucThese repetition items will always match the longest possible sequence; 857011be35a1SLionel Sambuc</li> 857111be35a1SLionel Sambuc 857211be35a1SLionel Sambuc<li> 857311be35a1SLionel Sambuca single character class followed by '<code>+</code>', 8574*0a6a1f1dSLionel Sambucwhich matches one or more repetitions of characters in the class. 857511be35a1SLionel SambucThese repetition items will always match the longest possible sequence; 857611be35a1SLionel Sambuc</li> 857711be35a1SLionel Sambuc 857811be35a1SLionel Sambuc<li> 857911be35a1SLionel Sambuca single character class followed by '<code>-</code>', 8580*0a6a1f1dSLionel Sambucwhich also matches zero or more repetitions of characters in the class. 858111be35a1SLionel SambucUnlike '<code>*</code>', 8582*0a6a1f1dSLionel Sambucthese repetition items will always match the shortest possible sequence; 858311be35a1SLionel Sambuc</li> 858411be35a1SLionel Sambuc 858511be35a1SLionel Sambuc<li> 858611be35a1SLionel Sambuca single character class followed by '<code>?</code>', 8587*0a6a1f1dSLionel Sambucwhich matches zero or one occurrence of a character in the class. 8588*0a6a1f1dSLionel SambucIt always matches one occurrence if possible; 858911be35a1SLionel Sambuc</li> 859011be35a1SLionel Sambuc 859111be35a1SLionel Sambuc<li> 859211be35a1SLionel Sambuc<code>%<em>n</em></code>, for <em>n</em> between 1 and 9; 859311be35a1SLionel Sambucsuch item matches a substring equal to the <em>n</em>-th captured string 859411be35a1SLionel Sambuc(see below); 859511be35a1SLionel Sambuc</li> 859611be35a1SLionel Sambuc 859711be35a1SLionel Sambuc<li> 859811be35a1SLionel Sambuc<code>%b<em>xy</em></code>, where <em>x</em> and <em>y</em> are two distinct characters; 859911be35a1SLionel Sambucsuch item matches strings that start with <em>x</em>, end with <em>y</em>, 860011be35a1SLionel Sambucand where the <em>x</em> and <em>y</em> are <em>balanced</em>. 860111be35a1SLionel SambucThis means that, if one reads the string from left to right, 860211be35a1SLionel Sambuccounting <em>+1</em> for an <em>x</em> and <em>-1</em> for a <em>y</em>, 860311be35a1SLionel Sambucthe ending <em>y</em> is the first <em>y</em> where the count reaches 0. 860411be35a1SLionel SambucFor instance, the item <code>%b()</code> matches expressions with 860511be35a1SLionel Sambucbalanced parentheses. 860611be35a1SLionel Sambuc</li> 860711be35a1SLionel Sambuc 8608*0a6a1f1dSLionel Sambuc<li> 8609*0a6a1f1dSLionel Sambuc<code>%f[<em>set</em>]</code>, a <em>frontier pattern</em>; 8610*0a6a1f1dSLionel Sambucsuch item matches an empty string at any position such that 8611*0a6a1f1dSLionel Sambucthe next character belongs to <em>set</em> 8612*0a6a1f1dSLionel Sambucand the previous character does not belong to <em>set</em>. 8613*0a6a1f1dSLionel SambucThe set <em>set</em> is interpreted as previously described. 8614*0a6a1f1dSLionel SambucThe beginning and the end of the subject are handled as if 8615*0a6a1f1dSLionel Sambucthey were the character '<code>\0</code>'. 8616*0a6a1f1dSLionel Sambuc</li> 8617*0a6a1f1dSLionel Sambuc 861811be35a1SLionel Sambuc</ul> 861911be35a1SLionel Sambuc 862011be35a1SLionel Sambuc 862111be35a1SLionel Sambuc 862211be35a1SLionel Sambuc 862311be35a1SLionel Sambuc<h4>Pattern:</h4><p> 862411be35a1SLionel SambucA <em>pattern</em> is a sequence of pattern items. 8625*0a6a1f1dSLionel SambucA caret '<code>^</code>' at the beginning of a pattern anchors the match at the 862611be35a1SLionel Sambucbeginning of the subject string. 862711be35a1SLionel SambucA '<code>$</code>' at the end of a pattern anchors the match at the 862811be35a1SLionel Sambucend of the subject string. 862911be35a1SLionel SambucAt other positions, 863011be35a1SLionel Sambuc'<code>^</code>' and '<code>$</code>' have no special meaning and represent themselves. 863111be35a1SLionel Sambuc 863211be35a1SLionel Sambuc 863311be35a1SLionel Sambuc 863411be35a1SLionel Sambuc 863511be35a1SLionel Sambuc 863611be35a1SLionel Sambuc<h4>Captures:</h4><p> 863711be35a1SLionel SambucA pattern can contain sub-patterns enclosed in parentheses; 863811be35a1SLionel Sambucthey describe <em>captures</em>. 863911be35a1SLionel SambucWhen a match succeeds, the substrings of the subject string 864011be35a1SLionel Sambucthat match captures are stored (<em>captured</em>) for future use. 864111be35a1SLionel SambucCaptures are numbered according to their left parentheses. 864211be35a1SLionel SambucFor instance, in the pattern <code>"(a*(.)%w(%s*))"</code>, 864311be35a1SLionel Sambucthe part of the string matching <code>"a*(.)%w(%s*)"</code> is 864411be35a1SLionel Sambucstored as the first capture (and therefore has number 1); 864511be35a1SLionel Sambucthe character matching "<code>.</code>" is captured with number 2, 864611be35a1SLionel Sambucand the part matching "<code>%s*</code>" has number 3. 864711be35a1SLionel Sambuc 864811be35a1SLionel Sambuc 864911be35a1SLionel Sambuc<p> 865011be35a1SLionel SambucAs a special case, the empty capture <code>()</code> captures 865111be35a1SLionel Sambucthe current string position (a number). 865211be35a1SLionel SambucFor instance, if we apply the pattern <code>"()aa()"</code> on the 865311be35a1SLionel Sambucstring <code>"flaaap"</code>, there will be two captures: 3 and 5. 865411be35a1SLionel Sambuc 865511be35a1SLionel Sambuc 8656*0a6a1f1dSLionel Sambuc 8657*0a6a1f1dSLionel Sambuc 8658*0a6a1f1dSLionel Sambuc 8659*0a6a1f1dSLionel Sambuc 8660*0a6a1f1dSLionel Sambuc 8661*0a6a1f1dSLionel Sambuc<h3>6.4.2 – <a name="6.4.2">Format Strings for Pack and Unpack</a></h3> 8662*0a6a1f1dSLionel Sambuc 866311be35a1SLionel Sambuc<p> 8664*0a6a1f1dSLionel SambucThe first argument to <a href="#pdf-string.pack"><code>string.pack</code></a>, 8665*0a6a1f1dSLionel Sambuc<a href="#pdf-string.packsize"><code>string.packsize</code></a>, and <a href="#pdf-string.unpack"><code>string.unpack</code></a> 8666*0a6a1f1dSLionel Sambucis a format string, 8667*0a6a1f1dSLionel Sambucwhich describes the layout of the structure being created or read. 8668*0a6a1f1dSLionel Sambuc 8669*0a6a1f1dSLionel Sambuc 8670*0a6a1f1dSLionel Sambuc<p> 8671*0a6a1f1dSLionel SambucA format string is a sequence of conversion options. 8672*0a6a1f1dSLionel SambucThe conversion options are as follows: 8673*0a6a1f1dSLionel Sambuc 8674*0a6a1f1dSLionel Sambuc<ul> 8675*0a6a1f1dSLionel Sambuc<li><b><code><</code>: </b>sets little endian</li> 8676*0a6a1f1dSLionel Sambuc<li><b><code>></code>: </b>sets big endian</li> 8677*0a6a1f1dSLionel Sambuc<li><b><code>=</code>: </b>sets native endian</li> 8678*0a6a1f1dSLionel Sambuc<li><b><code>![<em>n</em>]</code>: </b>sets maximum alignment to <code>n</code> 8679*0a6a1f1dSLionel Sambuc(default is native alignment)</li> 8680*0a6a1f1dSLionel Sambuc<li><b><code>b</code>: </b>a signed byte (<code>char</code>)</li> 8681*0a6a1f1dSLionel Sambuc<li><b><code>B</code>: </b>an unsigned byte (<code>char</code>)</li> 8682*0a6a1f1dSLionel Sambuc<li><b><code>h</code>: </b>a signed <code>short</code> (native size)</li> 8683*0a6a1f1dSLionel Sambuc<li><b><code>H</code>: </b>an unsigned <code>short</code> (native size)</li> 8684*0a6a1f1dSLionel Sambuc<li><b><code>l</code>: </b>a signed <code>long</code> (native size)</li> 8685*0a6a1f1dSLionel Sambuc<li><b><code>L</code>: </b>an unsigned <code>long</code> (native size)</li> 8686*0a6a1f1dSLionel Sambuc<li><b><code>j</code>: </b>a <code>lua_Integer</code></li> 8687*0a6a1f1dSLionel Sambuc<li><b><code>J</code>: </b>a <code>lua_Unsigned</code></li> 8688*0a6a1f1dSLionel Sambuc<li><b><code>T</code>: </b>a <code>size_t</code> (native size)</li> 8689*0a6a1f1dSLionel Sambuc<li><b><code>i[<em>n</em>]</code>: </b>a signed <code>int</code> with <code>n</code> bytes 8690*0a6a1f1dSLionel Sambuc(default is native size)</li> 8691*0a6a1f1dSLionel Sambuc<li><b><code>I[<em>n</em>]</code>: </b>an unsigned <code>int</code> with <code>n</code> bytes 8692*0a6a1f1dSLionel Sambuc(default is native size)</li> 8693*0a6a1f1dSLionel Sambuc<li><b><code>f</code>: </b>a <code>float</code> (native size)</li> 8694*0a6a1f1dSLionel Sambuc<li><b><code>d</code>: </b>a <code>double</code> (native size)</li> 8695*0a6a1f1dSLionel Sambuc<li><b><code>n</code>: </b>a <code>lua_Number</code></li> 8696*0a6a1f1dSLionel Sambuc<li><b><code>c<em>n</em></code>: </b>a fixed-sized string with <code>n</code> bytes</li> 8697*0a6a1f1dSLionel Sambuc<li><b><code>z</code>: </b>a zero-terminated string</li> 8698*0a6a1f1dSLionel Sambuc<li><b><code>s[<em>n</em>]</code>: </b>a string preceded by its length 8699*0a6a1f1dSLionel Sambuccoded as an unsigned integer with <code>n</code> bytes 8700*0a6a1f1dSLionel Sambuc(default is a <code>size_t</code>)</li> 8701*0a6a1f1dSLionel Sambuc<li><b><code>x</code>: </b>one byte of padding</li> 8702*0a6a1f1dSLionel Sambuc<li><b><code>X<em>op</em></code>: </b>an empty item that aligns 8703*0a6a1f1dSLionel Sambucaccording to option <code>op</code> 8704*0a6a1f1dSLionel Sambuc(which is otherwise ignored)</li> 8705*0a6a1f1dSLionel Sambuc<li><b>'<code> </code>': </b>(empty space) ignored</li> 8706*0a6a1f1dSLionel Sambuc</ul><p> 8707*0a6a1f1dSLionel Sambuc(A "<code>[<em>n</em>]</code>" means an optional integral numeral.) 8708*0a6a1f1dSLionel SambucExcept for padding, spaces, and configurations 8709*0a6a1f1dSLionel Sambuc(options "<code>xX <=>!</code>"), 8710*0a6a1f1dSLionel Sambuceach option corresponds to an argument (in <a href="#pdf-string.pack"><code>string.pack</code></a>) 8711*0a6a1f1dSLionel Sambucor a result (in <a href="#pdf-string.unpack"><code>string.unpack</code></a>). 8712*0a6a1f1dSLionel Sambuc 8713*0a6a1f1dSLionel Sambuc 8714*0a6a1f1dSLionel Sambuc<p> 8715*0a6a1f1dSLionel SambucFor 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>", 8716*0a6a1f1dSLionel Sambuc<code>n</code> can be any integer between 1 and 16. 8717*0a6a1f1dSLionel SambucAll integral options check overflows; 8718*0a6a1f1dSLionel Sambuc<a href="#pdf-string.pack"><code>string.pack</code></a> checks whether the given value fits in the given size; 8719*0a6a1f1dSLionel Sambuc<a href="#pdf-string.unpack"><code>string.unpack</code></a> checks whether the read value fits in a Lua integer. 8720*0a6a1f1dSLionel Sambuc 8721*0a6a1f1dSLionel Sambuc 8722*0a6a1f1dSLionel Sambuc<p> 8723*0a6a1f1dSLionel SambucAny format string starts as if prefixed by "<code>!1=</code>", 8724*0a6a1f1dSLionel Sambucthat is, 8725*0a6a1f1dSLionel Sambucwith maximum alignment of 1 (no alignment) 8726*0a6a1f1dSLionel Sambucand native endianness. 8727*0a6a1f1dSLionel Sambuc 8728*0a6a1f1dSLionel Sambuc 8729*0a6a1f1dSLionel Sambuc<p> 8730*0a6a1f1dSLionel SambucAlignment works as follows: 8731*0a6a1f1dSLionel SambucFor each option, 8732*0a6a1f1dSLionel Sambucthe format gets extra padding until the data starts 8733*0a6a1f1dSLionel Sambucat an offset that is a multiple of the minimum between the 8734*0a6a1f1dSLionel Sambucoption size and the maximum alignment; 8735*0a6a1f1dSLionel Sambucthis minimum must be a power of 2. 8736*0a6a1f1dSLionel SambucOptions "<code>c</code>" and "<code>z</code>" are not aligned; 8737*0a6a1f1dSLionel Sambucoption "<code>s</code>" follows the alignment of its starting integer. 8738*0a6a1f1dSLionel Sambuc 8739*0a6a1f1dSLionel Sambuc 8740*0a6a1f1dSLionel Sambuc<p> 8741*0a6a1f1dSLionel SambucAll padding is filled with zeros by <a href="#pdf-string.pack"><code>string.pack</code></a> 8742*0a6a1f1dSLionel Sambuc(and ignored by <a href="#pdf-string.unpack"><code>string.unpack</code></a>). 874311be35a1SLionel Sambuc 874411be35a1SLionel Sambuc 874511be35a1SLionel Sambuc 874611be35a1SLionel Sambuc 874711be35a1SLionel Sambuc 874811be35a1SLionel Sambuc 874911be35a1SLionel Sambuc 8750*0a6a1f1dSLionel Sambuc<h2>6.5 – <a name="6.5">UTF-8 Support</a></h2> 8751*0a6a1f1dSLionel Sambuc 8752*0a6a1f1dSLionel Sambuc<p> 8753*0a6a1f1dSLionel SambucThis library provides basic support for UTF-8 encoding. 8754*0a6a1f1dSLionel SambucIt provides all its functions inside the table <a name="pdf-utf8"><code>utf8</code></a>. 8755*0a6a1f1dSLionel SambucThis library does not provide any support for Unicode other 8756*0a6a1f1dSLionel Sambucthan the handling of the encoding. 8757*0a6a1f1dSLionel SambucAny operation that needs the meaning of a character, 8758*0a6a1f1dSLionel Sambucsuch as character classification, is outside its scope. 8759*0a6a1f1dSLionel Sambuc 8760*0a6a1f1dSLionel Sambuc 8761*0a6a1f1dSLionel Sambuc<p> 8762*0a6a1f1dSLionel SambucUnless stated otherwise, 8763*0a6a1f1dSLionel Sambucall functions that expect a byte position as a parameter 8764*0a6a1f1dSLionel Sambucassume that the given position is either the start of a byte sequence 8765*0a6a1f1dSLionel Sambucor one plus the length of the subject string. 8766*0a6a1f1dSLionel SambucAs in the string library, 8767*0a6a1f1dSLionel Sambucnegative indices count from the end of the string. 8768*0a6a1f1dSLionel Sambuc 8769*0a6a1f1dSLionel Sambuc 8770*0a6a1f1dSLionel Sambuc<p> 8771*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-utf8.char"><code>utf8.char (···)</code></a></h3> 8772*0a6a1f1dSLionel SambucReceives zero or more integers, 8773*0a6a1f1dSLionel Sambucconverts each one to its corresponding UTF-8 byte sequence 8774*0a6a1f1dSLionel Sambucand returns a string with the concatenation of all these sequences. 877511be35a1SLionel Sambuc 877611be35a1SLionel Sambuc 877711be35a1SLionel Sambuc 877811be35a1SLionel Sambuc 8779*0a6a1f1dSLionel Sambuc<p> 8780*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-utf8.charpattern"><code>utf8.charpattern</code></a></h3> 8781*0a6a1f1dSLionel SambucThe pattern (a string, not a function) "<code>[\0-\x7F\xC2-\xF4][\x80-\xBF]*</code>" 8782*0a6a1f1dSLionel Sambuc(see <a href="#6.4.1">§6.4.1</a>), 8783*0a6a1f1dSLionel Sambucwhich matches exactly one UTF-8 byte sequence, 8784*0a6a1f1dSLionel Sambucassuming that the subject is a valid UTF-8 string. 8785*0a6a1f1dSLionel Sambuc 8786*0a6a1f1dSLionel Sambuc 8787*0a6a1f1dSLionel Sambuc 8788*0a6a1f1dSLionel Sambuc 8789*0a6a1f1dSLionel Sambuc<p> 8790*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-utf8.codes"><code>utf8.codes (s)</code></a></h3> 8791*0a6a1f1dSLionel Sambuc 8792*0a6a1f1dSLionel Sambuc 8793*0a6a1f1dSLionel Sambuc<p> 8794*0a6a1f1dSLionel SambucReturns values so that the construction 8795*0a6a1f1dSLionel Sambuc 8796*0a6a1f1dSLionel Sambuc<pre> 8797*0a6a1f1dSLionel Sambuc for p, c in utf8.codes(s) do <em>body</em> end 8798*0a6a1f1dSLionel Sambuc</pre><p> 8799*0a6a1f1dSLionel Sambucwill iterate over all characters in string <code>s</code>, 8800*0a6a1f1dSLionel Sambucwith <code>p</code> being the position (in bytes) and <code>c</code> the code point 8801*0a6a1f1dSLionel Sambucof each character. 8802*0a6a1f1dSLionel SambucIt raises an error if it meets any invalid byte sequence. 8803*0a6a1f1dSLionel Sambuc 8804*0a6a1f1dSLionel Sambuc 8805*0a6a1f1dSLionel Sambuc 8806*0a6a1f1dSLionel Sambuc 8807*0a6a1f1dSLionel Sambuc<p> 8808*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-utf8.codepoint"><code>utf8.codepoint (s [, i [, j]])</code></a></h3> 8809*0a6a1f1dSLionel SambucReturns the codepoints (as integers) from all characters in <code>s</code> 8810*0a6a1f1dSLionel Sambucthat start between byte position <code>i</code> and <code>j</code> (both included). 8811*0a6a1f1dSLionel SambucThe default for <code>i</code> is 1 and for <code>j</code> is <code>i</code>. 8812*0a6a1f1dSLionel SambucIt raises an error if it meets any invalid byte sequence. 8813*0a6a1f1dSLionel Sambuc 8814*0a6a1f1dSLionel Sambuc 8815*0a6a1f1dSLionel Sambuc 8816*0a6a1f1dSLionel Sambuc 8817*0a6a1f1dSLionel Sambuc<p> 8818*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-utf8.len"><code>utf8.len (s [, i [, j]])</code></a></h3> 8819*0a6a1f1dSLionel SambucReturns the number of UTF-8 characters in string <code>s</code> 8820*0a6a1f1dSLionel Sambucthat start between positions <code>i</code> and <code>j</code> (both inclusive). 8821*0a6a1f1dSLionel SambucThe default for <code>i</code> is 1 and for <code>j</code> is -1. 8822*0a6a1f1dSLionel SambucIf it finds any invalid byte sequence, 8823*0a6a1f1dSLionel Sambucreturns a false value plus the position of the first invalid byte. 8824*0a6a1f1dSLionel Sambuc 8825*0a6a1f1dSLionel Sambuc 8826*0a6a1f1dSLionel Sambuc 8827*0a6a1f1dSLionel Sambuc 8828*0a6a1f1dSLionel Sambuc<p> 8829*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-utf8.offset"><code>utf8.offset (s, n [, i])</code></a></h3> 8830*0a6a1f1dSLionel SambucReturns the position (in bytes) where the encoding of the 8831*0a6a1f1dSLionel Sambuc<code>n</code>-th character of <code>s</code> 8832*0a6a1f1dSLionel Sambuc(counting from position <code>i</code>) starts. 8833*0a6a1f1dSLionel SambucA negative <code>n</code> gets characters before position <code>i</code>. 8834*0a6a1f1dSLionel SambucThe default for <code>i</code> is 1 when <code>n</code> is non-negative 8835*0a6a1f1dSLionel Sambucand <code>#s + 1</code> otherwise, 8836*0a6a1f1dSLionel Sambucso that <code>utf8.offset(s, -n)</code> gets the offset of the 8837*0a6a1f1dSLionel Sambuc<code>n</code>-th character from the end of the string. 8838*0a6a1f1dSLionel SambucIf the specified character is neither in the subject 8839*0a6a1f1dSLionel Sambucnor right after its end, 8840*0a6a1f1dSLionel Sambucthe function returns <b>nil</b>. 8841*0a6a1f1dSLionel Sambuc 8842*0a6a1f1dSLionel Sambuc 8843*0a6a1f1dSLionel Sambuc<p> 8844*0a6a1f1dSLionel SambucAs a special case, 8845*0a6a1f1dSLionel Sambucwhen <code>n</code> is 0 the function returns the start of the encoding 8846*0a6a1f1dSLionel Sambucof the character that contains the <code>i</code>-th byte of <code>s</code>. 8847*0a6a1f1dSLionel Sambuc 8848*0a6a1f1dSLionel Sambuc 8849*0a6a1f1dSLionel Sambuc<p> 8850*0a6a1f1dSLionel SambucThis function assumes that <code>s</code> is a valid UTF-8 string. 8851*0a6a1f1dSLionel Sambuc 8852*0a6a1f1dSLionel Sambuc 8853*0a6a1f1dSLionel Sambuc 8854*0a6a1f1dSLionel Sambuc 8855*0a6a1f1dSLionel Sambuc 8856*0a6a1f1dSLionel Sambuc 8857*0a6a1f1dSLionel Sambuc 8858*0a6a1f1dSLionel Sambuc<h2>6.6 – <a name="6.6">Table Manipulation</a></h2> 8859*0a6a1f1dSLionel Sambuc 8860*0a6a1f1dSLionel Sambuc<p> 886111be35a1SLionel SambucThis library provides generic functions for table manipulation. 886211be35a1SLionel SambucIt provides all its functions inside the table <a name="pdf-table"><code>table</code></a>. 886311be35a1SLionel Sambuc 886411be35a1SLionel Sambuc 886511be35a1SLionel Sambuc<p> 8866*0a6a1f1dSLionel SambucRemember that, whenever an operation needs the length of a table, 8867*0a6a1f1dSLionel Sambucthe table must be a proper sequence 8868*0a6a1f1dSLionel Sambucor have a <code>__len</code> metamethod (see <a href="#3.4.7">§3.4.7</a>). 8869*0a6a1f1dSLionel SambucAll functions ignore non-numeric keys 8870*0a6a1f1dSLionel Sambucin the tables given as arguments. 887111be35a1SLionel Sambuc 887211be35a1SLionel Sambuc 887311be35a1SLionel Sambuc<p> 8874*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-table.concat"><code>table.concat (list [, sep [, i [, j]]])</code></a></h3> 8875*0a6a1f1dSLionel Sambuc 8876*0a6a1f1dSLionel Sambuc 8877*0a6a1f1dSLionel Sambuc<p> 8878*0a6a1f1dSLionel SambucGiven a list where all elements are strings or numbers, 8879*0a6a1f1dSLionel Sambucreturns the string <code>list[i]..sep..list[i+1] ··· sep..list[j]</code>. 888011be35a1SLionel SambucThe default value for <code>sep</code> is the empty string, 888111be35a1SLionel Sambucthe default for <code>i</code> is 1, 8882*0a6a1f1dSLionel Sambucand the default for <code>j</code> is <code>#list</code>. 888311be35a1SLionel SambucIf <code>i</code> is greater than <code>j</code>, returns the empty string. 888411be35a1SLionel Sambuc 888511be35a1SLionel Sambuc 888611be35a1SLionel Sambuc 888711be35a1SLionel Sambuc 888811be35a1SLionel Sambuc<p> 8889*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-table.insert"><code>table.insert (list, [pos,] value)</code></a></h3> 889011be35a1SLionel Sambuc 889111be35a1SLionel Sambuc 889211be35a1SLionel Sambuc<p> 8893*0a6a1f1dSLionel SambucInserts element <code>value</code> at position <code>pos</code> in <code>list</code>, 8894*0a6a1f1dSLionel Sambucshifting up the elements 8895*0a6a1f1dSLionel Sambuc<code>list[pos], list[pos+1], ···, list[#list]</code>. 8896*0a6a1f1dSLionel SambucThe default value for <code>pos</code> is <code>#list+1</code>, 889711be35a1SLionel Sambucso that a call <code>table.insert(t,x)</code> inserts <code>x</code> at the end 8898*0a6a1f1dSLionel Sambucof list <code>t</code>. 889911be35a1SLionel Sambuc 890011be35a1SLionel Sambuc 890111be35a1SLionel Sambuc 890211be35a1SLionel Sambuc 890311be35a1SLionel Sambuc<p> 8904*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-table.move"><code>table.move (a1, f, e, t [,a2])</code></a></h3> 890511be35a1SLionel Sambuc 890611be35a1SLionel Sambuc 890711be35a1SLionel Sambuc<p> 8908*0a6a1f1dSLionel SambucMoves elements from table <code>a1</code> to table <code>a2</code>. 8909*0a6a1f1dSLionel SambucThis function performs the equivalent to the following 8910*0a6a1f1dSLionel Sambucmultiple assignment: 8911*0a6a1f1dSLionel Sambuc<code>a2[t],··· = a1[f],···,a1[e]</code>. 8912*0a6a1f1dSLionel SambucThe default for <code>a2</code> is <code>a1</code>. 8913*0a6a1f1dSLionel SambucThe destination range can overlap with the source range. 8914*0a6a1f1dSLionel SambucThe number of elements to be moved must fit in a Lua integer. 891511be35a1SLionel Sambuc 891611be35a1SLionel Sambuc 891711be35a1SLionel Sambuc 891811be35a1SLionel Sambuc 891911be35a1SLionel Sambuc<p> 8920*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-table.pack"><code>table.pack (···)</code></a></h3> 892111be35a1SLionel Sambuc 892211be35a1SLionel Sambuc 892311be35a1SLionel Sambuc<p> 8924*0a6a1f1dSLionel SambucReturns a new table with all parameters stored into keys 1, 2, etc. 8925*0a6a1f1dSLionel Sambucand with a field "<code>n</code>" with the total number of parameters. 8926*0a6a1f1dSLionel SambucNote that the resulting table may not be a sequence. 892711be35a1SLionel Sambuc 892811be35a1SLionel Sambuc 892911be35a1SLionel Sambuc 893011be35a1SLionel Sambuc 893111be35a1SLionel Sambuc<p> 8932*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-table.remove"><code>table.remove (list [, pos])</code></a></h3> 8933*0a6a1f1dSLionel Sambuc 8934*0a6a1f1dSLionel Sambuc 8935*0a6a1f1dSLionel Sambuc<p> 8936*0a6a1f1dSLionel SambucRemoves from <code>list</code> the element at position <code>pos</code>, 8937*0a6a1f1dSLionel Sambucreturning the value of the removed element. 8938*0a6a1f1dSLionel SambucWhen <code>pos</code> is an integer between 1 and <code>#list</code>, 8939*0a6a1f1dSLionel Sambucit shifts down the elements 8940*0a6a1f1dSLionel Sambuc<code>list[pos+1], list[pos+2], ···, list[#list]</code> 8941*0a6a1f1dSLionel Sambucand erases element <code>list[#list]</code>; 8942*0a6a1f1dSLionel SambucThe index <code>pos</code> can also be 0 when <code>#list</code> is 0, 8943*0a6a1f1dSLionel Sambucor <code>#list + 1</code>; 8944*0a6a1f1dSLionel Sambucin those cases, the function erases the element <code>list[pos]</code>. 8945*0a6a1f1dSLionel Sambuc 8946*0a6a1f1dSLionel Sambuc 8947*0a6a1f1dSLionel Sambuc<p> 8948*0a6a1f1dSLionel SambucThe default value for <code>pos</code> is <code>#list</code>, 8949*0a6a1f1dSLionel Sambucso that a call <code>table.remove(l)</code> removes the last element 8950*0a6a1f1dSLionel Sambucof list <code>l</code>. 8951*0a6a1f1dSLionel Sambuc 8952*0a6a1f1dSLionel Sambuc 8953*0a6a1f1dSLionel Sambuc 8954*0a6a1f1dSLionel Sambuc 8955*0a6a1f1dSLionel Sambuc<p> 8956*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-table.sort"><code>table.sort (list [, comp])</code></a></h3> 8957*0a6a1f1dSLionel Sambuc 8958*0a6a1f1dSLionel Sambuc 8959*0a6a1f1dSLionel Sambuc<p> 8960*0a6a1f1dSLionel SambucSorts list elements in a given order, <em>in-place</em>, 8961*0a6a1f1dSLionel Sambucfrom <code>list[1]</code> to <code>list[#list]</code>. 896211be35a1SLionel SambucIf <code>comp</code> is given, 8963*0a6a1f1dSLionel Sambucthen it must be a function that receives two list elements 8964*0a6a1f1dSLionel Sambucand returns true when the first element must come 8965*0a6a1f1dSLionel Sambucbefore the second in the final order 8966*0a6a1f1dSLionel Sambuc(so that <code>not comp(list[i+1],list[i])</code> will be true after the sort). 896711be35a1SLionel SambucIf <code>comp</code> is not given, 896811be35a1SLionel Sambucthen the standard Lua operator <code><</code> is used instead. 896911be35a1SLionel Sambuc 897011be35a1SLionel Sambuc 897111be35a1SLionel Sambuc<p> 897211be35a1SLionel SambucThe sort algorithm is not stable; 897311be35a1SLionel Sambucthat is, elements considered equal by the given order 897411be35a1SLionel Sambucmay have their relative positions changed by the sort. 897511be35a1SLionel Sambuc 897611be35a1SLionel Sambuc 897711be35a1SLionel Sambuc 897811be35a1SLionel Sambuc 8979*0a6a1f1dSLionel Sambuc<p> 8980*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-table.unpack"><code>table.unpack (list [, i [, j]])</code></a></h3> 898111be35a1SLionel Sambuc 898211be35a1SLionel Sambuc 898311be35a1SLionel Sambuc<p> 8984*0a6a1f1dSLionel SambucReturns the elements from the given list. 8985*0a6a1f1dSLionel SambucThis function is equivalent to 8986*0a6a1f1dSLionel Sambuc 8987*0a6a1f1dSLionel Sambuc<pre> 8988*0a6a1f1dSLionel Sambuc return list[i], list[i+1], ···, list[j] 8989*0a6a1f1dSLionel Sambuc</pre><p> 8990*0a6a1f1dSLionel SambucBy default, <code>i</code> is 1 and <code>j</code> is <code>#list</code>. 8991*0a6a1f1dSLionel Sambuc 8992*0a6a1f1dSLionel Sambuc 8993*0a6a1f1dSLionel Sambuc 8994*0a6a1f1dSLionel Sambuc 8995*0a6a1f1dSLionel Sambuc 8996*0a6a1f1dSLionel Sambuc 8997*0a6a1f1dSLionel Sambuc 8998*0a6a1f1dSLionel Sambuc<h2>6.7 – <a name="6.7">Mathematical Functions</a></h2> 8999*0a6a1f1dSLionel Sambuc 9000*0a6a1f1dSLionel Sambuc<p> 9001*0a6a1f1dSLionel SambucThis library provides basic mathematical functions. 9002*0a6a1f1dSLionel SambucIt provides all its functions and constants inside the table <a name="pdf-math"><code>math</code></a>. 9003*0a6a1f1dSLionel SambucFunctions with the annotation "<code>integer/float</code>" give 9004*0a6a1f1dSLionel Sambucinteger results for integer arguments 9005*0a6a1f1dSLionel Sambucand float results for float (or mixed) arguments. 9006*0a6a1f1dSLionel SambucRounding functions 9007*0a6a1f1dSLionel Sambuc(<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>) 9008*0a6a1f1dSLionel Sambucreturn an integer when the result fits in the range of an integer, 9009*0a6a1f1dSLionel Sambucor a float otherwise. 901011be35a1SLionel Sambuc 901111be35a1SLionel Sambuc 901211be35a1SLionel Sambuc<p> 901311be35a1SLionel Sambuc<hr><h3><a name="pdf-math.abs"><code>math.abs (x)</code></a></h3> 901411be35a1SLionel Sambuc 901511be35a1SLionel Sambuc 901611be35a1SLionel Sambuc<p> 9017*0a6a1f1dSLionel SambucReturns the absolute value of <code>x</code>. (integer/float) 901811be35a1SLionel Sambuc 901911be35a1SLionel Sambuc 902011be35a1SLionel Sambuc 902111be35a1SLionel Sambuc 902211be35a1SLionel Sambuc<p> 902311be35a1SLionel Sambuc<hr><h3><a name="pdf-math.acos"><code>math.acos (x)</code></a></h3> 902411be35a1SLionel Sambuc 902511be35a1SLionel Sambuc 902611be35a1SLionel Sambuc<p> 902711be35a1SLionel SambucReturns the arc cosine of <code>x</code> (in radians). 902811be35a1SLionel Sambuc 902911be35a1SLionel Sambuc 903011be35a1SLionel Sambuc 903111be35a1SLionel Sambuc 903211be35a1SLionel Sambuc<p> 903311be35a1SLionel Sambuc<hr><h3><a name="pdf-math.asin"><code>math.asin (x)</code></a></h3> 903411be35a1SLionel Sambuc 903511be35a1SLionel Sambuc 903611be35a1SLionel Sambuc<p> 903711be35a1SLionel SambucReturns the arc sine of <code>x</code> (in radians). 903811be35a1SLionel Sambuc 903911be35a1SLionel Sambuc 904011be35a1SLionel Sambuc 904111be35a1SLionel Sambuc 904211be35a1SLionel Sambuc<p> 9043*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-math.atan"><code>math.atan (y [, x])</code></a></h3> 904411be35a1SLionel Sambuc 904511be35a1SLionel Sambuc 904611be35a1SLionel Sambuc<p> 904711be35a1SLionel Sambuc 904811be35a1SLionel SambucReturns the arc tangent of <code>y/x</code> (in radians), 904911be35a1SLionel Sambucbut uses the signs of both parameters to find the 905011be35a1SLionel Sambucquadrant of the result. 905111be35a1SLionel Sambuc(It also handles correctly the case of <code>x</code> being zero.) 905211be35a1SLionel Sambuc 905311be35a1SLionel Sambuc 9054*0a6a1f1dSLionel Sambuc<p> 9055*0a6a1f1dSLionel SambucThe default value for <code>x</code> is 1, 9056*0a6a1f1dSLionel Sambucso that the call <code>math.atan(y)</code> 9057*0a6a1f1dSLionel Sambucreturns the arc tangent of <code>y</code>. 9058*0a6a1f1dSLionel Sambuc 9059*0a6a1f1dSLionel Sambuc 906011be35a1SLionel Sambuc 906111be35a1SLionel Sambuc 906211be35a1SLionel Sambuc<p> 906311be35a1SLionel Sambuc<hr><h3><a name="pdf-math.ceil"><code>math.ceil (x)</code></a></h3> 906411be35a1SLionel Sambuc 906511be35a1SLionel Sambuc 906611be35a1SLionel Sambuc<p> 9067*0a6a1f1dSLionel SambucReturns the smallest integral value larger than or equal to <code>x</code>. 906811be35a1SLionel Sambuc 906911be35a1SLionel Sambuc 907011be35a1SLionel Sambuc 907111be35a1SLionel Sambuc 907211be35a1SLionel Sambuc<p> 907311be35a1SLionel Sambuc<hr><h3><a name="pdf-math.cos"><code>math.cos (x)</code></a></h3> 907411be35a1SLionel Sambuc 907511be35a1SLionel Sambuc 907611be35a1SLionel Sambuc<p> 907711be35a1SLionel SambucReturns the cosine of <code>x</code> (assumed to be in radians). 907811be35a1SLionel Sambuc 907911be35a1SLionel Sambuc 908011be35a1SLionel Sambuc 908111be35a1SLionel Sambuc 908211be35a1SLionel Sambuc<p> 908311be35a1SLionel Sambuc<hr><h3><a name="pdf-math.deg"><code>math.deg (x)</code></a></h3> 908411be35a1SLionel Sambuc 908511be35a1SLionel Sambuc 908611be35a1SLionel Sambuc<p> 9087*0a6a1f1dSLionel SambucConverts the angle <code>x</code> from radians to degrees. 908811be35a1SLionel Sambuc 908911be35a1SLionel Sambuc 909011be35a1SLionel Sambuc 909111be35a1SLionel Sambuc 909211be35a1SLionel Sambuc<p> 909311be35a1SLionel Sambuc<hr><h3><a name="pdf-math.exp"><code>math.exp (x)</code></a></h3> 909411be35a1SLionel Sambuc 909511be35a1SLionel Sambuc 909611be35a1SLionel Sambuc<p> 9097*0a6a1f1dSLionel SambucReturns the value <em>e<sup>x</sup></em> 9098*0a6a1f1dSLionel Sambuc(where <code>e</code> is the base of natural logarithms). 909911be35a1SLionel Sambuc 910011be35a1SLionel Sambuc 910111be35a1SLionel Sambuc 910211be35a1SLionel Sambuc 910311be35a1SLionel Sambuc<p> 910411be35a1SLionel Sambuc<hr><h3><a name="pdf-math.floor"><code>math.floor (x)</code></a></h3> 910511be35a1SLionel Sambuc 910611be35a1SLionel Sambuc 910711be35a1SLionel Sambuc<p> 9108*0a6a1f1dSLionel SambucReturns the largest integral value smaller than or equal to <code>x</code>. 910911be35a1SLionel Sambuc 911011be35a1SLionel Sambuc 911111be35a1SLionel Sambuc 911211be35a1SLionel Sambuc 911311be35a1SLionel Sambuc<p> 911411be35a1SLionel Sambuc<hr><h3><a name="pdf-math.fmod"><code>math.fmod (x, y)</code></a></h3> 911511be35a1SLionel Sambuc 911611be35a1SLionel Sambuc 911711be35a1SLionel Sambuc<p> 911811be35a1SLionel SambucReturns the remainder of the division of <code>x</code> by <code>y</code> 9119*0a6a1f1dSLionel Sambucthat rounds the quotient towards zero. (integer/float) 912011be35a1SLionel Sambuc 912111be35a1SLionel Sambuc 912211be35a1SLionel Sambuc 912311be35a1SLionel Sambuc 912411be35a1SLionel Sambuc<p> 912511be35a1SLionel Sambuc<hr><h3><a name="pdf-math.huge"><code>math.huge</code></a></h3> 912611be35a1SLionel Sambuc 912711be35a1SLionel Sambuc 912811be35a1SLionel Sambuc<p> 9129*0a6a1f1dSLionel SambucThe float value <code>HUGE_VAL</code>, 9130*0a6a1f1dSLionel Sambuca value larger than any other numeric value. 913111be35a1SLionel Sambuc 913211be35a1SLionel Sambuc 913311be35a1SLionel Sambuc 913411be35a1SLionel Sambuc 913511be35a1SLionel Sambuc<p> 9136*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-math.log"><code>math.log (x [, base])</code></a></h3> 913711be35a1SLionel Sambuc 913811be35a1SLionel Sambuc 913911be35a1SLionel Sambuc<p> 9140*0a6a1f1dSLionel SambucReturns the logarithm of <code>x</code> in the given base. 9141*0a6a1f1dSLionel SambucThe default for <code>base</code> is <em>e</em> 9142*0a6a1f1dSLionel Sambuc(so that the function returns the natural logarithm of <code>x</code>). 914311be35a1SLionel Sambuc 914411be35a1SLionel Sambuc 914511be35a1SLionel Sambuc 914611be35a1SLionel Sambuc 914711be35a1SLionel Sambuc<p> 914811be35a1SLionel Sambuc<hr><h3><a name="pdf-math.max"><code>math.max (x, ···)</code></a></h3> 914911be35a1SLionel Sambuc 915011be35a1SLionel Sambuc 915111be35a1SLionel Sambuc<p> 9152*0a6a1f1dSLionel SambucReturns the argument with the maximum value, 9153*0a6a1f1dSLionel Sambucaccording to the Lua operator <code><</code>. (integer/float) 9154*0a6a1f1dSLionel Sambuc 9155*0a6a1f1dSLionel Sambuc 9156*0a6a1f1dSLionel Sambuc 9157*0a6a1f1dSLionel Sambuc 9158*0a6a1f1dSLionel Sambuc<p> 9159*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-math.maxinteger"><code>math.maxinteger</code></a></h3> 9160*0a6a1f1dSLionel SambucAn integer with the maximum value for an integer. 916111be35a1SLionel Sambuc 916211be35a1SLionel Sambuc 916311be35a1SLionel Sambuc 916411be35a1SLionel Sambuc 916511be35a1SLionel Sambuc<p> 916611be35a1SLionel Sambuc<hr><h3><a name="pdf-math.min"><code>math.min (x, ···)</code></a></h3> 916711be35a1SLionel Sambuc 916811be35a1SLionel Sambuc 916911be35a1SLionel Sambuc<p> 9170*0a6a1f1dSLionel SambucReturns the argument with the minimum value, 9171*0a6a1f1dSLionel Sambucaccording to the Lua operator <code><</code>. (integer/float) 9172*0a6a1f1dSLionel Sambuc 9173*0a6a1f1dSLionel Sambuc 9174*0a6a1f1dSLionel Sambuc 9175*0a6a1f1dSLionel Sambuc 9176*0a6a1f1dSLionel Sambuc<p> 9177*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-math.mininteger"><code>math.mininteger</code></a></h3> 9178*0a6a1f1dSLionel SambucAn integer with the minimum value for an integer. 917911be35a1SLionel Sambuc 918011be35a1SLionel Sambuc 918111be35a1SLionel Sambuc 918211be35a1SLionel Sambuc 918311be35a1SLionel Sambuc<p> 918411be35a1SLionel Sambuc<hr><h3><a name="pdf-math.modf"><code>math.modf (x)</code></a></h3> 918511be35a1SLionel Sambuc 918611be35a1SLionel Sambuc 918711be35a1SLionel Sambuc<p> 9188*0a6a1f1dSLionel SambucReturns the integral part of <code>x</code> and the fractional part of <code>x</code>. 9189*0a6a1f1dSLionel SambucIts second result is always a float. 919011be35a1SLionel Sambuc 919111be35a1SLionel Sambuc 919211be35a1SLionel Sambuc 919311be35a1SLionel Sambuc 919411be35a1SLionel Sambuc<p> 919511be35a1SLionel Sambuc<hr><h3><a name="pdf-math.pi"><code>math.pi</code></a></h3> 919611be35a1SLionel Sambuc 919711be35a1SLionel Sambuc 919811be35a1SLionel Sambuc<p> 9199*0a6a1f1dSLionel SambucThe value of <em>π</em>. 920011be35a1SLionel Sambuc 920111be35a1SLionel Sambuc 920211be35a1SLionel Sambuc 920311be35a1SLionel Sambuc 920411be35a1SLionel Sambuc<p> 920511be35a1SLionel Sambuc<hr><h3><a name="pdf-math.rad"><code>math.rad (x)</code></a></h3> 920611be35a1SLionel Sambuc 920711be35a1SLionel Sambuc 920811be35a1SLionel Sambuc<p> 9209*0a6a1f1dSLionel SambucConverts the angle <code>x</code> from degrees to radians. 921011be35a1SLionel Sambuc 921111be35a1SLionel Sambuc 921211be35a1SLionel Sambuc 921311be35a1SLionel Sambuc 921411be35a1SLionel Sambuc<p> 921511be35a1SLionel Sambuc<hr><h3><a name="pdf-math.random"><code>math.random ([m [, n]])</code></a></h3> 921611be35a1SLionel Sambuc 921711be35a1SLionel Sambuc 921811be35a1SLionel Sambuc<p> 9219*0a6a1f1dSLionel SambucWhen called without arguments, 9220*0a6a1f1dSLionel Sambucreturns a pseudo-random float with uniform distribution 9221*0a6a1f1dSLionel Sambucin the range <em>[0,1)</em>. 9222*0a6a1f1dSLionel SambucWhen called with two integers <code>m</code> and <code>n</code>, 9223*0a6a1f1dSLionel Sambuc<code>math.random</code> returns a pseudo-random integer 9224*0a6a1f1dSLionel Sambucwith uniform distribution in the range <em>[m, n]</em>. 9225*0a6a1f1dSLionel Sambuc(The value <em>m-n</em> cannot be negative and must fit in a Lua integer.) 9226*0a6a1f1dSLionel SambucThe call <code>math.random(n)</code> is equivalent to <code>math.random(1,n)</code>. 922711be35a1SLionel Sambuc 922811be35a1SLionel Sambuc 922911be35a1SLionel Sambuc<p> 9230*0a6a1f1dSLionel SambucThis function is an interface to the underling 9231*0a6a1f1dSLionel Sambucpseudo-random generator function provided by C. 9232*0a6a1f1dSLionel SambucNo guarantees can be given for its statistical properties. 923311be35a1SLionel Sambuc 923411be35a1SLionel Sambuc 923511be35a1SLionel Sambuc 923611be35a1SLionel Sambuc 923711be35a1SLionel Sambuc<p> 923811be35a1SLionel Sambuc<hr><h3><a name="pdf-math.randomseed"><code>math.randomseed (x)</code></a></h3> 923911be35a1SLionel Sambuc 924011be35a1SLionel Sambuc 924111be35a1SLionel Sambuc<p> 924211be35a1SLionel SambucSets <code>x</code> as the "seed" 924311be35a1SLionel Sambucfor the pseudo-random generator: 924411be35a1SLionel Sambucequal seeds produce equal sequences of numbers. 924511be35a1SLionel Sambuc 924611be35a1SLionel Sambuc 924711be35a1SLionel Sambuc 924811be35a1SLionel Sambuc 924911be35a1SLionel Sambuc<p> 925011be35a1SLionel Sambuc<hr><h3><a name="pdf-math.sin"><code>math.sin (x)</code></a></h3> 925111be35a1SLionel Sambuc 925211be35a1SLionel Sambuc 925311be35a1SLionel Sambuc<p> 925411be35a1SLionel SambucReturns the sine of <code>x</code> (assumed to be in radians). 925511be35a1SLionel Sambuc 925611be35a1SLionel Sambuc 925711be35a1SLionel Sambuc 925811be35a1SLionel Sambuc 925911be35a1SLionel Sambuc<p> 926011be35a1SLionel Sambuc<hr><h3><a name="pdf-math.sqrt"><code>math.sqrt (x)</code></a></h3> 926111be35a1SLionel Sambuc 926211be35a1SLionel Sambuc 926311be35a1SLionel Sambuc<p> 926411be35a1SLionel SambucReturns the square root of <code>x</code>. 926511be35a1SLionel Sambuc(You can also use the expression <code>x^0.5</code> to compute this value.) 926611be35a1SLionel Sambuc 926711be35a1SLionel Sambuc 926811be35a1SLionel Sambuc 926911be35a1SLionel Sambuc 927011be35a1SLionel Sambuc<p> 927111be35a1SLionel Sambuc<hr><h3><a name="pdf-math.tan"><code>math.tan (x)</code></a></h3> 927211be35a1SLionel Sambuc 927311be35a1SLionel Sambuc 927411be35a1SLionel Sambuc<p> 927511be35a1SLionel SambucReturns the tangent of <code>x</code> (assumed to be in radians). 927611be35a1SLionel Sambuc 927711be35a1SLionel Sambuc 927811be35a1SLionel Sambuc 927911be35a1SLionel Sambuc 928011be35a1SLionel Sambuc<p> 9281*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-math.tointeger"><code>math.tointeger (x)</code></a></h3> 928211be35a1SLionel Sambuc 928311be35a1SLionel Sambuc 928411be35a1SLionel Sambuc<p> 9285*0a6a1f1dSLionel SambucIf the value <code>x</code> is convertible to an integer, 9286*0a6a1f1dSLionel Sambucreturns that integer. 9287*0a6a1f1dSLionel SambucOtherwise, returns <b>nil</b>. 9288*0a6a1f1dSLionel Sambuc 9289*0a6a1f1dSLionel Sambuc 9290*0a6a1f1dSLionel Sambuc 9291*0a6a1f1dSLionel Sambuc 9292*0a6a1f1dSLionel Sambuc<p> 9293*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-math.type"><code>math.type (x)</code></a></h3> 9294*0a6a1f1dSLionel Sambuc 9295*0a6a1f1dSLionel Sambuc 9296*0a6a1f1dSLionel Sambuc<p> 9297*0a6a1f1dSLionel SambucReturns "<code>integer</code>" if <code>x</code> is an integer, 9298*0a6a1f1dSLionel Sambuc"<code>float</code>" if it is a float, 9299*0a6a1f1dSLionel Sambucor <b>nil</b> if <code>x</code> is not a number. 9300*0a6a1f1dSLionel Sambuc 9301*0a6a1f1dSLionel Sambuc 9302*0a6a1f1dSLionel Sambuc 9303*0a6a1f1dSLionel Sambuc 9304*0a6a1f1dSLionel Sambuc<p> 9305*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-math.ult"><code>math.ult (m, n)</code></a></h3> 9306*0a6a1f1dSLionel Sambuc 9307*0a6a1f1dSLionel Sambuc 9308*0a6a1f1dSLionel Sambuc<p> 9309*0a6a1f1dSLionel SambucReturns a boolean, 9310*0a6a1f1dSLionel Sambuctrue if integer <code>m</code> is below integer <code>n</code> when 9311*0a6a1f1dSLionel Sambucthey are compared as unsigned integers. 931211be35a1SLionel Sambuc 931311be35a1SLionel Sambuc 931411be35a1SLionel Sambuc 931511be35a1SLionel Sambuc 931611be35a1SLionel Sambuc 931711be35a1SLionel Sambuc 931811be35a1SLionel Sambuc 9319*0a6a1f1dSLionel Sambuc<h2>6.8 – <a name="6.8">Input and Output Facilities</a></h2> 932011be35a1SLionel Sambuc 932111be35a1SLionel Sambuc<p> 932211be35a1SLionel SambucThe I/O library provides two different styles for file manipulation. 9323*0a6a1f1dSLionel SambucThe first one uses implicit file handles; 932411be35a1SLionel Sambucthat is, there are operations to set a default input file and a 932511be35a1SLionel Sambucdefault output file, 932611be35a1SLionel Sambucand all input/output operations are over these default files. 9327*0a6a1f1dSLionel SambucThe second style uses explicit file handles. 932811be35a1SLionel Sambuc 932911be35a1SLionel Sambuc 933011be35a1SLionel Sambuc<p> 9331*0a6a1f1dSLionel SambucWhen using implicit file handles, 933211be35a1SLionel Sambucall operations are supplied by table <a name="pdf-io"><code>io</code></a>. 9333*0a6a1f1dSLionel SambucWhen using explicit file handles, 9334*0a6a1f1dSLionel Sambucthe operation <a href="#pdf-io.open"><code>io.open</code></a> returns a file handle 9335*0a6a1f1dSLionel Sambucand then all operations are supplied as methods of the file handle. 933611be35a1SLionel Sambuc 933711be35a1SLionel Sambuc 933811be35a1SLionel Sambuc<p> 933911be35a1SLionel SambucThe table <code>io</code> also provides 9340*0a6a1f1dSLionel Sambucthree predefined file handles with their usual meanings from C: 934111be35a1SLionel Sambuc<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>. 934211be35a1SLionel SambucThe I/O library never closes these files. 934311be35a1SLionel Sambuc 934411be35a1SLionel Sambuc 934511be35a1SLionel Sambuc<p> 934611be35a1SLionel SambucUnless otherwise stated, 934711be35a1SLionel Sambucall I/O functions return <b>nil</b> on failure 934811be35a1SLionel Sambuc(plus an error message as a second result and 934911be35a1SLionel Sambuca system-dependent error code as a third result) 935011be35a1SLionel Sambucand some value different from <b>nil</b> on success. 9351*0a6a1f1dSLionel SambucOn non-POSIX systems, 9352*0a6a1f1dSLionel Sambucthe computation of the error message and error code 9353*0a6a1f1dSLionel Sambucin case of errors 9354*0a6a1f1dSLionel Sambucmay be not thread safe, 9355*0a6a1f1dSLionel Sambucbecause they rely on the global C variable <code>errno</code>. 935611be35a1SLionel Sambuc 935711be35a1SLionel Sambuc 935811be35a1SLionel Sambuc<p> 935911be35a1SLionel Sambuc<hr><h3><a name="pdf-io.close"><code>io.close ([file])</code></a></h3> 936011be35a1SLionel Sambuc 936111be35a1SLionel Sambuc 936211be35a1SLionel Sambuc<p> 936311be35a1SLionel SambucEquivalent to <code>file:close()</code>. 936411be35a1SLionel SambucWithout a <code>file</code>, closes the default output file. 936511be35a1SLionel Sambuc 936611be35a1SLionel Sambuc 936711be35a1SLionel Sambuc 936811be35a1SLionel Sambuc 936911be35a1SLionel Sambuc<p> 937011be35a1SLionel Sambuc<hr><h3><a name="pdf-io.flush"><code>io.flush ()</code></a></h3> 937111be35a1SLionel Sambuc 937211be35a1SLionel Sambuc 937311be35a1SLionel Sambuc<p> 9374*0a6a1f1dSLionel SambucEquivalent to <code>io.output():flush()</code>. 937511be35a1SLionel Sambuc 937611be35a1SLionel Sambuc 937711be35a1SLionel Sambuc 937811be35a1SLionel Sambuc 937911be35a1SLionel Sambuc<p> 938011be35a1SLionel Sambuc<hr><h3><a name="pdf-io.input"><code>io.input ([file])</code></a></h3> 938111be35a1SLionel Sambuc 938211be35a1SLionel Sambuc 938311be35a1SLionel Sambuc<p> 938411be35a1SLionel SambucWhen called with a file name, it opens the named file (in text mode), 938511be35a1SLionel Sambucand sets its handle as the default input file. 938611be35a1SLionel SambucWhen called with a file handle, 938711be35a1SLionel Sambucit simply sets this file handle as the default input file. 938811be35a1SLionel SambucWhen called without parameters, 938911be35a1SLionel Sambucit returns the current default input file. 939011be35a1SLionel Sambuc 939111be35a1SLionel Sambuc 939211be35a1SLionel Sambuc<p> 939311be35a1SLionel SambucIn case of errors this function raises the error, 939411be35a1SLionel Sambucinstead of returning an error code. 939511be35a1SLionel Sambuc 939611be35a1SLionel Sambuc 939711be35a1SLionel Sambuc 939811be35a1SLionel Sambuc 939911be35a1SLionel Sambuc<p> 9400*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-io.lines"><code>io.lines ([filename ···])</code></a></h3> 940111be35a1SLionel Sambuc 940211be35a1SLionel Sambuc 940311be35a1SLionel Sambuc<p> 940411be35a1SLionel SambucOpens the given file name in read mode 9405*0a6a1f1dSLionel Sambucand returns an iterator function that 9406*0a6a1f1dSLionel Sambucworks like <code>file:lines(···)</code> over the opened file. 940711be35a1SLionel SambucWhen the iterator function detects the end of file, 9408*0a6a1f1dSLionel Sambucit returns no values (to finish the loop) and automatically closes the file. 940911be35a1SLionel Sambuc 941011be35a1SLionel Sambuc 941111be35a1SLionel Sambuc<p> 941211be35a1SLionel SambucThe call <code>io.lines()</code> (with no file name) is equivalent 9413*0a6a1f1dSLionel Sambucto <code>io.input():lines("*l")</code>; 941411be35a1SLionel Sambucthat is, it iterates over the lines of the default input file. 941511be35a1SLionel SambucIn this case it does not close the file when the loop ends. 941611be35a1SLionel Sambuc 941711be35a1SLionel Sambuc 9418*0a6a1f1dSLionel Sambuc<p> 9419*0a6a1f1dSLionel SambucIn case of errors this function raises the error, 9420*0a6a1f1dSLionel Sambucinstead of returning an error code. 9421*0a6a1f1dSLionel Sambuc 9422*0a6a1f1dSLionel Sambuc 942311be35a1SLionel Sambuc 942411be35a1SLionel Sambuc 942511be35a1SLionel Sambuc<p> 942611be35a1SLionel Sambuc<hr><h3><a name="pdf-io.open"><code>io.open (filename [, mode])</code></a></h3> 942711be35a1SLionel Sambuc 942811be35a1SLionel Sambuc 942911be35a1SLionel Sambuc<p> 943011be35a1SLionel SambucThis function opens a file, 943111be35a1SLionel Sambucin the mode specified in the string <code>mode</code>. 943211be35a1SLionel SambucIt returns a new file handle, 943311be35a1SLionel Sambucor, in case of errors, <b>nil</b> plus an error message. 943411be35a1SLionel Sambuc 943511be35a1SLionel Sambuc 943611be35a1SLionel Sambuc<p> 943711be35a1SLionel SambucThe <code>mode</code> string can be any of the following: 943811be35a1SLionel Sambuc 943911be35a1SLionel Sambuc<ul> 9440*0a6a1f1dSLionel Sambuc<li><b>"<code>r</code>": </b> read mode (the default);</li> 9441*0a6a1f1dSLionel Sambuc<li><b>"<code>w</code>": </b> write mode;</li> 9442*0a6a1f1dSLionel Sambuc<li><b>"<code>a</code>": </b> append mode;</li> 9443*0a6a1f1dSLionel Sambuc<li><b>"<code>r+</code>": </b> update mode, all previous data is preserved;</li> 9444*0a6a1f1dSLionel Sambuc<li><b>"<code>w+</code>": </b> update mode, all previous data is erased;</li> 9445*0a6a1f1dSLionel Sambuc<li><b>"<code>a+</code>": </b> append update mode, previous data is preserved, 944611be35a1SLionel Sambuc writing is only allowed at the end of file.</li> 944711be35a1SLionel Sambuc</ul><p> 944811be35a1SLionel SambucThe <code>mode</code> string can also have a '<code>b</code>' at the end, 944911be35a1SLionel Sambucwhich is needed in some systems to open the file in binary mode. 945011be35a1SLionel Sambuc 945111be35a1SLionel Sambuc 945211be35a1SLionel Sambuc 945311be35a1SLionel Sambuc 945411be35a1SLionel Sambuc<p> 945511be35a1SLionel Sambuc<hr><h3><a name="pdf-io.output"><code>io.output ([file])</code></a></h3> 945611be35a1SLionel Sambuc 945711be35a1SLionel Sambuc 945811be35a1SLionel Sambuc<p> 945911be35a1SLionel SambucSimilar to <a href="#pdf-io.input"><code>io.input</code></a>, but operates over the default output file. 946011be35a1SLionel Sambuc 946111be35a1SLionel Sambuc 946211be35a1SLionel Sambuc 946311be35a1SLionel Sambuc 946411be35a1SLionel Sambuc<p> 946511be35a1SLionel Sambuc<hr><h3><a name="pdf-io.popen"><code>io.popen (prog [, mode])</code></a></h3> 946611be35a1SLionel Sambuc 946711be35a1SLionel Sambuc 946811be35a1SLionel Sambuc<p> 9469*0a6a1f1dSLionel SambucThis function is system dependent and is not available 9470*0a6a1f1dSLionel Sambucon all platforms. 9471*0a6a1f1dSLionel Sambuc 9472*0a6a1f1dSLionel Sambuc 9473*0a6a1f1dSLionel Sambuc<p> 947411be35a1SLionel SambucStarts program <code>prog</code> in a separated process and returns 947511be35a1SLionel Sambuca file handle that you can use to read data from this program 947611be35a1SLionel Sambuc(if <code>mode</code> is <code>"r"</code>, the default) 947711be35a1SLionel Sambucor to write data to this program 947811be35a1SLionel Sambuc(if <code>mode</code> is <code>"w"</code>). 947911be35a1SLionel Sambuc 948011be35a1SLionel Sambuc 948111be35a1SLionel Sambuc 948211be35a1SLionel Sambuc 948311be35a1SLionel Sambuc<p> 948411be35a1SLionel Sambuc<hr><h3><a name="pdf-io.read"><code>io.read (···)</code></a></h3> 948511be35a1SLionel Sambuc 948611be35a1SLionel Sambuc 948711be35a1SLionel Sambuc<p> 9488*0a6a1f1dSLionel SambucEquivalent to <code>io.input():read(···)</code>. 948911be35a1SLionel Sambuc 949011be35a1SLionel Sambuc 949111be35a1SLionel Sambuc 949211be35a1SLionel Sambuc 949311be35a1SLionel Sambuc<p> 949411be35a1SLionel Sambuc<hr><h3><a name="pdf-io.tmpfile"><code>io.tmpfile ()</code></a></h3> 949511be35a1SLionel Sambuc 949611be35a1SLionel Sambuc 949711be35a1SLionel Sambuc<p> 949811be35a1SLionel SambucReturns a handle for a temporary file. 949911be35a1SLionel SambucThis file is opened in update mode 950011be35a1SLionel Sambucand it is automatically removed when the program ends. 950111be35a1SLionel Sambuc 950211be35a1SLionel Sambuc 950311be35a1SLionel Sambuc 950411be35a1SLionel Sambuc 950511be35a1SLionel Sambuc<p> 950611be35a1SLionel Sambuc<hr><h3><a name="pdf-io.type"><code>io.type (obj)</code></a></h3> 950711be35a1SLionel Sambuc 950811be35a1SLionel Sambuc 950911be35a1SLionel Sambuc<p> 951011be35a1SLionel SambucChecks whether <code>obj</code> is a valid file handle. 951111be35a1SLionel SambucReturns the string <code>"file"</code> if <code>obj</code> is an open file handle, 951211be35a1SLionel Sambuc<code>"closed file"</code> if <code>obj</code> is a closed file handle, 951311be35a1SLionel Sambucor <b>nil</b> if <code>obj</code> is not a file handle. 951411be35a1SLionel Sambuc 951511be35a1SLionel Sambuc 951611be35a1SLionel Sambuc 951711be35a1SLionel Sambuc 951811be35a1SLionel Sambuc<p> 951911be35a1SLionel Sambuc<hr><h3><a name="pdf-io.write"><code>io.write (···)</code></a></h3> 952011be35a1SLionel Sambuc 952111be35a1SLionel Sambuc 952211be35a1SLionel Sambuc<p> 9523*0a6a1f1dSLionel SambucEquivalent to <code>io.output():write(···)</code>. 952411be35a1SLionel Sambuc 952511be35a1SLionel Sambuc 952611be35a1SLionel Sambuc 952711be35a1SLionel Sambuc 952811be35a1SLionel Sambuc<p> 952911be35a1SLionel Sambuc<hr><h3><a name="pdf-file:close"><code>file:close ()</code></a></h3> 953011be35a1SLionel Sambuc 953111be35a1SLionel Sambuc 953211be35a1SLionel Sambuc<p> 953311be35a1SLionel SambucCloses <code>file</code>. 953411be35a1SLionel SambucNote that files are automatically closed when 953511be35a1SLionel Sambuctheir handles are garbage collected, 953611be35a1SLionel Sambucbut that takes an unpredictable amount of time to happen. 953711be35a1SLionel Sambuc 953811be35a1SLionel Sambuc 9539*0a6a1f1dSLionel Sambuc<p> 9540*0a6a1f1dSLionel SambucWhen closing a file handle created with <a href="#pdf-io.popen"><code>io.popen</code></a>, 9541*0a6a1f1dSLionel Sambuc<a href="#pdf-file:close"><code>file:close</code></a> returns the same values 9542*0a6a1f1dSLionel Sambucreturned by <a href="#pdf-os.execute"><code>os.execute</code></a>. 9543*0a6a1f1dSLionel Sambuc 9544*0a6a1f1dSLionel Sambuc 954511be35a1SLionel Sambuc 954611be35a1SLionel Sambuc 954711be35a1SLionel Sambuc<p> 954811be35a1SLionel Sambuc<hr><h3><a name="pdf-file:flush"><code>file:flush ()</code></a></h3> 954911be35a1SLionel Sambuc 955011be35a1SLionel Sambuc 955111be35a1SLionel Sambuc<p> 955211be35a1SLionel SambucSaves any written data to <code>file</code>. 955311be35a1SLionel Sambuc 955411be35a1SLionel Sambuc 955511be35a1SLionel Sambuc 955611be35a1SLionel Sambuc 955711be35a1SLionel Sambuc<p> 9558*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-file:lines"><code>file:lines (···)</code></a></h3> 955911be35a1SLionel Sambuc 956011be35a1SLionel Sambuc 956111be35a1SLionel Sambuc<p> 956211be35a1SLionel SambucReturns an iterator function that, 956311be35a1SLionel Sambuceach time it is called, 9564*0a6a1f1dSLionel Sambucreads the file according to the given formats. 9565*0a6a1f1dSLionel SambucWhen no format is given, 9566*0a6a1f1dSLionel Sambucuses "<code>l</code>" as a default. 9567*0a6a1f1dSLionel SambucAs an example, the construction 956811be35a1SLionel Sambuc 956911be35a1SLionel Sambuc<pre> 9570*0a6a1f1dSLionel Sambuc for c in file:lines(1) do <em>body</em> end 957111be35a1SLionel Sambuc</pre><p> 9572*0a6a1f1dSLionel Sambucwill iterate over all characters of the file, 9573*0a6a1f1dSLionel Sambucstarting at the current position. 9574*0a6a1f1dSLionel SambucUnlike <a href="#pdf-io.lines"><code>io.lines</code></a>, this function does not close the file 9575*0a6a1f1dSLionel Sambucwhen the loop ends. 9576*0a6a1f1dSLionel Sambuc 9577*0a6a1f1dSLionel Sambuc 9578*0a6a1f1dSLionel Sambuc<p> 9579*0a6a1f1dSLionel SambucIn case of errors this function raises the error, 9580*0a6a1f1dSLionel Sambucinstead of returning an error code. 958111be35a1SLionel Sambuc 958211be35a1SLionel Sambuc 958311be35a1SLionel Sambuc 958411be35a1SLionel Sambuc 958511be35a1SLionel Sambuc<p> 958611be35a1SLionel Sambuc<hr><h3><a name="pdf-file:read"><code>file:read (···)</code></a></h3> 958711be35a1SLionel Sambuc 958811be35a1SLionel Sambuc 958911be35a1SLionel Sambuc<p> 959011be35a1SLionel SambucReads the file <code>file</code>, 959111be35a1SLionel Sambucaccording to the given formats, which specify what to read. 959211be35a1SLionel SambucFor each format, 9593*0a6a1f1dSLionel Sambucthe function returns a string or a number with the characters read, 959411be35a1SLionel Sambucor <b>nil</b> if it cannot read data with the specified format. 9595*0a6a1f1dSLionel Sambuc(In this latter case, 9596*0a6a1f1dSLionel Sambucthe function does not read subsequent formats.) 959711be35a1SLionel SambucWhen called without formats, 9598*0a6a1f1dSLionel Sambucit uses a default format that reads the next line 959911be35a1SLionel Sambuc(see below). 960011be35a1SLionel Sambuc 960111be35a1SLionel Sambuc 960211be35a1SLionel Sambuc<p> 960311be35a1SLionel SambucThe available formats are 960411be35a1SLionel Sambuc 960511be35a1SLionel Sambuc<ul> 960611be35a1SLionel Sambuc 9607*0a6a1f1dSLionel Sambuc<li><b>"<code>n</code>": </b> 9608*0a6a1f1dSLionel Sambucreads a numeral and returns it as a float or an integer, 9609*0a6a1f1dSLionel Sambucfollowing the lexical conventions of Lua. 9610*0a6a1f1dSLionel Sambuc(The numeral may have leading spaces and a sign.) 9611*0a6a1f1dSLionel SambucThis format always reads the longest input sequence that 9612*0a6a1f1dSLionel Sambucis a valid prefix for a numeral; 9613*0a6a1f1dSLionel Sambucif that prefix does not form a valid numeral 9614*0a6a1f1dSLionel Sambuc(e.g., an empty string, "<code>0x</code>", or "<code>3.4e-</code>"), 9615*0a6a1f1dSLionel Sambucit is discarded and the function returns <b>nil</b>. 961611be35a1SLionel Sambuc</li> 961711be35a1SLionel Sambuc 9618*0a6a1f1dSLionel Sambuc<li><b>"<code>a</code>": </b> 961911be35a1SLionel Sambucreads the whole file, starting at the current position. 962011be35a1SLionel SambucOn end of file, it returns the empty string. 962111be35a1SLionel Sambuc</li> 962211be35a1SLionel Sambuc 9623*0a6a1f1dSLionel Sambuc<li><b>"<code>l</code>": </b> 9624*0a6a1f1dSLionel Sambucreads the next line skipping the end of line, 962511be35a1SLionel Sambucreturning <b>nil</b> on end of file. 962611be35a1SLionel SambucThis is the default format. 962711be35a1SLionel Sambuc</li> 962811be35a1SLionel Sambuc 9629*0a6a1f1dSLionel Sambuc<li><b>"<code>L</code>": </b> 9630*0a6a1f1dSLionel Sambucreads the next line keeping the end-of-line character (if present), 963111be35a1SLionel Sambucreturning <b>nil</b> on end of file. 9632*0a6a1f1dSLionel Sambuc</li> 9633*0a6a1f1dSLionel Sambuc 9634*0a6a1f1dSLionel Sambuc<li><b><em>number</em>: </b> 9635*0a6a1f1dSLionel Sambucreads a string with up to this number of bytes, 9636*0a6a1f1dSLionel Sambucreturning <b>nil</b> on end of file. 9637*0a6a1f1dSLionel SambucIf <code>number</code> is zero, 963811be35a1SLionel Sambucit reads nothing and returns an empty string, 963911be35a1SLionel Sambucor <b>nil</b> on end of file. 964011be35a1SLionel Sambuc</li> 964111be35a1SLionel Sambuc 9642*0a6a1f1dSLionel Sambuc</ul><p> 9643*0a6a1f1dSLionel SambucThe formats "<code>l</code>" and "<code>L</code>" should be used only for text files. 9644*0a6a1f1dSLionel Sambuc 964511be35a1SLionel Sambuc 964611be35a1SLionel Sambuc 964711be35a1SLionel Sambuc 964811be35a1SLionel Sambuc<p> 9649*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-file:seek"><code>file:seek ([whence [, offset]])</code></a></h3> 965011be35a1SLionel Sambuc 965111be35a1SLionel Sambuc 965211be35a1SLionel Sambuc<p> 965311be35a1SLionel SambucSets and gets the file position, 965411be35a1SLionel Sambucmeasured from the beginning of the file, 965511be35a1SLionel Sambucto the position given by <code>offset</code> plus a base 965611be35a1SLionel Sambucspecified by the string <code>whence</code>, as follows: 965711be35a1SLionel Sambuc 965811be35a1SLionel Sambuc<ul> 9659*0a6a1f1dSLionel Sambuc<li><b>"<code>set</code>": </b> base is position 0 (beginning of the file);</li> 9660*0a6a1f1dSLionel Sambuc<li><b>"<code>cur</code>": </b> base is current position;</li> 9661*0a6a1f1dSLionel Sambuc<li><b>"<code>end</code>": </b> base is end of file;</li> 966211be35a1SLionel Sambuc</ul><p> 9663*0a6a1f1dSLionel SambucIn case of success, <code>seek</code> returns the final file position, 966411be35a1SLionel Sambucmeasured in bytes from the beginning of the file. 9665*0a6a1f1dSLionel SambucIf <code>seek</code> fails, it returns <b>nil</b>, 966611be35a1SLionel Sambucplus a string describing the error. 966711be35a1SLionel Sambuc 966811be35a1SLionel Sambuc 966911be35a1SLionel Sambuc<p> 967011be35a1SLionel SambucThe default value for <code>whence</code> is <code>"cur"</code>, 967111be35a1SLionel Sambucand for <code>offset</code> is 0. 967211be35a1SLionel SambucTherefore, the call <code>file:seek()</code> returns the current 967311be35a1SLionel Sambucfile position, without changing it; 967411be35a1SLionel Sambucthe call <code>file:seek("set")</code> sets the position to the 967511be35a1SLionel Sambucbeginning of the file (and returns 0); 967611be35a1SLionel Sambucand the call <code>file:seek("end")</code> sets the position to the 967711be35a1SLionel Sambucend of the file, and returns its size. 967811be35a1SLionel Sambuc 967911be35a1SLionel Sambuc 968011be35a1SLionel Sambuc 968111be35a1SLionel Sambuc 968211be35a1SLionel Sambuc<p> 968311be35a1SLionel Sambuc<hr><h3><a name="pdf-file:setvbuf"><code>file:setvbuf (mode [, size])</code></a></h3> 968411be35a1SLionel Sambuc 968511be35a1SLionel Sambuc 968611be35a1SLionel Sambuc<p> 968711be35a1SLionel SambucSets the buffering mode for an output file. 968811be35a1SLionel SambucThere are three available modes: 968911be35a1SLionel Sambuc 969011be35a1SLionel Sambuc<ul> 969111be35a1SLionel Sambuc 9692*0a6a1f1dSLionel Sambuc<li><b>"<code>no</code>": </b> 969311be35a1SLionel Sambucno buffering; the result of any output operation appears immediately. 969411be35a1SLionel Sambuc</li> 969511be35a1SLionel Sambuc 9696*0a6a1f1dSLionel Sambuc<li><b>"<code>full</code>": </b> 969711be35a1SLionel Sambucfull buffering; output operation is performed only 9698*0a6a1f1dSLionel Sambucwhen the buffer is full or when 9699*0a6a1f1dSLionel Sambucyou explicitly <code>flush</code> the file (see <a href="#pdf-io.flush"><code>io.flush</code></a>). 970011be35a1SLionel Sambuc</li> 970111be35a1SLionel Sambuc 9702*0a6a1f1dSLionel Sambuc<li><b>"<code>line</code>": </b> 970311be35a1SLionel Sambucline buffering; output is buffered until a newline is output 970411be35a1SLionel Sambucor there is any input from some special files 970511be35a1SLionel Sambuc(such as a terminal device). 970611be35a1SLionel Sambuc</li> 970711be35a1SLionel Sambuc 970811be35a1SLionel Sambuc</ul><p> 970911be35a1SLionel SambucFor the last two cases, <code>size</code> 971011be35a1SLionel Sambucspecifies the size of the buffer, in bytes. 971111be35a1SLionel SambucThe default is an appropriate size. 971211be35a1SLionel Sambuc 971311be35a1SLionel Sambuc 971411be35a1SLionel Sambuc 971511be35a1SLionel Sambuc 971611be35a1SLionel Sambuc<p> 971711be35a1SLionel Sambuc<hr><h3><a name="pdf-file:write"><code>file:write (···)</code></a></h3> 971811be35a1SLionel Sambuc 971911be35a1SLionel Sambuc 972011be35a1SLionel Sambuc<p> 9721*0a6a1f1dSLionel SambucWrites the value of each of its arguments to <code>file</code>. 972211be35a1SLionel SambucThe arguments must be strings or numbers. 9723*0a6a1f1dSLionel Sambuc 9724*0a6a1f1dSLionel Sambuc 9725*0a6a1f1dSLionel Sambuc<p> 9726*0a6a1f1dSLionel SambucIn case of success, this function returns <code>file</code>. 9727*0a6a1f1dSLionel SambucOtherwise it returns <b>nil</b> plus a string describing the error. 972811be35a1SLionel Sambuc 972911be35a1SLionel Sambuc 973011be35a1SLionel Sambuc 973111be35a1SLionel Sambuc 973211be35a1SLionel Sambuc 973311be35a1SLionel Sambuc 973411be35a1SLionel Sambuc 9735*0a6a1f1dSLionel Sambuc<h2>6.9 – <a name="6.9">Operating System Facilities</a></h2> 973611be35a1SLionel Sambuc 973711be35a1SLionel Sambuc<p> 973811be35a1SLionel SambucThis library is implemented through table <a name="pdf-os"><code>os</code></a>. 973911be35a1SLionel Sambuc 974011be35a1SLionel Sambuc 974111be35a1SLionel Sambuc<p> 974211be35a1SLionel Sambuc<hr><h3><a name="pdf-os.clock"><code>os.clock ()</code></a></h3> 974311be35a1SLionel Sambuc 974411be35a1SLionel Sambuc 974511be35a1SLionel Sambuc<p> 974611be35a1SLionel SambucReturns an approximation of the amount in seconds of CPU time 974711be35a1SLionel Sambucused by the program. 974811be35a1SLionel Sambuc 974911be35a1SLionel Sambuc 975011be35a1SLionel Sambuc 975111be35a1SLionel Sambuc 975211be35a1SLionel Sambuc<p> 975311be35a1SLionel Sambuc<hr><h3><a name="pdf-os.date"><code>os.date ([format [, time]])</code></a></h3> 975411be35a1SLionel Sambuc 975511be35a1SLionel Sambuc 975611be35a1SLionel Sambuc<p> 975711be35a1SLionel SambucReturns a string or a table containing date and time, 975811be35a1SLionel Sambucformatted according to the given string <code>format</code>. 975911be35a1SLionel Sambuc 976011be35a1SLionel Sambuc 976111be35a1SLionel Sambuc<p> 976211be35a1SLionel SambucIf the <code>time</code> argument is present, 976311be35a1SLionel Sambucthis is the time to be formatted 976411be35a1SLionel Sambuc(see the <a href="#pdf-os.time"><code>os.time</code></a> function for a description of this value). 976511be35a1SLionel SambucOtherwise, <code>date</code> formats the current time. 976611be35a1SLionel Sambuc 976711be35a1SLionel Sambuc 976811be35a1SLionel Sambuc<p> 976911be35a1SLionel SambucIf <code>format</code> starts with '<code>!</code>', 977011be35a1SLionel Sambucthen the date is formatted in Coordinated Universal Time. 977111be35a1SLionel SambucAfter this optional character, 977211be35a1SLionel Sambucif <code>format</code> is the string "<code>*t</code>", 977311be35a1SLionel Sambucthen <code>date</code> returns a table with the following fields: 9774*0a6a1f1dSLionel Sambuc<code>year</code> (four digits), <code>month</code> (1–12), <code>day</code> (1–31), 9775*0a6a1f1dSLionel Sambuc<code>hour</code> (0–23), <code>min</code> (0–59), <code>sec</code> (0–61), 977611be35a1SLionel Sambuc<code>wday</code> (weekday, Sunday is 1), 977711be35a1SLionel Sambuc<code>yday</code> (day of the year), 977811be35a1SLionel Sambucand <code>isdst</code> (daylight saving flag, a boolean). 9779*0a6a1f1dSLionel SambucThis last field may be absent 9780*0a6a1f1dSLionel Sambucif the information is not available. 978111be35a1SLionel Sambuc 978211be35a1SLionel Sambuc 978311be35a1SLionel Sambuc<p> 978411be35a1SLionel SambucIf <code>format</code> is not "<code>*t</code>", 978511be35a1SLionel Sambucthen <code>date</code> returns the date as a string, 9786*0a6a1f1dSLionel Sambucformatted according to the same rules as the ISO C function <code>strftime</code>. 978711be35a1SLionel Sambuc 978811be35a1SLionel Sambuc 978911be35a1SLionel Sambuc<p> 979011be35a1SLionel SambucWhen called without arguments, 979111be35a1SLionel Sambuc<code>date</code> returns a reasonable date and time representation that depends on 979211be35a1SLionel Sambucthe host system and on the current locale 979311be35a1SLionel Sambuc(that is, <code>os.date()</code> is equivalent to <code>os.date("%c")</code>). 979411be35a1SLionel Sambuc 979511be35a1SLionel Sambuc 9796*0a6a1f1dSLionel Sambuc<p> 9797*0a6a1f1dSLionel SambucOn non-POSIX systems, 9798*0a6a1f1dSLionel Sambucthis function may be not thread safe 9799*0a6a1f1dSLionel Sambucbecause of its reliance on C function <code>gmtime</code> and C function <code>localtime</code>. 9800*0a6a1f1dSLionel Sambuc 9801*0a6a1f1dSLionel Sambuc 980211be35a1SLionel Sambuc 980311be35a1SLionel Sambuc 980411be35a1SLionel Sambuc<p> 980511be35a1SLionel Sambuc<hr><h3><a name="pdf-os.difftime"><code>os.difftime (t2, t1)</code></a></h3> 980611be35a1SLionel Sambuc 980711be35a1SLionel Sambuc 980811be35a1SLionel Sambuc<p> 9809*0a6a1f1dSLionel SambucReturns the difference, in seconds, 9810*0a6a1f1dSLionel Sambucfrom time <code>t1</code> to time <code>t2</code> 9811*0a6a1f1dSLionel Sambuc(where the times are values returned by <a href="#pdf-os.time"><code>os.time</code></a>). 981211be35a1SLionel SambucIn POSIX, Windows, and some other systems, 981311be35a1SLionel Sambucthis value is exactly <code>t2</code><em>-</em><code>t1</code>. 981411be35a1SLionel Sambuc 981511be35a1SLionel Sambuc 981611be35a1SLionel Sambuc 981711be35a1SLionel Sambuc 981811be35a1SLionel Sambuc<p> 981911be35a1SLionel Sambuc<hr><h3><a name="pdf-os.execute"><code>os.execute ([command])</code></a></h3> 982011be35a1SLionel Sambuc 982111be35a1SLionel Sambuc 982211be35a1SLionel Sambuc<p> 9823*0a6a1f1dSLionel SambucThis function is equivalent to the ISO C function <code>system</code>. 982411be35a1SLionel SambucIt passes <code>command</code> to be executed by an operating system shell. 9825*0a6a1f1dSLionel SambucIts first result is <b>true</b> 9826*0a6a1f1dSLionel Sambucif the command terminated successfully, 9827*0a6a1f1dSLionel Sambucor <b>nil</b> otherwise. 9828*0a6a1f1dSLionel SambucAfter this first result 9829*0a6a1f1dSLionel Sambucthe function returns a string plus a number, 9830*0a6a1f1dSLionel Sambucas follows: 9831*0a6a1f1dSLionel Sambuc 9832*0a6a1f1dSLionel Sambuc<ul> 9833*0a6a1f1dSLionel Sambuc 9834*0a6a1f1dSLionel Sambuc<li><b>"<code>exit</code>": </b> 9835*0a6a1f1dSLionel Sambucthe command terminated normally; 9836*0a6a1f1dSLionel Sambucthe following number is the exit status of the command. 9837*0a6a1f1dSLionel Sambuc</li> 9838*0a6a1f1dSLionel Sambuc 9839*0a6a1f1dSLionel Sambuc<li><b>"<code>signal</code>": </b> 9840*0a6a1f1dSLionel Sambucthe command was terminated by a signal; 9841*0a6a1f1dSLionel Sambucthe following number is the signal that terminated the command. 9842*0a6a1f1dSLionel Sambuc</li> 9843*0a6a1f1dSLionel Sambuc 9844*0a6a1f1dSLionel Sambuc</ul> 9845*0a6a1f1dSLionel Sambuc 9846*0a6a1f1dSLionel Sambuc<p> 9847*0a6a1f1dSLionel SambucWhen called without a <code>command</code>, 9848*0a6a1f1dSLionel Sambuc<code>os.execute</code> returns a boolean that is true if a shell is available. 984911be35a1SLionel Sambuc 985011be35a1SLionel Sambuc 985111be35a1SLionel Sambuc 985211be35a1SLionel Sambuc 985311be35a1SLionel Sambuc<p> 9854*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-os.exit"><code>os.exit ([code [, close]])</code></a></h3> 985511be35a1SLionel Sambuc 985611be35a1SLionel Sambuc 985711be35a1SLionel Sambuc<p> 9858*0a6a1f1dSLionel SambucCalls the ISO C function <code>exit</code> to terminate the host program. 9859*0a6a1f1dSLionel SambucIf <code>code</code> is <b>true</b>, 9860*0a6a1f1dSLionel Sambucthe returned status is <code>EXIT_SUCCESS</code>; 9861*0a6a1f1dSLionel Sambucif <code>code</code> is <b>false</b>, 9862*0a6a1f1dSLionel Sambucthe returned status is <code>EXIT_FAILURE</code>; 9863*0a6a1f1dSLionel Sambucif <code>code</code> is a number, 9864*0a6a1f1dSLionel Sambucthe returned status is this number. 9865*0a6a1f1dSLionel SambucThe default value for <code>code</code> is <b>true</b>. 9866*0a6a1f1dSLionel Sambuc 9867*0a6a1f1dSLionel Sambuc 9868*0a6a1f1dSLionel Sambuc<p> 9869*0a6a1f1dSLionel SambucIf the optional second argument <code>close</code> is true, 9870*0a6a1f1dSLionel Sambuccloses the Lua state before exiting. 987111be35a1SLionel Sambuc 987211be35a1SLionel Sambuc 987311be35a1SLionel Sambuc 987411be35a1SLionel Sambuc 987511be35a1SLionel Sambuc<p> 987611be35a1SLionel Sambuc<hr><h3><a name="pdf-os.getenv"><code>os.getenv (varname)</code></a></h3> 987711be35a1SLionel Sambuc 987811be35a1SLionel Sambuc 987911be35a1SLionel Sambuc<p> 988011be35a1SLionel SambucReturns the value of the process environment variable <code>varname</code>, 988111be35a1SLionel Sambucor <b>nil</b> if the variable is not defined. 988211be35a1SLionel Sambuc 988311be35a1SLionel Sambuc 988411be35a1SLionel Sambuc 988511be35a1SLionel Sambuc 988611be35a1SLionel Sambuc<p> 988711be35a1SLionel Sambuc<hr><h3><a name="pdf-os.remove"><code>os.remove (filename)</code></a></h3> 988811be35a1SLionel Sambuc 988911be35a1SLionel Sambuc 989011be35a1SLionel Sambuc<p> 9891*0a6a1f1dSLionel SambucDeletes the file (or empty directory, on POSIX systems) 9892*0a6a1f1dSLionel Sambucwith the given name. 989311be35a1SLionel SambucIf this function fails, it returns <b>nil</b>, 9894*0a6a1f1dSLionel Sambucplus a string describing the error and the error code. 989511be35a1SLionel Sambuc 989611be35a1SLionel Sambuc 989711be35a1SLionel Sambuc 989811be35a1SLionel Sambuc 989911be35a1SLionel Sambuc<p> 990011be35a1SLionel Sambuc<hr><h3><a name="pdf-os.rename"><code>os.rename (oldname, newname)</code></a></h3> 990111be35a1SLionel Sambuc 990211be35a1SLionel Sambuc 990311be35a1SLionel Sambuc<p> 990411be35a1SLionel SambucRenames file or directory named <code>oldname</code> to <code>newname</code>. 990511be35a1SLionel SambucIf this function fails, it returns <b>nil</b>, 9906*0a6a1f1dSLionel Sambucplus a string describing the error and the error code. 990711be35a1SLionel Sambuc 990811be35a1SLionel Sambuc 990911be35a1SLionel Sambuc 991011be35a1SLionel Sambuc 991111be35a1SLionel Sambuc<p> 991211be35a1SLionel Sambuc<hr><h3><a name="pdf-os.setlocale"><code>os.setlocale (locale [, category])</code></a></h3> 991311be35a1SLionel Sambuc 991411be35a1SLionel Sambuc 991511be35a1SLionel Sambuc<p> 991611be35a1SLionel SambucSets the current locale of the program. 9917*0a6a1f1dSLionel Sambuc<code>locale</code> is a system-dependent string specifying a locale; 991811be35a1SLionel Sambuc<code>category</code> is an optional string describing which category to change: 991911be35a1SLionel Sambuc<code>"all"</code>, <code>"collate"</code>, <code>"ctype"</code>, 992011be35a1SLionel Sambuc<code>"monetary"</code>, <code>"numeric"</code>, or <code>"time"</code>; 992111be35a1SLionel Sambucthe default category is <code>"all"</code>. 992211be35a1SLionel SambucThe function returns the name of the new locale, 992311be35a1SLionel Sambucor <b>nil</b> if the request cannot be honored. 992411be35a1SLionel Sambuc 992511be35a1SLionel Sambuc 992611be35a1SLionel Sambuc<p> 992711be35a1SLionel SambucIf <code>locale</code> is the empty string, 992811be35a1SLionel Sambucthe current locale is set to an implementation-defined native locale. 992911be35a1SLionel SambucIf <code>locale</code> is the string "<code>C</code>", 993011be35a1SLionel Sambucthe current locale is set to the standard C locale. 993111be35a1SLionel Sambuc 993211be35a1SLionel Sambuc 993311be35a1SLionel Sambuc<p> 993411be35a1SLionel SambucWhen called with <b>nil</b> as the first argument, 993511be35a1SLionel Sambucthis function only returns the name of the current locale 993611be35a1SLionel Sambucfor the given category. 993711be35a1SLionel Sambuc 993811be35a1SLionel Sambuc 9939*0a6a1f1dSLionel Sambuc<p> 9940*0a6a1f1dSLionel SambucThis function may be not thread safe 9941*0a6a1f1dSLionel Sambucbecause of its reliance on C function <code>setlocale</code>. 9942*0a6a1f1dSLionel Sambuc 9943*0a6a1f1dSLionel Sambuc 994411be35a1SLionel Sambuc 994511be35a1SLionel Sambuc 994611be35a1SLionel Sambuc<p> 994711be35a1SLionel Sambuc<hr><h3><a name="pdf-os.time"><code>os.time ([table])</code></a></h3> 994811be35a1SLionel Sambuc 994911be35a1SLionel Sambuc 995011be35a1SLionel Sambuc<p> 995111be35a1SLionel SambucReturns the current time when called without arguments, 9952*0a6a1f1dSLionel Sambucor a time representing the local date and time specified by the given table. 995311be35a1SLionel SambucThis table must have fields <code>year</code>, <code>month</code>, and <code>day</code>, 9954*0a6a1f1dSLionel Sambucand may have fields 9955*0a6a1f1dSLionel Sambuc<code>hour</code> (default is 12), 9956*0a6a1f1dSLionel Sambuc<code>min</code> (default is 0), 9957*0a6a1f1dSLionel Sambuc<code>sec</code> (default is 0), 9958*0a6a1f1dSLionel Sambucand <code>isdst</code> (default is <b>nil</b>). 9959*0a6a1f1dSLionel SambucOther fields are ignored. 9960*0a6a1f1dSLionel SambucFor a description of these fields, see the <a href="#pdf-os.date"><code>os.date</code></a> function. 9961*0a6a1f1dSLionel Sambuc 9962*0a6a1f1dSLionel Sambuc 9963*0a6a1f1dSLionel Sambuc<p> 9964*0a6a1f1dSLionel SambucThe values in these fields do not need to be inside their valid ranges. 9965*0a6a1f1dSLionel SambucFor instance, if <code>sec</code> is -10, 9966*0a6a1f1dSLionel Sambucit means -10 seconds from the time specified by the other fields; 9967*0a6a1f1dSLionel Sambucif <code>hour</code> is 1000, 9968*0a6a1f1dSLionel Sambucit means +1000 hours from the time specified by the other fields. 996911be35a1SLionel Sambuc 997011be35a1SLionel Sambuc 997111be35a1SLionel Sambuc<p> 997211be35a1SLionel SambucThe returned value is a number, whose meaning depends on your system. 9973*0a6a1f1dSLionel SambucIn POSIX, Windows, and some other systems, 9974*0a6a1f1dSLionel Sambucthis number counts the number 997511be35a1SLionel Sambucof seconds since some given start time (the "epoch"). 997611be35a1SLionel SambucIn other systems, the meaning is not specified, 997711be35a1SLionel Sambucand the number returned by <code>time</code> can be used only as an argument to 9978*0a6a1f1dSLionel Sambuc<a href="#pdf-os.date"><code>os.date</code></a> and <a href="#pdf-os.difftime"><code>os.difftime</code></a>. 997911be35a1SLionel Sambuc 998011be35a1SLionel Sambuc 998111be35a1SLionel Sambuc 998211be35a1SLionel Sambuc 998311be35a1SLionel Sambuc<p> 998411be35a1SLionel Sambuc<hr><h3><a name="pdf-os.tmpname"><code>os.tmpname ()</code></a></h3> 998511be35a1SLionel Sambuc 998611be35a1SLionel Sambuc 998711be35a1SLionel Sambuc<p> 998811be35a1SLionel SambucReturns a string with a file name that can 998911be35a1SLionel Sambucbe used for a temporary file. 999011be35a1SLionel SambucThe file must be explicitly opened before its use 999111be35a1SLionel Sambucand explicitly removed when no longer needed. 999211be35a1SLionel Sambuc 999311be35a1SLionel Sambuc 999411be35a1SLionel Sambuc<p> 9995*0a6a1f1dSLionel SambucOn POSIX systems, 999611be35a1SLionel Sambucthis function also creates a file with that name, 999711be35a1SLionel Sambucto avoid security risks. 999811be35a1SLionel Sambuc(Someone else might create the file with wrong permissions 999911be35a1SLionel Sambucin the time between getting the name and creating the file.) 1000011be35a1SLionel SambucYou still have to open the file to use it 1000111be35a1SLionel Sambucand to remove it (even if you do not use it). 1000211be35a1SLionel Sambuc 1000311be35a1SLionel Sambuc 1000411be35a1SLionel Sambuc<p> 1000511be35a1SLionel SambucWhen possible, 1000611be35a1SLionel Sambucyou may prefer to use <a href="#pdf-io.tmpfile"><code>io.tmpfile</code></a>, 1000711be35a1SLionel Sambucwhich automatically removes the file when the program ends. 1000811be35a1SLionel Sambuc 1000911be35a1SLionel Sambuc 1001011be35a1SLionel Sambuc 1001111be35a1SLionel Sambuc 1001211be35a1SLionel Sambuc 1001311be35a1SLionel Sambuc 1001411be35a1SLionel Sambuc 10015*0a6a1f1dSLionel Sambuc<h2>6.10 – <a name="6.10">The Debug Library</a></h2> 1001611be35a1SLionel Sambuc 1001711be35a1SLionel Sambuc<p> 1001811be35a1SLionel SambucThis library provides 10019*0a6a1f1dSLionel Sambucthe functionality of the debug interface (<a href="#4.9">§4.9</a>) to Lua programs. 1002011be35a1SLionel SambucYou should exert care when using this library. 10021*0a6a1f1dSLionel SambucSeveral of its functions 10022*0a6a1f1dSLionel Sambucviolate basic assumptions about Lua code 1002311be35a1SLionel Sambuc(e.g., that variables local to a function 10024*0a6a1f1dSLionel Sambuccannot be accessed from outside; 10025*0a6a1f1dSLionel Sambucthat userdata metatables cannot be changed by Lua code; 10026*0a6a1f1dSLionel Sambucthat Lua programs do not crash) 1002711be35a1SLionel Sambucand therefore can compromise otherwise secure code. 10028*0a6a1f1dSLionel SambucMoreover, some functions in this library may be slow. 1002911be35a1SLionel Sambuc 1003011be35a1SLionel Sambuc 1003111be35a1SLionel Sambuc<p> 1003211be35a1SLionel SambucAll functions in this library are provided 1003311be35a1SLionel Sambucinside the <a name="pdf-debug"><code>debug</code></a> table. 1003411be35a1SLionel SambucAll functions that operate over a thread 1003511be35a1SLionel Sambuchave an optional first argument which is the 1003611be35a1SLionel Sambucthread to operate over. 1003711be35a1SLionel SambucThe default is always the current thread. 1003811be35a1SLionel Sambuc 1003911be35a1SLionel Sambuc 1004011be35a1SLionel Sambuc<p> 1004111be35a1SLionel Sambuc<hr><h3><a name="pdf-debug.debug"><code>debug.debug ()</code></a></h3> 1004211be35a1SLionel Sambuc 1004311be35a1SLionel Sambuc 1004411be35a1SLionel Sambuc<p> 1004511be35a1SLionel SambucEnters an interactive mode with the user, 1004611be35a1SLionel Sambucrunning each string that the user enters. 1004711be35a1SLionel SambucUsing simple commands and other debug facilities, 1004811be35a1SLionel Sambucthe user can inspect global and local variables, 1004911be35a1SLionel Sambucchange their values, evaluate expressions, and so on. 1005011be35a1SLionel SambucA line containing only the word <code>cont</code> finishes this function, 1005111be35a1SLionel Sambucso that the caller continues its execution. 1005211be35a1SLionel Sambuc 1005311be35a1SLionel Sambuc 1005411be35a1SLionel Sambuc<p> 1005511be35a1SLionel SambucNote that commands for <code>debug.debug</code> are not lexically nested 10056*0a6a1f1dSLionel Sambucwithin any function and so have no direct access to local variables. 1005711be35a1SLionel Sambuc 1005811be35a1SLionel Sambuc 1005911be35a1SLionel Sambuc 1006011be35a1SLionel Sambuc 1006111be35a1SLionel Sambuc<p> 1006211be35a1SLionel Sambuc<hr><h3><a name="pdf-debug.gethook"><code>debug.gethook ([thread])</code></a></h3> 1006311be35a1SLionel Sambuc 1006411be35a1SLionel Sambuc 1006511be35a1SLionel Sambuc<p> 1006611be35a1SLionel SambucReturns the current hook settings of the thread, as three values: 1006711be35a1SLionel Sambucthe current hook function, the current hook mask, 1006811be35a1SLionel Sambucand the current hook count 1006911be35a1SLionel Sambuc(as set by the <a href="#pdf-debug.sethook"><code>debug.sethook</code></a> function). 1007011be35a1SLionel Sambuc 1007111be35a1SLionel Sambuc 1007211be35a1SLionel Sambuc 1007311be35a1SLionel Sambuc 1007411be35a1SLionel Sambuc<p> 10075*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-debug.getinfo"><code>debug.getinfo ([thread,] f [, what])</code></a></h3> 1007611be35a1SLionel Sambuc 1007711be35a1SLionel Sambuc 1007811be35a1SLionel Sambuc<p> 1007911be35a1SLionel SambucReturns a table with information about a function. 10080*0a6a1f1dSLionel SambucYou can give the function directly 10081*0a6a1f1dSLionel Sambucor you can give a number as the value of <code>f</code>, 10082*0a6a1f1dSLionel Sambucwhich means the function running at level <code>f</code> of the call stack 1008311be35a1SLionel Sambucof the given thread: 1008411be35a1SLionel Sambuclevel 0 is the current function (<code>getinfo</code> itself); 10085*0a6a1f1dSLionel Sambuclevel 1 is the function that called <code>getinfo</code> 10086*0a6a1f1dSLionel Sambuc(except for tail calls, which do not count on the stack); 1008711be35a1SLionel Sambucand so on. 10088*0a6a1f1dSLionel SambucIf <code>f</code> is a number larger than the number of active functions, 1008911be35a1SLionel Sambucthen <code>getinfo</code> returns <b>nil</b>. 1009011be35a1SLionel Sambuc 1009111be35a1SLionel Sambuc 1009211be35a1SLionel Sambuc<p> 1009311be35a1SLionel SambucThe returned table can contain all the fields returned by <a href="#lua_getinfo"><code>lua_getinfo</code></a>, 1009411be35a1SLionel Sambucwith the string <code>what</code> describing which fields to fill in. 1009511be35a1SLionel SambucThe default for <code>what</code> is to get all information available, 1009611be35a1SLionel Sambucexcept the table of valid lines. 1009711be35a1SLionel SambucIf present, 1009811be35a1SLionel Sambucthe option '<code>f</code>' 1009911be35a1SLionel Sambucadds a field named <code>func</code> with the function itself. 1010011be35a1SLionel SambucIf present, 1010111be35a1SLionel Sambucthe option '<code>L</code>' 1010211be35a1SLionel Sambucadds a field named <code>activelines</code> with the table of 1010311be35a1SLionel Sambucvalid lines. 1010411be35a1SLionel Sambuc 1010511be35a1SLionel Sambuc 1010611be35a1SLionel Sambuc<p> 1010711be35a1SLionel SambucFor instance, the expression <code>debug.getinfo(1,"n").name</code> returns 10108*0a6a1f1dSLionel Sambuca name for the current function, 1010911be35a1SLionel Sambucif a reasonable name can be found, 1011011be35a1SLionel Sambucand the expression <code>debug.getinfo(print)</code> 1011111be35a1SLionel Sambucreturns a table with all available information 1011211be35a1SLionel Sambucabout the <a href="#pdf-print"><code>print</code></a> function. 1011311be35a1SLionel Sambuc 1011411be35a1SLionel Sambuc 1011511be35a1SLionel Sambuc 1011611be35a1SLionel Sambuc 1011711be35a1SLionel Sambuc<p> 10118*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-debug.getlocal"><code>debug.getlocal ([thread,] f, local)</code></a></h3> 1011911be35a1SLionel Sambuc 1012011be35a1SLionel Sambuc 1012111be35a1SLionel Sambuc<p> 1012211be35a1SLionel SambucThis function returns the name and the value of the local variable 10123*0a6a1f1dSLionel Sambucwith index <code>local</code> of the function at level <code>f</code> of the stack. 10124*0a6a1f1dSLionel SambucThis function accesses not only explicit local variables, 10125*0a6a1f1dSLionel Sambucbut also parameters, temporaries, etc. 10126*0a6a1f1dSLionel Sambuc 10127*0a6a1f1dSLionel Sambuc 10128*0a6a1f1dSLionel Sambuc<p> 10129*0a6a1f1dSLionel SambucThe first parameter or local variable has index 1, and so on, 10130*0a6a1f1dSLionel Sambucfollowing the order that they are declared in the code, 10131*0a6a1f1dSLionel Sambuccounting only the variables that are active 10132*0a6a1f1dSLionel Sambucin the current scope of the function. 10133*0a6a1f1dSLionel SambucNegative indices refer to vararg parameters; 10134*0a6a1f1dSLionel Sambuc-1 is the first vararg parameter. 10135*0a6a1f1dSLionel SambucThe function returns <b>nil</b> if there is no variable with the given index, 10136*0a6a1f1dSLionel Sambucand raises an error when called with a level out of range. 1013711be35a1SLionel Sambuc(You can call <a href="#pdf-debug.getinfo"><code>debug.getinfo</code></a> to check whether the level is valid.) 1013811be35a1SLionel Sambuc 1013911be35a1SLionel Sambuc 1014011be35a1SLionel Sambuc<p> 10141*0a6a1f1dSLionel SambucVariable names starting with '<code>(</code>' (open parenthesis) 10142*0a6a1f1dSLionel Sambucrepresent variables with no known names 10143*0a6a1f1dSLionel Sambuc(internal variables such as loop control variables, 10144*0a6a1f1dSLionel Sambucand variables from chunks saved without debug information). 10145*0a6a1f1dSLionel Sambuc 10146*0a6a1f1dSLionel Sambuc 10147*0a6a1f1dSLionel Sambuc<p> 10148*0a6a1f1dSLionel SambucThe parameter <code>f</code> may also be a function. 10149*0a6a1f1dSLionel SambucIn that case, <code>getlocal</code> returns only the name of function parameters. 1015011be35a1SLionel Sambuc 1015111be35a1SLionel Sambuc 1015211be35a1SLionel Sambuc 1015311be35a1SLionel Sambuc 1015411be35a1SLionel Sambuc<p> 10155*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-debug.getmetatable"><code>debug.getmetatable (value)</code></a></h3> 1015611be35a1SLionel Sambuc 1015711be35a1SLionel Sambuc 1015811be35a1SLionel Sambuc<p> 10159*0a6a1f1dSLionel SambucReturns the metatable of the given <code>value</code> 1016011be35a1SLionel Sambucor <b>nil</b> if it does not have a metatable. 1016111be35a1SLionel Sambuc 1016211be35a1SLionel Sambuc 1016311be35a1SLionel Sambuc 1016411be35a1SLionel Sambuc 1016511be35a1SLionel Sambuc<p> 1016611be35a1SLionel Sambuc<hr><h3><a name="pdf-debug.getregistry"><code>debug.getregistry ()</code></a></h3> 1016711be35a1SLionel Sambuc 1016811be35a1SLionel Sambuc 1016911be35a1SLionel Sambuc<p> 10170*0a6a1f1dSLionel SambucReturns the registry table (see <a href="#4.5">§4.5</a>). 1017111be35a1SLionel Sambuc 1017211be35a1SLionel Sambuc 1017311be35a1SLionel Sambuc 1017411be35a1SLionel Sambuc 1017511be35a1SLionel Sambuc<p> 10176*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-debug.getupvalue"><code>debug.getupvalue (f, up)</code></a></h3> 1017711be35a1SLionel Sambuc 1017811be35a1SLionel Sambuc 1017911be35a1SLionel Sambuc<p> 1018011be35a1SLionel SambucThis function returns the name and the value of the upvalue 10181*0a6a1f1dSLionel Sambucwith index <code>up</code> of the function <code>f</code>. 1018211be35a1SLionel SambucThe function returns <b>nil</b> if there is no upvalue with the given index. 1018311be35a1SLionel Sambuc 1018411be35a1SLionel Sambuc 10185*0a6a1f1dSLionel Sambuc<p> 10186*0a6a1f1dSLionel SambucVariable names starting with '<code>(</code>' (open parenthesis) 10187*0a6a1f1dSLionel Sambucrepresent variables with no known names 10188*0a6a1f1dSLionel Sambuc(variables from chunks saved without debug information). 10189*0a6a1f1dSLionel Sambuc 10190*0a6a1f1dSLionel Sambuc 1019111be35a1SLionel Sambuc 1019211be35a1SLionel Sambuc 1019311be35a1SLionel Sambuc<p> 10194*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-debug.getuservalue"><code>debug.getuservalue (u)</code></a></h3> 1019511be35a1SLionel Sambuc 1019611be35a1SLionel Sambuc 1019711be35a1SLionel Sambuc<p> 10198*0a6a1f1dSLionel SambucReturns the Lua value associated to <code>u</code>. 10199*0a6a1f1dSLionel SambucIf <code>u</code> is not a userdata, 10200*0a6a1f1dSLionel Sambucreturns <b>nil</b>. 1020111be35a1SLionel Sambuc 1020211be35a1SLionel Sambuc 1020311be35a1SLionel Sambuc 1020411be35a1SLionel Sambuc 1020511be35a1SLionel Sambuc<p> 1020611be35a1SLionel Sambuc<hr><h3><a name="pdf-debug.sethook"><code>debug.sethook ([thread,] hook, mask [, count])</code></a></h3> 1020711be35a1SLionel Sambuc 1020811be35a1SLionel Sambuc 1020911be35a1SLionel Sambuc<p> 1021011be35a1SLionel SambucSets the given function as a hook. 1021111be35a1SLionel SambucThe string <code>mask</code> and the number <code>count</code> describe 1021211be35a1SLionel Sambucwhen the hook will be called. 10213*0a6a1f1dSLionel SambucThe string mask may have any combination of the following characters, 1021411be35a1SLionel Sambucwith the given meaning: 1021511be35a1SLionel Sambuc 1021611be35a1SLionel Sambuc<ul> 10217*0a6a1f1dSLionel Sambuc<li><b>'<code>c</code>': </b> the hook is called every time Lua calls a function;</li> 10218*0a6a1f1dSLionel Sambuc<li><b>'<code>r</code>': </b> the hook is called every time Lua returns from a function;</li> 10219*0a6a1f1dSLionel Sambuc<li><b>'<code>l</code>': </b> the hook is called every time Lua enters a new line of code.</li> 1022011be35a1SLionel Sambuc</ul><p> 10221*0a6a1f1dSLionel SambucMoreover, 10222*0a6a1f1dSLionel Sambucwith a <code>count</code> different from zero, 10223*0a6a1f1dSLionel Sambucthe hook is called also after every <code>count</code> instructions. 1022411be35a1SLionel Sambuc 1022511be35a1SLionel Sambuc 1022611be35a1SLionel Sambuc<p> 1022711be35a1SLionel SambucWhen called without arguments, 1022811be35a1SLionel Sambuc<a href="#pdf-debug.sethook"><code>debug.sethook</code></a> turns off the hook. 1022911be35a1SLionel Sambuc 1023011be35a1SLionel Sambuc 1023111be35a1SLionel Sambuc<p> 1023211be35a1SLionel SambucWhen the hook is called, its first parameter is a string 1023311be35a1SLionel Sambucdescribing the event that has triggered its call: 10234*0a6a1f1dSLionel Sambuc<code>"call"</code> (or <code>"tail call"</code>), 10235*0a6a1f1dSLionel Sambuc<code>"return"</code>, 1023611be35a1SLionel Sambuc<code>"line"</code>, and <code>"count"</code>. 1023711be35a1SLionel SambucFor line events, 1023811be35a1SLionel Sambucthe hook also gets the new line number as its second parameter. 1023911be35a1SLionel SambucInside a hook, 1024011be35a1SLionel Sambucyou can call <code>getinfo</code> with level 2 to get more information about 1024111be35a1SLionel Sambucthe running function 1024211be35a1SLionel Sambuc(level 0 is the <code>getinfo</code> function, 10243*0a6a1f1dSLionel Sambucand level 1 is the hook function). 1024411be35a1SLionel Sambuc 1024511be35a1SLionel Sambuc 1024611be35a1SLionel Sambuc 1024711be35a1SLionel Sambuc 1024811be35a1SLionel Sambuc<p> 1024911be35a1SLionel Sambuc<hr><h3><a name="pdf-debug.setlocal"><code>debug.setlocal ([thread,] level, local, value)</code></a></h3> 1025011be35a1SLionel Sambuc 1025111be35a1SLionel Sambuc 1025211be35a1SLionel Sambuc<p> 1025311be35a1SLionel SambucThis function assigns the value <code>value</code> to the local variable 1025411be35a1SLionel Sambucwith index <code>local</code> of the function at level <code>level</code> of the stack. 1025511be35a1SLionel SambucThe function returns <b>nil</b> if there is no local 1025611be35a1SLionel Sambucvariable with the given index, 1025711be35a1SLionel Sambucand raises an error when called with a <code>level</code> out of range. 1025811be35a1SLionel Sambuc(You can call <code>getinfo</code> to check whether the level is valid.) 1025911be35a1SLionel SambucOtherwise, it returns the name of the local variable. 1026011be35a1SLionel Sambuc 1026111be35a1SLionel Sambuc 10262*0a6a1f1dSLionel Sambuc<p> 10263*0a6a1f1dSLionel SambucSee <a href="#pdf-debug.getlocal"><code>debug.getlocal</code></a> for more information about 10264*0a6a1f1dSLionel Sambucvariable indices and names. 10265*0a6a1f1dSLionel Sambuc 10266*0a6a1f1dSLionel Sambuc 1026711be35a1SLionel Sambuc 1026811be35a1SLionel Sambuc 1026911be35a1SLionel Sambuc<p> 10270*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-debug.setmetatable"><code>debug.setmetatable (value, table)</code></a></h3> 1027111be35a1SLionel Sambuc 1027211be35a1SLionel Sambuc 1027311be35a1SLionel Sambuc<p> 10274*0a6a1f1dSLionel SambucSets the metatable for the given <code>value</code> to the given <code>table</code> 1027511be35a1SLionel Sambuc(which can be <b>nil</b>). 10276*0a6a1f1dSLionel SambucReturns <code>value</code>. 1027711be35a1SLionel Sambuc 1027811be35a1SLionel Sambuc 1027911be35a1SLionel Sambuc 1028011be35a1SLionel Sambuc 1028111be35a1SLionel Sambuc<p> 10282*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-debug.setupvalue"><code>debug.setupvalue (f, up, value)</code></a></h3> 1028311be35a1SLionel Sambuc 1028411be35a1SLionel Sambuc 1028511be35a1SLionel Sambuc<p> 1028611be35a1SLionel SambucThis function assigns the value <code>value</code> to the upvalue 10287*0a6a1f1dSLionel Sambucwith index <code>up</code> of the function <code>f</code>. 1028811be35a1SLionel SambucThe function returns <b>nil</b> if there is no upvalue 1028911be35a1SLionel Sambucwith the given index. 1029011be35a1SLionel SambucOtherwise, it returns the name of the upvalue. 1029111be35a1SLionel Sambuc 1029211be35a1SLionel Sambuc 1029311be35a1SLionel Sambuc 1029411be35a1SLionel Sambuc 1029511be35a1SLionel Sambuc<p> 10296*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-debug.setuservalue"><code>debug.setuservalue (udata, value)</code></a></h3> 10297*0a6a1f1dSLionel Sambuc 10298*0a6a1f1dSLionel Sambuc 10299*0a6a1f1dSLionel Sambuc<p> 10300*0a6a1f1dSLionel SambucSets the given <code>value</code> as 10301*0a6a1f1dSLionel Sambucthe Lua value associated to the given <code>udata</code>. 10302*0a6a1f1dSLionel Sambuc<code>udata</code> must be a full userdata. 10303*0a6a1f1dSLionel Sambuc 10304*0a6a1f1dSLionel Sambuc 10305*0a6a1f1dSLionel Sambuc<p> 10306*0a6a1f1dSLionel SambucReturns <code>udata</code>. 10307*0a6a1f1dSLionel Sambuc 10308*0a6a1f1dSLionel Sambuc 10309*0a6a1f1dSLionel Sambuc 10310*0a6a1f1dSLionel Sambuc 10311*0a6a1f1dSLionel Sambuc<p> 1031211be35a1SLionel Sambuc<hr><h3><a name="pdf-debug.traceback"><code>debug.traceback ([thread,] [message [, level]])</code></a></h3> 1031311be35a1SLionel Sambuc 1031411be35a1SLionel Sambuc 1031511be35a1SLionel Sambuc<p> 10316*0a6a1f1dSLionel SambucIf <code>message</code> is present but is neither a string nor <b>nil</b>, 10317*0a6a1f1dSLionel Sambucthis function returns <code>message</code> without further processing. 10318*0a6a1f1dSLionel SambucOtherwise, 10319*0a6a1f1dSLionel Sambucit returns a string with a traceback of the call stack. 10320*0a6a1f1dSLionel SambucThe optional <code>message</code> string is appended 1032111be35a1SLionel Sambucat the beginning of the traceback. 1032211be35a1SLionel SambucAn optional <code>level</code> number tells at which level 1032311be35a1SLionel Sambucto start the traceback 1032411be35a1SLionel Sambuc(default is 1, the function calling <code>traceback</code>). 1032511be35a1SLionel Sambuc 1032611be35a1SLionel Sambuc 1032711be35a1SLionel Sambuc 1032811be35a1SLionel Sambuc 10329*0a6a1f1dSLionel Sambuc<p> 10330*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-debug.upvalueid"><code>debug.upvalueid (f, n)</code></a></h3> 10331*0a6a1f1dSLionel Sambuc 10332*0a6a1f1dSLionel Sambuc 10333*0a6a1f1dSLionel Sambuc<p> 10334*0a6a1f1dSLionel SambucReturns a unique identifier (as a light userdata) 10335*0a6a1f1dSLionel Sambucfor the upvalue numbered <code>n</code> 10336*0a6a1f1dSLionel Sambucfrom the given function. 10337*0a6a1f1dSLionel Sambuc 10338*0a6a1f1dSLionel Sambuc 10339*0a6a1f1dSLionel Sambuc<p> 10340*0a6a1f1dSLionel SambucThese unique identifiers allow a program to check whether different 10341*0a6a1f1dSLionel Sambucclosures share upvalues. 10342*0a6a1f1dSLionel SambucLua closures that share an upvalue 10343*0a6a1f1dSLionel Sambuc(that is, that access a same external local variable) 10344*0a6a1f1dSLionel Sambucwill return identical ids for those upvalue indices. 1034511be35a1SLionel Sambuc 1034611be35a1SLionel Sambuc 1034711be35a1SLionel Sambuc 10348*0a6a1f1dSLionel Sambuc 10349*0a6a1f1dSLionel Sambuc<p> 10350*0a6a1f1dSLionel Sambuc<hr><h3><a name="pdf-debug.upvaluejoin"><code>debug.upvaluejoin (f1, n1, f2, n2)</code></a></h3> 10351*0a6a1f1dSLionel Sambuc 10352*0a6a1f1dSLionel Sambuc 10353*0a6a1f1dSLionel Sambuc<p> 10354*0a6a1f1dSLionel SambucMake the <code>n1</code>-th upvalue of the Lua closure <code>f1</code> 10355*0a6a1f1dSLionel Sambucrefer to the <code>n2</code>-th upvalue of the Lua closure <code>f2</code>. 10356*0a6a1f1dSLionel Sambuc 10357*0a6a1f1dSLionel Sambuc 10358*0a6a1f1dSLionel Sambuc 10359*0a6a1f1dSLionel Sambuc 10360*0a6a1f1dSLionel Sambuc 10361*0a6a1f1dSLionel Sambuc 10362*0a6a1f1dSLionel Sambuc 10363*0a6a1f1dSLionel Sambuc<h1>7 – <a name="7">Lua Standalone</a></h1> 1036411be35a1SLionel Sambuc 1036511be35a1SLionel Sambuc<p> 1036611be35a1SLionel SambucAlthough Lua has been designed as an extension language, 1036711be35a1SLionel Sambucto be embedded in a host C program, 10368*0a6a1f1dSLionel Sambucit is also frequently used as a standalone language. 10369*0a6a1f1dSLionel SambucAn interpreter for Lua as a standalone language, 1037011be35a1SLionel Sambuccalled simply <code>lua</code>, 1037111be35a1SLionel Sambucis provided with the standard distribution. 10372*0a6a1f1dSLionel SambucThe standalone interpreter includes 1037311be35a1SLionel Sambucall standard libraries, including the debug library. 1037411be35a1SLionel SambucIts usage is: 1037511be35a1SLionel Sambuc 1037611be35a1SLionel Sambuc<pre> 1037711be35a1SLionel Sambuc lua [options] [script [args]] 1037811be35a1SLionel Sambuc</pre><p> 1037911be35a1SLionel SambucThe options are: 1038011be35a1SLionel Sambuc 1038111be35a1SLionel Sambuc<ul> 1038211be35a1SLionel Sambuc<li><b><code>-e <em>stat</em></code>: </b> executes string <em>stat</em>;</li> 1038311be35a1SLionel Sambuc<li><b><code>-l <em>mod</em></code>: </b> "requires" <em>mod</em>;</li> 1038411be35a1SLionel Sambuc<li><b><code>-i</code>: </b> enters interactive mode after running <em>script</em>;</li> 1038511be35a1SLionel Sambuc<li><b><code>-v</code>: </b> prints version information;</li> 10386*0a6a1f1dSLionel Sambuc<li><b><code>-E</code>: </b> ignores environment variables;</li> 1038711be35a1SLionel Sambuc<li><b><code>--</code>: </b> stops handling options;</li> 1038811be35a1SLionel Sambuc<li><b><code>-</code>: </b> executes <code>stdin</code> as a file and stops handling options.</li> 1038911be35a1SLionel Sambuc</ul><p> 10390*0a6a1f1dSLionel SambucAfter handling its options, <code>lua</code> runs the given <em>script</em>. 1039111be35a1SLionel SambucWhen called without arguments, 1039211be35a1SLionel Sambuc<code>lua</code> behaves as <code>lua -v -i</code> 1039311be35a1SLionel Sambucwhen the standard input (<code>stdin</code>) is a terminal, 1039411be35a1SLionel Sambucand as <code>lua -</code> otherwise. 1039511be35a1SLionel Sambuc 1039611be35a1SLionel Sambuc 1039711be35a1SLionel Sambuc<p> 10398*0a6a1f1dSLionel SambucWhen called without option <code>-E</code>, 10399*0a6a1f1dSLionel Sambucthe interpreter checks for an environment variable <a name="pdf-LUA_INIT_5_3"><code>LUA_INIT_5_3</code></a> 10400*0a6a1f1dSLionel Sambuc(or <a name="pdf-LUA_INIT"><code>LUA_INIT</code></a> if the versioned name is not defined) 10401*0a6a1f1dSLionel Sambucbefore running any argument. 10402*0a6a1f1dSLionel SambucIf the variable content has the format <code>@<em>filename</em></code>, 1040311be35a1SLionel Sambucthen <code>lua</code> executes the file. 1040411be35a1SLionel SambucOtherwise, <code>lua</code> executes the string itself. 1040511be35a1SLionel Sambuc 1040611be35a1SLionel Sambuc 1040711be35a1SLionel Sambuc<p> 10408*0a6a1f1dSLionel SambucWhen called with option <code>-E</code>, 10409*0a6a1f1dSLionel Sambucbesides ignoring <code>LUA_INIT</code>, 10410*0a6a1f1dSLionel SambucLua also ignores 10411*0a6a1f1dSLionel Sambucthe values of <code>LUA_PATH</code> and <code>LUA_CPATH</code>, 10412*0a6a1f1dSLionel Sambucsetting the values of 10413*0a6a1f1dSLionel Sambuc<a href="#pdf-package.path"><code>package.path</code></a> and <a href="#pdf-package.cpath"><code>package.cpath</code></a> 10414*0a6a1f1dSLionel Sambucwith the default paths defined in <code>luaconf.h</code>. 10415*0a6a1f1dSLionel Sambuc 10416*0a6a1f1dSLionel Sambuc 10417*0a6a1f1dSLionel Sambuc<p> 10418*0a6a1f1dSLionel SambucAll options are handled in order, except <code>-i</code> and <code>-E</code>. 1041911be35a1SLionel SambucFor instance, an invocation like 1042011be35a1SLionel Sambuc 1042111be35a1SLionel Sambuc<pre> 1042211be35a1SLionel Sambuc $ lua -e'a=1' -e 'print(a)' script.lua 1042311be35a1SLionel Sambuc</pre><p> 10424*0a6a1f1dSLionel Sambucwill first set <code>a</code> to 1, then print the value of <code>a</code>, 1042511be35a1SLionel Sambucand finally run the file <code>script.lua</code> with no arguments. 1042611be35a1SLionel Sambuc(Here <code>$</code> is the shell prompt. Your prompt may be different.) 1042711be35a1SLionel Sambuc 1042811be35a1SLionel Sambuc 1042911be35a1SLionel Sambuc<p> 10430*0a6a1f1dSLionel SambucBefore running any code, 10431*0a6a1f1dSLionel Sambuc<code>lua</code> collects all command-line arguments 1043211be35a1SLionel Sambucin a global table called <code>arg</code>. 10433*0a6a1f1dSLionel SambucThe script name goes to index 0, 1043411be35a1SLionel Sambucthe first argument after the script name goes to index 1, 1043511be35a1SLionel Sambucand so on. 1043611be35a1SLionel SambucAny arguments before the script name 10437*0a6a1f1dSLionel Sambuc(that is, the interpreter name plus its options) 1043811be35a1SLionel Sambucgo to negative indices. 1043911be35a1SLionel SambucFor instance, in the call 1044011be35a1SLionel Sambuc 1044111be35a1SLionel Sambuc<pre> 1044211be35a1SLionel Sambuc $ lua -la b.lua t1 t2 1044311be35a1SLionel Sambuc</pre><p> 10444*0a6a1f1dSLionel Sambucthe table is like this: 1044511be35a1SLionel Sambuc 1044611be35a1SLionel Sambuc<pre> 1044711be35a1SLionel Sambuc arg = { [-2] = "lua", [-1] = "-la", 1044811be35a1SLionel Sambuc [0] = "b.lua", 1044911be35a1SLionel Sambuc [1] = "t1", [2] = "t2" } 1045011be35a1SLionel Sambuc</pre><p> 10451*0a6a1f1dSLionel SambucIf there is no script in the call, 10452*0a6a1f1dSLionel Sambucthe interpreter name goes to index 0, 10453*0a6a1f1dSLionel Sambucfollowed by the other arguments. 10454*0a6a1f1dSLionel SambucFor instance, the call 10455*0a6a1f1dSLionel Sambuc 10456*0a6a1f1dSLionel Sambuc<pre> 10457*0a6a1f1dSLionel Sambuc $ lua -e "print(arg[1])" 10458*0a6a1f1dSLionel Sambuc</pre><p> 10459*0a6a1f1dSLionel Sambucwill print "<code>-e</code>". 10460*0a6a1f1dSLionel SambucIf there is a script, 10461*0a6a1f1dSLionel Sambucthe script is called with parameters 10462*0a6a1f1dSLionel Sambuc<code>arg[1]</code>, ···, <code>arg[#arg]</code>. 10463*0a6a1f1dSLionel Sambuc(Like all chunks in Lua, 10464*0a6a1f1dSLionel Sambucthe script is compiled as a vararg function.) 1046511be35a1SLionel Sambuc 1046611be35a1SLionel Sambuc 1046711be35a1SLionel Sambuc<p> 1046811be35a1SLionel SambucIn interactive mode, 10469*0a6a1f1dSLionel SambucLua repeatedly prompts and waits for a line. 10470*0a6a1f1dSLionel SambucAfter reading a line, 10471*0a6a1f1dSLionel SambucLua first try to interpret the line as an expression. 10472*0a6a1f1dSLionel SambucIf it succeeds, it prints its value. 10473*0a6a1f1dSLionel SambucOtherwise, it interprets the line as a statement. 10474*0a6a1f1dSLionel SambucIf you write an incomplete statement, 1047511be35a1SLionel Sambucthe interpreter waits for its completion 1047611be35a1SLionel Sambucby issuing a different prompt. 1047711be35a1SLionel Sambuc 1047811be35a1SLionel Sambuc 1047911be35a1SLionel Sambuc<p> 10480*0a6a1f1dSLionel SambucIn case of unprotected errors in the script, 10481*0a6a1f1dSLionel Sambucthe interpreter reports the error to the standard error stream. 10482*0a6a1f1dSLionel SambucIf the error object is not a string but 10483*0a6a1f1dSLionel Sambuchas a metamethod <code>__tostring</code>, 10484*0a6a1f1dSLionel Sambucthe interpreter calls this metamethod to produce the final message. 10485*0a6a1f1dSLionel SambucOtherwise, the interpreter converts the error object to a string 10486*0a6a1f1dSLionel Sambucand adds a stack traceback to it. 1048711be35a1SLionel Sambuc 10488*0a6a1f1dSLionel Sambuc 10489*0a6a1f1dSLionel Sambuc<p> 10490*0a6a1f1dSLionel SambucWhen finishing normally, 10491*0a6a1f1dSLionel Sambucthe interpreter closes its main Lua state 10492*0a6a1f1dSLionel Sambuc(see <a href="#lua_close"><code>lua_close</code></a>). 10493*0a6a1f1dSLionel SambucThe script can avoid this step by 10494*0a6a1f1dSLionel Sambuccalling <a href="#pdf-os.exit"><code>os.exit</code></a> to terminate. 1049511be35a1SLionel Sambuc 1049611be35a1SLionel Sambuc 1049711be35a1SLionel Sambuc<p> 1049811be35a1SLionel SambucTo allow the use of Lua as a 1049911be35a1SLionel Sambucscript interpreter in Unix systems, 10500*0a6a1f1dSLionel Sambucthe standalone interpreter skips 1050111be35a1SLionel Sambucthe first line of a chunk if it starts with <code>#</code>. 1050211be35a1SLionel SambucTherefore, Lua scripts can be made into executable programs 1050311be35a1SLionel Sambucby using <code>chmod +x</code> and the <code>#!</code> form, 1050411be35a1SLionel Sambucas in 1050511be35a1SLionel Sambuc 1050611be35a1SLionel Sambuc<pre> 1050711be35a1SLionel Sambuc #!/usr/local/bin/lua 1050811be35a1SLionel Sambuc</pre><p> 1050911be35a1SLionel Sambuc(Of course, 1051011be35a1SLionel Sambucthe location of the Lua interpreter may be different in your machine. 1051111be35a1SLionel SambucIf <code>lua</code> is in your <code>PATH</code>, 1051211be35a1SLionel Sambucthen 1051311be35a1SLionel Sambuc 1051411be35a1SLionel Sambuc<pre> 1051511be35a1SLionel Sambuc #!/usr/bin/env lua 1051611be35a1SLionel Sambuc</pre><p> 1051711be35a1SLionel Sambucis a more portable solution.) 1051811be35a1SLionel Sambuc 1051911be35a1SLionel Sambuc 1052011be35a1SLionel Sambuc 10521*0a6a1f1dSLionel Sambuc<h1>8 – <a name="8">Incompatibilities with the Previous Version</a></h1> 1052211be35a1SLionel Sambuc 1052311be35a1SLionel Sambuc<p> 1052411be35a1SLionel SambucHere we list the incompatibilities that you may find when moving a program 10525*0a6a1f1dSLionel Sambucfrom Lua 5.2 to Lua 5.3. 10526*0a6a1f1dSLionel SambucYou can avoid some incompatibilities by compiling Lua with 1052711be35a1SLionel Sambucappropriate options (see file <code>luaconf.h</code>). 1052811be35a1SLionel SambucHowever, 10529*0a6a1f1dSLionel Sambucall these compatibility options will be removed in the future. 10530*0a6a1f1dSLionel Sambuc 10531*0a6a1f1dSLionel Sambuc 10532*0a6a1f1dSLionel Sambuc<p> 10533*0a6a1f1dSLionel SambucLua versions can always change the C API in ways that 10534*0a6a1f1dSLionel Sambucdo not imply source-code changes in a program, 10535*0a6a1f1dSLionel Sambucsuch as the numeric values for constants 10536*0a6a1f1dSLionel Sambucor the implementation of functions as macros. 10537*0a6a1f1dSLionel SambucTherefore, 10538*0a6a1f1dSLionel Sambucyou should not assume that binaries are compatible between 10539*0a6a1f1dSLionel Sambucdifferent Lua versions. 10540*0a6a1f1dSLionel SambucAlways recompile clients of the Lua API when 10541*0a6a1f1dSLionel Sambucusing a new version. 10542*0a6a1f1dSLionel Sambuc 10543*0a6a1f1dSLionel Sambuc 10544*0a6a1f1dSLionel Sambuc<p> 10545*0a6a1f1dSLionel SambucSimilarly, Lua versions can always change the internal representation 10546*0a6a1f1dSLionel Sambucof precompiled chunks; 10547*0a6a1f1dSLionel Sambucprecompiled chunks are not compatible between different Lua versions. 10548*0a6a1f1dSLionel Sambuc 10549*0a6a1f1dSLionel Sambuc 10550*0a6a1f1dSLionel Sambuc<p> 10551*0a6a1f1dSLionel SambucThe standard paths in the official distribution may 10552*0a6a1f1dSLionel Sambucchange between versions. 1055311be35a1SLionel Sambuc 1055411be35a1SLionel Sambuc 1055511be35a1SLionel Sambuc 10556*0a6a1f1dSLionel Sambuc<h2>8.1 – <a name="8.1">Changes in the Language</a></h2> 1055711be35a1SLionel Sambuc<ul> 1055811be35a1SLionel Sambuc 1055911be35a1SLionel Sambuc<li> 10560*0a6a1f1dSLionel SambucThe main difference between Lua 5.2 and Lua 5.3 is the 10561*0a6a1f1dSLionel Sambucintroduction of an integer subtype for numbers. 10562*0a6a1f1dSLionel SambucAlthough this change should not affect "normal" computations, 10563*0a6a1f1dSLionel Sambucsome computations 10564*0a6a1f1dSLionel Sambuc(mainly those that involve some kind of overflow) 10565*0a6a1f1dSLionel Sambuccan give different results. 10566*0a6a1f1dSLionel Sambuc 10567*0a6a1f1dSLionel Sambuc 10568*0a6a1f1dSLionel Sambuc<p> 10569*0a6a1f1dSLionel SambucYou can fix these differences by forcing a number to be a float 10570*0a6a1f1dSLionel Sambuc(in Lua 5.2 all numbers were float), 10571*0a6a1f1dSLionel Sambucin particular writing constants with an ending <code>.0</code> 10572*0a6a1f1dSLionel Sambucor using <code>x = x + 0.0</code> to convert a variable. 10573*0a6a1f1dSLionel Sambuc(This recommendation is only for a quick fix 10574*0a6a1f1dSLionel Sambucfor an occasional incompatibility; 10575*0a6a1f1dSLionel Sambucit is not a general guideline for good programming. 10576*0a6a1f1dSLionel SambucFor good programming, 10577*0a6a1f1dSLionel Sambucuse floats where you need floats 10578*0a6a1f1dSLionel Sambucand integers where you need integers.) 1057911be35a1SLionel Sambuc</li> 1058011be35a1SLionel Sambuc 1058111be35a1SLionel Sambuc<li> 10582*0a6a1f1dSLionel SambucThe conversion of a float to a string now adds a <code>.0</code> suffix 10583*0a6a1f1dSLionel Sambucto the result if it looks like an integer. 10584*0a6a1f1dSLionel Sambuc(For instance, the float 2.0 will be printed as <code>2.0</code>, 10585*0a6a1f1dSLionel Sambucnot as <code>2</code>.) 10586*0a6a1f1dSLionel SambucYou should always use an explicit format 10587*0a6a1f1dSLionel Sambucwhen you need a specific format for numbers. 10588*0a6a1f1dSLionel Sambuc 10589*0a6a1f1dSLionel Sambuc 10590*0a6a1f1dSLionel Sambuc<p> 10591*0a6a1f1dSLionel Sambuc(Formally this is not an incompatibility, 10592*0a6a1f1dSLionel Sambucbecause Lua does not specify how numbers are formatted as strings, 10593*0a6a1f1dSLionel Sambucbut some programs assumed a specific format.) 1059411be35a1SLionel Sambuc</li> 1059511be35a1SLionel Sambuc 1059611be35a1SLionel Sambuc<li> 10597*0a6a1f1dSLionel SambucThe generational mode for the garbage collector was removed. 10598*0a6a1f1dSLionel Sambuc(It was an experimental feature in Lua 5.2.) 1059911be35a1SLionel Sambuc</li> 1060011be35a1SLionel Sambuc 1060111be35a1SLionel Sambuc</ul> 1060211be35a1SLionel Sambuc 1060311be35a1SLionel Sambuc 1060411be35a1SLionel Sambuc 1060511be35a1SLionel Sambuc 10606*0a6a1f1dSLionel Sambuc<h2>8.2 – <a name="8.2">Changes in the Libraries</a></h2> 1060711be35a1SLionel Sambuc<ul> 1060811be35a1SLionel Sambuc 1060911be35a1SLionel Sambuc<li> 10610*0a6a1f1dSLionel SambucThe <code>bit32</code> library has been deprecated. 10611*0a6a1f1dSLionel SambucIt is easy to require a compatible external library or, 10612*0a6a1f1dSLionel Sambucbetter yet, to replace its functions with appropriate bitwise operations. 10613*0a6a1f1dSLionel Sambuc(Keep in mind that <code>bit32</code> operates on 32-bit integers, 10614*0a6a1f1dSLionel Sambucwhile the bitwise operators in Lua 5.3 operate on Lua integers, 10615*0a6a1f1dSLionel Sambucwhich by default have 64 bits.) 1061611be35a1SLionel Sambuc</li> 1061711be35a1SLionel Sambuc 1061811be35a1SLionel Sambuc<li> 10619*0a6a1f1dSLionel SambucThe Table library now respects metamethods 10620*0a6a1f1dSLionel Sambucfor setting and getting elements. 1062111be35a1SLionel Sambuc</li> 1062211be35a1SLionel Sambuc 1062311be35a1SLionel Sambuc<li> 10624*0a6a1f1dSLionel SambucThe <a href="#pdf-ipairs"><code>ipairs</code></a> iterator now respects metamethods and 10625*0a6a1f1dSLionel Sambucits <code>__ipairs</code> metamethod has been deprecated. 1062611be35a1SLionel Sambuc</li> 1062711be35a1SLionel Sambuc 1062811be35a1SLionel Sambuc<li> 10629*0a6a1f1dSLionel SambucOption names in <a href="#pdf-io.read"><code>io.read</code></a> do not have a starting '<code>*</code>' anymore. 10630*0a6a1f1dSLionel SambucFor compatibility, Lua will continue to accept (and ignore) this character. 1063111be35a1SLionel Sambuc</li> 1063211be35a1SLionel Sambuc 1063311be35a1SLionel Sambuc<li> 10634*0a6a1f1dSLionel SambucThe following functions were deprecated in the mathematical library: 10635*0a6a1f1dSLionel Sambuc<code>atan2</code>, <code>cosh</code>, <code>sinh</code>, <code>tanh</code>, <code>pow</code>, 10636*0a6a1f1dSLionel Sambuc<code>frexp</code>, and <code>ldexp</code>. 10637*0a6a1f1dSLionel SambucYou can replace <code>math.pow(x,y)</code> with <code>x^y</code>; 10638*0a6a1f1dSLionel Sambucyou can replace <code>math.atan2</code> with <code>math.atan</code>, 10639*0a6a1f1dSLionel Sambucwhich now accepts one or two parameters; 10640*0a6a1f1dSLionel Sambucyou can replace <code>math.ldexp(x,exp)</code> with <code>x * 2.0^exp</code>. 10641*0a6a1f1dSLionel SambucFor the other operations, 10642*0a6a1f1dSLionel Sambucyou can either use an external library or 10643*0a6a1f1dSLionel Sambucimplement them in Lua. 1064411be35a1SLionel Sambuc</li> 1064511be35a1SLionel Sambuc 1064611be35a1SLionel Sambuc<li> 10647*0a6a1f1dSLionel SambucThe searcher for C loaders used by <a href="#pdf-require"><code>require</code></a> 10648*0a6a1f1dSLionel Sambucchanged the way it handles versioned names. 10649*0a6a1f1dSLionel SambucNow, the version should come after the module name 10650*0a6a1f1dSLionel Sambuc(as is usual in most other tools). 10651*0a6a1f1dSLionel SambucFor compatibility, that searcher still tries the old format 10652*0a6a1f1dSLionel Sambucif it cannot find an open function according to the new style. 10653*0a6a1f1dSLionel Sambuc(Lua 5.2 already worked that way, 10654*0a6a1f1dSLionel Sambucbut it did not document the change.) 1065511be35a1SLionel Sambuc</li> 1065611be35a1SLionel Sambuc 1065711be35a1SLionel Sambuc<li> 10658*0a6a1f1dSLionel SambucThe call <code>collectgarbage("count")</code> now returns only one result. 10659*0a6a1f1dSLionel Sambuc(You can compute that second result from the fractional part 10660*0a6a1f1dSLionel Sambucof the first result.) 1066111be35a1SLionel Sambuc</li> 1066211be35a1SLionel Sambuc 1066311be35a1SLionel Sambuc</ul> 1066411be35a1SLionel Sambuc 1066511be35a1SLionel Sambuc 1066611be35a1SLionel Sambuc 1066711be35a1SLionel Sambuc 10668*0a6a1f1dSLionel Sambuc<h2>8.3 – <a name="8.3">Changes in the API</a></h2> 10669*0a6a1f1dSLionel Sambuc 10670*0a6a1f1dSLionel Sambuc 1067111be35a1SLionel Sambuc<ul> 1067211be35a1SLionel Sambuc 1067311be35a1SLionel Sambuc<li> 10674*0a6a1f1dSLionel SambucContinuation functions now receive as parameters what they needed 10675*0a6a1f1dSLionel Sambucto get through <code>lua_getctx</code>, 10676*0a6a1f1dSLionel Sambucso <code>lua_getctx</code> has been removed. 10677*0a6a1f1dSLionel SambucAdapt your code accordingly. 1067811be35a1SLionel Sambuc</li> 1067911be35a1SLionel Sambuc 1068011be35a1SLionel Sambuc<li> 10681*0a6a1f1dSLionel SambucFunction <a href="#lua_dump"><code>lua_dump</code></a> has an extra parameter, <code>strip</code>. 10682*0a6a1f1dSLionel SambucUse 0 as the value of this parameter to get the old behavior. 1068311be35a1SLionel Sambuc</li> 1068411be35a1SLionel Sambuc 1068511be35a1SLionel Sambuc<li> 10686*0a6a1f1dSLionel SambucFunctions to inject/project unsigned integers 10687*0a6a1f1dSLionel Sambuc(<code>lua_pushunsigned</code>, <code>lua_tounsigned</code>, <code>lua_tounsignedx</code>, 10688*0a6a1f1dSLionel Sambuc<code>luaL_checkunsigned</code>, <code>luaL_optunsigned</code>) 10689*0a6a1f1dSLionel Sambucwere deprecated. 10690*0a6a1f1dSLionel SambucUse their signed equivalents with a type cast. 1069111be35a1SLionel Sambuc</li> 1069211be35a1SLionel Sambuc 1069311be35a1SLionel Sambuc<li> 10694*0a6a1f1dSLionel SambucMacros to project non-default integer types 10695*0a6a1f1dSLionel Sambuc(<code>luaL_checkint</code>, <code>luaL_optint</code>, <code>luaL_checklong</code>, <code>luaL_optlong</code>) 10696*0a6a1f1dSLionel Sambucwere deprecated. 10697*0a6a1f1dSLionel SambucUse their equivalent over <a href="#lua_Integer"><code>lua_Integer</code></a> with a type cast 10698*0a6a1f1dSLionel Sambuc(or, when possible, use <a href="#lua_Integer"><code>lua_Integer</code></a> in your code). 1069911be35a1SLionel Sambuc</li> 1070011be35a1SLionel Sambuc 1070111be35a1SLionel Sambuc</ul> 1070211be35a1SLionel Sambuc 1070311be35a1SLionel Sambuc 1070411be35a1SLionel Sambuc 1070511be35a1SLionel Sambuc 10706*0a6a1f1dSLionel Sambuc<h1>9 – <a name="9">The Complete Syntax of Lua</a></h1> 1070711be35a1SLionel Sambuc 1070811be35a1SLionel Sambuc<p> 1070911be35a1SLionel SambucHere is the complete syntax of Lua in extended BNF. 10710*0a6a1f1dSLionel SambucAs usual in extended BNF, 10711*0a6a1f1dSLionel Sambuc{A} means 0 or more As, 10712*0a6a1f1dSLionel Sambucand [A] means an optional A. 10713*0a6a1f1dSLionel Sambuc(For operator precedences, see <a href="#3.4.8">§3.4.8</a>; 10714*0a6a1f1dSLionel Sambucfor a description of the terminals 10715*0a6a1f1dSLionel SambucName, Numeral, 10716*0a6a1f1dSLionel Sambucand LiteralString, see <a href="#3.1">§3.1</a>.) 1071711be35a1SLionel Sambuc 1071811be35a1SLionel Sambuc 1071911be35a1SLionel Sambuc 1072011be35a1SLionel Sambuc 1072111be35a1SLionel Sambuc<pre> 1072211be35a1SLionel Sambuc 10723*0a6a1f1dSLionel Sambuc chunk ::= block 1072411be35a1SLionel Sambuc 10725*0a6a1f1dSLionel Sambuc block ::= {stat} [retstat] 1072611be35a1SLionel Sambuc 10727*0a6a1f1dSLionel Sambuc stat ::= ‘<b>;</b>’ | 10728*0a6a1f1dSLionel Sambuc varlist ‘<b>=</b>’ explist | 1072911be35a1SLionel Sambuc functioncall | 10730*0a6a1f1dSLionel Sambuc label | 10731*0a6a1f1dSLionel Sambuc <b>break</b> | 10732*0a6a1f1dSLionel Sambuc <b>goto</b> Name | 1073311be35a1SLionel Sambuc <b>do</b> block <b>end</b> | 1073411be35a1SLionel Sambuc <b>while</b> exp <b>do</b> block <b>end</b> | 1073511be35a1SLionel Sambuc <b>repeat</b> block <b>until</b> exp | 1073611be35a1SLionel Sambuc <b>if</b> exp <b>then</b> block {<b>elseif</b> exp <b>then</b> block} [<b>else</b> block] <b>end</b> | 10737*0a6a1f1dSLionel Sambuc <b>for</b> Name ‘<b>=</b>’ exp ‘<b>,</b>’ exp [‘<b>,</b>’ exp] <b>do</b> block <b>end</b> | 1073811be35a1SLionel Sambuc <b>for</b> namelist <b>in</b> explist <b>do</b> block <b>end</b> | 1073911be35a1SLionel Sambuc <b>function</b> funcname funcbody | 1074011be35a1SLionel Sambuc <b>local</b> <b>function</b> Name funcbody | 10741*0a6a1f1dSLionel Sambuc <b>local</b> namelist [‘<b>=</b>’ explist] 1074211be35a1SLionel Sambuc 10743*0a6a1f1dSLionel Sambuc retstat ::= <b>return</b> [explist] [‘<b>;</b>’] 1074411be35a1SLionel Sambuc 10745*0a6a1f1dSLionel Sambuc label ::= ‘<b>::</b>’ Name ‘<b>::</b>’ 1074611be35a1SLionel Sambuc 10747*0a6a1f1dSLionel Sambuc funcname ::= Name {‘<b>.</b>’ Name} [‘<b>:</b>’ Name] 1074811be35a1SLionel Sambuc 10749*0a6a1f1dSLionel Sambuc varlist ::= var {‘<b>,</b>’ var} 1075011be35a1SLionel Sambuc 10751*0a6a1f1dSLionel Sambuc var ::= Name | prefixexp ‘<b>[</b>’ exp ‘<b>]</b>’ | prefixexp ‘<b>.</b>’ Name 1075211be35a1SLionel Sambuc 10753*0a6a1f1dSLionel Sambuc namelist ::= Name {‘<b>,</b>’ Name} 1075411be35a1SLionel Sambuc 10755*0a6a1f1dSLionel Sambuc explist ::= exp {‘<b>,</b>’ exp} 10756*0a6a1f1dSLionel Sambuc 10757*0a6a1f1dSLionel Sambuc exp ::= <b>nil</b> | <b>false</b> | <b>true</b> | Numeral | LiteralString | ‘<b>...</b>’ | functiondef | 1075811be35a1SLionel Sambuc prefixexp | tableconstructor | exp binop exp | unop exp 1075911be35a1SLionel Sambuc 10760*0a6a1f1dSLionel Sambuc prefixexp ::= var | functioncall | ‘<b>(</b>’ exp ‘<b>)</b>’ 1076111be35a1SLionel Sambuc 10762*0a6a1f1dSLionel Sambuc functioncall ::= prefixexp args | prefixexp ‘<b>:</b>’ Name args 1076311be35a1SLionel Sambuc 10764*0a6a1f1dSLionel Sambuc args ::= ‘<b>(</b>’ [explist] ‘<b>)</b>’ | tableconstructor | LiteralString 1076511be35a1SLionel Sambuc 10766*0a6a1f1dSLionel Sambuc functiondef ::= <b>function</b> funcbody 1076711be35a1SLionel Sambuc 10768*0a6a1f1dSLionel Sambuc funcbody ::= ‘<b>(</b>’ [parlist] ‘<b>)</b>’ block <b>end</b> 1076911be35a1SLionel Sambuc 10770*0a6a1f1dSLionel Sambuc parlist ::= namelist [‘<b>,</b>’ ‘<b>...</b>’] | ‘<b>...</b>’ 1077111be35a1SLionel Sambuc 10772*0a6a1f1dSLionel Sambuc tableconstructor ::= ‘<b>{</b>’ [fieldlist] ‘<b>}</b>’ 1077311be35a1SLionel Sambuc 1077411be35a1SLionel Sambuc fieldlist ::= field {fieldsep field} [fieldsep] 1077511be35a1SLionel Sambuc 10776*0a6a1f1dSLionel Sambuc field ::= ‘<b>[</b>’ exp ‘<b>]</b>’ ‘<b>=</b>’ exp | Name ‘<b>=</b>’ exp | exp 1077711be35a1SLionel Sambuc 10778*0a6a1f1dSLionel Sambuc fieldsep ::= ‘<b>,</b>’ | ‘<b>;</b>’ 1077911be35a1SLionel Sambuc 10780*0a6a1f1dSLionel Sambuc binop ::= ‘<b>+</b>’ | ‘<b>-</b>’ | ‘<b>*</b>’ | ‘<b>/</b>’ | ‘<b>//</b>’ | ‘<b>^</b>’ | ‘<b>%</b>’ | 10781*0a6a1f1dSLionel Sambuc ‘<b>&</b>’ | ‘<b>~</b>’ | ‘<b>|</b>’ | ‘<b>>></b>’ | ‘<b><<</b>’ | ‘<b>..</b>’ | 10782*0a6a1f1dSLionel Sambuc ‘<b><</b>’ | ‘<b><=</b>’ | ‘<b>></b>’ | ‘<b>>=</b>’ | ‘<b>==</b>’ | ‘<b>~=</b>’ | 1078311be35a1SLionel Sambuc <b>and</b> | <b>or</b> 1078411be35a1SLionel Sambuc 10785*0a6a1f1dSLionel Sambuc unop ::= ‘<b>-</b>’ | <b>not</b> | ‘<b>#</b>’ | ‘<b>~</b>’ 1078611be35a1SLionel Sambuc 1078711be35a1SLionel Sambuc</pre> 1078811be35a1SLionel Sambuc 1078911be35a1SLionel Sambuc<p> 1079011be35a1SLionel Sambuc 1079111be35a1SLionel Sambuc 1079211be35a1SLionel Sambuc 1079311be35a1SLionel Sambuc 1079411be35a1SLionel Sambuc 1079511be35a1SLionel Sambuc 1079611be35a1SLionel Sambuc 10797*0a6a1f1dSLionel Sambuc 10798*0a6a1f1dSLionel Sambuc<P CLASS="footer"> 1079911be35a1SLionel SambucLast update: 10800*0a6a1f1dSLionel SambucWed Jun 10 18:31:15 BRT 2015 10801*0a6a1f1dSLionel Sambuc</P> 1080211be35a1SLionel Sambuc<!-- 10803*0a6a1f1dSLionel SambucLast change: revised for Lua 5.3.1 1080411be35a1SLionel Sambuc--> 1080511be35a1SLionel Sambuc 1080611be35a1SLionel Sambuc</body></html> 1080711be35a1SLionel Sambuc 10808