manual.html 249 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>Lua 5.1 Reference Manual</title>
  5. <link rel="stylesheet" type="text/css" href="lua.css">
  6. <link rel="stylesheet" type="text/css" href="manual.css">
  7. <META HTTP-EQUIV="content-type" CONTENT="text/html; charset=iso-8859-1">
  8. </head>
  9. <body>
  10. <hr>
  11. <h1>
  12. <a href="http://www.lua.org/"><img src="logo.gif" alt="" border="0"></a>
  13. Lua 5.1 Reference Manual
  14. </h1>
  15. by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes
  16. <p>
  17. <small>
  18. Copyright &copy; 2006&ndash;2012 Lua.org, PUC-Rio.
  19. Freely available under the terms of the
  20. <a href="http://www.lua.org/license.html">Lua license</a>.
  21. </small>
  22. <hr>
  23. <p>
  24. <a href="contents.html#contents">contents</A>
  25. &middot;
  26. <a href="contents.html#index">index</A>
  27. &middot;
  28. <A HREF="http://www.lua.org/manual/">other versions</A>
  29. <!-- ====================================================================== -->
  30. <p>
  31. <!-- $Id: manual.of,v 1.49.1.2 2012/01/13 20:23:26 roberto Exp $ -->
  32. <h1>1 - <a name="1">Introduction</a></h1>
  33. <p>
  34. Lua is an extension programming language designed to support
  35. general procedural programming with data description
  36. facilities.
  37. It also offers good support for object-oriented programming,
  38. functional programming, and data-driven programming.
  39. Lua is intended to be used as a powerful, light-weight
  40. scripting language for any program that needs one.
  41. Lua is implemented as a library, written in <em>clean</em> C
  42. (that is, in the common subset of ANSI&nbsp;C and C++).
  43. <p>
  44. Being an extension language, Lua has no notion of a "main" program:
  45. it only works <em>embedded</em> in a host client,
  46. called the <em>embedding program</em> or simply the <em>host</em>.
  47. This host program can invoke functions to execute a piece of Lua code,
  48. can write and read Lua variables,
  49. and can register C&nbsp;functions to be called by Lua code.
  50. Through the use of C&nbsp;functions, Lua can be augmented to cope with
  51. a wide range of different domains,
  52. thus creating customized programming languages sharing a syntactical framework.
  53. The Lua distribution includes a sample host program called <code>lua</code>,
  54. which uses the Lua library to offer a complete, stand-alone Lua interpreter.
  55. <p>
  56. Lua is free software,
  57. and is provided as usual with no guarantees,
  58. as stated in its license.
  59. The implementation described in this manual is available
  60. at Lua's official web site, <code>www.lua.org</code>.
  61. <p>
  62. Like any other reference manual,
  63. this document is dry in places.
  64. For a discussion of the decisions behind the design of Lua,
  65. see the technical papers available at Lua's web site.
  66. For a detailed introduction to programming in Lua,
  67. see Roberto's book, <em>Programming in Lua (Second Edition)</em>.
  68. <h1>2 - <a name="2">The Language</a></h1>
  69. <p>
  70. This section describes the lexis, the syntax, and the semantics of Lua.
  71. In other words,
  72. this section describes
  73. which tokens are valid,
  74. how they can be combined,
  75. and what their combinations mean.
  76. <p>
  77. The language constructs will be explained using the usual extended BNF notation,
  78. in which
  79. {<em>a</em>}&nbsp;means&nbsp;0 or more <em>a</em>'s, and
  80. [<em>a</em>]&nbsp;means an optional <em>a</em>.
  81. Non-terminals are shown like non-terminal,
  82. keywords are shown like <b>kword</b>,
  83. and other terminal symbols are shown like `<b>=</b>&acute;.
  84. The complete syntax of Lua can be found in <a href="#8">&sect;8</a>
  85. at the end of this manual.
  86. <h2>2.1 - <a name="2.1">Lexical Conventions</a></h2>
  87. <p>
  88. <em>Names</em>
  89. (also called <em>identifiers</em>)
  90. in Lua can be any string of letters,
  91. digits, and underscores,
  92. not beginning with a digit.
  93. This coincides with the definition of names in most languages.
  94. (The definition of letter depends on the current locale:
  95. any character considered alphabetic by the current locale
  96. can be used in an identifier.)
  97. Identifiers are used to name variables and table fields.
  98. <p>
  99. The following <em>keywords</em> are reserved
  100. and cannot be used as names:
  101. <pre>
  102. and break do else elseif
  103. end false for function if
  104. in local nil not or
  105. repeat return then true until while
  106. </pre>
  107. <p>
  108. Lua is a case-sensitive language:
  109. <code>and</code> is a reserved word, but <code>And</code> and <code>AND</code>
  110. are two different, valid names.
  111. As a convention, names starting with an underscore followed by
  112. uppercase letters (such as <a href="#pdf-_VERSION"><code>_VERSION</code></a>)
  113. are reserved for internal global variables used by Lua.
  114. <p>
  115. The following strings denote other tokens:
  116. <pre>
  117. + - * / % ^ #
  118. == ~= &lt;= &gt;= &lt; &gt; =
  119. ( ) { } [ ]
  120. ; : , . .. ...
  121. </pre>
  122. <p>
  123. <em>Literal strings</em>
  124. can be delimited by matching single or double quotes,
  125. and can contain the following C-like escape sequences:
  126. '<code>\a</code>' (bell),
  127. '<code>\b</code>' (backspace),
  128. '<code>\f</code>' (form feed),
  129. '<code>\n</code>' (newline),
  130. '<code>\r</code>' (carriage return),
  131. '<code>\t</code>' (horizontal tab),
  132. '<code>\v</code>' (vertical tab),
  133. '<code>\\</code>' (backslash),
  134. '<code>\"</code>' (quotation mark [double quote]),
  135. and '<code>\'</code>' (apostrophe [single quote]).
  136. Moreover, a backslash followed by a real newline
  137. results in a newline in the string.
  138. A character in a string can also be specified by its numerical value
  139. using the escape sequence <code>\<em>ddd</em></code>,
  140. where <em>ddd</em> is a sequence of up to three decimal digits.
  141. (Note that if a numerical escape is to be followed by a digit,
  142. it must be expressed using exactly three digits.)
  143. Strings in Lua can contain any 8-bit value, including embedded zeros,
  144. which can be specified as '<code>\0</code>'.
  145. <p>
  146. Literal strings can also be defined using a long format
  147. enclosed by <em>long brackets</em>.
  148. We define an <em>opening long bracket of level <em>n</em></em> as an opening
  149. square bracket followed by <em>n</em> equal signs followed by another
  150. opening square bracket.
  151. So, an opening long bracket of level&nbsp;0 is written as <code>[[</code>,
  152. an opening long bracket of level&nbsp;1 is written as <code>[=[</code>,
  153. and so on.
  154. A <em>closing long bracket</em> is defined similarly;
  155. for instance, a closing long bracket of level&nbsp;4 is written as <code>]====]</code>.
  156. A long string starts with an opening long bracket of any level and
  157. ends at the first closing long bracket of the same level.
  158. Literals in this bracketed form can run for several lines,
  159. do not interpret any escape sequences,
  160. and ignore long brackets of any other level.
  161. They can contain anything except a closing bracket of the proper level.
  162. <p>
  163. For convenience,
  164. when the opening long bracket is immediately followed by a newline,
  165. the newline is not included in the string.
  166. As an example, in a system using ASCII
  167. (in which '<code>a</code>' is coded as&nbsp;97,
  168. newline is coded as&nbsp;10, and '<code>1</code>' is coded as&nbsp;49),
  169. the five literal strings below denote the same string:
  170. <pre>
  171. a = 'alo\n123"'
  172. a = "alo\n123\""
  173. a = '\97lo\10\04923"'
  174. a = [[alo
  175. 123"]]
  176. a = [==[
  177. alo
  178. 123"]==]
  179. </pre>
  180. <p>
  181. A <em>numerical constant</em> can be written with an optional decimal part
  182. and an optional decimal exponent.
  183. Lua also accepts integer hexadecimal constants,
  184. by prefixing them with <code>0x</code>.
  185. Examples of valid numerical constants are
  186. <pre>
  187. 3 3.0 3.1416 314.16e-2 0.31416E1 0xff 0x56
  188. </pre>
  189. <p>
  190. A <em>comment</em> starts with a double hyphen (<code>--</code>)
  191. anywhere outside a string.
  192. If the text immediately after <code>--</code> is not an opening long bracket,
  193. the comment is a <em>short comment</em>,
  194. which runs until the end of the line.
  195. Otherwise, it is a <em>long comment</em>,
  196. which runs until the corresponding closing long bracket.
  197. Long comments are frequently used to disable code temporarily.
  198. <h2>2.2 - <a name="2.2">Values and Types</a></h2>
  199. <p>
  200. Lua is a <em>dynamically typed language</em>.
  201. This means that
  202. variables do not have types; only values do.
  203. There are no type definitions in the language.
  204. All values carry their own type.
  205. <p>
  206. All values in Lua are <em>first-class values</em>.
  207. This means that all values can be stored in variables,
  208. passed as arguments to other functions, and returned as results.
  209. <p>
  210. There are eight basic types in Lua:
  211. <em>nil</em>, <em>boolean</em>, <em>number</em>,
  212. <em>string</em>, <em>function</em>, <em>userdata</em>,
  213. <em>thread</em>, and <em>table</em>.
  214. <em>Nil</em> is the type of the value <b>nil</b>,
  215. whose main property is to be different from any other value;
  216. it usually represents the absence of a useful value.
  217. <em>Boolean</em> is the type of the values <b>false</b> and <b>true</b>.
  218. Both <b>nil</b> and <b>false</b> make a condition false;
  219. any other value makes it true.
  220. <em>Number</em> represents real (double-precision floating-point) numbers.
  221. (It is easy to build Lua interpreters that use other
  222. internal representations for numbers,
  223. such as single-precision float or long integers;
  224. see file <code>luaconf.h</code>.)
  225. <em>String</em> represents arrays of characters.
  226. Lua is 8-bit clean:
  227. strings can contain any 8-bit character,
  228. including embedded zeros ('<code>\0</code>') (see <a href="#2.1">&sect;2.1</a>).
  229. <p>
  230. Lua can call (and manipulate) functions written in Lua and
  231. functions written in C
  232. (see <a href="#2.5.8">&sect;2.5.8</a>).
  233. <p>
  234. The type <em>userdata</em> is provided to allow arbitrary C&nbsp;data to
  235. be stored in Lua variables.
  236. This type corresponds to a block of raw memory
  237. and has no pre-defined operations in Lua,
  238. except assignment and identity test.
  239. However, by using <em>metatables</em>,
  240. the programmer can define operations for userdata values
  241. (see <a href="#2.8">&sect;2.8</a>).
  242. Userdata values cannot be created or modified in Lua,
  243. only through the C&nbsp;API.
  244. This guarantees the integrity of data owned by the host program.
  245. <p>
  246. The type <em>thread</em> represents independent threads of execution
  247. and it is used to implement coroutines (see <a href="#2.11">&sect;2.11</a>).
  248. Do not confuse Lua threads with operating-system threads.
  249. Lua supports coroutines on all systems,
  250. even those that do not support threads.
  251. <p>
  252. The type <em>table</em> implements associative arrays,
  253. that is, arrays that can be indexed not only with numbers,
  254. but with any value (except <b>nil</b>).
  255. Tables can be <em>heterogeneous</em>;
  256. that is, they can contain values of all types (except <b>nil</b>).
  257. Tables are the sole data structuring mechanism in Lua;
  258. they can be used to represent ordinary arrays,
  259. symbol tables, sets, records, graphs, trees, etc.
  260. To represent records, Lua uses the field name as an index.
  261. The language supports this representation by
  262. providing <code>a.name</code> as syntactic sugar for <code>a["name"]</code>.
  263. There are several convenient ways to create tables in Lua
  264. (see <a href="#2.5.7">&sect;2.5.7</a>).
  265. <p>
  266. Like indices,
  267. the value of a table field can be of any type (except <b>nil</b>).
  268. In particular,
  269. because functions are first-class values,
  270. table fields can contain functions.
  271. Thus tables can also carry <em>methods</em> (see <a href="#2.5.9">&sect;2.5.9</a>).
  272. <p>
  273. Tables, functions, threads, and (full) userdata values are <em>objects</em>:
  274. variables do not actually <em>contain</em> these values,
  275. only <em>references</em> to them.
  276. Assignment, parameter passing, and function returns
  277. always manipulate references to such values;
  278. these operations do not imply any kind of copy.
  279. <p>
  280. The library function <a href="#pdf-type"><code>type</code></a> returns a string describing the type
  281. of a given value.
  282. <h3>2.2.1 - <a name="2.2.1">Coercion</a></h3>
  283. <p>
  284. Lua provides automatic conversion between
  285. string and number values at run time.
  286. Any arithmetic operation applied to a string tries to convert
  287. this string to a number, following the usual conversion rules.
  288. Conversely, whenever a number is used where a string is expected,
  289. the number is converted to a string, in a reasonable format.
  290. For complete control over how numbers are converted to strings,
  291. use the <code>format</code> function from the string library
  292. (see <a href="#pdf-string.format"><code>string.format</code></a>).
  293. <h2>2.3 - <a name="2.3">Variables</a></h2>
  294. <p>
  295. Variables are places that store values.
  296. There are three kinds of variables in Lua:
  297. global variables, local variables, and table fields.
  298. <p>
  299. A single name can denote a global variable or a local variable
  300. (or a function's formal parameter,
  301. which is a particular kind of local variable):
  302. <pre>
  303. var ::= Name
  304. </pre><p>
  305. Name denotes identifiers, as defined in <a href="#2.1">&sect;2.1</a>.
  306. <p>
  307. Any variable is assumed to be global unless explicitly declared
  308. as a local (see <a href="#2.4.7">&sect;2.4.7</a>).
  309. Local variables are <em>lexically scoped</em>:
  310. local variables can be freely accessed by functions
  311. defined inside their scope (see <a href="#2.6">&sect;2.6</a>).
  312. <p>
  313. Before the first assignment to a variable, its value is <b>nil</b>.
  314. <p>
  315. Square brackets are used to index a table:
  316. <pre>
  317. var ::= prefixexp `<b>[</b>&acute; exp `<b>]</b>&acute;
  318. </pre><p>
  319. The meaning of accesses to global variables
  320. and table fields can be changed via metatables.
  321. An access to an indexed variable <code>t[i]</code> is equivalent to
  322. a call <code>gettable_event(t,i)</code>.
  323. (See <a href="#2.8">&sect;2.8</a> for a complete description of the
  324. <code>gettable_event</code> function.
  325. This function is not defined or callable in Lua.
  326. We use it here only for explanatory purposes.)
  327. <p>
  328. The syntax <code>var.Name</code> is just syntactic sugar for
  329. <code>var["Name"]</code>:
  330. <pre>
  331. var ::= prefixexp `<b>.</b>&acute; Name
  332. </pre>
  333. <p>
  334. All global variables live as fields in ordinary Lua tables,
  335. called <em>environment tables</em> or simply
  336. <em>environments</em> (see <a href="#2.9">&sect;2.9</a>).
  337. Each function has its own reference to an environment,
  338. so that all global variables in this function
  339. will refer to this environment table.
  340. When a function is created,
  341. it inherits the environment from the function that created it.
  342. To get the environment table of a Lua function,
  343. you call <a href="#pdf-getfenv"><code>getfenv</code></a>.
  344. To replace it,
  345. you call <a href="#pdf-setfenv"><code>setfenv</code></a>.
  346. (You can only manipulate the environment of C&nbsp;functions
  347. through the debug library; (see <a href="#5.9">&sect;5.9</a>).)
  348. <p>
  349. An access to a global variable <code>x</code>
  350. is equivalent to <code>_env.x</code>,
  351. which in turn is equivalent to
  352. <pre>
  353. gettable_event(_env, "x")
  354. </pre><p>
  355. where <code>_env</code> is the environment of the running function.
  356. (See <a href="#2.8">&sect;2.8</a> for a complete description of the
  357. <code>gettable_event</code> function.
  358. This function is not defined or callable in Lua.
  359. Similarly, the <code>_env</code> variable is not defined in Lua.
  360. We use them here only for explanatory purposes.)
  361. <h2>2.4 - <a name="2.4">Statements</a></h2>
  362. <p>
  363. Lua supports an almost conventional set of statements,
  364. similar to those in Pascal or C.
  365. This set includes
  366. assignments, control structures, function calls,
  367. and variable declarations.
  368. <h3>2.4.1 - <a name="2.4.1">Chunks</a></h3>
  369. <p>
  370. The unit of execution of Lua is called a <em>chunk</em>.
  371. A chunk is simply a sequence of statements,
  372. which are executed sequentially.
  373. Each statement can be optionally followed by a semicolon:
  374. <pre>
  375. chunk ::= {stat [`<b>;</b>&acute;]}
  376. </pre><p>
  377. There are no empty statements and thus '<code>;;</code>' is not legal.
  378. <p>
  379. Lua handles a chunk as the body of an anonymous function
  380. with a variable number of arguments
  381. (see <a href="#2.5.9">&sect;2.5.9</a>).
  382. As such, chunks can define local variables,
  383. receive arguments, and return values.
  384. <p>
  385. A chunk can be stored in a file or in a string inside the host program.
  386. To execute a chunk,
  387. Lua first pre-compiles the chunk into instructions for a virtual machine,
  388. and then it executes the compiled code
  389. with an interpreter for the virtual machine.
  390. <p>
  391. Chunks can also be pre-compiled into binary form;
  392. see program <code>luac</code> for details.
  393. Programs in source and compiled forms are interchangeable;
  394. Lua automatically detects the file type and acts accordingly.
  395. <h3>2.4.2 - <a name="2.4.2">Blocks</a></h3><p>
  396. A block is a list of statements;
  397. syntactically, a block is the same as a chunk:
  398. <pre>
  399. block ::= chunk
  400. </pre>
  401. <p>
  402. A block can be explicitly delimited to produce a single statement:
  403. <pre>
  404. stat ::= <b>do</b> block <b>end</b>
  405. </pre><p>
  406. Explicit blocks are useful
  407. to control the scope of variable declarations.
  408. Explicit blocks are also sometimes used to
  409. add a <b>return</b> or <b>break</b> statement in the middle
  410. of another block (see <a href="#2.4.4">&sect;2.4.4</a>).
  411. <h3>2.4.3 - <a name="2.4.3">Assignment</a></h3>
  412. <p>
  413. Lua allows multiple assignments.
  414. Therefore, the syntax for assignment
  415. defines a list of variables on the left side
  416. and a list of expressions on the right side.
  417. The elements in both lists are separated by commas:
  418. <pre>
  419. stat ::= varlist `<b>=</b>&acute; explist
  420. varlist ::= var {`<b>,</b>&acute; var}
  421. explist ::= exp {`<b>,</b>&acute; exp}
  422. </pre><p>
  423. Expressions are discussed in <a href="#2.5">&sect;2.5</a>.
  424. <p>
  425. Before the assignment,
  426. the list of values is <em>adjusted</em> to the length of
  427. the list of variables.
  428. If there are more values than needed,
  429. the excess values are thrown away.
  430. If there are fewer values than needed,
  431. the list is extended with as many <b>nil</b>'s as needed.
  432. If the list of expressions ends with a function call,
  433. then all values returned by that call enter the list of values,
  434. before the adjustment
  435. (except when the call is enclosed in parentheses; see <a href="#2.5">&sect;2.5</a>).
  436. <p>
  437. The assignment statement first evaluates all its expressions
  438. and only then are the assignments performed.
  439. Thus the code
  440. <pre>
  441. i = 3
  442. i, a[i] = i+1, 20
  443. </pre><p>
  444. sets <code>a[3]</code> to 20, without affecting <code>a[4]</code>
  445. because the <code>i</code> in <code>a[i]</code> is evaluated (to 3)
  446. before it is assigned&nbsp;4.
  447. Similarly, the line
  448. <pre>
  449. x, y = y, x
  450. </pre><p>
  451. exchanges the values of <code>x</code> and <code>y</code>,
  452. and
  453. <pre>
  454. x, y, z = y, z, x
  455. </pre><p>
  456. cyclically permutes the values of <code>x</code>, <code>y</code>, and <code>z</code>.
  457. <p>
  458. The meaning of assignments to global variables
  459. and table fields can be changed via metatables.
  460. An assignment to an indexed variable <code>t[i] = val</code> is equivalent to
  461. <code>settable_event(t,i,val)</code>.
  462. (See <a href="#2.8">&sect;2.8</a> for a complete description of the
  463. <code>settable_event</code> function.
  464. This function is not defined or callable in Lua.
  465. We use it here only for explanatory purposes.)
  466. <p>
  467. An assignment to a global variable <code>x = val</code>
  468. is equivalent to the assignment
  469. <code>_env.x = val</code>,
  470. which in turn is equivalent to
  471. <pre>
  472. settable_event(_env, "x", val)
  473. </pre><p>
  474. where <code>_env</code> is the environment of the running function.
  475. (The <code>_env</code> variable is not defined in Lua.
  476. We use it here only for explanatory purposes.)
  477. <h3>2.4.4 - <a name="2.4.4">Control Structures</a></h3><p>
  478. The control structures
  479. <b>if</b>, <b>while</b>, and <b>repeat</b> have the usual meaning and
  480. familiar syntax:
  481. <pre>
  482. stat ::= <b>while</b> exp <b>do</b> block <b>end</b>
  483. stat ::= <b>repeat</b> block <b>until</b> exp
  484. stat ::= <b>if</b> exp <b>then</b> block {<b>elseif</b> exp <b>then</b> block} [<b>else</b> block] <b>end</b>
  485. </pre><p>
  486. Lua also has a <b>for</b> statement, in two flavors (see <a href="#2.4.5">&sect;2.4.5</a>).
  487. <p>
  488. The condition expression of a
  489. control structure can return any value.
  490. Both <b>false</b> and <b>nil</b> are considered false.
  491. All values different from <b>nil</b> and <b>false</b> are considered true
  492. (in particular, the number 0 and the empty string are also true).
  493. <p>
  494. In the <b>repeat</b>&ndash;<b>until</b> loop,
  495. the inner block does not end at the <b>until</b> keyword,
  496. but only after the condition.
  497. So, the condition can refer to local variables
  498. declared inside the loop block.
  499. <p>
  500. The <b>return</b> statement is used to return values
  501. from a function or a chunk (which is just a function).
  502. Functions and chunks can return more than one value,
  503. and so the syntax for the <b>return</b> statement is
  504. <pre>
  505. stat ::= <b>return</b> [explist]
  506. </pre>
  507. <p>
  508. The <b>break</b> statement is used to terminate the execution of a
  509. <b>while</b>, <b>repeat</b>, or <b>for</b> loop,
  510. skipping to the next statement after the loop:
  511. <pre>
  512. stat ::= <b>break</b>
  513. </pre><p>
  514. A <b>break</b> ends the innermost enclosing loop.
  515. <p>
  516. The <b>return</b> and <b>break</b>
  517. statements can only be written as the <em>last</em> statement of a block.
  518. If it is really necessary to <b>return</b> or <b>break</b> in the
  519. middle of a block,
  520. then an explicit inner block can be used,
  521. as in the idioms
  522. <code>do return end</code> and <code>do break end</code>,
  523. because now <b>return</b> and <b>break</b> are the last statements in
  524. their (inner) blocks.
  525. <h3>2.4.5 - <a name="2.4.5">For Statement</a></h3>
  526. <p>
  527. The <b>for</b> statement has two forms:
  528. one numeric and one generic.
  529. <p>
  530. The numeric <b>for</b> loop repeats a block of code while a
  531. control variable runs through an arithmetic progression.
  532. It has the following syntax:
  533. <pre>
  534. stat ::= <b>for</b> Name `<b>=</b>&acute; exp `<b>,</b>&acute; exp [`<b>,</b>&acute; exp] <b>do</b> block <b>end</b>
  535. </pre><p>
  536. The <em>block</em> is repeated for <em>name</em> starting at the value of
  537. the first <em>exp</em>, until it passes the second <em>exp</em> by steps of the
  538. third <em>exp</em>.
  539. More precisely, a <b>for</b> statement like
  540. <pre>
  541. for v = <em>e1</em>, <em>e2</em>, <em>e3</em> do <em>block</em> end
  542. </pre><p>
  543. is equivalent to the code:
  544. <pre>
  545. do
  546. local <em>var</em>, <em>limit</em>, <em>step</em> = tonumber(<em>e1</em>), tonumber(<em>e2</em>), tonumber(<em>e3</em>)
  547. if not (<em>var</em> and <em>limit</em> and <em>step</em>) then error() end
  548. while (<em>step</em> &gt; 0 and <em>var</em> &lt;= <em>limit</em>) or (<em>step</em> &lt;= 0 and <em>var</em> &gt;= <em>limit</em>) do
  549. local v = <em>var</em>
  550. <em>block</em>
  551. <em>var</em> = <em>var</em> + <em>step</em>
  552. end
  553. end
  554. </pre><p>
  555. Note the following:
  556. <ul>
  557. <li>
  558. All three control expressions are evaluated only once,
  559. before the loop starts.
  560. They must all result in numbers.
  561. </li>
  562. <li>
  563. <code><em>var</em></code>, <code><em>limit</em></code>, and <code><em>step</em></code> are invisible variables.
  564. The names shown here are for explanatory purposes only.
  565. </li>
  566. <li>
  567. If the third expression (the step) is absent,
  568. then a step of&nbsp;1 is used.
  569. </li>
  570. <li>
  571. You can use <b>break</b> to exit a <b>for</b> loop.
  572. </li>
  573. <li>
  574. The loop variable <code>v</code> is local to the loop;
  575. you cannot use its value after the <b>for</b> ends or is broken.
  576. If you need this value,
  577. assign it to another variable before breaking or exiting the loop.
  578. </li>
  579. </ul>
  580. <p>
  581. The generic <b>for</b> statement works over functions,
  582. called <em>iterators</em>.
  583. On each iteration, the iterator function is called to produce a new value,
  584. stopping when this new value is <b>nil</b>.
  585. The generic <b>for</b> loop has the following syntax:
  586. <pre>
  587. stat ::= <b>for</b> namelist <b>in</b> explist <b>do</b> block <b>end</b>
  588. namelist ::= Name {`<b>,</b>&acute; Name}
  589. </pre><p>
  590. A <b>for</b> statement like
  591. <pre>
  592. for <em>var_1</em>, &middot;&middot;&middot;, <em>var_n</em> in <em>explist</em> do <em>block</em> end
  593. </pre><p>
  594. is equivalent to the code:
  595. <pre>
  596. do
  597. local <em>f</em>, <em>s</em>, <em>var</em> = <em>explist</em>
  598. while true do
  599. local <em>var_1</em>, &middot;&middot;&middot;, <em>var_n</em> = <em>f</em>(<em>s</em>, <em>var</em>)
  600. <em>var</em> = <em>var_1</em>
  601. if <em>var</em> == nil then break end
  602. <em>block</em>
  603. end
  604. end
  605. </pre><p>
  606. Note the following:
  607. <ul>
  608. <li>
  609. <code><em>explist</em></code> is evaluated only once.
  610. Its results are an <em>iterator</em> function,
  611. a <em>state</em>,
  612. and an initial value for the first <em>iterator variable</em>.
  613. </li>
  614. <li>
  615. <code><em>f</em></code>, <code><em>s</em></code>, and <code><em>var</em></code> are invisible variables.
  616. The names are here for explanatory purposes only.
  617. </li>
  618. <li>
  619. You can use <b>break</b> to exit a <b>for</b> loop.
  620. </li>
  621. <li>
  622. The loop variables <code><em>var_i</em></code> are local to the loop;
  623. you cannot use their values after the <b>for</b> ends.
  624. If you need these values,
  625. then assign them to other variables before breaking or exiting the loop.
  626. </li>
  627. </ul>
  628. <h3>2.4.6 - <a name="2.4.6">Function Calls as Statements</a></h3><p>
  629. To allow possible side-effects,
  630. function calls can be executed as statements:
  631. <pre>
  632. stat ::= functioncall
  633. </pre><p>
  634. In this case, all returned values are thrown away.
  635. Function calls are explained in <a href="#2.5.8">&sect;2.5.8</a>.
  636. <h3>2.4.7 - <a name="2.4.7">Local Declarations</a></h3><p>
  637. Local variables can be declared anywhere inside a block.
  638. The declaration can include an initial assignment:
  639. <pre>
  640. stat ::= <b>local</b> namelist [`<b>=</b>&acute; explist]
  641. </pre><p>
  642. If present, an initial assignment has the same semantics
  643. of a multiple assignment (see <a href="#2.4.3">&sect;2.4.3</a>).
  644. Otherwise, all variables are initialized with <b>nil</b>.
  645. <p>
  646. A chunk is also a block (see <a href="#2.4.1">&sect;2.4.1</a>),
  647. and so local variables can be declared in a chunk outside any explicit block.
  648. The scope of such local variables extends until the end of the chunk.
  649. <p>
  650. The visibility rules for local variables are explained in <a href="#2.6">&sect;2.6</a>.
  651. <h2>2.5 - <a name="2.5">Expressions</a></h2>
  652. <p>
  653. The basic expressions in Lua are the following:
  654. <pre>
  655. exp ::= prefixexp
  656. exp ::= <b>nil</b> | <b>false</b> | <b>true</b>
  657. exp ::= Number
  658. exp ::= String
  659. exp ::= function
  660. exp ::= tableconstructor
  661. exp ::= `<b>...</b>&acute;
  662. exp ::= exp binop exp
  663. exp ::= unop exp
  664. prefixexp ::= var | functioncall | `<b>(</b>&acute; exp `<b>)</b>&acute;
  665. </pre>
  666. <p>
  667. Numbers and literal strings are explained in <a href="#2.1">&sect;2.1</a>;
  668. variables are explained in <a href="#2.3">&sect;2.3</a>;
  669. function definitions are explained in <a href="#2.5.9">&sect;2.5.9</a>;
  670. function calls are explained in <a href="#2.5.8">&sect;2.5.8</a>;
  671. table constructors are explained in <a href="#2.5.7">&sect;2.5.7</a>.
  672. Vararg expressions,
  673. denoted by three dots ('<code>...</code>'), can only be used when
  674. directly inside a vararg function;
  675. they are explained in <a href="#2.5.9">&sect;2.5.9</a>.
  676. <p>
  677. Binary operators comprise arithmetic operators (see <a href="#2.5.1">&sect;2.5.1</a>),
  678. relational operators (see <a href="#2.5.2">&sect;2.5.2</a>), logical operators (see <a href="#2.5.3">&sect;2.5.3</a>),
  679. and the concatenation operator (see <a href="#2.5.4">&sect;2.5.4</a>).
  680. Unary operators comprise the unary minus (see <a href="#2.5.1">&sect;2.5.1</a>),
  681. the unary <b>not</b> (see <a href="#2.5.3">&sect;2.5.3</a>),
  682. and the unary <em>length operator</em> (see <a href="#2.5.5">&sect;2.5.5</a>).
  683. <p>
  684. Both function calls and vararg expressions can result in multiple values.
  685. If an expression is used as a statement
  686. (only possible for function calls (see <a href="#2.4.6">&sect;2.4.6</a>)),
  687. then its return list is adjusted to zero elements,
  688. thus discarding all returned values.
  689. If an expression is used as the last (or the only) element
  690. of a list of expressions,
  691. then no adjustment is made
  692. (unless the call is enclosed in parentheses).
  693. In all other contexts,
  694. Lua adjusts the result list to one element,
  695. discarding all values except the first one.
  696. <p>
  697. Here are some examples:
  698. <pre>
  699. f() -- adjusted to 0 results
  700. g(f(), x) -- f() is adjusted to 1 result
  701. g(x, f()) -- g gets x plus all results from f()
  702. a,b,c = f(), x -- f() is adjusted to 1 result (c gets nil)
  703. a,b = ... -- a gets the first vararg parameter, b gets
  704. -- the second (both a and b can get nil if there
  705. -- is no corresponding vararg parameter)
  706. a,b,c = x, f() -- f() is adjusted to 2 results
  707. a,b,c = f() -- f() is adjusted to 3 results
  708. return f() -- returns all results from f()
  709. return ... -- returns all received vararg parameters
  710. return x,y,f() -- returns x, y, and all results from f()
  711. {f()} -- creates a list with all results from f()
  712. {...} -- creates a list with all vararg parameters
  713. {f(), nil} -- f() is adjusted to 1 result
  714. </pre>
  715. <p>
  716. Any expression enclosed in parentheses always results in only one value.
  717. Thus,
  718. <code>(f(x,y,z))</code> is always a single value,
  719. even if <code>f</code> returns several values.
  720. (The value of <code>(f(x,y,z))</code> is the first value returned by <code>f</code>
  721. or <b>nil</b> if <code>f</code> does not return any values.)
  722. <h3>2.5.1 - <a name="2.5.1">Arithmetic Operators</a></h3><p>
  723. Lua supports the usual arithmetic operators:
  724. the binary <code>+</code> (addition),
  725. <code>-</code> (subtraction), <code>*</code> (multiplication),
  726. <code>/</code> (division), <code>%</code> (modulo), and <code>^</code> (exponentiation);
  727. and unary <code>-</code> (negation).
  728. If the operands are numbers, or strings that can be converted to
  729. numbers (see <a href="#2.2.1">&sect;2.2.1</a>),
  730. then all operations have the usual meaning.
  731. Exponentiation works for any exponent.
  732. For instance, <code>x^(-0.5)</code> computes the inverse of the square root of <code>x</code>.
  733. Modulo is defined as
  734. <pre>
  735. a % b == a - math.floor(a/b)*b
  736. </pre><p>
  737. That is, it is the remainder of a division that rounds
  738. the quotient towards minus infinity.
  739. <h3>2.5.2 - <a name="2.5.2">Relational Operators</a></h3><p>
  740. The relational operators in Lua are
  741. <pre>
  742. == ~= &lt; &gt; &lt;= &gt;=
  743. </pre><p>
  744. These operators always result in <b>false</b> or <b>true</b>.
  745. <p>
  746. Equality (<code>==</code>) first compares the type of its operands.
  747. If the types are different, then the result is <b>false</b>.
  748. Otherwise, the values of the operands are compared.
  749. Numbers and strings are compared in the usual way.
  750. Objects (tables, userdata, threads, and functions)
  751. are compared by <em>reference</em>:
  752. two objects are considered equal only if they are the <em>same</em> object.
  753. Every time you create a new object
  754. (a table, userdata, thread, or function),
  755. this new object is different from any previously existing object.
  756. <p>
  757. You can change the way that Lua compares tables and userdata
  758. by using the "eq" metamethod (see <a href="#2.8">&sect;2.8</a>).
  759. <p>
  760. The conversion rules of <a href="#2.2.1">&sect;2.2.1</a>
  761. <em>do not</em> apply to equality comparisons.
  762. Thus, <code>"0"==0</code> evaluates to <b>false</b>,
  763. and <code>t[0]</code> and <code>t["0"]</code> denote different
  764. entries in a table.
  765. <p>
  766. The operator <code>~=</code> is exactly the negation of equality (<code>==</code>).
  767. <p>
  768. The order operators work as follows.
  769. If both arguments are numbers, then they are compared as such.
  770. Otherwise, if both arguments are strings,
  771. then their values are compared according to the current locale.
  772. Otherwise, Lua tries to call the "lt" or the "le"
  773. metamethod (see <a href="#2.8">&sect;2.8</a>).
  774. A comparison <code>a &gt; b</code> is translated to <code>b &lt; a</code>
  775. and <code>a &gt;= b</code> is translated to <code>b &lt;= a</code>.
  776. <h3>2.5.3 - <a name="2.5.3">Logical Operators</a></h3><p>
  777. The logical operators in Lua are
  778. <b>and</b>, <b>or</b>, and <b>not</b>.
  779. Like the control structures (see <a href="#2.4.4">&sect;2.4.4</a>),
  780. all logical operators consider both <b>false</b> and <b>nil</b> as false
  781. and anything else as true.
  782. <p>
  783. The negation operator <b>not</b> always returns <b>false</b> or <b>true</b>.
  784. The conjunction operator <b>and</b> returns its first argument
  785. if this value is <b>false</b> or <b>nil</b>;
  786. otherwise, <b>and</b> returns its second argument.
  787. The disjunction operator <b>or</b> returns its first argument
  788. if this value is different from <b>nil</b> and <b>false</b>;
  789. otherwise, <b>or</b> returns its second argument.
  790. Both <b>and</b> and <b>or</b> use short-cut evaluation;
  791. that is,
  792. the second operand is evaluated only if necessary.
  793. Here are some examples:
  794. <pre>
  795. 10 or 20 --&gt; 10
  796. 10 or error() --&gt; 10
  797. nil or "a" --&gt; "a"
  798. nil and 10 --&gt; nil
  799. false and error() --&gt; false
  800. false and nil --&gt; false
  801. false or nil --&gt; nil
  802. 10 and 20 --&gt; 20
  803. </pre><p>
  804. (In this manual,
  805. <code>--&gt;</code> indicates the result of the preceding expression.)
  806. <h3>2.5.4 - <a name="2.5.4">Concatenation</a></h3><p>
  807. The string concatenation operator in Lua is
  808. denoted by two dots ('<code>..</code>').
  809. If both operands are strings or numbers, then they are converted to
  810. strings according to the rules mentioned in <a href="#2.2.1">&sect;2.2.1</a>.
  811. Otherwise, the "concat" metamethod is called (see <a href="#2.8">&sect;2.8</a>).
  812. <h3>2.5.5 - <a name="2.5.5">The Length Operator</a></h3>
  813. <p>
  814. The length operator is denoted by the unary operator <code>#</code>.
  815. The length of a string is its number of bytes
  816. (that is, the usual meaning of string length when each
  817. character is one byte).
  818. <p>
  819. The length of a table <code>t</code> is defined to be any
  820. integer index <code>n</code>
  821. such that <code>t[n]</code> is not <b>nil</b> and <code>t[n+1]</code> is <b>nil</b>;
  822. moreover, if <code>t[1]</code> is <b>nil</b>, <code>n</code> can be zero.
  823. For a regular array, with non-nil values from 1 to a given <code>n</code>,
  824. its length is exactly that <code>n</code>,
  825. the index of its last value.
  826. If the array has "holes"
  827. (that is, <b>nil</b> values between other non-nil values),
  828. then <code>#t</code> can be any of the indices that
  829. directly precedes a <b>nil</b> value
  830. (that is, it may consider any such <b>nil</b> value as the end of
  831. the array).
  832. <h3>2.5.6 - <a name="2.5.6">Precedence</a></h3><p>
  833. Operator precedence in Lua follows the table below,
  834. from lower to higher priority:
  835. <pre>
  836. or
  837. and
  838. &lt; &gt; &lt;= &gt;= ~= ==
  839. ..
  840. + -
  841. * / %
  842. not # - (unary)
  843. ^
  844. </pre><p>
  845. As usual,
  846. you can use parentheses to change the precedences of an expression.
  847. The concatenation ('<code>..</code>') and exponentiation ('<code>^</code>')
  848. operators are right associative.
  849. All other binary operators are left associative.
  850. <h3>2.5.7 - <a name="2.5.7">Table Constructors</a></h3><p>
  851. Table constructors are expressions that create tables.
  852. Every time a constructor is evaluated, a new table is created.
  853. A constructor can be used to create an empty table
  854. or to create a table and initialize some of its fields.
  855. The general syntax for constructors is
  856. <pre>
  857. tableconstructor ::= `<b>{</b>&acute; [fieldlist] `<b>}</b>&acute;
  858. fieldlist ::= field {fieldsep field} [fieldsep]
  859. field ::= `<b>[</b>&acute; exp `<b>]</b>&acute; `<b>=</b>&acute; exp | Name `<b>=</b>&acute; exp | exp
  860. fieldsep ::= `<b>,</b>&acute; | `<b>;</b>&acute;
  861. </pre>
  862. <p>
  863. Each field of the form <code>[exp1] = exp2</code> adds to the new table an entry
  864. with key <code>exp1</code> and value <code>exp2</code>.
  865. A field of the form <code>name = exp</code> is equivalent to
  866. <code>["name"] = exp</code>.
  867. Finally, fields of the form <code>exp</code> are equivalent to
  868. <code>[i] = exp</code>, where <code>i</code> are consecutive numerical integers,
  869. starting with 1.
  870. Fields in the other formats do not affect this counting.
  871. For example,
  872. <pre>
  873. a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 }
  874. </pre><p>
  875. is equivalent to
  876. <pre>
  877. do
  878. local t = {}
  879. t[f(1)] = g
  880. t[1] = "x" -- 1st exp
  881. t[2] = "y" -- 2nd exp
  882. t.x = 1 -- t["x"] = 1
  883. t[3] = f(x) -- 3rd exp
  884. t[30] = 23
  885. t[4] = 45 -- 4th exp
  886. a = t
  887. end
  888. </pre>
  889. <p>
  890. If the last field in the list has the form <code>exp</code>
  891. and the expression is a function call or a vararg expression,
  892. then all values returned by this expression enter the list consecutively
  893. (see <a href="#2.5.8">&sect;2.5.8</a>).
  894. To avoid this,
  895. enclose the function call or the vararg expression
  896. in parentheses (see <a href="#2.5">&sect;2.5</a>).
  897. <p>
  898. The field list can have an optional trailing separator,
  899. as a convenience for machine-generated code.
  900. <h3>2.5.8 - <a name="2.5.8">Function Calls</a></h3><p>
  901. A function call in Lua has the following syntax:
  902. <pre>
  903. functioncall ::= prefixexp args
  904. </pre><p>
  905. In a function call,
  906. first prefixexp and args are evaluated.
  907. If the value of prefixexp has type <em>function</em>,
  908. then this function is called
  909. with the given arguments.
  910. Otherwise, the prefixexp "call" metamethod is called,
  911. having as first parameter the value of prefixexp,
  912. followed by the original call arguments
  913. (see <a href="#2.8">&sect;2.8</a>).
  914. <p>
  915. The form
  916. <pre>
  917. functioncall ::= prefixexp `<b>:</b>&acute; Name args
  918. </pre><p>
  919. can be used to call "methods".
  920. A call <code>v:name(<em>args</em>)</code>
  921. is syntactic sugar for <code>v.name(v,<em>args</em>)</code>,
  922. except that <code>v</code> is evaluated only once.
  923. <p>
  924. Arguments have the following syntax:
  925. <pre>
  926. args ::= `<b>(</b>&acute; [explist] `<b>)</b>&acute;
  927. args ::= tableconstructor
  928. args ::= String
  929. </pre><p>
  930. All argument expressions are evaluated before the call.
  931. A call of the form <code>f{<em>fields</em>}</code> is
  932. syntactic sugar for <code>f({<em>fields</em>})</code>;
  933. that is, the argument list is a single new table.
  934. A call of the form <code>f'<em>string</em>'</code>
  935. (or <code>f"<em>string</em>"</code> or <code>f[[<em>string</em>]]</code>)
  936. is syntactic sugar for <code>f('<em>string</em>')</code>;
  937. that is, the argument list is a single literal string.
  938. <p>
  939. As an exception to the free-format syntax of Lua,
  940. you cannot put a line break before the '<code>(</code>' in a function call.
  941. This restriction avoids some ambiguities in the language.
  942. If you write
  943. <pre>
  944. a = f
  945. (g).x(a)
  946. </pre><p>
  947. Lua would see that as a single statement, <code>a = f(g).x(a)</code>.
  948. So, if you want two statements, you must add a semi-colon between them.
  949. If you actually want to call <code>f</code>,
  950. you must remove the line break before <code>(g)</code>.
  951. <p>
  952. A call of the form <code>return</code> <em>functioncall</em> is called
  953. a <em>tail call</em>.
  954. Lua implements <em>proper tail calls</em>
  955. (or <em>proper tail recursion</em>):
  956. in a tail call,
  957. the called function reuses the stack entry of the calling function.
  958. Therefore, there is no limit on the number of nested tail calls that
  959. a program can execute.
  960. However, a tail call erases any debug information about the
  961. calling function.
  962. Note that a tail call only happens with a particular syntax,
  963. where the <b>return</b> has one single function call as argument;
  964. this syntax makes the calling function return exactly
  965. the returns of the called function.
  966. So, none of the following examples are tail calls:
  967. <pre>
  968. return (f(x)) -- results adjusted to 1
  969. return 2 * f(x)
  970. return x, f(x) -- additional results
  971. f(x); return -- results discarded
  972. return x or f(x) -- results adjusted to 1
  973. </pre>
  974. <h3>2.5.9 - <a name="2.5.9">Function Definitions</a></h3>
  975. <p>
  976. The syntax for function definition is
  977. <pre>
  978. function ::= <b>function</b> funcbody
  979. funcbody ::= `<b>(</b>&acute; [parlist] `<b>)</b>&acute; block <b>end</b>
  980. </pre>
  981. <p>
  982. The following syntactic sugar simplifies function definitions:
  983. <pre>
  984. stat ::= <b>function</b> funcname funcbody
  985. stat ::= <b>local</b> <b>function</b> Name funcbody
  986. funcname ::= Name {`<b>.</b>&acute; Name} [`<b>:</b>&acute; Name]
  987. </pre><p>
  988. The statement
  989. <pre>
  990. function f () <em>body</em> end
  991. </pre><p>
  992. translates to
  993. <pre>
  994. f = function () <em>body</em> end
  995. </pre><p>
  996. The statement
  997. <pre>
  998. function t.a.b.c.f () <em>body</em> end
  999. </pre><p>
  1000. translates to
  1001. <pre>
  1002. t.a.b.c.f = function () <em>body</em> end
  1003. </pre><p>
  1004. The statement
  1005. <pre>
  1006. local function f () <em>body</em> end
  1007. </pre><p>
  1008. translates to
  1009. <pre>
  1010. local f; f = function () <em>body</em> end
  1011. </pre><p>
  1012. <em>not</em> to
  1013. <pre>
  1014. local f = function () <em>body</em> end
  1015. </pre><p>
  1016. (This only makes a difference when the body of the function
  1017. contains references to <code>f</code>.)
  1018. <p>
  1019. A function definition is an executable expression,
  1020. whose value has type <em>function</em>.
  1021. When Lua pre-compiles a chunk,
  1022. all its function bodies are pre-compiled too.
  1023. Then, whenever Lua executes the function definition,
  1024. the function is <em>instantiated</em> (or <em>closed</em>).
  1025. This function instance (or <em>closure</em>)
  1026. is the final value of the expression.
  1027. Different instances of the same function
  1028. can refer to different external local variables
  1029. and can have different environment tables.
  1030. <p>
  1031. Parameters act as local variables that are
  1032. initialized with the argument values:
  1033. <pre>
  1034. parlist ::= namelist [`<b>,</b>&acute; `<b>...</b>&acute;] | `<b>...</b>&acute;
  1035. </pre><p>
  1036. When a function is called,
  1037. the list of arguments is adjusted to
  1038. the length of the list of parameters,
  1039. unless the function is a variadic or <em>vararg function</em>,
  1040. which is
  1041. indicated by three dots ('<code>...</code>') at the end of its parameter list.
  1042. A vararg function does not adjust its argument list;
  1043. instead, it collects all extra arguments and supplies them
  1044. to the function through a <em>vararg expression</em>,
  1045. which is also written as three dots.
  1046. The value of this expression is a list of all actual extra arguments,
  1047. similar to a function with multiple results.
  1048. If a vararg expression is used inside another expression
  1049. or in the middle of a list of expressions,
  1050. then its return list is adjusted to one element.
  1051. If the expression is used as the last element of a list of expressions,
  1052. then no adjustment is made
  1053. (unless that last expression is enclosed in parentheses).
  1054. <p>
  1055. As an example, consider the following definitions:
  1056. <pre>
  1057. function f(a, b) end
  1058. function g(a, b, ...) end
  1059. function r() return 1,2,3 end
  1060. </pre><p>
  1061. Then, we have the following mapping from arguments to parameters and
  1062. to the vararg expression:
  1063. <pre>
  1064. CALL PARAMETERS
  1065. f(3) a=3, b=nil
  1066. f(3, 4) a=3, b=4
  1067. f(3, 4, 5) a=3, b=4
  1068. f(r(), 10) a=1, b=10
  1069. f(r()) a=1, b=2
  1070. g(3) a=3, b=nil, ... --&gt; (nothing)
  1071. g(3, 4) a=3, b=4, ... --&gt; (nothing)
  1072. g(3, 4, 5, 8) a=3, b=4, ... --&gt; 5 8
  1073. g(5, r()) a=5, b=1, ... --&gt; 2 3
  1074. </pre>
  1075. <p>
  1076. Results are returned using the <b>return</b> statement (see <a href="#2.4.4">&sect;2.4.4</a>).
  1077. If control reaches the end of a function
  1078. without encountering a <b>return</b> statement,
  1079. then the function returns with no results.
  1080. <p>
  1081. The <em>colon</em> syntax
  1082. is used for defining <em>methods</em>,
  1083. that is, functions that have an implicit extra parameter <code>self</code>.
  1084. Thus, the statement
  1085. <pre>
  1086. function t.a.b.c:f (<em>params</em>) <em>body</em> end
  1087. </pre><p>
  1088. is syntactic sugar for
  1089. <pre>
  1090. t.a.b.c.f = function (self, <em>params</em>) <em>body</em> end
  1091. </pre>
  1092. <h2>2.6 - <a name="2.6">Visibility Rules</a></h2>
  1093. <p>
  1094. Lua is a lexically scoped language.
  1095. The scope of variables begins at the first statement <em>after</em>
  1096. their declaration and lasts until the end of the innermost block that
  1097. includes the declaration.
  1098. Consider the following example:
  1099. <pre>
  1100. x = 10 -- global variable
  1101. do -- new block
  1102. local x = x -- new 'x', with value 10
  1103. print(x) --&gt; 10
  1104. x = x+1
  1105. do -- another block
  1106. local x = x+1 -- another 'x'
  1107. print(x) --&gt; 12
  1108. end
  1109. print(x) --&gt; 11
  1110. end
  1111. print(x) --&gt; 10 (the global one)
  1112. </pre>
  1113. <p>
  1114. Notice that, in a declaration like <code>local x = x</code>,
  1115. the new <code>x</code> being declared is not in scope yet,
  1116. and so the second <code>x</code> refers to the outside variable.
  1117. <p>
  1118. Because of the lexical scoping rules,
  1119. local variables can be freely accessed by functions
  1120. defined inside their scope.
  1121. A local variable used by an inner function is called
  1122. an <em>upvalue</em>, or <em>external local variable</em>,
  1123. inside the inner function.
  1124. <p>
  1125. Notice that each execution of a <b>local</b> statement
  1126. defines new local variables.
  1127. Consider the following example:
  1128. <pre>
  1129. a = {}
  1130. local x = 20
  1131. for i=1,10 do
  1132. local y = 0
  1133. a[i] = function () y=y+1; return x+y end
  1134. end
  1135. </pre><p>
  1136. The loop creates ten closures
  1137. (that is, ten instances of the anonymous function).
  1138. Each of these closures uses a different <code>y</code> variable,
  1139. while all of them share the same <code>x</code>.
  1140. <h2>2.7 - <a name="2.7">Error Handling</a></h2>
  1141. <p>
  1142. Because Lua is an embedded extension language,
  1143. all Lua actions start from C&nbsp;code in the host program
  1144. calling a function from the Lua library (see <a href="#lua_pcall"><code>lua_pcall</code></a>).
  1145. Whenever an error occurs during Lua compilation or execution,
  1146. control returns to C,
  1147. which can take appropriate measures
  1148. (such as printing an error message).
  1149. <p>
  1150. Lua code can explicitly generate an error by calling the
  1151. <a href="#pdf-error"><code>error</code></a> function.
  1152. If you need to catch errors in Lua,
  1153. you can use the <a href="#pdf-pcall"><code>pcall</code></a> function.
  1154. <h2>2.8 - <a name="2.8">Metatables</a></h2>
  1155. <p>
  1156. Every value in Lua can have a <em>metatable</em>.
  1157. This <em>metatable</em> is an ordinary Lua table
  1158. that defines the behavior of the original value
  1159. under certain special operations.
  1160. You can change several aspects of the behavior
  1161. of operations over a value by setting specific fields in its metatable.
  1162. For instance, when a non-numeric value is the operand of an addition,
  1163. Lua checks for a function in the field <code>"__add"</code> in its metatable.
  1164. If it finds one,
  1165. Lua calls this function to perform the addition.
  1166. <p>
  1167. We call the keys in a metatable <em>events</em>
  1168. and the values <em>metamethods</em>.
  1169. In the previous example, the event is <code>"add"</code>
  1170. and the metamethod is the function that performs the addition.
  1171. <p>
  1172. You can query the metatable of any value
  1173. through the <a href="#pdf-getmetatable"><code>getmetatable</code></a> function.
  1174. <p>
  1175. You can replace the metatable of tables
  1176. through the <a href="#pdf-setmetatable"><code>setmetatable</code></a>
  1177. function.
  1178. You cannot change the metatable of other types from Lua
  1179. (except by using the debug library);
  1180. you must use the C&nbsp;API for that.
  1181. <p>
  1182. Tables and full userdata have individual metatables
  1183. (although multiple tables and userdata can share their metatables).
  1184. Values of all other types share one single metatable per type;
  1185. that is, there is one single metatable for all numbers,
  1186. one for all strings, etc.
  1187. <p>
  1188. A metatable controls how an object behaves in arithmetic operations,
  1189. order comparisons, concatenation, length operation, and indexing.
  1190. A metatable also can define a function to be called when a userdata
  1191. is garbage collected.
  1192. For each of these operations Lua associates a specific key
  1193. called an <em>event</em>.
  1194. When Lua performs one of these operations over a value,
  1195. it checks whether this value has a metatable with the corresponding event.
  1196. If so, the value associated with that key (the metamethod)
  1197. controls how Lua will perform the operation.
  1198. <p>
  1199. Metatables control the operations listed next.
  1200. Each operation is identified by its corresponding name.
  1201. The key for each operation is a string with its name prefixed by
  1202. two underscores, '<code>__</code>';
  1203. for instance, the key for operation "add" is the
  1204. string <code>"__add"</code>.
  1205. The semantics of these operations is better explained by a Lua function
  1206. describing how the interpreter executes the operation.
  1207. <p>
  1208. The code shown here in Lua is only illustrative;
  1209. the real behavior is hard coded in the interpreter
  1210. and it is much more efficient than this simulation.
  1211. All functions used in these descriptions
  1212. (<a href="#pdf-rawget"><code>rawget</code></a>, <a href="#pdf-tonumber"><code>tonumber</code></a>, etc.)
  1213. are described in <a href="#5.1">&sect;5.1</a>.
  1214. In particular, to retrieve the metamethod of a given object,
  1215. we use the expression
  1216. <pre>
  1217. metatable(obj)[event]
  1218. </pre><p>
  1219. This should be read as
  1220. <pre>
  1221. rawget(getmetatable(obj) or {}, event)
  1222. </pre><p>
  1223. That is, the access to a metamethod does not invoke other metamethods,
  1224. and the access to objects with no metatables does not fail
  1225. (it simply results in <b>nil</b>).
  1226. <ul>
  1227. <li><b>"add":</b>
  1228. the <code>+</code> operation.
  1229. <p>
  1230. The function <code>getbinhandler</code> below defines how Lua chooses a handler
  1231. for a binary operation.
  1232. First, Lua tries the first operand.
  1233. If its type does not define a handler for the operation,
  1234. then Lua tries the second operand.
  1235. <pre>
  1236. function getbinhandler (op1, op2, event)
  1237. return metatable(op1)[event] or metatable(op2)[event]
  1238. end
  1239. </pre><p>
  1240. By using this function,
  1241. the behavior of the <code>op1 + op2</code> is
  1242. <pre>
  1243. function add_event (op1, op2)
  1244. local o1, o2 = tonumber(op1), tonumber(op2)
  1245. if o1 and o2 then -- both operands are numeric?
  1246. return o1 + o2 -- '+' here is the primitive 'add'
  1247. else -- at least one of the operands is not numeric
  1248. local h = getbinhandler(op1, op2, "__add")
  1249. if h then
  1250. -- call the handler with both operands
  1251. return (h(op1, op2))
  1252. else -- no handler available: default behavior
  1253. error(&middot;&middot;&middot;)
  1254. end
  1255. end
  1256. end
  1257. </pre><p>
  1258. </li>
  1259. <li><b>"sub":</b>
  1260. the <code>-</code> operation.
  1261. Behavior similar to the "add" operation.
  1262. </li>
  1263. <li><b>"mul":</b>
  1264. the <code>*</code> operation.
  1265. Behavior similar to the "add" operation.
  1266. </li>
  1267. <li><b>"div":</b>
  1268. the <code>/</code> operation.
  1269. Behavior similar to the "add" operation.
  1270. </li>
  1271. <li><b>"mod":</b>
  1272. the <code>%</code> operation.
  1273. Behavior similar to the "add" operation,
  1274. with the operation
  1275. <code>o1 - floor(o1/o2)*o2</code> as the primitive operation.
  1276. </li>
  1277. <li><b>"pow":</b>
  1278. the <code>^</code> (exponentiation) operation.
  1279. Behavior similar to the "add" operation,
  1280. with the function <code>pow</code> (from the C&nbsp;math library)
  1281. as the primitive operation.
  1282. </li>
  1283. <li><b>"unm":</b>
  1284. the unary <code>-</code> operation.
  1285. <pre>
  1286. function unm_event (op)
  1287. local o = tonumber(op)
  1288. if o then -- operand is numeric?
  1289. return -o -- '-' here is the primitive 'unm'
  1290. else -- the operand is not numeric.
  1291. -- Try to get a handler from the operand
  1292. local h = metatable(op).__unm
  1293. if h then
  1294. -- call the handler with the operand
  1295. return (h(op))
  1296. else -- no handler available: default behavior
  1297. error(&middot;&middot;&middot;)
  1298. end
  1299. end
  1300. end
  1301. </pre><p>
  1302. </li>
  1303. <li><b>"concat":</b>
  1304. the <code>..</code> (concatenation) operation.
  1305. <pre>
  1306. function concat_event (op1, op2)
  1307. if (type(op1) == "string" or type(op1) == "number") and
  1308. (type(op2) == "string" or type(op2) == "number") then
  1309. return op1 .. op2 -- primitive string concatenation
  1310. else
  1311. local h = getbinhandler(op1, op2, "__concat")
  1312. if h then
  1313. return (h(op1, op2))
  1314. else
  1315. error(&middot;&middot;&middot;)
  1316. end
  1317. end
  1318. end
  1319. </pre><p>
  1320. </li>
  1321. <li><b>"len":</b>
  1322. the <code>#</code> operation.
  1323. <pre>
  1324. function len_event (op)
  1325. if type(op) == "string" then
  1326. return strlen(op) -- primitive string length
  1327. elseif type(op) == "table" then
  1328. return #op -- primitive table length
  1329. else
  1330. local h = metatable(op).__len
  1331. if h then
  1332. -- call the handler with the operand
  1333. return (h(op))
  1334. else -- no handler available: default behavior
  1335. error(&middot;&middot;&middot;)
  1336. end
  1337. end
  1338. end
  1339. </pre><p>
  1340. See <a href="#2.5.5">&sect;2.5.5</a> for a description of the length of a table.
  1341. </li>
  1342. <li><b>"eq":</b>
  1343. the <code>==</code> operation.
  1344. The function <code>getcomphandler</code> defines how Lua chooses a metamethod
  1345. for comparison operators.
  1346. A metamethod only is selected when both objects
  1347. being compared have the same type
  1348. and the same metamethod for the selected operation.
  1349. <pre>
  1350. function getcomphandler (op1, op2, event)
  1351. if type(op1) ~= type(op2) then return nil end
  1352. local mm1 = metatable(op1)[event]
  1353. local mm2 = metatable(op2)[event]
  1354. if mm1 == mm2 then return mm1 else return nil end
  1355. end
  1356. </pre><p>
  1357. The "eq" event is defined as follows:
  1358. <pre>
  1359. function eq_event (op1, op2)
  1360. if type(op1) ~= type(op2) then -- different types?
  1361. return false -- different objects
  1362. end
  1363. if op1 == op2 then -- primitive equal?
  1364. return true -- objects are equal
  1365. end
  1366. -- try metamethod
  1367. local h = getcomphandler(op1, op2, "__eq")
  1368. if h then
  1369. return (h(op1, op2))
  1370. else
  1371. return false
  1372. end
  1373. end
  1374. </pre><p>
  1375. <code>a ~= b</code> is equivalent to <code>not (a == b)</code>.
  1376. </li>
  1377. <li><b>"lt":</b>
  1378. the <code>&lt;</code> operation.
  1379. <pre>
  1380. function lt_event (op1, op2)
  1381. if type(op1) == "number" and type(op2) == "number" then
  1382. return op1 &lt; op2 -- numeric comparison
  1383. elseif type(op1) == "string" and type(op2) == "string" then
  1384. return op1 &lt; op2 -- lexicographic comparison
  1385. else
  1386. local h = getcomphandler(op1, op2, "__lt")
  1387. if h then
  1388. return (h(op1, op2))
  1389. else
  1390. error(&middot;&middot;&middot;)
  1391. end
  1392. end
  1393. end
  1394. </pre><p>
  1395. <code>a &gt; b</code> is equivalent to <code>b &lt; a</code>.
  1396. </li>
  1397. <li><b>"le":</b>
  1398. the <code>&lt;=</code> operation.
  1399. <pre>
  1400. function le_event (op1, op2)
  1401. if type(op1) == "number" and type(op2) == "number" then
  1402. return op1 &lt;= op2 -- numeric comparison
  1403. elseif type(op1) == "string" and type(op2) == "string" then
  1404. return op1 &lt;= op2 -- lexicographic comparison
  1405. else
  1406. local h = getcomphandler(op1, op2, "__le")
  1407. if h then
  1408. return (h(op1, op2))
  1409. else
  1410. h = getcomphandler(op1, op2, "__lt")
  1411. if h then
  1412. return not h(op2, op1)
  1413. else
  1414. error(&middot;&middot;&middot;)
  1415. end
  1416. end
  1417. end
  1418. end
  1419. </pre><p>
  1420. <code>a &gt;= b</code> is equivalent to <code>b &lt;= a</code>.
  1421. Note that, in the absence of a "le" metamethod,
  1422. Lua tries the "lt", assuming that <code>a &lt;= b</code> is
  1423. equivalent to <code>not (b &lt; a)</code>.
  1424. </li>
  1425. <li><b>"index":</b>
  1426. The indexing access <code>table[key]</code>.
  1427. <pre>
  1428. function gettable_event (table, key)
  1429. local h
  1430. if type(table) == "table" then
  1431. local v = rawget(table, key)
  1432. if v ~= nil then return v end
  1433. h = metatable(table).__index
  1434. if h == nil then return nil end
  1435. else
  1436. h = metatable(table).__index
  1437. if h == nil then
  1438. error(&middot;&middot;&middot;)
  1439. end
  1440. end
  1441. if type(h) == "function" then
  1442. return (h(table, key)) -- call the handler
  1443. else return h[key] -- or repeat operation on it
  1444. end
  1445. end
  1446. </pre><p>
  1447. </li>
  1448. <li><b>"newindex":</b>
  1449. The indexing assignment <code>table[key] = value</code>.
  1450. <pre>
  1451. function settable_event (table, key, value)
  1452. local h
  1453. if type(table) == "table" then
  1454. local v = rawget(table, key)
  1455. if v ~= nil then rawset(table, key, value); return end
  1456. h = metatable(table).__newindex
  1457. if h == nil then rawset(table, key, value); return end
  1458. else
  1459. h = metatable(table).__newindex
  1460. if h == nil then
  1461. error(&middot;&middot;&middot;)
  1462. end
  1463. end
  1464. if type(h) == "function" then
  1465. h(table, key,value) -- call the handler
  1466. else h[key] = value -- or repeat operation on it
  1467. end
  1468. end
  1469. </pre><p>
  1470. </li>
  1471. <li><b>"call":</b>
  1472. called when Lua calls a value.
  1473. <pre>
  1474. function function_event (func, ...)
  1475. if type(func) == "function" then
  1476. return func(...) -- primitive call
  1477. else
  1478. local h = metatable(func).__call
  1479. if h then
  1480. return h(func, ...)
  1481. else
  1482. error(&middot;&middot;&middot;)
  1483. end
  1484. end
  1485. end
  1486. </pre><p>
  1487. </li>
  1488. </ul>
  1489. <h2>2.9 - <a name="2.9">Environments</a></h2>
  1490. <p>
  1491. Besides metatables,
  1492. objects of types thread, function, and userdata
  1493. have another table associated with them,
  1494. called their <em>environment</em>.
  1495. Like metatables, environments are regular tables and
  1496. multiple objects can share the same environment.
  1497. <p>
  1498. Threads are created sharing the environment of the creating thread.
  1499. Userdata and C&nbsp;functions are created sharing the environment
  1500. of the creating C&nbsp;function.
  1501. Non-nested Lua functions
  1502. (created by <a href="#pdf-loadfile"><code>loadfile</code></a>, <a href="#pdf-loadstring"><code>loadstring</code></a> or <a href="#pdf-load"><code>load</code></a>)
  1503. are created sharing the environment of the creating thread.
  1504. Nested Lua functions are created sharing the environment of
  1505. the creating Lua function.
  1506. <p>
  1507. Environments associated with userdata have no meaning for Lua.
  1508. It is only a convenience feature for programmers to associate a table to
  1509. a userdata.
  1510. <p>
  1511. Environments associated with threads are called
  1512. <em>global environments</em>.
  1513. They are used as the default environment for threads and
  1514. non-nested Lua functions created by the thread
  1515. and can be directly accessed by C&nbsp;code (see <a href="#3.3">&sect;3.3</a>).
  1516. <p>
  1517. The environment associated with a C&nbsp;function can be directly
  1518. accessed by C&nbsp;code (see <a href="#3.3">&sect;3.3</a>).
  1519. It is used as the default environment for other C&nbsp;functions
  1520. and userdata created by the function.
  1521. <p>
  1522. Environments associated with Lua functions are used to resolve
  1523. all accesses to global variables within the function (see <a href="#2.3">&sect;2.3</a>).
  1524. They are used as the default environment for nested Lua functions
  1525. created by the function.
  1526. <p>
  1527. You can change the environment of a Lua function or the
  1528. running thread by calling <a href="#pdf-setfenv"><code>setfenv</code></a>.
  1529. You can get the environment of a Lua function or the running thread
  1530. by calling <a href="#pdf-getfenv"><code>getfenv</code></a>.
  1531. To manipulate the environment of other objects
  1532. (userdata, C&nbsp;functions, other threads) you must
  1533. use the C&nbsp;API.
  1534. <h2>2.10 - <a name="2.10">Garbage Collection</a></h2>
  1535. <p>
  1536. Lua performs automatic memory management.
  1537. This means that
  1538. you have to worry neither about allocating memory for new objects
  1539. nor about freeing it when the objects are no longer needed.
  1540. Lua manages memory automatically by running
  1541. a <em>garbage collector</em> from time to time
  1542. to collect all <em>dead objects</em>
  1543. (that is, objects that are no longer accessible from Lua).
  1544. All memory used by Lua is subject to automatic management:
  1545. tables, userdata, functions, threads, strings, etc.
  1546. <p>
  1547. Lua implements an incremental mark-and-sweep collector.
  1548. It uses two numbers to control its garbage-collection cycles:
  1549. the <em>garbage-collector pause</em> and
  1550. the <em>garbage-collector step multiplier</em>.
  1551. Both use percentage points as units
  1552. (so that a value of 100 means an internal value of 1).
  1553. <p>
  1554. The garbage-collector pause
  1555. controls how long the collector waits before starting a new cycle.
  1556. Larger values make the collector less aggressive.
  1557. Values smaller than 100 mean the collector will not wait to
  1558. start a new cycle.
  1559. A value of 200 means that the collector waits for the total memory in use
  1560. to double before starting a new cycle.
  1561. <p>
  1562. The step multiplier
  1563. controls the relative speed of the collector relative to
  1564. memory allocation.
  1565. Larger values make the collector more aggressive but also increase
  1566. the size of each incremental step.
  1567. Values smaller than 100 make the collector too slow and
  1568. can result in the collector never finishing a cycle.
  1569. The default, 200, means that the collector runs at "twice"
  1570. the speed of memory allocation.
  1571. <p>
  1572. You can change these numbers by calling <a href="#lua_gc"><code>lua_gc</code></a> in C
  1573. or <a href="#pdf-collectgarbage"><code>collectgarbage</code></a> in Lua.
  1574. With these functions you can also control
  1575. the collector directly (e.g., stop and restart it).
  1576. <h3>2.10.1 - <a name="2.10.1">Garbage-Collection Metamethods</a></h3>
  1577. <p>
  1578. Using the C&nbsp;API,
  1579. you can set garbage-collector metamethods for userdata (see <a href="#2.8">&sect;2.8</a>).
  1580. These metamethods are also called <em>finalizers</em>.
  1581. Finalizers allow you to coordinate Lua's garbage collection
  1582. with external resource management
  1583. (such as closing files, network or database connections,
  1584. or freeing your own memory).
  1585. <p>
  1586. Garbage userdata with a field <code>__gc</code> in their metatables are not
  1587. collected immediately by the garbage collector.
  1588. Instead, Lua puts them in a list.
  1589. After the collection,
  1590. Lua does the equivalent of the following function
  1591. for each userdata in that list:
  1592. <pre>
  1593. function gc_event (udata)
  1594. local h = metatable(udata).__gc
  1595. if h then
  1596. h(udata)
  1597. end
  1598. end
  1599. </pre>
  1600. <p>
  1601. At the end of each garbage-collection cycle,
  1602. the finalizers for userdata are called in <em>reverse</em>
  1603. order of their creation,
  1604. among those collected in that cycle.
  1605. That is, the first finalizer to be called is the one associated
  1606. with the userdata created last in the program.
  1607. The userdata itself is freed only in the next garbage-collection cycle.
  1608. <h3>2.10.2 - <a name="2.10.2">Weak Tables</a></h3>
  1609. <p>
  1610. A <em>weak table</em> is a table whose elements are
  1611. <em>weak references</em>.
  1612. A weak reference is ignored by the garbage collector.
  1613. In other words,
  1614. if the only references to an object are weak references,
  1615. then the garbage collector will collect this object.
  1616. <p>
  1617. A weak table can have weak keys, weak values, or both.
  1618. A table with weak keys allows the collection of its keys,
  1619. but prevents the collection of its values.
  1620. A table with both weak keys and weak values allows the collection of
  1621. both keys and values.
  1622. In any case, if either the key or the value is collected,
  1623. the whole pair is removed from the table.
  1624. The weakness of a table is controlled by the
  1625. <code>__mode</code> field of its metatable.
  1626. If the <code>__mode</code> field is a string containing the character&nbsp;'<code>k</code>',
  1627. the keys in the table are weak.
  1628. If <code>__mode</code> contains '<code>v</code>',
  1629. the values in the table are weak.
  1630. <p>
  1631. After you use a table as a metatable,
  1632. you should not change the value of its <code>__mode</code> field.
  1633. Otherwise, the weak behavior of the tables controlled by this
  1634. metatable is undefined.
  1635. <h2>2.11 - <a name="2.11">Coroutines</a></h2>
  1636. <p>
  1637. Lua supports coroutines,
  1638. also called <em>collaborative multithreading</em>.
  1639. A coroutine in Lua represents an independent thread of execution.
  1640. Unlike threads in multithread systems, however,
  1641. a coroutine only suspends its execution by explicitly calling
  1642. a yield function.
  1643. <p>
  1644. You create a coroutine with a call to <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>.
  1645. Its sole argument is a function
  1646. that is the main function of the coroutine.
  1647. The <code>create</code> function only creates a new coroutine and
  1648. returns a handle to it (an object of type <em>thread</em>);
  1649. it does not start the coroutine execution.
  1650. <p>
  1651. When you first call <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>,
  1652. passing as its first argument
  1653. a thread returned by <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>,
  1654. the coroutine starts its execution,
  1655. at the first line of its main function.
  1656. Extra arguments passed to <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> are passed on
  1657. to the coroutine main function.
  1658. After the coroutine starts running,
  1659. it runs until it terminates or <em>yields</em>.
  1660. <p>
  1661. A coroutine can terminate its execution in two ways:
  1662. normally, when its main function returns
  1663. (explicitly or implicitly, after the last instruction);
  1664. and abnormally, if there is an unprotected error.
  1665. In the first case, <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> returns <b>true</b>,
  1666. plus any values returned by the coroutine main function.
  1667. In case of errors, <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> returns <b>false</b>
  1668. plus an error message.
  1669. <p>
  1670. A coroutine yields by calling <a href="#pdf-coroutine.yield"><code>coroutine.yield</code></a>.
  1671. When a coroutine yields,
  1672. the corresponding <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> returns immediately,
  1673. even if the yield happens inside nested function calls
  1674. (that is, not in the main function,
  1675. but in a function directly or indirectly called by the main function).
  1676. In the case of a yield, <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> also returns <b>true</b>,
  1677. plus any values passed to <a href="#pdf-coroutine.yield"><code>coroutine.yield</code></a>.
  1678. The next time you resume the same coroutine,
  1679. it continues its execution from the point where it yielded,
  1680. with the call to <a href="#pdf-coroutine.yield"><code>coroutine.yield</code></a> returning any extra
  1681. arguments passed to <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>.
  1682. <p>
  1683. Like <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>,
  1684. the <a href="#pdf-coroutine.wrap"><code>coroutine.wrap</code></a> function also creates a coroutine,
  1685. but instead of returning the coroutine itself,
  1686. it returns a function that, when called, resumes the coroutine.
  1687. Any arguments passed to this function
  1688. go as extra arguments to <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>.
  1689. <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>,
  1690. except the first one (the boolean error code).
  1691. Unlike <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>,
  1692. <a href="#pdf-coroutine.wrap"><code>coroutine.wrap</code></a> does not catch errors;
  1693. any error is propagated to the caller.
  1694. <p>
  1695. As an example,
  1696. consider the following code:
  1697. <pre>
  1698. function foo (a)
  1699. print("foo", a)
  1700. return coroutine.yield(2*a)
  1701. end
  1702. co = coroutine.create(function (a,b)
  1703. print("co-body", a, b)
  1704. local r = foo(a+1)
  1705. print("co-body", r)
  1706. local r, s = coroutine.yield(a+b, a-b)
  1707. print("co-body", r, s)
  1708. return b, "end"
  1709. end)
  1710. print("main", coroutine.resume(co, 1, 10))
  1711. print("main", coroutine.resume(co, "r"))
  1712. print("main", coroutine.resume(co, "x", "y"))
  1713. print("main", coroutine.resume(co, "x", "y"))
  1714. </pre><p>
  1715. When you run it, it produces the following output:
  1716. <pre>
  1717. co-body 1 10
  1718. foo 2
  1719. main true 4
  1720. co-body r
  1721. main true 11 -9
  1722. co-body x y
  1723. main true 10 end
  1724. main false cannot resume dead coroutine
  1725. </pre>
  1726. <h1>3 - <a name="3">The Application Program Interface</a></h1>
  1727. <p>
  1728. This section describes the C&nbsp;API for Lua, that is,
  1729. the set of C&nbsp;functions available to the host program to communicate
  1730. with Lua.
  1731. All API functions and related types and constants
  1732. are declared in the header file <a name="pdf-lua.h"><code>lua.h</code></a>.
  1733. <p>
  1734. Even when we use the term "function",
  1735. any facility in the API may be provided as a macro instead.
  1736. All such macros use each of their arguments exactly once
  1737. (except for the first argument, which is always a Lua state),
  1738. and so do not generate any hidden side-effects.
  1739. <p>
  1740. As in most C&nbsp;libraries,
  1741. the Lua API functions do not check their arguments for validity or consistency.
  1742. However, you can change this behavior by compiling Lua
  1743. with a proper definition for the macro <a name="pdf-luai_apicheck"><code>luai_apicheck</code></a>,
  1744. in file <code>luaconf.h</code>.
  1745. <h2>3.1 - <a name="3.1">The Stack</a></h2>
  1746. <p>
  1747. Lua uses a <em>virtual stack</em> to pass values to and from C.
  1748. Each element in this stack represents a Lua value
  1749. (<b>nil</b>, number, string, etc.).
  1750. <p>
  1751. Whenever Lua calls C, the called function gets a new stack,
  1752. which is independent of previous stacks and of stacks of
  1753. C&nbsp;functions that are still active.
  1754. This stack initially contains any arguments to the C&nbsp;function
  1755. and it is where the C&nbsp;function pushes its results
  1756. to be returned to the caller (see <a href="#lua_CFunction"><code>lua_CFunction</code></a>).
  1757. <p>
  1758. For convenience,
  1759. most query operations in the API do not follow a strict stack discipline.
  1760. Instead, they can refer to any element in the stack
  1761. by using an <em>index</em>:
  1762. A positive index represents an <em>absolute</em> stack position
  1763. (starting at&nbsp;1);
  1764. a negative index represents an <em>offset</em> relative to the top of the stack.
  1765. More specifically, if the stack has <em>n</em> elements,
  1766. then index&nbsp;1 represents the first element
  1767. (that is, the element that was pushed onto the stack first)
  1768. and
  1769. index&nbsp;<em>n</em> represents the last element;
  1770. index&nbsp;-1 also represents the last element
  1771. (that is, the element at the&nbsp;top)
  1772. and index <em>-n</em> represents the first element.
  1773. We say that an index is <em>valid</em>
  1774. if it lies between&nbsp;1 and the stack top
  1775. (that is, if <code>1 &le; abs(index) &le; top</code>).
  1776. <h2>3.2 - <a name="3.2">Stack Size</a></h2>
  1777. <p>
  1778. When you interact with Lua API,
  1779. you are responsible for ensuring consistency.
  1780. In particular,
  1781. <em>you are responsible for controlling stack overflow</em>.
  1782. You can use the function <a href="#lua_checkstack"><code>lua_checkstack</code></a>
  1783. to grow the stack size.
  1784. <p>
  1785. Whenever Lua calls C,
  1786. it ensures that at least <a name="pdf-LUA_MINSTACK"><code>LUA_MINSTACK</code></a> stack positions are available.
  1787. <code>LUA_MINSTACK</code> is defined as 20,
  1788. so that usually you do not have to worry about stack space
  1789. unless your code has loops pushing elements onto the stack.
  1790. <p>
  1791. Most query functions accept as indices any value inside the
  1792. available stack space, that is, indices up to the maximum stack size
  1793. you have set through <a href="#lua_checkstack"><code>lua_checkstack</code></a>.
  1794. Such indices are called <em>acceptable indices</em>.
  1795. More formally, we define an <em>acceptable index</em>
  1796. as follows:
  1797. <pre>
  1798. (index &lt; 0 &amp;&amp; abs(index) &lt;= top) ||
  1799. (index &gt; 0 &amp;&amp; index &lt;= stackspace)
  1800. </pre><p>
  1801. Note that 0 is never an acceptable index.
  1802. <h2>3.3 - <a name="3.3">Pseudo-Indices</a></h2>
  1803. <p>
  1804. Unless otherwise noted,
  1805. any function that accepts valid indices can also be called with
  1806. <em>pseudo-indices</em>,
  1807. which represent some Lua values that are accessible to C&nbsp;code
  1808. but which are not in the stack.
  1809. Pseudo-indices are used to access the thread environment,
  1810. the function environment,
  1811. the registry,
  1812. and the upvalues of a C&nbsp;function (see <a href="#3.4">&sect;3.4</a>).
  1813. <p>
  1814. The thread environment (where global variables live) is
  1815. always at pseudo-index <a name="pdf-LUA_GLOBALSINDEX"><code>LUA_GLOBALSINDEX</code></a>.
  1816. The environment of the running C&nbsp;function is always
  1817. at pseudo-index <a name="pdf-LUA_ENVIRONINDEX"><code>LUA_ENVIRONINDEX</code></a>.
  1818. <p>
  1819. To access and change the value of global variables,
  1820. you can use regular table operations over an environment table.
  1821. For instance, to access the value of a global variable, do
  1822. <pre>
  1823. lua_getfield(L, LUA_GLOBALSINDEX, varname);
  1824. </pre>
  1825. <h2>3.4 - <a name="3.4">C Closures</a></h2>
  1826. <p>
  1827. When a C&nbsp;function is created,
  1828. it is possible to associate some values with it,
  1829. thus creating a <em>C&nbsp;closure</em>;
  1830. these values are called <em>upvalues</em> and are
  1831. accessible to the function whenever it is called
  1832. (see <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a>).
  1833. <p>
  1834. Whenever a C&nbsp;function is called,
  1835. its upvalues are located at specific pseudo-indices.
  1836. These pseudo-indices are produced by the macro
  1837. <a name="lua_upvalueindex"><code>lua_upvalueindex</code></a>.
  1838. The first value associated with a function is at position
  1839. <code>lua_upvalueindex(1)</code>, and so on.
  1840. Any access to <code>lua_upvalueindex(<em>n</em>)</code>,
  1841. where <em>n</em> is greater than the number of upvalues of the
  1842. current function (but not greater than 256),
  1843. produces an acceptable (but invalid) index.
  1844. <h2>3.5 - <a name="3.5">Registry</a></h2>
  1845. <p>
  1846. Lua provides a <em>registry</em>,
  1847. a pre-defined table that can be used by any C&nbsp;code to
  1848. store whatever Lua value it needs to store.
  1849. This table is always located at pseudo-index
  1850. <a name="pdf-LUA_REGISTRYINDEX"><code>LUA_REGISTRYINDEX</code></a>.
  1851. Any C&nbsp;library can store data into this table,
  1852. but it should take care to choose keys different from those used
  1853. by other libraries, to avoid collisions.
  1854. Typically, you should use as key a string containing your library name
  1855. or a light userdata with the address of a C&nbsp;object in your code.
  1856. <p>
  1857. The integer keys in the registry are used by the reference mechanism,
  1858. implemented by the auxiliary library,
  1859. and therefore should not be used for other purposes.
  1860. <h2>3.6 - <a name="3.6">Error Handling in C</a></h2>
  1861. <p>
  1862. Internally, Lua uses the C <code>longjmp</code> facility to handle errors.
  1863. (You can also choose to use exceptions if you use C++;
  1864. see file <code>luaconf.h</code>.)
  1865. When Lua faces any error
  1866. (such as memory allocation errors, type errors, syntax errors,
  1867. and runtime errors)
  1868. it <em>raises</em> an error;
  1869. that is, it does a long jump.
  1870. A <em>protected environment</em> uses <code>setjmp</code>
  1871. to set a recover point;
  1872. any error jumps to the most recent active recover point.
  1873. <p>
  1874. Most functions in the API can throw an error,
  1875. for instance due to a memory allocation error.
  1876. The documentation for each function indicates whether
  1877. it can throw errors.
  1878. <p>
  1879. Inside a C&nbsp;function you can throw an error by calling <a href="#lua_error"><code>lua_error</code></a>.
  1880. <h2>3.7 - <a name="3.7">Functions and Types</a></h2>
  1881. <p>
  1882. Here we list all functions and types from the C&nbsp;API in
  1883. alphabetical order.
  1884. Each function has an indicator like this:
  1885. <span class="apii">[-o, +p, <em>x</em>]</span>
  1886. <p>
  1887. The first field, <code>o</code>,
  1888. is how many elements the function pops from the stack.
  1889. The second field, <code>p</code>,
  1890. is how many elements the function pushes onto the stack.
  1891. (Any function always pushes its results after popping its arguments.)
  1892. A field in the form <code>x|y</code> means the function can push (or pop)
  1893. <code>x</code> or <code>y</code> elements,
  1894. depending on the situation;
  1895. an interrogation mark '<code>?</code>' means that
  1896. we cannot know how many elements the function pops/pushes
  1897. by looking only at its arguments
  1898. (e.g., they may depend on what is on the stack).
  1899. The third field, <code>x</code>,
  1900. tells whether the function may throw errors:
  1901. '<code>-</code>' means the function never throws any error;
  1902. '<code>m</code>' means the function may throw an error
  1903. only due to not enough memory;
  1904. '<code>e</code>' means the function may throw other kinds of errors;
  1905. '<code>v</code>' means the function may throw an error on purpose.
  1906. <hr><h3><a name="lua_Alloc"><code>lua_Alloc</code></a></h3>
  1907. <pre>typedef void * (*lua_Alloc) (void *ud,
  1908. void *ptr,
  1909. size_t osize,
  1910. size_t nsize);</pre>
  1911. <p>
  1912. The type of the memory-allocation function used by Lua states.
  1913. The allocator function must provide a
  1914. functionality similar to <code>realloc</code>,
  1915. but not exactly the same.
  1916. Its arguments are
  1917. <code>ud</code>, an opaque pointer passed to <a href="#lua_newstate"><code>lua_newstate</code></a>;
  1918. <code>ptr</code>, a pointer to the block being allocated/reallocated/freed;
  1919. <code>osize</code>, the original size of the block;
  1920. <code>nsize</code>, the new size of the block.
  1921. <code>ptr</code> is <code>NULL</code> if and only if <code>osize</code> is zero.
  1922. When <code>nsize</code> is zero, the allocator must return <code>NULL</code>;
  1923. if <code>osize</code> is not zero,
  1924. it should free the block pointed to by <code>ptr</code>.
  1925. When <code>nsize</code> is not zero, the allocator returns <code>NULL</code>
  1926. if and only if it cannot fill the request.
  1927. When <code>nsize</code> is not zero and <code>osize</code> is zero,
  1928. the allocator should behave like <code>malloc</code>.
  1929. When <code>nsize</code> and <code>osize</code> are not zero,
  1930. the allocator behaves like <code>realloc</code>.
  1931. Lua assumes that the allocator never fails when
  1932. <code>osize &gt;= nsize</code>.
  1933. <p>
  1934. Here is a simple implementation for the allocator function.
  1935. It is used in the auxiliary library by <a href="#luaL_newstate"><code>luaL_newstate</code></a>.
  1936. <pre>
  1937. static void *l_alloc (void *ud, void *ptr, size_t osize,
  1938. size_t nsize) {
  1939. (void)ud; (void)osize; /* not used */
  1940. if (nsize == 0) {
  1941. free(ptr);
  1942. return NULL;
  1943. }
  1944. else
  1945. return realloc(ptr, nsize);
  1946. }
  1947. </pre><p>
  1948. This code assumes
  1949. that <code>free(NULL)</code> has no effect and that
  1950. <code>realloc(NULL, size)</code> is equivalent to <code>malloc(size)</code>.
  1951. ANSI&nbsp;C ensures both behaviors.
  1952. <hr><h3><a name="lua_atpanic"><code>lua_atpanic</code></a></h3><p>
  1953. <span class="apii">[-0, +0, <em>-</em>]</span>
  1954. <pre>lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf);</pre>
  1955. <p>
  1956. Sets a new panic function and returns the old one.
  1957. <p>
  1958. If an error happens outside any protected environment,
  1959. Lua calls a <em>panic function</em>
  1960. and then calls <code>exit(EXIT_FAILURE)</code>,
  1961. thus exiting the host application.
  1962. Your panic function can avoid this exit by
  1963. never returning (e.g., doing a long jump).
  1964. <p>
  1965. The panic function can access the error message at the top of the stack.
  1966. <hr><h3><a name="lua_call"><code>lua_call</code></a></h3><p>
  1967. <span class="apii">[-(nargs + 1), +nresults, <em>e</em>]</span>
  1968. <pre>void lua_call (lua_State *L, int nargs, int nresults);</pre>
  1969. <p>
  1970. Calls a function.
  1971. <p>
  1972. To call a function you must use the following protocol:
  1973. first, the function to be called is pushed onto the stack;
  1974. then, the arguments to the function are pushed
  1975. in direct order;
  1976. that is, the first argument is pushed first.
  1977. Finally you call <a href="#lua_call"><code>lua_call</code></a>;
  1978. <code>nargs</code> is the number of arguments that you pushed onto the stack.
  1979. All arguments and the function value are popped from the stack
  1980. when the function is called.
  1981. The function results are pushed onto the stack when the function returns.
  1982. The number of results is adjusted to <code>nresults</code>,
  1983. unless <code>nresults</code> is <a name="pdf-LUA_MULTRET"><code>LUA_MULTRET</code></a>.
  1984. In this case, <em>all</em> results from the function are pushed.
  1985. Lua takes care that the returned values fit into the stack space.
  1986. The function results are pushed onto the stack in direct order
  1987. (the first result is pushed first),
  1988. so that after the call the last result is on the top of the stack.
  1989. <p>
  1990. Any error inside the called function is propagated upwards
  1991. (with a <code>longjmp</code>).
  1992. <p>
  1993. The following example shows how the host program can do the
  1994. equivalent to this Lua code:
  1995. <pre>
  1996. a = f("how", t.x, 14)
  1997. </pre><p>
  1998. Here it is in&nbsp;C:
  1999. <pre>
  2000. lua_getfield(L, LUA_GLOBALSINDEX, "f"); /* function to be called */
  2001. lua_pushstring(L, "how"); /* 1st argument */
  2002. lua_getfield(L, LUA_GLOBALSINDEX, "t"); /* table to be indexed */
  2003. lua_getfield(L, -1, "x"); /* push result of t.x (2nd arg) */
  2004. lua_remove(L, -2); /* remove 't' from the stack */
  2005. lua_pushinteger(L, 14); /* 3rd argument */
  2006. lua_call(L, 3, 1); /* call 'f' with 3 arguments and 1 result */
  2007. lua_setfield(L, LUA_GLOBALSINDEX, "a"); /* set global 'a' */
  2008. </pre><p>
  2009. Note that the code above is "balanced":
  2010. at its end, the stack is back to its original configuration.
  2011. This is considered good programming practice.
  2012. <hr><h3><a name="lua_CFunction"><code>lua_CFunction</code></a></h3>
  2013. <pre>typedef int (*lua_CFunction) (lua_State *L);</pre>
  2014. <p>
  2015. Type for C&nbsp;functions.
  2016. <p>
  2017. In order to communicate properly with Lua,
  2018. a C&nbsp;function must use the following protocol,
  2019. which defines the way parameters and results are passed:
  2020. a C&nbsp;function receives its arguments from Lua in its stack
  2021. in direct order (the first argument is pushed first).
  2022. So, when the function starts,
  2023. <code>lua_gettop(L)</code> returns the number of arguments received by the function.
  2024. The first argument (if any) is at index 1
  2025. and its last argument is at index <code>lua_gettop(L)</code>.
  2026. To return values to Lua, a C&nbsp;function just pushes them onto the stack,
  2027. in direct order (the first result is pushed first),
  2028. and returns the number of results.
  2029. Any other value in the stack below the results will be properly
  2030. discarded by Lua.
  2031. Like a Lua function, a C&nbsp;function called by Lua can also return
  2032. many results.
  2033. <p>
  2034. As an example, the following function receives a variable number
  2035. of numerical arguments and returns their average and sum:
  2036. <pre>
  2037. static int foo (lua_State *L) {
  2038. int n = lua_gettop(L); /* number of arguments */
  2039. lua_Number sum = 0;
  2040. int i;
  2041. for (i = 1; i &lt;= n; i++) {
  2042. if (!lua_isnumber(L, i)) {
  2043. lua_pushstring(L, "incorrect argument");
  2044. lua_error(L);
  2045. }
  2046. sum += lua_tonumber(L, i);
  2047. }
  2048. lua_pushnumber(L, sum/n); /* first result */
  2049. lua_pushnumber(L, sum); /* second result */
  2050. return 2; /* number of results */
  2051. }
  2052. </pre>
  2053. <hr><h3><a name="lua_checkstack"><code>lua_checkstack</code></a></h3><p>
  2054. <span class="apii">[-0, +0, <em>m</em>]</span>
  2055. <pre>int lua_checkstack (lua_State *L, int extra);</pre>
  2056. <p>
  2057. Ensures that there are at least <code>extra</code> free stack slots in the stack.
  2058. It returns false if it cannot grow the stack to that size.
  2059. This function never shrinks the stack;
  2060. if the stack is already larger than the new size,
  2061. it is left unchanged.
  2062. <hr><h3><a name="lua_close"><code>lua_close</code></a></h3><p>
  2063. <span class="apii">[-0, +0, <em>-</em>]</span>
  2064. <pre>void lua_close (lua_State *L);</pre>
  2065. <p>
  2066. Destroys all objects in the given Lua state
  2067. (calling the corresponding garbage-collection metamethods, if any)
  2068. and frees all dynamic memory used by this state.
  2069. On several platforms, you may not need to call this function,
  2070. because all resources are naturally released when the host program ends.
  2071. On the other hand, long-running programs,
  2072. such as a daemon or a web server,
  2073. might need to release states as soon as they are not needed,
  2074. to avoid growing too large.
  2075. <hr><h3><a name="lua_concat"><code>lua_concat</code></a></h3><p>
  2076. <span class="apii">[-n, +1, <em>e</em>]</span>
  2077. <pre>void lua_concat (lua_State *L, int n);</pre>
  2078. <p>
  2079. Concatenates the <code>n</code> values at the top of the stack,
  2080. pops them, and leaves the result at the top.
  2081. If <code>n</code>&nbsp;is&nbsp;1, the result is the single value on the stack
  2082. (that is, the function does nothing);
  2083. if <code>n</code> is 0, the result is the empty string.
  2084. Concatenation is performed following the usual semantics of Lua
  2085. (see <a href="#2.5.4">&sect;2.5.4</a>).
  2086. <hr><h3><a name="lua_cpcall"><code>lua_cpcall</code></a></h3><p>
  2087. <span class="apii">[-0, +(0|1), <em>-</em>]</span>
  2088. <pre>int lua_cpcall (lua_State *L, lua_CFunction func, void *ud);</pre>
  2089. <p>
  2090. Calls the C&nbsp;function <code>func</code> in protected mode.
  2091. <code>func</code> starts with only one element in its stack,
  2092. a light userdata containing <code>ud</code>.
  2093. In case of errors,
  2094. <a href="#lua_cpcall"><code>lua_cpcall</code></a> returns the same error codes as <a href="#lua_pcall"><code>lua_pcall</code></a>,
  2095. plus the error object on the top of the stack;
  2096. otherwise, it returns zero, and does not change the stack.
  2097. All values returned by <code>func</code> are discarded.
  2098. <hr><h3><a name="lua_createtable"><code>lua_createtable</code></a></h3><p>
  2099. <span class="apii">[-0, +1, <em>m</em>]</span>
  2100. <pre>void lua_createtable (lua_State *L, int narr, int nrec);</pre>
  2101. <p>
  2102. Creates a new empty table and pushes it onto the stack.
  2103. The new table has space pre-allocated
  2104. for <code>narr</code> array elements and <code>nrec</code> non-array elements.
  2105. This pre-allocation is useful when you know exactly how many elements
  2106. the table will have.
  2107. Otherwise you can use the function <a href="#lua_newtable"><code>lua_newtable</code></a>.
  2108. <hr><h3><a name="lua_dump"><code>lua_dump</code></a></h3><p>
  2109. <span class="apii">[-0, +0, <em>m</em>]</span>
  2110. <pre>int lua_dump (lua_State *L, lua_Writer writer, void *data);</pre>
  2111. <p>
  2112. Dumps a function as a binary chunk.
  2113. Receives a Lua function on the top of the stack
  2114. and produces a binary chunk that,
  2115. if loaded again,
  2116. results in a function equivalent to the one dumped.
  2117. As it produces parts of the chunk,
  2118. <a href="#lua_dump"><code>lua_dump</code></a> calls function <code>writer</code> (see <a href="#lua_Writer"><code>lua_Writer</code></a>)
  2119. with the given <code>data</code>
  2120. to write them.
  2121. <p>
  2122. The value returned is the error code returned by the last
  2123. call to the writer;
  2124. 0&nbsp;means no errors.
  2125. <p>
  2126. This function does not pop the Lua function from the stack.
  2127. <hr><h3><a name="lua_equal"><code>lua_equal</code></a></h3><p>
  2128. <span class="apii">[-0, +0, <em>e</em>]</span>
  2129. <pre>int lua_equal (lua_State *L, int index1, int index2);</pre>
  2130. <p>
  2131. Returns 1 if the two values in acceptable indices <code>index1</code> and
  2132. <code>index2</code> are equal,
  2133. following the semantics of the Lua <code>==</code> operator
  2134. (that is, may call metamethods).
  2135. Otherwise returns&nbsp;0.
  2136. Also returns&nbsp;0 if any of the indices is non valid.
  2137. <hr><h3><a name="lua_error"><code>lua_error</code></a></h3><p>
  2138. <span class="apii">[-1, +0, <em>v</em>]</span>
  2139. <pre>int lua_error (lua_State *L);</pre>
  2140. <p>
  2141. Generates a Lua error.
  2142. The error message (which can actually be a Lua value of any type)
  2143. must be on the stack top.
  2144. This function does a long jump,
  2145. and therefore never returns.
  2146. (see <a href="#luaL_error"><code>luaL_error</code></a>).
  2147. <hr><h3><a name="lua_gc"><code>lua_gc</code></a></h3><p>
  2148. <span class="apii">[-0, +0, <em>e</em>]</span>
  2149. <pre>int lua_gc (lua_State *L, int what, int data);</pre>
  2150. <p>
  2151. Controls the garbage collector.
  2152. <p>
  2153. This function performs several tasks,
  2154. according to the value of the parameter <code>what</code>:
  2155. <ul>
  2156. <li><b><code>LUA_GCSTOP</code>:</b>
  2157. stops the garbage collector.
  2158. </li>
  2159. <li><b><code>LUA_GCRESTART</code>:</b>
  2160. restarts the garbage collector.
  2161. </li>
  2162. <li><b><code>LUA_GCCOLLECT</code>:</b>
  2163. performs a full garbage-collection cycle.
  2164. </li>
  2165. <li><b><code>LUA_GCCOUNT</code>:</b>
  2166. returns the current amount of memory (in Kbytes) in use by Lua.
  2167. </li>
  2168. <li><b><code>LUA_GCCOUNTB</code>:</b>
  2169. returns the remainder of dividing the current amount of bytes of
  2170. memory in use by Lua by 1024.
  2171. </li>
  2172. <li><b><code>LUA_GCSTEP</code>:</b>
  2173. performs an incremental step of garbage collection.
  2174. The step "size" is controlled by <code>data</code>
  2175. (larger values mean more steps) in a non-specified way.
  2176. If you want to control the step size
  2177. you must experimentally tune the value of <code>data</code>.
  2178. The function returns 1 if the step finished a
  2179. garbage-collection cycle.
  2180. </li>
  2181. <li><b><code>LUA_GCSETPAUSE</code>:</b>
  2182. sets <code>data</code> as the new value
  2183. for the <em>pause</em> of the collector (see <a href="#2.10">&sect;2.10</a>).
  2184. The function returns the previous value of the pause.
  2185. </li>
  2186. <li><b><code>LUA_GCSETSTEPMUL</code>:</b>
  2187. sets <code>data</code> as the new value for the <em>step multiplier</em> of
  2188. the collector (see <a href="#2.10">&sect;2.10</a>).
  2189. The function returns the previous value of the step multiplier.
  2190. </li>
  2191. </ul>
  2192. <hr><h3><a name="lua_getallocf"><code>lua_getallocf</code></a></h3><p>
  2193. <span class="apii">[-0, +0, <em>-</em>]</span>
  2194. <pre>lua_Alloc lua_getallocf (lua_State *L, void **ud);</pre>
  2195. <p>
  2196. Returns the memory-allocation function of a given state.
  2197. If <code>ud</code> is not <code>NULL</code>, Lua stores in <code>*ud</code> the
  2198. opaque pointer passed to <a href="#lua_newstate"><code>lua_newstate</code></a>.
  2199. <hr><h3><a name="lua_getfenv"><code>lua_getfenv</code></a></h3><p>
  2200. <span class="apii">[-0, +1, <em>-</em>]</span>
  2201. <pre>void lua_getfenv (lua_State *L, int index);</pre>
  2202. <p>
  2203. Pushes onto the stack the environment table of
  2204. the value at the given index.
  2205. <hr><h3><a name="lua_getfield"><code>lua_getfield</code></a></h3><p>
  2206. <span class="apii">[-0, +1, <em>e</em>]</span>
  2207. <pre>void lua_getfield (lua_State *L, int index, const char *k);</pre>
  2208. <p>
  2209. Pushes onto the stack the value <code>t[k]</code>,
  2210. where <code>t</code> is the value at the given valid index.
  2211. As in Lua, this function may trigger a metamethod
  2212. for the "index" event (see <a href="#2.8">&sect;2.8</a>).
  2213. <hr><h3><a name="lua_getglobal"><code>lua_getglobal</code></a></h3><p>
  2214. <span class="apii">[-0, +1, <em>e</em>]</span>
  2215. <pre>void lua_getglobal (lua_State *L, const char *name);</pre>
  2216. <p>
  2217. Pushes onto the stack the value of the global <code>name</code>.
  2218. It is defined as a macro:
  2219. <pre>
  2220. #define lua_getglobal(L,s) lua_getfield(L, LUA_GLOBALSINDEX, s)
  2221. </pre>
  2222. <hr><h3><a name="lua_getmetatable"><code>lua_getmetatable</code></a></h3><p>
  2223. <span class="apii">[-0, +(0|1), <em>-</em>]</span>
  2224. <pre>int lua_getmetatable (lua_State *L, int index);</pre>
  2225. <p>
  2226. Pushes onto the stack the metatable of the value at the given
  2227. acceptable index.
  2228. If the index is not valid,
  2229. or if the value does not have a metatable,
  2230. the function returns&nbsp;0 and pushes nothing on the stack.
  2231. <hr><h3><a name="lua_gettable"><code>lua_gettable</code></a></h3><p>
  2232. <span class="apii">[-1, +1, <em>e</em>]</span>
  2233. <pre>void lua_gettable (lua_State *L, int index);</pre>
  2234. <p>
  2235. Pushes onto the stack the value <code>t[k]</code>,
  2236. where <code>t</code> is the value at the given valid index
  2237. and <code>k</code> is the value at the top of the stack.
  2238. <p>
  2239. This function pops the key from the stack
  2240. (putting the resulting value in its place).
  2241. As in Lua, this function may trigger a metamethod
  2242. for the "index" event (see <a href="#2.8">&sect;2.8</a>).
  2243. <hr><h3><a name="lua_gettop"><code>lua_gettop</code></a></h3><p>
  2244. <span class="apii">[-0, +0, <em>-</em>]</span>
  2245. <pre>int lua_gettop (lua_State *L);</pre>
  2246. <p>
  2247. Returns the index of the top element in the stack.
  2248. Because indices start at&nbsp;1,
  2249. this result is equal to the number of elements in the stack
  2250. (and so 0&nbsp;means an empty stack).
  2251. <hr><h3><a name="lua_insert"><code>lua_insert</code></a></h3><p>
  2252. <span class="apii">[-1, +1, <em>-</em>]</span>
  2253. <pre>void lua_insert (lua_State *L, int index);</pre>
  2254. <p>
  2255. Moves the top element into the given valid index,
  2256. shifting up the elements above this index to open space.
  2257. Cannot be called with a pseudo-index,
  2258. because a pseudo-index is not an actual stack position.
  2259. <hr><h3><a name="lua_Integer"><code>lua_Integer</code></a></h3>
  2260. <pre>typedef ptrdiff_t lua_Integer;</pre>
  2261. <p>
  2262. The type used by the Lua API to represent integral values.
  2263. <p>
  2264. By default it is a <code>ptrdiff_t</code>,
  2265. which is usually the largest signed integral type the machine handles
  2266. "comfortably".
  2267. <hr><h3><a name="lua_isboolean"><code>lua_isboolean</code></a></h3><p>
  2268. <span class="apii">[-0, +0, <em>-</em>]</span>
  2269. <pre>int lua_isboolean (lua_State *L, int index);</pre>
  2270. <p>
  2271. Returns 1 if the value at the given acceptable index has type boolean,
  2272. and 0&nbsp;otherwise.
  2273. <hr><h3><a name="lua_iscfunction"><code>lua_iscfunction</code></a></h3><p>
  2274. <span class="apii">[-0, +0, <em>-</em>]</span>
  2275. <pre>int lua_iscfunction (lua_State *L, int index);</pre>
  2276. <p>
  2277. Returns 1 if the value at the given acceptable index is a C&nbsp;function,
  2278. and 0&nbsp;otherwise.
  2279. <hr><h3><a name="lua_isfunction"><code>lua_isfunction</code></a></h3><p>
  2280. <span class="apii">[-0, +0, <em>-</em>]</span>
  2281. <pre>int lua_isfunction (lua_State *L, int index);</pre>
  2282. <p>
  2283. Returns 1 if the value at the given acceptable index is a function
  2284. (either C or Lua), and 0&nbsp;otherwise.
  2285. <hr><h3><a name="lua_islightuserdata"><code>lua_islightuserdata</code></a></h3><p>
  2286. <span class="apii">[-0, +0, <em>-</em>]</span>
  2287. <pre>int lua_islightuserdata (lua_State *L, int index);</pre>
  2288. <p>
  2289. Returns 1 if the value at the given acceptable index is a light userdata,
  2290. and 0&nbsp;otherwise.
  2291. <hr><h3><a name="lua_isnil"><code>lua_isnil</code></a></h3><p>
  2292. <span class="apii">[-0, +0, <em>-</em>]</span>
  2293. <pre>int lua_isnil (lua_State *L, int index);</pre>
  2294. <p>
  2295. Returns 1 if the value at the given acceptable index is <b>nil</b>,
  2296. and 0&nbsp;otherwise.
  2297. <hr><h3><a name="lua_isnone"><code>lua_isnone</code></a></h3><p>
  2298. <span class="apii">[-0, +0, <em>-</em>]</span>
  2299. <pre>int lua_isnone (lua_State *L, int index);</pre>
  2300. <p>
  2301. Returns 1 if the given acceptable index is not valid
  2302. (that is, it refers to an element outside the current stack),
  2303. and 0&nbsp;otherwise.
  2304. <hr><h3><a name="lua_isnoneornil"><code>lua_isnoneornil</code></a></h3><p>
  2305. <span class="apii">[-0, +0, <em>-</em>]</span>
  2306. <pre>int lua_isnoneornil (lua_State *L, int index);</pre>
  2307. <p>
  2308. Returns 1 if the given acceptable index is not valid
  2309. (that is, it refers to an element outside the current stack)
  2310. or if the value at this index is <b>nil</b>,
  2311. and 0&nbsp;otherwise.
  2312. <hr><h3><a name="lua_isnumber"><code>lua_isnumber</code></a></h3><p>
  2313. <span class="apii">[-0, +0, <em>-</em>]</span>
  2314. <pre>int lua_isnumber (lua_State *L, int index);</pre>
  2315. <p>
  2316. Returns 1 if the value at the given acceptable index is a number
  2317. or a string convertible to a number,
  2318. and 0&nbsp;otherwise.
  2319. <hr><h3><a name="lua_isstring"><code>lua_isstring</code></a></h3><p>
  2320. <span class="apii">[-0, +0, <em>-</em>]</span>
  2321. <pre>int lua_isstring (lua_State *L, int index);</pre>
  2322. <p>
  2323. Returns 1 if the value at the given acceptable index is a string
  2324. or a number (which is always convertible to a string),
  2325. and 0&nbsp;otherwise.
  2326. <hr><h3><a name="lua_istable"><code>lua_istable</code></a></h3><p>
  2327. <span class="apii">[-0, +0, <em>-</em>]</span>
  2328. <pre>int lua_istable (lua_State *L, int index);</pre>
  2329. <p>
  2330. Returns 1 if the value at the given acceptable index is a table,
  2331. and 0&nbsp;otherwise.
  2332. <hr><h3><a name="lua_isthread"><code>lua_isthread</code></a></h3><p>
  2333. <span class="apii">[-0, +0, <em>-</em>]</span>
  2334. <pre>int lua_isthread (lua_State *L, int index);</pre>
  2335. <p>
  2336. Returns 1 if the value at the given acceptable index is a thread,
  2337. and 0&nbsp;otherwise.
  2338. <hr><h3><a name="lua_isuserdata"><code>lua_isuserdata</code></a></h3><p>
  2339. <span class="apii">[-0, +0, <em>-</em>]</span>
  2340. <pre>int lua_isuserdata (lua_State *L, int index);</pre>
  2341. <p>
  2342. Returns 1 if the value at the given acceptable index is a userdata
  2343. (either full or light), and 0&nbsp;otherwise.
  2344. <hr><h3><a name="lua_lessthan"><code>lua_lessthan</code></a></h3><p>
  2345. <span class="apii">[-0, +0, <em>e</em>]</span>
  2346. <pre>int lua_lessthan (lua_State *L, int index1, int index2);</pre>
  2347. <p>
  2348. Returns 1 if the value at acceptable index <code>index1</code> is smaller
  2349. than the value at acceptable index <code>index2</code>,
  2350. following the semantics of the Lua <code>&lt;</code> operator
  2351. (that is, may call metamethods).
  2352. Otherwise returns&nbsp;0.
  2353. Also returns&nbsp;0 if any of the indices is non valid.
  2354. <hr><h3><a name="lua_load"><code>lua_load</code></a></h3><p>
  2355. <span class="apii">[-0, +1, <em>-</em>]</span>
  2356. <pre>int lua_load (lua_State *L,
  2357. lua_Reader reader,
  2358. void *data,
  2359. const char *chunkname);</pre>
  2360. <p>
  2361. Loads a Lua chunk.
  2362. If there are no errors,
  2363. <a href="#lua_load"><code>lua_load</code></a> pushes the compiled chunk as a Lua
  2364. function on top of the stack.
  2365. Otherwise, it pushes an error message.
  2366. The return values of <a href="#lua_load"><code>lua_load</code></a> are:
  2367. <ul>
  2368. <li><b>0:</b> no errors;</li>
  2369. <li><b><a name="pdf-LUA_ERRSYNTAX"><code>LUA_ERRSYNTAX</code></a>:</b>
  2370. syntax error during pre-compilation;</li>
  2371. <li><b><a href="#pdf-LUA_ERRMEM"><code>LUA_ERRMEM</code></a>:</b>
  2372. memory allocation error.</li>
  2373. </ul>
  2374. <p>
  2375. This function only loads a chunk;
  2376. it does not run it.
  2377. <p>
  2378. <a href="#lua_load"><code>lua_load</code></a> automatically detects whether the chunk is text or binary,
  2379. and loads it accordingly (see program <code>luac</code>).
  2380. <p>
  2381. The <a href="#lua_load"><code>lua_load</code></a> function uses a user-supplied <code>reader</code> function
  2382. to read the chunk (see <a href="#lua_Reader"><code>lua_Reader</code></a>).
  2383. The <code>data</code> argument is an opaque value passed to the reader function.
  2384. <p>
  2385. The <code>chunkname</code> argument gives a name to the chunk,
  2386. which is used for error messages and in debug information (see <a href="#3.8">&sect;3.8</a>).
  2387. <hr><h3><a name="lua_newstate"><code>lua_newstate</code></a></h3><p>
  2388. <span class="apii">[-0, +0, <em>-</em>]</span>
  2389. <pre>lua_State *lua_newstate (lua_Alloc f, void *ud);</pre>
  2390. <p>
  2391. Creates a new, independent state.
  2392. Returns <code>NULL</code> if cannot create the state
  2393. (due to lack of memory).
  2394. The argument <code>f</code> is the allocator function;
  2395. Lua does all memory allocation for this state through this function.
  2396. The second argument, <code>ud</code>, is an opaque pointer that Lua
  2397. simply passes to the allocator in every call.
  2398. <hr><h3><a name="lua_newtable"><code>lua_newtable</code></a></h3><p>
  2399. <span class="apii">[-0, +1, <em>m</em>]</span>
  2400. <pre>void lua_newtable (lua_State *L);</pre>
  2401. <p>
  2402. Creates a new empty table and pushes it onto the stack.
  2403. It is equivalent to <code>lua_createtable(L, 0, 0)</code>.
  2404. <hr><h3><a name="lua_newthread"><code>lua_newthread</code></a></h3><p>
  2405. <span class="apii">[-0, +1, <em>m</em>]</span>
  2406. <pre>lua_State *lua_newthread (lua_State *L);</pre>
  2407. <p>
  2408. Creates a new thread, pushes it on the stack,
  2409. and returns a pointer to a <a href="#lua_State"><code>lua_State</code></a> that represents this new thread.
  2410. The new state returned by this function shares with the original state
  2411. all global objects (such as tables),
  2412. but has an independent execution stack.
  2413. <p>
  2414. There is no explicit function to close or to destroy a thread.
  2415. Threads are subject to garbage collection,
  2416. like any Lua object.
  2417. <hr><h3><a name="lua_newuserdata"><code>lua_newuserdata</code></a></h3><p>
  2418. <span class="apii">[-0, +1, <em>m</em>]</span>
  2419. <pre>void *lua_newuserdata (lua_State *L, size_t size);</pre>
  2420. <p>
  2421. This function allocates a new block of memory with the given size,
  2422. pushes onto the stack a new full userdata with the block address,
  2423. and returns this address.
  2424. <p>
  2425. Userdata represent C&nbsp;values in Lua.
  2426. A <em>full userdata</em> represents a block of memory.
  2427. It is an object (like a table):
  2428. you must create it, it can have its own metatable,
  2429. and you can detect when it is being collected.
  2430. A full userdata is only equal to itself (under raw equality).
  2431. <p>
  2432. When Lua collects a full userdata with a <code>gc</code> metamethod,
  2433. Lua calls the metamethod and marks the userdata as finalized.
  2434. When this userdata is collected again then
  2435. Lua frees its corresponding memory.
  2436. <hr><h3><a name="lua_next"><code>lua_next</code></a></h3><p>
  2437. <span class="apii">[-1, +(2|0), <em>e</em>]</span>
  2438. <pre>int lua_next (lua_State *L, int index);</pre>
  2439. <p>
  2440. Pops a key from the stack,
  2441. and pushes a key-value pair from the table at the given index
  2442. (the "next" pair after the given key).
  2443. If there are no more elements in the table,
  2444. then <a href="#lua_next"><code>lua_next</code></a> returns 0 (and pushes nothing).
  2445. <p>
  2446. A typical traversal looks like this:
  2447. <pre>
  2448. /* table is in the stack at index 't' */
  2449. lua_pushnil(L); /* first key */
  2450. while (lua_next(L, t) != 0) {
  2451. /* uses 'key' (at index -2) and 'value' (at index -1) */
  2452. printf("%s - %s\n",
  2453. lua_typename(L, lua_type(L, -2)),
  2454. lua_typename(L, lua_type(L, -1)));
  2455. /* removes 'value'; keeps 'key' for next iteration */
  2456. lua_pop(L, 1);
  2457. }
  2458. </pre>
  2459. <p>
  2460. While traversing a table,
  2461. do not call <a href="#lua_tolstring"><code>lua_tolstring</code></a> directly on a key,
  2462. unless you know that the key is actually a string.
  2463. Recall that <a href="#lua_tolstring"><code>lua_tolstring</code></a> <em>changes</em>
  2464. the value at the given index;
  2465. this confuses the next call to <a href="#lua_next"><code>lua_next</code></a>.
  2466. <hr><h3><a name="lua_Number"><code>lua_Number</code></a></h3>
  2467. <pre>typedef double lua_Number;</pre>
  2468. <p>
  2469. The type of numbers in Lua.
  2470. By default, it is double, but that can be changed in <code>luaconf.h</code>.
  2471. <p>
  2472. Through the configuration file you can change
  2473. Lua to operate with another type for numbers (e.g., float or long).
  2474. <hr><h3><a name="lua_objlen"><code>lua_objlen</code></a></h3><p>
  2475. <span class="apii">[-0, +0, <em>-</em>]</span>
  2476. <pre>size_t lua_objlen (lua_State *L, int index);</pre>
  2477. <p>
  2478. Returns the "length" of the value at the given acceptable index:
  2479. for strings, this is the string length;
  2480. for tables, this is the result of the length operator ('<code>#</code>');
  2481. for userdata, this is the size of the block of memory allocated
  2482. for the userdata;
  2483. for other values, it is&nbsp;0.
  2484. <hr><h3><a name="lua_pcall"><code>lua_pcall</code></a></h3><p>
  2485. <span class="apii">[-(nargs + 1), +(nresults|1), <em>-</em>]</span>
  2486. <pre>int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc);</pre>
  2487. <p>
  2488. Calls a function in protected mode.
  2489. <p>
  2490. Both <code>nargs</code> and <code>nresults</code> have the same meaning as
  2491. in <a href="#lua_call"><code>lua_call</code></a>.
  2492. If there are no errors during the call,
  2493. <a href="#lua_pcall"><code>lua_pcall</code></a> behaves exactly like <a href="#lua_call"><code>lua_call</code></a>.
  2494. However, if there is any error,
  2495. <a href="#lua_pcall"><code>lua_pcall</code></a> catches it,
  2496. pushes a single value on the stack (the error message),
  2497. and returns an error code.
  2498. Like <a href="#lua_call"><code>lua_call</code></a>,
  2499. <a href="#lua_pcall"><code>lua_pcall</code></a> always removes the function
  2500. and its arguments from the stack.
  2501. <p>
  2502. If <code>errfunc</code> is 0,
  2503. then the error message returned on the stack
  2504. is exactly the original error message.
  2505. Otherwise, <code>errfunc</code> is the stack index of an
  2506. <em>error handler function</em>.
  2507. (In the current implementation, this index cannot be a pseudo-index.)
  2508. In case of runtime errors,
  2509. this function will be called with the error message
  2510. and its return value will be the message returned on the stack by <a href="#lua_pcall"><code>lua_pcall</code></a>.
  2511. <p>
  2512. Typically, the error handler function is used to add more debug
  2513. information to the error message, such as a stack traceback.
  2514. Such information cannot be gathered after the return of <a href="#lua_pcall"><code>lua_pcall</code></a>,
  2515. since by then the stack has unwound.
  2516. <p>
  2517. The <a href="#lua_pcall"><code>lua_pcall</code></a> function returns 0 in case of success
  2518. or one of the following error codes
  2519. (defined in <code>lua.h</code>):
  2520. <ul>
  2521. <li><b><a name="pdf-LUA_ERRRUN"><code>LUA_ERRRUN</code></a>:</b>
  2522. a runtime error.
  2523. </li>
  2524. <li><b><a name="pdf-LUA_ERRMEM"><code>LUA_ERRMEM</code></a>:</b>
  2525. memory allocation error.
  2526. For such errors, Lua does not call the error handler function.
  2527. </li>
  2528. <li><b><a name="pdf-LUA_ERRERR"><code>LUA_ERRERR</code></a>:</b>
  2529. error while running the error handler function.
  2530. </li>
  2531. </ul>
  2532. <hr><h3><a name="lua_pop"><code>lua_pop</code></a></h3><p>
  2533. <span class="apii">[-n, +0, <em>-</em>]</span>
  2534. <pre>void lua_pop (lua_State *L, int n);</pre>
  2535. <p>
  2536. Pops <code>n</code> elements from the stack.
  2537. <hr><h3><a name="lua_pushboolean"><code>lua_pushboolean</code></a></h3><p>
  2538. <span class="apii">[-0, +1, <em>-</em>]</span>
  2539. <pre>void lua_pushboolean (lua_State *L, int b);</pre>
  2540. <p>
  2541. Pushes a boolean value with value <code>b</code> onto the stack.
  2542. <hr><h3><a name="lua_pushcclosure"><code>lua_pushcclosure</code></a></h3><p>
  2543. <span class="apii">[-n, +1, <em>m</em>]</span>
  2544. <pre>void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);</pre>
  2545. <p>
  2546. Pushes a new C&nbsp;closure onto the stack.
  2547. <p>
  2548. When a C&nbsp;function is created,
  2549. it is possible to associate some values with it,
  2550. thus creating a C&nbsp;closure (see <a href="#3.4">&sect;3.4</a>);
  2551. these values are then accessible to the function whenever it is called.
  2552. To associate values with a C&nbsp;function,
  2553. first these values should be pushed onto the stack
  2554. (when there are multiple values, the first value is pushed first).
  2555. Then <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a>
  2556. is called to create and push the C&nbsp;function onto the stack,
  2557. with the argument <code>n</code> telling how many values should be
  2558. associated with the function.
  2559. <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a> also pops these values from the stack.
  2560. <p>
  2561. The maximum value for <code>n</code> is 255.
  2562. <hr><h3><a name="lua_pushcfunction"><code>lua_pushcfunction</code></a></h3><p>
  2563. <span class="apii">[-0, +1, <em>m</em>]</span>
  2564. <pre>void lua_pushcfunction (lua_State *L, lua_CFunction f);</pre>
  2565. <p>
  2566. Pushes a C&nbsp;function onto the stack.
  2567. This function receives a pointer to a C function
  2568. and pushes onto the stack a Lua value of type <code>function</code> that,
  2569. when called, invokes the corresponding C&nbsp;function.
  2570. <p>
  2571. Any function to be registered in Lua must
  2572. follow the correct protocol to receive its parameters
  2573. and return its results (see <a href="#lua_CFunction"><code>lua_CFunction</code></a>).
  2574. <p>
  2575. <code>lua_pushcfunction</code> is defined as a macro:
  2576. <pre>
  2577. #define lua_pushcfunction(L,f) lua_pushcclosure(L,f,0)
  2578. </pre>
  2579. <hr><h3><a name="lua_pushfstring"><code>lua_pushfstring</code></a></h3><p>
  2580. <span class="apii">[-0, +1, <em>m</em>]</span>
  2581. <pre>const char *lua_pushfstring (lua_State *L, const char *fmt, ...);</pre>
  2582. <p>
  2583. Pushes onto the stack a formatted string
  2584. and returns a pointer to this string.
  2585. It is similar to the C&nbsp;function <code>sprintf</code>,
  2586. but has some important differences:
  2587. <ul>
  2588. <li>
  2589. You do not have to allocate space for the result:
  2590. the result is a Lua string and Lua takes care of memory allocation
  2591. (and deallocation, through garbage collection).
  2592. </li>
  2593. <li>
  2594. The conversion specifiers are quite restricted.
  2595. There are no flags, widths, or precisions.
  2596. The conversion specifiers can only be
  2597. '<code>%%</code>' (inserts a '<code>%</code>' in the string),
  2598. '<code>%s</code>' (inserts a zero-terminated string, with no size restrictions),
  2599. '<code>%f</code>' (inserts a <a href="#lua_Number"><code>lua_Number</code></a>),
  2600. '<code>%p</code>' (inserts a pointer as a hexadecimal numeral),
  2601. '<code>%d</code>' (inserts an <code>int</code>), and
  2602. '<code>%c</code>' (inserts an <code>int</code> as a character).
  2603. </li>
  2604. </ul>
  2605. <hr><h3><a name="lua_pushinteger"><code>lua_pushinteger</code></a></h3><p>
  2606. <span class="apii">[-0, +1, <em>-</em>]</span>
  2607. <pre>void lua_pushinteger (lua_State *L, lua_Integer n);</pre>
  2608. <p>
  2609. Pushes a number with value <code>n</code> onto the stack.
  2610. <hr><h3><a name="lua_pushlightuserdata"><code>lua_pushlightuserdata</code></a></h3><p>
  2611. <span class="apii">[-0, +1, <em>-</em>]</span>
  2612. <pre>void lua_pushlightuserdata (lua_State *L, void *p);</pre>
  2613. <p>
  2614. Pushes a light userdata onto the stack.
  2615. <p>
  2616. Userdata represent C&nbsp;values in Lua.
  2617. A <em>light userdata</em> represents a pointer.
  2618. It is a value (like a number):
  2619. you do not create it, it has no individual metatable,
  2620. and it is not collected (as it was never created).
  2621. A light userdata is equal to "any"
  2622. light userdata with the same C&nbsp;address.
  2623. <hr><h3><a name="lua_pushliteral"><code>lua_pushliteral</code></a></h3><p>
  2624. <span class="apii">[-0, +1, <em>m</em>]</span>
  2625. <pre>void lua_pushliteral (lua_State *L, const char *s);</pre>
  2626. <p>
  2627. This macro is equivalent to <a href="#lua_pushlstring"><code>lua_pushlstring</code></a>,
  2628. but can be used only when <code>s</code> is a literal string.
  2629. In these cases, it automatically provides the string length.
  2630. <hr><h3><a name="lua_pushlstring"><code>lua_pushlstring</code></a></h3><p>
  2631. <span class="apii">[-0, +1, <em>m</em>]</span>
  2632. <pre>void lua_pushlstring (lua_State *L, const char *s, size_t len);</pre>
  2633. <p>
  2634. Pushes the string pointed to by <code>s</code> with size <code>len</code>
  2635. onto the stack.
  2636. Lua makes (or reuses) an internal copy of the given string,
  2637. so the memory at <code>s</code> can be freed or reused immediately after
  2638. the function returns.
  2639. The string can contain embedded zeros.
  2640. <hr><h3><a name="lua_pushnil"><code>lua_pushnil</code></a></h3><p>
  2641. <span class="apii">[-0, +1, <em>-</em>]</span>
  2642. <pre>void lua_pushnil (lua_State *L);</pre>
  2643. <p>
  2644. Pushes a nil value onto the stack.
  2645. <hr><h3><a name="lua_pushnumber"><code>lua_pushnumber</code></a></h3><p>
  2646. <span class="apii">[-0, +1, <em>-</em>]</span>
  2647. <pre>void lua_pushnumber (lua_State *L, lua_Number n);</pre>
  2648. <p>
  2649. Pushes a number with value <code>n</code> onto the stack.
  2650. <hr><h3><a name="lua_pushstring"><code>lua_pushstring</code></a></h3><p>
  2651. <span class="apii">[-0, +1, <em>m</em>]</span>
  2652. <pre>void lua_pushstring (lua_State *L, const char *s);</pre>
  2653. <p>
  2654. Pushes the zero-terminated string pointed to by <code>s</code>
  2655. onto the stack.
  2656. Lua makes (or reuses) an internal copy of the given string,
  2657. so the memory at <code>s</code> can be freed or reused immediately after
  2658. the function returns.
  2659. The string cannot contain embedded zeros;
  2660. it is assumed to end at the first zero.
  2661. <hr><h3><a name="lua_pushthread"><code>lua_pushthread</code></a></h3><p>
  2662. <span class="apii">[-0, +1, <em>-</em>]</span>
  2663. <pre>int lua_pushthread (lua_State *L);</pre>
  2664. <p>
  2665. Pushes the thread represented by <code>L</code> onto the stack.
  2666. Returns 1 if this thread is the main thread of its state.
  2667. <hr><h3><a name="lua_pushvalue"><code>lua_pushvalue</code></a></h3><p>
  2668. <span class="apii">[-0, +1, <em>-</em>]</span>
  2669. <pre>void lua_pushvalue (lua_State *L, int index);</pre>
  2670. <p>
  2671. Pushes a copy of the element at the given valid index
  2672. onto the stack.
  2673. <hr><h3><a name="lua_pushvfstring"><code>lua_pushvfstring</code></a></h3><p>
  2674. <span class="apii">[-0, +1, <em>m</em>]</span>
  2675. <pre>const char *lua_pushvfstring (lua_State *L,
  2676. const char *fmt,
  2677. va_list argp);</pre>
  2678. <p>
  2679. Equivalent to <a href="#lua_pushfstring"><code>lua_pushfstring</code></a>, except that it receives a <code>va_list</code>
  2680. instead of a variable number of arguments.
  2681. <hr><h3><a name="lua_rawequal"><code>lua_rawequal</code></a></h3><p>
  2682. <span class="apii">[-0, +0, <em>-</em>]</span>
  2683. <pre>int lua_rawequal (lua_State *L, int index1, int index2);</pre>
  2684. <p>
  2685. Returns 1 if the two values in acceptable indices <code>index1</code> and
  2686. <code>index2</code> are primitively equal
  2687. (that is, without calling metamethods).
  2688. Otherwise returns&nbsp;0.
  2689. Also returns&nbsp;0 if any of the indices are non valid.
  2690. <hr><h3><a name="lua_rawget"><code>lua_rawget</code></a></h3><p>
  2691. <span class="apii">[-1, +1, <em>-</em>]</span>
  2692. <pre>void lua_rawget (lua_State *L, int index);</pre>
  2693. <p>
  2694. Similar to <a href="#lua_gettable"><code>lua_gettable</code></a>, but does a raw access
  2695. (i.e., without metamethods).
  2696. <hr><h3><a name="lua_rawgeti"><code>lua_rawgeti</code></a></h3><p>
  2697. <span class="apii">[-0, +1, <em>-</em>]</span>
  2698. <pre>void lua_rawgeti (lua_State *L, int index, int n);</pre>
  2699. <p>
  2700. Pushes onto the stack the value <code>t[n]</code>,
  2701. where <code>t</code> is the value at the given valid index.
  2702. The access is raw;
  2703. that is, it does not invoke metamethods.
  2704. <hr><h3><a name="lua_rawset"><code>lua_rawset</code></a></h3><p>
  2705. <span class="apii">[-2, +0, <em>m</em>]</span>
  2706. <pre>void lua_rawset (lua_State *L, int index);</pre>
  2707. <p>
  2708. Similar to <a href="#lua_settable"><code>lua_settable</code></a>, but does a raw assignment
  2709. (i.e., without metamethods).
  2710. <hr><h3><a name="lua_rawseti"><code>lua_rawseti</code></a></h3><p>
  2711. <span class="apii">[-1, +0, <em>m</em>]</span>
  2712. <pre>void lua_rawseti (lua_State *L, int index, int n);</pre>
  2713. <p>
  2714. Does the equivalent of <code>t[n] = v</code>,
  2715. where <code>t</code> is the value at the given valid index
  2716. and <code>v</code> is the value at the top of the stack.
  2717. <p>
  2718. This function pops the value from the stack.
  2719. The assignment is raw;
  2720. that is, it does not invoke metamethods.
  2721. <hr><h3><a name="lua_Reader"><code>lua_Reader</code></a></h3>
  2722. <pre>typedef const char * (*lua_Reader) (lua_State *L,
  2723. void *data,
  2724. size_t *size);</pre>
  2725. <p>
  2726. The reader function used by <a href="#lua_load"><code>lua_load</code></a>.
  2727. Every time it needs another piece of the chunk,
  2728. <a href="#lua_load"><code>lua_load</code></a> calls the reader,
  2729. passing along its <code>data</code> parameter.
  2730. The reader must return a pointer to a block of memory
  2731. with a new piece of the chunk
  2732. and set <code>size</code> to the block size.
  2733. The block must exist until the reader function is called again.
  2734. To signal the end of the chunk,
  2735. the reader must return <code>NULL</code> or set <code>size</code> to zero.
  2736. The reader function may return pieces of any size greater than zero.
  2737. <hr><h3><a name="lua_register"><code>lua_register</code></a></h3><p>
  2738. <span class="apii">[-0, +0, <em>e</em>]</span>
  2739. <pre>void lua_register (lua_State *L,
  2740. const char *name,
  2741. lua_CFunction f);</pre>
  2742. <p>
  2743. Sets the C function <code>f</code> as the new value of global <code>name</code>.
  2744. It is defined as a macro:
  2745. <pre>
  2746. #define lua_register(L,n,f) \
  2747. (lua_pushcfunction(L, f), lua_setglobal(L, n))
  2748. </pre>
  2749. <hr><h3><a name="lua_remove"><code>lua_remove</code></a></h3><p>
  2750. <span class="apii">[-1, +0, <em>-</em>]</span>
  2751. <pre>void lua_remove (lua_State *L, int index);</pre>
  2752. <p>
  2753. Removes the element at the given valid index,
  2754. shifting down the elements above this index to fill the gap.
  2755. Cannot be called with a pseudo-index,
  2756. because a pseudo-index is not an actual stack position.
  2757. <hr><h3><a name="lua_replace"><code>lua_replace</code></a></h3><p>
  2758. <span class="apii">[-1, +0, <em>-</em>]</span>
  2759. <pre>void lua_replace (lua_State *L, int index);</pre>
  2760. <p>
  2761. Moves the top element into the given position (and pops it),
  2762. without shifting any element
  2763. (therefore replacing the value at the given position).
  2764. <hr><h3><a name="lua_resume"><code>lua_resume</code></a></h3><p>
  2765. <span class="apii">[-?, +?, <em>-</em>]</span>
  2766. <pre>int lua_resume (lua_State *L, int narg);</pre>
  2767. <p>
  2768. Starts and resumes a coroutine in a given thread.
  2769. <p>
  2770. To start a coroutine, you first create a new thread
  2771. (see <a href="#lua_newthread"><code>lua_newthread</code></a>);
  2772. then you push onto its stack the main function plus any arguments;
  2773. then you call <a href="#lua_resume"><code>lua_resume</code></a>,
  2774. with <code>narg</code> being the number of arguments.
  2775. This call returns when the coroutine suspends or finishes its execution.
  2776. When it returns, the stack contains all values passed to <a href="#lua_yield"><code>lua_yield</code></a>,
  2777. or all values returned by the body function.
  2778. <a href="#lua_resume"><code>lua_resume</code></a> returns
  2779. <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> if the coroutine yields,
  2780. 0 if the coroutine finishes its execution
  2781. without errors,
  2782. or an error code in case of errors (see <a href="#lua_pcall"><code>lua_pcall</code></a>).
  2783. In case of errors,
  2784. the stack is not unwound,
  2785. so you can use the debug API over it.
  2786. The error message is on the top of the stack.
  2787. To restart a coroutine, you put on its stack only the values to
  2788. be passed as results from <code>yield</code>,
  2789. and then call <a href="#lua_resume"><code>lua_resume</code></a>.
  2790. <hr><h3><a name="lua_setallocf"><code>lua_setallocf</code></a></h3><p>
  2791. <span class="apii">[-0, +0, <em>-</em>]</span>
  2792. <pre>void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);</pre>
  2793. <p>
  2794. Changes the allocator function of a given state to <code>f</code>
  2795. with user data <code>ud</code>.
  2796. <hr><h3><a name="lua_setfenv"><code>lua_setfenv</code></a></h3><p>
  2797. <span class="apii">[-1, +0, <em>-</em>]</span>
  2798. <pre>int lua_setfenv (lua_State *L, int index);</pre>
  2799. <p>
  2800. Pops a table from the stack and sets it as
  2801. the new environment for the value at the given index.
  2802. If the value at the given index is
  2803. neither a function nor a thread nor a userdata,
  2804. <a href="#lua_setfenv"><code>lua_setfenv</code></a> returns 0.
  2805. Otherwise it returns 1.
  2806. <hr><h3><a name="lua_setfield"><code>lua_setfield</code></a></h3><p>
  2807. <span class="apii">[-1, +0, <em>e</em>]</span>
  2808. <pre>void lua_setfield (lua_State *L, int index, const char *k);</pre>
  2809. <p>
  2810. Does the equivalent to <code>t[k] = v</code>,
  2811. where <code>t</code> is the value at the given valid index
  2812. and <code>v</code> is the value at the top of the stack.
  2813. <p>
  2814. This function pops the value from the stack.
  2815. As in Lua, this function may trigger a metamethod
  2816. for the "newindex" event (see <a href="#2.8">&sect;2.8</a>).
  2817. <hr><h3><a name="lua_setglobal"><code>lua_setglobal</code></a></h3><p>
  2818. <span class="apii">[-1, +0, <em>e</em>]</span>
  2819. <pre>void lua_setglobal (lua_State *L, const char *name);</pre>
  2820. <p>
  2821. Pops a value from the stack and
  2822. sets it as the new value of global <code>name</code>.
  2823. It is defined as a macro:
  2824. <pre>
  2825. #define lua_setglobal(L,s) lua_setfield(L, LUA_GLOBALSINDEX, s)
  2826. </pre>
  2827. <hr><h3><a name="lua_setmetatable"><code>lua_setmetatable</code></a></h3><p>
  2828. <span class="apii">[-1, +0, <em>-</em>]</span>
  2829. <pre>int lua_setmetatable (lua_State *L, int index);</pre>
  2830. <p>
  2831. Pops a table from the stack and
  2832. sets it as the new metatable for the value at the given
  2833. acceptable index.
  2834. <hr><h3><a name="lua_settable"><code>lua_settable</code></a></h3><p>
  2835. <span class="apii">[-2, +0, <em>e</em>]</span>
  2836. <pre>void lua_settable (lua_State *L, int index);</pre>
  2837. <p>
  2838. Does the equivalent to <code>t[k] = v</code>,
  2839. where <code>t</code> is the value at the given valid index,
  2840. <code>v</code> is the value at the top of the stack,
  2841. and <code>k</code> is the value just below the top.
  2842. <p>
  2843. This function pops both the key and the value from the stack.
  2844. As in Lua, this function may trigger a metamethod
  2845. for the "newindex" event (see <a href="#2.8">&sect;2.8</a>).
  2846. <hr><h3><a name="lua_settop"><code>lua_settop</code></a></h3><p>
  2847. <span class="apii">[-?, +?, <em>-</em>]</span>
  2848. <pre>void lua_settop (lua_State *L, int index);</pre>
  2849. <p>
  2850. Accepts any acceptable index, or&nbsp;0,
  2851. and sets the stack top to this index.
  2852. If the new top is larger than the old one,
  2853. then the new elements are filled with <b>nil</b>.
  2854. If <code>index</code> is&nbsp;0, then all stack elements are removed.
  2855. <hr><h3><a name="lua_State"><code>lua_State</code></a></h3>
  2856. <pre>typedef struct lua_State lua_State;</pre>
  2857. <p>
  2858. Opaque structure that keeps the whole state of a Lua interpreter.
  2859. The Lua library is fully reentrant:
  2860. it has no global variables.
  2861. All information about a state is kept in this structure.
  2862. <p>
  2863. A pointer to this state must be passed as the first argument to
  2864. every function in the library, except to <a href="#lua_newstate"><code>lua_newstate</code></a>,
  2865. which creates a Lua state from scratch.
  2866. <hr><h3><a name="lua_status"><code>lua_status</code></a></h3><p>
  2867. <span class="apii">[-0, +0, <em>-</em>]</span>
  2868. <pre>int lua_status (lua_State *L);</pre>
  2869. <p>
  2870. Returns the status of the thread <code>L</code>.
  2871. <p>
  2872. The status can be 0 for a normal thread,
  2873. an error code if the thread finished its execution with an error,
  2874. or <a name="pdf-LUA_YIELD"><code>LUA_YIELD</code></a> if the thread is suspended.
  2875. <hr><h3><a name="lua_toboolean"><code>lua_toboolean</code></a></h3><p>
  2876. <span class="apii">[-0, +0, <em>-</em>]</span>
  2877. <pre>int lua_toboolean (lua_State *L, int index);</pre>
  2878. <p>
  2879. Converts the Lua value at the given acceptable index to a C&nbsp;boolean
  2880. value (0&nbsp;or&nbsp;1).
  2881. Like all tests in Lua,
  2882. <a href="#lua_toboolean"><code>lua_toboolean</code></a> returns 1 for any Lua value
  2883. different from <b>false</b> and <b>nil</b>;
  2884. otherwise it returns 0.
  2885. It also returns 0 when called with a non-valid index.
  2886. (If you want to accept only actual boolean values,
  2887. use <a href="#lua_isboolean"><code>lua_isboolean</code></a> to test the value's type.)
  2888. <hr><h3><a name="lua_tocfunction"><code>lua_tocfunction</code></a></h3><p>
  2889. <span class="apii">[-0, +0, <em>-</em>]</span>
  2890. <pre>lua_CFunction lua_tocfunction (lua_State *L, int index);</pre>
  2891. <p>
  2892. Converts a value at the given acceptable index to a C&nbsp;function.
  2893. That value must be a C&nbsp;function;
  2894. otherwise, returns <code>NULL</code>.
  2895. <hr><h3><a name="lua_tointeger"><code>lua_tointeger</code></a></h3><p>
  2896. <span class="apii">[-0, +0, <em>-</em>]</span>
  2897. <pre>lua_Integer lua_tointeger (lua_State *L, int index);</pre>
  2898. <p>
  2899. Converts the Lua value at the given acceptable index
  2900. to the signed integral type <a href="#lua_Integer"><code>lua_Integer</code></a>.
  2901. The Lua value must be a number or a string convertible to a number
  2902. (see <a href="#2.2.1">&sect;2.2.1</a>);
  2903. otherwise, <a href="#lua_tointeger"><code>lua_tointeger</code></a> returns&nbsp;0.
  2904. <p>
  2905. If the number is not an integer,
  2906. it is truncated in some non-specified way.
  2907. <hr><h3><a name="lua_tolstring"><code>lua_tolstring</code></a></h3><p>
  2908. <span class="apii">[-0, +0, <em>m</em>]</span>
  2909. <pre>const char *lua_tolstring (lua_State *L, int index, size_t *len);</pre>
  2910. <p>
  2911. Converts the Lua value at the given acceptable index to a C&nbsp;string.
  2912. If <code>len</code> is not <code>NULL</code>,
  2913. it also sets <code>*len</code> with the string length.
  2914. The Lua value must be a string or a number;
  2915. otherwise, the function returns <code>NULL</code>.
  2916. If the value is a number,
  2917. then <a href="#lua_tolstring"><code>lua_tolstring</code></a> also
  2918. <em>changes the actual value in the stack to a string</em>.
  2919. (This change confuses <a href="#lua_next"><code>lua_next</code></a>
  2920. when <a href="#lua_tolstring"><code>lua_tolstring</code></a> is applied to keys during a table traversal.)
  2921. <p>
  2922. <a href="#lua_tolstring"><code>lua_tolstring</code></a> returns a fully aligned pointer
  2923. to a string inside the Lua state.
  2924. This string always has a zero ('<code>\0</code>')
  2925. after its last character (as in&nbsp;C),
  2926. but can contain other zeros in its body.
  2927. Because Lua has garbage collection,
  2928. there is no guarantee that the pointer returned by <a href="#lua_tolstring"><code>lua_tolstring</code></a>
  2929. will be valid after the corresponding value is removed from the stack.
  2930. <hr><h3><a name="lua_tonumber"><code>lua_tonumber</code></a></h3><p>
  2931. <span class="apii">[-0, +0, <em>-</em>]</span>
  2932. <pre>lua_Number lua_tonumber (lua_State *L, int index);</pre>
  2933. <p>
  2934. Converts the Lua value at the given acceptable index
  2935. to the C&nbsp;type <a href="#lua_Number"><code>lua_Number</code></a> (see <a href="#lua_Number"><code>lua_Number</code></a>).
  2936. The Lua value must be a number or a string convertible to a number
  2937. (see <a href="#2.2.1">&sect;2.2.1</a>);
  2938. otherwise, <a href="#lua_tonumber"><code>lua_tonumber</code></a> returns&nbsp;0.
  2939. <hr><h3><a name="lua_topointer"><code>lua_topointer</code></a></h3><p>
  2940. <span class="apii">[-0, +0, <em>-</em>]</span>
  2941. <pre>const void *lua_topointer (lua_State *L, int index);</pre>
  2942. <p>
  2943. Converts the value at the given acceptable index to a generic
  2944. C&nbsp;pointer (<code>void*</code>).
  2945. The value can be a userdata, a table, a thread, or a function;
  2946. otherwise, <a href="#lua_topointer"><code>lua_topointer</code></a> returns <code>NULL</code>.
  2947. Different objects will give different pointers.
  2948. There is no way to convert the pointer back to its original value.
  2949. <p>
  2950. Typically this function is used only for debug information.
  2951. <hr><h3><a name="lua_tostring"><code>lua_tostring</code></a></h3><p>
  2952. <span class="apii">[-0, +0, <em>m</em>]</span>
  2953. <pre>const char *lua_tostring (lua_State *L, int index);</pre>
  2954. <p>
  2955. Equivalent to <a href="#lua_tolstring"><code>lua_tolstring</code></a> with <code>len</code> equal to <code>NULL</code>.
  2956. <hr><h3><a name="lua_tothread"><code>lua_tothread</code></a></h3><p>
  2957. <span class="apii">[-0, +0, <em>-</em>]</span>
  2958. <pre>lua_State *lua_tothread (lua_State *L, int index);</pre>
  2959. <p>
  2960. Converts the value at the given acceptable index to a Lua thread
  2961. (represented as <code>lua_State*</code>).
  2962. This value must be a thread;
  2963. otherwise, the function returns <code>NULL</code>.
  2964. <hr><h3><a name="lua_touserdata"><code>lua_touserdata</code></a></h3><p>
  2965. <span class="apii">[-0, +0, <em>-</em>]</span>
  2966. <pre>void *lua_touserdata (lua_State *L, int index);</pre>
  2967. <p>
  2968. If the value at the given acceptable index is a full userdata,
  2969. returns its block address.
  2970. If the value is a light userdata,
  2971. returns its pointer.
  2972. Otherwise, returns <code>NULL</code>.
  2973. <hr><h3><a name="lua_type"><code>lua_type</code></a></h3><p>
  2974. <span class="apii">[-0, +0, <em>-</em>]</span>
  2975. <pre>int lua_type (lua_State *L, int index);</pre>
  2976. <p>
  2977. Returns the type of the value in the given acceptable index,
  2978. or <code>LUA_TNONE</code> for a non-valid index
  2979. (that is, an index to an "empty" stack position).
  2980. The types returned by <a href="#lua_type"><code>lua_type</code></a> are coded by the following constants
  2981. defined in <code>lua.h</code>:
  2982. <code>LUA_TNIL</code>,
  2983. <code>LUA_TNUMBER</code>,
  2984. <code>LUA_TBOOLEAN</code>,
  2985. <code>LUA_TSTRING</code>,
  2986. <code>LUA_TTABLE</code>,
  2987. <code>LUA_TFUNCTION</code>,
  2988. <code>LUA_TUSERDATA</code>,
  2989. <code>LUA_TTHREAD</code>,
  2990. and
  2991. <code>LUA_TLIGHTUSERDATA</code>.
  2992. <hr><h3><a name="lua_typename"><code>lua_typename</code></a></h3><p>
  2993. <span class="apii">[-0, +0, <em>-</em>]</span>
  2994. <pre>const char *lua_typename (lua_State *L, int tp);</pre>
  2995. <p>
  2996. Returns the name of the type encoded by the value <code>tp</code>,
  2997. which must be one the values returned by <a href="#lua_type"><code>lua_type</code></a>.
  2998. <hr><h3><a name="lua_Writer"><code>lua_Writer</code></a></h3>
  2999. <pre>typedef int (*lua_Writer) (lua_State *L,
  3000. const void* p,
  3001. size_t sz,
  3002. void* ud);</pre>
  3003. <p>
  3004. The type of the writer function used by <a href="#lua_dump"><code>lua_dump</code></a>.
  3005. Every time it produces another piece of chunk,
  3006. <a href="#lua_dump"><code>lua_dump</code></a> calls the writer,
  3007. passing along the buffer to be written (<code>p</code>),
  3008. its size (<code>sz</code>),
  3009. and the <code>data</code> parameter supplied to <a href="#lua_dump"><code>lua_dump</code></a>.
  3010. <p>
  3011. The writer returns an error code:
  3012. 0&nbsp;means no errors;
  3013. any other value means an error and stops <a href="#lua_dump"><code>lua_dump</code></a> from
  3014. calling the writer again.
  3015. <hr><h3><a name="lua_xmove"><code>lua_xmove</code></a></h3><p>
  3016. <span class="apii">[-?, +?, <em>-</em>]</span>
  3017. <pre>void lua_xmove (lua_State *from, lua_State *to, int n);</pre>
  3018. <p>
  3019. Exchange values between different threads of the <em>same</em> global state.
  3020. <p>
  3021. This function pops <code>n</code> values from the stack <code>from</code>,
  3022. and pushes them onto the stack <code>to</code>.
  3023. <hr><h3><a name="lua_yield"><code>lua_yield</code></a></h3><p>
  3024. <span class="apii">[-?, +?, <em>-</em>]</span>
  3025. <pre>int lua_yield (lua_State *L, int nresults);</pre>
  3026. <p>
  3027. Yields a coroutine.
  3028. <p>
  3029. This function should only be called as the
  3030. return expression of a C&nbsp;function, as follows:
  3031. <pre>
  3032. return lua_yield (L, nresults);
  3033. </pre><p>
  3034. When a C&nbsp;function calls <a href="#lua_yield"><code>lua_yield</code></a> in that way,
  3035. the running coroutine suspends its execution,
  3036. and the call to <a href="#lua_resume"><code>lua_resume</code></a> that started this coroutine returns.
  3037. The parameter <code>nresults</code> is the number of values from the stack
  3038. that are passed as results to <a href="#lua_resume"><code>lua_resume</code></a>.
  3039. <h2>3.8 - <a name="3.8">The Debug Interface</a></h2>
  3040. <p>
  3041. Lua has no built-in debugging facilities.
  3042. Instead, it offers a special interface
  3043. by means of functions and <em>hooks</em>.
  3044. This interface allows the construction of different
  3045. kinds of debuggers, profilers, and other tools
  3046. that need "inside information" from the interpreter.
  3047. <hr><h3><a name="lua_Debug"><code>lua_Debug</code></a></h3>
  3048. <pre>typedef struct lua_Debug {
  3049. int event;
  3050. const char *name; /* (n) */
  3051. const char *namewhat; /* (n) */
  3052. const char *what; /* (S) */
  3053. const char *source; /* (S) */
  3054. int currentline; /* (l) */
  3055. int nups; /* (u) number of upvalues */
  3056. int linedefined; /* (S) */
  3057. int lastlinedefined; /* (S) */
  3058. char short_src[LUA_IDSIZE]; /* (S) */
  3059. /* private part */
  3060. <em>other fields</em>
  3061. } lua_Debug;</pre>
  3062. <p>
  3063. A structure used to carry different pieces of
  3064. information about an active function.
  3065. <a href="#lua_getstack"><code>lua_getstack</code></a> fills only the private part
  3066. of this structure, for later use.
  3067. To fill the other fields of <a href="#lua_Debug"><code>lua_Debug</code></a> with useful information,
  3068. call <a href="#lua_getinfo"><code>lua_getinfo</code></a>.
  3069. <p>
  3070. The fields of <a href="#lua_Debug"><code>lua_Debug</code></a> have the following meaning:
  3071. <ul>
  3072. <li><b><code>source</code>:</b>
  3073. If the function was defined in a string,
  3074. then <code>source</code> is that string.
  3075. If the function was defined in a file,
  3076. then <code>source</code> starts with a '<code>@</code>' followed by the file name.
  3077. </li>
  3078. <li><b><code>short_src</code>:</b>
  3079. a "printable" version of <code>source</code>, to be used in error messages.
  3080. </li>
  3081. <li><b><code>linedefined</code>:</b>
  3082. the line number where the definition of the function starts.
  3083. </li>
  3084. <li><b><code>lastlinedefined</code>:</b>
  3085. the line number where the definition of the function ends.
  3086. </li>
  3087. <li><b><code>what</code>:</b>
  3088. the string <code>"Lua"</code> if the function is a Lua function,
  3089. <code>"C"</code> if it is a C&nbsp;function,
  3090. <code>"main"</code> if it is the main part of a chunk,
  3091. and <code>"tail"</code> if it was a function that did a tail call.
  3092. In the latter case,
  3093. Lua has no other information about the function.
  3094. </li>
  3095. <li><b><code>currentline</code>:</b>
  3096. the current line where the given function is executing.
  3097. When no line information is available,
  3098. <code>currentline</code> is set to -1.
  3099. </li>
  3100. <li><b><code>name</code>:</b>
  3101. a reasonable name for the given function.
  3102. Because functions in Lua are first-class values,
  3103. they do not have a fixed name:
  3104. some functions can be the value of multiple global variables,
  3105. while others can be stored only in a table field.
  3106. The <code>lua_getinfo</code> function checks how the function was
  3107. called to find a suitable name.
  3108. If it cannot find a name,
  3109. then <code>name</code> is set to <code>NULL</code>.
  3110. </li>
  3111. <li><b><code>namewhat</code>:</b>
  3112. explains the <code>name</code> field.
  3113. The value of <code>namewhat</code> can be
  3114. <code>"global"</code>, <code>"local"</code>, <code>"method"</code>,
  3115. <code>"field"</code>, <code>"upvalue"</code>, or <code>""</code> (the empty string),
  3116. according to how the function was called.
  3117. (Lua uses the empty string when no other option seems to apply.)
  3118. </li>
  3119. <li><b><code>nups</code>:</b>
  3120. the number of upvalues of the function.
  3121. </li>
  3122. </ul>
  3123. <hr><h3><a name="lua_gethook"><code>lua_gethook</code></a></h3><p>
  3124. <span class="apii">[-0, +0, <em>-</em>]</span>
  3125. <pre>lua_Hook lua_gethook (lua_State *L);</pre>
  3126. <p>
  3127. Returns the current hook function.
  3128. <hr><h3><a name="lua_gethookcount"><code>lua_gethookcount</code></a></h3><p>
  3129. <span class="apii">[-0, +0, <em>-</em>]</span>
  3130. <pre>int lua_gethookcount (lua_State *L);</pre>
  3131. <p>
  3132. Returns the current hook count.
  3133. <hr><h3><a name="lua_gethookmask"><code>lua_gethookmask</code></a></h3><p>
  3134. <span class="apii">[-0, +0, <em>-</em>]</span>
  3135. <pre>int lua_gethookmask (lua_State *L);</pre>
  3136. <p>
  3137. Returns the current hook mask.
  3138. <hr><h3><a name="lua_getinfo"><code>lua_getinfo</code></a></h3><p>
  3139. <span class="apii">[-(0|1), +(0|1|2), <em>m</em>]</span>
  3140. <pre>int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);</pre>
  3141. <p>
  3142. Returns information about a specific function or function invocation.
  3143. <p>
  3144. To get information about a function invocation,
  3145. the parameter <code>ar</code> must be a valid activation record that was
  3146. filled by a previous call to <a href="#lua_getstack"><code>lua_getstack</code></a> or
  3147. given as argument to a hook (see <a href="#lua_Hook"><code>lua_Hook</code></a>).
  3148. <p>
  3149. To get information about a function you push it onto the stack
  3150. and start the <code>what</code> string with the character '<code>&gt;</code>'.
  3151. (In that case,
  3152. <code>lua_getinfo</code> pops the function in the top of the stack.)
  3153. For instance, to know in which line a function <code>f</code> was defined,
  3154. you can write the following code:
  3155. <pre>
  3156. lua_Debug ar;
  3157. lua_getfield(L, LUA_GLOBALSINDEX, "f"); /* get global 'f' */
  3158. lua_getinfo(L, "&gt;S", &amp;ar);
  3159. printf("%d\n", ar.linedefined);
  3160. </pre>
  3161. <p>
  3162. Each character in the string <code>what</code>
  3163. selects some fields of the structure <code>ar</code> to be filled or
  3164. a value to be pushed on the stack:
  3165. <ul>
  3166. <li><b>'<code>n</code>':</b> fills in the field <code>name</code> and <code>namewhat</code>;
  3167. </li>
  3168. <li><b>'<code>S</code>':</b>
  3169. fills in the fields <code>source</code>, <code>short_src</code>,
  3170. <code>linedefined</code>, <code>lastlinedefined</code>, and <code>what</code>;
  3171. </li>
  3172. <li><b>'<code>l</code>':</b> fills in the field <code>currentline</code>;
  3173. </li>
  3174. <li><b>'<code>u</code>':</b> fills in the field <code>nups</code>;
  3175. </li>
  3176. <li><b>'<code>f</code>':</b>
  3177. pushes onto the stack the function that is
  3178. running at the given level;
  3179. </li>
  3180. <li><b>'<code>L</code>':</b>
  3181. pushes onto the stack a table whose indices are the
  3182. numbers of the lines that are valid on the function.
  3183. (A <em>valid line</em> is a line with some associated code,
  3184. that is, a line where you can put a break point.
  3185. Non-valid lines include empty lines and comments.)
  3186. </li>
  3187. </ul>
  3188. <p>
  3189. This function returns 0 on error
  3190. (for instance, an invalid option in <code>what</code>).
  3191. <hr><h3><a name="lua_getlocal"><code>lua_getlocal</code></a></h3><p>
  3192. <span class="apii">[-0, +(0|1), <em>-</em>]</span>
  3193. <pre>const char *lua_getlocal (lua_State *L, lua_Debug *ar, int n);</pre>
  3194. <p>
  3195. Gets information about a local variable of a given activation record.
  3196. The parameter <code>ar</code> must be a valid activation record that was
  3197. filled by a previous call to <a href="#lua_getstack"><code>lua_getstack</code></a> or
  3198. given as argument to a hook (see <a href="#lua_Hook"><code>lua_Hook</code></a>).
  3199. The index <code>n</code> selects which local variable to inspect
  3200. (1 is the first parameter or active local variable, and so on,
  3201. until the last active local variable).
  3202. <a href="#lua_getlocal"><code>lua_getlocal</code></a> pushes the variable's value onto the stack
  3203. and returns its name.
  3204. <p>
  3205. Variable names starting with '<code>(</code>' (open parentheses)
  3206. represent internal variables
  3207. (loop control variables, temporaries, and C&nbsp;function locals).
  3208. <p>
  3209. Returns <code>NULL</code> (and pushes nothing)
  3210. when the index is greater than
  3211. the number of active local variables.
  3212. <hr><h3><a name="lua_getstack"><code>lua_getstack</code></a></h3><p>
  3213. <span class="apii">[-0, +0, <em>-</em>]</span>
  3214. <pre>int lua_getstack (lua_State *L, int level, lua_Debug *ar);</pre>
  3215. <p>
  3216. Get information about the interpreter runtime stack.
  3217. <p>
  3218. This function fills parts of a <a href="#lua_Debug"><code>lua_Debug</code></a> structure with
  3219. an identification of the <em>activation record</em>
  3220. of the function executing at a given level.
  3221. Level&nbsp;0 is the current running function,
  3222. whereas level <em>n+1</em> is the function that has called level <em>n</em>.
  3223. When there are no errors, <a href="#lua_getstack"><code>lua_getstack</code></a> returns 1;
  3224. when called with a level greater than the stack depth,
  3225. it returns 0.
  3226. <hr><h3><a name="lua_getupvalue"><code>lua_getupvalue</code></a></h3><p>
  3227. <span class="apii">[-0, +(0|1), <em>-</em>]</span>
  3228. <pre>const char *lua_getupvalue (lua_State *L, int funcindex, int n);</pre>
  3229. <p>
  3230. Gets information about a closure's upvalue.
  3231. (For Lua functions,
  3232. upvalues are the external local variables that the function uses,
  3233. and that are consequently included in its closure.)
  3234. <a href="#lua_getupvalue"><code>lua_getupvalue</code></a> gets the index <code>n</code> of an upvalue,
  3235. pushes the upvalue's value onto the stack,
  3236. and returns its name.
  3237. <code>funcindex</code> points to the closure in the stack.
  3238. (Upvalues have no particular order,
  3239. as they are active through the whole function.
  3240. So, they are numbered in an arbitrary order.)
  3241. <p>
  3242. Returns <code>NULL</code> (and pushes nothing)
  3243. when the index is greater than the number of upvalues.
  3244. For C&nbsp;functions, this function uses the empty string <code>""</code>
  3245. as a name for all upvalues.
  3246. <hr><h3><a name="lua_Hook"><code>lua_Hook</code></a></h3>
  3247. <pre>typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);</pre>
  3248. <p>
  3249. Type for debugging hook functions.
  3250. <p>
  3251. Whenever a hook is called, its <code>ar</code> argument has its field
  3252. <code>event</code> set to the specific event that triggered the hook.
  3253. Lua identifies these events with the following constants:
  3254. <a name="pdf-LUA_HOOKCALL"><code>LUA_HOOKCALL</code></a>, <a name="pdf-LUA_HOOKRET"><code>LUA_HOOKRET</code></a>,
  3255. <a name="pdf-LUA_HOOKTAILRET"><code>LUA_HOOKTAILRET</code></a>, <a name="pdf-LUA_HOOKLINE"><code>LUA_HOOKLINE</code></a>,
  3256. and <a name="pdf-LUA_HOOKCOUNT"><code>LUA_HOOKCOUNT</code></a>.
  3257. Moreover, for line events, the field <code>currentline</code> is also set.
  3258. To get the value of any other field in <code>ar</code>,
  3259. the hook must call <a href="#lua_getinfo"><code>lua_getinfo</code></a>.
  3260. For return events, <code>event</code> can be <code>LUA_HOOKRET</code>,
  3261. the normal value, or <code>LUA_HOOKTAILRET</code>.
  3262. In the latter case, Lua is simulating a return from
  3263. a function that did a tail call;
  3264. in this case, it is useless to call <a href="#lua_getinfo"><code>lua_getinfo</code></a>.
  3265. <p>
  3266. While Lua is running a hook, it disables other calls to hooks.
  3267. Therefore, if a hook calls back Lua to execute a function or a chunk,
  3268. this execution occurs without any calls to hooks.
  3269. <hr><h3><a name="lua_sethook"><code>lua_sethook</code></a></h3><p>
  3270. <span class="apii">[-0, +0, <em>-</em>]</span>
  3271. <pre>int lua_sethook (lua_State *L, lua_Hook f, int mask, int count);</pre>
  3272. <p>
  3273. Sets the debugging hook function.
  3274. <p>
  3275. Argument <code>f</code> is the hook function.
  3276. <code>mask</code> specifies on which events the hook will be called:
  3277. it is formed by a bitwise or of the constants
  3278. <a name="pdf-LUA_MASKCALL"><code>LUA_MASKCALL</code></a>,
  3279. <a name="pdf-LUA_MASKRET"><code>LUA_MASKRET</code></a>,
  3280. <a name="pdf-LUA_MASKLINE"><code>LUA_MASKLINE</code></a>,
  3281. and <a name="pdf-LUA_MASKCOUNT"><code>LUA_MASKCOUNT</code></a>.
  3282. The <code>count</code> argument is only meaningful when the mask
  3283. includes <code>LUA_MASKCOUNT</code>.
  3284. For each event, the hook is called as explained below:
  3285. <ul>
  3286. <li><b>The call hook:</b> is called when the interpreter calls a function.
  3287. The hook is called just after Lua enters the new function,
  3288. before the function gets its arguments.
  3289. </li>
  3290. <li><b>The return hook:</b> is called when the interpreter returns from a function.
  3291. The hook is called just before Lua leaves the function.
  3292. You have no access to the values to be returned by the function.
  3293. </li>
  3294. <li><b>The line hook:</b> is called when the interpreter is about to
  3295. start the execution of a new line of code,
  3296. or when it jumps back in the code (even to the same line).
  3297. (This event only happens while Lua is executing a Lua function.)
  3298. </li>
  3299. <li><b>The count hook:</b> is called after the interpreter executes every
  3300. <code>count</code> instructions.
  3301. (This event only happens while Lua is executing a Lua function.)
  3302. </li>
  3303. </ul>
  3304. <p>
  3305. A hook is disabled by setting <code>mask</code> to zero.
  3306. <hr><h3><a name="lua_setlocal"><code>lua_setlocal</code></a></h3><p>
  3307. <span class="apii">[-(0|1), +0, <em>-</em>]</span>
  3308. <pre>const char *lua_setlocal (lua_State *L, lua_Debug *ar, int n);</pre>
  3309. <p>
  3310. Sets the value of a local variable of a given activation record.
  3311. Parameters <code>ar</code> and <code>n</code> are as in <a href="#lua_getlocal"><code>lua_getlocal</code></a>
  3312. (see <a href="#lua_getlocal"><code>lua_getlocal</code></a>).
  3313. <a href="#lua_setlocal"><code>lua_setlocal</code></a> assigns the value at the top of the stack
  3314. to the variable and returns its name.
  3315. It also pops the value from the stack.
  3316. <p>
  3317. Returns <code>NULL</code> (and pops nothing)
  3318. when the index is greater than
  3319. the number of active local variables.
  3320. <hr><h3><a name="lua_setupvalue"><code>lua_setupvalue</code></a></h3><p>
  3321. <span class="apii">[-(0|1), +0, <em>-</em>]</span>
  3322. <pre>const char *lua_setupvalue (lua_State *L, int funcindex, int n);</pre>
  3323. <p>
  3324. Sets the value of a closure's upvalue.
  3325. It assigns the value at the top of the stack
  3326. to the upvalue and returns its name.
  3327. It also pops the value from the stack.
  3328. Parameters <code>funcindex</code> and <code>n</code> are as in the <a href="#lua_getupvalue"><code>lua_getupvalue</code></a>
  3329. (see <a href="#lua_getupvalue"><code>lua_getupvalue</code></a>).
  3330. <p>
  3331. Returns <code>NULL</code> (and pops nothing)
  3332. when the index is greater than the number of upvalues.
  3333. <h1>4 - <a name="4">The Auxiliary Library</a></h1>
  3334. <p>
  3335. The <em>auxiliary library</em> provides several convenient functions
  3336. to interface C with Lua.
  3337. While the basic API provides the primitive functions for all
  3338. interactions between C and Lua,
  3339. the auxiliary library provides higher-level functions for some
  3340. common tasks.
  3341. <p>
  3342. All functions from the auxiliary library
  3343. are defined in header file <code>lauxlib.h</code> and
  3344. have a prefix <code>luaL_</code>.
  3345. <p>
  3346. All functions in the auxiliary library are built on
  3347. top of the basic API,
  3348. and so they provide nothing that cannot be done with this API.
  3349. <p>
  3350. Several functions in the auxiliary library are used to
  3351. check C&nbsp;function arguments.
  3352. Their names are always <code>luaL_check*</code> or <code>luaL_opt*</code>.
  3353. All of these functions throw an error if the check is not satisfied.
  3354. Because the error message is formatted for arguments
  3355. (e.g., "<code>bad argument #1</code>"),
  3356. you should not use these functions for other stack values.
  3357. <h2>4.1 - <a name="4.1">Functions and Types</a></h2>
  3358. <p>
  3359. Here we list all functions and types from the auxiliary library
  3360. in alphabetical order.
  3361. <hr><h3><a name="luaL_addchar"><code>luaL_addchar</code></a></h3><p>
  3362. <span class="apii">[-0, +0, <em>m</em>]</span>
  3363. <pre>void luaL_addchar (luaL_Buffer *B, char c);</pre>
  3364. <p>
  3365. Adds the character <code>c</code> to the buffer <code>B</code>
  3366. (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
  3367. <hr><h3><a name="luaL_addlstring"><code>luaL_addlstring</code></a></h3><p>
  3368. <span class="apii">[-0, +0, <em>m</em>]</span>
  3369. <pre>void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l);</pre>
  3370. <p>
  3371. Adds the string pointed to by <code>s</code> with length <code>l</code> to
  3372. the buffer <code>B</code>
  3373. (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
  3374. The string may contain embedded zeros.
  3375. <hr><h3><a name="luaL_addsize"><code>luaL_addsize</code></a></h3><p>
  3376. <span class="apii">[-0, +0, <em>m</em>]</span>
  3377. <pre>void luaL_addsize (luaL_Buffer *B, size_t n);</pre>
  3378. <p>
  3379. Adds to the buffer <code>B</code> (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>)
  3380. a string of length <code>n</code> previously copied to the
  3381. buffer area (see <a href="#luaL_prepbuffer"><code>luaL_prepbuffer</code></a>).
  3382. <hr><h3><a name="luaL_addstring"><code>luaL_addstring</code></a></h3><p>
  3383. <span class="apii">[-0, +0, <em>m</em>]</span>
  3384. <pre>void luaL_addstring (luaL_Buffer *B, const char *s);</pre>
  3385. <p>
  3386. Adds the zero-terminated string pointed to by <code>s</code>
  3387. to the buffer <code>B</code>
  3388. (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
  3389. The string may not contain embedded zeros.
  3390. <hr><h3><a name="luaL_addvalue"><code>luaL_addvalue</code></a></h3><p>
  3391. <span class="apii">[-1, +0, <em>m</em>]</span>
  3392. <pre>void luaL_addvalue (luaL_Buffer *B);</pre>
  3393. <p>
  3394. Adds the value at the top of the stack
  3395. to the buffer <code>B</code>
  3396. (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
  3397. Pops the value.
  3398. <p>
  3399. This is the only function on string buffers that can (and must)
  3400. be called with an extra element on the stack,
  3401. which is the value to be added to the buffer.
  3402. <hr><h3><a name="luaL_argcheck"><code>luaL_argcheck</code></a></h3><p>
  3403. <span class="apii">[-0, +0, <em>v</em>]</span>
  3404. <pre>void luaL_argcheck (lua_State *L,
  3405. int cond,
  3406. int narg,
  3407. const char *extramsg);</pre>
  3408. <p>
  3409. Checks whether <code>cond</code> is true.
  3410. If not, raises an error with the following message,
  3411. where <code>func</code> is retrieved from the call stack:
  3412. <pre>
  3413. bad argument #&lt;narg&gt; to &lt;func&gt; (&lt;extramsg&gt;)
  3414. </pre>
  3415. <hr><h3><a name="luaL_argerror"><code>luaL_argerror</code></a></h3><p>
  3416. <span class="apii">[-0, +0, <em>v</em>]</span>
  3417. <pre>int luaL_argerror (lua_State *L, int narg, const char *extramsg);</pre>
  3418. <p>
  3419. Raises an error with the following message,
  3420. where <code>func</code> is retrieved from the call stack:
  3421. <pre>
  3422. bad argument #&lt;narg&gt; to &lt;func&gt; (&lt;extramsg&gt;)
  3423. </pre>
  3424. <p>
  3425. This function never returns,
  3426. but it is an idiom to use it in C&nbsp;functions
  3427. as <code>return luaL_argerror(<em>args</em>)</code>.
  3428. <hr><h3><a name="luaL_Buffer"><code>luaL_Buffer</code></a></h3>
  3429. <pre>typedef struct luaL_Buffer luaL_Buffer;</pre>
  3430. <p>
  3431. Type for a <em>string buffer</em>.
  3432. <p>
  3433. A string buffer allows C&nbsp;code to build Lua strings piecemeal.
  3434. Its pattern of use is as follows:
  3435. <ul>
  3436. <li>First you declare a variable <code>b</code> of type <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>.</li>
  3437. <li>Then you initialize it with a call <code>luaL_buffinit(L, &amp;b)</code>.</li>
  3438. <li>
  3439. Then you add string pieces to the buffer calling any of
  3440. the <code>luaL_add*</code> functions.
  3441. </li>
  3442. <li>
  3443. You finish by calling <code>luaL_pushresult(&amp;b)</code>.
  3444. This call leaves the final string on the top of the stack.
  3445. </li>
  3446. </ul>
  3447. <p>
  3448. During its normal operation,
  3449. a string buffer uses a variable number of stack slots.
  3450. So, while using a buffer, you cannot assume that you know where
  3451. the top of the stack is.
  3452. You can use the stack between successive calls to buffer operations
  3453. as long as that use is balanced;
  3454. that is,
  3455. when you call a buffer operation,
  3456. the stack is at the same level
  3457. it was immediately after the previous buffer operation.
  3458. (The only exception to this rule is <a href="#luaL_addvalue"><code>luaL_addvalue</code></a>.)
  3459. After calling <a href="#luaL_pushresult"><code>luaL_pushresult</code></a> the stack is back to its
  3460. level when the buffer was initialized,
  3461. plus the final string on its top.
  3462. <hr><h3><a name="luaL_buffinit"><code>luaL_buffinit</code></a></h3><p>
  3463. <span class="apii">[-0, +0, <em>-</em>]</span>
  3464. <pre>void luaL_buffinit (lua_State *L, luaL_Buffer *B);</pre>
  3465. <p>
  3466. Initializes a buffer <code>B</code>.
  3467. This function does not allocate any space;
  3468. the buffer must be declared as a variable
  3469. (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
  3470. <hr><h3><a name="luaL_callmeta"><code>luaL_callmeta</code></a></h3><p>
  3471. <span class="apii">[-0, +(0|1), <em>e</em>]</span>
  3472. <pre>int luaL_callmeta (lua_State *L, int obj, const char *e);</pre>
  3473. <p>
  3474. Calls a metamethod.
  3475. <p>
  3476. If the object at index <code>obj</code> has a metatable and this
  3477. metatable has a field <code>e</code>,
  3478. this function calls this field and passes the object as its only argument.
  3479. In this case this function returns 1 and pushes onto the
  3480. stack the value returned by the call.
  3481. If there is no metatable or no metamethod,
  3482. this function returns 0 (without pushing any value on the stack).
  3483. <hr><h3><a name="luaL_checkany"><code>luaL_checkany</code></a></h3><p>
  3484. <span class="apii">[-0, +0, <em>v</em>]</span>
  3485. <pre>void luaL_checkany (lua_State *L, int narg);</pre>
  3486. <p>
  3487. Checks whether the function has an argument
  3488. of any type (including <b>nil</b>) at position <code>narg</code>.
  3489. <hr><h3><a name="luaL_checkint"><code>luaL_checkint</code></a></h3><p>
  3490. <span class="apii">[-0, +0, <em>v</em>]</span>
  3491. <pre>int luaL_checkint (lua_State *L, int narg);</pre>
  3492. <p>
  3493. Checks whether the function argument <code>narg</code> is a number
  3494. and returns this number cast to an <code>int</code>.
  3495. <hr><h3><a name="luaL_checkinteger"><code>luaL_checkinteger</code></a></h3><p>
  3496. <span class="apii">[-0, +0, <em>v</em>]</span>
  3497. <pre>lua_Integer luaL_checkinteger (lua_State *L, int narg);</pre>
  3498. <p>
  3499. Checks whether the function argument <code>narg</code> is a number
  3500. and returns this number cast to a <a href="#lua_Integer"><code>lua_Integer</code></a>.
  3501. <hr><h3><a name="luaL_checklong"><code>luaL_checklong</code></a></h3><p>
  3502. <span class="apii">[-0, +0, <em>v</em>]</span>
  3503. <pre>long luaL_checklong (lua_State *L, int narg);</pre>
  3504. <p>
  3505. Checks whether the function argument <code>narg</code> is a number
  3506. and returns this number cast to a <code>long</code>.
  3507. <hr><h3><a name="luaL_checklstring"><code>luaL_checklstring</code></a></h3><p>
  3508. <span class="apii">[-0, +0, <em>v</em>]</span>
  3509. <pre>const char *luaL_checklstring (lua_State *L, int narg, size_t *l);</pre>
  3510. <p>
  3511. Checks whether the function argument <code>narg</code> is a string
  3512. and returns this string;
  3513. if <code>l</code> is not <code>NULL</code> fills <code>*l</code>
  3514. with the string's length.
  3515. <p>
  3516. This function uses <a href="#lua_tolstring"><code>lua_tolstring</code></a> to get its result,
  3517. so all conversions and caveats of that function apply here.
  3518. <hr><h3><a name="luaL_checknumber"><code>luaL_checknumber</code></a></h3><p>
  3519. <span class="apii">[-0, +0, <em>v</em>]</span>
  3520. <pre>lua_Number luaL_checknumber (lua_State *L, int narg);</pre>
  3521. <p>
  3522. Checks whether the function argument <code>narg</code> is a number
  3523. and returns this number.
  3524. <hr><h3><a name="luaL_checkoption"><code>luaL_checkoption</code></a></h3><p>
  3525. <span class="apii">[-0, +0, <em>v</em>]</span>
  3526. <pre>int luaL_checkoption (lua_State *L,
  3527. int narg,
  3528. const char *def,
  3529. const char *const lst[]);</pre>
  3530. <p>
  3531. Checks whether the function argument <code>narg</code> is a string and
  3532. searches for this string in the array <code>lst</code>
  3533. (which must be NULL-terminated).
  3534. Returns the index in the array where the string was found.
  3535. Raises an error if the argument is not a string or
  3536. if the string cannot be found.
  3537. <p>
  3538. If <code>def</code> is not <code>NULL</code>,
  3539. the function uses <code>def</code> as a default value when
  3540. there is no argument <code>narg</code> or if this argument is <b>nil</b>.
  3541. <p>
  3542. This is a useful function for mapping strings to C&nbsp;enums.
  3543. (The usual convention in Lua libraries is
  3544. to use strings instead of numbers to select options.)
  3545. <hr><h3><a name="luaL_checkstack"><code>luaL_checkstack</code></a></h3><p>
  3546. <span class="apii">[-0, +0, <em>v</em>]</span>
  3547. <pre>void luaL_checkstack (lua_State *L, int sz, const char *msg);</pre>
  3548. <p>
  3549. Grows the stack size to <code>top + sz</code> elements,
  3550. raising an error if the stack cannot grow to that size.
  3551. <code>msg</code> is an additional text to go into the error message.
  3552. <hr><h3><a name="luaL_checkstring"><code>luaL_checkstring</code></a></h3><p>
  3553. <span class="apii">[-0, +0, <em>v</em>]</span>
  3554. <pre>const char *luaL_checkstring (lua_State *L, int narg);</pre>
  3555. <p>
  3556. Checks whether the function argument <code>narg</code> is a string
  3557. and returns this string.
  3558. <p>
  3559. This function uses <a href="#lua_tolstring"><code>lua_tolstring</code></a> to get its result,
  3560. so all conversions and caveats of that function apply here.
  3561. <hr><h3><a name="luaL_checktype"><code>luaL_checktype</code></a></h3><p>
  3562. <span class="apii">[-0, +0, <em>v</em>]</span>
  3563. <pre>void luaL_checktype (lua_State *L, int narg, int t);</pre>
  3564. <p>
  3565. Checks whether the function argument <code>narg</code> has type <code>t</code>.
  3566. See <a href="#lua_type"><code>lua_type</code></a> for the encoding of types for <code>t</code>.
  3567. <hr><h3><a name="luaL_checkudata"><code>luaL_checkudata</code></a></h3><p>
  3568. <span class="apii">[-0, +0, <em>v</em>]</span>
  3569. <pre>void *luaL_checkudata (lua_State *L, int narg, const char *tname);</pre>
  3570. <p>
  3571. Checks whether the function argument <code>narg</code> is a userdata
  3572. of the type <code>tname</code> (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>).
  3573. <hr><h3><a name="luaL_dofile"><code>luaL_dofile</code></a></h3><p>
  3574. <span class="apii">[-0, +?, <em>m</em>]</span>
  3575. <pre>int luaL_dofile (lua_State *L, const char *filename);</pre>
  3576. <p>
  3577. Loads and runs the given file.
  3578. It is defined as the following macro:
  3579. <pre>
  3580. (luaL_loadfile(L, filename) || lua_pcall(L, 0, LUA_MULTRET, 0))
  3581. </pre><p>
  3582. It returns 0 if there are no errors
  3583. or 1 in case of errors.
  3584. <hr><h3><a name="luaL_dostring"><code>luaL_dostring</code></a></h3><p>
  3585. <span class="apii">[-0, +?, <em>m</em>]</span>
  3586. <pre>int luaL_dostring (lua_State *L, const char *str);</pre>
  3587. <p>
  3588. Loads and runs the given string.
  3589. It is defined as the following macro:
  3590. <pre>
  3591. (luaL_loadstring(L, str) || lua_pcall(L, 0, LUA_MULTRET, 0))
  3592. </pre><p>
  3593. It returns 0 if there are no errors
  3594. or 1 in case of errors.
  3595. <hr><h3><a name="luaL_error"><code>luaL_error</code></a></h3><p>
  3596. <span class="apii">[-0, +0, <em>v</em>]</span>
  3597. <pre>int luaL_error (lua_State *L, const char *fmt, ...);</pre>
  3598. <p>
  3599. Raises an error.
  3600. The error message format is given by <code>fmt</code>
  3601. plus any extra arguments,
  3602. following the same rules of <a href="#lua_pushfstring"><code>lua_pushfstring</code></a>.
  3603. It also adds at the beginning of the message the file name and
  3604. the line number where the error occurred,
  3605. if this information is available.
  3606. <p>
  3607. This function never returns,
  3608. but it is an idiom to use it in C&nbsp;functions
  3609. as <code>return luaL_error(<em>args</em>)</code>.
  3610. <hr><h3><a name="luaL_getmetafield"><code>luaL_getmetafield</code></a></h3><p>
  3611. <span class="apii">[-0, +(0|1), <em>m</em>]</span>
  3612. <pre>int luaL_getmetafield (lua_State *L, int obj, const char *e);</pre>
  3613. <p>
  3614. Pushes onto the stack the field <code>e</code> from the metatable
  3615. of the object at index <code>obj</code>.
  3616. If the object does not have a metatable,
  3617. or if the metatable does not have this field,
  3618. returns 0 and pushes nothing.
  3619. <hr><h3><a name="luaL_getmetatable"><code>luaL_getmetatable</code></a></h3><p>
  3620. <span class="apii">[-0, +1, <em>-</em>]</span>
  3621. <pre>void luaL_getmetatable (lua_State *L, const char *tname);</pre>
  3622. <p>
  3623. Pushes onto the stack the metatable associated with name <code>tname</code>
  3624. in the registry (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>).
  3625. <hr><h3><a name="luaL_gsub"><code>luaL_gsub</code></a></h3><p>
  3626. <span class="apii">[-0, +1, <em>m</em>]</span>
  3627. <pre>const char *luaL_gsub (lua_State *L,
  3628. const char *s,
  3629. const char *p,
  3630. const char *r);</pre>
  3631. <p>
  3632. Creates a copy of string <code>s</code> by replacing
  3633. any occurrence of the string <code>p</code>
  3634. with the string <code>r</code>.
  3635. Pushes the resulting string on the stack and returns it.
  3636. <hr><h3><a name="luaL_loadbuffer"><code>luaL_loadbuffer</code></a></h3><p>
  3637. <span class="apii">[-0, +1, <em>m</em>]</span>
  3638. <pre>int luaL_loadbuffer (lua_State *L,
  3639. const char *buff,
  3640. size_t sz,
  3641. const char *name);</pre>
  3642. <p>
  3643. Loads a buffer as a Lua chunk.
  3644. This function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in the
  3645. buffer pointed to by <code>buff</code> with size <code>sz</code>.
  3646. <p>
  3647. This function returns the same results as <a href="#lua_load"><code>lua_load</code></a>.
  3648. <code>name</code> is the chunk name,
  3649. used for debug information and error messages.
  3650. <hr><h3><a name="luaL_loadfile"><code>luaL_loadfile</code></a></h3><p>
  3651. <span class="apii">[-0, +1, <em>m</em>]</span>
  3652. <pre>int luaL_loadfile (lua_State *L, const char *filename);</pre>
  3653. <p>
  3654. Loads a file as a Lua chunk.
  3655. This function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in the file
  3656. named <code>filename</code>.
  3657. If <code>filename</code> is <code>NULL</code>,
  3658. then it loads from the standard input.
  3659. The first line in the file is ignored if it starts with a <code>#</code>.
  3660. <p>
  3661. This function returns the same results as <a href="#lua_load"><code>lua_load</code></a>,
  3662. but it has an extra error code <a name="pdf-LUA_ERRFILE"><code>LUA_ERRFILE</code></a>
  3663. if it cannot open/read the file.
  3664. <p>
  3665. As <a href="#lua_load"><code>lua_load</code></a>, this function only loads the chunk;
  3666. it does not run it.
  3667. <hr><h3><a name="luaL_loadstring"><code>luaL_loadstring</code></a></h3><p>
  3668. <span class="apii">[-0, +1, <em>m</em>]</span>
  3669. <pre>int luaL_loadstring (lua_State *L, const char *s);</pre>
  3670. <p>
  3671. Loads a string as a Lua chunk.
  3672. This function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in
  3673. the zero-terminated string <code>s</code>.
  3674. <p>
  3675. This function returns the same results as <a href="#lua_load"><code>lua_load</code></a>.
  3676. <p>
  3677. Also as <a href="#lua_load"><code>lua_load</code></a>, this function only loads the chunk;
  3678. it does not run it.
  3679. <hr><h3><a name="luaL_newmetatable"><code>luaL_newmetatable</code></a></h3><p>
  3680. <span class="apii">[-0, +1, <em>m</em>]</span>
  3681. <pre>int luaL_newmetatable (lua_State *L, const char *tname);</pre>
  3682. <p>
  3683. If the registry already has the key <code>tname</code>,
  3684. returns 0.
  3685. Otherwise,
  3686. creates a new table to be used as a metatable for userdata,
  3687. adds it to the registry with key <code>tname</code>,
  3688. and returns 1.
  3689. <p>
  3690. In both cases pushes onto the stack the final value associated
  3691. with <code>tname</code> in the registry.
  3692. <hr><h3><a name="luaL_newstate"><code>luaL_newstate</code></a></h3><p>
  3693. <span class="apii">[-0, +0, <em>-</em>]</span>
  3694. <pre>lua_State *luaL_newstate (void);</pre>
  3695. <p>
  3696. Creates a new Lua state.
  3697. It calls <a href="#lua_newstate"><code>lua_newstate</code></a> with an
  3698. allocator based on the standard&nbsp;C <code>realloc</code> function
  3699. and then sets a panic function (see <a href="#lua_atpanic"><code>lua_atpanic</code></a>) that prints
  3700. an error message to the standard error output in case of fatal
  3701. errors.
  3702. <p>
  3703. Returns the new state,
  3704. or <code>NULL</code> if there is a memory allocation error.
  3705. <hr><h3><a name="luaL_openlibs"><code>luaL_openlibs</code></a></h3><p>
  3706. <span class="apii">[-0, +0, <em>m</em>]</span>
  3707. <pre>void luaL_openlibs (lua_State *L);</pre>
  3708. <p>
  3709. Opens all standard Lua libraries into the given state.
  3710. <hr><h3><a name="luaL_optint"><code>luaL_optint</code></a></h3><p>
  3711. <span class="apii">[-0, +0, <em>v</em>]</span>
  3712. <pre>int luaL_optint (lua_State *L, int narg, int d);</pre>
  3713. <p>
  3714. If the function argument <code>narg</code> is a number,
  3715. returns this number cast to an <code>int</code>.
  3716. If this argument is absent or is <b>nil</b>,
  3717. returns <code>d</code>.
  3718. Otherwise, raises an error.
  3719. <hr><h3><a name="luaL_optinteger"><code>luaL_optinteger</code></a></h3><p>
  3720. <span class="apii">[-0, +0, <em>v</em>]</span>
  3721. <pre>lua_Integer luaL_optinteger (lua_State *L,
  3722. int narg,
  3723. lua_Integer d);</pre>
  3724. <p>
  3725. If the function argument <code>narg</code> is a number,
  3726. returns this number cast to a <a href="#lua_Integer"><code>lua_Integer</code></a>.
  3727. If this argument is absent or is <b>nil</b>,
  3728. returns <code>d</code>.
  3729. Otherwise, raises an error.
  3730. <hr><h3><a name="luaL_optlong"><code>luaL_optlong</code></a></h3><p>
  3731. <span class="apii">[-0, +0, <em>v</em>]</span>
  3732. <pre>long luaL_optlong (lua_State *L, int narg, long d);</pre>
  3733. <p>
  3734. If the function argument <code>narg</code> is a number,
  3735. returns this number cast to a <code>long</code>.
  3736. If this argument is absent or is <b>nil</b>,
  3737. returns <code>d</code>.
  3738. Otherwise, raises an error.
  3739. <hr><h3><a name="luaL_optlstring"><code>luaL_optlstring</code></a></h3><p>
  3740. <span class="apii">[-0, +0, <em>v</em>]</span>
  3741. <pre>const char *luaL_optlstring (lua_State *L,
  3742. int narg,
  3743. const char *d,
  3744. size_t *l);</pre>
  3745. <p>
  3746. If the function argument <code>narg</code> is a string,
  3747. returns this string.
  3748. If this argument is absent or is <b>nil</b>,
  3749. returns <code>d</code>.
  3750. Otherwise, raises an error.
  3751. <p>
  3752. If <code>l</code> is not <code>NULL</code>,
  3753. fills the position <code>*l</code> with the results's length.
  3754. <hr><h3><a name="luaL_optnumber"><code>luaL_optnumber</code></a></h3><p>
  3755. <span class="apii">[-0, +0, <em>v</em>]</span>
  3756. <pre>lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number d);</pre>
  3757. <p>
  3758. If the function argument <code>narg</code> is a number,
  3759. returns this number.
  3760. If this argument is absent or is <b>nil</b>,
  3761. returns <code>d</code>.
  3762. Otherwise, raises an error.
  3763. <hr><h3><a name="luaL_optstring"><code>luaL_optstring</code></a></h3><p>
  3764. <span class="apii">[-0, +0, <em>v</em>]</span>
  3765. <pre>const char *luaL_optstring (lua_State *L,
  3766. int narg,
  3767. const char *d);</pre>
  3768. <p>
  3769. If the function argument <code>narg</code> is a string,
  3770. returns this string.
  3771. If this argument is absent or is <b>nil</b>,
  3772. returns <code>d</code>.
  3773. Otherwise, raises an error.
  3774. <hr><h3><a name="luaL_prepbuffer"><code>luaL_prepbuffer</code></a></h3><p>
  3775. <span class="apii">[-0, +0, <em>-</em>]</span>
  3776. <pre>char *luaL_prepbuffer (luaL_Buffer *B);</pre>
  3777. <p>
  3778. Returns an address to a space of size <a name="pdf-LUAL_BUFFERSIZE"><code>LUAL_BUFFERSIZE</code></a>
  3779. where you can copy a string to be added to buffer <code>B</code>
  3780. (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
  3781. After copying the string into this space you must call
  3782. <a href="#luaL_addsize"><code>luaL_addsize</code></a> with the size of the string to actually add
  3783. it to the buffer.
  3784. <hr><h3><a name="luaL_pushresult"><code>luaL_pushresult</code></a></h3><p>
  3785. <span class="apii">[-?, +1, <em>m</em>]</span>
  3786. <pre>void luaL_pushresult (luaL_Buffer *B);</pre>
  3787. <p>
  3788. Finishes the use of buffer <code>B</code> leaving the final string on
  3789. the top of the stack.
  3790. <hr><h3><a name="luaL_ref"><code>luaL_ref</code></a></h3><p>
  3791. <span class="apii">[-1, +0, <em>m</em>]</span>
  3792. <pre>int luaL_ref (lua_State *L, int t);</pre>
  3793. <p>
  3794. Creates and returns a <em>reference</em>,
  3795. in the table at index <code>t</code>,
  3796. for the object at the top of the stack (and pops the object).
  3797. <p>
  3798. A reference is a unique integer key.
  3799. As long as you do not manually add integer keys into table <code>t</code>,
  3800. <a href="#luaL_ref"><code>luaL_ref</code></a> ensures the uniqueness of the key it returns.
  3801. You can retrieve an object referred by reference <code>r</code>
  3802. by calling <code>lua_rawgeti(L, t, r)</code>.
  3803. Function <a href="#luaL_unref"><code>luaL_unref</code></a> frees a reference and its associated object.
  3804. <p>
  3805. If the object at the top of the stack is <b>nil</b>,
  3806. <a href="#luaL_ref"><code>luaL_ref</code></a> returns the constant <a name="pdf-LUA_REFNIL"><code>LUA_REFNIL</code></a>.
  3807. The constant <a name="pdf-LUA_NOREF"><code>LUA_NOREF</code></a> is guaranteed to be different
  3808. from any reference returned by <a href="#luaL_ref"><code>luaL_ref</code></a>.
  3809. <hr><h3><a name="luaL_Reg"><code>luaL_Reg</code></a></h3>
  3810. <pre>typedef struct luaL_Reg {
  3811. const char *name;
  3812. lua_CFunction func;
  3813. } luaL_Reg;</pre>
  3814. <p>
  3815. Type for arrays of functions to be registered by
  3816. <a href="#luaL_register"><code>luaL_register</code></a>.
  3817. <code>name</code> is the function name and <code>func</code> is a pointer to
  3818. the function.
  3819. Any array of <a href="#luaL_Reg"><code>luaL_Reg</code></a> must end with an sentinel entry
  3820. in which both <code>name</code> and <code>func</code> are <code>NULL</code>.
  3821. <hr><h3><a name="luaL_register"><code>luaL_register</code></a></h3><p>
  3822. <span class="apii">[-(0|1), +1, <em>m</em>]</span>
  3823. <pre>void luaL_register (lua_State *L,
  3824. const char *libname,
  3825. const luaL_Reg *l);</pre>
  3826. <p>
  3827. Opens a library.
  3828. <p>
  3829. When called with <code>libname</code> equal to <code>NULL</code>,
  3830. it simply registers all functions in the list <code>l</code>
  3831. (see <a href="#luaL_Reg"><code>luaL_Reg</code></a>) into the table on the top of the stack.
  3832. <p>
  3833. When called with a non-null <code>libname</code>,
  3834. <code>luaL_register</code> creates a new table <code>t</code>,
  3835. sets it as the value of the global variable <code>libname</code>,
  3836. sets it as the value of <code>package.loaded[libname]</code>,
  3837. and registers on it all functions in the list <code>l</code>.
  3838. If there is a table in <code>package.loaded[libname]</code> or in
  3839. variable <code>libname</code>,
  3840. reuses this table instead of creating a new one.
  3841. <p>
  3842. In any case the function leaves the table
  3843. on the top of the stack.
  3844. <hr><h3><a name="luaL_typename"><code>luaL_typename</code></a></h3><p>
  3845. <span class="apii">[-0, +0, <em>-</em>]</span>
  3846. <pre>const char *luaL_typename (lua_State *L, int index);</pre>
  3847. <p>
  3848. Returns the name of the type of the value at the given index.
  3849. <hr><h3><a name="luaL_typerror"><code>luaL_typerror</code></a></h3><p>
  3850. <span class="apii">[-0, +0, <em>v</em>]</span>
  3851. <pre>int luaL_typerror (lua_State *L, int narg, const char *tname);</pre>
  3852. <p>
  3853. Generates an error with a message like the following:
  3854. <pre>
  3855. <em>location</em>: bad argument <em>narg</em> to '<em>func</em>' (<em>tname</em> expected, got <em>rt</em>)
  3856. </pre><p>
  3857. where <code><em>location</em></code> is produced by <a href="#luaL_where"><code>luaL_where</code></a>,
  3858. <code><em>func</em></code> is the name of the current function,
  3859. and <code><em>rt</em></code> is the type name of the actual argument.
  3860. <hr><h3><a name="luaL_unref"><code>luaL_unref</code></a></h3><p>
  3861. <span class="apii">[-0, +0, <em>-</em>]</span>
  3862. <pre>void luaL_unref (lua_State *L, int t, int ref);</pre>
  3863. <p>
  3864. Releases reference <code>ref</code> from the table at index <code>t</code>
  3865. (see <a href="#luaL_ref"><code>luaL_ref</code></a>).
  3866. The entry is removed from the table,
  3867. so that the referred object can be collected.
  3868. The reference <code>ref</code> is also freed to be used again.
  3869. <p>
  3870. If <code>ref</code> is <a href="#pdf-LUA_NOREF"><code>LUA_NOREF</code></a> or <a href="#pdf-LUA_REFNIL"><code>LUA_REFNIL</code></a>,
  3871. <a href="#luaL_unref"><code>luaL_unref</code></a> does nothing.
  3872. <hr><h3><a name="luaL_where"><code>luaL_where</code></a></h3><p>
  3873. <span class="apii">[-0, +1, <em>m</em>]</span>
  3874. <pre>void luaL_where (lua_State *L, int lvl);</pre>
  3875. <p>
  3876. Pushes onto the stack a string identifying the current position
  3877. of the control at level <code>lvl</code> in the call stack.
  3878. Typically this string has the following format:
  3879. <pre>
  3880. <em>chunkname</em>:<em>currentline</em>:
  3881. </pre><p>
  3882. Level&nbsp;0 is the running function,
  3883. level&nbsp;1 is the function that called the running function,
  3884. etc.
  3885. <p>
  3886. This function is used to build a prefix for error messages.
  3887. <h1>5 - <a name="5">Standard Libraries</a></h1>
  3888. <p>
  3889. The standard Lua libraries provide useful functions
  3890. that are implemented directly through the C&nbsp;API.
  3891. Some of these functions provide essential services to the language
  3892. (e.g., <a href="#pdf-type"><code>type</code></a> and <a href="#pdf-getmetatable"><code>getmetatable</code></a>);
  3893. others provide access to "outside" services (e.g., I/O);
  3894. and others could be implemented in Lua itself,
  3895. but are quite useful or have critical performance requirements that
  3896. deserve an implementation in C (e.g., <a href="#pdf-table.sort"><code>table.sort</code></a>).
  3897. <p>
  3898. All libraries are implemented through the official C&nbsp;API
  3899. and are provided as separate C&nbsp;modules.
  3900. Currently, Lua has the following standard libraries:
  3901. <ul>
  3902. <li>basic library, which includes the coroutine sub-library;</li>
  3903. <li>package library;</li>
  3904. <li>string manipulation;</li>
  3905. <li>table manipulation;</li>
  3906. <li>mathematical functions (sin, log, etc.);</li>
  3907. <li>input and output;</li>
  3908. <li>operating system facilities;</li>
  3909. <li>debug facilities.</li>
  3910. </ul><p>
  3911. Except for the basic and package libraries,
  3912. each library provides all its functions as fields of a global table
  3913. or as methods of its objects.
  3914. <p>
  3915. To have access to these libraries,
  3916. the C&nbsp;host program should call the <a href="#luaL_openlibs"><code>luaL_openlibs</code></a> function,
  3917. which opens all standard libraries.
  3918. Alternatively,
  3919. it can open them individually by calling
  3920. <a name="pdf-luaopen_base"><code>luaopen_base</code></a> (for the basic library),
  3921. <a name="pdf-luaopen_package"><code>luaopen_package</code></a> (for the package library),
  3922. <a name="pdf-luaopen_string"><code>luaopen_string</code></a> (for the string library),
  3923. <a name="pdf-luaopen_table"><code>luaopen_table</code></a> (for the table library),
  3924. <a name="pdf-luaopen_math"><code>luaopen_math</code></a> (for the mathematical library),
  3925. <a name="pdf-luaopen_io"><code>luaopen_io</code></a> (for the I/O library),
  3926. <a name="pdf-luaopen_os"><code>luaopen_os</code></a> (for the Operating System library),
  3927. and <a name="pdf-luaopen_debug"><code>luaopen_debug</code></a> (for the debug library).
  3928. These functions are declared in <a name="pdf-lualib.h"><code>lualib.h</code></a>
  3929. and should not be called directly:
  3930. you must call them like any other Lua C&nbsp;function,
  3931. e.g., by using <a href="#lua_call"><code>lua_call</code></a>.
  3932. <h2>5.1 - <a name="5.1">Basic Functions</a></h2>
  3933. <p>
  3934. The basic library provides some core functions to Lua.
  3935. If you do not include this library in your application,
  3936. you should check carefully whether you need to provide
  3937. implementations for some of its facilities.
  3938. <p>
  3939. <hr><h3><a name="pdf-assert"><code>assert (v [, message])</code></a></h3>
  3940. Issues an error when
  3941. the value of its argument <code>v</code> is false (i.e., <b>nil</b> or <b>false</b>);
  3942. otherwise, returns all its arguments.
  3943. <code>message</code> is an error message;
  3944. when absent, it defaults to "assertion failed!"
  3945. <p>
  3946. <hr><h3><a name="pdf-collectgarbage"><code>collectgarbage ([opt [, arg]])</code></a></h3>
  3947. <p>
  3948. This function is a generic interface to the garbage collector.
  3949. It performs different functions according to its first argument, <code>opt</code>:
  3950. <ul>
  3951. <li><b>"collect":</b>
  3952. performs a full garbage-collection cycle.
  3953. This is the default option.
  3954. </li>
  3955. <li><b>"stop":</b>
  3956. stops the garbage collector.
  3957. </li>
  3958. <li><b>"restart":</b>
  3959. restarts the garbage collector.
  3960. </li>
  3961. <li><b>"count":</b>
  3962. returns the total memory in use by Lua (in Kbytes).
  3963. </li>
  3964. <li><b>"step":</b>
  3965. performs a garbage-collection step.
  3966. The step "size" is controlled by <code>arg</code>
  3967. (larger values mean more steps) in a non-specified way.
  3968. If you want to control the step size
  3969. you must experimentally tune the value of <code>arg</code>.
  3970. Returns <b>true</b> if the step finished a collection cycle.
  3971. </li>
  3972. <li><b>"setpause":</b>
  3973. sets <code>arg</code> as the new value for the <em>pause</em> of
  3974. the collector (see <a href="#2.10">&sect;2.10</a>).
  3975. Returns the previous value for <em>pause</em>.
  3976. </li>
  3977. <li><b>"setstepmul":</b>
  3978. sets <code>arg</code> as the new value for the <em>step multiplier</em> of
  3979. the collector (see <a href="#2.10">&sect;2.10</a>).
  3980. Returns the previous value for <em>step</em>.
  3981. </li>
  3982. </ul>
  3983. <p>
  3984. <hr><h3><a name="pdf-dofile"><code>dofile ([filename])</code></a></h3>
  3985. Opens the named file and executes its contents as a Lua chunk.
  3986. When called without arguments,
  3987. <code>dofile</code> executes the contents of the standard input (<code>stdin</code>).
  3988. Returns all values returned by the chunk.
  3989. In case of errors, <code>dofile</code> propagates the error
  3990. to its caller (that is, <code>dofile</code> does not run in protected mode).
  3991. <p>
  3992. <hr><h3><a name="pdf-error"><code>error (message [, level])</code></a></h3>
  3993. Terminates the last protected function called
  3994. and returns <code>message</code> as the error message.
  3995. Function <code>error</code> never returns.
  3996. <p>
  3997. Usually, <code>error</code> adds some information about the error position
  3998. at the beginning of the message.
  3999. The <code>level</code> argument specifies how to get the error position.
  4000. With level&nbsp;1 (the default), the error position is where the
  4001. <code>error</code> function was called.
  4002. Level&nbsp;2 points the error to where the function
  4003. that called <code>error</code> was called; and so on.
  4004. Passing a level&nbsp;0 avoids the addition of error position information
  4005. to the message.
  4006. <p>
  4007. <hr><h3><a name="pdf-_G"><code>_G</code></a></h3>
  4008. A global variable (not a function) that
  4009. holds the global environment (that is, <code>_G._G = _G</code>).
  4010. Lua itself does not use this variable;
  4011. changing its value does not affect any environment,
  4012. nor vice-versa.
  4013. (Use <a href="#pdf-setfenv"><code>setfenv</code></a> to change environments.)
  4014. <p>
  4015. <hr><h3><a name="pdf-getfenv"><code>getfenv ([f])</code></a></h3>
  4016. Returns the current environment in use by the function.
  4017. <code>f</code> can be a Lua function or a number
  4018. that specifies the function at that stack level:
  4019. Level&nbsp;1 is the function calling <code>getfenv</code>.
  4020. If the given function is not a Lua function,
  4021. or if <code>f</code> is 0,
  4022. <code>getfenv</code> returns the global environment.
  4023. The default for <code>f</code> is 1.
  4024. <p>
  4025. <hr><h3><a name="pdf-getmetatable"><code>getmetatable (object)</code></a></h3>
  4026. <p>
  4027. If <code>object</code> does not have a metatable, returns <b>nil</b>.
  4028. Otherwise,
  4029. if the object's metatable has a <code>"__metatable"</code> field,
  4030. returns the associated value.
  4031. Otherwise, returns the metatable of the given object.
  4032. <p>
  4033. <hr><h3><a name="pdf-ipairs"><code>ipairs (t)</code></a></h3>
  4034. <p>
  4035. Returns three values: an iterator function, the table <code>t</code>, and 0,
  4036. so that the construction
  4037. <pre>
  4038. for i,v in ipairs(t) do <em>body</em> end
  4039. </pre><p>
  4040. will iterate over the pairs (<code>1,t[1]</code>), (<code>2,t[2]</code>), &middot;&middot;&middot;,
  4041. up to the first integer key absent from the table.
  4042. <p>
  4043. <hr><h3><a name="pdf-load"><code>load (func [, chunkname])</code></a></h3>
  4044. <p>
  4045. Loads a chunk using function <code>func</code> to get its pieces.
  4046. Each call to <code>func</code> must return a string that concatenates
  4047. with previous results.
  4048. A return of an empty string, <b>nil</b>, or no value signals the end of the chunk.
  4049. <p>
  4050. If there are no errors,
  4051. returns the compiled chunk as a function;
  4052. otherwise, returns <b>nil</b> plus the error message.
  4053. The environment of the returned function is the global environment.
  4054. <p>
  4055. <code>chunkname</code> is used as the chunk name for error messages
  4056. and debug information.
  4057. When absent,
  4058. it defaults to "<code>=(load)</code>".
  4059. <p>
  4060. <hr><h3><a name="pdf-loadfile"><code>loadfile ([filename])</code></a></h3>
  4061. <p>
  4062. Similar to <a href="#pdf-load"><code>load</code></a>,
  4063. but gets the chunk from file <code>filename</code>
  4064. or from the standard input,
  4065. if no file name is given.
  4066. <p>
  4067. <hr><h3><a name="pdf-loadstring"><code>loadstring (string [, chunkname])</code></a></h3>
  4068. <p>
  4069. Similar to <a href="#pdf-load"><code>load</code></a>,
  4070. but gets the chunk from the given string.
  4071. <p>
  4072. To load and run a given string, use the idiom
  4073. <pre>
  4074. assert(loadstring(s))()
  4075. </pre>
  4076. <p>
  4077. When absent,
  4078. <code>chunkname</code> defaults to the given string.
  4079. <p>
  4080. <hr><h3><a name="pdf-next"><code>next (table [, index])</code></a></h3>
  4081. <p>
  4082. Allows a program to traverse all fields of a table.
  4083. Its first argument is a table and its second argument
  4084. is an index in this table.
  4085. <code>next</code> returns the next index of the table
  4086. and its associated value.
  4087. When called with <b>nil</b> as its second argument,
  4088. <code>next</code> returns an initial index
  4089. and its associated value.
  4090. When called with the last index,
  4091. or with <b>nil</b> in an empty table,
  4092. <code>next</code> returns <b>nil</b>.
  4093. If the second argument is absent, then it is interpreted as <b>nil</b>.
  4094. In particular,
  4095. you can use <code>next(t)</code> to check whether a table is empty.
  4096. <p>
  4097. The order in which the indices are enumerated is not specified,
  4098. <em>even for numeric indices</em>.
  4099. (To traverse a table in numeric order,
  4100. use a numerical <b>for</b> or the <a href="#pdf-ipairs"><code>ipairs</code></a> function.)
  4101. <p>
  4102. The behavior of <code>next</code> is <em>undefined</em> if,
  4103. during the traversal,
  4104. you assign any value to a non-existent field in the table.
  4105. You may however modify existing fields.
  4106. In particular, you may clear existing fields.
  4107. <p>
  4108. <hr><h3><a name="pdf-pairs"><code>pairs (t)</code></a></h3>
  4109. <p>
  4110. Returns three values: the <a href="#pdf-next"><code>next</code></a> function, the table <code>t</code>, and <b>nil</b>,
  4111. so that the construction
  4112. <pre>
  4113. for k,v in pairs(t) do <em>body</em> end
  4114. </pre><p>
  4115. will iterate over all key&ndash;value pairs of table <code>t</code>.
  4116. <p>
  4117. See function <a href="#pdf-next"><code>next</code></a> for the caveats of modifying
  4118. the table during its traversal.
  4119. <p>
  4120. <hr><h3><a name="pdf-pcall"><code>pcall (f, arg1, &middot;&middot;&middot;)</code></a></h3>
  4121. <p>
  4122. Calls function <code>f</code> with
  4123. the given arguments in <em>protected mode</em>.
  4124. This means that any error inside&nbsp;<code>f</code> is not propagated;
  4125. instead, <code>pcall</code> catches the error
  4126. and returns a status code.
  4127. Its first result is the status code (a boolean),
  4128. which is true if the call succeeds without errors.
  4129. In such case, <code>pcall</code> also returns all results from the call,
  4130. after this first result.
  4131. In case of any error, <code>pcall</code> returns <b>false</b> plus the error message.
  4132. <p>
  4133. <hr><h3><a name="pdf-print"><code>print (&middot;&middot;&middot;)</code></a></h3>
  4134. Receives any number of arguments,
  4135. and prints their values to <code>stdout</code>,
  4136. using the <a href="#pdf-tostring"><code>tostring</code></a> function to convert them to strings.
  4137. <code>print</code> is not intended for formatted output,
  4138. but only as a quick way to show a value,
  4139. typically for debugging.
  4140. For formatted output, use <a href="#pdf-string.format"><code>string.format</code></a>.
  4141. <p>
  4142. <hr><h3><a name="pdf-rawequal"><code>rawequal (v1, v2)</code></a></h3>
  4143. Checks whether <code>v1</code> is equal to <code>v2</code>,
  4144. without invoking any metamethod.
  4145. Returns a boolean.
  4146. <p>
  4147. <hr><h3><a name="pdf-rawget"><code>rawget (table, index)</code></a></h3>
  4148. Gets the real value of <code>table[index]</code>,
  4149. without invoking any metamethod.
  4150. <code>table</code> must be a table;
  4151. <code>index</code> may be any value.
  4152. <p>
  4153. <hr><h3><a name="pdf-rawset"><code>rawset (table, index, value)</code></a></h3>
  4154. Sets the real value of <code>table[index]</code> to <code>value</code>,
  4155. without invoking any metamethod.
  4156. <code>table</code> must be a table,
  4157. <code>index</code> any value different from <b>nil</b>,
  4158. and <code>value</code> any Lua value.
  4159. <p>
  4160. This function returns <code>table</code>.
  4161. <p>
  4162. <hr><h3><a name="pdf-select"><code>select (index, &middot;&middot;&middot;)</code></a></h3>
  4163. <p>
  4164. If <code>index</code> is a number,
  4165. returns all arguments after argument number <code>index</code>.
  4166. Otherwise, <code>index</code> must be the string <code>"#"</code>,
  4167. and <code>select</code> returns the total number of extra arguments it received.
  4168. <p>
  4169. <hr><h3><a name="pdf-setfenv"><code>setfenv (f, table)</code></a></h3>
  4170. <p>
  4171. Sets the environment to be used by the given function.
  4172. <code>f</code> can be a Lua function or a number
  4173. that specifies the function at that stack level:
  4174. Level&nbsp;1 is the function calling <code>setfenv</code>.
  4175. <code>setfenv</code> returns the given function.
  4176. <p>
  4177. As a special case, when <code>f</code> is 0 <code>setfenv</code> changes
  4178. the environment of the running thread.
  4179. In this case, <code>setfenv</code> returns no values.
  4180. <p>
  4181. <hr><h3><a name="pdf-setmetatable"><code>setmetatable (table, metatable)</code></a></h3>
  4182. <p>
  4183. Sets the metatable for the given table.
  4184. (You cannot change the metatable of other types from Lua, only from&nbsp;C.)
  4185. If <code>metatable</code> is <b>nil</b>,
  4186. removes the metatable of the given table.
  4187. If the original metatable has a <code>"__metatable"</code> field,
  4188. raises an error.
  4189. <p>
  4190. This function returns <code>table</code>.
  4191. <p>
  4192. <hr><h3><a name="pdf-tonumber"><code>tonumber (e [, base])</code></a></h3>
  4193. Tries to convert its argument to a number.
  4194. If the argument is already a number or a string convertible
  4195. to a number, then <code>tonumber</code> returns this number;
  4196. otherwise, it returns <b>nil</b>.
  4197. <p>
  4198. An optional argument specifies the base to interpret the numeral.
  4199. The base may be any integer between 2 and 36, inclusive.
  4200. In bases above&nbsp;10, the letter '<code>A</code>' (in either upper or lower case)
  4201. represents&nbsp;10, '<code>B</code>' represents&nbsp;11, and so forth,
  4202. with '<code>Z</code>' representing 35.
  4203. In base 10 (the default), the number can have a decimal part,
  4204. as well as an optional exponent part (see <a href="#2.1">&sect;2.1</a>).
  4205. In other bases, only unsigned integers are accepted.
  4206. <p>
  4207. <hr><h3><a name="pdf-tostring"><code>tostring (e)</code></a></h3>
  4208. Receives an argument of any type and
  4209. converts it to a string in a reasonable format.
  4210. For complete control of how numbers are converted,
  4211. use <a href="#pdf-string.format"><code>string.format</code></a>.
  4212. <p>
  4213. If the metatable of <code>e</code> has a <code>"__tostring"</code> field,
  4214. then <code>tostring</code> calls the corresponding value
  4215. with <code>e</code> as argument,
  4216. and uses the result of the call as its result.
  4217. <p>
  4218. <hr><h3><a name="pdf-type"><code>type (v)</code></a></h3>
  4219. Returns the type of its only argument, coded as a string.
  4220. The possible results of this function are
  4221. "<code>nil</code>" (a string, not the value <b>nil</b>),
  4222. "<code>number</code>",
  4223. "<code>string</code>",
  4224. "<code>boolean</code>",
  4225. "<code>table</code>",
  4226. "<code>function</code>",
  4227. "<code>thread</code>",
  4228. and "<code>userdata</code>".
  4229. <p>
  4230. <hr><h3><a name="pdf-unpack"><code>unpack (list [, i [, j]])</code></a></h3>
  4231. Returns the elements from the given table.
  4232. This function is equivalent to
  4233. <pre>
  4234. return list[i], list[i+1], &middot;&middot;&middot;, list[j]
  4235. </pre><p>
  4236. except that the above code can be written only for a fixed number
  4237. of elements.
  4238. By default, <code>i</code> is&nbsp;1 and <code>j</code> is the length of the list,
  4239. as defined by the length operator (see <a href="#2.5.5">&sect;2.5.5</a>).
  4240. <p>
  4241. <hr><h3><a name="pdf-_VERSION"><code>_VERSION</code></a></h3>
  4242. A global variable (not a function) that
  4243. holds a string containing the current interpreter version.
  4244. The current contents of this variable is "<code>Lua 5.1</code>".
  4245. <p>
  4246. <hr><h3><a name="pdf-xpcall"><code>xpcall (f, err)</code></a></h3>
  4247. <p>
  4248. This function is similar to <a href="#pdf-pcall"><code>pcall</code></a>,
  4249. except that you can set a new error handler.
  4250. <p>
  4251. <code>xpcall</code> calls function <code>f</code> in protected mode,
  4252. using <code>err</code> as the error handler.
  4253. Any error inside <code>f</code> is not propagated;
  4254. instead, <code>xpcall</code> catches the error,
  4255. calls the <code>err</code> function with the original error object,
  4256. and returns a status code.
  4257. Its first result is the status code (a boolean),
  4258. which is true if the call succeeds without errors.
  4259. In this case, <code>xpcall</code> also returns all results from the call,
  4260. after this first result.
  4261. In case of any error,
  4262. <code>xpcall</code> returns <b>false</b> plus the result from <code>err</code>.
  4263. <h2>5.2 - <a name="5.2">Coroutine Manipulation</a></h2>
  4264. <p>
  4265. The operations related to coroutines comprise a sub-library of
  4266. the basic library and come inside the table <a name="pdf-coroutine"><code>coroutine</code></a>.
  4267. See <a href="#2.11">&sect;2.11</a> for a general description of coroutines.
  4268. <p>
  4269. <hr><h3><a name="pdf-coroutine.create"><code>coroutine.create (f)</code></a></h3>
  4270. <p>
  4271. Creates a new coroutine, with body <code>f</code>.
  4272. <code>f</code> must be a Lua function.
  4273. Returns this new coroutine,
  4274. an object with type <code>"thread"</code>.
  4275. <p>
  4276. <hr><h3><a name="pdf-coroutine.resume"><code>coroutine.resume (co [, val1, &middot;&middot;&middot;])</code></a></h3>
  4277. <p>
  4278. Starts or continues the execution of coroutine <code>co</code>.
  4279. The first time you resume a coroutine,
  4280. it starts running its body.
  4281. The values <code>val1</code>, &middot;&middot;&middot; are passed
  4282. as the arguments to the body function.
  4283. If the coroutine has yielded,
  4284. <code>resume</code> restarts it;
  4285. the values <code>val1</code>, &middot;&middot;&middot; are passed
  4286. as the results from the yield.
  4287. <p>
  4288. If the coroutine runs without any errors,
  4289. <code>resume</code> returns <b>true</b> plus any values passed to <code>yield</code>
  4290. (if the coroutine yields) or any values returned by the body function
  4291. (if the coroutine terminates).
  4292. If there is any error,
  4293. <code>resume</code> returns <b>false</b> plus the error message.
  4294. <p>
  4295. <hr><h3><a name="pdf-coroutine.running"><code>coroutine.running ()</code></a></h3>
  4296. <p>
  4297. Returns the running coroutine,
  4298. or <b>nil</b> when called by the main thread.
  4299. <p>
  4300. <hr><h3><a name="pdf-coroutine.status"><code>coroutine.status (co)</code></a></h3>
  4301. <p>
  4302. Returns the status of coroutine <code>co</code>, as a string:
  4303. <code>"running"</code>,
  4304. if the coroutine is running (that is, it called <code>status</code>);
  4305. <code>"suspended"</code>, if the coroutine is suspended in a call to <code>yield</code>,
  4306. or if it has not started running yet;
  4307. <code>"normal"</code> if the coroutine is active but not running
  4308. (that is, it has resumed another coroutine);
  4309. and <code>"dead"</code> if the coroutine has finished its body function,
  4310. or if it has stopped with an error.
  4311. <p>
  4312. <hr><h3><a name="pdf-coroutine.wrap"><code>coroutine.wrap (f)</code></a></h3>
  4313. <p>
  4314. Creates a new coroutine, with body <code>f</code>.
  4315. <code>f</code> must be a Lua function.
  4316. Returns a function that resumes the coroutine each time it is called.
  4317. Any arguments passed to the function behave as the
  4318. extra arguments to <code>resume</code>.
  4319. Returns the same values returned by <code>resume</code>,
  4320. except the first boolean.
  4321. In case of error, propagates the error.
  4322. <p>
  4323. <hr><h3><a name="pdf-coroutine.yield"><code>coroutine.yield (&middot;&middot;&middot;)</code></a></h3>
  4324. <p>
  4325. Suspends the execution of the calling coroutine.
  4326. The coroutine cannot be running a C&nbsp;function,
  4327. a metamethod, or an iterator.
  4328. Any arguments to <code>yield</code> are passed as extra results to <code>resume</code>.
  4329. <h2>5.3 - <a name="5.3">Modules</a></h2>
  4330. <p>
  4331. The package library provides basic
  4332. facilities for loading and building modules in Lua.
  4333. It exports two of its functions directly in the global environment:
  4334. <a href="#pdf-require"><code>require</code></a> and <a href="#pdf-module"><code>module</code></a>.
  4335. Everything else is exported in a table <a name="pdf-package"><code>package</code></a>.
  4336. <p>
  4337. <hr><h3><a name="pdf-module"><code>module (name [, &middot;&middot;&middot;])</code></a></h3>
  4338. <p>
  4339. Creates a module.
  4340. If there is a table in <code>package.loaded[name]</code>,
  4341. this table is the module.
  4342. Otherwise, if there is a global table <code>t</code> with the given name,
  4343. this table is the module.
  4344. Otherwise creates a new table <code>t</code> and
  4345. sets it as the value of the global <code>name</code> and
  4346. the value of <code>package.loaded[name]</code>.
  4347. This function also initializes <code>t._NAME</code> with the given name,
  4348. <code>t._M</code> with the module (<code>t</code> itself),
  4349. and <code>t._PACKAGE</code> with the package name
  4350. (the full module name minus last component; see below).
  4351. Finally, <code>module</code> sets <code>t</code> as the new environment
  4352. of the current function and the new value of <code>package.loaded[name]</code>,
  4353. so that <a href="#pdf-require"><code>require</code></a> returns <code>t</code>.
  4354. <p>
  4355. If <code>name</code> is a compound name
  4356. (that is, one with components separated by dots),
  4357. <code>module</code> creates (or reuses, if they already exist)
  4358. tables for each component.
  4359. For instance, if <code>name</code> is <code>a.b.c</code>,
  4360. then <code>module</code> stores the module table in field <code>c</code> of
  4361. field <code>b</code> of global <code>a</code>.
  4362. <p>
  4363. This function can receive optional <em>options</em> after
  4364. the module name,
  4365. where each option is a function to be applied over the module.
  4366. <p>
  4367. <hr><h3><a name="pdf-require"><code>require (modname)</code></a></h3>
  4368. <p>
  4369. Loads the given module.
  4370. The function starts by looking into the <a href="#pdf-package.loaded"><code>package.loaded</code></a> table
  4371. to determine whether <code>modname</code> is already loaded.
  4372. If it is, then <code>require</code> returns the value stored
  4373. at <code>package.loaded[modname]</code>.
  4374. Otherwise, it tries to find a <em>loader</em> for the module.
  4375. <p>
  4376. To find a loader,
  4377. <code>require</code> is guided by the <a href="#pdf-package.loaders"><code>package.loaders</code></a> array.
  4378. By changing this array,
  4379. we can change how <code>require</code> looks for a module.
  4380. The following explanation is based on the default configuration
  4381. for <a href="#pdf-package.loaders"><code>package.loaders</code></a>.
  4382. <p>
  4383. First <code>require</code> queries <code>package.preload[modname]</code>.
  4384. If it has a value,
  4385. this value (which should be a function) is the loader.
  4386. Otherwise <code>require</code> searches for a Lua loader using the
  4387. path stored in <a href="#pdf-package.path"><code>package.path</code></a>.
  4388. If that also fails, it searches for a C&nbsp;loader using the
  4389. path stored in <a href="#pdf-package.cpath"><code>package.cpath</code></a>.
  4390. If that also fails,
  4391. it tries an <em>all-in-one</em> loader (see <a href="#pdf-package.loaders"><code>package.loaders</code></a>).
  4392. <p>
  4393. Once a loader is found,
  4394. <code>require</code> calls the loader with a single argument, <code>modname</code>.
  4395. If the loader returns any value,
  4396. <code>require</code> assigns the returned value to <code>package.loaded[modname]</code>.
  4397. If the loader returns no value and
  4398. has not assigned any value to <code>package.loaded[modname]</code>,
  4399. then <code>require</code> assigns <b>true</b> to this entry.
  4400. In any case, <code>require</code> returns the
  4401. final value of <code>package.loaded[modname]</code>.
  4402. <p>
  4403. If there is any error loading or running the module,
  4404. or if it cannot find any loader for the module,
  4405. then <code>require</code> signals an error.
  4406. <p>
  4407. <hr><h3><a name="pdf-package.cpath"><code>package.cpath</code></a></h3>
  4408. <p>
  4409. The path used by <a href="#pdf-require"><code>require</code></a> to search for a C&nbsp;loader.
  4410. <p>
  4411. Lua initializes the C&nbsp;path <a href="#pdf-package.cpath"><code>package.cpath</code></a> in the same way
  4412. it initializes the Lua path <a href="#pdf-package.path"><code>package.path</code></a>,
  4413. using the environment variable <a name="pdf-LUA_CPATH"><code>LUA_CPATH</code></a>
  4414. or a default path defined in <code>luaconf.h</code>.
  4415. <p>
  4416. <hr><h3><a name="pdf-package.loaded"><code>package.loaded</code></a></h3>
  4417. <p>
  4418. A table used by <a href="#pdf-require"><code>require</code></a> to control which
  4419. modules are already loaded.
  4420. When you require a module <code>modname</code> and
  4421. <code>package.loaded[modname]</code> is not false,
  4422. <a href="#pdf-require"><code>require</code></a> simply returns the value stored there.
  4423. <p>
  4424. <hr><h3><a name="pdf-package.loaders"><code>package.loaders</code></a></h3>
  4425. <p>
  4426. A table used by <a href="#pdf-require"><code>require</code></a> to control how to load modules.
  4427. <p>
  4428. Each entry in this table is a <em>searcher function</em>.
  4429. When looking for a module,
  4430. <a href="#pdf-require"><code>require</code></a> calls each of these searchers in ascending order,
  4431. with the module name (the argument given to <a href="#pdf-require"><code>require</code></a>) as its
  4432. sole parameter.
  4433. The function can return another function (the module <em>loader</em>)
  4434. or a string explaining why it did not find that module
  4435. (or <b>nil</b> if it has nothing to say).
  4436. Lua initializes this table with four functions.
  4437. <p>
  4438. The first searcher simply looks for a loader in the
  4439. <a href="#pdf-package.preload"><code>package.preload</code></a> table.
  4440. <p>
  4441. The second searcher looks for a loader as a Lua library,
  4442. using the path stored at <a href="#pdf-package.path"><code>package.path</code></a>.
  4443. A path is a sequence of <em>templates</em> separated by semicolons.
  4444. For each template,
  4445. the searcher will change each interrogation
  4446. mark in the template by <code>filename</code>,
  4447. which is the module name with each dot replaced by a
  4448. "directory separator" (such as "<code>/</code>" in Unix);
  4449. then it will try to open the resulting file name.
  4450. So, for instance, if the Lua path is the string
  4451. <pre>
  4452. "./?.lua;./?.lc;/usr/local/?/init.lua"
  4453. </pre><p>
  4454. the search for a Lua file for module <code>foo</code>
  4455. will try to open the files
  4456. <code>./foo.lua</code>, <code>./foo.lc</code>, and
  4457. <code>/usr/local/foo/init.lua</code>, in that order.
  4458. <p>
  4459. The third searcher looks for a loader as a C&nbsp;library,
  4460. using the path given by the variable <a href="#pdf-package.cpath"><code>package.cpath</code></a>.
  4461. For instance,
  4462. if the C&nbsp;path is the string
  4463. <pre>
  4464. "./?.so;./?.dll;/usr/local/?/init.so"
  4465. </pre><p>
  4466. the searcher for module <code>foo</code>
  4467. will try to open the files <code>./foo.so</code>, <code>./foo.dll</code>,
  4468. and <code>/usr/local/foo/init.so</code>, in that order.
  4469. Once it finds a C&nbsp;library,
  4470. this searcher first uses a dynamic link facility to link the
  4471. application with the library.
  4472. Then it tries to find a C&nbsp;function inside the library to
  4473. be used as the loader.
  4474. The name of this C&nbsp;function is the string "<code>luaopen_</code>"
  4475. concatenated with a copy of the module name where each dot
  4476. is replaced by an underscore.
  4477. Moreover, if the module name has a hyphen,
  4478. its prefix up to (and including) the first hyphen is removed.
  4479. For instance, if the module name is <code>a.v1-b.c</code>,
  4480. the function name will be <code>luaopen_b_c</code>.
  4481. <p>
  4482. The fourth searcher tries an <em>all-in-one loader</em>.
  4483. It searches the C&nbsp;path for a library for
  4484. the root name of the given module.
  4485. For instance, when requiring <code>a.b.c</code>,
  4486. it will search for a C&nbsp;library for <code>a</code>.
  4487. If found, it looks into it for an open function for
  4488. the submodule;
  4489. in our example, that would be <code>luaopen_a_b_c</code>.
  4490. With this facility, a package can pack several C&nbsp;submodules
  4491. into one single library,
  4492. with each submodule keeping its original open function.
  4493. <p>
  4494. <hr><h3><a name="pdf-package.loadlib"><code>package.loadlib (libname, funcname)</code></a></h3>
  4495. <p>
  4496. Dynamically links the host program with the C&nbsp;library <code>libname</code>.
  4497. Inside this library, looks for a function <code>funcname</code>
  4498. and returns this function as a C&nbsp;function.
  4499. (So, <code>funcname</code> must follow the protocol (see <a href="#lua_CFunction"><code>lua_CFunction</code></a>)).
  4500. <p>
  4501. This is a low-level function.
  4502. It completely bypasses the package and module system.
  4503. Unlike <a href="#pdf-require"><code>require</code></a>,
  4504. it does not perform any path searching and
  4505. does not automatically adds extensions.
  4506. <code>libname</code> must be the complete file name of the C&nbsp;library,
  4507. including if necessary a path and extension.
  4508. <code>funcname</code> must be the exact name exported by the C&nbsp;library
  4509. (which may depend on the C&nbsp;compiler and linker used).
  4510. <p>
  4511. This function is not supported by ANSI C.
  4512. As such, it is only available on some platforms
  4513. (Windows, Linux, Mac OS X, Solaris, BSD,
  4514. plus other Unix systems that support the <code>dlfcn</code> standard).
  4515. <p>
  4516. <hr><h3><a name="pdf-package.path"><code>package.path</code></a></h3>
  4517. <p>
  4518. The path used by <a href="#pdf-require"><code>require</code></a> to search for a Lua loader.
  4519. <p>
  4520. At start-up, Lua initializes this variable with
  4521. the value of the environment variable <a name="pdf-LUA_PATH"><code>LUA_PATH</code></a> or
  4522. with a default path defined in <code>luaconf.h</code>,
  4523. if the environment variable is not defined.
  4524. Any "<code>;;</code>" in the value of the environment variable
  4525. is replaced by the default path.
  4526. <p>
  4527. <hr><h3><a name="pdf-package.preload"><code>package.preload</code></a></h3>
  4528. <p>
  4529. A table to store loaders for specific modules
  4530. (see <a href="#pdf-require"><code>require</code></a>).
  4531. <p>
  4532. <hr><h3><a name="pdf-package.seeall"><code>package.seeall (module)</code></a></h3>
  4533. <p>
  4534. Sets a metatable for <code>module</code> with
  4535. its <code>__index</code> field referring to the global environment,
  4536. so that this module inherits values
  4537. from the global environment.
  4538. To be used as an option to function <a href="#pdf-module"><code>module</code></a>.
  4539. <h2>5.4 - <a name="5.4">String Manipulation</a></h2>
  4540. <p>
  4541. This library provides generic functions for string manipulation,
  4542. such as finding and extracting substrings, and pattern matching.
  4543. When indexing a string in Lua, the first character is at position&nbsp;1
  4544. (not at&nbsp;0, as in C).
  4545. Indices are allowed to be negative and are interpreted as indexing backwards,
  4546. from the end of the string.
  4547. Thus, the last character is at position -1, and so on.
  4548. <p>
  4549. The string library provides all its functions inside the table
  4550. <a name="pdf-string"><code>string</code></a>.
  4551. It also sets a metatable for strings
  4552. where the <code>__index</code> field points to the <code>string</code> table.
  4553. Therefore, you can use the string functions in object-oriented style.
  4554. For instance, <code>string.byte(s, i)</code>
  4555. can be written as <code>s:byte(i)</code>.
  4556. <p>
  4557. The string library assumes one-byte character encodings.
  4558. <p>
  4559. <hr><h3><a name="pdf-string.byte"><code>string.byte (s [, i [, j]])</code></a></h3>
  4560. Returns the internal numerical codes of the characters <code>s[i]</code>,
  4561. <code>s[i+1]</code>, &middot;&middot;&middot;, <code>s[j]</code>.
  4562. The default value for <code>i</code> is&nbsp;1;
  4563. the default value for <code>j</code> is&nbsp;<code>i</code>.
  4564. <p>
  4565. Note that numerical codes are not necessarily portable across platforms.
  4566. <p>
  4567. <hr><h3><a name="pdf-string.char"><code>string.char (&middot;&middot;&middot;)</code></a></h3>
  4568. Receives zero or more integers.
  4569. Returns a string with length equal to the number of arguments,
  4570. in which each character has the internal numerical code equal
  4571. to its corresponding argument.
  4572. <p>
  4573. Note that numerical codes are not necessarily portable across platforms.
  4574. <p>
  4575. <hr><h3><a name="pdf-string.dump"><code>string.dump (function)</code></a></h3>
  4576. <p>
  4577. Returns a string containing a binary representation of the given function,
  4578. so that a later <a href="#pdf-loadstring"><code>loadstring</code></a> on this string returns
  4579. a copy of the function.
  4580. <code>function</code> must be a Lua function without upvalues.
  4581. <p>
  4582. <hr><h3><a name="pdf-string.find"><code>string.find (s, pattern [, init [, plain]])</code></a></h3>
  4583. Looks for the first match of
  4584. <code>pattern</code> in the string <code>s</code>.
  4585. If it finds a match, then <code>find</code> returns the indices of&nbsp;<code>s</code>
  4586. where this occurrence starts and ends;
  4587. otherwise, it returns <b>nil</b>.
  4588. A third, optional numerical argument <code>init</code> specifies
  4589. where to start the search;
  4590. its default value is&nbsp;1 and can be negative.
  4591. A value of <b>true</b> as a fourth, optional argument <code>plain</code>
  4592. turns off the pattern matching facilities,
  4593. so the function does a plain "find substring" operation,
  4594. with no characters in <code>pattern</code> being considered "magic".
  4595. Note that if <code>plain</code> is given, then <code>init</code> must be given as well.
  4596. <p>
  4597. If the pattern has captures,
  4598. then in a successful match
  4599. the captured values are also returned,
  4600. after the two indices.
  4601. <p>
  4602. <hr><h3><a name="pdf-string.format"><code>string.format (formatstring, &middot;&middot;&middot;)</code></a></h3>
  4603. Returns a formatted version of its variable number of arguments
  4604. following the description given in its first argument (which must be a string).
  4605. The format string follows the same rules as the <code>printf</code> family of
  4606. standard C&nbsp;functions.
  4607. The only differences are that the options/modifiers
  4608. <code>*</code>, <code>l</code>, <code>L</code>, <code>n</code>, <code>p</code>,
  4609. and <code>h</code> are not supported
  4610. and that there is an extra option, <code>q</code>.
  4611. The <code>q</code> option formats a string in a form suitable to be safely read
  4612. back by the Lua interpreter:
  4613. the string is written between double quotes,
  4614. and all double quotes, newlines, embedded zeros,
  4615. and backslashes in the string
  4616. are correctly escaped when written.
  4617. For instance, the call
  4618. <pre>
  4619. string.format('%q', 'a string with "quotes" and \n new line')
  4620. </pre><p>
  4621. will produce the string:
  4622. <pre>
  4623. "a string with \"quotes\" and \
  4624. new line"
  4625. </pre>
  4626. <p>
  4627. The options <code>c</code>, <code>d</code>, <code>E</code>, <code>e</code>, <code>f</code>,
  4628. <code>g</code>, <code>G</code>, <code>i</code>, <code>o</code>, <code>u</code>, <code>X</code>, and <code>x</code> all
  4629. expect a number as argument,
  4630. whereas <code>q</code> and <code>s</code> expect a string.
  4631. <p>
  4632. This function does not accept string values
  4633. containing embedded zeros,
  4634. except as arguments to the <code>q</code> option.
  4635. <p>
  4636. <hr><h3><a name="pdf-string.gmatch"><code>string.gmatch (s, pattern)</code></a></h3>
  4637. Returns an iterator function that,
  4638. each time it is called,
  4639. returns the next captures from <code>pattern</code> over string <code>s</code>.
  4640. If <code>pattern</code> specifies no captures,
  4641. then the whole match is produced in each call.
  4642. <p>
  4643. As an example, the following loop
  4644. <pre>
  4645. s = "hello world from Lua"
  4646. for w in string.gmatch(s, "%a+") do
  4647. print(w)
  4648. end
  4649. </pre><p>
  4650. will iterate over all the words from string <code>s</code>,
  4651. printing one per line.
  4652. The next example collects all pairs <code>key=value</code> from the
  4653. given string into a table:
  4654. <pre>
  4655. t = {}
  4656. s = "from=world, to=Lua"
  4657. for k, v in string.gmatch(s, "(%w+)=(%w+)") do
  4658. t[k] = v
  4659. end
  4660. </pre>
  4661. <p>
  4662. For this function, a '<code>^</code>' at the start of a pattern does not
  4663. work as an anchor, as this would prevent the iteration.
  4664. <p>
  4665. <hr><h3><a name="pdf-string.gsub"><code>string.gsub (s, pattern, repl [, n])</code></a></h3>
  4666. Returns a copy of <code>s</code>
  4667. in which all (or the first <code>n</code>, if given)
  4668. occurrences of the <code>pattern</code> have been
  4669. replaced by a replacement string specified by <code>repl</code>,
  4670. which can be a string, a table, or a function.
  4671. <code>gsub</code> also returns, as its second value,
  4672. the total number of matches that occurred.
  4673. <p>
  4674. If <code>repl</code> is a string, then its value is used for replacement.
  4675. The character&nbsp;<code>%</code> works as an escape character:
  4676. any sequence in <code>repl</code> of the form <code>%<em>n</em></code>,
  4677. with <em>n</em> between 1 and 9,
  4678. stands for the value of the <em>n</em>-th captured substring (see below).
  4679. The sequence <code>%0</code> stands for the whole match.
  4680. The sequence <code>%%</code> stands for a single&nbsp;<code>%</code>.
  4681. <p>
  4682. If <code>repl</code> is a table, then the table is queried for every match,
  4683. using the first capture as the key;
  4684. if the pattern specifies no captures,
  4685. then the whole match is used as the key.
  4686. <p>
  4687. If <code>repl</code> is a function, then this function is called every time a
  4688. match occurs, with all captured substrings passed as arguments,
  4689. in order;
  4690. if the pattern specifies no captures,
  4691. then the whole match is passed as a sole argument.
  4692. <p>
  4693. If the value returned by the table query or by the function call
  4694. is a string or a number,
  4695. then it is used as the replacement string;
  4696. otherwise, if it is <b>false</b> or <b>nil</b>,
  4697. then there is no replacement
  4698. (that is, the original match is kept in the string).
  4699. <p>
  4700. Here are some examples:
  4701. <pre>
  4702. x = string.gsub("hello world", "(%w+)", "%1 %1")
  4703. --&gt; x="hello hello world world"
  4704. x = string.gsub("hello world", "%w+", "%0 %0", 1)
  4705. --&gt; x="hello hello world"
  4706. x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1")
  4707. --&gt; x="world hello Lua from"
  4708. x = string.gsub("home = $HOME, user = $USER", "%$(%w+)", os.getenv)
  4709. --&gt; x="home = /home/roberto, user = roberto"
  4710. x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s)
  4711. return loadstring(s)()
  4712. end)
  4713. --&gt; x="4+5 = 9"
  4714. local t = {name="lua", version="5.1"}
  4715. x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t)
  4716. --&gt; x="lua-5.1.tar.gz"
  4717. </pre>
  4718. <p>
  4719. <hr><h3><a name="pdf-string.len"><code>string.len (s)</code></a></h3>
  4720. Receives a string and returns its length.
  4721. The empty string <code>""</code> has length 0.
  4722. Embedded zeros are counted,
  4723. so <code>"a\000bc\000"</code> has length 5.
  4724. <p>
  4725. <hr><h3><a name="pdf-string.lower"><code>string.lower (s)</code></a></h3>
  4726. Receives a string and returns a copy of this string with all
  4727. uppercase letters changed to lowercase.
  4728. All other characters are left unchanged.
  4729. The definition of what an uppercase letter is depends on the current locale.
  4730. <p>
  4731. <hr><h3><a name="pdf-string.match"><code>string.match (s, pattern [, init])</code></a></h3>
  4732. Looks for the first <em>match</em> of
  4733. <code>pattern</code> in the string <code>s</code>.
  4734. If it finds one, then <code>match</code> returns
  4735. the captures from the pattern;
  4736. otherwise it returns <b>nil</b>.
  4737. If <code>pattern</code> specifies no captures,
  4738. then the whole match is returned.
  4739. A third, optional numerical argument <code>init</code> specifies
  4740. where to start the search;
  4741. its default value is&nbsp;1 and can be negative.
  4742. <p>
  4743. <hr><h3><a name="pdf-string.rep"><code>string.rep (s, n)</code></a></h3>
  4744. Returns a string that is the concatenation of <code>n</code> copies of
  4745. the string <code>s</code>.
  4746. <p>
  4747. <hr><h3><a name="pdf-string.reverse"><code>string.reverse (s)</code></a></h3>
  4748. Returns a string that is the string <code>s</code> reversed.
  4749. <p>
  4750. <hr><h3><a name="pdf-string.sub"><code>string.sub (s, i [, j])</code></a></h3>
  4751. Returns the substring of <code>s</code> that
  4752. starts at <code>i</code> and continues until <code>j</code>;
  4753. <code>i</code> and <code>j</code> can be negative.
  4754. If <code>j</code> is absent, then it is assumed to be equal to -1
  4755. (which is the same as the string length).
  4756. In particular,
  4757. the call <code>string.sub(s,1,j)</code> returns a prefix of <code>s</code>
  4758. with length <code>j</code>,
  4759. and <code>string.sub(s, -i)</code> returns a suffix of <code>s</code>
  4760. with length <code>i</code>.
  4761. <p>
  4762. <hr><h3><a name="pdf-string.upper"><code>string.upper (s)</code></a></h3>
  4763. Receives a string and returns a copy of this string with all
  4764. lowercase letters changed to uppercase.
  4765. All other characters are left unchanged.
  4766. The definition of what a lowercase letter is depends on the current locale.
  4767. <h3>5.4.1 - <a name="5.4.1">Patterns</a></h3>
  4768. <h4>Character Class:</h4><p>
  4769. A <em>character class</em> is used to represent a set of characters.
  4770. The following combinations are allowed in describing a character class:
  4771. <ul>
  4772. <li><b><em>x</em>:</b>
  4773. (where <em>x</em> is not one of the <em>magic characters</em>
  4774. <code>^$()%.[]*+-?</code>)
  4775. represents the character <em>x</em> itself.
  4776. </li>
  4777. <li><b><code>.</code>:</b> (a dot) represents all characters.</li>
  4778. <li><b><code>%a</code>:</b> represents all letters.</li>
  4779. <li><b><code>%c</code>:</b> represents all control characters.</li>
  4780. <li><b><code>%d</code>:</b> represents all digits.</li>
  4781. <li><b><code>%l</code>:</b> represents all lowercase letters.</li>
  4782. <li><b><code>%p</code>:</b> represents all punctuation characters.</li>
  4783. <li><b><code>%s</code>:</b> represents all space characters.</li>
  4784. <li><b><code>%u</code>:</b> represents all uppercase letters.</li>
  4785. <li><b><code>%w</code>:</b> represents all alphanumeric characters.</li>
  4786. <li><b><code>%x</code>:</b> represents all hexadecimal digits.</li>
  4787. <li><b><code>%z</code>:</b> represents the character with representation 0.</li>
  4788. <li><b><code>%<em>x</em></code>:</b> (where <em>x</em> is any non-alphanumeric character)
  4789. represents the character <em>x</em>.
  4790. This is the standard way to escape the magic characters.
  4791. Any punctuation character (even the non magic)
  4792. can be preceded by a '<code>%</code>'
  4793. when used to represent itself in a pattern.
  4794. </li>
  4795. <li><b><code>[<em>set</em>]</code>:</b>
  4796. represents the class which is the union of all
  4797. characters in <em>set</em>.
  4798. A range of characters can be specified by
  4799. separating the end characters of the range with a '<code>-</code>'.
  4800. All classes <code>%</code><em>x</em> described above can also be used as
  4801. components in <em>set</em>.
  4802. All other characters in <em>set</em> represent themselves.
  4803. For example, <code>[%w_]</code> (or <code>[_%w]</code>)
  4804. represents all alphanumeric characters plus the underscore,
  4805. <code>[0-7]</code> represents the octal digits,
  4806. and <code>[0-7%l%-]</code> represents the octal digits plus
  4807. the lowercase letters plus the '<code>-</code>' character.
  4808. <p>
  4809. The interaction between ranges and classes is not defined.
  4810. Therefore, patterns like <code>[%a-z]</code> or <code>[a-%%]</code>
  4811. have no meaning.
  4812. </li>
  4813. <li><b><code>[^<em>set</em>]</code>:</b>
  4814. represents the complement of <em>set</em>,
  4815. where <em>set</em> is interpreted as above.
  4816. </li>
  4817. </ul><p>
  4818. For all classes represented by single letters (<code>%a</code>, <code>%c</code>, etc.),
  4819. the corresponding uppercase letter represents the complement of the class.
  4820. For instance, <code>%S</code> represents all non-space characters.
  4821. <p>
  4822. The definitions of letter, space, and other character groups
  4823. depend on the current locale.
  4824. In particular, the class <code>[a-z]</code> may not be equivalent to <code>%l</code>.
  4825. <h4>Pattern Item:</h4><p>
  4826. A <em>pattern item</em> can be
  4827. <ul>
  4828. <li>
  4829. a single character class,
  4830. which matches any single character in the class;
  4831. </li>
  4832. <li>
  4833. a single character class followed by '<code>*</code>',
  4834. which matches 0 or more repetitions of characters in the class.
  4835. These repetition items will always match the longest possible sequence;
  4836. </li>
  4837. <li>
  4838. a single character class followed by '<code>+</code>',
  4839. which matches 1 or more repetitions of characters in the class.
  4840. These repetition items will always match the longest possible sequence;
  4841. </li>
  4842. <li>
  4843. a single character class followed by '<code>-</code>',
  4844. which also matches 0 or more repetitions of characters in the class.
  4845. Unlike '<code>*</code>',
  4846. these repetition items will always match the <em>shortest</em> possible sequence;
  4847. </li>
  4848. <li>
  4849. a single character class followed by '<code>?</code>',
  4850. which matches 0 or 1 occurrence of a character in the class;
  4851. </li>
  4852. <li>
  4853. <code>%<em>n</em></code>, for <em>n</em> between 1 and 9;
  4854. such item matches a substring equal to the <em>n</em>-th captured string
  4855. (see below);
  4856. </li>
  4857. <li>
  4858. <code>%b<em>xy</em></code>, where <em>x</em> and <em>y</em> are two distinct characters;
  4859. such item matches strings that start with&nbsp;<em>x</em>, end with&nbsp;<em>y</em>,
  4860. and where the <em>x</em> and <em>y</em> are <em>balanced</em>.
  4861. This means that, if one reads the string from left to right,
  4862. counting <em>+1</em> for an <em>x</em> and <em>-1</em> for a <em>y</em>,
  4863. the ending <em>y</em> is the first <em>y</em> where the count reaches 0.
  4864. For instance, the item <code>%b()</code> matches expressions with
  4865. balanced parentheses.
  4866. </li>
  4867. </ul>
  4868. <h4>Pattern:</h4><p>
  4869. A <em>pattern</em> is a sequence of pattern items.
  4870. A '<code>^</code>' at the beginning of a pattern anchors the match at the
  4871. beginning of the subject string.
  4872. A '<code>$</code>' at the end of a pattern anchors the match at the
  4873. end of the subject string.
  4874. At other positions,
  4875. '<code>^</code>' and '<code>$</code>' have no special meaning and represent themselves.
  4876. <h4>Captures:</h4><p>
  4877. A pattern can contain sub-patterns enclosed in parentheses;
  4878. they describe <em>captures</em>.
  4879. When a match succeeds, the substrings of the subject string
  4880. that match captures are stored (<em>captured</em>) for future use.
  4881. Captures are numbered according to their left parentheses.
  4882. For instance, in the pattern <code>"(a*(.)%w(%s*))"</code>,
  4883. the part of the string matching <code>"a*(.)%w(%s*)"</code> is
  4884. stored as the first capture (and therefore has number&nbsp;1);
  4885. the character matching "<code>.</code>" is captured with number&nbsp;2,
  4886. and the part matching "<code>%s*</code>" has number&nbsp;3.
  4887. <p>
  4888. As a special case, the empty capture <code>()</code> captures
  4889. the current string position (a number).
  4890. For instance, if we apply the pattern <code>"()aa()"</code> on the
  4891. string <code>"flaaap"</code>, there will be two captures: 3&nbsp;and&nbsp;5.
  4892. <p>
  4893. A pattern cannot contain embedded zeros. Use <code>%z</code> instead.
  4894. <h2>5.5 - <a name="5.5">Table Manipulation</a></h2><p>
  4895. This library provides generic functions for table manipulation.
  4896. It provides all its functions inside the table <a name="pdf-table"><code>table</code></a>.
  4897. <p>
  4898. Most functions in the table library assume that the table
  4899. represents an array or a list.
  4900. For these functions, when we talk about the "length" of a table
  4901. we mean the result of the length operator.
  4902. <p>
  4903. <hr><h3><a name="pdf-table.concat"><code>table.concat (table [, sep [, i [, j]]])</code></a></h3>
  4904. Given an array where all elements are strings or numbers,
  4905. returns <code>table[i]..sep..table[i+1] &middot;&middot;&middot; sep..table[j]</code>.
  4906. The default value for <code>sep</code> is the empty string,
  4907. the default for <code>i</code> is 1,
  4908. and the default for <code>j</code> is the length of the table.
  4909. If <code>i</code> is greater than <code>j</code>, returns the empty string.
  4910. <p>
  4911. <hr><h3><a name="pdf-table.insert"><code>table.insert (table, [pos,] value)</code></a></h3>
  4912. <p>
  4913. Inserts element <code>value</code> at position <code>pos</code> in <code>table</code>,
  4914. shifting up other elements to open space, if necessary.
  4915. The default value for <code>pos</code> is <code>n+1</code>,
  4916. where <code>n</code> is the length of the table (see <a href="#2.5.5">&sect;2.5.5</a>),
  4917. so that a call <code>table.insert(t,x)</code> inserts <code>x</code> at the end
  4918. of table <code>t</code>.
  4919. <p>
  4920. <hr><h3><a name="pdf-table.maxn"><code>table.maxn (table)</code></a></h3>
  4921. <p>
  4922. Returns the largest positive numerical index of the given table,
  4923. or zero if the table has no positive numerical indices.
  4924. (To do its job this function does a linear traversal of
  4925. the whole table.)
  4926. <p>
  4927. <hr><h3><a name="pdf-table.remove"><code>table.remove (table [, pos])</code></a></h3>
  4928. <p>
  4929. Removes from <code>table</code> the element at position <code>pos</code>,
  4930. shifting down other elements to close the space, if necessary.
  4931. Returns the value of the removed element.
  4932. The default value for <code>pos</code> is <code>n</code>,
  4933. where <code>n</code> is the length of the table,
  4934. so that a call <code>table.remove(t)</code> removes the last element
  4935. of table <code>t</code>.
  4936. <p>
  4937. <hr><h3><a name="pdf-table.sort"><code>table.sort (table [, comp])</code></a></h3>
  4938. Sorts table elements in a given order, <em>in-place</em>,
  4939. from <code>table[1]</code> to <code>table[n]</code>,
  4940. where <code>n</code> is the length of the table.
  4941. If <code>comp</code> is given,
  4942. then it must be a function that receives two table elements,
  4943. and returns true
  4944. when the first is less than the second
  4945. (so that <code>not comp(a[i+1],a[i])</code> will be true after the sort).
  4946. If <code>comp</code> is not given,
  4947. then the standard Lua operator <code>&lt;</code> is used instead.
  4948. <p>
  4949. The sort algorithm is not stable;
  4950. that is, elements considered equal by the given order
  4951. may have their relative positions changed by the sort.
  4952. <h2>5.6 - <a name="5.6">Mathematical Functions</a></h2>
  4953. <p>
  4954. This library is an interface to the standard C&nbsp;math library.
  4955. It provides all its functions inside the table <a name="pdf-math"><code>math</code></a>.
  4956. <p>
  4957. <hr><h3><a name="pdf-math.abs"><code>math.abs (x)</code></a></h3>
  4958. <p>
  4959. Returns the absolute value of <code>x</code>.
  4960. <p>
  4961. <hr><h3><a name="pdf-math.acos"><code>math.acos (x)</code></a></h3>
  4962. <p>
  4963. Returns the arc cosine of <code>x</code> (in radians).
  4964. <p>
  4965. <hr><h3><a name="pdf-math.asin"><code>math.asin (x)</code></a></h3>
  4966. <p>
  4967. Returns the arc sine of <code>x</code> (in radians).
  4968. <p>
  4969. <hr><h3><a name="pdf-math.atan"><code>math.atan (x)</code></a></h3>
  4970. <p>
  4971. Returns the arc tangent of <code>x</code> (in radians).
  4972. <p>
  4973. <hr><h3><a name="pdf-math.atan2"><code>math.atan2 (y, x)</code></a></h3>
  4974. <p>
  4975. Returns the arc tangent of <code>y/x</code> (in radians),
  4976. but uses the signs of both parameters to find the
  4977. quadrant of the result.
  4978. (It also handles correctly the case of <code>x</code> being zero.)
  4979. <p>
  4980. <hr><h3><a name="pdf-math.ceil"><code>math.ceil (x)</code></a></h3>
  4981. <p>
  4982. Returns the smallest integer larger than or equal to <code>x</code>.
  4983. <p>
  4984. <hr><h3><a name="pdf-math.cos"><code>math.cos (x)</code></a></h3>
  4985. <p>
  4986. Returns the cosine of <code>x</code> (assumed to be in radians).
  4987. <p>
  4988. <hr><h3><a name="pdf-math.cosh"><code>math.cosh (x)</code></a></h3>
  4989. <p>
  4990. Returns the hyperbolic cosine of <code>x</code>.
  4991. <p>
  4992. <hr><h3><a name="pdf-math.deg"><code>math.deg (x)</code></a></h3>
  4993. <p>
  4994. Returns the angle <code>x</code> (given in radians) in degrees.
  4995. <p>
  4996. <hr><h3><a name="pdf-math.exp"><code>math.exp (x)</code></a></h3>
  4997. <p>
  4998. Returns the value <em>e<sup>x</sup></em>.
  4999. <p>
  5000. <hr><h3><a name="pdf-math.floor"><code>math.floor (x)</code></a></h3>
  5001. <p>
  5002. Returns the largest integer smaller than or equal to <code>x</code>.
  5003. <p>
  5004. <hr><h3><a name="pdf-math.fmod"><code>math.fmod (x, y)</code></a></h3>
  5005. <p>
  5006. Returns the remainder of the division of <code>x</code> by <code>y</code>
  5007. that rounds the quotient towards zero.
  5008. <p>
  5009. <hr><h3><a name="pdf-math.frexp"><code>math.frexp (x)</code></a></h3>
  5010. <p>
  5011. Returns <code>m</code> and <code>e</code> such that <em>x = m2<sup>e</sup></em>,
  5012. <code>e</code> is an integer and the absolute value of <code>m</code> is
  5013. in the range <em>[0.5, 1)</em>
  5014. (or zero when <code>x</code> is zero).
  5015. <p>
  5016. <hr><h3><a name="pdf-math.huge"><code>math.huge</code></a></h3>
  5017. <p>
  5018. The value <code>HUGE_VAL</code>,
  5019. a value larger than or equal to any other numerical value.
  5020. <p>
  5021. <hr><h3><a name="pdf-math.ldexp"><code>math.ldexp (m, e)</code></a></h3>
  5022. <p>
  5023. Returns <em>m2<sup>e</sup></em> (<code>e</code> should be an integer).
  5024. <p>
  5025. <hr><h3><a name="pdf-math.log"><code>math.log (x)</code></a></h3>
  5026. <p>
  5027. Returns the natural logarithm of <code>x</code>.
  5028. <p>
  5029. <hr><h3><a name="pdf-math.log10"><code>math.log10 (x)</code></a></h3>
  5030. <p>
  5031. Returns the base-10 logarithm of <code>x</code>.
  5032. <p>
  5033. <hr><h3><a name="pdf-math.max"><code>math.max (x, &middot;&middot;&middot;)</code></a></h3>
  5034. <p>
  5035. Returns the maximum value among its arguments.
  5036. <p>
  5037. <hr><h3><a name="pdf-math.min"><code>math.min (x, &middot;&middot;&middot;)</code></a></h3>
  5038. <p>
  5039. Returns the minimum value among its arguments.
  5040. <p>
  5041. <hr><h3><a name="pdf-math.modf"><code>math.modf (x)</code></a></h3>
  5042. <p>
  5043. Returns two numbers,
  5044. the integral part of <code>x</code> and the fractional part of <code>x</code>.
  5045. <p>
  5046. <hr><h3><a name="pdf-math.pi"><code>math.pi</code></a></h3>
  5047. <p>
  5048. The value of <em>pi</em>.
  5049. <p>
  5050. <hr><h3><a name="pdf-math.pow"><code>math.pow (x, y)</code></a></h3>
  5051. <p>
  5052. Returns <em>x<sup>y</sup></em>.
  5053. (You can also use the expression <code>x^y</code> to compute this value.)
  5054. <p>
  5055. <hr><h3><a name="pdf-math.rad"><code>math.rad (x)</code></a></h3>
  5056. <p>
  5057. Returns the angle <code>x</code> (given in degrees) in radians.
  5058. <p>
  5059. <hr><h3><a name="pdf-math.random"><code>math.random ([m [, n]])</code></a></h3>
  5060. <p>
  5061. This function is an interface to the simple
  5062. pseudo-random generator function <code>rand</code> provided by ANSI&nbsp;C.
  5063. (No guarantees can be given for its statistical properties.)
  5064. <p>
  5065. When called without arguments,
  5066. returns a uniform pseudo-random real number
  5067. in the range <em>[0,1)</em>.
  5068. When called with an integer number <code>m</code>,
  5069. <code>math.random</code> returns
  5070. a uniform pseudo-random integer in the range <em>[1, m]</em>.
  5071. When called with two integer numbers <code>m</code> and <code>n</code>,
  5072. <code>math.random</code> returns a uniform pseudo-random
  5073. integer in the range <em>[m, n]</em>.
  5074. <p>
  5075. <hr><h3><a name="pdf-math.randomseed"><code>math.randomseed (x)</code></a></h3>
  5076. <p>
  5077. Sets <code>x</code> as the "seed"
  5078. for the pseudo-random generator:
  5079. equal seeds produce equal sequences of numbers.
  5080. <p>
  5081. <hr><h3><a name="pdf-math.sin"><code>math.sin (x)</code></a></h3>
  5082. <p>
  5083. Returns the sine of <code>x</code> (assumed to be in radians).
  5084. <p>
  5085. <hr><h3><a name="pdf-math.sinh"><code>math.sinh (x)</code></a></h3>
  5086. <p>
  5087. Returns the hyperbolic sine of <code>x</code>.
  5088. <p>
  5089. <hr><h3><a name="pdf-math.sqrt"><code>math.sqrt (x)</code></a></h3>
  5090. <p>
  5091. Returns the square root of <code>x</code>.
  5092. (You can also use the expression <code>x^0.5</code> to compute this value.)
  5093. <p>
  5094. <hr><h3><a name="pdf-math.tan"><code>math.tan (x)</code></a></h3>
  5095. <p>
  5096. Returns the tangent of <code>x</code> (assumed to be in radians).
  5097. <p>
  5098. <hr><h3><a name="pdf-math.tanh"><code>math.tanh (x)</code></a></h3>
  5099. <p>
  5100. Returns the hyperbolic tangent of <code>x</code>.
  5101. <h2>5.7 - <a name="5.7">Input and Output Facilities</a></h2>
  5102. <p>
  5103. The I/O library provides two different styles for file manipulation.
  5104. The first one uses implicit file descriptors;
  5105. that is, there are operations to set a default input file and a
  5106. default output file,
  5107. and all input/output operations are over these default files.
  5108. The second style uses explicit file descriptors.
  5109. <p>
  5110. When using implicit file descriptors,
  5111. all operations are supplied by table <a name="pdf-io"><code>io</code></a>.
  5112. When using explicit file descriptors,
  5113. the operation <a href="#pdf-io.open"><code>io.open</code></a> returns a file descriptor
  5114. and then all operations are supplied as methods of the file descriptor.
  5115. <p>
  5116. The table <code>io</code> also provides
  5117. three predefined file descriptors with their usual meanings from C:
  5118. <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>.
  5119. The I/O library never closes these files.
  5120. <p>
  5121. Unless otherwise stated,
  5122. all I/O functions return <b>nil</b> on failure
  5123. (plus an error message as a second result and
  5124. a system-dependent error code as a third result)
  5125. and some value different from <b>nil</b> on success.
  5126. <p>
  5127. <hr><h3><a name="pdf-io.close"><code>io.close ([file])</code></a></h3>
  5128. <p>
  5129. Equivalent to <code>file:close()</code>.
  5130. Without a <code>file</code>, closes the default output file.
  5131. <p>
  5132. <hr><h3><a name="pdf-io.flush"><code>io.flush ()</code></a></h3>
  5133. <p>
  5134. Equivalent to <code>file:flush</code> over the default output file.
  5135. <p>
  5136. <hr><h3><a name="pdf-io.input"><code>io.input ([file])</code></a></h3>
  5137. <p>
  5138. When called with a file name, it opens the named file (in text mode),
  5139. and sets its handle as the default input file.
  5140. When called with a file handle,
  5141. it simply sets this file handle as the default input file.
  5142. When called without parameters,
  5143. it returns the current default input file.
  5144. <p>
  5145. In case of errors this function raises the error,
  5146. instead of returning an error code.
  5147. <p>
  5148. <hr><h3><a name="pdf-io.lines"><code>io.lines ([filename])</code></a></h3>
  5149. <p>
  5150. Opens the given file name in read mode
  5151. and returns an iterator function that,
  5152. each time it is called,
  5153. returns a new line from the file.
  5154. Therefore, the construction
  5155. <pre>
  5156. for line in io.lines(filename) do <em>body</em> end
  5157. </pre><p>
  5158. will iterate over all lines of the file.
  5159. When the iterator function detects the end of file,
  5160. it returns <b>nil</b> (to finish the loop) and automatically closes the file.
  5161. <p>
  5162. The call <code>io.lines()</code> (with no file name) is equivalent
  5163. to <code>io.input():lines()</code>;
  5164. that is, it iterates over the lines of the default input file.
  5165. In this case it does not close the file when the loop ends.
  5166. <p>
  5167. <hr><h3><a name="pdf-io.open"><code>io.open (filename [, mode])</code></a></h3>
  5168. <p>
  5169. This function opens a file,
  5170. in the mode specified in the string <code>mode</code>.
  5171. It returns a new file handle,
  5172. or, in case of errors, <b>nil</b> plus an error message.
  5173. <p>
  5174. The <code>mode</code> string can be any of the following:
  5175. <ul>
  5176. <li><b>"r":</b> read mode (the default);</li>
  5177. <li><b>"w":</b> write mode;</li>
  5178. <li><b>"a":</b> append mode;</li>
  5179. <li><b>"r+":</b> update mode, all previous data is preserved;</li>
  5180. <li><b>"w+":</b> update mode, all previous data is erased;</li>
  5181. <li><b>"a+":</b> append update mode, previous data is preserved,
  5182. writing is only allowed at the end of file.</li>
  5183. </ul><p>
  5184. The <code>mode</code> string can also have a '<code>b</code>' at the end,
  5185. which is needed in some systems to open the file in binary mode.
  5186. This string is exactly what is used in the
  5187. standard&nbsp;C function <code>fopen</code>.
  5188. <p>
  5189. <hr><h3><a name="pdf-io.output"><code>io.output ([file])</code></a></h3>
  5190. <p>
  5191. Similar to <a href="#pdf-io.input"><code>io.input</code></a>, but operates over the default output file.
  5192. <p>
  5193. <hr><h3><a name="pdf-io.popen"><code>io.popen (prog [, mode])</code></a></h3>
  5194. <p>
  5195. Starts program <code>prog</code> in a separated process and returns
  5196. a file handle that you can use to read data from this program
  5197. (if <code>mode</code> is <code>"r"</code>, the default)
  5198. or to write data to this program
  5199. (if <code>mode</code> is <code>"w"</code>).
  5200. <p>
  5201. This function is system dependent and is not available
  5202. on all platforms.
  5203. <p>
  5204. <hr><h3><a name="pdf-io.read"><code>io.read (&middot;&middot;&middot;)</code></a></h3>
  5205. <p>
  5206. Equivalent to <code>io.input():read</code>.
  5207. <p>
  5208. <hr><h3><a name="pdf-io.tmpfile"><code>io.tmpfile ()</code></a></h3>
  5209. <p>
  5210. Returns a handle for a temporary file.
  5211. This file is opened in update mode
  5212. and it is automatically removed when the program ends.
  5213. <p>
  5214. <hr><h3><a name="pdf-io.type"><code>io.type (obj)</code></a></h3>
  5215. <p>
  5216. Checks whether <code>obj</code> is a valid file handle.
  5217. Returns the string <code>"file"</code> if <code>obj</code> is an open file handle,
  5218. <code>"closed file"</code> if <code>obj</code> is a closed file handle,
  5219. or <b>nil</b> if <code>obj</code> is not a file handle.
  5220. <p>
  5221. <hr><h3><a name="pdf-io.write"><code>io.write (&middot;&middot;&middot;)</code></a></h3>
  5222. <p>
  5223. Equivalent to <code>io.output():write</code>.
  5224. <p>
  5225. <hr><h3><a name="pdf-file:close"><code>file:close ()</code></a></h3>
  5226. <p>
  5227. Closes <code>file</code>.
  5228. Note that files are automatically closed when
  5229. their handles are garbage collected,
  5230. but that takes an unpredictable amount of time to happen.
  5231. <p>
  5232. <hr><h3><a name="pdf-file:flush"><code>file:flush ()</code></a></h3>
  5233. <p>
  5234. Saves any written data to <code>file</code>.
  5235. <p>
  5236. <hr><h3><a name="pdf-file:lines"><code>file:lines ()</code></a></h3>
  5237. <p>
  5238. Returns an iterator function that,
  5239. each time it is called,
  5240. returns a new line from the file.
  5241. Therefore, the construction
  5242. <pre>
  5243. for line in file:lines() do <em>body</em> end
  5244. </pre><p>
  5245. will iterate over all lines of the file.
  5246. (Unlike <a href="#pdf-io.lines"><code>io.lines</code></a>, this function does not close the file
  5247. when the loop ends.)
  5248. <p>
  5249. <hr><h3><a name="pdf-file:read"><code>file:read (&middot;&middot;&middot;)</code></a></h3>
  5250. <p>
  5251. Reads the file <code>file</code>,
  5252. according to the given formats, which specify what to read.
  5253. For each format,
  5254. the function returns a string (or a number) with the characters read,
  5255. or <b>nil</b> if it cannot read data with the specified format.
  5256. When called without formats,
  5257. it uses a default format that reads the entire next line
  5258. (see below).
  5259. <p>
  5260. The available formats are
  5261. <ul>
  5262. <li><b>"*n":</b>
  5263. reads a number;
  5264. this is the only format that returns a number instead of a string.
  5265. </li>
  5266. <li><b>"*a":</b>
  5267. reads the whole file, starting at the current position.
  5268. On end of file, it returns the empty string.
  5269. </li>
  5270. <li><b>"*l":</b>
  5271. reads the next line (skipping the end of line),
  5272. returning <b>nil</b> on end of file.
  5273. This is the default format.
  5274. </li>
  5275. <li><b><em>number</em>:</b>
  5276. reads a string with up to this number of characters,
  5277. returning <b>nil</b> on end of file.
  5278. If number is zero,
  5279. it reads nothing and returns an empty string,
  5280. or <b>nil</b> on end of file.
  5281. </li>
  5282. </ul>
  5283. <p>
  5284. <hr><h3><a name="pdf-file:seek"><code>file:seek ([whence] [, offset])</code></a></h3>
  5285. <p>
  5286. Sets and gets the file position,
  5287. measured from the beginning of the file,
  5288. to the position given by <code>offset</code> plus a base
  5289. specified by the string <code>whence</code>, as follows:
  5290. <ul>
  5291. <li><b>"set":</b> base is position 0 (beginning of the file);</li>
  5292. <li><b>"cur":</b> base is current position;</li>
  5293. <li><b>"end":</b> base is end of file;</li>
  5294. </ul><p>
  5295. In case of success, function <code>seek</code> returns the final file position,
  5296. measured in bytes from the beginning of the file.
  5297. If this function fails, it returns <b>nil</b>,
  5298. plus a string describing the error.
  5299. <p>
  5300. The default value for <code>whence</code> is <code>"cur"</code>,
  5301. and for <code>offset</code> is 0.
  5302. Therefore, the call <code>file:seek()</code> returns the current
  5303. file position, without changing it;
  5304. the call <code>file:seek("set")</code> sets the position to the
  5305. beginning of the file (and returns 0);
  5306. and the call <code>file:seek("end")</code> sets the position to the
  5307. end of the file, and returns its size.
  5308. <p>
  5309. <hr><h3><a name="pdf-file:setvbuf"><code>file:setvbuf (mode [, size])</code></a></h3>
  5310. <p>
  5311. Sets the buffering mode for an output file.
  5312. There are three available modes:
  5313. <ul>
  5314. <li><b>"no":</b>
  5315. no buffering; the result of any output operation appears immediately.
  5316. </li>
  5317. <li><b>"full":</b>
  5318. full buffering; output operation is performed only
  5319. when the buffer is full (or when you explicitly <code>flush</code> the file
  5320. (see <a href="#pdf-io.flush"><code>io.flush</code></a>)).
  5321. </li>
  5322. <li><b>"line":</b>
  5323. line buffering; output is buffered until a newline is output
  5324. or there is any input from some special files
  5325. (such as a terminal device).
  5326. </li>
  5327. </ul><p>
  5328. For the last two cases, <code>size</code>
  5329. specifies the size of the buffer, in bytes.
  5330. The default is an appropriate size.
  5331. <p>
  5332. <hr><h3><a name="pdf-file:write"><code>file:write (&middot;&middot;&middot;)</code></a></h3>
  5333. <p>
  5334. Writes the value of each of its arguments to
  5335. the <code>file</code>.
  5336. The arguments must be strings or numbers.
  5337. To write other values,
  5338. use <a href="#pdf-tostring"><code>tostring</code></a> or <a href="#pdf-string.format"><code>string.format</code></a> before <code>write</code>.
  5339. <h2>5.8 - <a name="5.8">Operating System Facilities</a></h2>
  5340. <p>
  5341. This library is implemented through table <a name="pdf-os"><code>os</code></a>.
  5342. <p>
  5343. <hr><h3><a name="pdf-os.clock"><code>os.clock ()</code></a></h3>
  5344. <p>
  5345. Returns an approximation of the amount in seconds of CPU time
  5346. used by the program.
  5347. <p>
  5348. <hr><h3><a name="pdf-os.date"><code>os.date ([format [, time]])</code></a></h3>
  5349. <p>
  5350. Returns a string or a table containing date and time,
  5351. formatted according to the given string <code>format</code>.
  5352. <p>
  5353. If the <code>time</code> argument is present,
  5354. this is the time to be formatted
  5355. (see the <a href="#pdf-os.time"><code>os.time</code></a> function for a description of this value).
  5356. Otherwise, <code>date</code> formats the current time.
  5357. <p>
  5358. If <code>format</code> starts with '<code>!</code>',
  5359. then the date is formatted in Coordinated Universal Time.
  5360. After this optional character,
  5361. if <code>format</code> is the string "<code>*t</code>",
  5362. then <code>date</code> returns a table with the following fields:
  5363. <code>year</code> (four digits), <code>month</code> (1--12), <code>day</code> (1--31),
  5364. <code>hour</code> (0--23), <code>min</code> (0--59), <code>sec</code> (0--61),
  5365. <code>wday</code> (weekday, Sunday is&nbsp;1),
  5366. <code>yday</code> (day of the year),
  5367. and <code>isdst</code> (daylight saving flag, a boolean).
  5368. <p>
  5369. If <code>format</code> is not "<code>*t</code>",
  5370. then <code>date</code> returns the date as a string,
  5371. formatted according to the same rules as the C&nbsp;function <code>strftime</code>.
  5372. <p>
  5373. When called without arguments,
  5374. <code>date</code> returns a reasonable date and time representation that depends on
  5375. the host system and on the current locale
  5376. (that is, <code>os.date()</code> is equivalent to <code>os.date("%c")</code>).
  5377. <p>
  5378. <hr><h3><a name="pdf-os.difftime"><code>os.difftime (t2, t1)</code></a></h3>
  5379. <p>
  5380. Returns the number of seconds from time <code>t1</code> to time <code>t2</code>.
  5381. In POSIX, Windows, and some other systems,
  5382. this value is exactly <code>t2</code><em>-</em><code>t1</code>.
  5383. <p>
  5384. <hr><h3><a name="pdf-os.execute"><code>os.execute ([command])</code></a></h3>
  5385. <p>
  5386. This function is equivalent to the C&nbsp;function <code>system</code>.
  5387. It passes <code>command</code> to be executed by an operating system shell.
  5388. It returns a status code, which is system-dependent.
  5389. If <code>command</code> is absent, then it returns nonzero if a shell is available
  5390. and zero otherwise.
  5391. <p>
  5392. <hr><h3><a name="pdf-os.exit"><code>os.exit ([code])</code></a></h3>
  5393. <p>
  5394. Calls the C&nbsp;function <code>exit</code>,
  5395. with an optional <code>code</code>,
  5396. to terminate the host program.
  5397. The default value for <code>code</code> is the success code.
  5398. <p>
  5399. <hr><h3><a name="pdf-os.getenv"><code>os.getenv (varname)</code></a></h3>
  5400. <p>
  5401. Returns the value of the process environment variable <code>varname</code>,
  5402. or <b>nil</b> if the variable is not defined.
  5403. <p>
  5404. <hr><h3><a name="pdf-os.remove"><code>os.remove (filename)</code></a></h3>
  5405. <p>
  5406. Deletes the file or directory with the given name.
  5407. Directories must be empty to be removed.
  5408. If this function fails, it returns <b>nil</b>,
  5409. plus a string describing the error.
  5410. <p>
  5411. <hr><h3><a name="pdf-os.rename"><code>os.rename (oldname, newname)</code></a></h3>
  5412. <p>
  5413. Renames file or directory named <code>oldname</code> to <code>newname</code>.
  5414. If this function fails, it returns <b>nil</b>,
  5415. plus a string describing the error.
  5416. <p>
  5417. <hr><h3><a name="pdf-os.setlocale"><code>os.setlocale (locale [, category])</code></a></h3>
  5418. <p>
  5419. Sets the current locale of the program.
  5420. <code>locale</code> is a string specifying a locale;
  5421. <code>category</code> is an optional string describing which category to change:
  5422. <code>"all"</code>, <code>"collate"</code>, <code>"ctype"</code>,
  5423. <code>"monetary"</code>, <code>"numeric"</code>, or <code>"time"</code>;
  5424. the default category is <code>"all"</code>.
  5425. The function returns the name of the new locale,
  5426. or <b>nil</b> if the request cannot be honored.
  5427. <p>
  5428. If <code>locale</code> is the empty string,
  5429. the current locale is set to an implementation-defined native locale.
  5430. If <code>locale</code> is the string "<code>C</code>",
  5431. the current locale is set to the standard C locale.
  5432. <p>
  5433. When called with <b>nil</b> as the first argument,
  5434. this function only returns the name of the current locale
  5435. for the given category.
  5436. <p>
  5437. <hr><h3><a name="pdf-os.time"><code>os.time ([table])</code></a></h3>
  5438. <p>
  5439. Returns the current time when called without arguments,
  5440. or a time representing the date and time specified by the given table.
  5441. This table must have fields <code>year</code>, <code>month</code>, and <code>day</code>,
  5442. and may have fields <code>hour</code>, <code>min</code>, <code>sec</code>, and <code>isdst</code>
  5443. (for a description of these fields, see the <a href="#pdf-os.date"><code>os.date</code></a> function).
  5444. <p>
  5445. The returned value is a number, whose meaning depends on your system.
  5446. In POSIX, Windows, and some other systems, this number counts the number
  5447. of seconds since some given start time (the "epoch").
  5448. In other systems, the meaning is not specified,
  5449. and the number returned by <code>time</code> can be used only as an argument to
  5450. <code>date</code> and <code>difftime</code>.
  5451. <p>
  5452. <hr><h3><a name="pdf-os.tmpname"><code>os.tmpname ()</code></a></h3>
  5453. <p>
  5454. Returns a string with a file name that can
  5455. be used for a temporary file.
  5456. The file must be explicitly opened before its use
  5457. and explicitly removed when no longer needed.
  5458. <p>
  5459. On some systems (POSIX),
  5460. this function also creates a file with that name,
  5461. to avoid security risks.
  5462. (Someone else might create the file with wrong permissions
  5463. in the time between getting the name and creating the file.)
  5464. You still have to open the file to use it
  5465. and to remove it (even if you do not use it).
  5466. <p>
  5467. When possible,
  5468. you may prefer to use <a href="#pdf-io.tmpfile"><code>io.tmpfile</code></a>,
  5469. which automatically removes the file when the program ends.
  5470. <h2>5.9 - <a name="5.9">The Debug Library</a></h2>
  5471. <p>
  5472. This library provides
  5473. the functionality of the debug interface to Lua programs.
  5474. You should exert care when using this library.
  5475. The functions provided here should be used exclusively for debugging
  5476. and similar tasks, such as profiling.
  5477. Please resist the temptation to use them as a
  5478. usual programming tool:
  5479. they can be very slow.
  5480. Moreover, several of these functions
  5481. violate some assumptions about Lua code
  5482. (e.g., that variables local to a function
  5483. cannot be accessed from outside or
  5484. that userdata metatables cannot be changed by Lua code)
  5485. and therefore can compromise otherwise secure code.
  5486. <p>
  5487. All functions in this library are provided
  5488. inside the <a name="pdf-debug"><code>debug</code></a> table.
  5489. All functions that operate over a thread
  5490. have an optional first argument which is the
  5491. thread to operate over.
  5492. The default is always the current thread.
  5493. <p>
  5494. <hr><h3><a name="pdf-debug.debug"><code>debug.debug ()</code></a></h3>
  5495. <p>
  5496. Enters an interactive mode with the user,
  5497. running each string that the user enters.
  5498. Using simple commands and other debug facilities,
  5499. the user can inspect global and local variables,
  5500. change their values, evaluate expressions, and so on.
  5501. A line containing only the word <code>cont</code> finishes this function,
  5502. so that the caller continues its execution.
  5503. <p>
  5504. Note that commands for <code>debug.debug</code> are not lexically nested
  5505. within any function, and so have no direct access to local variables.
  5506. <p>
  5507. <hr><h3><a name="pdf-debug.getfenv"><code>debug.getfenv (o)</code></a></h3>
  5508. Returns the environment of object <code>o</code>.
  5509. <p>
  5510. <hr><h3><a name="pdf-debug.gethook"><code>debug.gethook ([thread])</code></a></h3>
  5511. <p>
  5512. Returns the current hook settings of the thread, as three values:
  5513. the current hook function, the current hook mask,
  5514. and the current hook count
  5515. (as set by the <a href="#pdf-debug.sethook"><code>debug.sethook</code></a> function).
  5516. <p>
  5517. <hr><h3><a name="pdf-debug.getinfo"><code>debug.getinfo ([thread,] function [, what])</code></a></h3>
  5518. <p>
  5519. Returns a table with information about a function.
  5520. You can give the function directly,
  5521. or you can give a number as the value of <code>function</code>,
  5522. which means the function running at level <code>function</code> of the call stack
  5523. of the given thread:
  5524. level&nbsp;0 is the current function (<code>getinfo</code> itself);
  5525. level&nbsp;1 is the function that called <code>getinfo</code>;
  5526. and so on.
  5527. If <code>function</code> is a number larger than the number of active functions,
  5528. then <code>getinfo</code> returns <b>nil</b>.
  5529. <p>
  5530. The returned table can contain all the fields returned by <a href="#lua_getinfo"><code>lua_getinfo</code></a>,
  5531. with the string <code>what</code> describing which fields to fill in.
  5532. The default for <code>what</code> is to get all information available,
  5533. except the table of valid lines.
  5534. If present,
  5535. the option '<code>f</code>'
  5536. adds a field named <code>func</code> with the function itself.
  5537. If present,
  5538. the option '<code>L</code>'
  5539. adds a field named <code>activelines</code> with the table of
  5540. valid lines.
  5541. <p>
  5542. For instance, the expression <code>debug.getinfo(1,"n").name</code> returns
  5543. a table with a name for the current function,
  5544. if a reasonable name can be found,
  5545. and the expression <code>debug.getinfo(print)</code>
  5546. returns a table with all available information
  5547. about the <a href="#pdf-print"><code>print</code></a> function.
  5548. <p>
  5549. <hr><h3><a name="pdf-debug.getlocal"><code>debug.getlocal ([thread,] level, local)</code></a></h3>
  5550. <p>
  5551. This function returns the name and the value of the local variable
  5552. with index <code>local</code> of the function at level <code>level</code> of the stack.
  5553. (The first parameter or local variable has index&nbsp;1, and so on,
  5554. until the last active local variable.)
  5555. The function returns <b>nil</b> if there is no local
  5556. variable with the given index,
  5557. and raises an error when called with a <code>level</code> out of range.
  5558. (You can call <a href="#pdf-debug.getinfo"><code>debug.getinfo</code></a> to check whether the level is valid.)
  5559. <p>
  5560. Variable names starting with '<code>(</code>' (open parentheses)
  5561. represent internal variables
  5562. (loop control variables, temporaries, and C&nbsp;function locals).
  5563. <p>
  5564. <hr><h3><a name="pdf-debug.getmetatable"><code>debug.getmetatable (object)</code></a></h3>
  5565. <p>
  5566. Returns the metatable of the given <code>object</code>
  5567. or <b>nil</b> if it does not have a metatable.
  5568. <p>
  5569. <hr><h3><a name="pdf-debug.getregistry"><code>debug.getregistry ()</code></a></h3>
  5570. <p>
  5571. Returns the registry table (see <a href="#3.5">&sect;3.5</a>).
  5572. <p>
  5573. <hr><h3><a name="pdf-debug.getupvalue"><code>debug.getupvalue (func, up)</code></a></h3>
  5574. <p>
  5575. This function returns the name and the value of the upvalue
  5576. with index <code>up</code> of the function <code>func</code>.
  5577. The function returns <b>nil</b> if there is no upvalue with the given index.
  5578. <p>
  5579. <hr><h3><a name="pdf-debug.setfenv"><code>debug.setfenv (object, table)</code></a></h3>
  5580. <p>
  5581. Sets the environment of the given <code>object</code> to the given <code>table</code>.
  5582. Returns <code>object</code>.
  5583. <p>
  5584. <hr><h3><a name="pdf-debug.sethook"><code>debug.sethook ([thread,] hook, mask [, count])</code></a></h3>
  5585. <p>
  5586. Sets the given function as a hook.
  5587. The string <code>mask</code> and the number <code>count</code> describe
  5588. when the hook will be called.
  5589. The string mask may have the following characters,
  5590. with the given meaning:
  5591. <ul>
  5592. <li><b><code>"c"</code>:</b> the hook is called every time Lua calls a function;</li>
  5593. <li><b><code>"r"</code>:</b> the hook is called every time Lua returns from a function;</li>
  5594. <li><b><code>"l"</code>:</b> the hook is called every time Lua enters a new line of code.</li>
  5595. </ul><p>
  5596. With a <code>count</code> different from zero,
  5597. the hook is called after every <code>count</code> instructions.
  5598. <p>
  5599. When called without arguments,
  5600. <a href="#pdf-debug.sethook"><code>debug.sethook</code></a> turns off the hook.
  5601. <p>
  5602. When the hook is called, its first parameter is a string
  5603. describing the event that has triggered its call:
  5604. <code>"call"</code>, <code>"return"</code> (or <code>"tail return"</code>,
  5605. when simulating a return from a tail call),
  5606. <code>"line"</code>, and <code>"count"</code>.
  5607. For line events,
  5608. the hook also gets the new line number as its second parameter.
  5609. Inside a hook,
  5610. you can call <code>getinfo</code> with level&nbsp;2 to get more information about
  5611. the running function
  5612. (level&nbsp;0 is the <code>getinfo</code> function,
  5613. and level&nbsp;1 is the hook function),
  5614. unless the event is <code>"tail return"</code>.
  5615. In this case, Lua is only simulating the return,
  5616. and a call to <code>getinfo</code> will return invalid data.
  5617. <p>
  5618. <hr><h3><a name="pdf-debug.setlocal"><code>debug.setlocal ([thread,] level, local, value)</code></a></h3>
  5619. <p>
  5620. This function assigns the value <code>value</code> to the local variable
  5621. with index <code>local</code> of the function at level <code>level</code> of the stack.
  5622. The function returns <b>nil</b> if there is no local
  5623. variable with the given index,
  5624. and raises an error when called with a <code>level</code> out of range.
  5625. (You can call <code>getinfo</code> to check whether the level is valid.)
  5626. Otherwise, it returns the name of the local variable.
  5627. <p>
  5628. <hr><h3><a name="pdf-debug.setmetatable"><code>debug.setmetatable (object, table)</code></a></h3>
  5629. <p>
  5630. Sets the metatable for the given <code>object</code> to the given <code>table</code>
  5631. (which can be <b>nil</b>).
  5632. <p>
  5633. <hr><h3><a name="pdf-debug.setupvalue"><code>debug.setupvalue (func, up, value)</code></a></h3>
  5634. <p>
  5635. This function assigns the value <code>value</code> to the upvalue
  5636. with index <code>up</code> of the function <code>func</code>.
  5637. The function returns <b>nil</b> if there is no upvalue
  5638. with the given index.
  5639. Otherwise, it returns the name of the upvalue.
  5640. <p>
  5641. <hr><h3><a name="pdf-debug.traceback"><code>debug.traceback ([thread,] [message [, level]])</code></a></h3>
  5642. <p>
  5643. Returns a string with a traceback of the call stack.
  5644. An optional <code>message</code> string is appended
  5645. at the beginning of the traceback.
  5646. An optional <code>level</code> number tells at which level
  5647. to start the traceback
  5648. (default is 1, the function calling <code>traceback</code>).
  5649. <h1>6 - <a name="6">Lua Stand-alone</a></h1>
  5650. <p>
  5651. Although Lua has been designed as an extension language,
  5652. to be embedded in a host C&nbsp;program,
  5653. it is also frequently used as a stand-alone language.
  5654. An interpreter for Lua as a stand-alone language,
  5655. called simply <code>lua</code>,
  5656. is provided with the standard distribution.
  5657. The stand-alone interpreter includes
  5658. all standard libraries, including the debug library.
  5659. Its usage is:
  5660. <pre>
  5661. lua [options] [script [args]]
  5662. </pre><p>
  5663. The options are:
  5664. <ul>
  5665. <li><b><code>-e <em>stat</em></code>:</b> executes string <em>stat</em>;</li>
  5666. <li><b><code>-l <em>mod</em></code>:</b> "requires" <em>mod</em>;</li>
  5667. <li><b><code>-i</code>:</b> enters interactive mode after running <em>script</em>;</li>
  5668. <li><b><code>-v</code>:</b> prints version information;</li>
  5669. <li><b><code>--</code>:</b> stops handling options;</li>
  5670. <li><b><code>-</code>:</b> executes <code>stdin</code> as a file and stops handling options.</li>
  5671. </ul><p>
  5672. After handling its options, <code>lua</code> runs the given <em>script</em>,
  5673. passing to it the given <em>args</em> as string arguments.
  5674. When called without arguments,
  5675. <code>lua</code> behaves as <code>lua -v -i</code>
  5676. when the standard input (<code>stdin</code>) is a terminal,
  5677. and as <code>lua -</code> otherwise.
  5678. <p>
  5679. Before running any argument,
  5680. the interpreter checks for an environment variable <a name="pdf-LUA_INIT"><code>LUA_INIT</code></a>.
  5681. If its format is <code>@<em>filename</em></code>,
  5682. then <code>lua</code> executes the file.
  5683. Otherwise, <code>lua</code> executes the string itself.
  5684. <p>
  5685. All options are handled in order, except <code>-i</code>.
  5686. For instance, an invocation like
  5687. <pre>
  5688. $ lua -e'a=1' -e 'print(a)' script.lua
  5689. </pre><p>
  5690. will first set <code>a</code> to 1, then print the value of <code>a</code> (which is '<code>1</code>'),
  5691. and finally run the file <code>script.lua</code> with no arguments.
  5692. (Here <code>$</code> is the shell prompt. Your prompt may be different.)
  5693. <p>
  5694. Before starting to run the script,
  5695. <code>lua</code> collects all arguments in the command line
  5696. in a global table called <code>arg</code>.
  5697. The script name is stored at index 0,
  5698. the first argument after the script name goes to index 1,
  5699. and so on.
  5700. Any arguments before the script name
  5701. (that is, the interpreter name plus the options)
  5702. go to negative indices.
  5703. For instance, in the call
  5704. <pre>
  5705. $ lua -la b.lua t1 t2
  5706. </pre><p>
  5707. the interpreter first runs the file <code>a.lua</code>,
  5708. then creates a table
  5709. <pre>
  5710. arg = { [-2] = "lua", [-1] = "-la",
  5711. [0] = "b.lua",
  5712. [1] = "t1", [2] = "t2" }
  5713. </pre><p>
  5714. and finally runs the file <code>b.lua</code>.
  5715. The script is called with <code>arg[1]</code>, <code>arg[2]</code>, &middot;&middot;&middot;
  5716. as arguments;
  5717. it can also access these arguments with the vararg expression '<code>...</code>'.
  5718. <p>
  5719. In interactive mode,
  5720. if you write an incomplete statement,
  5721. the interpreter waits for its completion
  5722. by issuing a different prompt.
  5723. <p>
  5724. If the global variable <a name="pdf-_PROMPT"><code>_PROMPT</code></a> contains a string,
  5725. then its value is used as the prompt.
  5726. Similarly, if the global variable <a name="pdf-_PROMPT2"><code>_PROMPT2</code></a> contains a string,
  5727. its value is used as the secondary prompt
  5728. (issued during incomplete statements).
  5729. Therefore, both prompts can be changed directly on the command line
  5730. or in any Lua programs by assigning to <code>_PROMPT</code>.
  5731. See the next example:
  5732. <pre>
  5733. $ lua -e"_PROMPT='myprompt&gt; '" -i
  5734. </pre><p>
  5735. (The outer pair of quotes is for the shell,
  5736. the inner pair is for Lua.)
  5737. Note the use of <code>-i</code> to enter interactive mode;
  5738. otherwise,
  5739. the program would just end silently
  5740. right after the assignment to <code>_PROMPT</code>.
  5741. <p>
  5742. To allow the use of Lua as a
  5743. script interpreter in Unix systems,
  5744. the stand-alone interpreter skips
  5745. the first line of a chunk if it starts with <code>#</code>.
  5746. Therefore, Lua scripts can be made into executable programs
  5747. by using <code>chmod +x</code> and the&nbsp;<code>#!</code> form,
  5748. as in
  5749. <pre>
  5750. #!/usr/local/bin/lua
  5751. </pre><p>
  5752. (Of course,
  5753. the location of the Lua interpreter may be different in your machine.
  5754. If <code>lua</code> is in your <code>PATH</code>,
  5755. then
  5756. <pre>
  5757. #!/usr/bin/env lua
  5758. </pre><p>
  5759. is a more portable solution.)
  5760. <h1>7 - <a name="7">Incompatibilities with the Previous Version</a></h1>
  5761. <p>
  5762. Here we list the incompatibilities that you may find when moving a program
  5763. from Lua&nbsp;5.0 to Lua&nbsp;5.1.
  5764. You can avoid most of the incompatibilities compiling Lua with
  5765. appropriate options (see file <code>luaconf.h</code>).
  5766. However,
  5767. all these compatibility options will be removed in the next version of Lua.
  5768. <h2>7.1 - <a name="7.1">Changes in the Language</a></h2>
  5769. <ul>
  5770. <li>
  5771. The vararg system changed from the pseudo-argument <code>arg</code> with a
  5772. table with the extra arguments to the vararg expression.
  5773. (See compile-time option <code>LUA_COMPAT_VARARG</code> in <code>luaconf.h</code>.)
  5774. </li>
  5775. <li>
  5776. There was a subtle change in the scope of the implicit
  5777. variables of the <b>for</b> statement and for the <b>repeat</b> statement.
  5778. </li>
  5779. <li>
  5780. The long string/long comment syntax (<code>[[<em>string</em>]]</code>)
  5781. does not allow nesting.
  5782. You can use the new syntax (<code>[=[<em>string</em>]=]</code>) in these cases.
  5783. (See compile-time option <code>LUA_COMPAT_LSTR</code> in <code>luaconf.h</code>.)
  5784. </li>
  5785. </ul>
  5786. <h2>7.2 - <a name="7.2">Changes in the Libraries</a></h2>
  5787. <ul>
  5788. <li>
  5789. Function <code>string.gfind</code> was renamed <a href="#pdf-string.gmatch"><code>string.gmatch</code></a>.
  5790. (See compile-time option <code>LUA_COMPAT_GFIND</code> in <code>luaconf.h</code>.)
  5791. </li>
  5792. <li>
  5793. When <a href="#pdf-string.gsub"><code>string.gsub</code></a> is called with a function as its
  5794. third argument,
  5795. whenever this function returns <b>nil</b> or <b>false</b> the
  5796. replacement string is the whole match,
  5797. instead of the empty string.
  5798. </li>
  5799. <li>
  5800. Function <code>table.setn</code> was deprecated.
  5801. Function <code>table.getn</code> corresponds
  5802. to the new length operator (<code>#</code>);
  5803. use the operator instead of the function.
  5804. (See compile-time option <code>LUA_COMPAT_GETN</code> in <code>luaconf.h</code>.)
  5805. </li>
  5806. <li>
  5807. Function <code>loadlib</code> was renamed <a href="#pdf-package.loadlib"><code>package.loadlib</code></a>.
  5808. (See compile-time option <code>LUA_COMPAT_LOADLIB</code> in <code>luaconf.h</code>.)
  5809. </li>
  5810. <li>
  5811. Function <code>math.mod</code> was renamed <a href="#pdf-math.fmod"><code>math.fmod</code></a>.
  5812. (See compile-time option <code>LUA_COMPAT_MOD</code> in <code>luaconf.h</code>.)
  5813. </li>
  5814. <li>
  5815. Functions <code>table.foreach</code> and <code>table.foreachi</code> are deprecated.
  5816. You can use a for loop with <code>pairs</code> or <code>ipairs</code> instead.
  5817. </li>
  5818. <li>
  5819. There were substantial changes in function <a href="#pdf-require"><code>require</code></a> due to
  5820. the new module system.
  5821. However, the new behavior is mostly compatible with the old,
  5822. but <code>require</code> gets the path from <a href="#pdf-package.path"><code>package.path</code></a> instead
  5823. of from <code>LUA_PATH</code>.
  5824. </li>
  5825. <li>
  5826. Function <a href="#pdf-collectgarbage"><code>collectgarbage</code></a> has different arguments.
  5827. Function <code>gcinfo</code> is deprecated;
  5828. use <code>collectgarbage("count")</code> instead.
  5829. </li>
  5830. </ul>
  5831. <h2>7.3 - <a name="7.3">Changes in the API</a></h2>
  5832. <ul>
  5833. <li>
  5834. The <code>luaopen_*</code> functions (to open libraries)
  5835. cannot be called directly,
  5836. like a regular C function.
  5837. They must be called through Lua,
  5838. like a Lua function.
  5839. </li>
  5840. <li>
  5841. Function <code>lua_open</code> was replaced by <a href="#lua_newstate"><code>lua_newstate</code></a> to
  5842. allow the user to set a memory-allocation function.
  5843. You can use <a href="#luaL_newstate"><code>luaL_newstate</code></a> from the standard library to
  5844. create a state with a standard allocation function
  5845. (based on <code>realloc</code>).
  5846. </li>
  5847. <li>
  5848. Functions <code>luaL_getn</code> and <code>luaL_setn</code>
  5849. (from the auxiliary library) are deprecated.
  5850. Use <a href="#lua_objlen"><code>lua_objlen</code></a> instead of <code>luaL_getn</code>
  5851. and nothing instead of <code>luaL_setn</code>.
  5852. </li>
  5853. <li>
  5854. Function <code>luaL_openlib</code> was replaced by <a href="#luaL_register"><code>luaL_register</code></a>.
  5855. </li>
  5856. <li>
  5857. Function <code>luaL_checkudata</code> now throws an error when the given value
  5858. is not a userdata of the expected type.
  5859. (In Lua&nbsp;5.0 it returned <code>NULL</code>.)
  5860. </li>
  5861. </ul>
  5862. <h1>8 - <a name="8">The Complete Syntax of Lua</a></h1>
  5863. <p>
  5864. Here is the complete syntax of Lua in extended BNF.
  5865. (It does not describe operator precedences.)
  5866. <pre>
  5867. chunk ::= {stat [`<b>;</b>&acute;]} [laststat [`<b>;</b>&acute;]]
  5868. block ::= chunk
  5869. stat ::= varlist `<b>=</b>&acute; explist |
  5870. functioncall |
  5871. <b>do</b> block <b>end</b> |
  5872. <b>while</b> exp <b>do</b> block <b>end</b> |
  5873. <b>repeat</b> block <b>until</b> exp |
  5874. <b>if</b> exp <b>then</b> block {<b>elseif</b> exp <b>then</b> block} [<b>else</b> block] <b>end</b> |
  5875. <b>for</b> Name `<b>=</b>&acute; exp `<b>,</b>&acute; exp [`<b>,</b>&acute; exp] <b>do</b> block <b>end</b> |
  5876. <b>for</b> namelist <b>in</b> explist <b>do</b> block <b>end</b> |
  5877. <b>function</b> funcname funcbody |
  5878. <b>local</b> <b>function</b> Name funcbody |
  5879. <b>local</b> namelist [`<b>=</b>&acute; explist]
  5880. laststat ::= <b>return</b> [explist] | <b>break</b>
  5881. funcname ::= Name {`<b>.</b>&acute; Name} [`<b>:</b>&acute; Name]
  5882. varlist ::= var {`<b>,</b>&acute; var}
  5883. var ::= Name | prefixexp `<b>[</b>&acute; exp `<b>]</b>&acute; | prefixexp `<b>.</b>&acute; Name
  5884. namelist ::= Name {`<b>,</b>&acute; Name}
  5885. explist ::= {exp `<b>,</b>&acute;} exp
  5886. exp ::= <b>nil</b> | <b>false</b> | <b>true</b> | Number | String | `<b>...</b>&acute; | function |
  5887. prefixexp | tableconstructor | exp binop exp | unop exp
  5888. prefixexp ::= var | functioncall | `<b>(</b>&acute; exp `<b>)</b>&acute;
  5889. functioncall ::= prefixexp args | prefixexp `<b>:</b>&acute; Name args
  5890. args ::= `<b>(</b>&acute; [explist] `<b>)</b>&acute; | tableconstructor | String
  5891. function ::= <b>function</b> funcbody
  5892. funcbody ::= `<b>(</b>&acute; [parlist] `<b>)</b>&acute; block <b>end</b>
  5893. parlist ::= namelist [`<b>,</b>&acute; `<b>...</b>&acute;] | `<b>...</b>&acute;
  5894. tableconstructor ::= `<b>{</b>&acute; [fieldlist] `<b>}</b>&acute;
  5895. fieldlist ::= field {fieldsep field} [fieldsep]
  5896. field ::= `<b>[</b>&acute; exp `<b>]</b>&acute; `<b>=</b>&acute; exp | Name `<b>=</b>&acute; exp | exp
  5897. fieldsep ::= `<b>,</b>&acute; | `<b>;</b>&acute;
  5898. binop ::= `<b>+</b>&acute; | `<b>-</b>&acute; | `<b>*</b>&acute; | `<b>/</b>&acute; | `<b>^</b>&acute; | `<b>%</b>&acute; | `<b>..</b>&acute; |
  5899. `<b>&lt;</b>&acute; | `<b>&lt;=</b>&acute; | `<b>&gt;</b>&acute; | `<b>&gt;=</b>&acute; | `<b>==</b>&acute; | `<b>~=</b>&acute; |
  5900. <b>and</b> | <b>or</b>
  5901. unop ::= `<b>-</b>&acute; | <b>not</b> | `<b>#</b>&acute;
  5902. </pre>
  5903. <p>
  5904. <HR>
  5905. <SMALL CLASS="footer">
  5906. Last update:
  5907. Mon Feb 13 18:54:19 BRST 2012
  5908. </SMALL>
  5909. <!--
  5910. Last change: revised for Lua 5.1.5
  5911. -->
  5912. </body></html>